source: client/engine/Cache.lib @ 9f31169

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 9f31169 was d1f50e9, checked in by ramon <ramongomez@…>, 13 years ago

Versión 1.0.4, #526: Funciones de la librería Cache soportan discos GPT.

git-svn-id: https://opengnsys.es/svn/branches/version1.0@3081 a21b9725-9963-47de-94b9-378ad31fedc9

  • Property mode set to 100755
File size: 10.7 KB
Line 
1#!/bin/bash
2#/**
3#@file    Cache.lib
4#@brief   Librería o clase Cache
5#@class   Cache
6#@brief   Funciones para gestión de la caché local de disco.
7#@version 1.0.4
8#@warning License: GNU GPLv3+
9#*/
10
11
12#/**
13#         ogCreateCache int_partsize
14#@brief   Define la caché local en la partición 4 del disco 1
15#@param   int_partsize   tamaño de la partición (en KB)
16#@return  (nada, por determinar)
17#@exception OG_ERR_FORMAT   formato incorrecto.
18#@note    Requisitos: sfdisk, parted, awk, sed
19#@warning El tamaño de caché debe estar entre 50 MB y la mitad del disco.
20#@warning La caché no puede solaparse con las particiones de datos.
21#@version 0.9.1 - Definición de caché local.
22#@author  Ramon Gomez, ETSII Universidad de Sevilla
23#@date    2010/03/09
24#@version 0.9.2 - Corrección definición de límites.
25#@author  Ramon Gomez, ETSII Universidad de Sevilla
26#@date    2010/06/01
27#@version 1.0.4 - Soporte para discos GPT.
28#@author  Universidad de Huelva
29#@date    2012/03/13
30#*/ ##
31function ogCreateCache ()
32{
33# Variables locales.
34local DISK PTTYPE PART START END ENDPREVPART SIZE MINSIZE MAXSIZE
35# Si se solicita, mostrar ayuda.
36if [ "$*" == "help" ]; then
37    ogHelp "$FUNCNAME" "$FUNCNAME int_partsize" "$FUNCNAME 10000000"
38    return
39fi
40# Error si no se recibe 1 parámetro.
41[ $# == 1 ] || ogRaiseError $OG_ERR_FORMAT || return $?
42
43DISK=$(ogDiskToDev 1) || return $?
44PART="4"
45END=$(ogGetLastSector $1)               # Sector final del disco.
46SIZE=$(echo $1|awk '{print $0*2}')      # En sectores de 512 B.
47START=$[END-SIZE+1]
48ENDPREVPART=$(ogGetLastSector $[PART-1])
49# Error si tamaño no está entre límites permitidos o si se solapa con la partición 3.
50MINSIZE=100000                          # Error de formateo si tamaño < 50 MB.
51MAXSIZE=$[END/2]                        # No permitir tamaño > mitad del disco.
52if [ $SIZE -lt $MINSIZE -o $SIZE -gt $MAXSIZE -o $START -le $ENDPREVPART ]; then
53    ogRaiseError $OG_ERR_FORMAT "$1" || return $?
54fi
55
56# Desmontar todos los sistemas de archivos del disco.
57ogUnmountAll 1 2>/dev/null
58# Definir particiones y notificar al kernel.
59# En el caso de ser disco GPT, de momento se borra la particion y se vuelve a crear por
60# lo que se pierden los datos.
61case "$PTTYPE" in
62    GPT)
63        # Si la tabla de particiones no es valida, volver a generarla.
64        [ ! $(sgdisk -p $DISK 2>&1 >/dev/null) ] || echo -e "2\nw\nY\n" | gdisk $DISK
65        # Si existe la cache se borra previamente
66        [ -n $(ogFindCache) && ogDeleteCache
67        # Capturamos el codigo de particion GPT para cache
68        ID=$(ogTypeToId CACHE)
69        sgdisk $DISK -n$PART:$START:$END -c$PART:CACHE -t$PART:$ID 2>/dev/null && partprobe
70        ;;
71    MSDOS)
72        # Si la tabla de particiones no es valida, volver a generarla.
73        [ $(parted -s $DISK print >/dev/null) ] || fdisk $DISK <<< "w"
74        # Definir particiones y notificar al kernel.
75        sfdisk -f $DISK -uS -N$PART <<<"$START,$SIZE,ca" 2>/dev/null && partprobe
76        ;;
77esac
78}
79
80
81#/**
82#         ogDeleteCache
83#@brief   Elimina la partición de caché local.
84#@return  (nada, por determinar)
85#@exception OG_ERR_FORMAT   formato incorrecto.
86#@note    Requisitos: sfdisk, parted, awk, sed
87#@version 0.91 - Definición de caché local.
88#@author  Ramon Gomez, ETSII Universidad de Sevilla
89#@date    2010/03/11
90#@version 1.0.4 - Soporte para discos GPT.
91#@author  Universidad de Huelva
92#@date    2012/03/13
93#*/ ##
94function ogDeleteCache ()
95{
96# Variables locales.
97local NDISK NPART DISK
98# Si se solicita, mostrar ayuda.
99if [ "$*" == "help" ]; then
100    ogHelp "$FUNCNAME" "$FUNCNAME"
101    return
102fi
103# Error si no se encuentra partición de caché.
104read NDISK NPART <<<"$(ogFindCache)"
105[ -n "$NDISK" -a -n "$NPART" ] || ogRaiseError $OG_ERR_PARTITION "$MSG_NOCACHE" || return $?
106DISK=$(ogDiskToDev $NDISK)
107
108# Desmontar todos los sistemas de archivos del disco.
109ogUnmountAll $NDISK 2>/dev/null
110case "$PTTYPE" in
111    GPT)
112        # Si la tabla de particiones no es valida, volver a generarla.
113        [ ! $(sgdisk -p $DISK 2>&1 >/dev/null) ] || echo -e "2\nw\nY\n" | gdisk $DISK
114        sgdisk $DISK -d$NPART 2>/dev/null && partprobe
115        ;;
116    MSDOS)
117        # Si la tabla de particiones no es valida, volver a generarla.
118        [ $(parted -s $DISK print >/dev/null) ] || fdisk $DISK <<< "w"
119        # Eliminar (poner a 0) la partición de caché.
120        sfdisk -f $DISK -N$NPART <<<"0,0,0" 2>/dev/null && partprobe
121        ;;
122esac
123# Borrar etiqueta de la caché.
124rm -f /dev/disk/by-label/CACHE
125}
126
127
128#/**
129#         ogFindCache
130#@brief   Detecta la partición caché local.
131#@param   No requiere parametros
132#@return  int_ndisk int_npart - devuelve el par nº de disco-nº de partición .
133#@warning Si no hay cache no devuelve nada
134#@version 0.1 - Integracion para Opengnsys - EAC: FindCache() en ATA.lib -  HIDRA: DetectarCache.sh
135#@author Ramon Gomez, ETSII Universidad de Sevilla
136#@Date    2008/06/19
137#@author  Antonio J. Doblas Viso. Universidad de Malaga
138#@Date    2008/10/27
139#@version 0.91 - Adaptacion a la cache local de OpenGnSys.
140#@author  Ramon Gomez, ETSII Universidad de Sevilla
141#@date    2010/03/16
142#*/ ##
143function ogFindCache ()
144{
145# Variables locales
146local PART
147# Si se solicita, mostrar ayuda.
148if [ "$*" == "help" ]; then
149    ogHelp "$FUNCNAME" "$FUNCNAME" "$FUNCNAME  =>  1 4"
150    return
151fi
152# Obtener el sistema de archivos etiquetado con "CACHE".
153PART=$(realpath /dev/disk/by-label/CACHE 2>/dev/null)
154# Si no, obtener la 1ª partición marcada como de tipo caché.
155PART=${PART:-$(sfdisk -l | awk '$6~/ca|a7/ {print $1}')}
156PART=${PART%% *}
157
158ogDevToDisk $PART 2>/dev/null
159}
160
161
162#/**
163#         ogFormatCache
164#@brief   Formatea el sistema de ficheros para la caché local.
165#@return  (por determinar)
166#@warning Prueba con formato Reiser.
167#@attention
168#@note    El sistema de archivos de la caché se queda montado.
169#@version 0.1 -  Integracion para Opengnsys  - EAC: FormatCache() en ATA.lib
170#@author  Antonio J. Doblas Viso. Universidad de Malaga
171#@date   2008/10/27
172#@version 0.91 - Creacion cache local.
173#@author  Ramon Gomez, ETSII Universidad de Sevilla
174#@date    2010-03-11
175#*/ ##
176function ogFormatCache ()
177{
178# Variables locales.
179local DEV MNTDIR
180# Si se solicita, mostrar ayuda.
181if [ "$*" == "help" ]; then
182    ogHelp "$FUNCNAME" "$FUNCNAME"
183    return
184fi
185
186# Error si no hay definida partición de caché.
187DEV=$(ogFindCache) || ogRaiseError $OG_ERR_PARTITION "$MSG_NOCACHE" || return $?
188DEV=$(ogDiskToDev $DEV) || return $?
189
190# Formatear sistema de ficheros.
191ogUnmountCache 2>/dev/null
192mkfs.ext4 -q -F $DEV -L "CACHE" -O extent,large_file 2>/dev/null || ogRaiseError $OG_ERR_PARTITION "CACHE" || return $?
193# Crear estructura básica.
194MNTDIR=$(ogMountCache)
195mkdir -p $MNTDIR/$OGIMG
196}
197
198
199#/**
200#         ogGetCacheSize
201#@brief   Devuelve el tamaño definido para la partición de caché.
202#@return  int_partsize   tamaño de la partición (en KB)
203#@exception OG_ERR_PARTITION  No existe partición de caché.
204#@version 0.1 -  Integracion para Opengnsys  -  EAC: InfoCache() en FileSystem.lib
205#@author  Antonio J. Doblas Viso. Universidad de Malaga
206#@date   2008/10/27
207#@version 0.91 - Definicion de cache local.
208#@author  Ramon Gomez, ETSII Universidad de Sevilla
209#@date    2010/03/09
210#*/ ##
211function ogGetCacheSize ()
212{
213# Variables locales
214local PART
215
216# Si se solicita, mostrar ayuda.
217if [ "$*" == "help" ]; then
218    ogHelp "$FUNCNAME" "$FUNCNAME" "$FUNCNAME  =>  10000000"
219    return
220fi
221# Error si no se encuentra partición de caché.
222PART=$(ogFindCache) || ogRaiseError $OG_ERR_PARTITION "$MSG_NOCACHE" || return $?
223
224# Devuelve tamaño de la partición de caché.
225ogGetPartitionSize $PART
226}
227
228
229#/**
230#         ogGetCacheSpace
231#@brief   Devuelve el espacio de disco disponible para la partición de caché.
232#@return  int_size   tamaño disponible (en KB)
233#@note    El espacio disponible es el que hay entre el límite superior de la partición 3 del disco 1 y el final de dicho disco, y no puede ser superior a la mitad de dicho disco.
234#@version 0.1 -  Integracion para Opengnsys  -  EAC: InfoCache() en FileSystem.lib
235#@author  Antonio J. Doblas Viso. Universidad de Malaga
236#@date   2008/10/27
237#@version 0.91 - Definicion de cache local.
238#@author  Ramon Gomez, ETSII Universidad de Sevilla
239#@date    2010/03/09
240#*/ ##
241function ogGetCacheSpace ()
242{
243
244# Variables locales.
245local DISK SECTORS CYLS ENDPART3
246# Si se solicita, mostrar ayuda.
247if [ "$*" == "help" ]; then
248    ogHelp "$FUNCNAME" "$FUNCNAME" "$FUNCNAME  =>  23165386"
249    return
250fi
251
252DISK=$(ogDiskToDev 1) || return $?
253SECTORS=$(awk -v D=${DISK#/dev/} '{if ($4==D) {print $3*2}}' /proc/partitions)
254CYLS=$(sfdisk -g $DISK | cut -f2 -d" ")
255SECTORS=$[SECTORS/CYLS*CYLS-1]
256ENDPART3=$(sfdisk -uS -l $DISK | awk -v P="${DISK}3" '{if ($1==P) print $3}')
257# Mostrar espacio libre en KB (1 KB = 2 sectores)
258if [ $ENDPART3 -gt $[SECTORS/2] ]; then
259    echo $[(SECTORS-ENDPART3)/2]
260else
261    echo $[SECTORS/4]
262fi
263}
264
265
266#/**
267#         ogMountCache
268#@brief   Monta la partición Cache y exporta la variable $OGCAC
269#@param   sin parametros
270#@return  path_mountpoint - Punto de montaje del sistema de archivos de cache.
271#@warning Salidas de errores no determinada
272#@version 0.1 -  Integracion para Opengnsys  -  EAC: MountCache() en FileSystem.lib - HIDRA: MontarCache.sh
273#@author  Ramon Gomez, ETSII Universidad de Sevilla
274#@date    2008/06/19
275#@author  Antonio J. Doblas Viso. Universidad de Malaga
276#@Date    2008/10/27
277#@version 0.91 - Adaptacion a la cache local de OpenGnSys.
278#@author  Ramon Gomez, ETSII Universidad de Sevilla
279#@date    2010/03/16
280#@version 1.0 - Correccion multiples montajes de cache.
281#@author  Antonio J. Doblas Viso, Universidad de Malaga
282#@date    2011/02/24
283#*/ ##
284function ogMountCache ()
285{
286# Si se solicita, mostrar ayuda.
287if [ "$*" == "help" ]; then
288    ogHelp "$FUNCNAME" "$FUNCNAME" "$FUNCNAME  ==>  /mnt/sda4"
289    return
290fi
291
292ogMountFs $(ogFindCache) 2>/dev/null || ogRaiseError $OG_ERR_PARTITION "$MSG_NOCACHE" || return $?
293}
294
295
296#/**
297#         ogUnmountCache
298#@brief   Desmonta la particion Cache y elimina la variable $OGCAC
299#@param   sin parametros
300#@return  nada
301#@warning Salidas de errores no determinada
302#@version 0.1 -  Integracion para Opengnsys  -  EAC: UmountCache() en FileSystem.lib
303#@author  Antonio J. Doblas Viso. Universidad de Malaga
304#@Date    2008/10/27
305#@version 0.91 - Adaptacion a la cache local de OpenGnSys.
306#@author  Ramon Gomez, ETSII Universidad de Sevilla
307#@date    2010/03/16
308#@version 1.0 - Correccion multiples montajes de cache.
309#@author  Antonio J. Doblas Viso, Universidad de Malaga
310#@date    2011/02/24
311#*/ ##
312function ogUnmountCache ()
313{
314# Variables locales.
315local CACHE
316# Si se solicita, mostrar ayuda.
317if [ "$*" == "help" ]; then
318    ogHelp "$FUNCNAME" "$FUNCNAME"
319    return
320fi
321
322CACHE=$(ogFindCache) || ogRaiseError $OG_ERR_PARTITION "$MSG_NOCACHE"
323ogIsMounted $CACHE || return 0
324ogUnmountFs $CACHE
325# Borrar enlace simbólico de /mnt/ParticiónCache.
326rm -f $(ogDiskToDev $CACHE | sed 's/dev/mnt/')
327}
328
Note: See TracBrowser for help on using the repository browser.