source: client/engine/Cache.lib @ 1222da75

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 1222da75 was 4c475a32, checked in by ramon <ramongomez@…>, 15 years ago

Solución ticket:169 - cargar módulos de discos en arranque del cliente.

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

  • Property mode set to 100755
File size: 8.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 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 - Versión adaptada del Proyecto EAC.
106#@author  Antonio J. Doblas Viso. Universidad de Malaga
107#@Date    27/10/2008
108#@version 0.91 - Adaptación a la caché local de OpenGnSys.
109#@author  Ramon Gomez, ETSII Universidad de Sevilla
110#@date    2010/03/16
111#*/ ##
112function ogFindCache ()
113{
114# Variables locales
115local PART
116# Si se solicita, mostrar ayuda.
117if [ "$*" == "help" ]; then
118    ogHelp "$FUNCNAME" "$FUNCNAME" "$FUNCNAME  =>  1 4"
119    return
120fi
121# Obtener el sistema de archivos etiquetado con "CACHE".
122PART=$(realpath /dev/disk/by-label/CACHE 2>/dev/null)
123# Si no, obtener la 1ª partición marcada como de tipo caché.
124PART=${PART:-$(sfdisk -l | awk '$6~/ca|a7/ {print $1}')}
125PART=${PART%% *}
126
127ogDevToDisk $PART 2>/dev/null
128}
129
130
131#/**
132#         ogFormatCache
133#@brief   Formatea el sistema de ficheros para la caché local.
134#@return  (por determinar)
135#@warning Prueba con formato Reiser.
136#@attention
137#@note    El sistema de archivos de la caché se queda montado.
138#@version 0.1 - Versión adaptada del Proyecto EAC.
139#@author  Antonio J. Doblas Viso. Universidad de Malaga
140#@date    27/10/2008
141#@version 0.91 - Creación caché local.
142#@author  Ramon Gomez, ETSII Universidad de Sevilla
143#@date    2010-03-11
144#*/ ##
145function ogFormatCache ()
146{
147# Variables locales.
148local DEV MNTDIR
149# Si se solicita, mostrar ayuda.
150if [ "$*" == "help" ]; then
151    ogHelp "$FUNCNAME" "$FUNCNAME"
152    return
153fi
154
155# Error si no hay definida partición de caché.
156DEV=$(ogFindCache) || ogRaiseError $OG_ERR_PARTITION "$MSG_NOCACHE" || return $?
157DEV=$(ogDiskToDev $DEV) || return $?
158
159# Formatear sistema de ficheros.
160ogUnmountCache 2>/dev/null
161           # Orden para formateo Reiser 3
162mkfs.reiserfs -f $DEV -l "CACHE" 2>/dev/null || ogRaiseError $OG_ERR_PARTITION "CACHE" || return $?
163           # Orden para formateo XFS
164           #mkfs.xfs $DEV -L "CACHE" || ogRaiseError $OG_ERR_PARTITION "CACHE" || return $?
165# Crear estructura básica
166MNTDIR=$(ogMountCache)
167mkdir -p $MNTDIR/$OGIMG
168}
169
170
171#/**
172#         ogGetCacheSize
173#@brief   Devuelve el tamaño definido para la partición de caché.
174#@return  int_partsize   tamaño de la partición (en KB)
175#@exception OG_ERR_PARTITION  No existe partición de caché.
176#@version 0.91 - Definición de caché local.
177#@author  Ramon Gomez, ETSII Universidad de Sevilla
178#@date    2010/03/09
179#*/ ##
180function ogGetCacheSize ()
181{
182# Variables locales
183local PART
184
185# Si se solicita, mostrar ayuda.
186if [ "$*" == "help" ]; then
187    ogHelp "$FUNCNAME" "$FUNCNAME" "$FUNCNAME  =>  10000000"
188    return
189fi
190# Error si no se encuentra partición de caché.
191PART=$(ogFindCache) || ogRaiseError $OG_ERR_PARTITION "$MSG_NOCACHE" || return $?
192
193# Devuelve tamaño de la partición de caché.
194ogGetPartitionSize $PART
195}
196
197
198#/**
199#         ogGetCacheSpace
200#@brief   Devuelve el espacio de disco disponible para la partición de caché.
201#@return  int_size   tamaño disponible (en KB)
202#@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.
203#@version 0.91 - Definición de caché local.
204#@author  Ramon Gomez, ETSII Universidad de Sevilla
205#@date    2010/03/09
206#*/ ##
207function ogGetCacheSpace ()
208{
209
210# Variables locales.
211local DISK SECTORS CYLS ENDPART3
212# Si se solicita, mostrar ayuda.
213if [ "$*" == "help" ]; then
214    ogHelp "$FUNCNAME" "$FUNCNAME" "$FUNCNAME  =>  23165386"
215    return
216fi
217
218DISK=$(ogDiskToDev 1) || return $?
219SECTORS=$(awk -v D=${DISK#/dev/} '{if ($4==D) {print $3*2}}' /proc/partitions)
220CYLS=$(sfdisk -g $DISK | cut -f2 -d" ")
221SECTORS=$[SECTORS/CYLS*CYLS-1]
222ENDPART3=$(sfdisk -uS -l $DISK | awk -v P="${DISK}3" '{if ($1==P) print $3}')
223# Mostrar espacio libre en KB (1 KB = 2 sectores)
224if [ $ENDPART3 -gt $[SECTORS/2] ]; then
225    echo $[(SECTORS-ENDPART3)/2]
226else
227    echo $[SECTORS/4]
228fi
229}
230
231
232#/**
233#         ogMountCache
234#@brief   Monta la partición Cache y exporta la variable $OGCAC
235#@param   sin parametros
236#@return  path_mountpoint - Punto de montaje del sistema de archivos de cache.
237#@warning Salidas de errores no determinada
238#@version 0.1 - Versión adaptada del Proyecto EAC.
239#@author  Antonio J. Doblas Viso. Universidad de Malaga
240#@Date    27/10/2008
241#@version 0.91 - Adaptación a la caché local de OpenGnSys.
242#@author  Ramon Gomez, ETSII Universidad de Sevilla
243#@date    2010/03/16
244#*/ ##
245function ogMountCache ()
246{
247# Si se solicita, mostrar ayuda.
248if [ "$*" == "help" ]; then
249    ogHelp "$FUNCNAME" "$FUNCNAME" "$FUNCNAME  ==>  /mnt/sda4"
250    return
251fi
252
253OGCAC=$(ogMountFs $(ogFindCache) 2>/dev/null) || ogRaiseError $OG_ERR_PARTITION "$MSG_NOCACHE" || return $?
254echo $OGCAC
255export OGCAC
256}
257
258
259#/**
260#         ogUnmountCache
261#@brief   Desmonta la particion Cache y elimina la variable $OGCAC
262#@param   sin parametros
263#@return  nada
264#@warning Salidas de errores no determinada
265#@version 0.1 - Versión adaptada del Proyecto EAC.
266#@author  Antonio J. Doblas Viso. Universidad de Malaga
267#@Date    27/10/2008
268#@version 0.91 - Adaptación a la caché local de OpenGnSys.
269#@author  Ramon Gomez, ETSII Universidad de Sevilla
270#@date    2010/03/16
271#*/ ##
272function ogUnmountCache ()
273{
274# Variables locales.
275local CACHE
276# Si se solicita, mostrar ayuda.
277if [ "$*" == "help" ]; then
278    ogHelp "$FUNCNAME" "$FUNCNAME"
279    return
280fi
281
282CACHE=$(ogFindCache) || ogRaiseError $OG_ERR_PARTITION "$MSG_NOCACHE"
283ogUnmountFs $CACHE
284# Borrar enlace simbólico de /mnt/ParticiónCache.
285rm -f $(ogDiskToDev $CACHE | sed 's/dev/mnt/')
286}
287
Note: See TracBrowser for help on using the repository browser.