source: client/engine/Cache.lib @ a0da76f

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

Versión 1.0.4, #543: Función ogCreateCache para discos vacíos; corregir varias erratas en motor de clonación.

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

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