source: client/engine/FileSystem.lib @ 55fcaa6

918-git-images-111dconfigfileconfigure-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-instalacionwebconsole3
Last change on this file since 55fcaa6 was 9eaa464, checked in by ramon <ramongomez@…>, 7 years ago

#830: Integrar cambios de ticket:830.

git-svn-id: https://opengnsys.es/svn/branches/version1.1@5618 a21b9725-9963-47de-94b9-378ad31fedc9

  • Property mode set to 100755
File size: 38.7 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
[99d1786]58    EXT[234])     PROG="e2fsck"; PARAMS="-y"; CODES=(1 2) ;;
[9eaa464]59    BTRFS)        PROG="btrfsck"; CODES=(1) ;;
[ea8051a]60    REISERFS)     PROG="fsck.reiserfs"; PARAMS="<<<\"Yes\""; CODES=(1 2) ;;
[1616b6e]61    REISER4)      PROG="fsck.reiser4"; PARAMS="-ay" ;;
[ea8051a]62    JFS)          PROG="fsck.jfs"; CODES=(1 2) ;;
[5bc8d01]63    XFS)          PROG="xfs_repair" ;;
[48c9e6e]64    F2FS)         PROG="fsck.f2fs" ;;
[5804229]65    NTFS)         PROG="ntfsfix" ;;
[3198512]66    EXFAT)        PROG="fsck.exfat" ;;
[9eaa464]67    FAT32)        PROG="dosfsck"; PARAMS="-a"; CODES=(1) ;;
68    FAT16)        PROG="dosfsck"; PARAMS="-a"; CODES=(1) ;;
69    FAT12)        PROG="dosfsck"; PARAMS="-a"; CODES=(1) ;;
[1cbf9e0]70    HFS)          PROG="fsck.hfs"; PARAMS="-f" ;;
71    HFSPLUS)      PROG="fsck.hfs"; PARAMS="-f" ;;
[3198512]72    UFS)          PROG="fsck.ufs" ;;
[0d6e7222]73    ZFS)          PROG="fsck.zfs" ;;
[c7d9af7]74    *)            ogRaiseError $OG_ERR_PARTITION "$1, $2, $TYPE"
[1616b6e]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
605#/**
606function ogIsReadonly ()
607{
608# Variables locales
609local PART
610
611# Si se solicita, mostrar ayuda.
612if [ "$*" == "help" ]; then
613    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_filesys" \
614           "if $FUNCNAME 1 1; then ... ; fi"
615    return
616fi
617# Falso, en caso de error.
618[ $# == 2 ] || return 1
[3e3cfdb]619PART="$(ogDiskToDev $1 $2 2>/dev/null)" || return 1
[3249002]620
621test -n "$(findmnt -n -o OPTIONS $PART | awk 'BEGIN {RS=","} /^ro$/ {print}')"
622}
623
624
625#/**
[b32f902]626#         ogIsWritable int_ndisk int_nfilesys
627#@brief   Comprueba si un sistema de archivos está montado de lectura y escritura.
[42669ebf]628#@param   int_ndisk      nº de orden del disco
[b32f902]629#@param   int_nfilesys   nº de orden del sistema de archivos
630#@return  Código de salida: 0 - lectura y escritura, 1 - solo lectura o no montado.
631#@version 1.0.5 - Primera versión para OpenGnSys.
[7685100]632#@author  Ramon Gomez, ETSII Universidad de Sevilla
[b32f902]633#@date    2013-10-09
634#/**
635function ogIsWritable ()
[42669ebf]636{
[59f9ad2]637# Variables locales
[b32f902]638local PART
[9d96c57]639
[1e7eaab]640# Si se solicita, mostrar ayuda.
[9d96c57]641if [ "$*" == "help" ]; then
[b32f902]642    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_filesys" \
[a3348ce]643           "if $FUNCNAME 1 1; then ... ; fi"
[9d96c57]644    return
645fi
[7685100]646# Falso, en caso de error.
647[ $# == 2 ] || return 1
[3e3cfdb]648PART="$(ogDiskToDev $1 $2 2>/dev/null)" || return 1
[9d96c57]649
[b32f902]650test -n "$(findmnt -n -o OPTIONS $PART | awk 'BEGIN {RS=","} /^rw$/ {print}')"
[9d96c57]651}
652
653
654#/**
655#         ogLock int_ndisk int_npartition
[89403cd]656#@see     ogLockPartition
657#*/
[42669ebf]658function ogLock ()
659{
[89403cd]660ogLockPartition "$@"
661}
662
663#/**
664#         ogLockPartition int_ndisk int_npartition
[9d96c57]665#@brief   Genera un fichero de bloqueo para una partición en uso exlusivo.
[42669ebf]666#@param   int_ndisk      nº de orden del disco
667#@param   int_npartition nº de orden de la partición
[9d96c57]668#@return  (nada)
669#@exception OG_ERR_FORMAT    Formato incorrecto.
670#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
[73c8417]671#@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]672#@version 0.9 - Primera versión para OpenGnSys.
[9d96c57]673#@author  Ramon Gomez, ETSII Universidad de Sevilla
674#@date    2009-09-03
[1e7eaab]675#*/ ##
[42669ebf]676function ogLockPartition ()
677{
[59f9ad2]678# Variables locales
679local PART LOCKFILE
[9d96c57]680
[1e7eaab]681# Si se solicita, mostrar ayuda.
[9d96c57]682if [ "$*" == "help" ]; then
683    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
684           "$FUNCNAME 1 1"
685    return
686fi
[1e7eaab]687# Error si no se reciben 2 parámetros.
[9d96c57]688[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
689
[1e7eaab]690# Obtener partición.
[9d96c57]691PART="$(ogDiskToDev $1 $2)" || return $?
692
[1e7eaab]693# Crear archivo de bloqueo exclusivo.
[73c8417]694LOCKFILE="/var/lock/lock${PART//\//-}"
695touch $LOCKFILE
[9d96c57]696}
697
698
699#/**
[b6208d8]700#         ogMount int_ndisk int_nfilesys
[3458879]701#@see     ogMountFs ogMountCache ogMountCdrom
[1e7eaab]702#*/ ##
[42669ebf]703function ogMount ()
704{
[ee4a96e]705case "$*" in
[18f4bc2]706    CACHE|cache)
707        ogMountCache ;;
[ee4a96e]708    CDROM|cdrom)
709        ogMountCdrom ;;
710    *)  ogMountFs "$@" ;;
711esac
[73c8417]712}
713
[ee4a96e]714
[73c8417]715#/**
[9eaa464]716#         ogMountFirstFs int_ndisk
717#@brief   Monta el primer sistema de archivos disponible en el disco.
718#@param   int_ndisk      nº de orden del disco
719#@return  Punto de montaje del primer sistema de archivos detectado
720#*/ ##
721function ogMountFirstFs ()
722{
723# Variables locales
724local PART NPARTS MNTDIR
725
726# Error si no se recibe 1 parámetro.
727[ $# == 1 ] || ogRaiseError $OG_ERR_FORMAT || return $?
728
729# Obtener nº de particiones del disco.
730NPARTS=$(ogGetPartitionsNumber "$1") || return $?
731for (( PART = 1; PART <= NPARTS; PART++ )); do
732    MNTDIR=$(ogMount $1 $PART 2>/dev/null)
733    if [ -n "$MNTDIR" ]; then
734        echo "$MNTDIR"
735        return 0
736    fi
737done
738ogRaiseError $OG_ERR_NOTFOUND "$1"
739return $OG_ERR_NOTFOUND
740}
741
742
743#/**
[b6208d8]744#         ogMountFs int_ndisk int_nfilesys
[aae34f6]745#@brief   Monta un sistema de archivos.
[42669ebf]746#@param   int_ndisk      nº de orden del disco
[b6208d8]747#@param   int_nfilesys   nº de orden del sistema de archivos
[aae34f6]748#@return  Punto de montaje
749#@exception OG_ERR_FORMAT    Formato incorrecto.
750#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
751#@exception OG_ERR_PARTITION Tipo de particion desconocido o no se puede montar.
[985bef0]752#@version 0.1 -  Integracion para Opengnsys  -  EAC:   MountPartition() en FileSystem.lib
753#@author  Antonio J. Doblas Viso. Universidad de Malaga
754#@date    2008-10-27
[b6208d8]755#@version 0.9 - Primera version para OpenGnSys.
[aae34f6]756#@author  Ramon Gomez, ETSII Universidad de Sevilla
[40488da]757#@date    2009-09-28
[b6208d8]758#@version 1.0.5 - Independiente del tipo de sistema de ficheros.
[02f271b]759#@author  Ramon Gomez, ETSII Universidad de Sevilla
[3011075]760#@date    2012-09-04
[ae4c525]761#@version 1.1.0 - Montar sistema de archivos ZFS y NTFS hibernado.
[0d6e7222]762#@author  Ramon Gomez, ETSII Universidad de Sevilla
[ae4c525]763#@date    2016-09-19
[1e7eaab]764#*/ ##
[42669ebf]765function ogMountFs ()
766{
[1e7eaab]767# Variables locales
[b6208d8]768local PART MNTDIR
[aae34f6]769
[1e7eaab]770# Si se solicita, mostrar ayuda.
[aae34f6]771if [ "$*" == "help" ]; then
[e087194]772    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_nfilesys" \
[aae34f6]773           "$FUNCNAME 1 1  =>  /mnt/sda1"
774    return
775fi
[1e7eaab]776# Error si no se reciben 2 parámetros.
[aae34f6]777[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
778
[1e7eaab]779# Obtener partición.
[b6208d8]780PART="$(ogDiskToDev "$1" "$2")" || return $?
[aae34f6]781
[1e7eaab]782# Comprobar si el sistema de archivos ya está montada.
[a3348ce]783MNTDIR="$(ogGetMountPoint $1 $2)"
[e2c805a]784# Si no, montarlo en un directorio de sistema.
[aae34f6]785if [ -z "$MNTDIR" ]; then
786    # Error si la particion esta bloqueada.
[52fa3da]787    if ogIsLocked $1 $2; then
788        ogRaiseError $OG_ERR_LOCKED "$MSG_PARTITION, $1 $2"
789        return $?
790    fi
[0d6e7222]791    # El camino de un dispositivo normal comienza por el carácter "/".
792    if [[ "$PART" =~ ^/ ]]; then
793        # Crear punto de montaje o enlace simbólico para caché local.
794        MNTDIR=${PART/dev/mnt}
[3249002]795        DEBUG="no"
[0d6e7222]796        if [ "$(ogFindCache)" == "$1 $2" -a -n "$OGCAC" ]; then
797            mkdir -p $OGCAC
798            ln -fs $OGCAC $MNTDIR
799        else
800            mkdir -p $MNTDIR
801        fi
[3249002]802        unset DEBUG
[0d6e7222]803        # Montar sistema de archivos.
804        mount $PART $MNTDIR &>/dev/null || \
[ae4c525]805                    mount $PART $MNTDIR -o force,remove_hiberfile &>/dev/null
806        case $? in
807            0)  # Correcto.
808                ;;
809            14) # Intentar limpiar hibernación NTFS y montar.
810                ntfsfix -d $PART &>/dev/null && mount $PART $MNTDIR &>/dev/null || \
811                        ogRaiseError $OG_ERR_PARTITION "$1, $2" || return $?
812                ;;
813            *)  # Probar montaje de solo lectura.
814                mount $PART $MNTDIR -o ro &>/dev/null || \
815                        ogRaiseError $OG_ERR_PARTITION "$1, $2" || return $?
816                ;;
817        esac
[3249002]818        # Aviso de montaje de solo lectura.
819        if ogIsReadonly $1 $2; then
820            ogEcho warning "$FUNCNAME: $MSG_MOUNTREADONLY: \"$1, $2\""
821        fi
[e2c805a]822    else
[0d6e7222]823        # Montar sistema de archivos ZFS (un ZPOOL no comienza por "/").
824        zfs mount $PART 2>/dev/null
[e2c805a]825    fi
[aae34f6]826fi
[cbbb046]827echo "$MNTDIR"
[aae34f6]828}
829
830
[cade8c0]831#/**
832#         ogMountCdrom
833#@brief   Monta dispositivo óptico por defecto
834#@return  Punto de montaje
835#@exception OG_ERR_FORMAT    Formato incorrecto.
836#@exception OG_ERR_PARTITION Tipo de particion desconocido o no se puede montar.
837#@version
838#@author 
839#@date   
840#*/ ##
[42669ebf]841function ogMountCdrom ()
842{
[ee4a96e]843local DEV MNTDIR
[cade8c0]844# Si se solicita, mostrar ayuda.
845if [ "$*" == "help" ]; then
846    ogHelp "$FUNCNAME" "$FUNCNAME" "$FUNCNAME"
847    return
848fi
849# Error si se reciben parámetros.
850[ $# == 0 ] || ogRaiseError $OG_ERR_FORMAT || return $?
[ee4a96e]851DEV="/dev/cdrom"            # Por defecto
852MNTDIR=$(mount | awk -v D=$DEV '{if ($1==D) {print $3}}')
853if [ -z "$MNTDIR" ]; then
854    MNTDIR=${DEV/dev/mnt}
855    mkdir -p $MNTDIR
856    mount -t iso9660 $DEV $MNTDIR || ogRaiseError $OG_ERR_PARTITION "cdrom" || return $?
857fi
858echo $MNTDIR
859}
860
[3f49cf7]861
862#/**
[b6208d8]863#         ogReduceFs int_ndisk int_nfilesys
[3f49cf7]864#@brief   Reduce el tamaño del sistema de archivos, sin tener en cuenta el espacio libre.
[42669ebf]865#@param   int_ndisk      nº de orden del disco
[b6208d8]866#@param   int_nfilesys   nº de orden del sistema de archivos
[d3669a2]867#@return  int_tamañoKB - tamaño en KB
[3f49cf7]868#@exception OG_ERR_FORMAT    Formato incorrecto.
869#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
870#@exception OG_ERR_PARTITION Partición desconocida o no accesible.
[df097c2]871#@warning En Windows, se borran los ficheros de hiberanción y de paginación.
[d3669a2]872#@warning El sistema de archivos se amplía al mínimo + 10%.
[3458879]873#@note    Requisitos:   *resize*
[985bef0]874#@version 0.1 -  Integracion para Opengnsys  -  EAC:   ReduceFileSystem() en ATA.lib
875#@author  Antonio J. Doblas Viso. Universidad de Malaga
876#@date    2008-10-27
[b6208d8]877#@version 0.9 - Primera version para OpenGnSys.
[3f49cf7]878#@author  Ramon Gomez, ETSII Universidad de Sevilla
879#@date    2009-09-23
[d3669a2]880#@version 0.9.2 - Añadir un 10% al tamaño mínimo requerido.
881#@author  Ramon Gomez, ETSII Universidad de Sevilla
882#@date    2010-09-27
[c114529]883#@version 1.0 -  Deteccion automatica del tamaño minimo adecuado
884#@author  Antonio J. Doblas Viso. Universidad de Malaga
885#@date    2011-02-24
[3be0219]886#@version 1.0.6 - Integrar código de antigua función "ogReduceFsCheck".
887#@author  Ramon Gomez, ETSII Universidad de Sevilla
888#@date    2014-10-28
[1e7eaab]889#*/ ##
[42669ebf]890function ogReduceFs ()
891{
[3f49cf7]892# Variables locales
[3be0219]893local PART BLKS SIZE MAXSIZE EXTRASIZE=0 RETVAL
[3f49cf7]894
[1e7eaab]895# Si se solicita, mostrar ayuda.
[3f49cf7]896if [ "$*" == "help" ]; then
[e087194]897    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_nfilesys" \
[3f49cf7]898           "$FUNCNAME 1 1"
899    return
900fi
[1e7eaab]901# Error si no se reciben 2 parámetros.
[3f49cf7]902[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
903
[1e7eaab]904# Obtener partición.
[3f49cf7]905PART="$(ogDiskToDev $1 $2)" || return $?
906
[1e7eaab]907# Redimensionar según el tipo de particion.
[3f49cf7]908case "$(ogGetFsType $1 $2)" in
[1c04494]909    EXT[234])
[3458879]910        ogUnmount $1 $2 2>/dev/null
[1c04494]911        # Ext2/3/4: Tamaño de los bloques del sistema de archivos
912        BLKS=$(tune2fs -l $PART | awk '/Block size/ {print int($3/512)}')
913        # Traduce el num. en sectores de 512B a tamano en MB.
[c114529]914        #SIZE=$(resize2fs -P $PART 2>/dev/null | \
915                #       awk -v B=$BLKS '/minimum size/ {print int($7*1.1*B/2048)}')
916        #resize2fs -fp $PART "${SIZE}M" &>/dev/null || ogRaiseError $OG_ERR_PARTITION "$1,$2" || return $?
917        resize2fs -fpM $PART  &>/dev/null || ogRaiseError $OG_ERR_PARTITION "$1,$2" || return $?
[743257e]918        ;;
[3be0219]919
[3198512]920    BTRFS)
921        MNTDIR=$(ogMount $1 $2)
[5bc8d01]922        # Calcular tamaño ocupado + 10%, redondeado + 1 (incluyendo letra de unidad).
923        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)) }')
924        btrfs filesystem resize ${SIZE} $MNTDIR 2>/dev/null
[3198512]925        ;;
926    REISERFS|REISER4)
927        # Calcular tamaño ocupado + 10%.
[3be0219]928        MNTDIR=$(ogMount $1 $2)
[3198512]929        SIZE=$[ $(df -k $MNTDIR | awk '{getline;print $3}') * 110 / 100 ]
930        ogUnmount $1 $2 2>/dev/null
[eb4614b]931        resize_reiserfs -s${SIZE}K $PART <<<"y"
[3198512]932        ;;
[3be0219]933
[5bc8d01]934    F2FS)   ;;          # No se reduce (por el momento).
[c452625]935    JFS)    ;;          # No se reduce (por el momento).
[5bc8d01]936    NILFS2) ;;          # No se reduce (probar "nilfs-resize").
[c452625]937    XFS)    ;;          # No se reduce (por el momento).
[3be0219]938
[b6208d8]939    NTFS)
[3be0219]940        # Calcular tamaño ocupado + 10%.
941        ogUnmount $1 $2 &>/dev/null
942        read -e MAXSIZE SIZE <<<$(ntfsresize -fi $PART | \
943                                  awk '/device size/ {d=$4}
944                                       /resize at/ {r=int($5*1.1/1024+1)*1024}
945                                       END { print d,r}')
946        # Error si no puede obtenerse el tamaño máximo del volumen.
[bec0f2f]947        [ -n "$MAXSIZE" -a -n "$SIZE" ] || ogRaiseError $OG_ERR_PARTITION "$1,$2" || return $?
[3be0219]948        # Simular la redimensión y comprobar si es necesario ampliarala.
949        RETVAL=1
950        while [ $RETVAL != 0 -a $[ SIZE+=EXTRASIZE ] -lt $MAXSIZE ]; do
951            # Obtener espacio de relocalización y devolver código de salida
952            # (ntfsresize devuelve 0 si no necesita relocalizar).
953            EXTRASIZE=$(ntfsresize -fns $SIZE $PART 2>/dev/null | \
954                        awk '/Needed relocations/ {print int($4*1.1/1024+1)*1024}'
955                        exit ${PIPESTATUS[0]})
956            RETVAL=$?
957        done
958        # Redimensionar solo si hace falta.
959        if [ $SIZE -lt $MAXSIZE ]; then
960            ntfsresize -fs $SIZE $PART <<<"y" >/dev/null || ogRaiseError $OG_ERR_PARTITION "$1,$2" || return $?
961        fi
[6277770]962        ;;
[3be0219]963
[c452625]964    EXFAT)  ;;          # No se reduce (por el momento).
965    FAT32|FAT16)  ;;    # No se reduce (probar "fatresize").
966    HFS|HFSPLUS)  ;;    # No se reduce (por el momento).
967    UFS)    ;;          # No se reduce (por el momento).
[3be0219]968
[1c04494]969    *)  ogRaiseError $OG_ERR_PARTITION "$1,$2"
[6277770]970        return $? ;;
[3f49cf7]971esac
[c114529]972
[3be0219]973# Devuelve tamaño del sistema de ficheros.
974ogGetFsSize $1 $2
[c114529]975}
976
977
[5dbb046]978#/**
[9d96c57]979#         ogUnlock int_ndisk int_npartition
[89403cd]980#@see     ogUnlockPartition
[6e390b1]981#*/ ##
[42669ebf]982function ogUnlock ()
983{
[89403cd]984ogUnlockPartition "$@"
985}
986
987#/**
988#         ogUnlockPartition int_ndisk int_npartition
[9d96c57]989#@brief   Elimina el fichero de bloqueo para una particion.
[42669ebf]990#@param   int_ndisk      nº de orden del disco
991#@param   int_npartition nº de orden de la partición
[9d96c57]992#@return  (nada)
993#@exception OG_ERR_FORMAT    Formato incorrecto.
994#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
[73c8417]995#@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]996#@version 0.9 - Primera versión para OpenGnSys.
[9d96c57]997#@author  Ramon Gomez, ETSII Universidad de Sevilla
998#@date    2009-09-03
[1e7eaab]999#*/ ##
[42669ebf]1000function ogUnlockPartition ()
1001{
[59f9ad2]1002# Variables locales
1003local PART LOCKFILE
[9d96c57]1004
[1e7eaab]1005# Si se solicita, mostrar ayuda.
[9d96c57]1006if [ "$*" == "help" ]; then
1007    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
1008           "$FUNCNAME 1 1"
1009    return
1010fi
[1e7eaab]1011# Error si no se reciben 2 parámetros.
[9d96c57]1012[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
1013
[1e7eaab]1014# Obtener partición.
[9d96c57]1015PART="$(ogDiskToDev $1 $2)" || return $?
1016
[1e7eaab]1017# Borrar archivo de bloqueo exclusivo.
[73c8417]1018LOCKFILE="/var/lock/lock${PART//\//-}"
1019rm -f $LOCKFILE
[9d96c57]1020}
1021
1022
1023#/**
[5dbb046]1024#         ogUnmount int_ndisk int_npartition
[40488da]1025#@see     ogUnmountFs
[6e390b1]1026#*/ ##
[42669ebf]1027function ogUnmount ()
1028{
[40488da]1029ogUnmountFs "$@"
[89403cd]1030}
1031
1032#/**
[b6208d8]1033#         ogUnmountFs int_ndisk int_nfilesys
[5dbb046]1034#@brief   Desmonta un sistema de archivos.
[42669ebf]1035#@param   int_ndisk      nº de orden del disco
[b6208d8]1036#@param   int_nfilesys   nº de orden del sistema de archivos
[5dbb046]1037#@return  Nada
1038#@exception OG_ERR_FORMAT    Formato incorrecto.
1039#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
1040#@warning La partición no está previamente montada o no se puede desmontar.
[985bef0]1041#@version 0.1 -  Integracion para Opengnsys  -  EAC:  UmountPartition() en FileSystem.lib
1042#@author  Antonio J. Doblas Viso. Universidad de Malaga
1043#@date    2008-10-27
[b6208d8]1044#@version 0.9 - Primera version para OpenGnSys.
[5dbb046]1045#@author  Ramon Gomez, ETSII Universidad de Sevilla
[40488da]1046#@date    2009-09-28
[1e7eaab]1047#*/ ##
[42669ebf]1048function ogUnmountFs ()
1049{
[59f9ad2]1050# Variables locales
[c56dec5]1051local PART MNTDIR
[5dbb046]1052
[1e7eaab]1053# Si se solicita, mostrar ayuda.
[5dbb046]1054if [ "$*" == "help" ]; then
1055    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" "$FUNCNAME 1 1"
1056    return
1057fi
[1e7eaab]1058# Error si no se reciben 2 parámetros.
[5dbb046]1059[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
1060
[1e7eaab]1061# Obtener partición y punto de montaje.
[5dbb046]1062PART="$(ogDiskToDev $1 $2)" || return $?
[a3348ce]1063MNTDIR="$(ogGetMountPoint $1 $2)"
[5dbb046]1064
[1e7eaab]1065# Si está montada, desmontarla.
[c56dec5]1066if [ -n "$MNTDIR" ]; then
[5a4f399]1067    # Error si la particion está bloqueada.
[52fa3da]1068    if ogIsLocked $1 $2; then
1069        ogRaiseError $OG_ERR_LOCKED "$MSG_PARTITION $1, $2"
1070        return $?
1071    fi
[5a4f399]1072    # Desmontar y borrar punto de montaje.
[52fa3da]1073    umount $PART 2>/dev/null || ogEcho warning "$FUNCNAME: $MSG_DONTUNMOUNT: \"$1, $2\""
[5a4f399]1074    rmdir $MNTDIR 2>/dev/null || rm -f $MNTDIR 2>/dev/null
[5dbb046]1075else
1076    ogEcho warning "$MSG_DONTMOUNT: \"$1,$2\""
1077fi
1078}
1079
[ee4a96e]1080
[be81649]1081#/**
1082#         ogUnmountAll int_ndisk
1083#@brief   Desmonta todos los sistema de archivos de un disco, excepto el caché local.
[42669ebf]1084#@param   int_ndisk      nº de orden del disco
[be81649]1085#@return  Nada
1086#@exception OG_ERR_FORMAT    Formato incorrecto.
1087#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
1088#@warning No se desmonta la partición marcada como caché local.
[b6208d8]1089#@version 0.9 - Versión para OpenGnSys.
[be81649]1090#@author  Ramon Gomez, ETSII Universidad de Sevilla
1091#@date    2009/10/07
[1e7eaab]1092#*/ ##
[42669ebf]1093function ogUnmountAll ()
1094{
[be81649]1095# Variables locales
[18f4bc2]1096local DISK PART
[1e7eaab]1097# Si se solicita, mostrar ayuda.
[18f4bc2]1098if [ "$*" == "help" ]; then
1099    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk" "FUNCNAME 1"
1100    return
1101fi
[1e7eaab]1102# Error si no se recibe 1 parámetro.
[18f4bc2]1103[ $# == 1 ] || ogRaiseError $OG_ERR_FORMAT || return $?
1104
[1e7eaab]1105# Obtener partición y punto de montaje.
[18f4bc2]1106DISK="$(ogDiskToDev $1)" || return $?
1107for ((PART=1; PART<=$(ogGetPartitionsNumber $1); PART++)); do
1108    case "$(ogGetFsType $1 $PART)" in
[5804229]1109        CACHE) ;;
[7c52c30]1110        *)     ogUnmount $1 $PART 2>/dev/null ;;
[7250491]1111    esac
[18f4bc2]1112done
1113}
1114
[62c18f0]1115#/**
1116#         ogUnsetDirtyBit int_ndisk int_npart
1117#@brief   Inhabilita el Dirty Bit del sistema de ficheros NTFS para evitar un CHKDSK en el primer arranque
1118#@param   int_ndisk      nº de orden del disco
1119#@param   int_npart      nº de orden de partición
1120#@return  Nada
1121#@exception OG_ERR_FORMAT    Formato incorrecto.
1122#@version 0.1 - Versión para OpenGnSys.
1123#@author  Carmelo Cabezuelo, ASIC Universidad Politécnica de Valencia
1124#@date    2016/04/20
1125#*/ ##
1126function ogUnsetDirtyBit ()
1127{
1128# Variables locales
1129local PART
1130# Si se solicita, mostrar ayuda.
1131if [ "$*" == "help" ]; then
1132    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk" "FUNCNAME 1"
1133    return
1134fi
1135# Error si no se reciben 2 parámetros.
1136[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
1137
1138# Obtener partición y punto de montaje.
1139case "$(ogGetFsType $1 $2)" in
1140    NTFS)
1141        ogUnmount $1 $2 2>/dev/null
1142        PART="$(ogDiskToDev $1 $2)" || return $?
1143        ntfsfix -d $PART ;;
1144    *) ;;
1145esac
1146}
[c114529]1147
[0390d46]1148# AVISO:  Componer corretcamente esta función.
[c114529]1149function ogGetFreeSize () {
[d5fc0dc]1150local particion unit factor valor
[c114529]1151if [ $# = 0 ]
1152then
1153        echo "sintaxis: ogGetFreeSize int_disco int_partition str_SizeOutput [ kB MB GB -default GB]-]" red
1154        echo "devuelve int_size : int_data : int_free" red
1155return
1156fi
1157if [ $# -ge 2 ]
1158then
1159        particion=`ogMount $1 $2 ` #1>/dev/null 2>&1
1160        if [ -z $3 ]
1161                then
1162                        unit=kB  # s B kB MB GB TB %
1163                else
1164                        unit=$3
1165        fi
1166        case $unit in
1167                kB)
1168                        factor="1.024";
1169                        #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}'`
1170                        valor=`df | grep  $particion | awk -F" " '{size=$2*1.024; used=$3*1.024; free=$4*1.024; printf "%d", free}'`
1171                        ;;
[a957f02]1172                MB)
1173                        factor="1.024/1000";
1174                        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}'`
1175                ;;
1176                GB)
1177                        factor="1.024/1000000";
1178                        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}'`
1179                ;;
1180        esac
1181        #echo $valor
1182        #NumberRound $valor
1183        #valor=`NumberRound $valor`;
1184        echo $valor
1185fi
[c114529]1186}
1187
Note: See TracBrowser for help on using the repository browser.