opengnsys/client/engine/Cache.lib

298 lines
9.5 KiB
Bash

#!/bin/bash
#/**
#@file Cache.lib
#@brief Librería o clase Cache
#@class Cache
#@brief Funciones para gestión de la caché local de disco.
#@version 0.9.1
#@warning License: GNU GPLv3+
#*/
#/**
# ogCreateCache int_partsize
#@brief Define la caché local en la partición 4 del disco 1
#@param int_partsize tamaño de la partición (en KB)
#@return (nada, por determinar)
#@exception OG_ERR_FORMAT formato incorrecto.
#@note Requisitos: sfdisk, parted, awk, sed
#@warning El tamaño de caché debe estar entre 50 MB y la mitad del disco.
#@warning La caché no puede solaparse con las particiones de datos.
#@version 0.9.1 - Definición de caché local.
#@author Ramon Gomez, ETSII Universidad de Sevilla
#@date 2010/03/09
#@version 0.9.2 - Corrección definición de límites.
#@author Ramon Gomez, ETSII Universidad de Sevilla
#@date 2010/06/01
#*/ ##
function ogCreateCache ()
{
# Variables locales.
local DISK PART SECTORS CYLS START END SIZE MINSIZE MAXSIZE ENDPART3
# Si se solicita, mostrar ayuda.
if [ "$*" == "help" ]; then
ogHelp "$FUNCNAME" "$FUNCNAME int_partsize" "$FUNCNAME 10000000"
return
fi
# Error si no se recibe 1 parámetro.
[ $# == 1 ] || ogRaiseError $OG_ERR_FORMAT || return $?
DISK=$(ogDiskToDev 1) || return $?
PART="4"
SECTORS=$(awk -v D=${DISK#/dev/} '{if ($4==D) {print $3*2}}' /proc/partitions)
CYLS=$(sfdisk -g $DISK | cut -f2 -d" ")
END=$[SECTORS/CYLS*CYLS-1] # Sector final del disco
SIZE=$(echo $1|awk '{print $0*2}') # En sectores de 512 B.
START=$[END-SIZE+1]
ENDPART3=$(sfdisk -uS -l $DISK | awk -v P="${DISK}3" '{if ($1==P) print $3}')
# Error si tamaño no está entre límites permitidos o si se solapa con la partición 3.
MINSIZE=100000 # Error de formateo si tamaño < 50 MB.
MAXSIZE=$[END/2] # No permitir tamaño > mitad del disco.
if [ $SIZE -lt $MINSIZE -o $SIZE -gt $MAXSIZE -o $START -le $ENDPART3 ]; then
ogRaiseError $OG_ERR_FORMAT "$1" || return $?
fi
# Desmontar todos los sistemas de archivos del disco.
ogUnmountAll 1 2>/dev/null
# Si la tabla de particiones no es valida, volver a generarla.
[ $(parted -s $DISK print >/dev/null) ] || fdisk $DISK <<< "w"
# Definir particiones y notificar al kernel.
sfdisk -f $DISK -uS -N$PART <<<"$START,$SIZE,ca" 2>/dev/null && partprobe
}
#/**
# ogDeleteCache
#@brief Elimina la partición de caché local.
#@return (nada, por determinar)
#@exception OG_ERR_FORMAT formato incorrecto.
#@note Requisitos: sfdisk, parted, awk, sed
#@version 0.91 - Definición de caché local.
#@author Ramon Gomez, ETSII Universidad de Sevilla
#@date 2010/03/11
#*/ ##
function ogDeleteCache ()
{
# Variables locales.
local NDISK NPART DISK
# Si se solicita, mostrar ayuda.
if [ "$*" == "help" ]; then
ogHelp "$FUNCNAME" "$FUNCNAME"
return
fi
# Error si no se encuentra partición de caché.
read NDISK NPART <<<"$(ogFindCache)"
[ -n "$NDISK" -a -n "$NPART" ] || ogRaiseError $OG_ERR_PARTITION "$MSG_NOCACHE" || return $?
DISK=$(ogDiskToDev $NDISK)
# Desmontar todos los sistemas de archivos del disco.
ogUnmountAll $NDISK 2>/dev/null
# Si la tabla de particiones no es valida, volver a generarla.
[ $(parted -s $DISK print >/dev/null) ] || fdisk $DISK <<< "w"
# Eliminar (poner a 0) la partición de caché.
sfdisk -f $DISK -N$NPART <<<"0,0,0" 2>/dev/null && partprobe
# Borrar etiqueta de la caché.
rm -f /dev/disk/by-label/CACHE
}
#/**
# ogFindCache
#@brief Detecta la partición caché local.
#@param No requiere parametros
#@return int_ndisk int_npart - devuelve el par nº de disco-nº de partición .
#@warning Si no hay cache no devuelve nada
#@version 0.1 - Integracion para Opengnsys - EAC: FindCache() en ATA.lib - HIDRA: DetectarCache.sh
#@author Ramon Gomez, ETSII Universidad de Sevilla
#@Date 2008/06/19
#@author Antonio J. Doblas Viso. Universidad de Malaga
#@Date 2008/10/27
#@version 0.91 - Adaptacion a la cache local de OpenGnSys.
#@author Ramon Gomez, ETSII Universidad de Sevilla
#@date 2010/03/16
#*/ ##
function ogFindCache ()
{
# Variables locales
local PART
# Si se solicita, mostrar ayuda.
if [ "$*" == "help" ]; then
ogHelp "$FUNCNAME" "$FUNCNAME" "$FUNCNAME => 1 4"
return
fi
# Obtener el sistema de archivos etiquetado con "CACHE".
PART=$(realpath /dev/disk/by-label/CACHE 2>/dev/null)
# Si no, obtener la 1ª partición marcada como de tipo caché.
PART=${PART:-$(sfdisk -l | awk '$6~/ca|a7/ {print $1}')}
PART=${PART%% *}
ogDevToDisk $PART 2>/dev/null
}
#/**
# ogFormatCache
#@brief Formatea el sistema de ficheros para la caché local.
#@return (por determinar)
#@warning Prueba con formato Reiser.
#@attention
#@note El sistema de archivos de la caché se queda montado.
#@version 0.1 - Integracion para Opengnsys - EAC: FormatCache() en ATA.lib
#@author Antonio J. Doblas Viso. Universidad de Malaga
#@date 2008/10/27
#@version 0.91 - Creacion cache local.
#@author Ramon Gomez, ETSII Universidad de Sevilla
#@date 2010-03-11
#*/ ##
function ogFormatCache ()
{
# Variables locales.
local DEV MNTDIR
# Si se solicita, mostrar ayuda.
if [ "$*" == "help" ]; then
ogHelp "$FUNCNAME" "$FUNCNAME"
return
fi
# Error si no hay definida partición de caché.
DEV=$(ogFindCache) || ogRaiseError $OG_ERR_PARTITION "$MSG_NOCACHE" || return $?
DEV=$(ogDiskToDev $DEV) || return $?
# Formatear sistema de ficheros.
ogUnmountCache 2>/dev/null
# Orden para formateo Reiser 3
mkfs.reiserfs -f $DEV -l "CACHE" 2>/dev/null || ogRaiseError $OG_ERR_PARTITION "CACHE" || return $?
# Orden para formateo XFS
#mkfs.xfs $DEV -L "CACHE" || ogRaiseError $OG_ERR_PARTITION "CACHE" || return $?
# Crear estructura básica
MNTDIR=$(ogMountCache)
mkdir -p $MNTDIR/$OGIMG
}
#/**
# ogGetCacheSize
#@brief Devuelve el tamaño definido para la partición de caché.
#@return int_partsize tamaño de la partición (en KB)
#@exception OG_ERR_PARTITION No existe partición de caché.
#@version 0.1 - Integracion para Opengnsys - EAC: InfoCache() en FileSystem.lib
#@author Antonio J. Doblas Viso. Universidad de Malaga
#@date 2008/10/27
#@version 0.91 - Definicion de cache local.
#@author Ramon Gomez, ETSII Universidad de Sevilla
#@date 2010/03/09
#*/ ##
function ogGetCacheSize ()
{
# Variables locales
local PART
# Si se solicita, mostrar ayuda.
if [ "$*" == "help" ]; then
ogHelp "$FUNCNAME" "$FUNCNAME" "$FUNCNAME => 10000000"
return
fi
# Error si no se encuentra partición de caché.
PART=$(ogFindCache) || ogRaiseError $OG_ERR_PARTITION "$MSG_NOCACHE" || return $?
# Devuelve tamaño de la partición de caché.
ogGetPartitionSize $PART
}
#/**
# ogGetCacheSpace
#@brief Devuelve el espacio de disco disponible para la partición de caché.
#@return int_size tamaño disponible (en KB)
#@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.
#@version 0.1 - Integracion para Opengnsys - EAC: InfoCache() en FileSystem.lib
#@author Antonio J. Doblas Viso. Universidad de Malaga
#@date 2008/10/27
#@version 0.91 - Definicion de cache local.
#@author Ramon Gomez, ETSII Universidad de Sevilla
#@date 2010/03/09
#*/ ##
function ogGetCacheSpace ()
{
# Variables locales.
local DISK SECTORS CYLS ENDPART3
# Si se solicita, mostrar ayuda.
if [ "$*" == "help" ]; then
ogHelp "$FUNCNAME" "$FUNCNAME" "$FUNCNAME => 23165386"
return
fi
DISK=$(ogDiskToDev 1) || return $?
SECTORS=$(awk -v D=${DISK#/dev/} '{if ($4==D) {print $3*2}}' /proc/partitions)
CYLS=$(sfdisk -g $DISK | cut -f2 -d" ")
SECTORS=$[SECTORS/CYLS*CYLS-1]
ENDPART3=$(sfdisk -uS -l $DISK | awk -v P="${DISK}3" '{if ($1==P) print $3}')
# Mostrar espacio libre en KB (1 KB = 2 sectores)
if [ $ENDPART3 -gt $[SECTORS/2] ]; then
echo $[(SECTORS-ENDPART3)/2]
else
echo $[SECTORS/4]
fi
}
#/**
# ogMountCache
#@brief Monta la partición Cache y exporta la variable $OGCAC
#@param sin parametros
#@return path_mountpoint - Punto de montaje del sistema de archivos de cache.
#@warning Salidas de errores no determinada
#@version 0.1 - Integracion para Opengnsys - EAC: MountCache() en FileSystem.lib - HIDRA: MontarCache.sh
#@author Ramon Gomez, ETSII Universidad de Sevilla
#@date 2008/06/19
#@author Antonio J. Doblas Viso. Universidad de Malaga
#@Date 2008/10/27
#@version 0.91 - Adaptacion a la cache local de OpenGnSys.
#@author Ramon Gomez, ETSII Universidad de Sevilla
#@date 2010/03/16
#*/ ##
function ogMountCache ()
{
# Si se solicita, mostrar ayuda.
if [ "$*" == "help" ]; then
ogHelp "$FUNCNAME" "$FUNCNAME" "$FUNCNAME ==> /mnt/sda4"
return
fi
OGCAC=$(ogMountFs $(ogFindCache) 2>/dev/null) || ogRaiseError $OG_ERR_PARTITION "$MSG_NOCACHE" || return $?
echo $OGCAC
export OGCAC
}
#/**
# ogUnmountCache
#@brief Desmonta la particion Cache y elimina la variable $OGCAC
#@param sin parametros
#@return nada
#@warning Salidas de errores no determinada
#@version 0.1 - Integracion para Opengnsys - EAC: UmountCache() en FileSystem.lib
#@author Antonio J. Doblas Viso. Universidad de Malaga
#@Date 2008/10/27
#@version 0.91 - Adaptacion a la cache local de OpenGnSys.
#@author Ramon Gomez, ETSII Universidad de Sevilla
#@date 2010/03/16
#*/ ##
function ogUnmountCache ()
{
# Variables locales.
local CACHE
# Si se solicita, mostrar ayuda.
if [ "$*" == "help" ]; then
ogHelp "$FUNCNAME" "$FUNCNAME"
return
fi
CACHE=$(ogFindCache) || ogRaiseError $OG_ERR_PARTITION "$MSG_NOCACHE"
ogUnmountFs $CACHE
# Borrar enlace simbólico de /mnt/ParticiónCache.
rm -f $(ogDiskToDev $CACHE | sed 's/dev/mnt/')
}