source: client/engine/FileSystem.lib @ 3915005

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 3915005 was 99d1786, checked in by ramon <ramongomez@…>, 14 years ago

Versión 1.0.2: corregir errata al chequear Ext2/3/4, que afecta al script createImage para sistemas Linux.

git-svn-id: https://opengnsys.es/svn/branches/version1.0@2341 a21b9725-9963-47de-94b9-378ad31fedc9

  • Property mode set to 100755
File size: 32.0 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#@version 1.0.2 - Ignorar códigos de salida de comprobación (no erróneos).
28#@author  Ramon Gomez, ETSII Universidad de Sevilla
29#@date    2011-09-23
30#*/
31function ogCheckFs ()
32{
33# Variables locales.
34local PART TYPE PROG PARAMS CODES
35
36# Error si no se reciben 2 parámetros.
37[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
38# Obtener partición.
39PART="$(ogDiskToDev $1 $2)" || return $?
40
41TYPE=$(ogGetFsType $1 $2)
42case "$TYPE" in
43    EXT[234])     PROG="e2fsck"; PARAMS="-y"; CODES=(1 2) ;;
44    REISERFS)     PROG="fsck.reiserfs"; PARAMS="<<<\"Yes\""; CODES=(1 2) ;;
45    REISER4)      PROG="fsck.reiser4"; PARAMS="-ay" ;;
46    JFS)          PROG="fsck.jfs"; CODES=(1 2) ;;
47    XFS)          PROG="fsck.xfs" ;;
48    NTFS|HNTFS)   PROG="ntfsfix" ;;
49    FAT32|HFAT32) PROG="dosfsck"; PARAMS="-a"; CODES=1 ;;
50    FAT16|HFAT16) PROG="dosfsck"; PARAMS="-a"; CODES=1 ;;
51    FAT12|HFAT12) PROG="dosfsck"; PARAMS="-a"; CODES=1 ;;
52    *)            ogRaiseError $OG_ERR_PARTITION "$1, $2, $TYPE"
53                  return $? ;;
54esac
55# Error si el sistema de archivos esta montado o bloqueado.
56if ogIsMounted $1 $2; then
57    ogRaiseError $OG_ERR_PARTITION "$1 $2"       # Indicar nuevo error
58    return $?
59fi
60if ogIsLocked $1 $2; then
61    ogRaiseError $OG_ERR_LOCKED "$1 $2"
62    return $?
63fi
64# Comprobar en modo uso exclusivo.
65ogLock $1 $2
66eval $PROG $PARAMS $PART
67ERRCODE=$?
68case $ERRCODE in
69    0|${CODES[*]})
70            ERRCODE=0 ;;
71    127)    ogRaiseError $OG_ERR_NOTEXEC "$PROG"
72            ERRCODE=$OG_ERR_NOTEXEC ;;
73    *)      ogRaiseError $OG_ERR_PARTITION "$1 $2"
74            ERRCODE=$OG_ERR_PARTITION ;;
75esac
76ogUnlock $1 $2
77return $ERRCODE
78}
79
80
81#/**
82#         ogExtendFs int_ndisk int_npartition
83#@brief   Extiende un sistema de archivos al tamaño de su partición.
84#@param   int_ndisk      nº de orden del disco
85#@param   int_npartition nº de orden de la partición
86#@return  (nada)
87#@exception OG_ERR_FORMAT   Formato incorrecto.
88#@exception OG_ERR_NOTFOUND Disco o particion no corresponden con un dispositivo.
89#@exception OG_ERR_PARTITION Partición desconocida o no accesible.
90#@note    Requisitos: *resize*
91#@version 0.1 -  Integracion para Opengnsys  -  EAC:   EnlargeFileSystem() en ATA.lib
92#@author  Antonio J. Doblas Viso. Universidad de Malaga
93#@date    2008-10-27
94#@version 0.9 - Primera adaptacion para OpenGNSys.
95#@author  Ramon Gomez, ETSII Universidad de Sevilla
96#@date    2009-09-23
97#*/
98function ogExtendFs ()
99{
100# Variables locales.
101local PART PROG PARAMS
102
103# Si se solicita, mostrar ayuda.
104if [ "$*" == "help" ]; then
105    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
106           "$FUNCNAME 1 1"
107    return
108fi
109# Error si no se reciben 2 parámetros.
110[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
111
112# Obtener partición.
113PART="$(ogDiskToDev $1 $2)" || return $?
114
115ogUnmount $1 $2 2>/dev/null
116# Redimensionar al tamano máximo según el tipo de partición.
117TYPE=$(ogGetFsType $1 $2)
118case "$TYPE" in
119    EXT[234])   PROG="resize2fs"; PARAMS="-f" ;;
120    REISERFS)   PROG="resize_reiserfs"; PARAMS="-f" ;;
121    NTFS|HNTFS) PROG="ntfsresize"; PARAMS="<<<\"y\" -f" ;;
122    *)          ogRaiseError $OG_ERR_PARTITION "$1 $2 $TYPE"
123                return $? ;;
124esac
125# Error si el sistema de archivos está montado o bloqueado.
126if ogIsMounted $1 $2; then
127    ogRaiseError $OG_ERR_PARTITION "$1 $2"       # Indicar nuevo error
128    return $?
129fi
130if ogIsLocked $1 $2; then
131    ogRaiseError $OG_ERR_LOCKED "$1 $2"
132    return $?
133fi
134# Redimensionar en modo uso exclusivo.
135ogLock $1 $2
136eval $PROG $PARAMS $PART &>/dev/null
137ERRCODE=$?
138case $ERRCODE in
139    0)    ;;
140    127)  ogRaiseError $OG_ERR_NOTEXEC "$PROG"
141          ERRCODE=$OG_ERR_NOTEXEC ;;
142    *)    ogRaiseError $OG_ERR_PARTITION "$1 $2"
143          ERRCODE=$OG_ERR_PARTITION ;;
144esac
145ogUnlock $1 $2
146return $ERRCODE
147}
148
149
150#/**
151#         ogFormat int_ndisk int_npartition | CACHE
152#@see     ogFormatFs ogFormatCache
153#*/
154function ogFormat ()
155{
156case "$*" in
157    CACHE|cache)  ogFormatCache ;;
158    *)            ogFormatFs "$@" ;;
159esac
160}
161
162
163#/**
164#         ogFormatFs int_ndisk int_npartition [type_fstype] [str_label]
165#@brief   Formatea un sistema de ficheros según el tipo de su partición.
166#@param   int_ndisk      nº de orden del disco
167#@param   int_npartition nº de orden de la partición
168#@param   type_fstype    mnemónico de sistema de ficheros a formatear
169#@param   str_label      etiqueta de volumen (opcional)
170#@return  (por determinar)
171#@exception OG_ERR_FORMAT    Formato incorrecto.
172#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
173#@exception OG_ERR_PARTITION Partición no accesible o desconocida.
174#@note    Requisitos:   mkfs*
175#@warning No formatea particiones montadas ni bloqueadas.
176#@todo    Definir salidas.
177#@version 0.9 - Primera versión para OpenGNSys.
178#@author  Ramon Gomez, ETSII Universidad de Sevilla
179#@date    2009-10-08
180#*/ ##
181function ogFormatFs ()
182{
183# Variables locales
184local PART ID TYPE LABEL PROG PARAMS ERRCODE
185
186# Si se solicita, mostrar ayuda.
187if [ "$*" == "help" ]; then
188    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition [str_label]" \
189           "$FUNCNAME 1 1" \
190           "$FUNCNAME 1 1 EXT4" \
191           "$FUNCNAME 1 1 \"DATA\"" \
192           "$FUNCNAME 1 1 EXT4 \"DATA\""
193    return
194fi
195# Error si no se reciben entre 2 y 4 parámetros.
196[ $# -ge 2 -a $# -le 4 ] || ogRaiseError $OG_ERR_FORMAT || return $?
197# Obtener dispositivo y tipo de sisitema de archivos.
198PART="$(ogDiskToDev $1 $2)" || return $?
199TYPE="$(ogGetFsType $1 $2)" || return $?
200
201# Elegir tipo de formato segun el tipo de particion.
202case "$3" in
203    EXT2)      ID=83; PROG="mkfs.ext2";;
204    EXT3)      ID=83; PROG="mkfs.ext3";;
205    EXT4)      ID=83; PROG="mkfs.ext4";;
206    REISERFS)  ID=83; PROG="mkfs.reiserfs"; PARAMS="-f" ;;
207    REISER4)   ID=83; PROG="mkfs.reiser4";;
208    XFS)       ID=83; PROG="mkfs.xfs"; PARAMS="-f" ;;
209    JFS)       ID=83; PROG="mkfs.jfs"; PARAMS="<<<\"y\"";;
210    NTFS)      ID=7;  PROG="mkntfs"; PARAMS="-f" ;;
211    HNTFS)     ID=17; PROG="mkntfs"; PARAMS="-f" ;;
212    FAT32)     ID=b;  PROG="mkdosfs"; PARAMS="-F 32" ;;
213    HFAT32)    ID=1b; PROG="mkdosfs"; PARAMS="-F 32" ;;
214    FAT16)     ID=6;  PROG="mkdosfs"; PARAMS="-F 16" ;;
215    HFAT16)    ID=16; PROG="mkdosfs"; PARAMS="-F 16" ;;
216    FAT12)     ID=1;  PROG="mkdosfs"; PARAMS="-F 12" ;;
217    HFAT12)    ID=11; PROG="mkdosfs"; PARAMS="-F 12" ;;
218    *)         LABEL="$3" ;;
219esac
220# Si no se indica explícitamente, detectar el tipo de sistema de archivos.
221if [ -z "$PROG" ]; then
222    case "$TYPE" in
223        EXT2)         PROG="mkfs.ext2";;
224        EXT3)         PROG="mkfs.ext3";;
225        EXT4)         PROG="mkfs.ext4";;
226        REISERFS)     PROG="mkfs.reiserfs"; PARAMS="-f" ;;
227        REISER4)      PROG="mkfs.reiser4";;
228        XFS)          PROG="mkfs.xfs"; PARAMS="-f" ;;
229        JFS)          PROG="mkfs.jfs"; PARAMS="<<<\"y\"" ;;
230        LINUX-SWAP)   PROG="mkswap" ;;
231        NTFS|HNTFS)   PROG="mkntfs"; PARAMS="-f" ;;
232        FAT32|HFAT32) PROG="mkdosfs"; PARAMS="-F 32" ;;
233        FAT16|HFAT16) PROG="mkdosfs"; PARAMS="-F 16" ;;
234        FAT12|HFAT12) PROG="mkdosfs"; PARAMS="-F 12" ;;
235        *)            ogRaiseError $OG_ERR_PARTITION "$1 $2 $TYPE"
236                      return $? ;;
237    esac
238else
239    [ $TYPE == "$3" -o $ID == "$(ogGetPartitionId $1 $2)" ] || ogRaiseError $OG_ERR_PARTITION "$3 != $TYPE" || return $?
240fi
241# Comprobar consistencia entre id. de partición y tipo de sistema de archivos.
242[ -n "$PROG" ] || ogRaiseError $OG_ERR_PARTITION "$3 != $TYPE" || return $?
243
244# Etiquetas de particion.
245if [ -z "$LABEL" ]; then
246    [ "$4" != "CACHE" ] || ogRaiseError $OG_ERR_FORMAT "$MSG_RESERVEDVALUE: CACHE" || return $?
247    [ -n "$4" ] && PARAMS="$PARAMS -L $4"
248else
249    PARAMS="$PARAMS -L $LABEL"
250fi
251
252# Error si la particion esta montada o está bloqueada.
253if ogIsMounted $1 $2; then
254    ogRaiseError $OG_ERR_PARTITION "$1 $2"       # Indicar nuevo error
255    return $?
256fi
257if ogIsLocked $1 $2; then
258    ogRaiseError $OG_ERR_LOCKED "$1 $2"
259    return $?
260fi
261# Formatear en modo uso exclusivo.
262ogLock $1 $2
263eval $PROG $PARAMS $PART 2>/dev/null
264ERRCODE=$?
265case $ERRCODE in
266    0)    ;;
267    127)  ogRaiseError $OG_ERR_NOTEXEC "$PROG" ;;
268    *)    ogRaiseError $OG_ERR_PARTITION "$1 $2" ;;
269esac
270ogUnlock $1 $2
271return $ERRCODE
272}
273
274
275#/**
276#         ogGetFsType int_ndisk int_npartition
277#@brief   Devuelve el mnemonico con el tipo de sistema de archivos.
278#@param   int_ndisk      nº de orden del disco
279#@param   int_npartition nº de orden de la partición
280#@return  Mnemonico
281#@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 }
282#@exception OG_ERR_FORMAT   Formato incorrecto.
283#@exception OG_ERR_NOTFOUND Disco o particion no corresponden con un dispositivo.
284#@version 0.1 -  Integracion para Opengnsys  -  EAC:   TypeFS() en ATA.lib
285#@author  Antonio J. Doblas Viso. Universidad de Malaga
286#@date    2008-10-27
287#@version 0.9 - Primera adaptacion para OpenGNSys.
288#@author  Ramon Gomez, ETSII Universidad de Sevilla
289#@date    2009-07-21
290#*/ ##
291function ogGetFsType () {
292
293# Variables locales.
294local ID TYPE
295
296# Si se solicita, mostrar ayuda.
297if [ "$*" == "help" ]; then
298    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
299           "$FUNCNAME 1 1  =>  NTFS"
300    return
301fi
302# Error si no se reciben 2 parámetros.
303[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
304
305# Detectar id. de tipo de partición y codificar al mnemonico.
306DISK=$(ogDiskToDev "$1") || return $?
307ID=$(ogGetPartitionId "$1" "$2") || return $?
308case "$ID" in
309     0)         TYPE="EMPTY" ;;
310     1)         TYPE="FAT12" ;;
311     5|f)       TYPE="EXTENDED" ;;
312     [6e])      TYPE="FAT16" ;;
313     7)         TYPE="NTFS" ;;          # Nota: también puede ser EXFAT
314     [bc])      TYPE="FAT32" ;;
315     11)        TYPE="HFAT12" ;;
316     12)        TYPE="COMPAQDIAG" ;;
317     1[6e])     TYPE="HFAT16" ;;
318     17)        TYPE="HNTFS" ;;
319     1[bc])     TYPE="HFAT32" ;;
320     42)        TYPE="WIN-DYNAMIC" ;;
321     82)        TYPE="LINUX-SWAP" ;;
322     83)        TYPE="$(parted -sm $DISK print 2>/dev/null | \
323                            awk -F: -v fs=$2 '{if ($1==fs) {print toupper($5)}}')"
324                # Si el tipo no es válido para partición Linux, Ext3 por defecto.
325                case "$TYPE" in
326                     EXT[234]|REISERFS|REISER4|BTRFS|JFS|XFS)
327                         ;;
328                     *)  TYPE="EXT3" ;;
329                esac
330                ;;
331     8e)        TYPE="LINUX-LVM" ;;
332     a7)        TYPE="CACHE" ;;         # (compatibilidad con Brutalix)
333     af)        TYPE="HFS" ;;
334     bf)        TYPE="SOLARIS" ;;
335     ca)        TYPE="CACHE" ;;
336     fd)        TYPE="LINUX-RAID" ;;
337     *)         TYPE="UNKNOWN" ;;
338esac
339echo $TYPE
340}
341
342
343#/**
344#         ogGetMountPoint int_ndisk int_npartition
345#@brief   Devuelve el punto de montaje de un sistema de archivos.
346#@param   int_ndisk      nº de orden del disco
347#@param   int_npartition nº de orden de la partición
348#@return  Punto de montaje
349#@exception OG_ERR_FORMAT    Formato incorrecto.
350#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
351#@note    Requisitos: \c mount* \c awk
352#@version 0.9 - Primera versión para OpenGNSys.
353#@author  Ramon Gomez, ETSII Universidad de Sevilla
354#@date    2009-10-15
355#*/ ##
356function ogGetMountPoint ()
357{
358# Variables locales
359local PART
360# Si se solicita, mostrar ayuda.
361if [ "$*" == "help" ]; then
362    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
363           "$FUNCNAME 1 1  =>  /mnt/sda1"
364    return
365fi
366# Error si no se reciben 2 parámetros.
367[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
368# Obtener partición.
369PART="$(ogDiskToDev $1 $2)" || return $?
370
371mount | awk -v P=$PART '{if ($1==P) {print $3}}'
372}
373
374
375#/**
376#         ogIsFormated int_ndisk int_npartition
377#@brief   Comprueba si un sistema de archivos está formateado.
378#@param   int_ndisk      nº de orden del disco o volumen.
379#@param   int_npartition nº de orden del sistema de archivos.
380#@return  Código de salida: 0 - formateado, 1 - sin formato o error.
381#@version 0.91 - Adaptación inicial para comprobar que existe caché.
382#@author  Ramon Gomez, ETSII Universidad de Sevilla
383#@date    2010-03-18
384#@version 1.0.1 - Devolver falso en caso de error.
385#@author  Ramon Gomez, ETSII Universidad de Sevilla
386#@date    2011-05-18
387#*/ ##
388function ogIsFormated ()
389{
390# Variables locales
391local DISK
392if [ "$*" == "help" ]; then
393    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
394           "if $FUNCNAME 1 1; then ... ; fi"
395    return
396fi
397# Falso, en caso de error.
398[ $# == 2 ] || return 1
399
400DISK=$(ogDiskToDev "$1") || return 1
401test -n "$(parted -sm $DISK print 2>/dev/null | \
402                      awk -F: -v fs=$2 '{if ($1==fs) print $5}')"
403}
404
405
406#/**
407#         ogIsMounted int_ndisk int_npartition
408#@brief   Comprueba si un sistema de archivos está montado.
409#@param   int_ndisk      nº de orden del disco
410#@param   int_npartition nº de orden de la partición
411#@return  Código de salida: 0 - montado, 1 - sin montar o error.
412#@version 0.9 - Primera versión para OpenGNSys.
413#@author  Ramon Gomez, ETSII Universidad de Sevilla
414#@date    2009-10-15
415#@version 1.0.1 - Devolver falso en caso de error.
416#@author  Ramon Gomez, ETSII Universidad de Sevilla
417#@date    2011-05-18
418#*/ ##
419function ogIsMounted ()
420{
421# Si se solicita, mostrar ayuda.
422if [ "$*" == "help" ]; then
423    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
424           "if $FUNCNAME 1 1; then ... ; fi"
425    return
426fi
427# Falso, en caso de error.
428[ $# == 2 ] || return 1
429
430test -n "$(ogGetMountPoint $1 $2)"
431}
432
433
434#/**
435#         ogIsLocked int_ndisk int_npartition
436#@brief   Comprueba si una partición está bloqueada por una operación de uso exclusivo.
437#@param   int_ndisk      nº de orden del disco
438#@param   int_npartition nº de orden de la partición
439#@return  Código de salida: 0 - bloqueado, 1 - sin bloquear o error.
440#@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 "-".
441#@version 0.9 - Primera versión para OpenGNSys.
442#@author  Ramon Gomez, ETSII Universidad de Sevilla
443#@date    2009-09-03
444#@version 1.0.1 - Devolver falso en caso de error.
445#@author  Ramon Gomez, ETSII Universidad de Sevilla
446#@date    2011-05-18
447#*/ ##
448function ogIsLocked ()
449{
450# Variables locales
451local PART LOCKFILE
452
453# Si se solicita, mostrar ayuda.
454if [ "$*" == "help" ]; then
455    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
456           "if $FUNCNAME 1 1; then ... ; fi"
457    return
458fi
459# Falso, en caso de error.
460[ $# == 2 ] || return 1
461
462# Obtener partición.
463PART="$(ogDiskToDev $1 $2)" || return 1
464
465# Comprobar existencia del fichero de bloqueo.
466LOCKFILE="/var/lock/lock${PART//\//-}"
467test -f $LOCKFILE
468}
469
470
471#/**
472#         ogLock int_ndisk int_npartition
473#@see     ogLockPartition
474#*/
475function ogLock ()
476{
477ogLockPartition "$@"
478}
479
480#/**
481#         ogLockPartition int_ndisk int_npartition
482#@brief   Genera un fichero de bloqueo para una partición en uso exlusivo.
483#@param   int_ndisk      nº de orden del disco
484#@param   int_npartition nº de orden de la partición
485#@return  (nada)
486#@exception OG_ERR_FORMAT    Formato incorrecto.
487#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
488#@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 "-".
489#@version 0.9 - Primera versión para OpenGNSys.
490#@author  Ramon Gomez, ETSII Universidad de Sevilla
491#@date    2009-09-03
492#*/ ##
493function ogLockPartition ()
494{
495# Variables locales
496local PART LOCKFILE
497
498# Si se solicita, mostrar ayuda.
499if [ "$*" == "help" ]; then
500    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
501           "$FUNCNAME 1 1"
502    return
503fi
504# Error si no se reciben 2 parámetros.
505[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
506
507# Obtener partición.
508PART="$(ogDiskToDev $1 $2)" || return $?
509
510# Crear archivo de bloqueo exclusivo.
511LOCKFILE="/var/lock/lock${PART//\//-}"
512touch $LOCKFILE
513}
514
515
516#/**
517#         ogMount int_ndisk int_npartition
518#@see     ogMountFs ogMountCache ogMountCdrom
519#*/ ##
520function ogMount ()
521{
522case "$*" in
523    CACHE|cache)
524        ogMountCache ;;
525    CDROM|cdrom)
526        ogMountCdrom ;;
527    *)  ogMountFs "$@" ;;
528esac
529}
530
531
532#/**
533#         ogMountFs int_ndisk int_npartition
534#@brief   Monta un sistema de archivos.
535#@param   int_ndisk      nº de orden del disco
536#@param   int_npartition nº de orden de la partición
537#@return  Punto de montaje
538#@exception OG_ERR_FORMAT    Formato incorrecto.
539#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
540#@exception OG_ERR_PARTITION Tipo de particion desconocido o no se puede montar.
541#@version 0.1 -  Integracion para Opengnsys  -  EAC:   MountPartition() en FileSystem.lib
542#@author  Antonio J. Doblas Viso. Universidad de Malaga
543#@date    2008-10-27
544#@version 0.9 - Primera version para OpenGNSys.
545#@author  Ramon Gomez, ETSII Universidad de Sevilla
546#@date    2009-09-28
547#*/ ##
548function ogMountFs ()
549{
550# Variables locales
551local PART TYPE MNTDIR MOUNT PARAMS
552
553# Si se solicita, mostrar ayuda.
554if [ "$*" == "help" ]; then
555    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
556           "$FUNCNAME 1 1  =>  /mnt/sda1"
557    return
558fi
559# Error si no se reciben 2 parámetros.
560[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
561
562# Obtener partición.
563PART="$(ogDiskToDev $1 $2)" || return $?
564
565# Comprobar si el sistema de archivos ya está montada.
566MNTDIR="$(ogGetMountPoint $1 $2)"
567# Si no, montarla en un directorio de sistema
568if [ -z "$MNTDIR" ]; then
569    # Error si la particion esta bloqueada.
570    if ogIsLocked $1 $2; then
571        ogRaiseError $OG_ERR_LOCKED "$MSG_PARTITION, $1 $2"
572        return $?
573    fi
574    # Crear punto de montaje o enlace símbolico para Caché local.
575    MNTDIR=${PART/dev/mnt}
576    TYPE="$(ogGetFsType $1 $2)" || return $?
577    if [ "$TYPE" == "CACHE" -a -n "$OGCAC" ]; then
578        ln -fs $OGCAC $MNTDIR
579    else
580        mkdir -p $MNTDIR
581    fi
582    # Montar según el tipo de sitema de archivos.
583    case "$TYPE" in
584        CACHE)      MOUNT=mount ;;
585        EXT[234])   MOUNT=mount ;;
586        REISERFS)   insmod /lib/modules/$(uname -r)/kernel/fs/reiserfs/reiserfs.ko 2>/dev/null
587                    MOUNT=mount ;;
588        JFS)        insmod /lib/modules/$(uname -r)/kernel/fs/jfs/jfs.ko 2>/dev/null
589                    MOUNT=mount ;;
590        XFS)        insmod /lib/modules/$(uname -r)/kernel/fs/xfs/xfs.ko 2>/dev/null
591                    MOUNT=mount ;;
592        NTFS|HNTFS) MOUNT=ntfs-3g ;;
593        FAT16|FAT32|HFAT16|HFAT32)
594                    MOUNT=mount; PARAMS="-t vfat" ;;
595        *)  #/// Error, si la partición no es montable.
596            rmdir $MNTDIR
597            ogRaiseError $OG_ERR_PARTITION "$1, $2, $TYPE"
598            return $OG_ERR_PARTITION
599            ;;
600    esac
601    $MOUNT $PARAMS $PART $MNTDIR 2>/dev/null || \
602               $MOUNT $PARAMS $PART $MNTDIR -o force,remove_hiberfile 2>/dev/null || \
603               ogRaiseError $OG_ERR_PARTITION "$1, $2, $TYPE" || return $?
604    # linea temporal durante desarrollo para poder usar el cliente completo nfs y testeas nuevas herramientas.
605    if grep -q nfsroot /proc/cmdline; then
606        echo "$PART $MNTDIR" >> /etc/mtab
607    fi
608    # fin linea temporal.
609fi
610echo $MNTDIR
611}
612
613
614#####  PRUEBAS
615# Montar CDROM
616function ogMountCdrom ()
617{
618local DEV MNTDIR
619DEV="/dev/cdrom"            # Por defecto
620MNTDIR=$(mount | awk -v D=$DEV '{if ($1==D) {print $3}}')
621if [ -z "$MNTDIR" ]; then
622    MNTDIR=${DEV/dev/mnt}
623    mkdir -p $MNTDIR
624    mount -t iso9660 $DEV $MNTDIR || ogRaiseError $OG_ERR_PARTITION "cdrom" || return $?
625fi
626echo $MNTDIR
627}
628
629
630#/**
631#         ogReduceFs int_ndisk int_npartition
632#@brief   Reduce el tamaño del sistema de archivos, sin tener en cuenta el espacio libre.
633#@param   int_ndisk      nº de orden del disco
634#@param   int_npartition nº de orden de la partición
635#@return  int_tamañoKB - tamaño en KB
636#@exception OG_ERR_FORMAT    Formato incorrecto.
637#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
638#@exception OG_ERR_PARTITION Partición desconocida o no accesible.
639#@warning En Windows, se borran los ficheros pagefile.sys e hiberfile.sys
640#@warning El sistema de archivos se amplía al mínimo + 10%.
641#@note    Requisitos:   *resize*
642#@version 0.1 -  Integracion para Opengnsys  -  EAC:   ReduceFileSystem() en ATA.lib
643#@author  Antonio J. Doblas Viso. Universidad de Malaga
644#@date    2008-10-27
645#@version 0.9 - Primera version para OpenGNSys.
646#@author  Ramon Gomez, ETSII Universidad de Sevilla
647#@date    2009-09-23
648#@version 0.9.2 - Añadir un 10% al tamaño mínimo requerido.
649#@author  Ramon Gomez, ETSII Universidad de Sevilla
650#@date    2010-09-27
651#@version 1.0 -  Deteccion automatica del tamaño minimo adecuado
652#@author  Antonio J. Doblas Viso. Universidad de Malaga
653#@date    2011-02-24
654#*/ ##
655function ogReduceFs ()
656{
657# Variables locales
658local PART BLKS SIZE
659
660# Si se solicita, mostrar ayuda.
661if [ "$*" == "help" ]; then
662    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
663           "$FUNCNAME 1 1"
664    return
665fi
666# Error si no se reciben 2 parámetros.
667[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
668
669# Obtener partición.
670PART="$(ogDiskToDev $1 $2)" || return $?
671
672# Redimensionar según el tipo de particion.
673case "$(ogGetFsType $1 $2)" in
674    EXT[234])
675        ogUnmount $1 $2 2>/dev/null
676        # Ext2/3/4: Tamaño de los bloques del sistema de archivos
677        BLKS=$(tune2fs -l $PART | awk '/Block size/ {print int($3/512)}')
678        # Traduce el num. en sectores de 512B a tamano en MB.
679        #SIZE=$(resize2fs -P $PART 2>/dev/null | \
680                #       awk -v B=$BLKS '/minimum size/ {print int($7*1.1*B/2048)}')
681        #resize2fs -fp $PART "${SIZE}M" &>/dev/null || ogRaiseError $OG_ERR_PARTITION "$1,$2" || return $?
682        resize2fs -fpM $PART  &>/dev/null || ogRaiseError $OG_ERR_PARTITION "$1,$2" || return $?
683            ;;
684#    REISERFS)          # Usar "resize_reiserfs"
685#           ;;
686    NTFS|HNTFS)
687        ogDeleteFile $1 $2 pagefile.sys
688        ogDeleteFile $1 $2 hiberfile.sys
689        ogUnmount $1 $2 2>/dev/null
690        ## NTFS: Obtiene tamaño mínimo en MB.
691        #SIZE=$(ntfsresize -fi $PART | awk '/resize at/ {print int($8*1.1)}')
692        #ntfsresize -fns "${SIZE}M" $PART >/dev/null || ogRaiseError $OG_ERR_PARTITION "$1,$2" || return $?
693        #ntfsresize -fs "${SIZE}M" $PART <<<"y" >/dev/null || ogRaiseError $OG_ERR_PARTITION "$1,$2" || return $?
694                SIZE=$(ogReduceFsCheck $1 $2)
695            [ "$SIZE" == 0 ] && return 1       
696        ntfsresize -fs "${SIZE}M" $PART <<<"y"  || ogRaiseError $OG_ERR_PARTITION "error reduciendo $1,$2" || return $?
697            ;;
698    *)  ogRaiseError $OG_ERR_PARTITION "$1,$2"
699            return $? ;;
700esac
701#/// Mostrar nuevo tamaño en KB.
702#echo $[SIZE*1024]
703ogGetFsSize $1 $2
704}
705
706
707function ogReduceFsCheck ()
708{
709#IMPORTANTE: retorna el valor en MB que podrá reducir el FS de una particion ntfs
710#valor devuelto 0, y codigo error 1. No se puede reducir, probar a reiniciar windows y chkdsk
711
712
713local  PART RC MODE SIZE SIZEDATA
714[ $# == 2 ] && MODE=STAGE1
715[ $# == 3 ] && MODE=STAGE2
716[ -z $MODE ] && return
717
718PART="$(ogDiskToDev $1 $2)" || return $?
719ogUnmount $1 $2 &>/dev/null
720
721
722case $MODE in
723        STAGE1)
724        #       echo "primera etapa $*"
725                ntfsresize -fi $PART &>/dev/null
726                RC=`echo $?`
727        #       echo "RC es" $RC
728                if [ "$RC" -eq "1" ]
729                then
730                        echo "0"
731                        return 1       
732                fi 
733                SIZEDATA=$(ntfsresize -fi $PART | awk '/resize at/ {print $8+1000}')
734        #       echo "salida" $?
735        #       echo $SIZEDATA
736                ogReduceFsCheck $1 $2 $SIZEDATA
737                return 0
738       ;;
739        STAGE2)
740        #       echo "segunda etapa $*"
741                SIZEDATA=$3
742                ntfsresize -fns "${SIZEDATA}M" $PART &>/tmp/ntfsresize.txt
743                RC=$?
744                if [ "$RC" == "0" ]
745                then 
746                        SIZE=$SIZEDATA 
747                        echo $SIZE     
748                else
749                        SIZEEXTRA=$(cat /tmp/ntfsresize.txt | awk '/Needed relocations :/ {print $0}' | awk -F"(" '{print $2}' | awk '{print $1+500}')
750                        SIZE=$(expr $SIZEDATA + $SIZEEXTRA)
751                        ogReduceFsCheck $1 $2 $SIZE
752                        return 0
753                fi
754        ;;
755        *)
756        return
757        ;;
758esac
759}
760
761
762
763#/**
764#         ogUnlock int_ndisk int_npartition
765#@see     ogUnlockPartition
766#*/
767function ogUnlock ()
768{
769ogUnlockPartition "$@"
770}
771
772#/**
773#         ogUnlockPartition int_ndisk int_npartition
774#@brief   Elimina el fichero de bloqueo para una particion.
775#@param   int_ndisk      nº de orden del disco
776#@param   int_npartition nº de orden de la partición
777#@return  (nada)
778#@exception OG_ERR_FORMAT    Formato incorrecto.
779#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
780#@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 "-".
781#@version 0.9 - Primera versión para OpenGNSys.
782#@author  Ramon Gomez, ETSII Universidad de Sevilla
783#@date    2009-09-03
784#*/ ##
785function ogUnlockPartition ()
786{
787# Variables locales
788local PART LOCKFILE
789
790# Si se solicita, mostrar ayuda.
791if [ "$*" == "help" ]; then
792    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
793           "$FUNCNAME 1 1"
794    return
795fi
796# Error si no se reciben 2 parámetros.
797[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
798
799# Obtener partición.
800PART="$(ogDiskToDev $1 $2)" || return $?
801
802# Borrar archivo de bloqueo exclusivo.
803LOCKFILE="/var/lock/lock${PART//\//-}"
804rm -f $LOCKFILE
805}
806
807
808#/**
809#         ogUnmount int_ndisk int_npartition
810#@see     ogUnmountFs
811#*/
812function ogUnmount ()
813{
814ogUnmountFs "$@"
815}
816
817#/**
818#         ogUnmountFs int_ndisk int_npartition
819#@brief   Desmonta un sistema de archivos.
820#@param   int_ndisk      nº de orden del disco
821#@param   int_npartition nº de orden de la partición
822#@return  Nada
823#@exception OG_ERR_FORMAT    Formato incorrecto.
824#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
825#@warning La partición no está previamente montada o no se puede desmontar.
826#@version 0.1 -  Integracion para Opengnsys  -  EAC:  UmountPartition() en FileSystem.lib
827#@author  Antonio J. Doblas Viso. Universidad de Malaga
828#@date    2008-10-27
829#@version 0.9 - Primera version para OpenGNSys.
830#@author  Ramon Gomez, ETSII Universidad de Sevilla
831#@date    2009-09-28
832#*/ ##
833function ogUnmountFs ()
834{
835# Variables locales
836local PART MNTDIR
837
838# Si se solicita, mostrar ayuda.
839if [ "$*" == "help" ]; then
840    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" "$FUNCNAME 1 1"
841    return
842fi
843# Error si no se reciben 2 parámetros.
844[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
845
846# Obtener partición y punto de montaje.
847PART="$(ogDiskToDev $1 $2)" || return $?
848MNTDIR="$(ogGetMountPoint $1 $2)"
849
850# Si está montada, desmontarla.
851if [ -n "$MNTDIR" ]; then
852    # Error si la particion está bloqueada.
853    if ogIsLocked $1 $2; then
854        ogRaiseError $OG_ERR_LOCKED "$MSG_PARTITION $1, $2"
855        return $?
856    fi
857    # Desmontar y borrar punto de montaje.
858    umount $PART 2>/dev/null || ogEcho warning "$FUNCNAME: $MSG_DONTUNMOUNT: \"$1, $2\""
859    rmdir $MNTDIR 2>/dev/null || rm -f $MNTDIR 2>/dev/null
860    # linea temporal durante desarrollo para testear nuevas herramientas con el cliente completo nfs
861    if grep -q nfsroot /proc/cmdline; then
862        grep -v $PART /etc/mtab > /var/tmp/mtab.temporal && cp /var/tmp/mtab.temporal /var/tmp/mtab && rm /var/tmp/mtab.temporal
863    fi
864    # fin linea temporal.
865else
866    ogEcho warning "$MSG_DONTMOUNT: \"$1,$2\""
867fi
868}
869
870
871#/**
872#         ogUnmountAll int_ndisk
873#@brief   Desmonta todos los sistema de archivos de un disco, excepto el caché local.
874#@param   int_ndisk      nº de orden del disco
875#@return  Nada
876#@exception OG_ERR_FORMAT    Formato incorrecto.
877#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
878#@warning No se desmonta la partición marcada como caché local.
879#@version 0.9 - Versión para OpenGNSys.
880#@author  Ramon Gomez, ETSII Universidad de Sevilla
881#@date    2009/10/07
882#*/ ##
883function ogUnmountAll ()
884{
885# Variables locales
886local DISK PART
887# Si se solicita, mostrar ayuda.
888if [ "$*" == "help" ]; then
889    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk" "FUNCNAME 1"
890    return
891fi
892# Error si no se recibe 1 parámetro.
893[ $# == 1 ] || ogRaiseError $OG_ERR_FORMAT || return $?
894
895# Obtener partición y punto de montaje.
896DISK="$(ogDiskToDev $1)" || return $?
897for ((PART=1; PART<=$(ogGetPartitionsNumber $1); PART++)); do
898    case "$(ogGetFsType $1 $PART)" in
899        CACHE|LINUX-SWAP)
900           ;;
901        *)
902           ogUnmount $1 $PART ;;
903    esac
904done
905}
906
907#/**  @function ogGetFsSize: @brief Muestra el tamanio del sistema de archivos indicado, permite definir la unidad de medida, por defecto GB
908#@param  $1 int_diskEAC
909#@param $2 int_PartitionEAC
910#@param $3 str_UnidadMediada            parametro opcional, admite [ kB MB GB -default GB]
911#@return  cadena con int_TotalSize:int_DataSize:int_DataFree
912#@warning  Salidas de errores no determinada
913#@warning
914#@attention
915#@version 0.1 -  Integracion para Opengnsys  -  EAC:  SizeFileSystem() en FileSystem.lib
916#@author  Antonio J. Doblas Viso. Universidad de Malaga
917#@date    2008-10-27
918#*/
919function ogGetFsSize () {
920if [ $# = 0 ]
921then
922        echo "sintaxis: ogGetFsSize int_disco int_partition str_SizeOutput [ kB MB GB -default GB]-]" red
923        echo "devuelve int_size : int_data : int_free" red
924return
925fi
926if [ $# -ge 2 ]
927then
928        particion=`ogMount $1 $2 ` #1>/dev/null 2>&1
929        if [ -z $3 ]
930                then
931                        unit=kB  # s B kB MB GB TB %
932                else
933                        unit=$3
934        fi
935        case $unit in
936                kB)
937                        factor="1.024";
938                #       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}'`
939                        valor=`df | grep  $particion | awk -F" " '{size=$2*1.024; used=$3*1.024; free=$4*1.024; printf "%d", size}'`
940                ;;
941                MB)
942                        factor="1.024/1000";
943                        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}'`
944                ;;
945                GB)
946                        factor="1.024/1000000";
947                        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}'`
948                ;;
949        esac
950        #echo $valor
951        #NumberRound $valor
952        #valor=`NumberRound $valor`;
953        ogUnmount $1 $2 1>/dev/null 2>&1
954        echo $valor
955
956fi
957}
958
959function ogGetFreeSize () {
960if [ $# = 0 ]
961then
962        echo "sintaxis: ogGetFreeSize int_disco int_partition str_SizeOutput [ kB MB GB -default GB]-]" red
963        echo "devuelve int_size : int_data : int_free" red
964return
965fi
966if [ $# -ge 2 ]
967then
968        particion=`ogMount $1 $2 ` #1>/dev/null 2>&1
969        if [ -z $3 ]
970                then
971                        unit=kB  # s B kB MB GB TB %
972                else
973                        unit=$3
974        fi
975        case $unit in
976                kB)
977                        factor="1.024";
978                        #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}'`
979                        valor=`df | grep  $particion | awk -F" " '{size=$2*1.024; used=$3*1.024; free=$4*1.024; printf "%d", free}'`
980                        ;;
981                MB)
982                        factor="1.024/1000";
983                        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}'`
984                ;;
985                GB)
986                        factor="1.024/1000000";
987                        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}'`
988                ;;
989        esac
990        #echo $valor
991        #NumberRound $valor
992        #valor=`NumberRound $valor`;
993        ogUnmount $1 $2 1>/dev/null 2>&1
994        echo $valor
995
996fi
997}
998
999
1000
1001function ogDeletePartitionsLabels ()
1002{
1003# Si se solicita, mostrar ayuda.
1004if [ "$*" == "help" ]; then
1005    ogHelp "$FUNCNAME" "$FUNCNAME " \
1006           "$FUNCNAME "
1007    return
1008fi
1009rm /dev/disk/by-label/* 2>/dev/null   # */ COMENTARIO OBLIGATORIO PARA DOXYGEN
1010}
1011
1012function ogDeletePartitionTable ()
1013{
1014
1015# Variables locales.
1016local DISK
1017
1018# Si se solicita, mostrar ayuda.
1019if [ "$*" == "help" ]; then
1020    ogHelp "$FUNCNAME int_disk"  \
1021           "$FUNCNAME 1"
1022    return
1023fi
1024# Error si no se reciben 1 parámetros.
1025[ $# == 1 ] || ogRaiseError $OG_ERR_FORMAT || return $?
1026
1027# Obteniendo Identificador linux de la particion.
1028DISK=$(ogDiskToDev $1) || return $?
1029
1030# Elimando las particiones con fdisk
1031echo -ne "o\nw" | fdisk $DISK
1032}
Note: See TracBrowser for help on using the repository browser.