source: client/engine/FileSystem.lib @ 308f61c

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 308f61c was e068946, checked in by ramon <ramongomez@…>, 13 years ago

Versión 1.0.4, #518: Corregir errata en función ogFormatFs aplicacando cambios de revisión r2998.

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