source: client/engine/Cache.lib @ b1f1311

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

Versión 1.0.4, #526: Corregida errata en función ogCreateCache para compatibilidad con discos GPT.

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

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