source: client/engine/FileSystem.lib @ 713af37

918-git-images-111dconfigure-oglivegit-imageslgromero-new-oglivemainmaint-cronmount-efivarfsmultivmmultivm-ogboot-installerogClonningEngineogboot-installer-jenkinsoglive-ipv6test-python-scriptsticket-301ticket-50ticket-50-oldticket-577ticket-585ticket-611ticket-612ticket-693ticket-700ubu24tplunification2use-local-agent-oglivevarios-instalacion
Last change on this file since 713af37 was f097c21, checked in by Irina Gómez <irinagomez@…>, 6 years ago

Fix initCache: if the size of the cache changes, it always formats the cache. ogCheckFs includes the CACHE file system type

  • Property mode set to 100755
File size: 39.2 KB
Line 
1#!/bin/bash
2#/**
3#@file    FileSystem.lib
4#@brief   Librería o clase FileSystem
5#@class   FileSystem
6#@brief   Funciones para gestión de sistemas de archivos.
7#@version 1.1.0
8#@warning License: GNU GPLv3+
9#*/
10
11
12#/**
13#         ogCheckFs int_ndisk int_nfilesys
14#@brief   Comprueba el estado de un sistema de archivos.
15#@param   int_ndisk      nº de orden del disco
16#@param   int_nfilesys   nº de orden del sistema de archivos
17#@return  (nada)
18#@exception OG_ERR_FORMAT    Formato incorrecto.
19#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
20#@exception OG_ERR_PARTITION Partición desconocida o no accesible.
21#@note    Requisitos: *fsck*
22#@warning No se comprueban sistemas de archivos montados o bloqueados.
23#@todo    Definir salidas.
24#@version 0.9 - Primera adaptación para OpenGnSys.
25#@author  Ramon Gomez, ETSII Universidad de Sevilla
26#@date    2009-10-07
27#@version 1.0.2 - Ignorar códigos de salida de comprobación (no erróneos).
28#@author  Ramon Gomez, ETSII Universidad de Sevilla
29#@date    2011-09-23
30#@version 1.0.4 - Soportar HFS/HFS+.
31#@author  Ramon Gomez, ETSII Universidad de Sevilla
32#@date    2012-05-21
33#@version 1.0.5 - Desmontar antes de comprobar, soportar Btrfs y ExFAT.
34#@author  Ramon Gomez, ETSII Universidad de Sevilla
35#@date    2012-09-05
36#@version 1.1.0 - Soportar F2FS.
37#@author  Ramon Gomez, ETSII Universidad de Sevilla
38#@date    2016-05-03
39#*/ ##
40function ogCheckFs ()
41{
42# Variables locales.
43local PART TYPE PROG PARAMS CODES ERRCODE
44# Si se solicita, mostrar ayuda.
45if [ "$*" == "help" ]; then
46    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_nfilesys" \
47           "$FUNCNAME 1 1"
48    return
49fi
50
51# Error si no se reciben 2 parámetros.
52[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
53# Obtener partición.
54PART="$(ogDiskToDev $1 $2)" || return $?
55
56TYPE=$(ogGetFsType $1 $2)
57case "$TYPE" in
58    EXT[234]|CACHE) PROG="e2fsck"; PARAMS="-y"; CODES=(1 2) ;;
59    BTRFS)          PROG="btrfsck"; CODES=(1) ;;
60    REISERFS)       PROG="fsck.reiserfs"; PARAMS="<<<\"Yes\""; CODES=(1 2) ;;
61    REISER4)        PROG="fsck.reiser4"; PARAMS="-ay" ;;
62    JFS)            PROG="fsck.jfs"; CODES=(1 2) ;;
63    XFS)            PROG="xfs_repair" ;;
64    F2FS)           PROG="fsck.f2fs" ;;
65    NTFS)           PROG="ntfsfix" ;;
66    EXFAT)          PROG="fsck.exfat" ;;
67    FAT32)          PROG="dosfsck"; PARAMS="-a"; CODES=(1) ;;
68    FAT16)          PROG="dosfsck"; PARAMS="-a"; CODES=(1) ;;
69    FAT12)          PROG="dosfsck"; PARAMS="-a"; CODES=(1) ;;
70    HFS)            PROG="fsck.hfs"; PARAMS="-f" ;;
71    HFSPLUS)        PROG="fsck.hfs"; PARAMS="-f" ;;
72    UFS)            PROG="fsck.ufs" ;;
73    ZFS)            PROG="fsck.zfs" ;;
74    *)              ogRaiseError $OG_ERR_PARTITION "$1, $2, $TYPE"
75                    return $? ;;
76esac
77# Error si el sistema de archivos esta montado o bloqueado.
78ogUnmount $1 $2
79if ogIsMounted $1 $2; then
80    ogRaiseError $OG_ERR_PARTITION "$1 $2"       # Indicar nuevo error
81    return $?
82fi
83if ogIsLocked $1 $2; then
84    ogRaiseError $OG_ERR_LOCKED "$1 $2"
85    return $?
86fi
87# Comprobar en modo uso exclusivo.
88ogLock $1 $2
89trap "ogUnlock $1 $2" 1 2 3 6 9
90eval $PROG $PARAMS $PART
91ERRCODE=$?
92case $ERRCODE in
93    0|${CODES[*]})
94            ERRCODE=0 ;;
95    127)    ogRaiseError $OG_ERR_NOTEXEC "$PROG"
96            ERRCODE=$OG_ERR_NOTEXEC ;;
97    *)      ogRaiseError $OG_ERR_PARTITION "$1 $2"
98            ERRCODE=$OG_ERR_PARTITION ;;
99esac
100ogUnlock $1 $2
101return $ERRCODE
102}
103
104
105#/**
106#         ogExtendFs int_ndisk int_nfilesys
107#@brief   Extiende un sistema de archivos al tamaño de su partición.
108#@param   int_ndisk      nº de orden del disco
109#@param   int_nfilesys   nº de orden del sistema de archivos
110#@return  (nada)
111#@exception OG_ERR_FORMAT   Formato incorrecto.
112#@exception OG_ERR_NOTFOUND Disco o particion no corresponden con un dispositivo.
113#@exception OG_ERR_PARTITION Partición desconocida o no accesible.
114#@note    Requisitos: *resize*
115#@version 0.1 -  Integracion para Opengnsys  -  EAC:   EnlargeFileSystem() en ATA.lib
116#@author  Antonio J. Doblas Viso. Universidad de Malaga
117#@date    2008-10-27
118#@version 0.9 - Primera adaptacion para OpenGnSys.
119#@author  Ramon Gomez, ETSII Universidad de Sevilla
120#@date    2009-09-23
121#@version 1.0.5 - Soporte para BTRFS.
122#@author  Ramon Gomez, ETSII Universidad de Sevilla
123#@date    2012-06-28
124#*/ ##
125function ogExtendFs ()
126{
127# Variables locales.
128local PART TYPE PROG PARAMS ERRCODE DOMOUNT
129
130# Si se solicita, mostrar ayuda.
131if [ "$*" == "help" ]; then
132    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_nfilesys" \
133           "$FUNCNAME 1 1"
134    return
135fi
136# Error si no se reciben 2 parámetros.
137[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
138
139# Obtener partición.
140PART="$(ogDiskToDev $1 $2)" || return $?
141
142# Redimensionar al tamano máximo según el tipo de partición.
143TYPE=$(ogGetFsType $1 $2)
144case "$TYPE" in
145    EXT[234])   PROG="resize2fs"; PARAMS="-f" ;;
146    BTRFS)      PROG="btrfs"; PARAMS="filesystem resize max"
147                DOMOUNT=1     # Debe estar montado.
148                ;;
149    REISERFS|REISER4)
150                PROG="resize_reiserfs"; PARAMS="-f" ;;
151    F2FS)       ;;            # No se reduce (por el momento).
152    JFS)        ;;            # No se reduce (por el momento).
153    NILFS2)     ;;            # No se reduce (probar "nilfs-resize").
154    XFS)        ;;            # No se reduce (por el momento).
155    NTFS)       PROG="ntfsresize"; PARAMS="<<<\"y\" -f" ;;
156    EXFAT)      ;;            # No se reduce (por el momento).
157    FAT32|FAT16)  ;;          # No se reduce (probar "fatresize").
158    HFS|HFSPLUS)  ;;          # No se reduce (por el momento).
159    UFS)        ;;            # No se reduce (por el momento).
160    *)          ogRaiseError $OG_ERR_PARTITION "$1 $2 $TYPE"
161                return $? ;;
162esac
163# Salida normal si no se va a aplicar la operación.
164[ -z "$PROG" ] && return
165# Error si el sistema de archivos no se queda en el estado de montaje adecuado.
166if [ "$DOMOUNT" ]; then
167    PART=$(ogMount $1 $2) || return $?                      # Indicar nuevo error
168else
169    ogUnmount $1 $2 2>/dev/null
170    if ogIsMounted $1 $2; then
171         ogRaiseError $OG_ERR_PARTITION "$1 $2"             # Indicar nuevo error
172         return $?
173    fi
174fi
175# Error si el sistema de archivos está bloqueado.
176if ogIsLocked $1 $2; then
177    ogRaiseError $OG_ERR_LOCKED "$1 $2"
178    return $?
179fi
180# Redimensionar en modo uso exclusivo.
181ogLock $1 $2
182trap "ogUnlock $1 $2" 1 2 3 6 9
183eval $PROG $PARAMS $PART &>/dev/null
184ERRCODE=$?
185case $ERRCODE in
186    0)    ;;
187    127)  ogRaiseError $OG_ERR_NOTEXEC "$PROG"
188          ERRCODE=$OG_ERR_NOTEXEC ;;
189    *)    ogRaiseError $OG_ERR_PARTITION "$1 $2"
190          ERRCODE=$OG_ERR_PARTITION ;;
191esac
192ogUnlock $1 $2
193return $ERRCODE
194}
195
196
197#/**
198#         ogFormat int_ndisk int_nfilesys | CACHE
199#@see     ogFormatFs ogFormatCache
200#*/ ##
201function ogFormat ()
202{
203case "$*" in
204    CACHE|cache)  ogFormatCache ;;
205    *)            ogFormatFs "$@" ;;
206esac
207}
208
209
210#/**
211#         ogFormatFs int_ndisk int_nfilesys [type_fstype] [str_label]
212#@brief   Formatea un sistema de ficheros según el tipo de su partición.
213#@param   int_ndisk      nº de orden del disco
214#@param   int_nfilesys   nº de orden del sistema de archivos
215#@param   type_fstype    mnemónico de sistema de ficheros a formatear (opcional al reformatear)
216#@param   str_label      etiqueta de volumen (opcional)
217#@return  (por determinar)
218#@exception OG_ERR_FORMAT    Formato de ejecución incorrecto.
219#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
220#@exception OG_ERR_PARTITION Partición no accesible o desconocida.
221#@note    Requisitos:   mkfs*
222#@warning No formatea particiones montadas ni bloqueadas.
223#@todo    Definir salidas.
224#@version 0.9 - Primera versión para OpenGnSys.
225#@author  Ramon Gomez, ETSII Universidad de Sevilla
226#@date    2009-10-08
227#@version 1.0.4 - Solucionado error cuando no se detecta tipo de sistema de ficheros pero si se indica.
228#@author  Universidad de Huelva
229#@date    2012-04-11
230#@version 1.0.5 - Comprobar errores al inicio e independizar del tipo de tabla de particiones.
231#@author  Universidad de Huelva
232#@date    2013-05-16
233#@version 1.1.0 - Soportar F2FS y NILFS.
234#@author  Ramon Gomez, ETSII Universidad de Sevilla
235#@date    2016-05-03
236#*/ ##
237function ogFormatFs ()
238{
239# Variables locales
240local PART ID TYPE LABEL PROG PARAMS LABELPARAM ERRCODE
241
242# Si se solicita, mostrar ayuda.
243if [ "$*" == "help" ]; then
244    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_nfilesys [str_label]" \
245           "$FUNCNAME 1 1" \
246           "$FUNCNAME 1 1 EXT4" \
247           "$FUNCNAME 1 1 \"DATA\"" \
248           "$FUNCNAME 1 1 EXT4 \"DATA\""
249    return
250fi
251# Error si no se reciben entre 2 y 4 parámetros.
252[ $# -ge 2 -a $# -le 4 ] || ogRaiseError $OG_ERR_FORMAT || return $?
253# Obtener fichero de dispositivo.
254PART="$(ogDiskToDev $1 $2)" || return $?
255# Error si la partición está montada o bloqueada.
256if ogIsMounted $1 $2; then
257    ogRaiseError $OG_ERR_DONTFORMAT "$MSG_MOUNT: $1 $2"
258    return $?
259fi
260if ogIsLocked $1 $2; then
261    ogRaiseError $OG_ERR_LOCKED "$1 $2"
262    return $?
263fi
264# Si no se indica el tipo de sisitema de archivos, intentar obtenerlo.
265TYPE="${3:-$(ogGetFsType $1 $2)}"
266# Error, si no especifica el tipo de sistema de archivos a formatear.
267[ -n "$TYPE" ] || ogRaiseError $OG_ERR_FORMAT "$1 $2 ..." || return $?
268
269# Elegir tipo de formato.
270case "$TYPE" in
271    EXT2)         PROG="mkfs.ext2"; PARAMS="-F" ;;
272    EXT3)         PROG="mkfs.ext3"; PARAMS="-F" ;;
273    EXT4)         PROG="mkfs.ext4"; PARAMS="-F" ;;
274    BTRFS)        PROG="mkfs.btrfs"; PARAMS="-f" ;;
275    REISERFS)     PROG="mkfs.reiserfs"; PARAMS="-f"; LABELPARAM="-l" ;;
276    REISER4)      PROG="mkfs.reiser4"; PARAMS="-f <<<\"y\"" ;;
277    XFS)          PROG="mkfs.xfs"; PARAMS="-f" ;;
278    JFS)          PROG="mkfs.jfs"; PARAMS="<<<\"y\"" ;;
279    F2FS)         PROG="mkfs.f2fs"; LABELPARAM="-l" ;;
280    NILFS2)       PROG="mkfs.nilfs2"; PARAMS="-f" ;;
281    LINUX-SWAP)   PROG="mkswap" ;;
282    NTFS)         PROG="mkntfs"; PARAMS="-f" ;;
283    EXFAT)        PROG="mkfs.exfat"; LABELPARAM="-n" ;;
284    FAT32)        PROG="mkdosfs"; PARAMS="-F 32"; LABELPARAM="-n" ;;
285    FAT16)        PROG="mkdosfs"; PARAMS="-F 16"; LABELPARAM="-n" ;;
286    FAT12)        PROG="mkdosfs"; PARAMS="-F 12"; LABELPARAM="-n" ;;
287    HFS)          PROG="mkfs.hfs" ;;
288    HFSPLUS)      PROG="mkfs.hfsplus"; LABELPARAM="-v" ;;
289    UFS)          PROG="mkfs.ufs"; PARAMS="-O 2" ;;
290    *)            ogRaiseError $OG_ERR_PARTITION "$1 $2 $TYPE"
291                  return $? ;;
292esac
293
294# Etiquetas de particion.
295if [ -z "$LABEL" ]; then
296    [ "$4" != "CACHE" ] || ogRaiseError $OG_ERR_FORMAT "$MSG_RESERVEDVALUE: CACHE" || return $?
297    [ -n "$4" ] && PARAMS="$PARAMS ${LABELPARAM:-"-L"} $4"
298else
299    PARAMS="$PARAMS ${LABELPARAM:-"-L"} $LABEL"
300fi
301
302# Formatear en modo uso exclusivo (desmontar siempre).
303ogLock $1 $2
304trap "ogUnlock $1 $2" 1 2 3 6 9
305umount $PART 2>/dev/null
306eval $PROG $PARAMS $PART 2>/dev/null
307ERRCODE=$?
308case $ERRCODE in
309    0)    ;;
310    127)  ogRaiseError $OG_ERR_NOTEXEC "$PROG" ;;
311    *)    ogRaiseError $OG_ERR_PARTITION "$1 $2" ;;
312esac
313ogUnlock $1 $2
314return $ERRCODE
315}
316
317
318#/**
319#         ogGetFsSize int_ndisk int_npartition [str_unit]
320#@brief Muestra el tamanio del sistema de archivos indicado, permite definir la unidad de medida, por defecto GB
321#@param   int_ndisk      nº de orden del disco
322#@param   int_npartition nº de orden de la partición
323#@param   str_unit       unidad (opcional, por defecto: KB)
324#@return  float_size - Tamaño del sistema de archivos
325#@note    str_unit = { KB, MB, GB, TB }
326#@exception OG_ERR_FORMAT   Formato incorrecto.
327#@exception OG_ERR_NOTFOUND Disco o partición no corresponden con un dispositivo.
328#@version 0.1 -  Integracion para Opengnsys  -  EAC:  SizeFileSystem() en FileSystem.lib
329#@author  Antonio J. Doblas Viso. Universidad de Malaga
330#@date    2008-10-27
331#@version 1.0.4 - Adaptación de las salidas.
332#@author  Ramon Gomez, ETSII Universidad de Sevilla
333#@date    2012-06-18
334#*/ ##
335function ogGetFsSize ()
336{
337# Variables locales.
338local MNTDIR UNIT VALUE FACTOR SIZE
339# Si se solicita, mostrar ayuda.
340if [ "$*" == "help" ]; then
341    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition [str_unit]" \
342           "$FUNCNAME 1 1  =>  15624188" \
343           "$FUNCNAME 1 1 KB  =>  15624188"
344    return
345fi
346# Error si no se reciben 2 o 3 parámetros.
347[ $# == 2 ] || [ $# == 3 ] || ogRaiseError $OG_ERR_FORMAT || return $?
348# Obtener unidad y factor de medida.
349UNIT="$3"
350UNIT=${UNIT:-"KB"}
351case "$UNIT" in
352    [kK]B)
353        FACTOR=1 ;;
354    MB) FACTOR=1024 ;;
355    GB) FACTOR=$[1024*1024] ;;
356    TB) FACTOR=$[1024*1024*1024] ;;
357    *)  ogRaiseError $OG_ERR_FORMAT "$3 != { KB, MB, GB, TB }"
358        return $? ;;
359esac
360
361# Obtener el tamaño del sistema de archivo (si no está formateado; tamaño = 0).
362MNTDIR="$(ogMount $1 $2 2>/dev/null)"
363if [ -n "$MNTDIR" ]; then
364    VALUE=$(df -BK "$MNTDIR" | awk '{getline; print $2}')
365    SIZE=$(echo "$VALUE $FACTOR" | awk '{printf "%f\n", $1/$2}')
366else
367    SIZE=0
368fi
369# Devolver el tamaño (quitar decimales si son 0).
370echo ${SIZE%.0*}
371}
372
373
374#/**
375#         ogGetFsType int_ndisk int_nfilesys
376#@brief   Devuelve el mnemonico con el tipo de sistema de archivos.
377#@param   int_ndisk      nº de orden del disco
378#@param   int_nfilesys   nº de orden del sistema de archivos
379#@return  Mnemonico
380#@note    Mnemonico: { EXT2, EXT3, EXT4, BTRFS, REISERFS, XFS, JFS, FAT12, FAT16, FAT32, NTFS, LINUX-SWAP, LINUX-LVM, LINUX-RAID, HFS, HFSPLUS, CACHE }
381#@exception OG_ERR_FORMAT   Formato incorrecto.
382#@exception OG_ERR_NOTFOUND Disco o particion no corresponden con un dispositivo.
383#@version 0.1 -  Integracion para Opengnsys  -  EAC:   TypeFS() en ATA.lib
384#@author  Antonio J. Doblas Viso. Universidad de Malaga
385#@date    2008-10-27
386#@version 0.9 - Primera adaptacion para OpenGnSys.
387#@author  Ramon Gomez, ETSII Universidad de Sevilla
388#@date    2009-07-21
389#@version 1.0.2 - Obtención de datos reales de sistemas de ficheros.
390#@author  Ramon Gomez, ETSII Universidad de Sevilla
391#@date    2011-12-02
392#@version 1.0.5 - Usar "blkid" para detectar tipo de sistema de archivo.
393#@author  Ramon Gomez, ETSII Universidad de Sevilla
394#@date    2014-06-10
395#@version 1.1.0 - Detectar volumen ZFS.
396#@author  Ramon Gomez, ETSII Universidad de Sevilla
397#@date    2014-11-14
398#*/ ##
399function ogGetFsType ()
400{
401# Variables locales.
402local PART TYPE
403# Si se solicita, mostrar ayuda.
404if [ "$*" == "help" ]; then
405    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_nfilesys" \
406           "$FUNCNAME 1 1  =>  NTFS"
407    return
408fi
409# Error si no se reciben 2 parámetros.
410[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
411
412# Detectar tipo de sistema de archivo (independientemente del tipo de partición).
413PART=$(ogDiskToDev "$1" "$2") || return $?
414if [[ "$PART" =~ ^/ ]]; then
415    TYPE=$(blkid -o export $PART | awk -F= '$1~/^TYPE/ { print toupper($2) }')
416else
417    zfs mount $PART 2>/dev/null
418    TYPE=$(mount | awk "\$1==\"$PART\" { print toupper(\$5) }")
419fi
420
421# Componer valores correctos.
422case "$TYPE" in
423    EXT4)      # Comprobar si es caché o Ext4.
424               if [ "$1 $2" == "$(ogFindCache)" ]; then
425                   ogIsFormated $1 $2 2>/dev/null && TYPE="CACHE"
426               fi
427               ;;
428    VFAT)      TYPE="$(blkid -po export $PART | awk -F= '$1~/^VERSION$/ { print toupper($2) }')" ;;
429    SWAP)      TYPE="LINUX-SWAP" ;;
430    LVM*)      TYPE="LINUX-LVM" ;;
431    *RAID*)    TYPE="LINUX-RAID" ;;
432    ZFS_MEMBER) TYPE="ZVOL" ;;
433    *_MEMBER)  TYPE="${TYPE/_MEMBER/}" ;;
434esac
435
436[ -n "$TYPE" ] && echo "$TYPE"
437}
438
439
440#/**
441#         ogGetMountPoint int_ndisk int_nfilesys
442#@brief   Devuelve el punto de montaje de un sistema de archivos.
443#@param   int_ndisk      nº de orden del disco
444#@param   int_nfilesys   nº de orden del sistema de archivos
445#@return  Punto de montaje
446#@exception OG_ERR_FORMAT    Formato incorrecto.
447#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
448#@note    Requisitos: \c mount* \c awk
449#@version 0.9 - Primera versión para OpenGnSys.
450#@author  Ramon Gomez, ETSII Universidad de Sevilla
451#@date    2009-10-15
452#@version 1.0.6 - Usar comando findmnt.
453#@author  Ramon Gomez, ETSII Universidad de Sevilla
454#@date    2014-09-04
455#*/ ##
456function ogGetMountPoint ()
457{
458# Variables locales
459local PART
460# Si se solicita, mostrar ayuda.
461if [ "$*" == "help" ]; then
462    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_nfilesys" \
463           "$FUNCNAME 1 1  =>  /mnt/sda1"
464    return
465fi
466# Error si no se reciben 2 parámetros.
467[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
468# Obtener partición.
469PART="$(ogDiskToDev $1 $2)" || return $?
470
471# Devolver punto de montaje.
472findmnt -n -o TARGET $PART
473}
474
475
476#/**
477#         ogIsFormated int_ndisk int_nfilesys
478#@brief   Comprueba si un sistema de archivos está formateado.
479#@param   int_ndisk      nº de orden del disco o volumen.
480#@param   int_nfilesys   nº de orden del sistema de archivos
481#@return  Código de salida: 0 - formateado, 1 - sin formato o error.
482#@version 0.91 - Adaptación inicial para comprobar que existe caché.
483#@author  Ramon Gomez, ETSII Universidad de Sevilla
484#@date    2010-03-18
485#@version 1.0.1 - Devolver falso en caso de error.
486#@author  Ramon Gomez, ETSII Universidad de Sevilla
487#@date    2011-05-18
488#@version 1.0.5 - Dejar de usar "parted".
489#@author  Ramon Gomez, ETSII Universidad de Sevilla
490#@date    2012-09-04
491#@version 1.1.0 - Comprobar sin montar el sistema de ficheros.
492#@author  Ramon Gomez, ETSII Universidad de Sevilla
493#@date    2016-01-21
494#*/ ##
495function ogIsFormated ()
496{
497# Variables locales
498local PART
499if [ "$*" == "help" ]; then
500    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_nfilesys" \
501           "if $FUNCNAME 1 1; then ... ; fi"
502    return
503fi
504# Falso, en caso de error.
505[ $# == 2 ] || return 1
506PART="$(ogDiskToDev $1 $2 2>/dev/null)" || return 1
507
508# Revisar tipo de sistema de ficheros.
509if [[ "$PART" =~ ^/ ]]; then
510    # Sistemas de ficheros genéricos.
511    test -n "$(blkid -s TYPE $PART | egrep -vi "swap|_member")"
512else
513    # ZFS.
514    test "$(zfs list -Hp -o canmount $PART 2>/dev/null)" = "on"
515fi
516}
517
518
519#/**
520#         ogIsLocked int_ndisk int_npartition
521#@see     ogIsPartitionLocked
522#*/
523function ogIsLocked ()
524{
525ogIsPartitionLocked "$@"
526}
527
528#/**
529#         ogIsPartitionLocked int_ndisk int_npartition
530#@brief   Comprueba si una partición o su disco están bloqueados por una operación de uso exclusivo.
531#@param   int_ndisk      nº de orden del disco
532#@param   int_npartition nº de orden de la partición
533#@return  Código de salida: 0 - bloqueado, 1 - sin bloquear o error.
534#@note    Los ficheros de bloqueo se localizan en \c /var/lock/dev, siendo \c dev el dispositivo de la partición o de su disco, sustituyendo el carácter "/" por "-".
535#@version 0.9 - Primera versión para OpenGnSys.
536#@author  Ramon Gomez, ETSII Universidad de Sevilla
537#@date    2009-09-03
538#@version 1.0.1 - Devolver falso en caso de error.
539#@author  Ramon Gomez, ETSII Universidad de Sevilla
540#@date    2011-05-18
541#@version 1.1.0 - Comprobar si el disco está también bloqueado.
542#@author  Ramon Gomez, ETSII Universidad de Sevilla
543#@date    2016-04-08
544#*/ ##
545function ogIsPartitionLocked ()
546{
547# Variables locales
548local DISK PART LOCKDISK LOCKPART
549
550# Si se solicita, mostrar ayuda.
551if [ "$*" == "help" ]; then
552    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
553           "if $FUNCNAME 1 1; then ... ; fi"
554    return
555fi
556# Falso, en caso de error.
557[ $# == 2 ] || return 1
558PART="$(ogDiskToDev $1 $2 2>/dev/null)" || return 1
559DISK="$(ogDiskToDev $1)"
560
561# Comprobar existencia de fichero de bloqueo de la partición o de su disco.
562LOCKDISK="/var/lock/lock${DISK//\//-}"
563LOCKPART="/var/lock/lock${PART//\//-}"
564test -f $LOCKDISK -o -f $LOCKPART
565}
566
567
568#/**
569#         ogIsMounted int_ndisk int_nfilesys
570#@brief   Comprueba si un sistema de archivos está montado.
571#@param   int_ndisk      nº de orden del disco
572#@param   int_nfilesys   nº de orden del sistema de archivos
573#@return  Código de salida: 0 - montado, 1 - sin montar o error.
574#@version 0.9 - Primera versión para OpenGnSys.
575#@author  Ramon Gomez, ETSII Universidad de Sevilla
576#@date    2009-10-15
577#@version 1.0.1 - Devolver falso en caso de error.
578#@author  Ramon Gomez, ETSII Universidad de Sevilla
579#@date    2011-05-18
580#*/ ##
581function ogIsMounted ()
582{
583# Si se solicita, mostrar ayuda.
584if [ "$*" == "help" ]; then
585    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_nfilesys" \
586           "if $FUNCNAME 1 1; then ... ; fi"
587    return
588fi
589# Falso, en caso de error.
590[ $# == 2 ] || return 1
591
592test -n "$(ogGetMountPoint $1 $2)"
593}
594
595
596#/**
597#         ogIsReadonly int_ndisk int_nfilesys
598#@brief   Comprueba si un sistema de archivos está montado solo de lectura.
599#@param   int_ndisk      nº de orden del disco
600#@param   int_nfilesys   nº de orden del sistema de archivos
601#@return  Código de salida: 0 - montado solo de lectura, 1 - con escritura o no montado.
602#@version 1.1.0 - Primera versión para OpenGnsys.
603#@author  Ramon Gomez, ETSII Universidad de Sevilla
604#@date    2016-01-20
605#*/ ##
606
607function ogIsReadonly ()
608{
609# Variables locales
610local PART
611
612# Si se solicita, mostrar ayuda.
613if [ "$*" == "help" ]; then
614    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_filesys" \
615           "if $FUNCNAME 1 1; then ... ; fi"
616    return
617fi
618# Falso, en caso de error.
619[ $# == 2 ] || return 1
620PART="$(ogDiskToDev $1 $2 2>/dev/null)" || return 1
621
622test -n "$(findmnt -n -o OPTIONS $PART | awk 'BEGIN {RS=","} /^ro$/ {print}')"
623}
624
625
626#/**
627#         ogIsWritable int_ndisk int_nfilesys
628#@brief   Comprueba si un sistema de archivos está montado de lectura y escritura.
629#@param   int_ndisk      nº de orden del disco
630#@param   int_nfilesys   nº de orden del sistema de archivos
631#@return  Código de salida: 0 - lectura y escritura, 1 - solo lectura o no montado.
632#@version 1.0.5 - Primera versión para OpenGnSys.
633#@author  Ramon Gomez, ETSII Universidad de Sevilla
634#@date    2013-10-09
635#*/ ##
636function ogIsWritable ()
637{
638# Variables locales
639local PART
640
641# Si se solicita, mostrar ayuda.
642if [ "$*" == "help" ]; then
643    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_filesys" \
644           "if $FUNCNAME 1 1; then ... ; fi"
645    return
646fi
647# Falso, en caso de error.
648[ $# == 2 ] || return 1
649PART="$(ogDiskToDev $1 $2 2>/dev/null)" || return 1
650
651test -n "$(findmnt -n -o OPTIONS $PART | awk 'BEGIN {RS=","} /^rw$/ {print}')"
652}
653
654
655#/**
656#         ogLock int_ndisk int_npartition
657#@see     ogLockPartition
658#*/
659function ogLock ()
660{
661ogLockPartition "$@"
662}
663
664#/**
665#         ogLockPartition int_ndisk int_npartition
666#@brief   Genera un fichero de bloqueo para una partición en uso exlusivo.
667#@param   int_ndisk      nº de orden del disco
668#@param   int_npartition nº de orden de la partición
669#@return  (nada)
670#@exception OG_ERR_FORMAT    Formato incorrecto.
671#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
672#@note    El fichero de bloqueo se localiza en \c /var/lock/part, siendo \c part el dispositivo de la partición, sustituyendo el carácter "/" por "-".
673#@version 0.9 - Primera versión para OpenGnSys.
674#@author  Ramon Gomez, ETSII Universidad de Sevilla
675#@date    2009-09-03
676#*/ ##
677function ogLockPartition ()
678{
679# Variables locales
680local PART LOCKFILE
681
682# Si se solicita, mostrar ayuda.
683if [ "$*" == "help" ]; then
684    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
685           "$FUNCNAME 1 1"
686    return
687fi
688# Error si no se reciben 2 parámetros.
689[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
690
691# Obtener partición.
692PART="$(ogDiskToDev $1 $2)" || return $?
693
694# Crear archivo de bloqueo exclusivo.
695LOCKFILE="/var/lock/lock${PART//\//-}"
696touch $LOCKFILE
697}
698
699
700#/**
701#         ogMount int_ndisk int_nfilesys
702#@see     ogMountFs ogMountCache ogMountCdrom
703#*/ ##
704function ogMount ()
705{
706case "$*" in
707    CACHE|cache)
708        ogMountCache ;;
709    CDROM|cdrom)
710        ogMountCdrom ;;
711    *)  ogMountFs "$@" ;;
712esac
713}
714
715
716#/**
717#         ogMountFirstFs int_ndisk
718#@brief   Monta el primer sistema de archivos disponible en el disco.
719#@param   int_ndisk      nº de orden del disco
720#@return  Punto de montaje del primer sistema de archivos detectado
721#*/ ##
722function ogMountFirstFs ()
723{
724# Variables locales
725local PART NPARTS MNTDIR
726
727# Error si no se recibe 1 parámetro.
728[ $# == 1 ] || ogRaiseError $OG_ERR_FORMAT || return $?
729
730# Obtener nº de particiones del disco.
731NPARTS=$(ogGetPartitionsNumber "$1") || return $?
732for (( PART = 1; PART <= NPARTS; PART++ )); do
733    MNTDIR=$(ogMount $1 $PART 2>/dev/null)
734    if [ -n "$MNTDIR" ]; then
735        echo "$MNTDIR"
736        return 0
737    fi
738done
739ogRaiseError $OG_ERR_NOTFOUND "$1"
740return $OG_ERR_NOTFOUND
741}
742
743
744#/**
745#         ogMountFs int_ndisk int_nfilesys
746#@brief   Monta un sistema de archivos.
747#@param   int_ndisk      nº de orden del disco
748#@param   int_nfilesys   nº de orden del sistema de archivos
749#@return  Punto de montaje
750#@exception OG_ERR_FORMAT    Formato incorrecto.
751#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
752#@exception OG_ERR_PARTITION Tipo de particion desconocido o no se puede montar.
753#@version 0.1 -  Integracion para Opengnsys  -  EAC:   MountPartition() en FileSystem.lib
754#@author  Antonio J. Doblas Viso. Universidad de Malaga
755#@date    2008-10-27
756#@version 0.9 - Primera version para OpenGnSys.
757#@author  Ramon Gomez, ETSII Universidad de Sevilla
758#@date    2009-09-28
759#@version 1.0.5 - Independiente del tipo de sistema de ficheros.
760#@author  Ramon Gomez, ETSII Universidad de Sevilla
761#@date    2012-09-04
762#@version 1.1.0 - Montar sistema de archivos ZFS y NTFS hibernado.
763#@author  Ramon Gomez, ETSII Universidad de Sevilla
764#@date    2016-09-19
765#*/ ##
766function ogMountFs ()
767{
768# Variables locales
769local PART MNTDIR
770
771# Si se solicita, mostrar ayuda.
772if [ "$*" == "help" ]; then
773    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_nfilesys" \
774           "$FUNCNAME 1 1  =>  /mnt/sda1"
775    return
776fi
777# Error si no se reciben 2 parámetros.
778[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
779
780# Obtener partición.
781PART="$(ogDiskToDev "$1" "$2")" || return $?
782
783# Comprobar si el sistema de archivos ya está montada.
784MNTDIR="$(ogGetMountPoint $1 $2)"
785# Si no, montarlo en un directorio de sistema.
786if [ -z "$MNTDIR" ]; then
787    # Error si la particion esta bloqueada.
788    if ogIsLocked $1 $2; then
789        ogRaiseError $OG_ERR_LOCKED "$MSG_PARTITION, $1 $2"
790        return $?
791    fi
792    # El camino de un dispositivo normal comienza por el carácter "/".
793    if [[ "$PART" =~ ^/ ]]; then
794        # Crear punto de montaje o enlace simbólico para caché local.
795        MNTDIR=${PART/dev/mnt}
796        DEBUG="no"
797        if [ "$(ogFindCache)" == "$1 $2" -a -n "$OGCAC" ]; then
798            mkdir -p $OGCAC
799            ln -fs $OGCAC $MNTDIR
800        else
801            mkdir -p $MNTDIR
802        fi
803        unset DEBUG
804        # Montar sistema de archivos.
805        mount $PART $MNTDIR &>/dev/null || \
806                    mount $PART $MNTDIR -o force,remove_hiberfile &>/dev/null
807        case $? in
808            0)  # Correcto.
809                ;;
810            14) # Intentar limpiar hibernación NTFS y montar.
811                ntfsfix -d $PART &>/dev/null && mount $PART $MNTDIR &>/dev/null || \
812                        ogRaiseError $OG_ERR_PARTITION "$1, $2" || return $?
813                ;;
814            *)  # Probar montaje de solo lectura.
815                mount $PART $MNTDIR -o ro &>/dev/null || \
816                        ogRaiseError $OG_ERR_PARTITION "$1, $2" || return $?
817                ;;
818        esac
819        # Aviso de montaje de solo lectura.
820        if ogIsReadonly $1 $2; then
821            ogEcho warning "$FUNCNAME: $MSG_MOUNTREADONLY: \"$1, $2\""
822        fi
823    else
824        # Montar sistema de archivos ZFS (un ZPOOL no comienza por "/").
825        zfs mount $PART 2>/dev/null
826    fi
827fi
828echo "$MNTDIR"
829}
830
831
832#/**
833#         ogMountCdrom
834#@brief   Monta dispositivo óptico por defecto
835#@return  Punto de montaje
836#@exception OG_ERR_FORMAT    Formato incorrecto.
837#@exception OG_ERR_PARTITION Tipo de particion desconocido o no se puede montar.
838#@version
839#@author 
840#@date   
841#*/ ##
842function ogMountCdrom ()
843{
844local DEV MNTDIR
845# Si se solicita, mostrar ayuda.
846if [ "$*" == "help" ]; then
847    ogHelp "$FUNCNAME" "$FUNCNAME" "$FUNCNAME"
848    return
849fi
850# Error si se reciben parámetros.
851[ $# == 0 ] || ogRaiseError $OG_ERR_FORMAT || return $?
852DEV="/dev/cdrom"            # Por defecto
853MNTDIR=$(mount | awk -v D=$DEV '{if ($1==D) {print $3}}')
854if [ -z "$MNTDIR" ]; then
855    MNTDIR=${DEV/dev/mnt}
856    mkdir -p $MNTDIR
857    mount -t iso9660 $DEV $MNTDIR || ogRaiseError $OG_ERR_PARTITION "cdrom" || return $?
858fi
859echo $MNTDIR
860}
861
862
863#/**
864#         ogReduceFs int_ndisk int_nfilesys
865#@brief   Reduce el tamaño del sistema de archivos, sin tener en cuenta el espacio libre.
866#@param   int_ndisk      nº de orden del disco
867#@param   int_nfilesys   nº de orden del sistema de archivos
868#@return  int_tamañoKB - tamaño en KB
869#@exception OG_ERR_FORMAT    Formato incorrecto.
870#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
871#@exception OG_ERR_PARTITION Partición desconocida o no accesible.
872#@warning En Windows, se borran los ficheros de hiberanción y de paginación.
873#@warning El sistema de archivos se amplía al mínimo + 10%.
874#@note    Requisitos:   *resize*
875#@version 0.1 -  Integracion para Opengnsys  -  EAC:   ReduceFileSystem() en ATA.lib
876#@author  Antonio J. Doblas Viso. Universidad de Malaga
877#@date    2008-10-27
878#@version 0.9 - Primera version para OpenGnSys.
879#@author  Ramon Gomez, ETSII Universidad de Sevilla
880#@date    2009-09-23
881#@version 0.9.2 - Añadir un 10% al tamaño mínimo requerido.
882#@author  Ramon Gomez, ETSII Universidad de Sevilla
883#@date    2010-09-27
884#@version 1.0 -  Deteccion automatica del tamaño minimo adecuado
885#@author  Antonio J. Doblas Viso. Universidad de Malaga
886#@date    2011-02-24
887#@version 1.0.6 - Integrar código de antigua función "ogReduceFsCheck".
888#@author  Ramon Gomez, ETSII Universidad de Sevilla
889#@date    2014-10-28
890#*/ ##
891function ogReduceFs ()
892{
893# Variables locales
894local PART BLKS SIZE MAXSIZE EXTRASIZE=0 RETVAL
895
896# Si se solicita, mostrar ayuda.
897if [ "$*" == "help" ]; then
898    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_nfilesys" \
899           "$FUNCNAME 1 1"
900    return
901fi
902# Error si no se reciben 2 parámetros.
903[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
904
905# Obtener partición.
906PART="$(ogDiskToDev $1 $2)" || return $?
907
908# Redimensionar según el tipo de particion.
909case "$(ogGetFsType $1 $2)" in
910    EXT[234])
911        ogUnmount $1 $2 2>/dev/null
912        # Ext2/3/4: Tamaño de los bloques del sistema de archivos
913        BLKS=$(tune2fs -l $PART | awk '/Block size/ {print int($3/512)}')
914        # Traduce el num. en sectores de 512B a tamano en MB.
915        #SIZE=$(resize2fs -P $PART 2>/dev/null | \
916                #       awk -v B=$BLKS '/minimum size/ {print int($7*1.1*B/2048)}')
917        #resize2fs -fp $PART "${SIZE}M" &>/dev/null || ogRaiseError $OG_ERR_PARTITION "$1,$2" || return $?
918        resize2fs -fpM $PART  &>/dev/null || ogRaiseError $OG_ERR_PARTITION "$1,$2" || return $?
919        ;;
920
921    BTRFS)
922        MNTDIR=$(ogMount $1 $2)
923        # Calcular tamaño ocupado + 10%, redondeado + 1 (incluyendo letra de unidad).
924        SIZE=$(btrfs filesystem show $MNTDIR | awk -v P=$PART '{ if ($8==P) printf ("%d%s", $6*1.1+1, substr($6,match($6,/[A-Z]/),1)) }')
925        btrfs filesystem resize ${SIZE} $MNTDIR 2>/dev/null
926        ;;
927    REISERFS|REISER4)
928        # Calcular tamaño ocupado + 10%.
929        MNTDIR=$(ogMount $1 $2)
930        SIZE=$[ $(df -k $MNTDIR | awk '{getline;print $3}') * 110 / 100 ]
931        ogUnmount $1 $2 2>/dev/null
932        resize_reiserfs -s${SIZE}K $PART <<<"y"
933        ;;
934
935    F2FS)   ;;          # No se reduce (por el momento).
936    JFS)    ;;          # No se reduce (por el momento).
937    NILFS2) ;;          # No se reduce (probar "nilfs-resize").
938    XFS)    ;;          # No se reduce (por el momento).
939
940    NTFS)
941        # Calcular tamaño ocupado + 10%.
942        ogUnmount $1 $2 &>/dev/null
943        read -e MAXSIZE SIZE <<<$(ntfsresize -fi $PART | \
944                                  awk '/device size/ {d=$4}
945                                       /resize at/ {r=int($5*1.1/1024+1)*1024}
946                                       END { print d,r}')
947        # Error si no puede obtenerse el tamaño máximo del volumen.
948        [ -n "$MAXSIZE" -a -n "$SIZE" ] || ogRaiseError $OG_ERR_PARTITION "$1,$2" || return $?
949        # Simular la redimensión y comprobar si es necesario ampliarala.
950        RETVAL=1
951        while [ $RETVAL != 0 -a $[ SIZE+=EXTRASIZE ] -lt $MAXSIZE ]; do
952            # Obtener espacio de relocalización y devolver código de salida
953            # (ntfsresize devuelve 0 si no necesita relocalizar).
954            EXTRASIZE=$(ntfsresize -fns $SIZE $PART 2>/dev/null | \
955                        awk '/Needed relocations/ {print int($4*1.1/1024+1)*1024}'
956                        exit ${PIPESTATUS[0]})
957            RETVAL=$?
958        done
959        # Redimensionar solo si hace falta.
960        if [ $SIZE -lt $MAXSIZE ]; then
961            ntfsresize -fs $SIZE $PART <<<"y" >/dev/null || ogRaiseError $OG_ERR_PARTITION "$1,$2" || return $?
962        fi
963        ;;
964
965    EXFAT)  ;;          # No se reduce (por el momento).
966    FAT32|FAT16)  ;;    # No se reduce (probar "fatresize").
967    HFS|HFSPLUS)  ;;    # No se reduce (por el momento).
968    UFS)    ;;          # No se reduce (por el momento).
969
970    *)  ogRaiseError $OG_ERR_PARTITION "$1,$2"
971        return $? ;;
972esac
973
974# Devuelve tamaño del sistema de ficheros.
975ogGetFsSize $1 $2
976}
977
978
979#/**
980#         ogUnlock int_ndisk int_npartition
981#@see     ogUnlockPartition
982#*/ ##
983function ogUnlock ()
984{
985ogUnlockPartition "$@"
986}
987
988#/**
989#         ogUnlockPartition int_ndisk int_npartition
990#@brief   Elimina el fichero de bloqueo para una particion.
991#@param   int_ndisk      nº de orden del disco
992#@param   int_npartition nº de orden de la partición
993#@return  (nada)
994#@exception OG_ERR_FORMAT    Formato incorrecto.
995#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
996#@note    El fichero de bloqueo se localiza en \c /var/lock/part, siendo \c part el dispositivo de la partición, sustituyendo el carácter "/" por "-".
997#@version 0.9 - Primera versión para OpenGnSys.
998#@author  Ramon Gomez, ETSII Universidad de Sevilla
999#@date    2009-09-03
1000#*/ ##
1001function ogUnlockPartition ()
1002{
1003# Variables locales
1004local PART LOCKFILE
1005
1006# Si se solicita, mostrar ayuda.
1007if [ "$*" == "help" ]; then
1008    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
1009           "$FUNCNAME 1 1"
1010    return
1011fi
1012# Error si no se reciben 2 parámetros.
1013[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
1014
1015# Obtener partición.
1016PART="$(ogDiskToDev $1 $2)" || return $?
1017
1018# Borrar archivo de bloqueo exclusivo.
1019LOCKFILE="/var/lock/lock${PART//\//-}"
1020rm -f $LOCKFILE
1021}
1022
1023
1024#/**
1025#         ogUnmount int_ndisk int_npartition
1026#@see     ogUnmountFs
1027#*/ ##
1028function ogUnmount ()
1029{
1030ogUnmountFs "$@"
1031}
1032
1033#/**
1034#         ogUnmountFs int_ndisk int_nfilesys
1035#@brief   Desmonta un sistema de archivos.
1036#@param   int_ndisk      nº de orden del disco
1037#@param   int_nfilesys   nº de orden del sistema de archivos
1038#@return  Nada
1039#@exception OG_ERR_FORMAT    Formato incorrecto.
1040#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
1041#@warning La partición no está previamente montada o no se puede desmontar.
1042#@version 0.1 -  Integracion para Opengnsys  -  EAC:  UmountPartition() en FileSystem.lib
1043#@author  Antonio J. Doblas Viso. Universidad de Malaga
1044#@date    2008-10-27
1045#@version 0.9 - Primera version para OpenGnSys.
1046#@author  Ramon Gomez, ETSII Universidad de Sevilla
1047#@date    2009-09-28
1048#*/ ##
1049function ogUnmountFs ()
1050{
1051# Variables locales
1052local PART MNTDIR
1053
1054# Si se solicita, mostrar ayuda.
1055if [ "$*" == "help" ]; then
1056    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" "$FUNCNAME 1 1"
1057    return
1058fi
1059# Error si no se reciben 2 parámetros.
1060[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
1061
1062# Obtener partición y punto de montaje.
1063PART="$(ogDiskToDev $1 $2)" || return $?
1064MNTDIR="$(ogGetMountPoint $1 $2)"
1065
1066# Si está montada, desmontarla.
1067if [ -n "$MNTDIR" ]; then
1068    # Error si la particion está bloqueada.
1069    if ogIsLocked $1 $2; then
1070        ogRaiseError $OG_ERR_LOCKED "$MSG_PARTITION $1, $2"
1071        return $?
1072    fi
1073    # Desmontar y borrar punto de montaje.
1074    umount $PART 2>/dev/null || ogEcho warning "$FUNCNAME: $MSG_DONTUNMOUNT: \"$1, $2\""
1075    rmdir $MNTDIR 2>/dev/null || rm -f $MNTDIR 2>/dev/null
1076else
1077    ogEcho warning "$MSG_DONTMOUNT: \"$1,$2\""
1078fi
1079}
1080
1081
1082#/**
1083#         ogUnmountAll int_ndisk
1084#@brief   Desmonta todos los sistema de archivos de un disco, excepto el caché local.
1085#@param   int_ndisk      nº de orden del disco
1086#@return  Nada
1087#@exception OG_ERR_FORMAT    Formato incorrecto.
1088#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
1089#@warning No se desmonta la partición marcada como caché local.
1090#@version 0.9 - Versión para OpenGnSys.
1091#@author  Ramon Gomez, ETSII Universidad de Sevilla
1092#@date    2009/10/07
1093#*/ ##
1094function ogUnmountAll ()
1095{
1096# Variables locales
1097local DISK PART
1098# Si se solicita, mostrar ayuda.
1099if [ "$*" == "help" ]; then
1100    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk" "FUNCNAME 1"
1101    return
1102fi
1103# Error si no se recibe 1 parámetro.
1104[ $# == 1 ] || ogRaiseError $OG_ERR_FORMAT || return $?
1105
1106# Obtener partición y punto de montaje.
1107DISK="$(ogDiskToDev $1)" || return $?
1108for ((PART=1; PART<=$(ogGetPartitionsNumber $1); PART++)); do
1109    case "$(ogGetFsType $1 $PART)" in
1110        CACHE) ;;
1111        *)     ogUnmount $1 $PART 2>/dev/null ;;
1112    esac
1113done
1114}
1115
1116#/**
1117#         ogUnsetDirtyBit int_ndisk int_npart
1118#@brief   Inhabilita el Dirty Bit del sistema de ficheros NTFS para evitar un CHKDSK en el primer arranque
1119#@param   int_ndisk      nº de orden del disco
1120#@param   int_npart      nº de orden de partición
1121#@return  Nada
1122#@exception OG_ERR_FORMAT    Formato incorrecto.
1123#@version 0.1 - Versión para OpenGnSys.
1124#@author  Carmelo Cabezuelo, ASIC Universidad Politécnica de Valencia
1125#@date    2016/04/20
1126#*/ ##
1127function ogUnsetDirtyBit ()
1128{
1129# Variables locales
1130local PART
1131# Si se solicita, mostrar ayuda.
1132if [ "$*" == "help" ]; then
1133    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk" "FUNCNAME 1"
1134    return
1135fi
1136# Error si no se reciben 2 parámetros.
1137[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
1138
1139# Obtener partición y punto de montaje.
1140case "$(ogGetFsType $1 $2)" in
1141    NTFS)
1142        ogUnmount $1 $2 2>/dev/null
1143        PART="$(ogDiskToDev $1 $2)" || return $?
1144        ntfsfix -d $PART ;;
1145    *) ;;
1146esac
1147}
1148
1149
1150#/**
1151#         ogGetFreeSize int_disco int_partition str_SizeOutput
1152#@brief   muestra informacion del tamaño total, datos y libre.
1153#@param   int_ndisk     nº de orden del disco
1154#@param   int_npart     nº de orden de partición
1155#@param   str_unitSize      unidad mostrada
1156#@return  int_size:int_data:int_free
1157#@TODO  Componer corretcamente esta función.
1158#@exception OG_ERR_FORMAT    Formato incorrecto.
1159#@version
1160#@author 
1161#@date   
1162#*/ ##
1163
1164function ogGetFreeSize ()
1165{
1166local particion unit factor valor
1167if [ $# = 0 ]
1168then
1169        echo "sintaxis: ogGetFreeSize int_disco int_partition str_SizeOutput [ kB MB GB -default GB]-]" red
1170        echo "devuelve int_size : int_data : int_free" red
1171return
1172fi
1173if [ $# -ge 2 ]
1174then
1175        particion=`ogMount $1 $2 ` #1>/dev/null 2>&1
1176        if [ -z $3 ]
1177                then
1178                        unit=kB  # s B kB MB GB TB %
1179                else
1180                        unit=$3
1181        fi
1182        case $unit in
1183                kB)
1184                        factor="1.024";
1185                        #valor=`df | grep  $particion | awk -F" " '{size=$2*1.024; used=$3*1.024; free=$4*1.024; printf "%d:%d:%d", size,used,free}'`
1186                        valor=`df | grep  $particion | awk -F" " '{size=$2*1.024; used=$3*1.024; free=$4*1.024; printf "%d", free}'`
1187                        ;;
1188                MB)
1189                        factor="1.024/1000";
1190                        valor=`df | grep  $particion | awk -F" " '{size=$2*1.024/1000; used=$3*1.024/1000; free=$4*1.024/1000; printf "%d:%d:%d", size,used,free}'`
1191                ;;
1192                GB)
1193                        factor="1.024/1000000";
1194                        valor=`df | grep $particion | awk -F" " '{size=$2*1.024/1000000; used=$3*1.024/1000000; free=$4*1.024/1000000; printf "%f:%f:%f", size,used,free}'`
1195                ;;
1196        esac
1197        #echo $valor
1198        #NumberRound $valor
1199        #valor=`NumberRound $valor`;
1200        echo $valor
1201fi
1202}
1203
Note: See TracBrowser for help on using the repository browser.