source: client/engine/FileSystem.lib @ 68f360e

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 68f360e was 68f360e, checked in by ramon <ramongomez@…>, 13 years ago

Versión 1.0.4, #543: Corregir errata en captura del tamaño de sistema de archivo en función {{ogGetFsSize}}}.

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

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