source: client/engine/FileSystem.lib @ ead38fb

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 ead38fb was 311532f, checked in by ramon <ramongomez@…>, 15 years ago

Creada clase de funciones Cache.

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

  • Property mode set to 100755
File size: 25.2 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#/**
353#         ogIsMounted int_ndisk int_npartition
354#@brief   Comprueba si un sistema de archivos está montado.
[42669ebf]355#@param   int_ndisk      nº de orden del disco
356#@param   int_npartition nº de orden de la partición
[f8f4dfa]357#@return  Código de salida: 0 - sin montar, 1 - montado.
358#@version 0.9 - Primera versión para OpenGNSys.
359#@author  Ramon Gomez, ETSII Universidad de Sevilla
360#@date    2009-10-15
[1e7eaab]361#*/ ##
[42669ebf]362function ogIsMounted ()
363{
364# Si se solicita, mostrar ayuda.
[a3348ce]365if [ "$*" == "help" ]; then
366    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
367           "if $FUNCNAME 1 1; then ... ; fi"
368    return
369fi
[1e7eaab]370# Error si no se reciben 2 parámetros.
[a3348ce]371[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
372
373test -n "$(ogGetMountPoint $1 $2)"
374}
375
376
[aae34f6]377#/**
[9d96c57]378#         ogIsLocked int_ndisk int_npartition
379#@brief   Comprueba si una partición está bloqueada por una operación de uso exclusivo.
[42669ebf]380#@param   int_ndisk      nº de orden del disco
381#@param   int_npartition nº de orden de la partición
[f8f4dfa]382#@return  Código de salida: 0 - sin bloquear, 1 - bloqueada.
[73c8417]383#@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]384#@version 0.9 - Primera versión para OpenGNSys.
[9d96c57]385#@author  Ramon Gomez, ETSII Universidad de Sevilla
386#@date    2009-09-03
[1e7eaab]387#*/ ##
[42669ebf]388function ogIsLocked ()
389{
[59f9ad2]390# Variables locales
[73c8417]391local PART LOCKFILE
[9d96c57]392
[1e7eaab]393# Si se solicita, mostrar ayuda.
[9d96c57]394if [ "$*" == "help" ]; then
395    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
[a3348ce]396           "if $FUNCNAME 1 1; then ... ; fi"
[9d96c57]397    return
398fi
[1e7eaab]399# Error si no se reciben 2 parámetros.
[9d96c57]400[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
401
[1e7eaab]402# Obtener partición.
[9d96c57]403PART="$(ogDiskToDev $1 $2)" || return $?
404
[1e7eaab]405# Comprobar existencia del fichero de bloqueo.
[73c8417]406LOCKFILE="/var/lock/lock${PART//\//-}"
407test -f $LOCKFILE
[9d96c57]408}
409
410
411#/**
412#         ogLock int_ndisk int_npartition
[89403cd]413#@see     ogLockPartition
414#*/
[42669ebf]415function ogLock ()
416{
[89403cd]417ogLockPartition "$@"
418}
419
420#/**
421#         ogLockPartition int_ndisk int_npartition
[9d96c57]422#@brief   Genera un fichero de bloqueo para una partición en uso exlusivo.
[42669ebf]423#@param   int_ndisk      nº de orden del disco
424#@param   int_npartition nº de orden de la partición
[9d96c57]425#@return  (nada)
426#@exception OG_ERR_FORMAT    Formato incorrecto.
427#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
[73c8417]428#@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]429#@version 0.9 - Primera versión para OpenGNSys.
[9d96c57]430#@author  Ramon Gomez, ETSII Universidad de Sevilla
431#@date    2009-09-03
[1e7eaab]432#*/ ##
[42669ebf]433function ogLockPartition ()
434{
[59f9ad2]435# Variables locales
436local PART LOCKFILE
[9d96c57]437
[1e7eaab]438# Si se solicita, mostrar ayuda.
[9d96c57]439if [ "$*" == "help" ]; then
440    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
441           "$FUNCNAME 1 1"
442    return
443fi
[1e7eaab]444# Error si no se reciben 2 parámetros.
[9d96c57]445[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
446
[1e7eaab]447# Obtener partición.
[9d96c57]448PART="$(ogDiskToDev $1 $2)" || return $?
449
[1e7eaab]450# Crear archivo de bloqueo exclusivo.
[73c8417]451LOCKFILE="/var/lock/lock${PART//\//-}"
452touch $LOCKFILE
[9d96c57]453}
454
455
456#/**
[aae34f6]457#         ogMount int_ndisk int_npartition
[3458879]458#@see     ogMountFs ogMountCache ogMountCdrom
[1e7eaab]459#*/ ##
[42669ebf]460function ogMount ()
461{
[ee4a96e]462case "$*" in
[18f4bc2]463    CACHE|cache)
464        ogMountCache ;;
[ee4a96e]465    CDROM|cdrom)
466        ogMountCdrom ;;
467    *)  ogMountFs "$@" ;;
468esac
[73c8417]469}
470
[ee4a96e]471
[73c8417]472#/**
[40488da]473#         ogMountFs int_ndisk int_npartition
[aae34f6]474#@brief   Monta un sistema de archivos.
[42669ebf]475#@param   int_ndisk      nº de orden del disco
476#@param   int_npartition nº de orden de la partición
[aae34f6]477#@return  Punto de montaje
478#@exception OG_ERR_FORMAT    Formato incorrecto.
479#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
480#@exception OG_ERR_PARTITION Tipo de particion desconocido o no se puede montar.
[40488da]481#@version 0.9 - Primera versión para OpenGNSys.
[aae34f6]482#@author  Ramon Gomez, ETSII Universidad de Sevilla
[40488da]483#@date    2009-09-28
[1e7eaab]484#*/ ##
[42669ebf]485function ogMountFs ()
486{
[1e7eaab]487# Variables locales
[049eadbe]488local PART TYPE MNTDIR MOUNT PARAMS
[aae34f6]489
[1e7eaab]490# Si se solicita, mostrar ayuda.
[aae34f6]491if [ "$*" == "help" ]; then
492    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
493           "$FUNCNAME 1 1  =>  /mnt/sda1"
494    return
495fi
[1e7eaab]496# Error si no se reciben 2 parámetros.
[aae34f6]497[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
498
[1e7eaab]499# Obtener partición.
[aae34f6]500PART="$(ogDiskToDev $1 $2)" || return $?
501
[1e7eaab]502# Comprobar si el sistema de archivos ya está montada.
[a3348ce]503MNTDIR="$(ogGetMountPoint $1 $2)"
[1e7eaab]504# Si no, montarla en un directorio de sistema
[aae34f6]505if [ -z "$MNTDIR" ]; then
506    # Error si la particion esta bloqueada.
[59f9ad2]507    ogIsLocked $1 $2 && ogRaiseError $OG_ERR_LOCKED "$1 $2" && return $?
[1e7eaab]508    # Crear punto de montaje.
[aae34f6]509    MNTDIR=${PART/dev/mnt}
510    mkdir -p $MNTDIR
[1e7eaab]511    # Montar según el tipo de sitema de archivos.
[aae34f6]512    TYPE="$(ogGetFsType $1 $2)" || return $?
513    case "$TYPE" in
[cb167ee0]514        CACHE)      MOUNT=mount ;;
515        EXT[234])   MOUNT=mount ;;
516        REISERFS)   insmod /lib/modules/$(uname -r)/kernel/fs/reiserfs/reiserfs.ko 2>/dev/null
[7250491]517                    MOUNT=mount ;;
[cb167ee0]518        JFS)        insmod /lib/modules/$(uname -r)/kernel/fs/jfs/jfs.ko 2>/dev/null
[7250491]519                    MOUNT=mount ;;
[cb167ee0]520        XFS)        insmod /lib/modules/$(uname -r)/kernel/fs/xfs/xfs.ko 2>/dev/null
[7250491]521                    MOUNT=mount ;;
[cb167ee0]522        NTFS|HNTFS) MOUNT=ntfs-3g ;;
[b550747]523        FAT16|FAT32|HFAT16|HFAT32)
[049eadbe]524                    MOUNT=mount; PARAMS="-t vfat" ;;
[5dbb046]525        *)  #/// Error, si la partición no es montable.
[aae34f6]526            rmdir $MNTDIR
527            ogRaiseError $OG_ERR_PARTITION "$1, $2, $TYPE"
[3458879]528            return $OG_ERR_PARTITION
529            ;;
[aae34f6]530    esac
[049eadbe]531    $MOUNT $PARAMS $PART $MNTDIR || $MOUNT $PARAMS $PART $MNTDIR -o force,remove_hiberfile || ogRaiseError $OG_ERR_PARTITION "$1, $2, $TYPE" || return $?
[f5a77d7]532        # linea temporal durante desarrollo para poder usar el cliente completo nfs y testeas nuevas herramientas.
[40488da]533    if grep -q nfsroot /proc/cmdline; then
534                echo "$PART $MNTDIR" >> /etc/mtab
[f5a77d7]535        fi
536        # fin linea temporal.
[aae34f6]537fi
538echo $MNTDIR
539}
540
541
[ee4a96e]542#####  PRUEBAS
543# Montar CDROM
[42669ebf]544function ogMountCdrom ()
545{
[ee4a96e]546local DEV MNTDIR
547DEV="/dev/cdrom"            # Por defecto
548MNTDIR=$(mount | awk -v D=$DEV '{if ($1==D) {print $3}}')
549if [ -z "$MNTDIR" ]; then
550    MNTDIR=${DEV/dev/mnt}
551    mkdir -p $MNTDIR
552    mount -t iso9660 $DEV $MNTDIR || ogRaiseError $OG_ERR_PARTITION "cdrom" || return $?
553fi
554echo $MNTDIR
555}
556
[3f49cf7]557
558#/**
559#         ogReduceFs int_ndisk int_npartition
560#@brief   Reduce el tamaño del sistema de archivos, sin tener en cuenta el espacio libre.
[42669ebf]561#@param   int_ndisk      nº de orden del disco
562#@param   int_npartition nº de orden de la partición
[3f49cf7]563#@return  tamañoKB
564#@exception OG_ERR_FORMAT    Formato incorrecto.
565#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
566#@exception OG_ERR_PARTITION Partición desconocida o no accesible.
[3458879]567#@warning En Windows, se borran los ficheros pagefile.sys e hiberfile.sys
568#@warning El sistema de archivos se amplía al mínimo + 1 KB.
569#@note    Requisitos:   *resize*
570#@version 0.9 - Primera versión para OpenGNSys.
[3f49cf7]571#@author  Ramon Gomez, ETSII Universidad de Sevilla
572#@date    2009-09-23
[1e7eaab]573#*/ ##
[42669ebf]574function ogReduceFs ()
575{
[3f49cf7]576# Variables locales
577local PART BLKS SIZE
578
[1e7eaab]579# Si se solicita, mostrar ayuda.
[3f49cf7]580if [ "$*" == "help" ]; then
581    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
582           "$FUNCNAME 1 1"
583    return
584fi
[1e7eaab]585# Error si no se reciben 2 parámetros.
[3f49cf7]586[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
587
[1e7eaab]588# Obtener partición.
[3f49cf7]589PART="$(ogDiskToDev $1 $2)" || return $?
590
[1e7eaab]591# Redimensionar según el tipo de particion.
[3f49cf7]592case "$(ogGetFsType $1 $2)" in
[1c04494]593    EXT[234])
[3458879]594        ogUnmount $1 $2 2>/dev/null
[1c04494]595        # Ext2/3/4: Tamaño de los bloques del sistema de archivos
596        BLKS=$(tune2fs -l $PART | awk '/Block size/ {print int($3/512)}')
597        # Traduce el num. en sectores de 512B a tamano en MB.
598        SIZE=$(resize2fs -P $PART 2>/dev/null | \
599                       awk -v B=$BLKS '/minimum size/ {print int($7*B/2048+1000)}')
600        resize2fs -fp $PART "${SIZE}M" &>/dev/null || ogRaiseError $OG_ERR_PARTITION "$1,$2" || return $?
601            ;;
[c7d9af7]602#    REISERFS)          # Usar "resize_reiserfs"
603#           ;;
[1c04494]604    NTFS|HNTFS)
[3458879]605        ogDeleteFile $1 $2 pagefile.sys
606        ogDeleteFile $1 $2 hiberfile.sys
607        ogUnmount $1 $2 2>/dev/null
[1c04494]608        # NTFS: Obtiene tamaño mínimo en MB.
609        SIZE=$(ntfsresize -fi $PART | awk '/resize at/ {print $8+1000}')
610        ntfsresize -fns "${SIZE}M" $PART >/dev/null || ogRaiseError $OG_ERR_PARTITION "$1,$2" || return $?
611        ntfsresize -fs "${SIZE}M" $PART <<<"y" >/dev/null || ogRaiseError $OG_ERR_PARTITION "$1,$2" || return $?
612            ;;
613    *)  ogRaiseError $OG_ERR_PARTITION "$1,$2"
614            return $? ;;
[3f49cf7]615esac
616#/// Mostrar nuevo tamaño en KB.
617echo $[SIZE*1024]
618}
619
620
[5dbb046]621#/**
[9d96c57]622#         ogUnlock int_ndisk int_npartition
[89403cd]623#@see     ogUnlockPartition
624#*/
[42669ebf]625function ogUnlock ()
626{
[89403cd]627ogUnlockPartition "$@"
628}
629
630#/**
631#         ogUnlockPartition int_ndisk int_npartition
[9d96c57]632#@brief   Elimina el fichero de bloqueo para una particion.
[42669ebf]633#@param   int_ndisk      nº de orden del disco
634#@param   int_npartition nº de orden de la partición
[9d96c57]635#@return  (nada)
636#@exception OG_ERR_FORMAT    Formato incorrecto.
637#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
[73c8417]638#@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]639#@version 0.9 - Primera versión para OpenGNSys.
[9d96c57]640#@author  Ramon Gomez, ETSII Universidad de Sevilla
641#@date    2009-09-03
[1e7eaab]642#*/ ##
[42669ebf]643function ogUnlockPartition ()
644{
[59f9ad2]645# Variables locales
646local PART LOCKFILE
[9d96c57]647
[1e7eaab]648# Si se solicita, mostrar ayuda.
[9d96c57]649if [ "$*" == "help" ]; then
650    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \
651           "$FUNCNAME 1 1"
652    return
653fi
[1e7eaab]654# Error si no se reciben 2 parámetros.
[9d96c57]655[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
656
[1e7eaab]657# Obtener partición.
[9d96c57]658PART="$(ogDiskToDev $1 $2)" || return $?
659
[1e7eaab]660# Borrar archivo de bloqueo exclusivo.
[73c8417]661LOCKFILE="/var/lock/lock${PART//\//-}"
662rm -f $LOCKFILE
[9d96c57]663}
664
665
666#/**
[5dbb046]667#         ogUnmount int_ndisk int_npartition
[40488da]668#@see     ogUnmountFs
[89403cd]669#*/
[42669ebf]670function ogUnmount ()
671{
[40488da]672ogUnmountFs "$@"
[89403cd]673}
674
675#/**
[40488da]676#         ogUnmountFs int_ndisk int_npartition
[5dbb046]677#@brief   Desmonta un sistema de archivos.
[42669ebf]678#@param   int_ndisk      nº de orden del disco
679#@param   int_npartition nº de orden de la partición
[5dbb046]680#@return  Nada
681#@exception OG_ERR_FORMAT    Formato incorrecto.
682#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
683#@warning La partición no está previamente montada o no se puede desmontar.
[40488da]684#@version 0.9 - Primera versión para OpenGNSys.
[5dbb046]685#@author  Ramon Gomez, ETSII Universidad de Sevilla
[40488da]686#@date    2009-09-28
[1e7eaab]687#*/ ##
[42669ebf]688function ogUnmountFs ()
689{
[59f9ad2]690# Variables locales
[c56dec5]691local PART MNTDIR
[5dbb046]692
[1e7eaab]693# Si se solicita, mostrar ayuda.
[5dbb046]694if [ "$*" == "help" ]; then
695    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" "$FUNCNAME 1 1"
696    return
697fi
[1e7eaab]698# Error si no se reciben 2 parámetros.
[5dbb046]699[ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $?
700
[1e7eaab]701# Obtener partición y punto de montaje.
[5dbb046]702PART="$(ogDiskToDev $1 $2)" || return $?
[a3348ce]703MNTDIR="$(ogGetMountPoint $1 $2)"
[5dbb046]704
[1e7eaab]705# Si está montada, desmontarla.
[c56dec5]706if [ -n "$MNTDIR" ]; then
[5dbb046]707    # Error si la particion esta bloqueada.
[59f9ad2]708    ogIsLocked $1 $2 && ogRaiseError $OG_ERR_LOCKED "$1 $2" && return $?
[1e7eaab]709    # Crear punto de montaje.
[5dbb046]710    umount $PART 2>/dev/null && rmdir $MNTDIR || ogEcho warning "$FUNCNAME: $MSG_DONTUNMOUNT: \"$1,$2\""
[5ef521e]711        # linea temporal durante desarrollo para testear nuevas herramientas con el cliente completo nfs
[40488da]712    if grep -q nfsroot /proc/cmdline; then
[5ef521e]713                cat /etc/mtab | grep -v $PART > /var/tmp/mtab.temporal && cp /var/tmp/mtab.temporal /var/tmp/mtab && rm /var/tmp/mtab.temporal
714        fi
715        # fin linea temporal.
[5dbb046]716else
717    ogEcho warning "$MSG_DONTMOUNT: \"$1,$2\""
718fi
719}
720
[ee4a96e]721
[be81649]722#/**
723#         ogUnmountAll int_ndisk
724#@brief   Desmonta todos los sistema de archivos de un disco, excepto el caché local.
[42669ebf]725#@param   int_ndisk      nº de orden del disco
[be81649]726#@return  Nada
727#@exception OG_ERR_FORMAT    Formato incorrecto.
728#@exception OG_ERR_NOTFOUND  Disco o particion no corresponden con un dispositivo.
729#@warning No se desmonta la partición marcada como caché local.
730#@version 0.9 - Versión para OpenGNSys.
731#@author  Ramon Gomez, ETSII Universidad de Sevilla
732#@date    2009/10/07
[1e7eaab]733#*/ ##
[42669ebf]734function ogUnmountAll ()
735{
[be81649]736# Variables locales
[18f4bc2]737local DISK PART
[1e7eaab]738# Si se solicita, mostrar ayuda.
[18f4bc2]739if [ "$*" == "help" ]; then
740    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk" "FUNCNAME 1"
741    return
742fi
[1e7eaab]743# Error si no se recibe 1 parámetro.
[18f4bc2]744[ $# == 1 ] || ogRaiseError $OG_ERR_FORMAT || return $?
745
[1e7eaab]746# Obtener partición y punto de montaje.
[18f4bc2]747DISK="$(ogDiskToDev $1)" || return $?
748for ((PART=1; PART<=$(ogGetPartitionsNumber $1); PART++)); do
749    case "$(ogGetFsType $1 $PART)" in
750        CACHE|LINUX-SWAP)
751           ;;
752        *)
753           ogUnmount $1 $PART ;;
[7250491]754    esac
[18f4bc2]755done
756}
757
758
[a957f02]759function ogGetFsSize () {
760#/**  @function ogGetFsSize: @brief Muestra el tamanio del sistema de archivos indicado, permite definir la unidad de medida, por defecto GB
761#@param  $1 int_diskEAC
762#@param $2 int_PartitionEAC
763#@param $3 str_UnidadMediada            parametro opcional, admite [ kB MB GB -default GB]
764#@return  cadena con int_TotalSize:int_DataSize:int_DataFree
765#@warning  Salidas de errores no determinada
766#@warning
767#@attention
768#@version 1.0   Date: 27/10/2008 Author Antonio J. Doblas Viso. Universidad de Malaga
769#*/
770if [ $# = 0 ]
771then
772        echo "sintaxis: ogGetFsSize int_disco int_partition str_SizeOutput [ kB MB GB -default GB]-]" red
773        echo "devuelve int_size : int_data : int_free" red
774return
775fi
776if [ $# -ge 2 ]
777then
778        particion=`ogMount $1 $2 ` #1>/dev/null 2>&1
779        if [ -z $3 ]
780                then
781                        unit=GB  # s B kB MB GB TB %
782                else
783                        unit=$3
784        fi
785        case $unit in
786                kB)
787                        factor="1.024";
788                        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}'`
789                ;;
790                MB)
791                        factor="1.024/1000";
792                        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}'`
793                ;;
794                GB)
795                        factor="1.024/1000000";
796                        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}'`
797                ;;
798        esac
799        #echo $valor
800        #NumberRound $valor
801        #valor=`NumberRound $valor`;
802        ogUnmount $1 $2 1>/dev/null 2>&1
803        echo $valor
804
805fi
806
807}
Note: See TracBrowser for help on using the repository browser.