source: client/engine/FileSystem.lib @ 62e3bcf

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 62e3bcf was 42669ebf, checked in by ramon <ramongomez@…>, 15 years ago

Funciones adaptadas a plantilla Doxygen; nueva función ogFsToId.

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

  • Property mode set to 100755
File size: 28.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
140#@see     ogFormatFs
141#*/
142function ogFormat ()
143{
144ogFormatFs "$@"
145}
146
147
148#/**
149#         ogFoarmatFs int_ndisk int_npartition [type_fstype] [str_label]
150#@brief   Formatea un sistema de ficheros según el tipo de su partición.
151#@param   int_ndisk      nº de orden del disco
152#@param   int_npartition nº de orden de la partición
153#@param   type_fstype    mnemónico de sistema de ficheros a formatear
154#@param   str_label      etiqueta de volumen (opcional)
155#@return  (por determinar)
156#@exception OG_ERR_FORMAT    Formato incorrecto.
157#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
158#@exception OG_ERR_PARTITION Partición no accesible o desconocida.
159#@note    Requisitos:   mkfs*
160#@warning No formatea particiones montadas ni bloqueadas.
161#@todo    Definir salidas.
162#@version 0.9 - Primera versión para OpenGNSys.
163#@author  Ramon Gomez, ETSII Universidad de Sevilla
164#@date    2009-10-08
165#*/ ##
166function ogFormatFs ()
167{
168# Variables locales
169local PART ID TYPE LABEL PROG PARAMS ERRCODE
170
171# Si se solicita, mostrar ayuda.
172if [ "$*" == "help" ]; then
173    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition [str_label]" \
174           "$FUNCNAME 1 1" \
175           "$FUNCNAME 1 1 EXT4" \
176           "$FUNCNAME 1 1 \"DATA\"" \
177           "$FUNCNAME 1 1 EXT4 \"DATA\""
178    return
179fi
180# Error si no se reciben entre 2 y 4 parámetros.
181[ $# -ge 2 -a $# -le 4 ] || ogRaiseError $OG_ERR_FORMAT || return $?
182# Obtener dispositivo y tipo de sisitema de archivos.
183PART="$(ogDiskToDev $1 $2)" || return $?
184TYPE="$(ogGetFsType $1 $2)" || return $?
185
186# Elegir tipo de formato segun el tipo de particion.
187case "$3" in
188    EXT2)      ID=83; PROG="mkfs.ext2";;
189    EXT3)      ID=83; PROG="mkfs.ext3";;
190    EXT4)      ID=83; PROG="mkfs.ext4";;
191    REISERFS)  ID=83; PROG="mkfs.reiserfs"; PARAMS="-f" ;;
192    REISER4)   ID=83; PROG="mkfs.reiser4";;
193    XFS)       ID=83; PROG="mkfs.xfs"; PARAMS="-f" ;;
194    JFS)       ID=83; PROG="mkfs.jfs"; PARAMS="<<<\"y\"";;
195    NTFS)      ID=7;  PROG="mkntfs"; PARAMS="-f" ;;
196    HNTFS)     ID=17; PROG="mkntfs"; PARAMS="-f" ;;
197    FAT32)     ID=b;  PROG="mkdosfs"; PARAMS="-F 32" ;;
198    HFAT32)    ID=1b; PROG="mkdosfs"; PARAMS="-F 32" ;;
199    FAT16)     ID=6;  PROG="mkdosfs"; PARAMS="-F 16" ;;
200    HFAT16)    ID=16; PROG="mkdosfs"; PARAMS="-F 16" ;;
201    FAT12)     ID=1;  PROG="mkdosfs"; PARAMS="-F 12" ;;
202    HFAT12)    ID=11; PROG="mkdosfs"; PARAMS="-F 12" ;;
203    *)         LABEL="$3" ;;
204esac
205# Si no se indica explícitamente, detectar el tipo de sistema de archivos.
206if [ -z "$PROG" ]; then
207    case "$TYPE" in
208        EXT2)         PROG="mkfs.ext2";;
209        EXT3)         PROG="mkfs.ext3";;
210        EXT4)         PROG="mkfs.ext4";;
211        REISERFS)     PROG="mkfs.reiserfs"; PARAMS="-f" ;;
212        REISER4)      PROG="mkfs.reiser4";;
213        XFS)          PROG="mkfs.xfs"; PARAMS="-f" ;;
214        JFS)          PROG="mkfs.jfs"; PARAMS="<<<\"y\"";;
215        NTFS|HNTFS)   PROG="mkntfs"; PARAMS="-f" ;;
216        FAT32|HFAT32) PROG="mkdosfs"; PARAMS="-F 32" ;;
217        FAT16|HFAT16) PROG="mkdosfs"; PARAMS="-F 16" ;;
218        FAT12|HFAT12) PROG="mkdosfs"; PARAMS="-F 12" ;;
219        *)            ogRaiseError $OG_ERR_PARTITION "$1 $2 $TYPE"
220                      return $? ;;
221    esac
222else
223    [ $TYPE == "$3" -o $ID == "$(ogGetPartitionId $1 $2)" ] || ogRaiseError $OG_ERR_PARTITION "$3 != $TYPE" || return $?
224fi
225# Comprobar consistencia entre id. de partición y tipo de sistema de archivos.
226[ -n "$PROG" ] || ogRaiseError $OG_ERR_PARTITION "$3 != $TYPE" || return $?
227
228# Etiquetas de particion.
229if [ -z "$LABEL" ]; then
230    [ "$4" != "CACHE" ] || ogRaiseError $OG_ERR_FORMAT "$MSG_RESERVEDVALUE: CACHE" || return $?
231    [ -n "$4" ] && PARAMS="$PARAMS -L $4"
232else
233    PARAMS="$PARAMS -L $LABEL"
234fi
235
236# Error si la particion esta montada o está bloqueada.
237if ogIsMounted $1 $2; then
238    ogRaiseError $OG_ERR_PARTITION "$1 $2"       # Indicar nuevo error
239    return $?
240fi
241if ogIsLocked $1 $2; then
242    ogRaiseError $OG_ERR_LOCKED "$1 $2"
243    return $?
244fi
245# Formatear en modo uso exclusivo.
246ogLock $1 $2
247eval $PROG $PARAMS $PART 2>/dev/null
248ERRCODE=$?
249case $ERRCODE in
250    0)    ;;
251    127)  ogRaiseError $OG_ERR_NOTEXEC "$PROG" ;;
252    *)    ogRaiseError $OG_ERR_PARTITION "$1 $2" ;;
253esac
254ogUnlock $1 $2
255return $ERRCODE
256}
257
258
259#/**
260#         ogGetFsType int_ndisk int_npartition
261#@brief   Devuelve el mnemonico con el tipo de sistema de archivos.
262#@param   int_ndisk      nº de orden del disco
263#@param   int_npartition nº de orden de la partición
264#@return  Mnemonico
265#@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 }
266#@exception OG_ERR_FORMAT   Formato incorrecto.
267#@exception OG_ERR_NOTFOUND Disco o particion no corresponden con un dispositivo.
268#@version 0.9 - Primera adaptación para OpenGNSys.
269#@author  Ramon Gomez, ETSII Universidad de Sevilla
270#@date    2009-07-21
271#*/ ##
272function ogGetFsType () {
273
274# Variables locales.
275local ID TYPE
276
277# Si se solicita, mostrar ayuda.
278if [ "$*" == "help" ]; then
279    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
280           "$FUNCNAME 1 1  =>  NTFS"
281    return
282fi
283# Error si no se reciben 2 parámetros.
284[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
285
286# Detectar id. de tipo de partición y codificar al mnemonico.
287DISK=$(ogDiskToDev "$1") || return $?
288ID=$(ogGetPartitionId "$1" "$2") || return $?
289case "$ID" in
290     0)         TYPE="EMPTY" ;;
291     1)         TYPE="FAT12" ;;
292     5|f)       TYPE="EXTENDED" ;;
293     [6e])      TYPE="FAT16" ;;
294     7)         TYPE="NTFS" ;;          # Nota: también puede ser EXFAT
295     [bc])      TYPE="FAT32" ;;
296     11)        TYPE="HFAT12" ;;
297     12)        TYPE="COMPAQDIAG" ;;
298     1[6e])     TYPE="HFAT16" ;;
299     17)        TYPE="HNTFS" ;;
300     1[bc])     TYPE="HFAT32" ;;
301     42)        TYPE="WIN-DYNAMIC" ;;
302     82)        TYPE="LINUX-SWAP" ;;
303     83)        TYPE="$(parted -s $DISK print | awk -v var=$2 '{if ($1==var) {print toupper($6)}}')"
304                TYPE=${TYPE:-"EXT3"}
305                ;;
306     8e)        TYPE="LINUX-LVM" ;;
307     a7)        TYPE="CACHE" ;;         # (compatibilidad con Brutalix)
308     bf)        TYPE="SOLARIS" ;;
309     ca)        TYPE="CACHE" ;;
310     fd)        TYPE="LINUX-RAID" ;;
311     *)         TYPE="UNKNOWN" ;;
312esac
313echo $TYPE
314}
315
316
317#/**
318#         ogGetMountPoint int_ndisk int_npartition
319#@brief   Devuelve el punto de montaje de un sistema de archivos.
320#@param   int_ndisk      nº de orden del disco
321#@param   int_npartition nº de orden de la partición
322#@return  Punto de montaje
323#@exception OG_ERR_FORMAT    Formato incorrecto.
324#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
325#@note    Requisitos: \c mount* \c awk
326#@version 0.9 - Primera versión para OpenGNSys.
327#@author  Ramon Gomez, ETSII Universidad de Sevilla
328#@date    2009-10-15
329#*/ ##
330function ogGetMountPoint ()
331{
332# Variables locales
333local PART
334# Si se solicita, mostrar ayuda.
335if [ "$*" == "help" ]; then
336    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
337           "$FUNCNAME 1 1  =>  /mnt/sda1"
338    return
339fi
340# Error si no se reciben 2 parámetros.
341[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
342# Obtener partición.
343PART="$(ogDiskToDev $1 $2)" || return $?
344
345echo $(mount | awk -v P=$PART '{if ($1==P) {print $3}}')
346}
347
348
349#/**
350#         ogIsMounted int_ndisk int_npartition
351#@brief   Comprueba si un sistema de archivos está montado.
352#@param   int_ndisk      nº de orden del disco
353#@param   int_npartition nº de orden de la partición
354#@return  Código de salida: 0 - sin montar, 1 - montado.
355#@version 0.9 - Primera versión para OpenGNSys.
356#@author  Ramon Gomez, ETSII Universidad de Sevilla
357#@date    2009-10-15
358#*/ ##
359function ogIsMounted ()
360{
361# Si se solicita, mostrar ayuda.
362if [ "$*" == "help" ]; then
363    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
364           "if $FUNCNAME 1 1; then ... ; fi"
365    return
366fi
367# Error si no se reciben 2 parámetros.
368[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
369
370test -n "$(ogGetMountPoint $1 $2)"
371}
372
373
374#/**
375#         ogIsLocked int_ndisk int_npartition
376#@brief   Comprueba si una partición está bloqueada por una operación de uso exclusivo.
377#@param   int_ndisk      nº de orden del disco
378#@param   int_npartition nº de orden de la partición
379#@return  Código de salida: 0 - sin bloquear, 1 - bloqueada.
380#@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 "-".
381#@version 0.9 - Primera versión para OpenGNSys.
382#@author  Ramon Gomez, ETSII Universidad de Sevilla
383#@date    2009-09-03
384#*/ ##
385function ogIsLocked ()
386{
387# Variables locales
388local PART LOCKFILE
389
390# Si se solicita, mostrar ayuda.
391if [ "$*" == "help" ]; then
392    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
393           "if $FUNCNAME 1 1; then ... ; fi"
394    return
395fi
396# Error si no se reciben 2 parámetros.
397[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
398
399# Obtener partición.
400PART="$(ogDiskToDev $1 $2)" || return $?
401
402# Comprobar existencia del fichero de bloqueo.
403LOCKFILE="/var/lock/lock${PART//\//-}"
404test -f $LOCKFILE
405}
406
407
408#/**
409#         ogLock int_ndisk int_npartition
410#@see     ogLockPartition
411#*/
412function ogLock ()
413{
414ogLockPartition "$@"
415}
416
417#/**
418#         ogLockPartition int_ndisk int_npartition
419#@brief   Genera un fichero de bloqueo para una partición en uso exlusivo.
420#@param   int_ndisk      nº de orden del disco
421#@param   int_npartition nº de orden de la partición
422#@return  (nada)
423#@exception OG_ERR_FORMAT    Formato incorrecto.
424#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
425#@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 "-".
426#@version 0.9 - Primera versión para OpenGNSys.
427#@author  Ramon Gomez, ETSII Universidad de Sevilla
428#@date    2009-09-03
429#*/ ##
430function ogLockPartition ()
431{
432# Variables locales
433local PART LOCKFILE
434
435# Si se solicita, mostrar ayuda.
436if [ "$*" == "help" ]; then
437    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
438           "$FUNCNAME 1 1"
439    return
440fi
441# Error si no se reciben 2 parámetros.
442[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
443
444# Obtener partición.
445PART="$(ogDiskToDev $1 $2)" || return $?
446
447# Crear archivo de bloqueo exclusivo.
448LOCKFILE="/var/lock/lock${PART//\//-}"
449touch $LOCKFILE
450}
451
452
453#/**
454#         ogMount int_ndisk int_npartition
455#@see     ogMountFs ogMountCache ogMountCdrom
456#*/ ##
457function ogMount ()
458{
459case "$*" in
460    CACHE|cache)
461        ogMountCache ;;
462    CDROM|cdrom)
463        ogMountCdrom ;;
464    *)  ogMountFs "$@" ;;
465esac
466}
467
468
469#/**
470#         ogMountFs int_ndisk int_npartition
471#@brief   Monta un sistema de archivos.
472#@param   int_ndisk      nº de orden del disco
473#@param   int_npartition nº de orden de la partición
474#@return  Punto de montaje
475#@exception OG_ERR_FORMAT    Formato incorrecto.
476#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
477#@exception OG_ERR_PARTITION Tipo de particion desconocido o no se puede montar.
478#@version 0.9 - Primera versión para OpenGNSys.
479#@author  Ramon Gomez, ETSII Universidad de Sevilla
480#@date    2009-09-28
481#*/ ##
482function ogMountFs ()
483{
484# Variables locales
485local PART TYPE MNTDIR MOUNT PARAMS
486
487# Si se solicita, mostrar ayuda.
488if [ "$*" == "help" ]; then
489    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
490           "$FUNCNAME 1 1  =>  /mnt/sda1"
491    return
492fi
493# Error si no se reciben 2 parámetros.
494[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
495
496# Obtener partición.
497PART="$(ogDiskToDev $1 $2)" || return $?
498
499# Comprobar si el sistema de archivos ya está montada.
500MNTDIR="$(ogGetMountPoint $1 $2)"
501# Si no, montarla en un directorio de sistema
502if [ -z "$MNTDIR" ]; then
503    # Error si la particion esta bloqueada.
504    ogIsLocked $1 $2 && ogRaiseError $OG_ERR_LOCKED "$1 $2" && return $?
505    # Crear punto de montaje.
506    MNTDIR=${PART/dev/mnt}
507    mkdir -p $MNTDIR
508    # Montar según el tipo de sitema de archivos.
509    TYPE="$(ogGetFsType $1 $2)" || return $?
510    case "$TYPE" in
511        CACHE)      MOUNT=mount ;;
512        EXT[234])   MOUNT=mount ;;
513        REISERFS)   insmod /lib/modules/$(uname -r)/kernel/fs/reiserfs/reiserfs.ko 2>/dev/null
514                    MOUNT=mount ;;
515        JFS)        insmod /lib/modules/$(uname -r)/kernel/fs/jfs/jfs.ko 2>/dev/null
516                    MOUNT=mount ;;
517        XFS)        insmod /lib/modules/$(uname -r)/kernel/fs/xfs/xfs.ko 2>/dev/null
518                    MOUNT=mount ;;
519        NTFS|HNTFS) MOUNT=ntfs-3g ;;
520        FAT16|FAT32|HFAT16|HFAT32)
521                    MOUNT=mount; PARAMS="-t vfat" ;;
522        *)  #/// Error, si la partición no es montable.
523            rmdir $MNTDIR
524            ogRaiseError $OG_ERR_PARTITION "$1, $2, $TYPE"
525            return $OG_ERR_PARTITION
526            ;;
527    esac
528    $MOUNT $PARAMS $PART $MNTDIR || $MOUNT $PARAMS $PART $MNTDIR -o force,remove_hiberfile || ogRaiseError $OG_ERR_PARTITION "$1, $2, $TYPE" || return $?
529        # linea temporal durante desarrollo para poder usar el cliente completo nfs y testeas nuevas herramientas.
530    if grep -q nfsroot /proc/cmdline; then
531                echo "$PART $MNTDIR" >> /etc/mtab
532        fi
533        # fin linea temporal.
534fi
535echo $MNTDIR
536}
537
538
539#####  PRUEBAS
540# Montar CDROM
541function ogMountCdrom ()
542{
543local DEV MNTDIR
544DEV="/dev/cdrom"            # Por defecto
545MNTDIR=$(mount | awk -v D=$DEV '{if ($1==D) {print $3}}')
546if [ -z "$MNTDIR" ]; then
547    MNTDIR=${DEV/dev/mnt}
548    mkdir -p $MNTDIR
549    mount -t iso9660 $DEV $MNTDIR || ogRaiseError $OG_ERR_PARTITION "cdrom" || return $?
550fi
551echo $MNTDIR
552}
553
554
555#/**
556#         ogReduceFs int_ndisk int_npartition
557#@brief   Reduce el tamaño del sistema de archivos, sin tener en cuenta el espacio libre.
558#@param   int_ndisk      nº de orden del disco
559#@param   int_npartition nº de orden de la partición
560#@return  tamañoKB
561#@exception OG_ERR_FORMAT    Formato incorrecto.
562#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
563#@exception OG_ERR_PARTITION Partición desconocida o no accesible.
564#@warning En Windows, se borran los ficheros pagefile.sys e hiberfile.sys
565#@warning El sistema de archivos se amplía al mínimo + 1 KB.
566#@note    Requisitos:   *resize*
567#@version 0.9 - Primera versión para OpenGNSys.
568#@author  Ramon Gomez, ETSII Universidad de Sevilla
569#@date    2009-09-23
570#*/ ##
571function ogReduceFs ()
572{
573# Variables locales
574local PART BLKS SIZE
575
576# Si se solicita, mostrar ayuda.
577if [ "$*" == "help" ]; then
578    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
579           "$FUNCNAME 1 1"
580    return
581fi
582# Error si no se reciben 2 parámetros.
583[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
584
585# Obtener partición.
586PART="$(ogDiskToDev $1 $2)" || return $?
587
588# Redimensionar según el tipo de particion.
589case "$(ogGetFsType $1 $2)" in
590    EXT[234])
591        ogUnmount $1 $2 2>/dev/null
592        # Ext2/3/4: Tamaño de los bloques del sistema de archivos
593        BLKS=$(tune2fs -l $PART | awk '/Block size/ {print int($3/512)}')
594        # Traduce el num. en sectores de 512B a tamano en MB.
595        SIZE=$(resize2fs -P $PART 2>/dev/null | \
596                       awk -v B=$BLKS '/minimum size/ {print int($7*B/2048+1000)}')
597        resize2fs -fp $PART "${SIZE}M" &>/dev/null || ogRaiseError $OG_ERR_PARTITION "$1,$2" || return $?
598            ;;
599#    REISERFS)          # Usar "resize_reiserfs"
600#           ;;
601    NTFS|HNTFS)
602        ogDeleteFile $1 $2 pagefile.sys
603        ogDeleteFile $1 $2 hiberfile.sys
604        ogUnmount $1 $2 2>/dev/null
605        # NTFS: Obtiene tamaño mínimo en MB.
606        SIZE=$(ntfsresize -fi $PART | awk '/resize at/ {print $8+1000}')
607        ntfsresize -fns "${SIZE}M" $PART >/dev/null || ogRaiseError $OG_ERR_PARTITION "$1,$2" || return $?
608        ntfsresize -fs "${SIZE}M" $PART <<<"y" >/dev/null || ogRaiseError $OG_ERR_PARTITION "$1,$2" || return $?
609            ;;
610    *)  ogRaiseError $OG_ERR_PARTITION "$1,$2"
611            return $? ;;
612esac
613#/// Mostrar nuevo tamaño en KB.
614echo $[SIZE*1024]
615}
616
617
618#/**
619#         ogUnlock int_ndisk int_npartition
620#@see     ogUnlockPartition
621#*/
622function ogUnlock ()
623{
624ogUnlockPartition "$@"
625}
626
627#/**
628#         ogUnlockPartition int_ndisk int_npartition
629#@brief   Elimina el fichero de bloqueo para una particion.
630#@param   int_ndisk      nº de orden del disco
631#@param   int_npartition nº de orden de la partición
632#@return  (nada)
633#@exception OG_ERR_FORMAT    Formato incorrecto.
634#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
635#@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 "-".
636#@version 0.9 - Primera versión para OpenGNSys.
637#@author  Ramon Gomez, ETSII Universidad de Sevilla
638#@date    2009-09-03
639#*/ ##
640function ogUnlockPartition ()
641{
642# Variables locales
643local PART LOCKFILE
644
645# Si se solicita, mostrar ayuda.
646if [ "$*" == "help" ]; then
647    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
648           "$FUNCNAME 1 1"
649    return
650fi
651# Error si no se reciben 2 parámetros.
652[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
653
654# Obtener partición.
655PART="$(ogDiskToDev $1 $2)" || return $?
656
657# Borrar archivo de bloqueo exclusivo.
658LOCKFILE="/var/lock/lock${PART//\//-}"
659rm -f $LOCKFILE
660}
661
662
663#/**
664#         ogUnmount int_ndisk int_npartition
665#@see     ogUnmountFs
666#*/
667function ogUnmount ()
668{
669ogUnmountFs "$@"
670}
671
672#/**
673#         ogUnmountFs int_ndisk int_npartition
674#@brief   Desmonta un sistema de archivos.
675#@param   int_ndisk      nº de orden del disco
676#@param   int_npartition nº de orden de la partición
677#@return  Nada
678#@exception OG_ERR_FORMAT    Formato incorrecto.
679#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
680#@warning La partición no está previamente montada o no se puede desmontar.
681#@version 0.9 - Primera versión para OpenGNSys.
682#@author  Ramon Gomez, ETSII Universidad de Sevilla
683#@date    2009-09-28
684#*/ ##
685function ogUnmountFs ()
686{
687# Variables locales
688local PART MNTDIR
689
690# Si se solicita, mostrar ayuda.
691if [ "$*" == "help" ]; then
692    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" "$FUNCNAME 1 1"
693    return
694fi
695# Error si no se reciben 2 parámetros.
696[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
697
698# Obtener partición y punto de montaje.
699PART="$(ogDiskToDev $1 $2)" || return $?
700MNTDIR="$(ogGetMountPoint $1 $2)"
701
702# Si está montada, desmontarla.
703if [ -n "$MNTDIR" ]; then
704    # Error si la particion esta bloqueada.
705    ogIsLocked $1 $2 && ogRaiseError $OG_ERR_LOCKED "$1 $2" && return $?
706    # Crear punto de montaje.
707    umount $PART 2>/dev/null && rmdir $MNTDIR || ogEcho warning "$FUNCNAME: $MSG_DONTUNMOUNT: \"$1,$2\""
708        # linea temporal durante desarrollo para testear nuevas herramientas con el cliente completo nfs
709    if grep -q nfsroot /proc/cmdline; then
710                cat /etc/mtab | grep -v $PART > /var/tmp/mtab.temporal && cp /var/tmp/mtab.temporal /var/tmp/mtab && rm /var/tmp/mtab.temporal
711        fi
712        # fin linea temporal.
713else
714    ogEcho warning "$MSG_DONTMOUNT: \"$1,$2\""
715fi
716}
717
718
719#/**
720#         ogUnmountAll int_ndisk
721#@brief   Desmonta todos los sistema de archivos de un disco, excepto el caché local.
722#@param   int_ndisk      nº de orden del disco
723#@return  Nada
724#@exception OG_ERR_FORMAT    Formato incorrecto.
725#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
726#@warning No se desmonta la partición marcada como caché local.
727#@version 0.9 - Versión para OpenGNSys.
728#@author  Ramon Gomez, ETSII Universidad de Sevilla
729#@date    2009/10/07
730#*/ ##
731function ogUnmountAll ()
732{
733# Variables locales
734local DISK PART
735# Si se solicita, mostrar ayuda.
736if [ "$*" == "help" ]; then
737    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk" "FUNCNAME 1"
738    return
739fi
740# Error si no se recibe 1 parámetro.
741[ $# == 1 ] || ogRaiseError $OG_ERR_FORMAT || return $?
742
743# Obtener partición y punto de montaje.
744DISK="$(ogDiskToDev $1)" || return $?
745for ((PART=1; PART<=$(ogGetPartitionsNumber $1); PART++)); do
746    case "$(ogGetFsType $1 $PART)" in
747        CACHE|LINUX-SWAP)
748           ;;
749        *)
750           ogUnmount $1 $PART ;;
751    esac
752done
753}
754
755
756function ogFindCache () {
757#/**  @function ogFindCache: @brief Detecta la particion CACHE EAC seg�n tipo a7 o label CACHE sobre particion ext3.
758#@param  no requiere parametros
759#@return  devuelve el identificador linux de la particion CACHE.
760#@warning  si no hay cache no devuelve nada
761#@attention Requisitos: la salida de fdisk de la cache a7 debe ser NeXTSTEP
762#@note    Notas sin especificar
763#@version 0.1       Date: 27/10/2008                 Author Antonio J. Doblas Viso. Universidad de Malaga
764#*/
765#
766if [ $# != 0 ]
767then
768        Msg "FindCache detecta la particion tipo a7 o aquella que tenga el label CACHE" info
769        Msg "devuelve /dev/sda3" info2
770        Msg "sintaxis: FindCache   --- Ejemplo: FindCache -> /dev/sda3/ " example
771        return
772fi
773end=`ogDiskToDev | wc -w`
774unset cache
775for (( disk = 1; disk <= $end; disk++))
776do
777        disco=`ogDiskToDev $disk`
778        totalpart=`parted $disco print | egrep ^" [0123456789] " -c`
779        #echo Buscando en disco: $disco con $totalpart particiones
780        if [ `fdisk -l $disco | egrep ^/dev | grep NeXTSTEP | cut -f1 -d" "` ]
781        then
782        #       echo comprobando si es particion a7
783                cache=`fdisk -l $disco | egrep ^/dev | grep NeXTSTEP | cut -f1 -d" "`
784        elif [  `fdisk -l $disco | egrep ^/dev | grep ca | cut -f1 -d" "` ]
785        then
786        #       echo comprobando si es particion ca
787                cache=`fdisk -l $disco | egrep ^/dev | grep ca | cut -f1 -d" "`
788        else
789                #echo comprobando si existe la etiqueta cache
790                if find /dev/disk | grep CACHE > /dev/null
791                then
792                        devcache=`ls -ls /dev/disk/by-label/CACHE | awk -F..\/..\/ '{print $2}'`
793                        cache=/dev/$devcache
794                fi
795        fi
796done
797if [ -n "$cache" ]
798then
799        echo $cache
800else
801        return 1
802fi
803}
804
805function ogMountCache () {
806#/**  @function ogMountCache: @brief Monta la particion Cache y exporta la variable $OGCAC
807#@param  sin parametros
808#@return Punto de montaje dentro de mnt, ejemplo con un parametro: ogMountCache => /mnt/sda3
809#@warning  Salidas de errores no determinada
810#@warning
811#@attention
812#@version 0.1   Date: 27/10/2008 Author Antonio J. Doblas Viso. Universidad de Malaga. Proyecto EAC
813#@note
814#*/
815CACHELINUX=`ogFindCache`
816CACHEOG=`ogDevToDisk $CACHELINUX`
817OGCAC=`ogMount $CACHEOG`
818echo $OGCAC
819export OGCAC
820}
821
822function ogUnmountCache () {
823#/**  @function UmountCache: @brief Desmonta la particion Cache y elimina la variable $OGCAC
824#@param  sin parametros
825#@return nada
826#@warning  Salidas de errores no determinada
827#@warning
828#@attention
829#@version 1.0   Date: 27/10/2008 Author Antonio J. Doblas Viso. Universidad de Malaga. Proyecto EAC
830#@note
831#*/
832CACHELINUX=`ogFindCache`
833CACHEOG=`ogDevToDisk $CACHELINUX`
834#echo ogUnmountPartition $cacheeac
835ogUnmountFs $CACHEOG
836unset OGCAC
837}
838
839
840function ogFormatCache () {
841#/**  @function ogFormatCache: @brief Formatea la Cache EAC y le asigna el label CACHE
842#@param  No requiere
843#@return   la propia del mkfs.ext3
844#@warning
845#@attention
846#@note
847#@version 0.1       Date: 27/10/2008                 Author Antonio J. Doblas Viso. Universidad de Malaga
848#*/
849
850if [ $# = 0 ]
851then
852        cd /
853        ogUnmountCache
854        dev=`ogFindCache`
855        exhe "Iniciando el formateo de la particion CACHE, ubicada en $dev, y asignandole el label CACHE"
856        mkfs.ext3 $dev -L CACHE
857        ogInfoCache
858fi
859if [ $# = 1 ]
860then
861        mkfs.ext3 $1 -L CACHE
862fi
863}
864
865function ogGetFsSize () {
866#/**  @function ogGetFsSize: @brief Muestra el tamanio del sistema de archivos indicado, permite definir la unidad de medida, por defecto GB
867#@param  $1 int_diskEAC
868#@param $2 int_PartitionEAC
869#@param $3 str_UnidadMediada            parametro opcional, admite [ kB MB GB -default GB]
870#@return  cadena con int_TotalSize:int_DataSize:int_DataFree
871#@warning  Salidas de errores no determinada
872#@warning
873#@attention
874#@version 1.0   Date: 27/10/2008 Author Antonio J. Doblas Viso. Universidad de Malaga
875#*/
876if [ $# = 0 ]
877then
878        echo "sintaxis: ogGetFsSize int_disco int_partition str_SizeOutput [ kB MB GB -default GB]-]" red
879        echo "devuelve int_size : int_data : int_free" red
880return
881fi
882if [ $# -ge 2 ]
883then
884        particion=`ogMount $1 $2 ` #1>/dev/null 2>&1
885        if [ -z $3 ]
886                then
887                        unit=GB  # s B kB MB GB TB %
888                else
889                        unit=$3
890        fi
891        case $unit in
892                kB)
893                        factor="1.024";
894                        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}'`
895                ;;
896                MB)
897                        factor="1.024/1000";
898                        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}'`
899                ;;
900                GB)
901                        factor="1.024/1000000";
902                        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}'`
903                ;;
904        esac
905        #echo $valor
906        #NumberRound $valor
907        #valor=`NumberRound $valor`;
908        ogUnmount $1 $2 1>/dev/null 2>&1
909        echo $valor
910
911fi
912
913}
Note: See TracBrowser for help on using the repository browser.