source: client/engine/Cache.lib @ f23f367

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 f23f367 was 5a4f399, checked in by ramon <ramongomez@…>, 15 years ago

Corrección en script initCache; nueva función ogIsFormated en rama trunk.

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

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