source: client/engine/Cache.lib @ 5e1020c

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 5e1020c was 92a6c37, checked in by adv <adv@…>, 14 years ago

Cache.lib 1.0 correccion multiples montajes/desmontajes de la cache.

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

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