source: client/engine/FileSystem.lib @ 71d382b

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 71d382b was 5a4f399, checked in by ramon <ramongomez@…>, 15 years ago

Corrección en script initCache; nueva función ogIsFormated en rama trunk.

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

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