source: client/engine/FileSystem.lib @ 739d358

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 739d358 was 985bef0, checked in by adv <adv@…>, 15 years ago

Adaptando comentarios-histórico al doxygen al engine. ticket:216

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

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