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 | #*/ |
---|
34 | function ogEcho () { |
---|
35 | |
---|
36 | # Variables locales |
---|
37 | local CONT=1 LOGS LOGLEVEL DATETIME |
---|
38 | |
---|
39 | # Selección de ficheros de rgistro de incidencias. |
---|
40 | while [ $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 |
---|
47 | done |
---|
48 | |
---|
49 | # Selección del nivel de registro (opcional). |
---|
50 | case "${1,,}" in |
---|
51 | help) shift ;; |
---|
52 | info) LOGLEVEL="$1"; shift ;; |
---|
53 | warning) LOGLEVEL="$1"; shift ;; |
---|
54 | error) LOGLEVEL="$1"; shift ;; |
---|
55 | *) ;; |
---|
56 | esac |
---|
57 | |
---|
58 | if [ -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 | echo "OpenGnsys $LOGLEVEL" "$DATETIME $*" 2>&1 | tee -a $LOGS |
---|
63 | else |
---|
64 | echo "$*" | tee -a $LOGS |
---|
65 | fi |
---|
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 | #*/ |
---|
80 | function ogExecAndLog () { |
---|
81 | |
---|
82 | # Variables locales |
---|
83 | local ISCOMMAND ISLOG ISSESSION COMMAND CONTINUE=1 FILES REDIREC |
---|
84 | |
---|
85 | # Si se solicita, mostrar ayuda. |
---|
86 | if [ "$*" == "help" ]; then |
---|
87 | ogHelp "$FUNCNAME str_logfile ... str_command ..." \ |
---|
88 | "$FUNCNAME COMMAND ls -al /" |
---|
89 | return |
---|
90 | fi |
---|
91 | |
---|
92 | # Procesar parámetros. |
---|
93 | while [ $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 |
---|
101 | done |
---|
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. |
---|
106 | if [ $ISCOMMAND ]; then |
---|
107 | FILES="$OGLOGCOMMAND" |
---|
108 | > $FILES |
---|
109 | REDIREC="2>&1" |
---|
110 | fi |
---|
111 | [ $ISLOG ] && FILES="$FILES $OGLOGFILE" |
---|
112 | [ $ISSESSION ] && FILES="$FILES $OGLOGSESSION" |
---|
113 | |
---|
114 | # Ejecutar comando. |
---|
115 | eval $COMMAND $REDIREC | tee -a $FILES |
---|
116 | # Salida de error del comando ejecutado. |
---|
117 | return ${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 | #*/ |
---|
130 | function ogGetCaller () { |
---|
131 | |
---|
132 | # Obtener el nombre del programa o del script que ha llamado al proceso actual. |
---|
133 | basename "$(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 | #*/ |
---|
153 | function ogHelp () { |
---|
154 | |
---|
155 | # Variables locales. |
---|
156 | local FUNC MSG |
---|
157 | |
---|
158 | # Mostrar función, descripción y formato. |
---|
159 | FUNC="${1:-${FUNCNAME[${#FUNCNAME[*]}-1]}}" |
---|
160 | MSG="MSG_HELP_$FUNC" |
---|
161 | ogEcho help "$MSG_FUNCTION $FUNC: ${!MSG}" |
---|
162 | [ -n "$2" ] && ogEcho help " $MSG_FORMAT: $2" |
---|
163 | # Mostrar ejemplos (si existen). |
---|
164 | shift 2 |
---|
165 | while [ $# -gt 0 ]; do |
---|
166 | ogEcho help " $MSG_EXAMPLE: $1" |
---|
167 | shift |
---|
168 | done |
---|
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 | #*/ |
---|
188 | function ogRaiseError () { |
---|
189 | |
---|
190 | # Variables locales |
---|
191 | local CONT=1 LOGS MSG CODE FUNCS |
---|
192 | |
---|
193 | # Si se solicita, mostrar ayuda. |
---|
194 | if [ "$*" == "help" ]; then |
---|
195 | ogHelp "$FUNCNAME" "$FUNCNAME [str_logfile ...] int_errorcode str_errormessage" |
---|
196 | return |
---|
197 | fi |
---|
198 | |
---|
199 | # Selección de rgistros de incidencias. |
---|
200 | while [ $CONT ]; do |
---|
201 | case "${1,,}" in |
---|
202 | log|command|session) LOGS="$LOGS $1"; shift ;; |
---|
203 | *) CONT= ;; |
---|
204 | esac |
---|
205 | done |
---|
206 | |
---|
207 | # Obtener código y mensaje de error. |
---|
208 | CODE="$1" |
---|
209 | case "$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 ;; |
---|
251 | esac |
---|
252 | |
---|
253 | # Obtener lista de funciones afectadas, incluyendo el script que las llama. |
---|
254 | FUNCS="${FUNCNAME[@]:1}" |
---|
255 | FUNCS="${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. |
---|
258 | if [ $CODE == $OG_ERR_FORMAT ] || ogCheckStringInGroup "$FUNCS" "$NODEBUGFUNCTIONS" || ! ogCheckStringInGroup "${FUNCS%% *}" "$NODEBUGFUNCTIONS"; then |
---|
259 | ogEcho $LOGS error "${FUNCS// /<-}: $MSG" >&2 |
---|
260 | fi |
---|
261 | return $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 | #*/ |
---|
277 | function ogIsRepoLocked () |
---|
278 | { |
---|
279 | # Variables locales. |
---|
280 | local f FILES |
---|
281 | |
---|
282 | # Si se solicita, mostrar ayuda. |
---|
283 | if [ "$*" == "help" ]; then |
---|
284 | ogHelp "$FUNCNAME" "$FUNCNAME" "if $FUNCNAME; then ...; fi" |
---|
285 | return |
---|
286 | fi |
---|
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. |
---|
293 | FILES=$(for f in /proc/[0-9]*/fd/*; do readlink -f "$f"; done | grep "^$OGIMG") # */ (comentario Doxygen) |
---|
294 | test -n "$FILES" |
---|
295 | } |
---|
296 | |
---|
297 | |
---|
298 | function ogCheckProgram () |
---|
299 | { |
---|
300 | # Si se solicita, mostrar ayuda. |
---|
301 | if [ "$*" == "help" ]; then |
---|
302 | ogHelp "$FUNCNAME \"str_program ...\"" \ |
---|
303 | "$FUNCNAME \"partimage partclone mbuffer\"" |
---|
304 | return |
---|
305 | fi |
---|
306 | |
---|
307 | # Error si no se recibe 1 parámetro. |
---|
308 | [ $# == 1 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
309 | |
---|
310 | local PERROR PLOG i |
---|
311 | PERROR=0 |
---|
312 | PLOG=" " |
---|
313 | for i in `echo $1` |
---|
314 | do |
---|
315 | if [ ! `which $i` ] |
---|
316 | then |
---|
317 | PERROR=1 |
---|
318 | PLOG="$PLOG $i" |
---|
319 | fi |
---|
320 | done |
---|
321 | if [ "$PERROR" == "1" ] |
---|
322 | then |
---|
323 | ogRaiseError $OG_ERR_NOTEXEC "$PLOG" || return $? |
---|
324 | else |
---|
325 | return 0 |
---|
326 | fi |
---|
327 | } |
---|
328 | |
---|
329 | |
---|
330 | |
---|
331 | #### PRUEBA |
---|
332 | function ogIsVirtualMachine() { |
---|
333 | case "$(dmidecode -s system-product-name)" in |
---|
334 | KVM|VirtualBox) |
---|
335 | return 1 ;; |
---|
336 | *) return 0 ;; |
---|
337 | esac |
---|
338 | } |
---|
339 | |
---|