source: client/engine/FileSystem.lib @ e05993a

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 e05993a was 42669ebf, checked in by ramon <ramongomez@…>, 15 years ago

Funciones adaptadas a plantilla Doxygen; nueva función ogFsToId.

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

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