source: client/engine/System.lib @ eeade34

v1.2.0
Last change on this file since eeade34 was 9c98a8d, checked in by adv <adv@…>, 4 years ago

#935 #906 gGrub4dosInstallMbr(): new Error code for PC's with BIOS legacy firmware

OG_ERR_NOTBIOS
error code: loadenviron.sh
string code: lang_es:cat:gb
admin code: ogRaiseError () fron system.lib

  • Property mode set to 100755
File size: 11.3 KB
Line 
1#!/bin/bash
2#/**
3#@file     System.lib
4#@brief    Librería o clase System
5#@class    System
6#@brief    Funciones básicas del sistema.
7#@version  1.1.0
8#@warning  License: GNU GPLv3+
9#*/
10
11
12#/**
13#         ogEcho [str_logtype ...] [str_loglevel] "str_message" ...
14#@brief   Muestra mensajes en consola y lo registra en fichero de incidencias.
15#@param   str_logtype  tipo de registro de incidencias.
16#@param   str_loglevel nivel de registro de incidencias.
17#@param   str_message  mensaje (puede recibir más de 1 parámetro.
18#@return  Mensaje mostrado.
19#@warning Si no se indica nivel de registro, solo muestra mensaje en pantalla.
20#@warning Si DEBUG="no", no se registran mensajes de error.
21#@note    logfile = { log, command, session }; usa "log" si se indica nivel de registro.
22#@note    loglevel = { help, info, warning, error }
23#@note    El nivel de ayuda \c (help) no se registra en el fichero de incidencias.
24#@version 0.9 - Primera versión para OpenGnSys
25#@author  Ramon Gomez, ETSII Universidad de Sevilla
26#@date    2009-07-23
27#@version 1.0.5 - Elegir fichero de log.
28#@author  Ramon Gomez, ETSII Universidad de Sevilla
29#@date    2014-03-17
30#@version 1.1.0 - Posibilidad de no registrar mensajes en ficheros.
31#@author  Ramon Gomez, ETSII Universidad de Sevilla
32#@date    2015-11-10
33#*/
34function ogEcho () {
35
36# Variables locales
37local CONT=1 LOGS LOGLEVEL DATETIME
38
39# Selección de ficheros de rgistro de incidencias.
40while [ $CONT ]; do
41    case "${1,,}" in
42        log)     LOGS="$LOGS $OGLOGFILE";    shift ;;
43        command) LOGS="$LOGS $OGLOGCOMMAND"; shift ;;
44        session) LOGS="$LOGS $OGLOGSESSION"; shift ;;
45        *)       CONT= ;;
46    esac
47done
48
49# Selección del nivel de registro (opcional).
50case "${1,,}" in
51     help)    shift ;;
52     info)    LOGLEVEL="$1"; shift ;;
53     warning) LOGLEVEL="$1"; shift ;;
54     error)   LOGLEVEL="$1"; shift ;;
55     *)       ;;
56esac
57
58if [ -n "$LOGLEVEL" ]; then
59    DATETIME=$(date +"%F %T")
60    # Registrar mensajes en fichero de log si la depuración no está desactivada.
61    [ "${DEBUG,,}" != "no" ] && LOGS="$OGLOGFILE $LOGS"
62    logger -s -t "OpenGnsys $LOGLEVEL" "$DATETIME $*" 2>&1 | tee -a $LOGS
63else
64    echo "$*" | tee -a $LOGS
65fi
66}
67
68
69#/**
70#         ogExecAndLog str_logfile ... str_command ...
71#@brief   Ejecuta un comando y guarda su salida en fichero de registro.
72#@param   str_logfile     fichero de registro (pueden ser varios).
73#@param   str_command     comando y comandos a ejecutar.
74#@return  Salida de ejecución del comando.
75#@note    str_logfile = { LOG, SESSION, COMMAND }
76#@version 1.0.6 - Primera versión para OpenGnSys
77#@author  Ramon Gomez, ETSII Universidad de Sevilla
78#@date    2013-07-02
79#*/
80function ogExecAndLog () {
81
82# Variables locales
83local ISCOMMAND ISLOG ISSESSION COMMAND CONTINUE=1 FILES REDIREC
84
85# Si se solicita, mostrar ayuda.
86if [ "$*" == "help" ]; then
87    ogHelp "$FUNCNAME str_logfile ... str_command ..." \
88           "$FUNCNAME COMMAND ls -al /"
89    return
90fi
91
92# Procesar parámetros.
93while [ $CONTINUE ]; do
94    case "${1,,}" in
95        command) ISCOMMAND=1; shift ;;
96        log)     ISLOG=1; shift ;;
97        session) ISSESSION=1; shift ;;
98        *)       COMMAND="$@"
99                 CONTINUE= ;;
100    esac
101done
102# Error si no se recibe un comando que ejecutar.
103[ -n "$COMMAND" ] || ogRaiseError $OG_ERR_FORMAT || return $?
104
105# Componer lista de ficheros de registro.
106if [ $ISCOMMAND ]; then
107    FILES="$OGLOGCOMMAND"
108    > $FILES
109    REDIREC="2>&1"
110fi
111[ $ISLOG ] && FILES="$FILES $OGLOGFILE"
112[ $ISSESSION ] && FILES="$FILES $OGLOGSESSION"
113
114# Ejecutar comando.
115eval $COMMAND $REDIREC | tee -a $FILES
116# Salida de error del comando ejecutado.
117return ${PIPESTATUS[0]}
118}
119
120
121#/**
122#         ogGetCaller
123#@brief   Devuelve nombre del programa o script ejecutor (padre).
124#@param   No.
125#@return  str_name - Nombre del programa ejecutor.
126#@version 0.10 - Primera versión para OpenGnSys.
127#@author  Ramon Gomez, ETSII Universidad de Sevilla
128#@date    2011-01-17
129#*/
130function ogGetCaller () {
131
132# Obtener el nombre del programa o del script que ha llamado al proceso actual.
133basename "$(COLUMNS=200 ps hp $PPID -o args | \
134            awk '{if ($1~/bash/ && $2!="") { print $2; }
135                  else { sub(/^-/,"",$1); print $1; } }')"
136}
137
138
139#/**
140#         ogHelp ["str_function" ["str_format" ["str_example" ... ]]]
141#@brief   Muestra mensaje de ayuda para una función determinda.
142#@param   str_function Nombre de la función.
143#@param   str_format   Formato de ejecución de la función.
144#@param   str_example  Ejemplo de ejecución de la función.
145#@return  str_help - Salida de ayuda.
146#@note    Si no se indican parámetros, la función se toma de la variable \c $FUNCNAME
147#@note    La descripción de la función se toma de la variable compuesta por \c MSG_FUNC_$función incluida en el fichero de idiomas.
148#@note    Pueden especificarse varios mensajes con ejemplos.
149#@version 0.9 - Primera versión para OpenGnSys.
150#@author  Ramon Gomez, ETSII Universidad de Sevilla
151#@date    2009-07-27
152#*/
153function ogHelp () {
154
155# Variables locales.
156local FUNC MSG
157
158# Mostrar función, descripción y formato.
159FUNC="${1:-${FUNCNAME[${#FUNCNAME[*]}-1]}}"
160MSG="MSG_HELP_$FUNC"
161ogEcho help "$MSG_FUNCTION $FUNC: ${!MSG}"
162[ -n "$2" ] && ogEcho help "    $MSG_FORMAT: $2"
163# Mostrar ejemplos (si existen).
164shift 2
165while [ $# -gt 0 ]; do
166    ogEcho help "    $MSG_EXAMPLE: $1"
167    shift
168done
169}
170
171
172#/**
173#         ogRaiseError [str_logtype ...] int_errcode ["str_errmessage" ...]
174#@brief   Devuelve el mensaje y el código de error correspondiente.
175#@param   str_logtype    tipo de registro de incidencias.
176#@param   int_errcode    código de error.
177#@param   str_errmessage mensajes complementarios de error.
178#@return  str_message - Mensaje de error, incluyendo las funciones relacionadas.
179#@warning No definidas
180#@note    Mensajes internacionales del fichero de idiomas.
181#@version 0.9 - Primera versión para OpenGnSys.
182#@author  Ramon Gomez, ETSII Universidad de Sevilla
183#@date    2009-07-21
184#@version 1.0.5 - Muestra en el mensaje todas las funciones relacionadas (separadas por <-).
185#@author  Ramon Gomez, ETSII Universidad de Sevilla
186#@date    2014-03-17
187#*/
188function ogRaiseError () {
189
190# Variables locales
191local CONT=1 LOGS MSG CODE FUNCS
192
193# Si se solicita, mostrar ayuda.
194if [ "$*" == "help" ]; then
195    ogHelp "$FUNCNAME" "$FUNCNAME [str_logfile ...] int_errorcode str_errormessage"
196    return
197fi
198
199# Selección de rgistros de incidencias.
200while [ $CONT ]; do
201    case "${1,,}" in
202        log|command|session)  LOGS="$LOGS $1"; shift ;;
203        *)                    CONT= ;;
204    esac
205done
206
207# Obtener código y mensaje de error.
208CODE="$1"
209case "$CODE" in
210     $OG_ERR_FORMAT)     MSG="$MSG_ERR_FORMAT \"$2\"" ;;
211     $OG_ERR_NOTFOUND)   MSG="$MSG_ERR_NOTFOUND \"$2\"" ;;
212     $OG_ERR_OUTOFLIMIT) MSG="$MSG_ERR_OUTOFLIMIT \"$2\"" ;;
213     $OG_ERR_PARTITION)  MSG="$MSG_ERR_PARTITION \"$2\"" ;;
214     $OG_ERR_LOCKED)     MSG="$MSG_ERR_LOCKED \"$2\"" ;;
215     $OG_ERR_CACHE)      MSG="$MSG_ERR_CACHE \"$2\"" ;;
216     $OG_ERR_NOGPT)      MSG="$MSG_ERR_NOGPT \"$2\"" ;;
217     $OG_ERR_REPO)       MSG="$MSG_ERR_REPO \"$2\"" ;;
218     $OG_ERR_FILESYS)    MSG="$MSG_ERR_FILESYS \"$2\"" ;;
219     $OG_ERR_IMAGE)      MSG="$MSG_ERR_IMAGE \"$2\"" ;;
220     $OG_ERR_NOTOS)      MSG="$MSG_ERR_NOTOS \"$2\"" ;;
221     $OG_ERR_NOTEXEC)    MSG="$MSG_ERR_NOTEXEC \"$2\"" ;;
222     $OG_ERR_NOTWRITE)   MSG="$MSG_ERR_NOTWRITE \"$2\"" ;;
223     $OG_ERR_NOTCACHE)   MSG="$MSG_ERR_NOTCACHE \"$2\"" ;;
224     $OG_ERR_CACHESIZE)  MSG="$MSG_ERR_CACHESIZE \"$2\"" ;;
225     $OG_ERR_REDUCEFS)   MSG="$MSG_ERR_REDUCEFS \"$2\"" ;;
226     $OG_ERR_EXTENDFS)   MSG="$MSG_ERR_EXTENDFS \"$2\"" ;;
227     $OG_ERR_IMGSIZEPARTITION)   MSG="$MSG_ERR_IMGSIZEPARTITION \"$2\"" ;;   
228     $OG_ERR_UPDATECACHE)       MSG="$MSG_ERR_UPDATECACHE \"$2\"" ;;
229     $OG_ERR_DONTFORMAT)        MSG="$MSG_ERR_DONTFORMAT \"$2\"" ;;
230     $OG_ERR_IMAGEFILE)   MSG="$MSG_ERR_IMAGEFILE \"$2\"" ;;
231     $OG_ERR_UCASTSYNTAXT)   MSG="$MSG_ERR_UCASTSYNTAXT \"$2\"" ;;
232     $OG_ERR_UCASTSENDPARTITION)   MSG="$MSG_ERR_UCASTSENDPARTITION \"$2\"" ;;   
233     $OG_ERR_UCASTSENDFILE)   MSG="$MSG_ERR_UCASTSENDFILE \"$2\"" ;; 
234     $OG_ERR_UCASTRECEIVERPARTITION)   MSG="$MSG_ERR_UCASTRECEIVERPARTITION \"$2\"" ;;   
235     $OG_ERR_UCASTRECEIVERFILE)   MSG="$MSG_ERR_UCASTRECEIVERFILE \"$2\"" ;; 
236     $OG_ERR_MCASTSYNTAXT)   MSG="$MSG_ERR_MCASTSYNTAXT \"$2\"" ;;
237     $OG_ERR_MCASTSENDFILE)   MSG="$MSG_ERR_MCASTSENDFILE \"$2\"" ;;
238     $OG_ERR_MCASTRECEIVERFILE)   MSG="$MSG_ERR_MCASTRECEIVERFILE \"$2\"" ;; 
239     $OG_ERR_MCASTSENDPARTITION)   MSG="$MSG_ERR_MCASTSENDPARTITION \"$2\"" ;;
240     $OG_ERR_MCASTRECEIVERPARTITION)   MSG="$MSG_ERR_MCASTRECEIVERPARTITION \"$2\"" ;; 
241     $OG_ERR_PROTOCOLJOINMASTER)   MSG="$MSG_ERR_PROTOCOLJOINMASTER \"$2\"" ;;
242     $OG_ERR_DONTMOUNT_IMAGE)   MSG="$MSG_ERR_DONTMOUNT_IMAGE \"$2\"" ;;
243     $OG_ERR_DONTUNMOUNT_IMAGE)   MSG="$MSG_ERR_DONTUNMOUNT_IMAGE \"$2\"" ;;
244     $OG_ERR_DONTSYNC_IMAGE)    MSG="$MSG_ERR_DONTSYNC_IMAGE \"$2\"" ;;
245     $OG_ERR_NOTDIFFERENT)      MSG="$MSG_ERR_NOTDIFFERENT \"$2\"" ;;
246     $OG_ERR_SYNCHRONIZING)     MSG="$MSG_ERR_SYNCHRONIZING \"$2\"" ;;
247     $OG_ERR_NOTUEFI)           MSG="$MSG_ERR_NOTUEFI \"$2\"" ;;
248     $OG_ERR_NOMSDOS)           MSG="$MSG_ERR_NOMSDOS \"$2\"" ;;
249     $OG_ERR_NOTBIOS)           MSG="$MSG_ERR_NOTBIOS \"$2\"" ;;
250     *)                  MSG="$MSG_ERR_GENERIC"; CODE=$OG_ERR_GENERIC ;;
251esac
252
253# Obtener lista de funciones afectadas, incluyendo el script que las llama.
254FUNCS="${FUNCNAME[@]:1}"
255FUNCS="${FUNCS/main/$(basename $0 2>/dev/null)}"
256
257# Mostrar mensaje de error si es función depurable y salir con el código indicado.
258if [ $CODE == $OG_ERR_FORMAT ] || ogCheckStringInGroup "$FUNCS" "$NODEBUGFUNCTIONS" || ! ogCheckStringInGroup "${FUNCS%% *}" "$NODEBUGFUNCTIONS"; then
259    ogEcho $LOGS error "${FUNCS// /<-}: $MSG" >&2
260fi
261return $CODE
262}
263
264
265#/**
266#         ogIsRepoLocked
267#@brief   Comprueba si el repositorio está siendo usado (tiene ficheros abiertos).
268#@param   No.
269#@return  Código de salida: 0 - bloqueado, 1 - sin bloquear o error.
270#@version 0.10 - Primera versión para OpenGnSys.
271#@author  Ramon Gomez, ETSII Universidad de Sevilla
272#@date    2011-01-17
273#@version 1.0.1 - Devolver falso en caso de error.
274#@author  Ramon Gomez, ETSII Universidad de Sevilla
275#@date    2011-05-18
276#*/
277function ogIsRepoLocked ()
278{
279# Variables locales.
280local f FILES
281
282# Si se solicita, mostrar ayuda.
283if [ "$*" == "help" ]; then
284    ogHelp "$FUNCNAME" "$FUNCNAME" "if $FUNCNAME; then ...; fi"
285    return
286fi
287
288# No hacer nada, si no está definido el punto de montaje del repositorio.
289[ -z "$OGIMG" ] && return 1
290
291# Comprobar si alguno de los ficheros abiertos por los procesos activos está en el
292# punto de montaje del repositorio de imágenes.
293FILES=$(for f in /proc/[0-9]*/fd/*; do readlink -f "$f"; done | grep "^$OGIMG")   # */ (comentario Doxygen)
294test -n "$FILES"
295}
296
297
298function ogCheckProgram ()
299{
300# Si se solicita, mostrar ayuda.
301if [ "$*" == "help" ]; then
302    ogHelp "$FUNCNAME \"str_program ...\"" \
303           "$FUNCNAME \"partimage partclone mbuffer\""
304    return
305fi
306
307# Error si no se recibe 1 parámetro.
308[ $# == 1 ] || ogRaiseError $OG_ERR_FORMAT || return $?
309
310local PERROR PLOG i
311PERROR=0
312PLOG=" "
313for i in `echo $1`
314do
315  if [ ! `which $i` ] 
316     then
317        PERROR=1
318        PLOG="$PLOG $i" 
319     fi
320done
321if [ "$PERROR" == "1" ] 
322then
323        ogRaiseError $OG_ERR_NOTEXEC "$PLOG" || return $?
324else           
325        return 0
326fi
327} 
328
329
330
331#### PRUEBA
332function ogIsVirtualMachine() {
333case "$(dmidecode -s system-product-name)" in
334    KVM|VirtualBox)
335        return 1 ;;
336    *)  return 0 ;;
337esac
338}
339
Note: See TracBrowser for help on using the repository browser.