source: client/engine/FileSystem.lib @ 3458879

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 3458879 was 3458879, checked in by ramon <ramongomez@…>, 16 years ago

Correcciones en varias funciones; función en pruebas ogGetIpAddress.

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