source: client/engine/FileSystem.lib @ e4dafd6

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

Versión 1.0.2: revisar variables locales y ayudas de las funciones del motor de clonación (modifica #446).

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

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