source: client/engine/FileSystem.lib @ 5d05b06

Last change on this file since 5d05b06 was cbebec2, checked in by Ramón M. Gómez <ramongomez@…>, 5 years ago

#960: ogReduceFs function can detect Btrfs over LVM metadevice.

  • Property mode set to 100755
File size: 39.2 KB
RevLine 
[2e15649]1#!/bin/bash
2#/**
3#@file    FileSystem.lib
[9f57de01]4#@brief   Librería o clase FileSystem
[2e15649]5#@class   FileSystem
6#@brief   Funciones para gestión de sistemas de archivos.
[0d6e7222]7#@version 1.1.0
[2e15649]8#@warning License: GNU GPLv3+
9#*/
10
11
[be81649]12#/**
[b6208d8]13#         ogCheckFs int_ndisk int_nfilesys
[be81649]14#@brief   Comprueba el estado de un sistema de archivos.
[42669ebf]15#@param   int_ndisk      nº de orden del disco
[b6208d8]16#@param   int_nfilesys   nº de orden del sistema de archivos
[be81649]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*
[a3348ce]22#@warning No se comprueban sistemas de archivos montados o bloqueados.
23#@todo    Definir salidas.
[1616b6e]24#@version 0.9 - Primera adaptación para OpenGnSys.
[be81649]25#@author  Ramon Gomez, ETSII Universidad de Sevilla
26#@date    2009-10-07
[1616b6e]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
[02f271b]30#@version 1.0.4 - Soportar HFS/HFS+.
31#@author  Ramon Gomez, ETSII Universidad de Sevilla
32#@date    2012-05-21
[3198512]33#@version 1.0.5 - Desmontar antes de comprobar, soportar Btrfs y ExFAT.
[3011075]34#@author  Ramon Gomez, ETSII Universidad de Sevilla
35#@date    2012-09-05
[48c9e6e]36#@version 1.1.0 - Soportar F2FS.
37#@author  Ramon Gomez, ETSII Universidad de Sevilla
38#@date    2016-05-03
[6e390b1]39#*/ ##
[42669ebf]40function ogCheckFs ()
41{
[3458879]42# Variables locales.
[cbbb046]43local PART TYPE PROG PARAMS CODES ERRCODE
44# Si se solicita, mostrar ayuda.
45if [ "$*" == "help" ]; then
[e087194]46    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_nfilesys" \
[cbbb046]47           "$FUNCNAME 1 1"
48    return
49fi
[be81649]50
[1616b6e]51# Error si no se reciben 2 parámetros.
[1956672]52[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
[1616b6e]53# Obtener partición.
[1956672]54PART="$(ogDiskToDev $1 $2)" || return $?
[be81649]55
56TYPE=$(ogGetFsType $1 $2)
57case "$TYPE" in
[f097c21]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 $? ;;
[1956672]76esac
[1616b6e]77# Error si el sistema de archivos esta montado o bloqueado.
[3011075]78ogUnmount $1 $2
[a3348ce]79if ogIsMounted $1 $2; then
[7250491]80    ogRaiseError $OG_ERR_PARTITION "$1 $2"       # Indicar nuevo error
[a3348ce]81    return $?
82fi
83if ogIsLocked $1 $2; then
84    ogRaiseError $OG_ERR_LOCKED "$1 $2"
85    return $?
86fi
[1616b6e]87# Comprobar en modo uso exclusivo.
[a3348ce]88ogLock $1 $2
[7b9dedd]89trap "ogUnlock $1 $2" 1 2 3 6 9
[a3348ce]90eval $PROG $PARAMS $PART
91ERRCODE=$?
92case $ERRCODE in
[ea8051a]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 ;;
[a3348ce]99esac
100ogUnlock $1 $2
101return $ERRCODE
[1956672]102}
103
104
[2e15649]105#/**
[e087194]106#         ogExtendFs int_ndisk int_nfilesys
[3f49cf7]107#@brief   Extiende un sistema de archivos al tamaño de su partición.
[42669ebf]108#@param   int_ndisk      nº de orden del disco
[b6208d8]109#@param   int_nfilesys   nº de orden del sistema de archivos
[3f49cf7]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*
[985bef0]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
[b6208d8]118#@version 0.9 - Primera adaptacion para OpenGnSys.
[3f49cf7]119#@author  Ramon Gomez, ETSII Universidad de Sevilla
120#@date    2009-09-23
[452ade2]121#@version 1.0.5 - Soporte para BTRFS.
122#@author  Ramon Gomez, ETSII Universidad de Sevilla
123#@date    2012-06-28
[6e390b1]124#*/ ##
[42669ebf]125function ogExtendFs ()
126{
[3f49cf7]127# Variables locales.
[3198512]128local PART TYPE PROG PARAMS ERRCODE DOMOUNT
[3f49cf7]129
[1616b6e]130# Si se solicita, mostrar ayuda.
[3f49cf7]131if [ "$*" == "help" ]; then
[e087194]132    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_nfilesys" \
[3f49cf7]133           "$FUNCNAME 1 1"
134    return
135fi
[1616b6e]136# Error si no se reciben 2 parámetros.
[3f49cf7]137[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
138
[1616b6e]139# Obtener partición.
[3f49cf7]140PART="$(ogDiskToDev $1 $2)" || return $?
141
[1616b6e]142# Redimensionar al tamano máximo según el tipo de partición.
[be81649]143TYPE=$(ogGetFsType $1 $2)
144case "$TYPE" in
[2717297]145    EXT[234])   PROG="resize2fs"; PARAMS="-f" ;;
[3198512]146    BTRFS)      PROG="btrfs"; PARAMS="filesystem resize max"
147                DOMOUNT=1     # Debe estar montado.
148                ;;
149    REISERFS|REISER4)
150                PROG="resize_reiserfs"; PARAMS="-f" ;;
[5bc8d01]151    F2FS)       ;;            # No se reduce (por el momento).
[c452625]152    JFS)        ;;            # No se reduce (por el momento).
[5bc8d01]153    NILFS2)     ;;            # No se reduce (probar "nilfs-resize").
[c452625]154    XFS)        ;;            # No se reduce (por el momento).
[5804229]155    NTFS)       PROG="ntfsresize"; PARAMS="<<<\"y\" -f" ;;
[c452625]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).
[2717297]160    *)          ogRaiseError $OG_ERR_PARTITION "$1 $2 $TYPE"
161                return $? ;;
[3f49cf7]162esac
[c452625]163# Salida normal si no se va a aplicar la operación.
164[ -z "$PROG" ] && return
[3198512]165# Error si el sistema de archivos no se queda en el estado de montaje adecuado.
166if [ "$DOMOUNT" ]; then
[5962edd]167    PART=$(ogMount $1 $2) || return $?                      # Indicar nuevo error
[3198512]168else
169    ogUnmount $1 $2 2>/dev/null
[7376c5b]170    if ogIsMounted $1 $2; then
171         ogRaiseError $OG_ERR_PARTITION "$1 $2"             # Indicar nuevo error
172         return $?
173    fi
[2717297]174fi
[3198512]175# Error si el sistema de archivos está bloqueado.
[2717297]176if ogIsLocked $1 $2; then
177    ogRaiseError $OG_ERR_LOCKED "$1 $2"
178    return $?
179fi
[1616b6e]180# Redimensionar en modo uso exclusivo.
[2717297]181ogLock $1 $2
[7b9dedd]182trap "ogUnlock $1 $2" 1 2 3 6 9
[1c04494]183eval $PROG $PARAMS $PART &>/dev/null
[2717297]184ERRCODE=$?
185case $ERRCODE in
186    0)    ;;
[ea8051a]187    127)  ogRaiseError $OG_ERR_NOTEXEC "$PROG"
188          ERRCODE=$OG_ERR_NOTEXEC ;;
189    *)    ogRaiseError $OG_ERR_PARTITION "$1 $2"
190          ERRCODE=$OG_ERR_PARTITION ;;
[2717297]191esac
192ogUnlock $1 $2
193return $ERRCODE
[3f49cf7]194}
195
196
197#/**
[e087194]198#         ogFormat int_ndisk int_nfilesys | CACHE
[e09311f]199#@see     ogFormatFs ogFormatCache
[6e390b1]200#*/ ##
[42669ebf]201function ogFormat ()
202{
[e09311f]203case "$*" in
204    CACHE|cache)  ogFormatCache ;;
205    *)            ogFormatFs "$@" ;;
206esac
[40488da]207}
208
209
210#/**
[b6208d8]211#         ogFormatFs int_ndisk int_nfilesys [type_fstype] [str_label]
[40488da]212#@brief   Formatea un sistema de ficheros según el tipo de su partición.
[42669ebf]213#@param   int_ndisk      nº de orden del disco
[b6208d8]214#@param   int_nfilesys   nº de orden del sistema de archivos
[3198512]215#@param   type_fstype    mnemónico de sistema de ficheros a formatear (opcional al reformatear)
[42669ebf]216#@param   str_label      etiqueta de volumen (opcional)
[40488da]217#@return  (por determinar)
[3198512]218#@exception OG_ERR_FORMAT    Formato de ejecución incorrecto.
[40488da]219#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
220#@exception OG_ERR_PARTITION Partición no accesible o desconocida.
[a3348ce]221#@note    Requisitos:   mkfs*
222#@warning No formatea particiones montadas ni bloqueadas.
223#@todo    Definir salidas.
[b6208d8]224#@version 0.9 - Primera versión para OpenGnSys.
[40488da]225#@author  Ramon Gomez, ETSII Universidad de Sevilla
[a3348ce]226#@date    2009-10-08
[066fa01]227#@version 1.0.4 - Solucionado error cuando no se detecta tipo de sistema de ficheros pero si se indica.
[e068946]228#@author  Universidad de Huelva
229#@date    2012-04-11
[3198512]230#@version 1.0.5 - Comprobar errores al inicio e independizar del tipo de tabla de particiones.
[066fa01]231#@author  Universidad de Huelva
[3198512]232#@date    2013-05-16
[48c9e6e]233#@version 1.1.0 - Soportar F2FS y NILFS.
234#@author  Ramon Gomez, ETSII Universidad de Sevilla
235#@date    2016-05-03
[1e7eaab]236#*/ ##
[42669ebf]237function ogFormatFs ()
238{
[40488da]239# Variables locales
[0d6e7222]240local PART ID TYPE LABEL PROG PARAMS LABELPARAM ERRCODE
[40488da]241
[42669ebf]242# Si se solicita, mostrar ayuda.
[40488da]243if [ "$*" == "help" ]; then
[e087194]244    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_nfilesys [str_label]" \
[40488da]245           "$FUNCNAME 1 1" \
[be81649]246           "$FUNCNAME 1 1 EXT4" \
[55ad138c]247           "$FUNCNAME 1 1 \"DATA\"" \
248           "$FUNCNAME 1 1 EXT4 \"DATA\""
[40488da]249    return
250fi
[1e7eaab]251# Error si no se reciben entre 2 y 4 parámetros.
[be81649]252[ $# -ge 2 -a $# -le 4 ] || ogRaiseError $OG_ERR_FORMAT || return $?
[8e29877]253# Obtener fichero de dispositivo.
[40488da]254PART="$(ogDiskToDev $1 $2)" || return $?
[8e29877]255# Error si la partición está montada o bloqueada.
256if ogIsMounted $1 $2; then
[1a2fa9d8]257    ogRaiseError $OG_ERR_DONTFORMAT "$MSG_MOUNT: $1 $2"
[8e29877]258    return $?
259fi
260if ogIsLocked $1 $2; then
261    ogRaiseError $OG_ERR_LOCKED "$1 $2"
262    return $?
263fi
[3198512]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 $?
[be81649]268
[3198512]269# Elegir tipo de formato.
270case "$TYPE" in
[3d70f46]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" ;;
[3198512]275    REISERFS)     PROG="mkfs.reiserfs"; PARAMS="-f"; LABELPARAM="-l" ;;
[5bc8d01]276    REISER4)      PROG="mkfs.reiser4"; PARAMS="-f <<<\"y\"" ;;
[3198512]277    XFS)          PROG="mkfs.xfs"; PARAMS="-f" ;;
278    JFS)          PROG="mkfs.jfs"; PARAMS="<<<\"y\"" ;;
[48c9e6e]279    F2FS)         PROG="mkfs.f2fs"; LABELPARAM="-l" ;;
280    NILFS2)       PROG="mkfs.nilfs2"; PARAMS="-f" ;;
[3198512]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 $? ;;
[40488da]292esac
[be81649]293
[1e7eaab]294# Etiquetas de particion.
[be81649]295if [ -z "$LABEL" ]; then
296    [ "$4" != "CACHE" ] || ogRaiseError $OG_ERR_FORMAT "$MSG_RESERVEDVALUE: CACHE" || return $?
[3198512]297    [ -n "$4" ] && PARAMS="$PARAMS ${LABELPARAM:-"-L"} $4"
[be81649]298else
[3198512]299    PARAMS="$PARAMS ${LABELPARAM:-"-L"} $LABEL"
[be81649]300fi
[40488da]301
[8e29877]302# Formatear en modo uso exclusivo (desmontar siempre).
[40488da]303ogLock $1 $2
[7b9dedd]304trap "ogUnlock $1 $2" 1 2 3 6 9
[3198512]305umount $PART 2>/dev/null
[a3348ce]306eval $PROG $PARAMS $PART 2>/dev/null
[be81649]307ERRCODE=$?
308case $ERRCODE in
309    0)    ;;
310    127)  ogRaiseError $OG_ERR_NOTEXEC "$PROG" ;;
311    *)    ogRaiseError $OG_ERR_PARTITION "$1 $2" ;;
312esac
[40488da]313ogUnlock $1 $2
[be81649]314return $ERRCODE
[40488da]315}
316
317
318#/**
[7b9dedd]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.
[68f360e]338local MNTDIR UNIT VALUE FACTOR SIZE
[7b9dedd]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 $?
[68f360e]348# Obtener unidad y factor de medida.
[7b9dedd]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
[68f360e]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*}
[7b9dedd]371}
372
373
374#/**
[b6208d8]375#         ogGetFsType int_ndisk int_nfilesys
[9f57de01]376#@brief   Devuelve el mnemonico con el tipo de sistema de archivos.
[42669ebf]377#@param   int_ndisk      nº de orden del disco
[b6208d8]378#@param   int_nfilesys   nº de orden del sistema de archivos
[9f57de01]379#@return  Mnemonico
[264b1bc]380#@note    Mnemonico: { EXT2, EXT3, EXT4, BTRFS, REISERFS, XFS, JFS, FAT12, FAT16, FAT32, NTFS, LINUX-SWAP, LINUX-LVM, LINUX-RAID, HFS, HFSPLUS, CACHE }
[9f57de01]381#@exception OG_ERR_FORMAT   Formato incorrecto.
382#@exception OG_ERR_NOTFOUND Disco o particion no corresponden con un dispositivo.
[985bef0]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
[5804229]386#@version 0.9 - Primera adaptacion para OpenGnSys.
[9f57de01]387#@author  Ramon Gomez, ETSII Universidad de Sevilla
[5dbb046]388#@date    2009-07-21
[5804229]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
[264b1bc]392#@version 1.0.5 - Usar "blkid" para detectar tipo de sistema de archivo.
[b6208d8]393#@author  Ramon Gomez, ETSII Universidad de Sevilla
[264b1bc]394#@date    2014-06-10
[0d6e7222]395#@version 1.1.0 - Detectar volumen ZFS.
396#@author  Ramon Gomez, ETSII Universidad de Sevilla
397#@date    2014-11-14
[1e7eaab]398#*/ ##
[cbbb046]399function ogGetFsType ()
400{
[59f9ad2]401# Variables locales.
[9eaa464]402local PART TYPE
[1e7eaab]403# Si se solicita, mostrar ayuda.
[59f9ad2]404if [ "$*" == "help" ]; then
[e087194]405    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_nfilesys" \
[59f9ad2]406           "$FUNCNAME 1 1  =>  NTFS"
407    return
408fi
[1e7eaab]409# Error si no se reciben 2 parámetros.
[a5df9b9]410[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
[2e15649]411
[264b1bc]412# Detectar tipo de sistema de archivo (independientemente del tipo de partición).
[b6208d8]413PART=$(ogDiskToDev "$1" "$2") || return $?
[0d6e7222]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
[264b1bc]420
421# Componer valores correctos.
422case "$TYPE" in
423    EXT4)      # Comprobar si es caché o Ext4.
[0d6e7222]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) }')" ;;
[264b1bc]429    SWAP)      TYPE="LINUX-SWAP" ;;
430    LVM*)      TYPE="LINUX-LVM" ;;
431    *RAID*)    TYPE="LINUX-RAID" ;;
[0d6e7222]432    ZFS_MEMBER) TYPE="ZVOL" ;;
[264b1bc]433    *_MEMBER)  TYPE="${TYPE/_MEMBER/}" ;;
[2e15649]434esac
[5804229]435
436[ -n "$TYPE" ] && echo "$TYPE"
[2e15649]437}
438
[aae34f6]439
[a3348ce]440#/**
[b6208d8]441#         ogGetMountPoint int_ndisk int_nfilesys
[a3348ce]442#@brief   Devuelve el punto de montaje de un sistema de archivos.
[42669ebf]443#@param   int_ndisk      nº de orden del disco
[b6208d8]444#@param   int_nfilesys   nº de orden del sistema de archivos
[f8f4dfa]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.
[3458879]448#@note    Requisitos: \c mount* \c awk
[b6208d8]449#@version 0.9 - Primera versión para OpenGnSys.
[f8f4dfa]450#@author  Ramon Gomez, ETSII Universidad de Sevilla
451#@date    2009-10-15
[c632cca]452#@version 1.0.6 - Usar comando findmnt.
453#@author  Ramon Gomez, ETSII Universidad de Sevilla
454#@date    2014-09-04
[1e7eaab]455#*/ ##
[42669ebf]456function ogGetMountPoint ()
457{
[a3348ce]458# Variables locales
459local PART
[1e7eaab]460# Si se solicita, mostrar ayuda.
[a3348ce]461if [ "$*" == "help" ]; then
[e087194]462    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_nfilesys" \
[a3348ce]463           "$FUNCNAME 1 1  =>  /mnt/sda1"
464    return
465fi
[1e7eaab]466# Error si no se reciben 2 parámetros.
[a3348ce]467[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
[1e7eaab]468# Obtener partición.
[a3348ce]469PART="$(ogDiskToDev $1 $2)" || return $?
470
[c632cca]471# Devolver punto de montaje.
472findmnt -n -o TARGET $PART
[a3348ce]473}
474
475
476#/**
[b6208d8]477#         ogIsFormated int_ndisk int_nfilesys
[5a4f399]478#@brief   Comprueba si un sistema de archivos está formateado.
479#@param   int_ndisk      nº de orden del disco o volumen.
[b6208d8]480#@param   int_nfilesys   nº de orden del sistema de archivos
[7685100]481#@return  Código de salida: 0 - formateado, 1 - sin formato o error.
[5a4f399]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
[7685100]485#@version 1.0.1 - Devolver falso en caso de error.
486#@author  Ramon Gomez, ETSII Universidad de Sevilla
487#@date    2011-05-18
[b6208d8]488#@version 1.0.5 - Dejar de usar "parted".
489#@author  Ramon Gomez, ETSII Universidad de Sevilla
[3011075]490#@date    2012-09-04
[3e3cfdb]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
[5a4f399]494#*/ ##
495function ogIsFormated ()
496{
497# Variables locales
[3e3cfdb]498local PART
[5a4f399]499if [ "$*" == "help" ]; then
[e087194]500    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_nfilesys" \
[5a4f399]501           "if $FUNCNAME 1 1; then ... ; fi"
502    return
503fi
[7685100]504# Falso, en caso de error.
505[ $# == 2 ] || return 1
[3e3cfdb]506PART="$(ogDiskToDev $1 $2 2>/dev/null)" || return 1
[5a4f399]507
[3e3cfdb]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
[5a4f399]516}
517
518
519#/**
[b32f902]520#         ogIsLocked int_ndisk int_npartition
[858b1b0]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.
[b32f902]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.
[858b1b0]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 "-".
[b32f902]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
[858b1b0]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
[b32f902]544#*/ ##
[858b1b0]545function ogIsPartitionLocked ()
[b32f902]546{
547# Variables locales
[858b1b0]548local DISK PART LOCKDISK LOCKPART
[b32f902]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
[3e3cfdb]558PART="$(ogDiskToDev $1 $2 2>/dev/null)" || return 1
[858b1b0]559DISK="$(ogDiskToDev $1)"
[b32f902]560
[858b1b0]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
[b32f902]565}
566
567
568#/**
[b6208d8]569#         ogIsMounted int_ndisk int_nfilesys
[a3348ce]570#@brief   Comprueba si un sistema de archivos está montado.
[42669ebf]571#@param   int_ndisk      nº de orden del disco
[b6208d8]572#@param   int_nfilesys   nº de orden del sistema de archivos
[7685100]573#@return  Código de salida: 0 - montado, 1 - sin montar o error.
[b6208d8]574#@version 0.9 - Primera versión para OpenGnSys.
[f8f4dfa]575#@author  Ramon Gomez, ETSII Universidad de Sevilla
576#@date    2009-10-15
[7685100]577#@version 1.0.1 - Devolver falso en caso de error.
578#@author  Ramon Gomez, ETSII Universidad de Sevilla
579#@date    2011-05-18
[1e7eaab]580#*/ ##
[42669ebf]581function ogIsMounted ()
582{
583# Si se solicita, mostrar ayuda.
[a3348ce]584if [ "$*" == "help" ]; then
[e087194]585    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_nfilesys" \
[a3348ce]586           "if $FUNCNAME 1 1; then ... ; fi"
587    return
588fi
[7685100]589# Falso, en caso de error.
590[ $# == 2 ] || return 1
[a3348ce]591
592test -n "$(ogGetMountPoint $1 $2)"
593}
594
595
[aae34f6]596#/**
[3249002]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
[d01525b]605#*/ ##
606
[3249002]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
[3e3cfdb]620PART="$(ogDiskToDev $1 $2 2>/dev/null)" || return 1
[3249002]621
622test -n "$(findmnt -n -o OPTIONS $PART | awk 'BEGIN {RS=","} /^ro$/ {print}')"
623}
624
625
626#/**
[b32f902]627#         ogIsWritable int_ndisk int_nfilesys
628#@brief   Comprueba si un sistema de archivos está montado de lectura y escritura.
[42669ebf]629#@param   int_ndisk      nº de orden del disco
[b32f902]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.
[7685100]633#@author  Ramon Gomez, ETSII Universidad de Sevilla
[b32f902]634#@date    2013-10-09
[d01525b]635#*/ ##
[b32f902]636function ogIsWritable ()
[42669ebf]637{
[59f9ad2]638# Variables locales
[b32f902]639local PART
[9d96c57]640
[1e7eaab]641# Si se solicita, mostrar ayuda.
[9d96c57]642if [ "$*" == "help" ]; then
[b32f902]643    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_filesys" \
[a3348ce]644           "if $FUNCNAME 1 1; then ... ; fi"
[9d96c57]645    return
646fi
[7685100]647# Falso, en caso de error.
648[ $# == 2 ] || return 1
[3e3cfdb]649PART="$(ogDiskToDev $1 $2 2>/dev/null)" || return 1
[9d96c57]650
[b32f902]651test -n "$(findmnt -n -o OPTIONS $PART | awk 'BEGIN {RS=","} /^rw$/ {print}')"
[9d96c57]652}
653
654
655#/**
656#         ogLock int_ndisk int_npartition
[89403cd]657#@see     ogLockPartition
658#*/
[42669ebf]659function ogLock ()
660{
[89403cd]661ogLockPartition "$@"
662}
663
664#/**
665#         ogLockPartition int_ndisk int_npartition
[9d96c57]666#@brief   Genera un fichero de bloqueo para una partición en uso exlusivo.
[42669ebf]667#@param   int_ndisk      nº de orden del disco
668#@param   int_npartition nº de orden de la partición
[9d96c57]669#@return  (nada)
670#@exception OG_ERR_FORMAT    Formato incorrecto.
671#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
[73c8417]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 "-".
[b6208d8]673#@version 0.9 - Primera versión para OpenGnSys.
[9d96c57]674#@author  Ramon Gomez, ETSII Universidad de Sevilla
675#@date    2009-09-03
[1e7eaab]676#*/ ##
[42669ebf]677function ogLockPartition ()
678{
[59f9ad2]679# Variables locales
680local PART LOCKFILE
[9d96c57]681
[1e7eaab]682# Si se solicita, mostrar ayuda.
[9d96c57]683if [ "$*" == "help" ]; then
684    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
685           "$FUNCNAME 1 1"
686    return
687fi
[1e7eaab]688# Error si no se reciben 2 parámetros.
[9d96c57]689[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
690
[1e7eaab]691# Obtener partición.
[9d96c57]692PART="$(ogDiskToDev $1 $2)" || return $?
693
[1e7eaab]694# Crear archivo de bloqueo exclusivo.
[73c8417]695LOCKFILE="/var/lock/lock${PART//\//-}"
696touch $LOCKFILE
[9d96c57]697}
698
699
700#/**
[b6208d8]701#         ogMount int_ndisk int_nfilesys
[3458879]702#@see     ogMountFs ogMountCache ogMountCdrom
[1e7eaab]703#*/ ##
[42669ebf]704function ogMount ()
705{
[ee4a96e]706case "$*" in
[18f4bc2]707    CACHE|cache)
708        ogMountCache ;;
[ee4a96e]709    CDROM|cdrom)
710        ogMountCdrom ;;
711    *)  ogMountFs "$@" ;;
712esac
[73c8417]713}
714
[ee4a96e]715
[73c8417]716#/**
[9eaa464]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#/**
[b6208d8]745#         ogMountFs int_ndisk int_nfilesys
[aae34f6]746#@brief   Monta un sistema de archivos.
[42669ebf]747#@param   int_ndisk      nº de orden del disco
[b6208d8]748#@param   int_nfilesys   nº de orden del sistema de archivos
[aae34f6]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.
[985bef0]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
[b6208d8]756#@version 0.9 - Primera version para OpenGnSys.
[aae34f6]757#@author  Ramon Gomez, ETSII Universidad de Sevilla
[40488da]758#@date    2009-09-28
[b6208d8]759#@version 1.0.5 - Independiente del tipo de sistema de ficheros.
[02f271b]760#@author  Ramon Gomez, ETSII Universidad de Sevilla
[3011075]761#@date    2012-09-04
[ae4c525]762#@version 1.1.0 - Montar sistema de archivos ZFS y NTFS hibernado.
[0d6e7222]763#@author  Ramon Gomez, ETSII Universidad de Sevilla
[ae4c525]764#@date    2016-09-19
[1e7eaab]765#*/ ##
[42669ebf]766function ogMountFs ()
767{
[1e7eaab]768# Variables locales
[b6208d8]769local PART MNTDIR
[aae34f6]770
[1e7eaab]771# Si se solicita, mostrar ayuda.
[aae34f6]772if [ "$*" == "help" ]; then
[e087194]773    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_nfilesys" \
[aae34f6]774           "$FUNCNAME 1 1  =>  /mnt/sda1"
775    return
776fi
[1e7eaab]777# Error si no se reciben 2 parámetros.
[aae34f6]778[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
779
[1e7eaab]780# Obtener partición.
[b6208d8]781PART="$(ogDiskToDev "$1" "$2")" || return $?
[aae34f6]782
[1e7eaab]783# Comprobar si el sistema de archivos ya está montada.
[a3348ce]784MNTDIR="$(ogGetMountPoint $1 $2)"
[e2c805a]785# Si no, montarlo en un directorio de sistema.
[aae34f6]786if [ -z "$MNTDIR" ]; then
787    # Error si la particion esta bloqueada.
[52fa3da]788    if ogIsLocked $1 $2; then
789        ogRaiseError $OG_ERR_LOCKED "$MSG_PARTITION, $1 $2"
790        return $?
791    fi
[0d6e7222]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}
[3249002]796        DEBUG="no"
[0d6e7222]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
[3249002]803        unset DEBUG
[0d6e7222]804        # Montar sistema de archivos.
805        mount $PART $MNTDIR &>/dev/null || \
[ae4c525]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
[3249002]819        # Aviso de montaje de solo lectura.
820        if ogIsReadonly $1 $2; then
821            ogEcho warning "$FUNCNAME: $MSG_MOUNTREADONLY: \"$1, $2\""
822        fi
[e2c805a]823    else
[0d6e7222]824        # Montar sistema de archivos ZFS (un ZPOOL no comienza por "/").
825        zfs mount $PART 2>/dev/null
[e2c805a]826    fi
[aae34f6]827fi
[cbbb046]828echo "$MNTDIR"
[aae34f6]829}
830
831
[cade8c0]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#*/ ##
[42669ebf]842function ogMountCdrom ()
843{
[ee4a96e]844local DEV MNTDIR
[cade8c0]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 $?
[ee4a96e]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
[3f49cf7]862
863#/**
[b6208d8]864#         ogReduceFs int_ndisk int_nfilesys
[3f49cf7]865#@brief   Reduce el tamaño del sistema de archivos, sin tener en cuenta el espacio libre.
[42669ebf]866#@param   int_ndisk      nº de orden del disco
[b6208d8]867#@param   int_nfilesys   nº de orden del sistema de archivos
[d3669a2]868#@return  int_tamañoKB - tamaño en KB
[3f49cf7]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.
[df097c2]872#@warning En Windows, se borran los ficheros de hiberanción y de paginación.
[d3669a2]873#@warning El sistema de archivos se amplía al mínimo + 10%.
[3458879]874#@note    Requisitos:   *resize*
[985bef0]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
[b6208d8]878#@version 0.9 - Primera version para OpenGnSys.
[3f49cf7]879#@author  Ramon Gomez, ETSII Universidad de Sevilla
880#@date    2009-09-23
[d3669a2]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
[c114529]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
[3be0219]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
[cbebec2]890#@version 1.1.1b - Detectar metadispositivos.
891#@author  Ramon Gomez, ETSII Universidad de Sevilla
892#@date    2020-02-24
[1e7eaab]893#*/ ##
[42669ebf]894function ogReduceFs ()
895{
[3f49cf7]896# Variables locales
[3be0219]897local PART BLKS SIZE MAXSIZE EXTRASIZE=0 RETVAL
[3f49cf7]898
[1e7eaab]899# Si se solicita, mostrar ayuda.
[3f49cf7]900if [ "$*" == "help" ]; then
[e087194]901    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_nfilesys" \
[3f49cf7]902           "$FUNCNAME 1 1"
903    return
904fi
[1e7eaab]905# Error si no se reciben 2 parámetros.
[3f49cf7]906[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
907
[1e7eaab]908# Obtener partición.
[cbebec2]909PART="$(readlink -f "$(ogDiskToDev $1 $2)")" || return $?
[3f49cf7]910
[1e7eaab]911# Redimensionar según el tipo de particion.
[3f49cf7]912case "$(ogGetFsType $1 $2)" in
[1c04494]913    EXT[234])
[cbebec2]914        ogUnmount $1 $2 &>/dev/null
[c114529]915        resize2fs -fpM $PART  &>/dev/null || ogRaiseError $OG_ERR_PARTITION "$1,$2" || return $?
[743257e]916        ;;
[3be0219]917
[3198512]918    BTRFS)
919        MNTDIR=$(ogMount $1 $2)
[5bc8d01]920        # Calcular tamaño ocupado + 10%, redondeado + 1 (incluyendo letra de unidad).
[cbebec2]921        SIZE=$(btrfs filesystem show $MNTDIR | awk -v P=$PART '{ d=""; "readlink -f "$8" 2>/dev/null"|getline d; if(d==P) printf("%d%s", $6*1.1+1, substr($6,match($6,/[A-Z]/),1)) }')
922        btrfs filesystem resize ${SIZE} $MNTDIR &>/dev/null
[3198512]923        ;;
924    REISERFS|REISER4)
925        # Calcular tamaño ocupado + 10%.
[3be0219]926        MNTDIR=$(ogMount $1 $2)
[3198512]927        SIZE=$[ $(df -k $MNTDIR | awk '{getline;print $3}') * 110 / 100 ]
928        ogUnmount $1 $2 2>/dev/null
[eb4614b]929        resize_reiserfs -s${SIZE}K $PART <<<"y"
[3198512]930        ;;
[3be0219]931
[5bc8d01]932    F2FS)   ;;          # No se reduce (por el momento).
[c452625]933    JFS)    ;;          # No se reduce (por el momento).
[5bc8d01]934    NILFS2) ;;          # No se reduce (probar "nilfs-resize").
[c452625]935    XFS)    ;;          # No se reduce (por el momento).
[3be0219]936
[b6208d8]937    NTFS)
[3be0219]938        # Calcular tamaño ocupado + 10%.
939        ogUnmount $1 $2 &>/dev/null
940        read -e MAXSIZE SIZE <<<$(ntfsresize -fi $PART | \
941                                  awk '/device size/ {d=$4}
942                                       /resize at/ {r=int($5*1.1/1024+1)*1024}
943                                       END { print d,r}')
944        # Error si no puede obtenerse el tamaño máximo del volumen.
[bec0f2f]945        [ -n "$MAXSIZE" -a -n "$SIZE" ] || ogRaiseError $OG_ERR_PARTITION "$1,$2" || return $?
[3be0219]946        # Simular la redimensión y comprobar si es necesario ampliarala.
947        RETVAL=1
948        while [ $RETVAL != 0 -a $[ SIZE+=EXTRASIZE ] -lt $MAXSIZE ]; do
949            # Obtener espacio de relocalización y devolver código de salida
950            # (ntfsresize devuelve 0 si no necesita relocalizar).
951            EXTRASIZE=$(ntfsresize -fns $SIZE $PART 2>/dev/null | \
952                        awk '/Needed relocations/ {print int($4*1.1/1024+1)*1024}'
953                        exit ${PIPESTATUS[0]})
954            RETVAL=$?
955        done
956        # Redimensionar solo si hace falta.
957        if [ $SIZE -lt $MAXSIZE ]; then
958            ntfsresize -fs $SIZE $PART <<<"y" >/dev/null || ogRaiseError $OG_ERR_PARTITION "$1,$2" || return $?
959        fi
[6277770]960        ;;
[3be0219]961
[c452625]962    EXFAT)  ;;          # No se reduce (por el momento).
[cbebec2]963    FAT32|FAT16)        # Se deja comentado por no haber un método seguro para extender el SF.
964        # Calcular tamaño ocupado + 10%.
965        #ogUnmount $1 $2 &>/dev/null
966        #SIZE=$(fatresize --info $PART | awk -F: '/Min size/ {printf("%d", $2*1.1)}')
967        #[ "$SIZE" ] && fatresize --size $SIZE $PART &>/dev/null
968        ;;
[c452625]969    HFS|HFSPLUS)  ;;    # No se reduce (por el momento).
970    UFS)    ;;          # No se reduce (por el momento).
[3be0219]971
[1c04494]972    *)  ogRaiseError $OG_ERR_PARTITION "$1,$2"
[6277770]973        return $? ;;
[3f49cf7]974esac
[c114529]975
[3be0219]976# Devuelve tamaño del sistema de ficheros.
977ogGetFsSize $1 $2
[c114529]978}
979
980
[5dbb046]981#/**
[9d96c57]982#         ogUnlock int_ndisk int_npartition
[89403cd]983#@see     ogUnlockPartition
[6e390b1]984#*/ ##
[42669ebf]985function ogUnlock ()
986{
[89403cd]987ogUnlockPartition "$@"
988}
989
990#/**
991#         ogUnlockPartition int_ndisk int_npartition
[9d96c57]992#@brief   Elimina el fichero de bloqueo para una particion.
[42669ebf]993#@param   int_ndisk      nº de orden del disco
994#@param   int_npartition nº de orden de la partición
[9d96c57]995#@return  (nada)
996#@exception OG_ERR_FORMAT    Formato incorrecto.
997#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
[73c8417]998#@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 "-".
[b6208d8]999#@version 0.9 - Primera versión para OpenGnSys.
[9d96c57]1000#@author  Ramon Gomez, ETSII Universidad de Sevilla
1001#@date    2009-09-03
[1e7eaab]1002#*/ ##
[42669ebf]1003function ogUnlockPartition ()
1004{
[59f9ad2]1005# Variables locales
1006local PART LOCKFILE
[9d96c57]1007
[1e7eaab]1008# Si se solicita, mostrar ayuda.
[9d96c57]1009if [ "$*" == "help" ]; then
1010    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
1011           "$FUNCNAME 1 1"
1012    return
1013fi
[1e7eaab]1014# Error si no se reciben 2 parámetros.
[9d96c57]1015[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
1016
[1e7eaab]1017# Obtener partición.
[9d96c57]1018PART="$(ogDiskToDev $1 $2)" || return $?
1019
[1e7eaab]1020# Borrar archivo de bloqueo exclusivo.
[73c8417]1021LOCKFILE="/var/lock/lock${PART//\//-}"
1022rm -f $LOCKFILE
[9d96c57]1023}
1024
1025
1026#/**
[5dbb046]1027#         ogUnmount int_ndisk int_npartition
[40488da]1028#@see     ogUnmountFs
[6e390b1]1029#*/ ##
[42669ebf]1030function ogUnmount ()
1031{
[40488da]1032ogUnmountFs "$@"
[89403cd]1033}
1034
1035#/**
[b6208d8]1036#         ogUnmountFs int_ndisk int_nfilesys
[5dbb046]1037#@brief   Desmonta un sistema de archivos.
[42669ebf]1038#@param   int_ndisk      nº de orden del disco
[b6208d8]1039#@param   int_nfilesys   nº de orden del sistema de archivos
[5dbb046]1040#@return  Nada
1041#@exception OG_ERR_FORMAT    Formato incorrecto.
1042#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
1043#@warning La partición no está previamente montada o no se puede desmontar.
[985bef0]1044#@version 0.1 -  Integracion para Opengnsys  -  EAC:  UmountPartition() en FileSystem.lib
1045#@author  Antonio J. Doblas Viso. Universidad de Malaga
1046#@date    2008-10-27
[b6208d8]1047#@version 0.9 - Primera version para OpenGnSys.
[5dbb046]1048#@author  Ramon Gomez, ETSII Universidad de Sevilla
[40488da]1049#@date    2009-09-28
[1e7eaab]1050#*/ ##
[42669ebf]1051function ogUnmountFs ()
1052{
[59f9ad2]1053# Variables locales
[c56dec5]1054local PART MNTDIR
[5dbb046]1055
[1e7eaab]1056# Si se solicita, mostrar ayuda.
[5dbb046]1057if [ "$*" == "help" ]; then
1058    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" "$FUNCNAME 1 1"
1059    return
1060fi
[1e7eaab]1061# Error si no se reciben 2 parámetros.
[5dbb046]1062[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
1063
[1e7eaab]1064# Obtener partición y punto de montaje.
[5dbb046]1065PART="$(ogDiskToDev $1 $2)" || return $?
[a3348ce]1066MNTDIR="$(ogGetMountPoint $1 $2)"
[5dbb046]1067
[1e7eaab]1068# Si está montada, desmontarla.
[c56dec5]1069if [ -n "$MNTDIR" ]; then
[5a4f399]1070    # Error si la particion está bloqueada.
[52fa3da]1071    if ogIsLocked $1 $2; then
1072        ogRaiseError $OG_ERR_LOCKED "$MSG_PARTITION $1, $2"
1073        return $?
1074    fi
[5a4f399]1075    # Desmontar y borrar punto de montaje.
[52fa3da]1076    umount $PART 2>/dev/null || ogEcho warning "$FUNCNAME: $MSG_DONTUNMOUNT: \"$1, $2\""
[5a4f399]1077    rmdir $MNTDIR 2>/dev/null || rm -f $MNTDIR 2>/dev/null
[5dbb046]1078else
1079    ogEcho warning "$MSG_DONTMOUNT: \"$1,$2\""
1080fi
1081}
1082
[ee4a96e]1083
[be81649]1084#/**
1085#         ogUnmountAll int_ndisk
1086#@brief   Desmonta todos los sistema de archivos de un disco, excepto el caché local.
[42669ebf]1087#@param   int_ndisk      nº de orden del disco
[be81649]1088#@return  Nada
1089#@exception OG_ERR_FORMAT    Formato incorrecto.
1090#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
1091#@warning No se desmonta la partición marcada como caché local.
[b6208d8]1092#@version 0.9 - Versión para OpenGnSys.
[be81649]1093#@author  Ramon Gomez, ETSII Universidad de Sevilla
[cbebec2]1094#@date    2009-10-07
[1e7eaab]1095#*/ ##
[42669ebf]1096function ogUnmountAll ()
1097{
[be81649]1098# Variables locales
[18f4bc2]1099local DISK PART
[1e7eaab]1100# Si se solicita, mostrar ayuda.
[18f4bc2]1101if [ "$*" == "help" ]; then
1102    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk" "FUNCNAME 1"
1103    return
1104fi
[1e7eaab]1105# Error si no se recibe 1 parámetro.
[18f4bc2]1106[ $# == 1 ] || ogRaiseError $OG_ERR_FORMAT || return $?
1107
[1e7eaab]1108# Obtener partición y punto de montaje.
[18f4bc2]1109DISK="$(ogDiskToDev $1)" || return $?
1110for ((PART=1; PART<=$(ogGetPartitionsNumber $1); PART++)); do
1111    case "$(ogGetFsType $1 $PART)" in
[5804229]1112        CACHE) ;;
[7c52c30]1113        *)     ogUnmount $1 $PART 2>/dev/null ;;
[7250491]1114    esac
[18f4bc2]1115done
1116}
1117
[62c18f0]1118#/**
1119#         ogUnsetDirtyBit int_ndisk int_npart
1120#@brief   Inhabilita el Dirty Bit del sistema de ficheros NTFS para evitar un CHKDSK en el primer arranque
1121#@param   int_ndisk      nº de orden del disco
1122#@param   int_npart      nº de orden de partición
1123#@return  Nada
1124#@exception OG_ERR_FORMAT    Formato incorrecto.
[cbebec2]1125#@version 1.1.0 - Versión para OpenGnsys.
[62c18f0]1126#@author  Carmelo Cabezuelo, ASIC Universidad Politécnica de Valencia
[cbebec2]1127#@date    2016-04-20
[62c18f0]1128#*/ ##
1129function ogUnsetDirtyBit ()
1130{
1131# Variables locales
1132local PART
1133# Si se solicita, mostrar ayuda.
1134if [ "$*" == "help" ]; then
1135    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk" "FUNCNAME 1"
1136    return
1137fi
1138# Error si no se reciben 2 parámetros.
1139[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
1140
1141# Obtener partición y punto de montaje.
1142case "$(ogGetFsType $1 $2)" in
1143    NTFS)
1144        ogUnmount $1 $2 2>/dev/null
1145        PART="$(ogDiskToDev $1 $2)" || return $?
1146        ntfsfix -d $PART ;;
1147    *) ;;
1148esac
1149}
[c114529]1150
[d01525b]1151
1152#/**
1153#         ogGetFreeSize int_disco int_partition str_SizeOutput
1154#@brief   muestra informacion del tamaño total, datos y libre.
1155#@param   int_ndisk     nº de orden del disco
1156#@param   int_npart     nº de orden de partición
1157#@param   str_unitSize      unidad mostrada
1158#@return  int_size:int_data:int_free
1159#@TODO  Componer corretcamente esta función.
1160#@exception OG_ERR_FORMAT    Formato incorrecto.
1161#@version
1162#@author 
1163#@date   
1164#*/ ##
1165
1166function ogGetFreeSize ()
1167{
[d5fc0dc]1168local particion unit factor valor
[c114529]1169if [ $# = 0 ]
1170then
1171        echo "sintaxis: ogGetFreeSize int_disco int_partition str_SizeOutput [ kB MB GB -default GB]-]" red
1172        echo "devuelve int_size : int_data : int_free" red
1173return
1174fi
1175if [ $# -ge 2 ]
1176then
1177        particion=`ogMount $1 $2 ` #1>/dev/null 2>&1
1178        if [ -z $3 ]
1179                then
1180                        unit=kB  # s B kB MB GB TB %
1181                else
1182                        unit=$3
1183        fi
1184        case $unit in
1185                kB)
1186                        factor="1.024";
1187                        #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}'`
1188                        valor=`df | grep  $particion | awk -F" " '{size=$2*1.024; used=$3*1.024; free=$4*1.024; printf "%d", free}'`
1189                        ;;
[a957f02]1190                MB)
1191                        factor="1.024/1000";
1192                        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}'`
1193                ;;
1194                GB)
1195                        factor="1.024/1000000";
1196                        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}'`
1197                ;;
1198        esac
1199        #echo $valor
1200        #NumberRound $valor
1201        #valor=`NumberRound $valor`;
1202        echo $valor
1203fi
[c114529]1204}
1205
Note: See TracBrowser for help on using the repository browser.