source: client/engine/FileSystem.lib @ 1c04494

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

Correcciones en scripts; metafunción ogGetOsType.

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

  • Property mode set to 100755
File size: 26.1 KB
Line 
1#!/bin/bash
2#/**
3#@file    FileSystem.lib
4#@brief   Librería o clase FileSystem
5#@class   FileSystem
6#@brief   Funciones para gestión de sistemas de archivos.
7#@version 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
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 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 ndisk      nº de orden del disco
258#@arg  \c npartition nº de orden de la partición
259#@return  Mnemonico
260#@note    Mnemonico: { EXT2, EXT3, EXT4, REISERFS, LINUX-SWAP, LINUX-LVM, LINUX-RAID, 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 DISK 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.
282DISK=$(ogDiskToDev $1) || return $?
283ID=$(ogGetPartitionId $1 $2) || return $?
284case "$ID" in
285     0)         TYPE="EMPTY" ;;
286     1)         TYPE="FAT12" ;;
287     5|f)       TYPE="EXTENDED" ;;
288     [6e])      TYPE="FAT16" ;;
289     7)         TYPE="NTFS" ;;          # Nota: también puede ser EXFAT
290     [bc])      TYPE="FAT32" ;;
291     11)        TYPE="HFAT12" ;;
292     12)        TYPE="COMPAQDIAG" ;;
293     1[6e])     TYPE="HFAT16" ;;
294     17)        TYPE="HNTFS" ;;
295     1[bc])     TYPE="HFAT32" ;;
296     42)        TYPE="WIN-DYNAMIC" ;;
297     82)        TYPE="LINUX-SWAP" ;;
298     83)        TYPE="$(parted -s $DISK print | awk -v var=$2 '{if ($1==var) {print toupper($6)}}')"
299                TYPE=${TYPE:-"EXT3"}
300                ;;
301     8e)        TYPE="LINUX-LVM" ;;
302     a7)        TYPE="CACHE" ;;         # (compatibilidad con Brutalix)
303     bf)        TYPE="SOLARIS" ;;
304     ca)        TYPE="CACHE" ;;
305     fd)        TYPE="LINUX-RAID" ;;
306     *)         TYPE="UNKNOWN" ;;
307esac
308echo $TYPE
309}
310
311
312#/**
313#         ogGetMountPoint int_ndisk int_npartition
314#@brief   Devuelve el punto de montaje de un sistema de archivos.
315#@arg  \c int_ndisk      nº de orden del disco
316#@arg  \c int_npartition nº de orden de la partición
317#@return  Punto de montaje
318#@exception OG_ERR_FORMAT    Formato incorrecto.
319#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
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 ndisk      nº de orden del disco
414#@arg  \c 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.1 - En pruebas para adaptarla a 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 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 ndisk      nº de orden del disco
465#@arg  \c 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 ndisk      nº de orden del disco
550#@arg  \c 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   El sistema de archivos se amplía al mínimo + 1 KB.
556#@note      Requisitos: *resize*
557#@version 0.1 - En pruebas para adaptarla a OpenGNSys.
558#@author  Ramon Gomez, ETSII Universidad de Sevilla
559#@date    2009-09-23
560#*/
561function ogReduceFs () {
562
563# Variables locales
564local PART BLKS SIZE
565
566#/// Si se solicita, mostrar ayuda.
567if [ "$*" == "help" ]; then
568    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
569           "$FUNCNAME 1 1"
570    return
571fi
572#/// Error si no se reciben 2 parámetros.
573[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
574
575#/// Obtener partición.
576PART="$(ogDiskToDev $1 $2)" || return $?
577
578#/// Redimensionar según el tipo de particion.
579case "$(ogGetFsType $1 $2)" in
580    EXT[234])
581        ogUnmount $1 $2 >/dev/null
582        # Ext2/3/4: Tamaño de los bloques del sistema de archivos
583        BLKS=$(tune2fs -l $PART | awk '/Block size/ {print int($3/512)}')
584        # Traduce el num. en sectores de 512B a tamano en MB.
585        SIZE=$(resize2fs -P $PART 2>/dev/null | \
586                       awk -v B=$BLKS '/minimum size/ {print int($7*B/2048+1000)}')
587        resize2fs -fp $PART "${SIZE}M" &>/dev/null || ogRaiseError $OG_ERR_PARTITION "$1,$2" || return $?
588            ;;
589    REISERFS)           # Usar "resize_reiserfs"
590            ;;
591    NTFS|HNTFS)
592        rm -f $(ogGetPath $1 $2 pagefile.sys)
593        ogUnmount $1 $2 >/dev/null
594        # NTFS: Obtiene tamaño mínimo en MB.
595        SIZE=$(ntfsresize -fi $PART | awk '/resize at/ {print $8+1000}')
596        ntfsresize -fns "${SIZE}M" $PART >/dev/null || ogRaiseError $OG_ERR_PARTITION "$1,$2" || return $?
597        ntfsresize -fs "${SIZE}M" $PART <<<"y" >/dev/null || ogRaiseError $OG_ERR_PARTITION "$1,$2" || return $?
598            ;;
599    *)  ogRaiseError $OG_ERR_PARTITION "$1,$2"
600            return $? ;;
601esac
602#/// Mostrar nuevo tamaño en KB.
603echo $[SIZE*1024]
604}
605
606
607#/**
608#         ogUnlock int_ndisk int_npartition
609#@see     ogUnlockPartition
610#*/
611function ogUnlock () {
612ogUnlockPartition "$@"
613}
614
615#/**
616#         ogUnlockPartition int_ndisk int_npartition
617#@brief   Elimina el fichero de bloqueo para una particion.
618#@arg  \c ndisk      nº de orden del disco
619#@arg  \c npartition nº de orden de la partición
620#@return  (nada)
621#@exception OG_ERR_FORMAT    Formato incorrecto.
622#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
623#@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 "-".
624#@version 0.1 - En pruebas para adaptarla a OpenGNSys.
625#@author  Ramon Gomez, ETSII Universidad de Sevilla
626#@date    2009-09-03
627#*/
628function ogUnlockPartition () {
629
630# Variables locales
631local PART LOCKFILE
632
633#/// Si se solicita, mostrar ayuda.
634if [ "$*" == "help" ]; then
635    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
636           "$FUNCNAME 1 1"
637    return
638fi
639#/// Error si no se reciben 2 parámetros.
640[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
641
642#/// Obtener partición.
643PART="$(ogDiskToDev $1 $2)" || return $?
644
645#/// Borrar archivo de bloqueo exclusivo.
646LOCKFILE="/var/lock/lock${PART//\//-}"
647rm -f $LOCKFILE
648}
649
650
651
652#/**
653#         ogUnmount int_ndisk int_npartition
654#@see     ogUnmountFs
655#*/
656function ogUnmount () {
657ogUnmountFs "$@"
658}
659
660#/**
661#         ogUnmountFs int_ndisk int_npartition
662#@brief   Desmonta un sistema de archivos.
663#@arg  \c ndisk      nº de orden del disco
664#@arg  \c npartition nº de orden de la partición
665#@return  Nada
666#@exception OG_ERR_FORMAT    Formato incorrecto.
667#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
668#@warning La partición no está previamente montada o no se puede desmontar.
669#@version 0.9 - Primera versión para OpenGNSys.
670#@author  Ramon Gomez, ETSII Universidad de Sevilla
671#@date    2009-09-28
672#*/
673function ogUnmountFs () {
674
675# Variables locales
676local PART MNTDIR
677
678#/// Si se solicita, mostrar ayuda.
679if [ "$*" == "help" ]; then
680    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" "$FUNCNAME 1 1"
681    return
682fi
683#/// Error si no se reciben 2 parámetros.
684[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
685
686#/// Obtener partición y punto de montaje.
687PART="$(ogDiskToDev $1 $2)" || return $?
688MNTDIR="$(ogGetMountPoint $1 $2)"
689
690#/// Si está montada, desmontarla.
691if [ -n "$MNTDIR" ]; then
692    # Error si la particion esta bloqueada.
693    ogIsLocked $1 $2 && ogRaiseError $OG_ERR_LOCKED "$1 $2" && return $?
694    #/// Crear punto de montaje.
695    umount $PART 2>/dev/null && rmdir $MNTDIR || ogEcho warning "$FUNCNAME: $MSG_DONTUNMOUNT: \"$1,$2\""
696        # linea temporal durante desarrollo para testear nuevas herramientas con el cliente completo nfs
697    if grep -q nfsroot /proc/cmdline; then
698                cat /etc/mtab | grep -v $PART > /var/tmp/mtab.temporal && cp /var/tmp/mtab.temporal /var/tmp/mtab && rm /var/tmp/mtab.temporal
699        fi
700        # fin linea temporal.
701else
702    ogEcho warning "$MSG_DONTMOUNT: \"$1,$2\""
703fi
704}
705
706
707#/**
708#         ogUnmountAll int_ndisk
709#@brief   Desmonta todos los sistema de archivos de un disco, excepto el caché local.
710#@arg  \c int_ndisk      nº de orden del disco
711#@return  Nada
712#@exception OG_ERR_FORMAT    Formato incorrecto.
713#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
714#@warning No se desmonta la partición marcada como caché local.
715#@version 0.9 - Versión para OpenGNSys.
716#@author  Ramon Gomez, ETSII Universidad de Sevilla
717#@date    2009/10/07
718#*/
719function ogUnmountAll () {
720
721# Variables locales
722local DISK PART
723#/// Si se solicita, mostrar ayuda.
724if [ "$*" == "help" ]; then
725    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk" "FUNCNAME 1"
726    return
727fi
728#/// Error si no se recibe 1 parámetro.
729[ $# == 1 ] || ogRaiseError $OG_ERR_FORMAT || return $?
730
731#/// Obtener partición y punto de montaje.
732DISK="$(ogDiskToDev $1)" || return $?
733for ((PART=1; PART<=$(ogGetPartitionsNumber $1); PART++)); do
734    case "$(ogGetFsType $1 $PART)" in
735        CACHE|LINUX-SWAP)
736           ;;
737        *)
738           ogUnmount $1 $PART ;;
739    esac
740done
741}
742
743
744function ogFindCache () {
745#/**  @function ogFindCache: @brief Detecta la particion CACHE EAC seg�n tipo a7 o label CACHE sobre particion ext3.
746#@param  no requiere parametros
747#@return  devuelve el identificador linux de la particion CACHE.
748#@warning  si no hay cache no devuelve nada
749#@attention Requisitos: la salida de fdisk de la cache a7 debe ser NeXTSTEP
750#@note    Notas sin especificar
751#@version 0.1       Date: 27/10/2008                 Author Antonio J. Doblas Viso. Universidad de Malaga
752#*/
753#
754if [ $# != 0 ]
755then
756        Msg "FindCache detecta la particion tipo a7 o aquella que tenga el label CACHE" info
757        Msg "devuelve /dev/sda3" info2
758        Msg "sintaxis: FindCache   --- Ejemplo: FindCache -> /dev/sda3/ " example
759        return
760fi
761end=`ogDiskToDev | wc -w`
762unset cache
763for (( disk = 1; disk <= $end; disk++))
764do
765        disco=`ogDiskToDev $disk`
766        totalpart=`parted $disco print | egrep ^" [0123456789] " -c`
767        #echo Buscando en disco: $disco con $totalpart particiones
768        if [ `fdisk -l $disco | egrep ^/dev | grep NeXTSTEP | cut -f1 -d" "` ]
769        then
770        #       echo comprobando si es particion a7
771                cache=`fdisk -l $disco | egrep ^/dev | grep NeXTSTEP | cut -f1 -d" "`
772        else
773                #echo comprobando si existe la etiqueta cache
774                if find /dev/disk | grep CACHE > /dev/null
775                then
776                        devcache=`ls -ls /dev/disk/by-label/CACHE | awk -F..\/..\/ '{print $2}'`
777                        cache=/dev/$devcache
778                fi
779        fi
780done
781if [ -n "$cache" ]
782then
783        echo $cache
784else
785        return 1
786fi
787}
788
789function ogMountCache () {
790#/**  @function ogMountCache: @brief Monta la particion Cache y exporta la variable $OGCAC
791#@param  sin parametros
792#@return Punto de montaje dentro de mnt, ejemplo con un parametro: ogMountCache => /mnt/sda3
793#@warning  Salidas de errores no determinada
794#@warning
795#@attention
796#@version 0.1   Date: 27/10/2008 Author Antonio J. Doblas Viso. Universidad de Malaga. Proyecto EAC
797#@note
798#*/
799CACHELINUX=`ogFindCache`
800CACHEOG=`ogDevToDisk $CACHELINUX`
801OGCAC=`ogMount $CACHEOG`
802echo $OGCAC
803export OGCAC
804}
805
806function ogUnmountCache () {
807#/**  @function UmountCache: @brief Desmonta la particion Cache y elimina la variable $OGCAC
808#@param  sin parametros
809#@return nada
810#@warning  Salidas de errores no determinada
811#@warning
812#@attention
813#@version 1.0   Date: 27/10/2008 Author Antonio J. Doblas Viso. Universidad de Malaga. Proyecto EAC
814#@note
815#*/
816CACHELINUX=`ogFindCache`
817CACHEOG=`ogDevToDisk $CACHELINUX`
818#echo ogUnmountPartition $cacheeac
819ogUnmountPartition $CACHEOG
820unset OGCAC
821}
822
823
824function ogFormatCache () {
825#/**  @function ogFormatCache: @brief Formatea la Cache EAC y le asigna el label CACHE
826#@param  No requiere
827#@return   la propia del mkfs.ext3
828#@warning
829#@attention
830#@note
831#@version 0.1       Date: 27/10/2008                 Author Antonio J. Doblas Viso. Universidad de Malaga
832#*/
833
834if [ $# = 0 ]
835then
836        cd /
837        ogUnmountCache
838        dev=`ogFindCache`
839        Msg "Iniciando el formateo de la particion CACHE, ubicada en $dev, y asignandole el label CACHE" red
840        mkfs.ext3 $dev -L CACHE
841        ogInfoCache
842fi
843if [ $# = 1 ]
844then
845        mkfs.ext3 $1 -L CACHE
846fi
847}
Note: See TracBrowser for help on using the repository browser.