source: client/engine/FileSystem.lib @ ead38fb

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 ead38fb was 311532f, checked in by ramon <ramongomez@…>, 15 years ago

Creada clase de funciones Cache.

git-svn-id: https://opengnsys.es/svn/trunk@811 a21b9725-9963-47de-94b9-378ad31fedc9

  • Property mode set to 100755
File size: 25.2 KB
Line 
1#!/bin/bash
2#/**
3#@file    FileSystem.lib
4#@brief   Librería o clase FileSystem
5#@class   FileSystem
6#@brief   Funciones para gestión de sistemas de archivos.
7#@version 0.9
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#*/
28function ogCheckFs ()
29{
30# Variables locales.
31local PART TYPE PROG PARAMS
32
33#/// Error si no se reciben 2 parámetros.
34[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
35#/// Obtener partición.
36PART="$(ogDiskToDev $1 $2)" || return $?
37
38TYPE=$(ogGetFsType $1 $2)
39case "$TYPE" in
40    EXT[234])     PROG="e2fsck" ;;
41    REISERFS)     PROG="reiserfsck"; PARAMS="<<<\"Yes\"" ;;
42    JFS)          PROG="fsck.jfs" ;;
43    XFS)          PROG="fsck.xfs" ;;
44    NTFS|HNTFS)   PROG="ntfsfix" ;;
45    FAT32|HFAT32) PROG="dosfsck"; PARAMS="-a" ;;
46    FAT16|HFAT16) PROG="dosfsck"; PARAMS="-a" ;;
47    FAT12|HFAT12) PROG="dosfsck"; PARAMS="-a" ;;
48    *)            ogRaiseError $OG_ERR_PARTITION "$1, $2, $TYPE"
49                      return $? ;;
50esac
51#/// Error si el sistema de archivos esta montado o bloqueado.
52if ogIsMounted $1 $2; then
53    ogRaiseError $OG_ERR_PARTITION "$1 $2"       # Indicar nuevo error
54    return $?
55fi
56if ogIsLocked $1 $2; then
57    ogRaiseError $OG_ERR_LOCKED "$1 $2"
58    return $?
59fi
60#/// Comprobar en modo uso exclusivo.
61ogLock $1 $2
62eval $PROG $PARAMS $PART
63ERRCODE=$?
64case $ERRCODE in
65    0)    ;;
66    127)  ogRaiseError $OG_ERR_NOTEXEC "$PROG" ;;
67    *)    ogRaiseError $OG_ERR_PARTITION "$1 $2" ;;
68esac
69ogUnlock $1 $2
70return $ERRCODE
71}
72
73
74#/**
75#         ogExtendFs int_ndisk int_npartition
76#@brief   Extiende un sistema de archivos al tamaño de su partición.
77#@param   int_ndisk      nº de orden del disco
78#@param   int_npartition nº de orden de la partición
79#@return  (nada)
80#@exception OG_ERR_FORMAT   Formato incorrecto.
81#@exception OG_ERR_NOTFOUND Disco o particion no corresponden con un dispositivo.
82#@exception OG_ERR_PARTITION Partición desconocida o no accesible.
83#@note    Requisitos: *resize*
84#@version 0.9 - Primera adaptación para OpenGNSys.
85#@author  Ramon Gomez, ETSII Universidad de Sevilla
86#@date    2009-09-23
87#*/
88function ogExtendFs ()
89{
90# Variables locales.
91local PART PROG PARAMS
92
93#/// Si se solicita, mostrar ayuda.
94if [ "$*" == "help" ]; then
95    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
96           "$FUNCNAME 1 1"
97    return
98fi
99#/// Error si no se reciben 2 parámetros.
100[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
101
102#/// Obtener partición.
103PART="$(ogDiskToDev $1 $2)" || return $?
104
105ogUnmount $1 $2 2>/dev/null
106#/// Redimensionar al tamano máximo según el tipo de partición.
107TYPE=$(ogGetFsType $1 $2)
108case "$TYPE" in
109    EXT[234])   PROG="resize2fs"; PARAMS="-f" ;;
110    REISERFS)   PROG="resize_reiserfs"; PARAMS="-f" ;;
111    NTFS|HNTFS) PROG="ntfsresize"; PARAMS="<<<\"y\" -f" ;;
112    *)          ogRaiseError $OG_ERR_PARTITION "$1 $2 $TYPE"
113                return $? ;;
114esac
115#/// Error si el sistema de archivos está montado o bloqueado.
116if ogIsMounted $1 $2; then
117    ogRaiseError $OG_ERR_PARTITION "$1 $2"       # Indicar nuevo error
118    return $?
119fi
120if ogIsLocked $1 $2; then
121    ogRaiseError $OG_ERR_LOCKED "$1 $2"
122    return $?
123fi
124#/// Redimensionar en modo uso exclusivo.
125ogLock $1 $2
126eval $PROG $PARAMS $PART &>/dev/null
127ERRCODE=$?
128case $ERRCODE in
129    0)    ;;
130    127)  ogRaiseError $OG_ERR_NOTEXEC "$PROG" ;;
131    *)    ogRaiseError $OG_ERR_PARTITION "$1 $2" ;;
132esac
133ogUnlock $1 $2
134return $ERRCODE
135}
136
137
138#/**
139#         ogFormat int_ndisk int_npartition | CACHE
140#@see     ogFormatFs ogFormatCache
141#*/
142function ogFormat ()
143{
144case "$*" in
145    CACHE|cache)  ogFormatCache ;;
146    *)            ogFormatFs "$@" ;;
147esac
148}
149
150
151#/**
152#         ogFoarmatFs int_ndisk int_npartition [type_fstype] [str_label]
153#@brief   Formatea un sistema de ficheros según el tipo de su partición.
154#@param   int_ndisk      nº de orden del disco
155#@param   int_npartition nº de orden de la partición
156#@param   type_fstype    mnemónico de sistema de ficheros a formatear
157#@param   str_label      etiqueta de volumen (opcional)
158#@return  (por determinar)
159#@exception OG_ERR_FORMAT    Formato incorrecto.
160#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
161#@exception OG_ERR_PARTITION Partición no accesible o desconocida.
162#@note    Requisitos:   mkfs*
163#@warning No formatea particiones montadas ni bloqueadas.
164#@todo    Definir salidas.
165#@version 0.9 - Primera versión para OpenGNSys.
166#@author  Ramon Gomez, ETSII Universidad de Sevilla
167#@date    2009-10-08
168#*/ ##
169function ogFormatFs ()
170{
171# Variables locales
172local PART ID TYPE LABEL PROG PARAMS ERRCODE
173
174# Si se solicita, mostrar ayuda.
175if [ "$*" == "help" ]; then
176    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition [str_label]" \
177           "$FUNCNAME 1 1" \
178           "$FUNCNAME 1 1 EXT4" \
179           "$FUNCNAME 1 1 \"DATA\"" \
180           "$FUNCNAME 1 1 EXT4 \"DATA\""
181    return
182fi
183# Error si no se reciben entre 2 y 4 parámetros.
184[ $# -ge 2 -a $# -le 4 ] || ogRaiseError $OG_ERR_FORMAT || return $?
185# Obtener dispositivo y tipo de sisitema de archivos.
186PART="$(ogDiskToDev $1 $2)" || return $?
187TYPE="$(ogGetFsType $1 $2)" || return $?
188
189# Elegir tipo de formato segun el tipo de particion.
190case "$3" in
191    EXT2)      ID=83; PROG="mkfs.ext2";;
192    EXT3)      ID=83; PROG="mkfs.ext3";;
193    EXT4)      ID=83; PROG="mkfs.ext4";;
194    REISERFS)  ID=83; PROG="mkfs.reiserfs"; PARAMS="-f" ;;
195    REISER4)   ID=83; PROG="mkfs.reiser4";;
196    XFS)       ID=83; PROG="mkfs.xfs"; PARAMS="-f" ;;
197    JFS)       ID=83; PROG="mkfs.jfs"; PARAMS="<<<\"y\"";;
198    NTFS)      ID=7;  PROG="mkntfs"; PARAMS="-f" ;;
199    HNTFS)     ID=17; PROG="mkntfs"; PARAMS="-f" ;;
200    FAT32)     ID=b;  PROG="mkdosfs"; PARAMS="-F 32" ;;
201    HFAT32)    ID=1b; PROG="mkdosfs"; PARAMS="-F 32" ;;
202    FAT16)     ID=6;  PROG="mkdosfs"; PARAMS="-F 16" ;;
203    HFAT16)    ID=16; PROG="mkdosfs"; PARAMS="-F 16" ;;
204    FAT12)     ID=1;  PROG="mkdosfs"; PARAMS="-F 12" ;;
205    HFAT12)    ID=11; PROG="mkdosfs"; PARAMS="-F 12" ;;
206    *)         LABEL="$3" ;;
207esac
208# Si no se indica explícitamente, detectar el tipo de sistema de archivos.
209if [ -z "$PROG" ]; then
210    case "$TYPE" in
211        EXT2)         PROG="mkfs.ext2";;
212        EXT3)         PROG="mkfs.ext3";;
213        EXT4)         PROG="mkfs.ext4";;
214        REISERFS)     PROG="mkfs.reiserfs"; PARAMS="-f" ;;
215        REISER4)      PROG="mkfs.reiser4";;
216        XFS)          PROG="mkfs.xfs"; PARAMS="-f" ;;
217        JFS)          PROG="mkfs.jfs"; PARAMS="<<<\"y\"";;
218        NTFS|HNTFS)   PROG="mkntfs"; PARAMS="-f" ;;
219        FAT32|HFAT32) PROG="mkdosfs"; PARAMS="-F 32" ;;
220        FAT16|HFAT16) PROG="mkdosfs"; PARAMS="-F 16" ;;
221        FAT12|HFAT12) PROG="mkdosfs"; PARAMS="-F 12" ;;
222        *)            ogRaiseError $OG_ERR_PARTITION "$1 $2 $TYPE"
223                      return $? ;;
224    esac
225else
226    [ $TYPE == "$3" -o $ID == "$(ogGetPartitionId $1 $2)" ] || ogRaiseError $OG_ERR_PARTITION "$3 != $TYPE" || return $?
227fi
228# Comprobar consistencia entre id. de partición y tipo de sistema de archivos.
229[ -n "$PROG" ] || ogRaiseError $OG_ERR_PARTITION "$3 != $TYPE" || return $?
230
231# Etiquetas de particion.
232if [ -z "$LABEL" ]; then
233    [ "$4" != "CACHE" ] || ogRaiseError $OG_ERR_FORMAT "$MSG_RESERVEDVALUE: CACHE" || return $?
234    [ -n "$4" ] && PARAMS="$PARAMS -L $4"
235else
236    PARAMS="$PARAMS -L $LABEL"
237fi
238
239# Error si la particion esta montada o está bloqueada.
240if ogIsMounted $1 $2; then
241    ogRaiseError $OG_ERR_PARTITION "$1 $2"       # Indicar nuevo error
242    return $?
243fi
244if ogIsLocked $1 $2; then
245    ogRaiseError $OG_ERR_LOCKED "$1 $2"
246    return $?
247fi
248# Formatear en modo uso exclusivo.
249ogLock $1 $2
250eval $PROG $PARAMS $PART 2>/dev/null
251ERRCODE=$?
252case $ERRCODE in
253    0)    ;;
254    127)  ogRaiseError $OG_ERR_NOTEXEC "$PROG" ;;
255    *)    ogRaiseError $OG_ERR_PARTITION "$1 $2" ;;
256esac
257ogUnlock $1 $2
258return $ERRCODE
259}
260
261
262#/**
263#         ogGetFsType int_ndisk int_npartition
264#@brief   Devuelve el mnemonico con el tipo de sistema de archivos.
265#@param   int_ndisk      nº de orden del disco
266#@param   int_npartition nº de orden de la partición
267#@return  Mnemonico
268#@note    Mnemonico: { EXT2, EXT3, EXT4, REISERFS, XFS, JFS, LINUX-SWAP, LINUX-LVM, LINUX-RAID, SOLARIS, FAT16, HFAT16, FAT32, HFAT32, NTFS, HNTFS, WIN-DYNAMIC, CACHE, EMPTY, EXTENDED, UNKNOWN }
269#@exception OG_ERR_FORMAT   Formato incorrecto.
270#@exception OG_ERR_NOTFOUND Disco o particion no corresponden con un dispositivo.
271#@version 0.9 - Primera adaptación para OpenGNSys.
272#@author  Ramon Gomez, ETSII Universidad de Sevilla
273#@date    2009-07-21
274#*/ ##
275function ogGetFsType () {
276
277# Variables locales.
278local ID TYPE
279
280# Si se solicita, mostrar ayuda.
281if [ "$*" == "help" ]; then
282    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
283           "$FUNCNAME 1 1  =>  NTFS"
284    return
285fi
286# Error si no se reciben 2 parámetros.
287[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
288
289# Detectar id. de tipo de partición y codificar al mnemonico.
290DISK=$(ogDiskToDev "$1") || return $?
291ID=$(ogGetPartitionId "$1" "$2") || return $?
292case "$ID" in
293     0)         TYPE="EMPTY" ;;
294     1)         TYPE="FAT12" ;;
295     5|f)       TYPE="EXTENDED" ;;
296     [6e])      TYPE="FAT16" ;;
297     7)         TYPE="NTFS" ;;          # Nota: también puede ser EXFAT
298     [bc])      TYPE="FAT32" ;;
299     11)        TYPE="HFAT12" ;;
300     12)        TYPE="COMPAQDIAG" ;;
301     1[6e])     TYPE="HFAT16" ;;
302     17)        TYPE="HNTFS" ;;
303     1[bc])     TYPE="HFAT32" ;;
304     42)        TYPE="WIN-DYNAMIC" ;;
305     82)        TYPE="LINUX-SWAP" ;;
306     83)        TYPE="$(parted -s $DISK print | awk -v var=$2 '{if ($1==var) {print toupper($6)}}')"
307                TYPE=${TYPE:-"EXT3"}
308                ;;
309     8e)        TYPE="LINUX-LVM" ;;
310     a7)        TYPE="CACHE" ;;         # (compatibilidad con Brutalix)
311     bf)        TYPE="SOLARIS" ;;
312     ca)        TYPE="CACHE" ;;
313     fd)        TYPE="LINUX-RAID" ;;
314     *)         TYPE="UNKNOWN" ;;
315esac
316echo $TYPE
317}
318
319
320#/**
321#         ogGetMountPoint int_ndisk int_npartition
322#@brief   Devuelve el punto de montaje de un sistema de archivos.
323#@param   int_ndisk      nº de orden del disco
324#@param   int_npartition nº de orden de la partición
325#@return  Punto de montaje
326#@exception OG_ERR_FORMAT    Formato incorrecto.
327#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
328#@note    Requisitos: \c mount* \c awk
329#@version 0.9 - Primera versión para OpenGNSys.
330#@author  Ramon Gomez, ETSII Universidad de Sevilla
331#@date    2009-10-15
332#*/ ##
333function ogGetMountPoint ()
334{
335# Variables locales
336local PART
337# Si se solicita, mostrar ayuda.
338if [ "$*" == "help" ]; then
339    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
340           "$FUNCNAME 1 1  =>  /mnt/sda1"
341    return
342fi
343# Error si no se reciben 2 parámetros.
344[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
345# Obtener partición.
346PART="$(ogDiskToDev $1 $2)" || return $?
347
348echo $(mount | awk -v P=$PART '{if ($1==P) {print $3}}')
349}
350
351
352#/**
353#         ogIsMounted int_ndisk int_npartition
354#@brief   Comprueba si un sistema de archivos está montado.
355#@param   int_ndisk      nº de orden del disco
356#@param   int_npartition nº de orden de la partición
357#@return  Código de salida: 0 - sin montar, 1 - montado.
358#@version 0.9 - Primera versión para OpenGNSys.
359#@author  Ramon Gomez, ETSII Universidad de Sevilla
360#@date    2009-10-15
361#*/ ##
362function ogIsMounted ()
363{
364# Si se solicita, mostrar ayuda.
365if [ "$*" == "help" ]; then
366    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
367           "if $FUNCNAME 1 1; then ... ; fi"
368    return
369fi
370# Error si no se reciben 2 parámetros.
371[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
372
373test -n "$(ogGetMountPoint $1 $2)"
374}
375
376
377#/**
378#         ogIsLocked int_ndisk int_npartition
379#@brief   Comprueba si una partición está bloqueada por una operación de uso exclusivo.
380#@param   int_ndisk      nº de orden del disco
381#@param   int_npartition nº de orden de la partición
382#@return  Código de salida: 0 - sin bloquear, 1 - bloqueada.
383#@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 "-".
384#@version 0.9 - Primera versión para OpenGNSys.
385#@author  Ramon Gomez, ETSII Universidad de Sevilla
386#@date    2009-09-03
387#*/ ##
388function ogIsLocked ()
389{
390# Variables locales
391local PART LOCKFILE
392
393# Si se solicita, mostrar ayuda.
394if [ "$*" == "help" ]; then
395    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
396           "if $FUNCNAME 1 1; then ... ; fi"
397    return
398fi
399# Error si no se reciben 2 parámetros.
400[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
401
402# Obtener partición.
403PART="$(ogDiskToDev $1 $2)" || return $?
404
405# Comprobar existencia del fichero de bloqueo.
406LOCKFILE="/var/lock/lock${PART//\//-}"
407test -f $LOCKFILE
408}
409
410
411#/**
412#         ogLock int_ndisk int_npartition
413#@see     ogLockPartition
414#*/
415function ogLock ()
416{
417ogLockPartition "$@"
418}
419
420#/**
421#         ogLockPartition int_ndisk int_npartition
422#@brief   Genera un fichero de bloqueo para una partición en uso exlusivo.
423#@param   int_ndisk      nº de orden del disco
424#@param   int_npartition nº de orden de la partición
425#@return  (nada)
426#@exception OG_ERR_FORMAT    Formato incorrecto.
427#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
428#@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 "-".
429#@version 0.9 - Primera versión para OpenGNSys.
430#@author  Ramon Gomez, ETSII Universidad de Sevilla
431#@date    2009-09-03
432#*/ ##
433function ogLockPartition ()
434{
435# Variables locales
436local PART LOCKFILE
437
438# Si se solicita, mostrar ayuda.
439if [ "$*" == "help" ]; then
440    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
441           "$FUNCNAME 1 1"
442    return
443fi
444# Error si no se reciben 2 parámetros.
445[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
446
447# Obtener partición.
448PART="$(ogDiskToDev $1 $2)" || return $?
449
450# Crear archivo de bloqueo exclusivo.
451LOCKFILE="/var/lock/lock${PART//\//-}"
452touch $LOCKFILE
453}
454
455
456#/**
457#         ogMount int_ndisk int_npartition
458#@see     ogMountFs ogMountCache ogMountCdrom
459#*/ ##
460function ogMount ()
461{
462case "$*" in
463    CACHE|cache)
464        ogMountCache ;;
465    CDROM|cdrom)
466        ogMountCdrom ;;
467    *)  ogMountFs "$@" ;;
468esac
469}
470
471
472#/**
473#         ogMountFs int_ndisk int_npartition
474#@brief   Monta un sistema de archivos.
475#@param   int_ndisk      nº de orden del disco
476#@param   int_npartition nº de orden de la partición
477#@return  Punto de montaje
478#@exception OG_ERR_FORMAT    Formato incorrecto.
479#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
480#@exception OG_ERR_PARTITION Tipo de particion desconocido o no se puede montar.
481#@version 0.9 - Primera versión para OpenGNSys.
482#@author  Ramon Gomez, ETSII Universidad de Sevilla
483#@date    2009-09-28
484#*/ ##
485function ogMountFs ()
486{
487# Variables locales
488local PART TYPE MNTDIR MOUNT PARAMS
489
490# Si se solicita, mostrar ayuda.
491if [ "$*" == "help" ]; then
492    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
493           "$FUNCNAME 1 1  =>  /mnt/sda1"
494    return
495fi
496# Error si no se reciben 2 parámetros.
497[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
498
499# Obtener partición.
500PART="$(ogDiskToDev $1 $2)" || return $?
501
502# Comprobar si el sistema de archivos ya está montada.
503MNTDIR="$(ogGetMountPoint $1 $2)"
504# Si no, montarla en un directorio de sistema
505if [ -z "$MNTDIR" ]; then
506    # Error si la particion esta bloqueada.
507    ogIsLocked $1 $2 && ogRaiseError $OG_ERR_LOCKED "$1 $2" && return $?
508    # Crear punto de montaje.
509    MNTDIR=${PART/dev/mnt}
510    mkdir -p $MNTDIR
511    # Montar según el tipo de sitema de archivos.
512    TYPE="$(ogGetFsType $1 $2)" || return $?
513    case "$TYPE" in
514        CACHE)      MOUNT=mount ;;
515        EXT[234])   MOUNT=mount ;;
516        REISERFS)   insmod /lib/modules/$(uname -r)/kernel/fs/reiserfs/reiserfs.ko 2>/dev/null
517                    MOUNT=mount ;;
518        JFS)        insmod /lib/modules/$(uname -r)/kernel/fs/jfs/jfs.ko 2>/dev/null
519                    MOUNT=mount ;;
520        XFS)        insmod /lib/modules/$(uname -r)/kernel/fs/xfs/xfs.ko 2>/dev/null
521                    MOUNT=mount ;;
522        NTFS|HNTFS) MOUNT=ntfs-3g ;;
523        FAT16|FAT32|HFAT16|HFAT32)
524                    MOUNT=mount; PARAMS="-t vfat" ;;
525        *)  #/// Error, si la partición no es montable.
526            rmdir $MNTDIR
527            ogRaiseError $OG_ERR_PARTITION "$1, $2, $TYPE"
528            return $OG_ERR_PARTITION
529            ;;
530    esac
531    $MOUNT $PARAMS $PART $MNTDIR || $MOUNT $PARAMS $PART $MNTDIR -o force,remove_hiberfile || ogRaiseError $OG_ERR_PARTITION "$1, $2, $TYPE" || return $?
532        # linea temporal durante desarrollo para poder usar el cliente completo nfs y testeas nuevas herramientas.
533    if grep -q nfsroot /proc/cmdline; then
534                echo "$PART $MNTDIR" >> /etc/mtab
535        fi
536        # fin linea temporal.
537fi
538echo $MNTDIR
539}
540
541
542#####  PRUEBAS
543# Montar CDROM
544function ogMountCdrom ()
545{
546local DEV MNTDIR
547DEV="/dev/cdrom"            # Por defecto
548MNTDIR=$(mount | awk -v D=$DEV '{if ($1==D) {print $3}}')
549if [ -z "$MNTDIR" ]; then
550    MNTDIR=${DEV/dev/mnt}
551    mkdir -p $MNTDIR
552    mount -t iso9660 $DEV $MNTDIR || ogRaiseError $OG_ERR_PARTITION "cdrom" || return $?
553fi
554echo $MNTDIR
555}
556
557
558#/**
559#         ogReduceFs int_ndisk int_npartition
560#@brief   Reduce el tamaño del sistema de archivos, sin tener en cuenta el espacio libre.
561#@param   int_ndisk      nº de orden del disco
562#@param   int_npartition nº de orden de la partición
563#@return  tamañoKB
564#@exception OG_ERR_FORMAT    Formato incorrecto.
565#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
566#@exception OG_ERR_PARTITION Partición desconocida o no accesible.
567#@warning En Windows, se borran los ficheros pagefile.sys e hiberfile.sys
568#@warning El sistema de archivos se amplía al mínimo + 1 KB.
569#@note    Requisitos:   *resize*
570#@version 0.9 - Primera versión para OpenGNSys.
571#@author  Ramon Gomez, ETSII Universidad de Sevilla
572#@date    2009-09-23
573#*/ ##
574function ogReduceFs ()
575{
576# Variables locales
577local PART BLKS SIZE
578
579# Si se solicita, mostrar ayuda.
580if [ "$*" == "help" ]; then
581    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
582           "$FUNCNAME 1 1"
583    return
584fi
585# Error si no se reciben 2 parámetros.
586[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
587
588# Obtener partición.
589PART="$(ogDiskToDev $1 $2)" || return $?
590
591# Redimensionar según el tipo de particion.
592case "$(ogGetFsType $1 $2)" in
593    EXT[234])
594        ogUnmount $1 $2 2>/dev/null
595        # Ext2/3/4: Tamaño de los bloques del sistema de archivos
596        BLKS=$(tune2fs -l $PART | awk '/Block size/ {print int($3/512)}')
597        # Traduce el num. en sectores de 512B a tamano en MB.
598        SIZE=$(resize2fs -P $PART 2>/dev/null | \
599                       awk -v B=$BLKS '/minimum size/ {print int($7*B/2048+1000)}')
600        resize2fs -fp $PART "${SIZE}M" &>/dev/null || ogRaiseError $OG_ERR_PARTITION "$1,$2" || return $?
601            ;;
602#    REISERFS)          # Usar "resize_reiserfs"
603#           ;;
604    NTFS|HNTFS)
605        ogDeleteFile $1 $2 pagefile.sys
606        ogDeleteFile $1 $2 hiberfile.sys
607        ogUnmount $1 $2 2>/dev/null
608        # NTFS: Obtiene tamaño mínimo en MB.
609        SIZE=$(ntfsresize -fi $PART | awk '/resize at/ {print $8+1000}')
610        ntfsresize -fns "${SIZE}M" $PART >/dev/null || ogRaiseError $OG_ERR_PARTITION "$1,$2" || return $?
611        ntfsresize -fs "${SIZE}M" $PART <<<"y" >/dev/null || ogRaiseError $OG_ERR_PARTITION "$1,$2" || return $?
612            ;;
613    *)  ogRaiseError $OG_ERR_PARTITION "$1,$2"
614            return $? ;;
615esac
616#/// Mostrar nuevo tamaño en KB.
617echo $[SIZE*1024]
618}
619
620
621#/**
622#         ogUnlock int_ndisk int_npartition
623#@see     ogUnlockPartition
624#*/
625function ogUnlock ()
626{
627ogUnlockPartition "$@"
628}
629
630#/**
631#         ogUnlockPartition int_ndisk int_npartition
632#@brief   Elimina el fichero de bloqueo para una particion.
633#@param   int_ndisk      nº de orden del disco
634#@param   int_npartition nº de orden de la partición
635#@return  (nada)
636#@exception OG_ERR_FORMAT    Formato incorrecto.
637#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
638#@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 "-".
639#@version 0.9 - Primera versión para OpenGNSys.
640#@author  Ramon Gomez, ETSII Universidad de Sevilla
641#@date    2009-09-03
642#*/ ##
643function ogUnlockPartition ()
644{
645# Variables locales
646local PART LOCKFILE
647
648# Si se solicita, mostrar ayuda.
649if [ "$*" == "help" ]; then
650    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
651           "$FUNCNAME 1 1"
652    return
653fi
654# Error si no se reciben 2 parámetros.
655[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
656
657# Obtener partición.
658PART="$(ogDiskToDev $1 $2)" || return $?
659
660# Borrar archivo de bloqueo exclusivo.
661LOCKFILE="/var/lock/lock${PART//\//-}"
662rm -f $LOCKFILE
663}
664
665
666#/**
667#         ogUnmount int_ndisk int_npartition
668#@see     ogUnmountFs
669#*/
670function ogUnmount ()
671{
672ogUnmountFs "$@"
673}
674
675#/**
676#         ogUnmountFs int_ndisk int_npartition
677#@brief   Desmonta un sistema de archivos.
678#@param   int_ndisk      nº de orden del disco
679#@param   int_npartition nº de orden de la partición
680#@return  Nada
681#@exception OG_ERR_FORMAT    Formato incorrecto.
682#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
683#@warning La partición no está previamente montada o no se puede desmontar.
684#@version 0.9 - Primera versión para OpenGNSys.
685#@author  Ramon Gomez, ETSII Universidad de Sevilla
686#@date    2009-09-28
687#*/ ##
688function ogUnmountFs ()
689{
690# Variables locales
691local PART MNTDIR
692
693# Si se solicita, mostrar ayuda.
694if [ "$*" == "help" ]; then
695    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" "$FUNCNAME 1 1"
696    return
697fi
698# Error si no se reciben 2 parámetros.
699[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
700
701# Obtener partición y punto de montaje.
702PART="$(ogDiskToDev $1 $2)" || return $?
703MNTDIR="$(ogGetMountPoint $1 $2)"
704
705# Si está montada, desmontarla.
706if [ -n "$MNTDIR" ]; then
707    # Error si la particion esta bloqueada.
708    ogIsLocked $1 $2 && ogRaiseError $OG_ERR_LOCKED "$1 $2" && return $?
709    # Crear punto de montaje.
710    umount $PART 2>/dev/null && rmdir $MNTDIR || ogEcho warning "$FUNCNAME: $MSG_DONTUNMOUNT: \"$1,$2\""
711        # linea temporal durante desarrollo para testear nuevas herramientas con el cliente completo nfs
712    if grep -q nfsroot /proc/cmdline; then
713                cat /etc/mtab | grep -v $PART > /var/tmp/mtab.temporal && cp /var/tmp/mtab.temporal /var/tmp/mtab && rm /var/tmp/mtab.temporal
714        fi
715        # fin linea temporal.
716else
717    ogEcho warning "$MSG_DONTMOUNT: \"$1,$2\""
718fi
719}
720
721
722#/**
723#         ogUnmountAll int_ndisk
724#@brief   Desmonta todos los sistema de archivos de un disco, excepto el caché local.
725#@param   int_ndisk      nº de orden del disco
726#@return  Nada
727#@exception OG_ERR_FORMAT    Formato incorrecto.
728#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
729#@warning No se desmonta la partición marcada como caché local.
730#@version 0.9 - Versión para OpenGNSys.
731#@author  Ramon Gomez, ETSII Universidad de Sevilla
732#@date    2009/10/07
733#*/ ##
734function ogUnmountAll ()
735{
736# Variables locales
737local DISK PART
738# Si se solicita, mostrar ayuda.
739if [ "$*" == "help" ]; then
740    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk" "FUNCNAME 1"
741    return
742fi
743# Error si no se recibe 1 parámetro.
744[ $# == 1 ] || ogRaiseError $OG_ERR_FORMAT || return $?
745
746# Obtener partición y punto de montaje.
747DISK="$(ogDiskToDev $1)" || return $?
748for ((PART=1; PART<=$(ogGetPartitionsNumber $1); PART++)); do
749    case "$(ogGetFsType $1 $PART)" in
750        CACHE|LINUX-SWAP)
751           ;;
752        *)
753           ogUnmount $1 $PART ;;
754    esac
755done
756}
757
758
759function ogGetFsSize () {
760#/**  @function ogGetFsSize: @brief Muestra el tamanio del sistema de archivos indicado, permite definir la unidad de medida, por defecto GB
761#@param  $1 int_diskEAC
762#@param $2 int_PartitionEAC
763#@param $3 str_UnidadMediada            parametro opcional, admite [ kB MB GB -default GB]
764#@return  cadena con int_TotalSize:int_DataSize:int_DataFree
765#@warning  Salidas de errores no determinada
766#@warning
767#@attention
768#@version 1.0   Date: 27/10/2008 Author Antonio J. Doblas Viso. Universidad de Malaga
769#*/
770if [ $# = 0 ]
771then
772        echo "sintaxis: ogGetFsSize int_disco int_partition str_SizeOutput [ kB MB GB -default GB]-]" red
773        echo "devuelve int_size : int_data : int_free" red
774return
775fi
776if [ $# -ge 2 ]
777then
778        particion=`ogMount $1 $2 ` #1>/dev/null 2>&1
779        if [ -z $3 ]
780                then
781                        unit=GB  # s B kB MB GB TB %
782                else
783                        unit=$3
784        fi
785        case $unit in
786                kB)
787                        factor="1.024";
788                        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}'`
789                ;;
790                MB)
791                        factor="1.024/1000";
792                        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}'`
793                ;;
794                GB)
795                        factor="1.024/1000000";
796                        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}'`
797                ;;
798        esac
799        #echo $valor
800        #NumberRound $valor
801        #valor=`NumberRound $valor`;
802        ogUnmount $1 $2 1>/dev/null 2>&1
803        echo $valor
804
805fi
806
807}
Note: See TracBrowser for help on using the repository browser.