source: client/engine/FileSystem.lib @ 9eefff4

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 9eefff4 was 5a4f399, checked in by ramon <ramongomez@…>, 15 years ago

Corrección en script initCache; nueva función ogIsFormated en rama trunk.

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

  • Property mode set to 100755
File size: 26.3 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#         ogIsFormated int_ndisk int_npartition
354#@brief   Comprueba si un sistema de archivos está formateado.
355#@param   int_ndisk      nº de orden del disco o volumen.
356#@param   int_npartition nº de orden del sistema de archivos.
357#@return  int_code       Código de salida
358#@version 0.91 - Adaptación inicial para comprobar que existe caché.
359#@author  Ramon Gomez, ETSII Universidad de Sevilla
360#@date    2010-03-18
361#*/ ##
362function ogIsFormated ()
363{
364# Variables locales
365local DISK
366if [ "$*" == "help" ]; then
367    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
368           "if $FUNCNAME 1 1; then ... ; fi"
369    return
370fi
371# Error si no se reciben 2 parámetros.
372[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
373
374DISK=$(ogDiskToDev "$1")
375test -n "$(parted -sm $DISK print 2>/dev/null | \
376                      awk -F: -v sf="$2" '{if ($1==sf) print $5}')"
377}
378
379
380#/**
381#         ogIsMounted int_ndisk int_npartition
382#@brief   Comprueba si un sistema de archivos está montado.
383#@param   int_ndisk      nº de orden del disco
384#@param   int_npartition nº de orden de la partición
385#@return  Código de salida: 0 - sin montar, 1 - montado.
386#@version 0.9 - Primera versión para OpenGNSys.
387#@author  Ramon Gomez, ETSII Universidad de Sevilla
388#@date    2009-10-15
389#*/ ##
390function ogIsMounted ()
391{
392# Si se solicita, mostrar ayuda.
393if [ "$*" == "help" ]; then
394    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
395           "if $FUNCNAME 1 1; then ... ; fi"
396    return
397fi
398# Error si no se reciben 2 parámetros.
399[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
400
401test -n "$(ogGetMountPoint $1 $2)"
402}
403
404
405#/**
406#         ogIsLocked int_ndisk int_npartition
407#@brief   Comprueba si una partición está bloqueada por una operación de uso exclusivo.
408#@param   int_ndisk      nº de orden del disco
409#@param   int_npartition nº de orden de la partición
410#@return  Código de salida: 0 - sin bloquear, 1 - bloqueada.
411#@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 "-".
412#@version 0.9 - Primera versión para OpenGNSys.
413#@author  Ramon Gomez, ETSII Universidad de Sevilla
414#@date    2009-09-03
415#*/ ##
416function ogIsLocked ()
417{
418# Variables locales
419local PART LOCKFILE
420
421# Si se solicita, mostrar ayuda.
422if [ "$*" == "help" ]; then
423    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
424           "if $FUNCNAME 1 1; then ... ; fi"
425    return
426fi
427# Error si no se reciben 2 parámetros.
428[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
429
430# Obtener partición.
431PART="$(ogDiskToDev $1 $2)" || return $?
432
433# Comprobar existencia del fichero de bloqueo.
434LOCKFILE="/var/lock/lock${PART//\//-}"
435test -f $LOCKFILE
436}
437
438
439#/**
440#         ogLock int_ndisk int_npartition
441#@see     ogLockPartition
442#*/
443function ogLock ()
444{
445ogLockPartition "$@"
446}
447
448#/**
449#         ogLockPartition int_ndisk int_npartition
450#@brief   Genera un fichero de bloqueo para una partición en uso exlusivo.
451#@param   int_ndisk      nº de orden del disco
452#@param   int_npartition nº de orden de la partición
453#@return  (nada)
454#@exception OG_ERR_FORMAT    Formato incorrecto.
455#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
456#@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 "-".
457#@version 0.9 - Primera versión para OpenGNSys.
458#@author  Ramon Gomez, ETSII Universidad de Sevilla
459#@date    2009-09-03
460#*/ ##
461function ogLockPartition ()
462{
463# Variables locales
464local PART LOCKFILE
465
466# Si se solicita, mostrar ayuda.
467if [ "$*" == "help" ]; then
468    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
469           "$FUNCNAME 1 1"
470    return
471fi
472# Error si no se reciben 2 parámetros.
473[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
474
475# Obtener partición.
476PART="$(ogDiskToDev $1 $2)" || return $?
477
478# Crear archivo de bloqueo exclusivo.
479LOCKFILE="/var/lock/lock${PART//\//-}"
480touch $LOCKFILE
481}
482
483
484#/**
485#         ogMount int_ndisk int_npartition
486#@see     ogMountFs ogMountCache ogMountCdrom
487#*/ ##
488function ogMount ()
489{
490case "$*" in
491    CACHE|cache)
492        ogMountCache ;;
493    CDROM|cdrom)
494        ogMountCdrom ;;
495    *)  ogMountFs "$@" ;;
496esac
497}
498
499
500#/**
501#         ogMountFs int_ndisk int_npartition
502#@brief   Monta un sistema de archivos.
503#@param   int_ndisk      nº de orden del disco
504#@param   int_npartition nº de orden de la partición
505#@return  Punto de montaje
506#@exception OG_ERR_FORMAT    Formato incorrecto.
507#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
508#@exception OG_ERR_PARTITION Tipo de particion desconocido o no se puede montar.
509#@version 0.9 - Primera versión para OpenGNSys.
510#@author  Ramon Gomez, ETSII Universidad de Sevilla
511#@date    2009-09-28
512#*/ ##
513function ogMountFs ()
514{
515# Variables locales
516local PART TYPE MNTDIR MOUNT PARAMS
517
518# Si se solicita, mostrar ayuda.
519if [ "$*" == "help" ]; then
520    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
521           "$FUNCNAME 1 1  =>  /mnt/sda1"
522    return
523fi
524# Error si no se reciben 2 parámetros.
525[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
526
527# Obtener partición.
528PART="$(ogDiskToDev $1 $2)" || return $?
529
530# Comprobar si el sistema de archivos ya está montada.
531MNTDIR="$(ogGetMountPoint $1 $2)"
532# Si no, montarla en un directorio de sistema
533if [ -z "$MNTDIR" ]; then
534    # Error si la particion esta bloqueada.
535    ogIsLocked $1 $2 && ogRaiseError $OG_ERR_LOCKED "$1 $2" && return $?
536    # Crear punto de montaje o enlace símbolico para Caché local.
537    MNTDIR=${PART/dev/mnt}
538    TYPE="$(ogGetFsType $1 $2)" || return $?
539    if [ "$TYPE" == "CACHE" -a -n "$OGCAC" ]; then
540        ln -fs $OGCAC $MNTDIR
541    else
542        mkdir -p $MNTDIR
543    fi
544    # Montar según el tipo de sitema de archivos.
545    case "$TYPE" in
546        CACHE)      MOUNT=mount ;;
547        EXT[234])   MOUNT=mount ;;
548        REISERFS)   insmod /lib/modules/$(uname -r)/kernel/fs/reiserfs/reiserfs.ko 2>/dev/null
549                    MOUNT=mount ;;
550        JFS)        insmod /lib/modules/$(uname -r)/kernel/fs/jfs/jfs.ko 2>/dev/null
551                    MOUNT=mount ;;
552        XFS)        insmod /lib/modules/$(uname -r)/kernel/fs/xfs/xfs.ko 2>/dev/null
553                    MOUNT=mount ;;
554        NTFS|HNTFS) MOUNT=ntfs-3g ;;
555        FAT16|FAT32|HFAT16|HFAT32)
556                    MOUNT=mount; PARAMS="-t vfat" ;;
557        *)  #/// Error, si la partición no es montable.
558            rmdir $MNTDIR
559            ogRaiseError $OG_ERR_PARTITION "$1, $2, $TYPE"
560            return $OG_ERR_PARTITION
561            ;;
562    esac
563    $MOUNT $PARAMS $PART $MNTDIR || $MOUNT $PARAMS $PART $MNTDIR -o force,remove_hiberfile || ogRaiseError $OG_ERR_PARTITION "$1, $2, $TYPE" || return $?
564        # linea temporal durante desarrollo para poder usar el cliente completo nfs y testeas nuevas herramientas.
565    if grep -q nfsroot /proc/cmdline; then
566                echo "$PART $MNTDIR" >> /etc/mtab
567        fi
568        # fin linea temporal.
569fi
570echo $MNTDIR
571}
572
573
574#####  PRUEBAS
575# Montar CDROM
576function ogMountCdrom ()
577{
578local DEV MNTDIR
579DEV="/dev/cdrom"            # Por defecto
580MNTDIR=$(mount | awk -v D=$DEV '{if ($1==D) {print $3}}')
581if [ -z "$MNTDIR" ]; then
582    MNTDIR=${DEV/dev/mnt}
583    mkdir -p $MNTDIR
584    mount -t iso9660 $DEV $MNTDIR || ogRaiseError $OG_ERR_PARTITION "cdrom" || return $?
585fi
586echo $MNTDIR
587}
588
589
590#/**
591#         ogReduceFs int_ndisk int_npartition
592#@brief   Reduce el tamaño del sistema de archivos, sin tener en cuenta el espacio libre.
593#@param   int_ndisk      nº de orden del disco
594#@param   int_npartition nº de orden de la partición
595#@return  tamañoKB
596#@exception OG_ERR_FORMAT    Formato incorrecto.
597#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
598#@exception OG_ERR_PARTITION Partición desconocida o no accesible.
599#@warning En Windows, se borran los ficheros pagefile.sys e hiberfile.sys
600#@warning El sistema de archivos se amplía al mínimo + 1 KB.
601#@note    Requisitos:   *resize*
602#@version 0.9 - Primera versión para OpenGNSys.
603#@author  Ramon Gomez, ETSII Universidad de Sevilla
604#@date    2009-09-23
605#*/ ##
606function ogReduceFs ()
607{
608# Variables locales
609local PART BLKS SIZE
610
611# Si se solicita, mostrar ayuda.
612if [ "$*" == "help" ]; then
613    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
614           "$FUNCNAME 1 1"
615    return
616fi
617# Error si no se reciben 2 parámetros.
618[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
619
620# Obtener partición.
621PART="$(ogDiskToDev $1 $2)" || return $?
622
623# Redimensionar según el tipo de particion.
624case "$(ogGetFsType $1 $2)" in
625    EXT[234])
626        ogUnmount $1 $2 2>/dev/null
627        # Ext2/3/4: Tamaño de los bloques del sistema de archivos
628        BLKS=$(tune2fs -l $PART | awk '/Block size/ {print int($3/512)}')
629        # Traduce el num. en sectores de 512B a tamano en MB.
630        SIZE=$(resize2fs -P $PART 2>/dev/null | \
631                       awk -v B=$BLKS '/minimum size/ {print int($7*B/2048+1000)}')
632        resize2fs -fp $PART "${SIZE}M" &>/dev/null || ogRaiseError $OG_ERR_PARTITION "$1,$2" || return $?
633            ;;
634#    REISERFS)          # Usar "resize_reiserfs"
635#           ;;
636    NTFS|HNTFS)
637        ogDeleteFile $1 $2 pagefile.sys
638        ogDeleteFile $1 $2 hiberfile.sys
639        ogUnmount $1 $2 2>/dev/null
640        # NTFS: Obtiene tamaño mínimo en MB.
641        SIZE=$(ntfsresize -fi $PART | awk '/resize at/ {print $8+1000}')
642        ntfsresize -fns "${SIZE}M" $PART >/dev/null || ogRaiseError $OG_ERR_PARTITION "$1,$2" || return $?
643        ntfsresize -fs "${SIZE}M" $PART <<<"y" >/dev/null || ogRaiseError $OG_ERR_PARTITION "$1,$2" || return $?
644            ;;
645    *)  ogRaiseError $OG_ERR_PARTITION "$1,$2"
646            return $? ;;
647esac
648#/// Mostrar nuevo tamaño en KB.
649echo $[SIZE*1024]
650}
651
652
653#/**
654#         ogUnlock int_ndisk int_npartition
655#@see     ogUnlockPartition
656#*/
657function ogUnlock ()
658{
659ogUnlockPartition "$@"
660}
661
662#/**
663#         ogUnlockPartition int_ndisk int_npartition
664#@brief   Elimina el fichero de bloqueo para una particion.
665#@param   int_ndisk      nº de orden del disco
666#@param   int_npartition nº de orden de la partición
667#@return  (nada)
668#@exception OG_ERR_FORMAT    Formato incorrecto.
669#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
670#@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 "-".
671#@version 0.9 - Primera versión para OpenGNSys.
672#@author  Ramon Gomez, ETSII Universidad de Sevilla
673#@date    2009-09-03
674#*/ ##
675function ogUnlockPartition ()
676{
677# Variables locales
678local PART LOCKFILE
679
680# Si se solicita, mostrar ayuda.
681if [ "$*" == "help" ]; then
682    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
683           "$FUNCNAME 1 1"
684    return
685fi
686# Error si no se reciben 2 parámetros.
687[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
688
689# Obtener partición.
690PART="$(ogDiskToDev $1 $2)" || return $?
691
692# Borrar archivo de bloqueo exclusivo.
693LOCKFILE="/var/lock/lock${PART//\//-}"
694rm -f $LOCKFILE
695}
696
697
698#/**
699#         ogUnmount int_ndisk int_npartition
700#@see     ogUnmountFs
701#*/
702function ogUnmount ()
703{
704ogUnmountFs "$@"
705}
706
707#/**
708#         ogUnmountFs int_ndisk int_npartition
709#@brief   Desmonta un sistema de archivos.
710#@param   int_ndisk      nº de orden del disco
711#@param   int_npartition nº de orden de la partición
712#@return  Nada
713#@exception OG_ERR_FORMAT    Formato incorrecto.
714#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
715#@warning La partición no está previamente montada o no se puede desmontar.
716#@version 0.9 - Primera versión para OpenGNSys.
717#@author  Ramon Gomez, ETSII Universidad de Sevilla
718#@date    2009-09-28
719#*/ ##
720function ogUnmountFs ()
721{
722# Variables locales
723local PART MNTDIR
724
725# Si se solicita, mostrar ayuda.
726if [ "$*" == "help" ]; then
727    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" "$FUNCNAME 1 1"
728    return
729fi
730# Error si no se reciben 2 parámetros.
731[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
732
733# Obtener partición y punto de montaje.
734PART="$(ogDiskToDev $1 $2)" || return $?
735MNTDIR="$(ogGetMountPoint $1 $2)"
736
737# Si está montada, desmontarla.
738if [ -n "$MNTDIR" ]; then
739    # Error si la particion está bloqueada.
740    ogIsLocked $1 $2 && ogRaiseError $OG_ERR_LOCKED "$1 $2" && return $?
741    # Desmontar y borrar punto de montaje.
742    umount $PART 2>/dev/null || ogEcho warning "$FUNCNAME: $MSG_DONTUNMOUNT: \"$1,$2\""
743    rmdir $MNTDIR 2>/dev/null || rm -f $MNTDIR 2>/dev/null
744        # linea temporal durante desarrollo para testear nuevas herramientas con el cliente completo nfs
745    if grep -q nfsroot /proc/cmdline; then
746                cat /etc/mtab | grep -v $PART > /var/tmp/mtab.temporal && cp /var/tmp/mtab.temporal /var/tmp/mtab && rm /var/tmp/mtab.temporal
747        fi
748        # fin linea temporal.
749else
750    ogEcho warning "$MSG_DONTMOUNT: \"$1,$2\""
751fi
752}
753
754
755#/**
756#         ogUnmountAll int_ndisk
757#@brief   Desmonta todos los sistema de archivos de un disco, excepto el caché local.
758#@param   int_ndisk      nº de orden del disco
759#@return  Nada
760#@exception OG_ERR_FORMAT    Formato incorrecto.
761#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
762#@warning No se desmonta la partición marcada como caché local.
763#@version 0.9 - Versión para OpenGNSys.
764#@author  Ramon Gomez, ETSII Universidad de Sevilla
765#@date    2009/10/07
766#*/ ##
767function ogUnmountAll ()
768{
769# Variables locales
770local DISK PART
771# Si se solicita, mostrar ayuda.
772if [ "$*" == "help" ]; then
773    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk" "FUNCNAME 1"
774    return
775fi
776# Error si no se recibe 1 parámetro.
777[ $# == 1 ] || ogRaiseError $OG_ERR_FORMAT || return $?
778
779# Obtener partición y punto de montaje.
780DISK="$(ogDiskToDev $1)" || return $?
781for ((PART=1; PART<=$(ogGetPartitionsNumber $1); PART++)); do
782    case "$(ogGetFsType $1 $PART)" in
783        CACHE|LINUX-SWAP)
784           ;;
785        *)
786           ogUnmount $1 $PART ;;
787    esac
788done
789}
790
791
792function ogGetFsSize () {
793#/**  @function ogGetFsSize: @brief Muestra el tamanio del sistema de archivos indicado, permite definir la unidad de medida, por defecto GB
794#@param  $1 int_diskEAC
795#@param $2 int_PartitionEAC
796#@param $3 str_UnidadMediada            parametro opcional, admite [ kB MB GB -default GB]
797#@return  cadena con int_TotalSize:int_DataSize:int_DataFree
798#@warning  Salidas de errores no determinada
799#@warning
800#@attention
801#@version 1.0   Date: 27/10/2008 Author Antonio J. Doblas Viso. Universidad de Malaga
802#*/
803if [ $# = 0 ]
804then
805        echo "sintaxis: ogGetFsSize int_disco int_partition str_SizeOutput [ kB MB GB -default GB]-]" red
806        echo "devuelve int_size : int_data : int_free" red
807return
808fi
809if [ $# -ge 2 ]
810then
811        particion=`ogMount $1 $2 ` #1>/dev/null 2>&1
812        if [ -z $3 ]
813                then
814                        unit=GB  # s B kB MB GB TB %
815                else
816                        unit=$3
817        fi
818        case $unit in
819                kB)
820                        factor="1.024";
821                        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}'`
822                ;;
823                MB)
824                        factor="1.024/1000";
825                        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}'`
826                ;;
827                GB)
828                        factor="1.024/1000000";
829                        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}'`
830                ;;
831        esac
832        #echo $valor
833        #NumberRound $valor
834        #valor=`NumberRound $valor`;
835        ogUnmount $1 $2 1>/dev/null 2>&1
836        echo $valor
837
838fi
839
840}
Note: See TracBrowser for help on using the repository browser.