source: client/engine/FileSystem.lib @ 4d2cdae

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 4d2cdae was c452625, checked in by ramon <ramongomez@…>, 12 years ago

#602: Ignorar operaciones aún no implementadas para algunos sistemas de archivos, evitando errores inadecuados.

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

  • Property mode set to 100755
File size: 32.4 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.
[452ade2]7#@version 1.0.5
[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
[6e390b1]36#*/ ##
[42669ebf]37function ogCheckFs ()
38{
[3458879]39# Variables locales.
[cbbb046]40local PART TYPE PROG PARAMS CODES ERRCODE
41# Si se solicita, mostrar ayuda.
42if [ "$*" == "help" ]; then
[e087194]43    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_nfilesys" \
[cbbb046]44           "$FUNCNAME 1 1"
45    return
46fi
[be81649]47
[1616b6e]48# Error si no se reciben 2 parámetros.
[1956672]49[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
[1616b6e]50# Obtener partición.
[1956672]51PART="$(ogDiskToDev $1 $2)" || return $?
[be81649]52
53TYPE=$(ogGetFsType $1 $2)
54case "$TYPE" in
[99d1786]55    EXT[234])     PROG="e2fsck"; PARAMS="-y"; CODES=(1 2) ;;
[e79df1d]56    BTRFS)        PROG="btrfsck"; CODES=1 ;;
[ea8051a]57    REISERFS)     PROG="fsck.reiserfs"; PARAMS="<<<\"Yes\""; CODES=(1 2) ;;
[1616b6e]58    REISER4)      PROG="fsck.reiser4"; PARAMS="-ay" ;;
[ea8051a]59    JFS)          PROG="fsck.jfs"; CODES=(1 2) ;;
[a3348ce]60    XFS)          PROG="fsck.xfs" ;;
[5804229]61    NTFS)         PROG="ntfsfix" ;;
[3198512]62    EXFAT)        PROG="fsck.exfat" ;;
[5804229]63    FAT32)        PROG="dosfsck"; PARAMS="-a"; CODES=1 ;;
64    FAT16)        PROG="dosfsck"; PARAMS="-a"; CODES=1 ;;
65    FAT12)        PROG="dosfsck"; PARAMS="-a"; CODES=1 ;;
[02f271b]66    HFS)          PROG="fsck.hfs" ;;
[b6208d8]67    HFSPLUS)      PROG="fsck.hfsplus" ;;
[3198512]68    UFS)          PROG="fsck.ufs" ;;
[c7d9af7]69    *)            ogRaiseError $OG_ERR_PARTITION "$1, $2, $TYPE"
[1616b6e]70                  return $? ;;
[1956672]71esac
[1616b6e]72# Error si el sistema de archivos esta montado o bloqueado.
[3011075]73ogUnmount $1 $2
[a3348ce]74if ogIsMounted $1 $2; then
[7250491]75    ogRaiseError $OG_ERR_PARTITION "$1 $2"       # Indicar nuevo error
[a3348ce]76    return $?
77fi
78if ogIsLocked $1 $2; then
79    ogRaiseError $OG_ERR_LOCKED "$1 $2"
80    return $?
81fi
[1616b6e]82# Comprobar en modo uso exclusivo.
[a3348ce]83ogLock $1 $2
[7b9dedd]84trap "ogUnlock $1 $2" 1 2 3 6 9
[a3348ce]85eval $PROG $PARAMS $PART
86ERRCODE=$?
87case $ERRCODE in
[ea8051a]88    0|${CODES[*]})
89            ERRCODE=0 ;;
90    127)    ogRaiseError $OG_ERR_NOTEXEC "$PROG"
91            ERRCODE=$OG_ERR_NOTEXEC ;;
92    *)      ogRaiseError $OG_ERR_PARTITION "$1 $2"
93            ERRCODE=$OG_ERR_PARTITION ;;
[a3348ce]94esac
95ogUnlock $1 $2
96return $ERRCODE
[1956672]97}
98
99
[2e15649]100#/**
[e087194]101#         ogExtendFs int_ndisk int_nfilesys
[3f49cf7]102#@brief   Extiende un sistema de archivos al tamaño de su partición.
[42669ebf]103#@param   int_ndisk      nº de orden del disco
[b6208d8]104#@param   int_nfilesys   nº de orden del sistema de archivos
[3f49cf7]105#@return  (nada)
106#@exception OG_ERR_FORMAT   Formato incorrecto.
107#@exception OG_ERR_NOTFOUND Disco o particion no corresponden con un dispositivo.
108#@exception OG_ERR_PARTITION Partición desconocida o no accesible.
109#@note    Requisitos: *resize*
[985bef0]110#@version 0.1 -  Integracion para Opengnsys  -  EAC:   EnlargeFileSystem() en ATA.lib
111#@author  Antonio J. Doblas Viso. Universidad de Malaga
112#@date    2008-10-27
[b6208d8]113#@version 0.9 - Primera adaptacion para OpenGnSys.
[3f49cf7]114#@author  Ramon Gomez, ETSII Universidad de Sevilla
115#@date    2009-09-23
[452ade2]116#@version 1.0.5 - Soporte para BTRFS.
117#@author  Ramon Gomez, ETSII Universidad de Sevilla
118#@date    2012-06-28
[6e390b1]119#*/ ##
[42669ebf]120function ogExtendFs ()
121{
[3f49cf7]122# Variables locales.
[3198512]123local PART TYPE PROG PARAMS ERRCODE DOMOUNT
[3f49cf7]124
[1616b6e]125# Si se solicita, mostrar ayuda.
[3f49cf7]126if [ "$*" == "help" ]; then
[e087194]127    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_nfilesys" \
[3f49cf7]128           "$FUNCNAME 1 1"
129    return
130fi
[1616b6e]131# Error si no se reciben 2 parámetros.
[3f49cf7]132[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
133
[1616b6e]134# Obtener partición.
[3f49cf7]135PART="$(ogDiskToDev $1 $2)" || return $?
136
[1616b6e]137# Redimensionar al tamano máximo según el tipo de partición.
[be81649]138TYPE=$(ogGetFsType $1 $2)
139case "$TYPE" in
[2717297]140    EXT[234])   PROG="resize2fs"; PARAMS="-f" ;;
[3198512]141    BTRFS)      PROG="btrfs"; PARAMS="filesystem resize max"
142                DOMOUNT=1     # Debe estar montado.
143                ;;
144    REISERFS|REISER4)
145                PROG="resize_reiserfs"; PARAMS="-f" ;;
[c452625]146    JFS)        ;;            # No se reduce (por el momento).
147    XFS)        ;;            # No se reduce (por el momento).
[5804229]148    NTFS)       PROG="ntfsresize"; PARAMS="<<<\"y\" -f" ;;
[c452625]149    EXFAT)      ;;            # No se reduce (por el momento).
150    FAT32|FAT16)  ;;          # No se reduce (probar "fatresize").
151    HFS|HFSPLUS)  ;;          # No se reduce (por el momento).
152    UFS)        ;;            # No se reduce (por el momento).
[2717297]153    *)          ogRaiseError $OG_ERR_PARTITION "$1 $2 $TYPE"
154                return $? ;;
[3f49cf7]155esac
[c452625]156# Salida normal si no se va a aplicar la operación.
157[ -z "$PROG" ] && return
[3198512]158# Error si el sistema de archivos no se queda en el estado de montaje adecuado.
159if [ "$DOMOUNT" ]; then
160    PART=$(ogMount $1 $2)
161    [ -n "$PART" ] || ogRaiseError $OG_ERR_PARTITION "$1 $2" || return $?   # Indicar nuevo error
162else
163    ogUnmount $1 $2 2>/dev/null
[7376c5b]164    if ogIsMounted $1 $2; then
165         ogRaiseError $OG_ERR_PARTITION "$1 $2"             # Indicar nuevo error
166         return $?
167    fi
[2717297]168fi
[3198512]169# Error si el sistema de archivos está bloqueado.
[2717297]170if ogIsLocked $1 $2; then
171    ogRaiseError $OG_ERR_LOCKED "$1 $2"
172    return $?
173fi
[1616b6e]174# Redimensionar en modo uso exclusivo.
[2717297]175ogLock $1 $2
[7b9dedd]176trap "ogUnlock $1 $2" 1 2 3 6 9
[1c04494]177eval $PROG $PARAMS $PART &>/dev/null
[2717297]178ERRCODE=$?
179case $ERRCODE in
180    0)    ;;
[ea8051a]181    127)  ogRaiseError $OG_ERR_NOTEXEC "$PROG"
182          ERRCODE=$OG_ERR_NOTEXEC ;;
183    *)    ogRaiseError $OG_ERR_PARTITION "$1 $2"
184          ERRCODE=$OG_ERR_PARTITION ;;
[2717297]185esac
186ogUnlock $1 $2
187return $ERRCODE
[3f49cf7]188}
189
190
191#/**
[e087194]192#         ogFormat int_ndisk int_nfilesys | CACHE
[e09311f]193#@see     ogFormatFs ogFormatCache
[6e390b1]194#*/ ##
[42669ebf]195function ogFormat ()
196{
[e09311f]197case "$*" in
198    CACHE|cache)  ogFormatCache ;;
199    *)            ogFormatFs "$@" ;;
200esac
[40488da]201}
202
203
204#/**
[b6208d8]205#         ogFormatFs int_ndisk int_nfilesys [type_fstype] [str_label]
[40488da]206#@brief   Formatea un sistema de ficheros según el tipo de su partición.
[42669ebf]207#@param   int_ndisk      nº de orden del disco
[b6208d8]208#@param   int_nfilesys   nº de orden del sistema de archivos
[3198512]209#@param   type_fstype    mnemónico de sistema de ficheros a formatear (opcional al reformatear)
[42669ebf]210#@param   str_label      etiqueta de volumen (opcional)
[40488da]211#@return  (por determinar)
[3198512]212#@exception OG_ERR_FORMAT    Formato de ejecución incorrecto.
[40488da]213#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
214#@exception OG_ERR_PARTITION Partición no accesible o desconocida.
[a3348ce]215#@note    Requisitos:   mkfs*
216#@warning No formatea particiones montadas ni bloqueadas.
217#@todo    Definir salidas.
[b6208d8]218#@version 0.9 - Primera versión para OpenGnSys.
[40488da]219#@author  Ramon Gomez, ETSII Universidad de Sevilla
[a3348ce]220#@date    2009-10-08
[066fa01]221#@version 1.0.4 - Solucionado error cuando no se detecta tipo de sistema de ficheros pero si se indica.
[e068946]222#@author  Universidad de Huelva
223#@date    2012-04-11
[3198512]224#@version 1.0.5 - Comprobar errores al inicio e independizar del tipo de tabla de particiones.
[066fa01]225#@author  Universidad de Huelva
[3198512]226#@date    2013-05-16
[1e7eaab]227#*/ ##
[42669ebf]228function ogFormatFs ()
229{
[40488da]230# Variables locales
[3198512]231local PART ID TYPE LABEL PROG PARAMS LABELPARAM ERRCODE
[40488da]232
[42669ebf]233# Si se solicita, mostrar ayuda.
[40488da]234if [ "$*" == "help" ]; then
[e087194]235    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_nfilesys [str_label]" \
[40488da]236           "$FUNCNAME 1 1" \
[be81649]237           "$FUNCNAME 1 1 EXT4" \
[55ad138c]238           "$FUNCNAME 1 1 \"DATA\"" \
239           "$FUNCNAME 1 1 EXT4 \"DATA\""
[40488da]240    return
241fi
[1e7eaab]242# Error si no se reciben entre 2 y 4 parámetros.
[be81649]243[ $# -ge 2 -a $# -le 4 ] || ogRaiseError $OG_ERR_FORMAT || return $?
[8e29877]244# Obtener fichero de dispositivo.
[40488da]245PART="$(ogDiskToDev $1 $2)" || return $?
[8e29877]246# Error si la partición está montada o bloqueada.
247if ogIsMounted $1 $2; then
248    ogRaiseError $OG_ERR_PARTITION "$1 $2"       # Indicar nuevo error
249    return $?
250fi
251if ogIsLocked $1 $2; then
252    ogRaiseError $OG_ERR_LOCKED "$1 $2"
253    return $?
254fi
[3198512]255# Si no se indica el tipo de sisitema de archivos, intentar obtenerlo.
256TYPE="${3:-$(ogGetFsType $1 $2)}"
257# Error, si no especifica el tipo de sistema de archivos a formatear.
258[ -n "$TYPE" ] || ogRaiseError $OG_ERR_FORMAT "$1 $2 ..." || return $?
[be81649]259
[3198512]260# Elegir tipo de formato.
261case "$TYPE" in
262    EXT2)         PROG="mkfs.ext2" ;;
263    EXT3)         PROG="mkfs.ext3" ;;
264    EXT4)         PROG="mkfs.ext4" ;;
265    BTRFS)        PROG="mkfs.btrfs" ;;
266    REISERFS)     PROG="mkfs.reiserfs"; PARAMS="-f"; LABELPARAM="-l" ;;
267    REISER4)      PROG="mkfs.reiser4"; PARAMS="-fy" ;;
268    XFS)          PROG="mkfs.xfs"; PARAMS="-f" ;;
269    JFS)          PROG="mkfs.jfs"; PARAMS="<<<\"y\"" ;;
270    LINUX-SWAP)   PROG="mkswap" ;;
271    NTFS)         PROG="mkntfs"; PARAMS="-f" ;;
272    EXFAT)        PROG="mkfs.exfat"; LABELPARAM="-n" ;;
273    FAT32)        PROG="mkdosfs"; PARAMS="-F 32"; LABELPARAM="-n" ;;
274    FAT16)        PROG="mkdosfs"; PARAMS="-F 16"; LABELPARAM="-n" ;;
275    FAT12)        PROG="mkdosfs"; PARAMS="-F 12"; LABELPARAM="-n" ;;
276    HFS)          PROG="mkfs.hfs" ;;
277    HFSPLUS)      PROG="mkfs.hfsplus"; LABELPARAM="-v" ;;
278    UFS)          PROG="mkfs.ufs"; PARAMS="-O 2" ;;
279    *)            ogRaiseError $OG_ERR_PARTITION "$1 $2 $TYPE"
280                  return $? ;;
[40488da]281esac
[be81649]282
[1e7eaab]283# Etiquetas de particion.
[be81649]284if [ -z "$LABEL" ]; then
285    [ "$4" != "CACHE" ] || ogRaiseError $OG_ERR_FORMAT "$MSG_RESERVEDVALUE: CACHE" || return $?
[3198512]286    [ -n "$4" ] && PARAMS="$PARAMS ${LABELPARAM:-"-L"} $4"
[be81649]287else
[3198512]288    PARAMS="$PARAMS ${LABELPARAM:-"-L"} $LABEL"
[be81649]289fi
[40488da]290
[8e29877]291# Formatear en modo uso exclusivo (desmontar siempre).
[40488da]292ogLock $1 $2
[7b9dedd]293trap "ogUnlock $1 $2" 1 2 3 6 9
[3198512]294umount $PART 2>/dev/null
[a3348ce]295eval $PROG $PARAMS $PART 2>/dev/null
[be81649]296ERRCODE=$?
297case $ERRCODE in
298    0)    ;;
299    127)  ogRaiseError $OG_ERR_NOTEXEC "$PROG" ;;
300    *)    ogRaiseError $OG_ERR_PARTITION "$1 $2" ;;
301esac
[40488da]302ogUnlock $1 $2
[be81649]303return $ERRCODE
[40488da]304}
305
306
307#/**
[7b9dedd]308#         ogGetFsSize int_ndisk int_npartition [str_unit]
309#@brief Muestra el tamanio del sistema de archivos indicado, permite definir la unidad de medida, por defecto GB
310#@param   int_ndisk      nº de orden del disco
311#@param   int_npartition nº de orden de la partición
312#@param   str_unit       unidad (opcional, por defecto: KB)
313#@return  float_size - Tamaño del sistema de archivos
314#@note    str_unit = { KB, MB, GB, TB }
315#@exception OG_ERR_FORMAT   Formato incorrecto.
316#@exception OG_ERR_NOTFOUND Disco o partición no corresponden con un dispositivo.
317#@version 0.1 -  Integracion para Opengnsys  -  EAC:  SizeFileSystem() en FileSystem.lib
318#@author  Antonio J. Doblas Viso. Universidad de Malaga
319#@date    2008-10-27
320#@version 1.0.4 - Adaptación de las salidas.
321#@author  Ramon Gomez, ETSII Universidad de Sevilla
322#@date    2012-06-18
323#*/ ##
324function ogGetFsSize ()
325{
326# Variables locales.
[68f360e]327local MNTDIR UNIT VALUE FACTOR SIZE
[7b9dedd]328# Si se solicita, mostrar ayuda.
329if [ "$*" == "help" ]; then
330    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition [str_unit]" \
331           "$FUNCNAME 1 1  =>  15624188" \
332           "$FUNCNAME 1 1 KB  =>  15624188"
333    return
334fi
335# Error si no se reciben 2 o 3 parámetros.
336[ $# == 2 ] || [ $# == 3 ] || ogRaiseError $OG_ERR_FORMAT || return $?
[68f360e]337# Obtener unidad y factor de medida.
[7b9dedd]338UNIT="$3"
339UNIT=${UNIT:-"KB"}
340case "$UNIT" in
341    [kK]B)
342        FACTOR=1 ;;
343    MB) FACTOR=1024 ;;
344    GB) FACTOR=$[1024*1024] ;;
345    TB) FACTOR=$[1024*1024*1024] ;;
346    *)  ogRaiseError $OG_ERR_FORMAT "$3 != { KB, MB, GB, TB }"
347        return $? ;;
348esac
[68f360e]349
350# Obtener el tamaño del sistema de archivo (si no está formateado; tamaño = 0).
351MNTDIR="$(ogMount $1 $2 2>/dev/null)"
352if [ -n "$MNTDIR" ]; then
353    VALUE=$(df -BK "$MNTDIR" | awk '{getline; print $2}')
354    SIZE=$(echo "$VALUE $FACTOR" | awk '{printf "%f\n", $1/$2}')
355else
356    SIZE=0
357fi
358# Devolver el tamaño (quitar decimales si son 0).
359echo ${SIZE%.0*}
[7b9dedd]360}
361
362
363#/**
[b6208d8]364#         ogGetFsType int_ndisk int_nfilesys
[9f57de01]365#@brief   Devuelve el mnemonico con el tipo de sistema de archivos.
[42669ebf]366#@param   int_ndisk      nº de orden del disco
[b6208d8]367#@param   int_nfilesys   nº de orden del sistema de archivos
[9f57de01]368#@return  Mnemonico
[3198512]369#@note    Mnemonico: { EXT2, EXT3, EXT4, BTRFS, REISERFS, XFS, JFS, FAT32, NTFS, CACHE }
[9f57de01]370#@exception OG_ERR_FORMAT   Formato incorrecto.
371#@exception OG_ERR_NOTFOUND Disco o particion no corresponden con un dispositivo.
[985bef0]372#@version 0.1 -  Integracion para Opengnsys  -  EAC:   TypeFS() en ATA.lib
373#@author  Antonio J. Doblas Viso. Universidad de Malaga
374#@date    2008-10-27
[5804229]375#@version 0.9 - Primera adaptacion para OpenGnSys.
[9f57de01]376#@author  Ramon Gomez, ETSII Universidad de Sevilla
[5dbb046]377#@date    2009-07-21
[5804229]378#@version 1.0.2 - Obtención de datos reales de sistemas de ficheros.
379#@author  Ramon Gomez, ETSII Universidad de Sevilla
380#@date    2011-12-02
[b6208d8]381#@version 1.0.5 - Usar "mount" en vez de "parted".
382#@author  Ramon Gomez, ETSII Universidad de Sevilla
[3011075]383#@date    2012-09-04
[1e7eaab]384#*/ ##
[cbbb046]385function ogGetFsType ()
386{
[59f9ad2]387# Variables locales.
[b6208d8]388local PART ID TYPE
[1e7eaab]389# Si se solicita, mostrar ayuda.
[59f9ad2]390if [ "$*" == "help" ]; then
[e087194]391    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_nfilesys" \
[59f9ad2]392           "$FUNCNAME 1 1  =>  NTFS"
393    return
394fi
[1e7eaab]395# Error si no se reciben 2 parámetros.
[a5df9b9]396[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
[2e15649]397
[b6208d8]398# Detectar id. de tipo de partición.
399PART=$(ogDiskToDev "$1" "$2") || return $?
400ID=$(ogGetPartitionId "$1" "$2")
[4e1dc53]401[ "$ID" == "a7" ] && ID="ca"    # Traducir antiguo id. de partición de caché.
[5804229]402TYPE=""
[2e15649]403case "$ID" in
[f2c8049]404     ca|CA00)  # Detectar caché local (revisar detección en tablas GPT).
[5804229]405               ogIsFormated $1 $2 2>/dev/null && TYPE="CACHE"
406               ;;
407     *)        # Detectar sistema de ficheros.
[e79df1d]408               TYPE=$(blkid -o export $PART | awk -F= '$1~/^TYPE/ { print toupper($2) }')
409               # Componer valores correctos.
[b6208d8]410               case "$TYPE" in
[e79df1d]411                    VFAT)      TYPE="FAT32" ;;  # Nota: usar "file -Ls" para detectar.
412                    SWAP)      TYPE="LINUX-SWAP" ;;
413                    LVM*)      TYPE="LINUX-LVM" ;;
414                    *RAID*)    TYPE="LINUX-RAID" ;;
415                    *_MEMBER)  TYPE="${TYPE/_MEMBER/}" ;;
[b6208d8]416               esac
[5804229]417               ;;
[2e15649]418esac
[5804229]419
420[ -n "$TYPE" ] && echo "$TYPE"
[2e15649]421}
422
[aae34f6]423
[a3348ce]424#/**
[b6208d8]425#         ogGetMountPoint int_ndisk int_nfilesys
[a3348ce]426#@brief   Devuelve el punto de montaje de un sistema de archivos.
[42669ebf]427#@param   int_ndisk      nº de orden del disco
[b6208d8]428#@param   int_nfilesys   nº de orden del sistema de archivos
[f8f4dfa]429#@return  Punto de montaje
430#@exception OG_ERR_FORMAT    Formato incorrecto.
431#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
[3458879]432#@note    Requisitos: \c mount* \c awk
[b6208d8]433#@version 0.9 - Primera versión para OpenGnSys.
[f8f4dfa]434#@author  Ramon Gomez, ETSII Universidad de Sevilla
435#@date    2009-10-15
[1e7eaab]436#*/ ##
[42669ebf]437function ogGetMountPoint ()
438{
[a3348ce]439# Variables locales
440local PART
[1e7eaab]441# Si se solicita, mostrar ayuda.
[a3348ce]442if [ "$*" == "help" ]; then
[e087194]443    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_nfilesys" \
[a3348ce]444           "$FUNCNAME 1 1  =>  /mnt/sda1"
445    return
446fi
[1e7eaab]447# Error si no se reciben 2 parámetros.
[a3348ce]448[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
[1e7eaab]449# Obtener partición.
[a3348ce]450PART="$(ogDiskToDev $1 $2)" || return $?
451
[2af9738]452mount | awk -v P=$PART '{if ($1==P) {print $3}}'
[a3348ce]453}
454
455
456#/**
[b6208d8]457#         ogIsFormated int_ndisk int_nfilesys
[5a4f399]458#@brief   Comprueba si un sistema de archivos está formateado.
459#@param   int_ndisk      nº de orden del disco o volumen.
[b6208d8]460#@param   int_nfilesys   nº de orden del sistema de archivos
[7685100]461#@return  Código de salida: 0 - formateado, 1 - sin formato o error.
[5a4f399]462#@version 0.91 - Adaptación inicial para comprobar que existe caché.
463#@author  Ramon Gomez, ETSII Universidad de Sevilla
464#@date    2010-03-18
[7685100]465#@version 1.0.1 - Devolver falso en caso de error.
466#@author  Ramon Gomez, ETSII Universidad de Sevilla
467#@date    2011-05-18
[b6208d8]468#@version 1.0.5 - Dejar de usar "parted".
469#@author  Ramon Gomez, ETSII Universidad de Sevilla
[3011075]470#@date    2012-09-04
[5a4f399]471#*/ ##
472function ogIsFormated ()
473{
474# Variables locales
475local DISK
476if [ "$*" == "help" ]; then
[e087194]477    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_nfilesys" \
[5a4f399]478           "if $FUNCNAME 1 1; then ... ; fi"
479    return
480fi
[7685100]481# Falso, en caso de error.
482[ $# == 2 ] || return 1
[5a4f399]483
[b6208d8]484test -n "$(ogMount "$1" "$2")"
[5a4f399]485}
486
487
488#/**
[b6208d8]489#         ogIsMounted int_ndisk int_nfilesys
[a3348ce]490#@brief   Comprueba si un sistema de archivos está montado.
[42669ebf]491#@param   int_ndisk      nº de orden del disco
[b6208d8]492#@param   int_nfilesys   nº de orden del sistema de archivos
[7685100]493#@return  Código de salida: 0 - montado, 1 - sin montar o error.
[b6208d8]494#@version 0.9 - Primera versión para OpenGnSys.
[f8f4dfa]495#@author  Ramon Gomez, ETSII Universidad de Sevilla
496#@date    2009-10-15
[7685100]497#@version 1.0.1 - Devolver falso en caso de error.
498#@author  Ramon Gomez, ETSII Universidad de Sevilla
499#@date    2011-05-18
[1e7eaab]500#*/ ##
[42669ebf]501function ogIsMounted ()
502{
503# Si se solicita, mostrar ayuda.
[a3348ce]504if [ "$*" == "help" ]; then
[e087194]505    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_nfilesys" \
[a3348ce]506           "if $FUNCNAME 1 1; then ... ; fi"
507    return
508fi
[7685100]509# Falso, en caso de error.
510[ $# == 2 ] || return 1
[a3348ce]511
512test -n "$(ogGetMountPoint $1 $2)"
513}
514
515
[aae34f6]516#/**
[9d96c57]517#         ogIsLocked int_ndisk int_npartition
518#@brief   Comprueba si una partición está bloqueada por una operación de uso exclusivo.
[42669ebf]519#@param   int_ndisk      nº de orden del disco
520#@param   int_npartition nº de orden de la partición
[7685100]521#@return  Código de salida: 0 - bloqueado, 1 - sin bloquear o error.
[73c8417]522#@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]523#@version 0.9 - Primera versión para OpenGnSys.
[9d96c57]524#@author  Ramon Gomez, ETSII Universidad de Sevilla
525#@date    2009-09-03
[7685100]526#@version 1.0.1 - Devolver falso en caso de error.
527#@author  Ramon Gomez, ETSII Universidad de Sevilla
528#@date    2011-05-18
[1e7eaab]529#*/ ##
[42669ebf]530function ogIsLocked ()
531{
[59f9ad2]532# Variables locales
[73c8417]533local PART LOCKFILE
[9d96c57]534
[1e7eaab]535# Si se solicita, mostrar ayuda.
[9d96c57]536if [ "$*" == "help" ]; then
537    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
[a3348ce]538           "if $FUNCNAME 1 1; then ... ; fi"
[9d96c57]539    return
540fi
[7685100]541# Falso, en caso de error.
542[ $# == 2 ] || return 1
[9d96c57]543
[1e7eaab]544# Obtener partición.
[7685100]545PART="$(ogDiskToDev $1 $2)" || return 1
[9d96c57]546
[1e7eaab]547# Comprobar existencia del fichero de bloqueo.
[73c8417]548LOCKFILE="/var/lock/lock${PART//\//-}"
549test -f $LOCKFILE
[9d96c57]550}
551
552
553#/**
554#         ogLock int_ndisk int_npartition
[89403cd]555#@see     ogLockPartition
556#*/
[42669ebf]557function ogLock ()
558{
[89403cd]559ogLockPartition "$@"
560}
561
562#/**
563#         ogLockPartition int_ndisk int_npartition
[9d96c57]564#@brief   Genera un fichero de bloqueo para una partición en uso exlusivo.
[42669ebf]565#@param   int_ndisk      nº de orden del disco
566#@param   int_npartition nº de orden de la partición
[9d96c57]567#@return  (nada)
568#@exception OG_ERR_FORMAT    Formato incorrecto.
569#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
[73c8417]570#@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]571#@version 0.9 - Primera versión para OpenGnSys.
[9d96c57]572#@author  Ramon Gomez, ETSII Universidad de Sevilla
573#@date    2009-09-03
[1e7eaab]574#*/ ##
[42669ebf]575function ogLockPartition ()
576{
[59f9ad2]577# Variables locales
578local PART LOCKFILE
[9d96c57]579
[1e7eaab]580# Si se solicita, mostrar ayuda.
[9d96c57]581if [ "$*" == "help" ]; then
582    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
583           "$FUNCNAME 1 1"
584    return
585fi
[1e7eaab]586# Error si no se reciben 2 parámetros.
[9d96c57]587[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
588
[1e7eaab]589# Obtener partición.
[9d96c57]590PART="$(ogDiskToDev $1 $2)" || return $?
591
[1e7eaab]592# Crear archivo de bloqueo exclusivo.
[73c8417]593LOCKFILE="/var/lock/lock${PART//\//-}"
594touch $LOCKFILE
[9d96c57]595}
596
597
598#/**
[b6208d8]599#         ogMount int_ndisk int_nfilesys
[3458879]600#@see     ogMountFs ogMountCache ogMountCdrom
[1e7eaab]601#*/ ##
[42669ebf]602function ogMount ()
603{
[ee4a96e]604case "$*" in
[18f4bc2]605    CACHE|cache)
606        ogMountCache ;;
[ee4a96e]607    CDROM|cdrom)
608        ogMountCdrom ;;
609    *)  ogMountFs "$@" ;;
610esac
[73c8417]611}
612
[ee4a96e]613
[73c8417]614#/**
[b6208d8]615#         ogMountFs int_ndisk int_nfilesys
[aae34f6]616#@brief   Monta un sistema de archivos.
[42669ebf]617#@param   int_ndisk      nº de orden del disco
[b6208d8]618#@param   int_nfilesys   nº de orden del sistema de archivos
[aae34f6]619#@return  Punto de montaje
620#@exception OG_ERR_FORMAT    Formato incorrecto.
621#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
622#@exception OG_ERR_PARTITION Tipo de particion desconocido o no se puede montar.
[985bef0]623#@version 0.1 -  Integracion para Opengnsys  -  EAC:   MountPartition() en FileSystem.lib
624#@author  Antonio J. Doblas Viso. Universidad de Malaga
625#@date    2008-10-27
[b6208d8]626#@version 0.9 - Primera version para OpenGnSys.
[aae34f6]627#@author  Ramon Gomez, ETSII Universidad de Sevilla
[40488da]628#@date    2009-09-28
[b6208d8]629#@version 1.0.5 - Independiente del tipo de sistema de ficheros.
[02f271b]630#@author  Ramon Gomez, ETSII Universidad de Sevilla
[3011075]631#@date    2012-09-04
[1e7eaab]632#*/ ##
[42669ebf]633function ogMountFs ()
634{
[1e7eaab]635# Variables locales
[b6208d8]636local PART MNTDIR
[aae34f6]637
[1e7eaab]638# Si se solicita, mostrar ayuda.
[aae34f6]639if [ "$*" == "help" ]; then
[e087194]640    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_nfilesys" \
[aae34f6]641           "$FUNCNAME 1 1  =>  /mnt/sda1"
642    return
643fi
[1e7eaab]644# Error si no se reciben 2 parámetros.
[aae34f6]645[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
646
[1e7eaab]647# Obtener partición.
[b6208d8]648PART="$(ogDiskToDev "$1" "$2")" || return $?
[aae34f6]649
[1e7eaab]650# Comprobar si el sistema de archivos ya está montada.
[a3348ce]651MNTDIR="$(ogGetMountPoint $1 $2)"
[e2c805a]652# Si no, montarlo en un directorio de sistema.
[aae34f6]653if [ -z "$MNTDIR" ]; then
654    # Error si la particion esta bloqueada.
[52fa3da]655    if ogIsLocked $1 $2; then
656        ogRaiseError $OG_ERR_LOCKED "$MSG_PARTITION, $1 $2"
657        return $?
658    fi
[e2c805a]659    # Crear punto de montaje o enlace simbólico para caché local.
[aae34f6]660    MNTDIR=${PART/dev/mnt}
[3ffa256]661    if [ "$(ogFindCache)" == "$1 $2" -a -n "$OGCAC" ]; then
[e2c805a]662        mkdir -p $OGCAC
663        ln -fs $OGCAC $MNTDIR
664    else
665        mkdir -p $MNTDIR
666    fi
[b6208d8]667    # Montar sistema de archivos.
668    mount $PART $MNTDIR &>/dev/null || \
669               mount $PART $MNTDIR -o force,remove_hiberfile &>/dev/null || \
670               ogRaiseError $OG_ERR_PARTITION "$1, $2" || return $?
[aae34f6]671fi
[cbbb046]672echo "$MNTDIR"
[aae34f6]673}
674
675
[ee4a96e]676#####  PRUEBAS
677# Montar CDROM
[42669ebf]678function ogMountCdrom ()
679{
[ee4a96e]680local DEV MNTDIR
681DEV="/dev/cdrom"            # Por defecto
682MNTDIR=$(mount | awk -v D=$DEV '{if ($1==D) {print $3}}')
683if [ -z "$MNTDIR" ]; then
684    MNTDIR=${DEV/dev/mnt}
685    mkdir -p $MNTDIR
686    mount -t iso9660 $DEV $MNTDIR || ogRaiseError $OG_ERR_PARTITION "cdrom" || return $?
687fi
688echo $MNTDIR
689}
690
[3f49cf7]691
692#/**
[b6208d8]693#         ogReduceFs int_ndisk int_nfilesys
[3f49cf7]694#@brief   Reduce el tamaño del sistema de archivos, sin tener en cuenta el espacio libre.
[42669ebf]695#@param   int_ndisk      nº de orden del disco
[b6208d8]696#@param   int_nfilesys   nº de orden del sistema de archivos
[d3669a2]697#@return  int_tamañoKB - tamaño en KB
[3f49cf7]698#@exception OG_ERR_FORMAT    Formato incorrecto.
699#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
700#@exception OG_ERR_PARTITION Partición desconocida o no accesible.
[3458879]701#@warning En Windows, se borran los ficheros pagefile.sys e hiberfile.sys
[d3669a2]702#@warning El sistema de archivos se amplía al mínimo + 10%.
[3458879]703#@note    Requisitos:   *resize*
[985bef0]704#@version 0.1 -  Integracion para Opengnsys  -  EAC:   ReduceFileSystem() en ATA.lib
705#@author  Antonio J. Doblas Viso. Universidad de Malaga
706#@date    2008-10-27
[b6208d8]707#@version 0.9 - Primera version para OpenGnSys.
[3f49cf7]708#@author  Ramon Gomez, ETSII Universidad de Sevilla
709#@date    2009-09-23
[d3669a2]710#@version 0.9.2 - Añadir un 10% al tamaño mínimo requerido.
711#@author  Ramon Gomez, ETSII Universidad de Sevilla
712#@date    2010-09-27
[c114529]713#@version 1.0 -  Deteccion automatica del tamaño minimo adecuado
714#@author  Antonio J. Doblas Viso. Universidad de Malaga
715#@date    2011-02-24
[1e7eaab]716#*/ ##
[42669ebf]717function ogReduceFs ()
718{
[3f49cf7]719# Variables locales
720local PART BLKS SIZE
721
[1e7eaab]722# Si se solicita, mostrar ayuda.
[3f49cf7]723if [ "$*" == "help" ]; then
[e087194]724    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_nfilesys" \
[3f49cf7]725           "$FUNCNAME 1 1"
726    return
727fi
[1e7eaab]728# Error si no se reciben 2 parámetros.
[3f49cf7]729[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
730
[1e7eaab]731# Obtener partición.
[3f49cf7]732PART="$(ogDiskToDev $1 $2)" || return $?
733
[1e7eaab]734# Redimensionar según el tipo de particion.
[3f49cf7]735case "$(ogGetFsType $1 $2)" in
[1c04494]736    EXT[234])
[3458879]737        ogUnmount $1 $2 2>/dev/null
[1c04494]738        # Ext2/3/4: Tamaño de los bloques del sistema de archivos
739        BLKS=$(tune2fs -l $PART | awk '/Block size/ {print int($3/512)}')
740        # Traduce el num. en sectores de 512B a tamano en MB.
[c114529]741        #SIZE=$(resize2fs -P $PART 2>/dev/null | \
742                #       awk -v B=$BLKS '/minimum size/ {print int($7*1.1*B/2048)}')
743        #resize2fs -fp $PART "${SIZE}M" &>/dev/null || ogRaiseError $OG_ERR_PARTITION "$1,$2" || return $?
744        resize2fs -fpM $PART  &>/dev/null || ogRaiseError $OG_ERR_PARTITION "$1,$2" || return $?
[743257e]745        ;;
[3198512]746    BTRFS)
747        MNTDIR=$(ogMount $1 $2)
748        # Calcular tamaño ocupado + 10%.
749        SIZE=$[ $(df -k $MNTDIR | awk '{getline;print $3}') * 110 / 100 ]
750        btrfs filesystem resize ${SIZE}k $MNTDIR
751        ;;
752    REISERFS|REISER4)
753        MNTDIR=$(ogMount $1 $2)
754        # Calcular tamaño ocupado + 10%.
755        SIZE=$[ $(df -k $MNTDIR | awk '{getline;print $3}') * 110 / 100 ]
756        ogUnmount $1 $2 2>/dev/null
757        resize_reiserfs -s${SIZE}K $PART
758        ;;
[c452625]759    JFS)    ;;          # No se reduce (por el momento).
760    XFS)    ;;          # No se reduce (por el momento).
[b6208d8]761    NTFS)
[3458879]762        ogDeleteFile $1 $2 pagefile.sys
[6277770]763        ogDeleteFile $1 $2 hiberfil.sys
[3458879]764        ogUnmount $1 $2 2>/dev/null
[c114529]765        ## NTFS: Obtiene tamaño mínimo en MB.
766        #SIZE=$(ntfsresize -fi $PART | awk '/resize at/ {print int($8*1.1)}')
767        #ntfsresize -fns "${SIZE}M" $PART >/dev/null || ogRaiseError $OG_ERR_PARTITION "$1,$2" || return $?
768        #ntfsresize -fs "${SIZE}M" $PART <<<"y" >/dev/null || ogRaiseError $OG_ERR_PARTITION "$1,$2" || return $?
[6277770]769        SIZE=$(ogReduceFsCheck $1 $2)
770        [ "$SIZE" == 0 ] && return 1   
[c114529]771        ntfsresize -fs "${SIZE}M" $PART <<<"y"  || ogRaiseError $OG_ERR_PARTITION "error reduciendo $1,$2" || return $?
[6277770]772        ;;
[c452625]773    EXFAT)  ;;          # No se reduce (por el momento).
774    FAT32|FAT16)  ;;    # No se reduce (probar "fatresize").
775    HFS|HFSPLUS)  ;;    # No se reduce (por el momento).
776    UFS)    ;;          # No se reduce (por el momento).
[1c04494]777    *)  ogRaiseError $OG_ERR_PARTITION "$1,$2"
[6277770]778        return $? ;;
[3f49cf7]779esac
[c114529]780ogGetFsSize $1 $2
[3f49cf7]781}
782
783
[c114529]784function ogReduceFsCheck ()
785{
786#IMPORTANTE: retorna el valor en MB que podrá reducir el FS de una particion ntfs
787#valor devuelto 0, y codigo error 1. No se puede reducir, probar a reiniciar windows y chkdsk
788
789
790local  PART RC MODE SIZE SIZEDATA
791[ $# == 2 ] && MODE=STAGE1
792[ $# == 3 ] && MODE=STAGE2
793[ -z $MODE ] && return
794
795PART="$(ogDiskToDev $1 $2)" || return $?
796ogUnmount $1 $2 &>/dev/null
797
798
799case $MODE in
800        STAGE1)
801        #       echo "primera etapa $*"
802                ntfsresize -fi $PART &>/dev/null
803                RC=`echo $?`
804        #       echo "RC es" $RC
805                if [ "$RC" -eq "1" ]
806                then
807                        echo "0"
808                        return 1       
809                fi 
810                SIZEDATA=$(ntfsresize -fi $PART | awk '/resize at/ {print $8+1000}')
811        #       echo "salida" $?
812        #       echo $SIZEDATA
813                ogReduceFsCheck $1 $2 $SIZEDATA
814                return 0
815       ;;
816        STAGE2)
817        #       echo "segunda etapa $*"
818                SIZEDATA=$3
819                ntfsresize -fns "${SIZEDATA}M" $PART &>/tmp/ntfsresize.txt
820                RC=$?
821                if [ "$RC" == "0" ]
822                then 
823                        SIZE=$SIZEDATA 
824                        echo $SIZE     
825                else
826                        SIZEEXTRA=$(cat /tmp/ntfsresize.txt | awk '/Needed relocations :/ {print $0}' | awk -F"(" '{print $2}' | awk '{print $1+500}')
827                        SIZE=$(expr $SIZEDATA + $SIZEEXTRA)
828                        ogReduceFsCheck $1 $2 $SIZE
829                        return 0
830                fi
831        ;;
832        *)
833        return
834        ;;
835esac
836}
837
838
839
[5dbb046]840#/**
[9d96c57]841#         ogUnlock int_ndisk int_npartition
[89403cd]842#@see     ogUnlockPartition
[6e390b1]843#*/ ##
[42669ebf]844function ogUnlock ()
845{
[89403cd]846ogUnlockPartition "$@"
847}
848
849#/**
850#         ogUnlockPartition int_ndisk int_npartition
[9d96c57]851#@brief   Elimina el fichero de bloqueo para una particion.
[42669ebf]852#@param   int_ndisk      nº de orden del disco
853#@param   int_npartition nº de orden de la partición
[9d96c57]854#@return  (nada)
855#@exception OG_ERR_FORMAT    Formato incorrecto.
856#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
[73c8417]857#@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]858#@version 0.9 - Primera versión para OpenGnSys.
[9d96c57]859#@author  Ramon Gomez, ETSII Universidad de Sevilla
860#@date    2009-09-03
[1e7eaab]861#*/ ##
[42669ebf]862function ogUnlockPartition ()
863{
[59f9ad2]864# Variables locales
865local PART LOCKFILE
[9d96c57]866
[1e7eaab]867# Si se solicita, mostrar ayuda.
[9d96c57]868if [ "$*" == "help" ]; then
869    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
870           "$FUNCNAME 1 1"
871    return
872fi
[1e7eaab]873# Error si no se reciben 2 parámetros.
[9d96c57]874[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
875
[1e7eaab]876# Obtener partición.
[9d96c57]877PART="$(ogDiskToDev $1 $2)" || return $?
878
[1e7eaab]879# Borrar archivo de bloqueo exclusivo.
[73c8417]880LOCKFILE="/var/lock/lock${PART//\//-}"
881rm -f $LOCKFILE
[9d96c57]882}
883
884
885#/**
[5dbb046]886#         ogUnmount int_ndisk int_npartition
[40488da]887#@see     ogUnmountFs
[6e390b1]888#*/ ##
[42669ebf]889function ogUnmount ()
890{
[40488da]891ogUnmountFs "$@"
[89403cd]892}
893
894#/**
[b6208d8]895#         ogUnmountFs int_ndisk int_nfilesys
[5dbb046]896#@brief   Desmonta un sistema de archivos.
[42669ebf]897#@param   int_ndisk      nº de orden del disco
[b6208d8]898#@param   int_nfilesys   nº de orden del sistema de archivos
[5dbb046]899#@return  Nada
900#@exception OG_ERR_FORMAT    Formato incorrecto.
901#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
902#@warning La partición no está previamente montada o no se puede desmontar.
[985bef0]903#@version 0.1 -  Integracion para Opengnsys  -  EAC:  UmountPartition() en FileSystem.lib
904#@author  Antonio J. Doblas Viso. Universidad de Malaga
905#@date    2008-10-27
[b6208d8]906#@version 0.9 - Primera version para OpenGnSys.
[5dbb046]907#@author  Ramon Gomez, ETSII Universidad de Sevilla
[40488da]908#@date    2009-09-28
[1e7eaab]909#*/ ##
[42669ebf]910function ogUnmountFs ()
911{
[59f9ad2]912# Variables locales
[c56dec5]913local PART MNTDIR
[5dbb046]914
[1e7eaab]915# Si se solicita, mostrar ayuda.
[5dbb046]916if [ "$*" == "help" ]; then
917    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" "$FUNCNAME 1 1"
918    return
919fi
[1e7eaab]920# Error si no se reciben 2 parámetros.
[5dbb046]921[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
922
[1e7eaab]923# Obtener partición y punto de montaje.
[5dbb046]924PART="$(ogDiskToDev $1 $2)" || return $?
[a3348ce]925MNTDIR="$(ogGetMountPoint $1 $2)"
[5dbb046]926
[1e7eaab]927# Si está montada, desmontarla.
[c56dec5]928if [ -n "$MNTDIR" ]; then
[5a4f399]929    # Error si la particion está bloqueada.
[52fa3da]930    if ogIsLocked $1 $2; then
931        ogRaiseError $OG_ERR_LOCKED "$MSG_PARTITION $1, $2"
932        return $?
933    fi
[5a4f399]934    # Desmontar y borrar punto de montaje.
[52fa3da]935    umount $PART 2>/dev/null || ogEcho warning "$FUNCNAME: $MSG_DONTUNMOUNT: \"$1, $2\""
[5a4f399]936    rmdir $MNTDIR 2>/dev/null || rm -f $MNTDIR 2>/dev/null
[5dbb046]937else
938    ogEcho warning "$MSG_DONTMOUNT: \"$1,$2\""
939fi
940}
941
[ee4a96e]942
[be81649]943#/**
944#         ogUnmountAll int_ndisk
945#@brief   Desmonta todos los sistema de archivos de un disco, excepto el caché local.
[42669ebf]946#@param   int_ndisk      nº de orden del disco
[be81649]947#@return  Nada
948#@exception OG_ERR_FORMAT    Formato incorrecto.
949#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
950#@warning No se desmonta la partición marcada como caché local.
[b6208d8]951#@version 0.9 - Versión para OpenGnSys.
[be81649]952#@author  Ramon Gomez, ETSII Universidad de Sevilla
953#@date    2009/10/07
[1e7eaab]954#*/ ##
[42669ebf]955function ogUnmountAll ()
956{
[be81649]957# Variables locales
[18f4bc2]958local DISK PART
[1e7eaab]959# Si se solicita, mostrar ayuda.
[18f4bc2]960if [ "$*" == "help" ]; then
961    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk" "FUNCNAME 1"
962    return
963fi
[1e7eaab]964# Error si no se recibe 1 parámetro.
[18f4bc2]965[ $# == 1 ] || ogRaiseError $OG_ERR_FORMAT || return $?
966
[1e7eaab]967# Obtener partición y punto de montaje.
[18f4bc2]968DISK="$(ogDiskToDev $1)" || return $?
969for ((PART=1; PART<=$(ogGetPartitionsNumber $1); PART++)); do
970    case "$(ogGetFsType $1 $PART)" in
[5804229]971        CACHE) ;;
[7c52c30]972        *)     ogUnmount $1 $PART 2>/dev/null ;;
[7250491]973    esac
[18f4bc2]974done
975}
976
[c114529]977
[0390d46]978# AVISO:  Componer corretcamente esta función.
[c114529]979function ogGetFreeSize () {
[d5fc0dc]980local particion unit factor valor
[c114529]981if [ $# = 0 ]
982then
983        echo "sintaxis: ogGetFreeSize int_disco int_partition str_SizeOutput [ kB MB GB -default GB]-]" red
984        echo "devuelve int_size : int_data : int_free" red
985return
986fi
987if [ $# -ge 2 ]
988then
989        particion=`ogMount $1 $2 ` #1>/dev/null 2>&1
990        if [ -z $3 ]
991                then
992                        unit=kB  # s B kB MB GB TB %
993                else
994                        unit=$3
995        fi
996        case $unit in
997                kB)
998                        factor="1.024";
999                        #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}'`
1000                        valor=`df | grep  $particion | awk -F" " '{size=$2*1.024; used=$3*1.024; free=$4*1.024; printf "%d", free}'`
1001                        ;;
[a957f02]1002                MB)
1003                        factor="1.024/1000";
1004                        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}'`
1005                ;;
1006                GB)
1007                        factor="1.024/1000000";
1008                        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}'`
1009                ;;
1010        esac
1011        #echo $valor
1012        #NumberRound $valor
1013        #valor=`NumberRound $valor`;
1014        echo $valor
1015fi
[c114529]1016}
1017
Note: See TracBrowser for help on using the repository browser.