source: client/engine/Image.lib @ 62ccd9b

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 62ccd9b was 914d834, checked in by adv <adv@…>, 14 years ago

branch version 1.0

git-svn-id: https://opengnsys.es/svn/branches/version1.0@1503 a21b9725-9963-47de-94b9-378ad31fedc9

  • Property mode set to 100755
File size: 21.2 KB
Line 
1#!/bin/bash
2#/**
3#@file    Image.lib
4#@brief   Librería o clase Image
5#@class   Image
6#@brief   Funciones para creación, restauración y clonación de imágenes de sistemas.
7#@version 0.9
8#@warning License: GNU GPLv3+
9#*/
10
11# Bloquear imagen para creación.
12function ogLockImage ()
13{
14# Variables locales
15local IMGDIR
16
17# Si se solicita, mostrar ayuda.
18if [ "$*" == "help" ]; then
19    ogHelp "$FUNCNAME" "$FUNCNAME [str_repo] path_image" \
20           "$FUNCNAME /opt/opengnsys/images/aula1/winxp.img" \
21           "$FUNCNAME REPO /aula1/winxp.img"
22    return
23fi
24# Error si no se reciben 1 o 2 parámetros.
25[ $# -lt 1 -o $# -gt 2 ] && ogRaiseError $OG_ERR_FORMAT && return $?
26# Comprobar que existe directorio de imagen
27IMGDIR=$(ogGetParentPath $@) || return $?
28# Crear fichero de bloqueo.
29touch $IMGDIR/$(basename "${!#}").lock
30}
31
32# Desbloquear imagen (borrar fichero de bloqueo).
33function ogUnlockImage ()
34{
35# Variables locales
36local IMGDIR
37
38# Si se solicita, mostrar ayuda.
39if [ "$*" == "help" ]; then
40    ogHelp "$FUNCNAME" "$FUNCNAME [str_repo] path_image" \
41           "$FUNCNAME /opt/opengnsys/images/aula1/winxp.img" \
42           "$FUNCNAME REPO /aula1/winxp.img"
43    return
44fi
45# Error si no se reciben 1 o 2 parámetros.
46[ $# -lt 1 -o $# -gt 2 ] && ogRaiseError $OG_ERR_FORMAT && return $?
47# Borrar fichero de bloqueo para la imagen.
48rm -f $(ogGetPath $@.lock)
49}
50
51# Comprobar si el fichero de bloqueo para una imagen.
52function ogIsImageLocked ()
53{
54# Variables locales
55local IMGDIR
56
57# Si se solicita, mostrar ayuda.
58if [ "$*" == "help" ]; then
59    ogHelp "$FUNCNAME" "$FUNCNAME [str_repo] path_image" \
60           "if $FUNCNAME /opt/opengnsys/images/aula1/winxp.img; then ...; fi" \
61           "if $FUNCNAME REPO /aula1/winxp.img; then ...; fi"
62    return
63fi
64# Comprobar si existe el fichero de bloqueo.
65test -n "$(ogGetPath $@.lock)"
66}
67
68
69function ogPartcloneSyntax ()
70{
71#TODO: comprobar como unico parametro particion /dev/sda1
72#COMPAR="partclone.$FS --clone --force --source $PART"
73COMPAR="-F -c -s "
74TYPE="$(ogGetFsType `ogDevToDisk $1`)"
75case "$TYPE" in
76    EXT[234])
77        echo "partclone.extfs $COMPAR $1"
78        ;;
79    REISERFS|XFS|JFS)
80        echo "partclone.dd $COMPAR $1"
81        ;;
82    NTFS|HNTFS)
83        echo "partclone.ntfs $COMPAR $1"
84        ;;
85    FAT16|FAT32|HFAT16|HFAT32)
86        echo "partclone.fat $COMPAR $1"
87        ;;
88    HFS|HFS+)
89        echo "partclone.hfsp $COMPAR $1"
90        ;;
91    *)  ogRaiseError $OG_ERR_PARTITION "$1 $TYPE"
92        return $? ;;
93esac   
94}
95
96#/**
97#         ogCreateImageSyntax partition filename [tools]  [levelcompresor]
98#@brief   Genera una cadena de texto con la instrucción para crear un fichero imagen
99#@param 1  partition             identificador linux del dispositivo particion.
100#@param 2  filename           path absoluto del fichero imagen
101#@param 3  [opcional] str_tools          herrmaienta de clonacion [partimage, partclone, ntfsclone]
102#@param 4  [opcional] str_levelcompresor nivel de compresion. [0 -none-, 1-lzop-, 2-gzip]
103#@return  cadena con el comando que se debe ejecutar.
104#@exception OG_ERR_FORMAT    formato incorrecto.
105#@warning En pruebas iniciales
106#@TODO    introducir las herramientas fsarchiver, dd
107#@TODO    introducir el nivel de compresion gzip
108#@version 1.0 - Primeras pruebas
109#@author  Antonio J. Doblas Viso. Universidad de Málaga
110#@date    2010/02/08
111#*/ ##
112
113function ogCreateImageSyntax()
114{
115local FS TOOL LEVEL PART IMGFILE BUFFER
116
117# Si se solicita, mostrar ayuda.
118if [ "$*" == "help" ]; then
119    ogHelp "$FUNCNAME" "$FUNCNAME partition filename [tool] [levelcompresor]" \
120           "$FUNCNAME /dev/sda1 /opt/opengnsys/images/prueba.img partclone lzop"
121    return
122fi
123# Error si no se reciben al menos los 2 parámetros para obtener el valor default.
124[ $# -lt 2 ] && ogRaiseError $OG_ERR_FORMAT && return $?
125
126
127PART=`echo $1`
128IMGFILE=`echo $2`
129
130case "$#" in
131   "2")
132        # Sintaxis por defecto OG PART IMGFILE
133        #echo "partimage -M -f3 -o -d -V0 -B gui=no -c -z1 save $PART $IMGFILE"
134    # Se comenta la instruccion que debería ir aqui
135    ogCreateImageSyntax $1 $2 partclone gzip
136   ;;
137   "4")
138        # Sintaxis condicionada.
139        # comprobamos parametro herramienta compresion.
140        TOOL=$(echo $3 | tr [A-Z] [a-z])       
141        #ogCheckProgram $TOOL
142        #comprobar parámetro compresor.
143        LEVEL=$(echo $4 | tr [A-Z] [a-z])
144        #ogCheckProgram $LEVEL
145
146        # herramienta
147        case "$TOOL" in
148                "ntfsclone")
149                        PARAM1="ntfsclone --force --save-image -O - $PART"
150                ;;
151                "partimage"|DEFAULT)
152                        PARAM1="partimage -M -f3 -o -d -B gui=no -c -z0 --volume=0 save $PART stdout"
153                ;;
154                "partclone")
155                        PARAM1=`ogPartcloneSyntax $PART` || ogRaiseError $OG_ERR_FORMAT || return $?
156                ;;
157        esac
158        # mbuffer
159        which mbuffer > /dev/null && PARAM2="| mbuffer -q -m 40M " || PARAM2=" "
160
161        # nivel de compresion
162        case "$LEVEL" in
163        "0"|"none")
164                PARAM3=" > "
165        ;;
166        "1"|"lzop")
167                PARAM3=" | lzop > "
168        ;;
169        "2"|"gzip")
170                PARAM3=" | gzip -c > "
171        ;;
172        "3"|"bzip")
173                PARAM3=" | bzip -c > "
174        ;;
175        esac
176        #sintaxis final.
177        echo "$PARAM1 $PARAM2 $PARAM3 $IMGFILE"
178        ;;
179esac
180}
181
182
183#/**
184#         ogRestoreImageSyntax filename partition [tools]  [levelcompresor]
185#@brief   Genera una cadena de texto con la instrucción para crear un fichero imagen
186#@param 1  filename           path absoluto del fichero imagen
187#@param 2  partition             identificador linux del dispositivo particion.
188#@param 3  [opcional] str_tools          herrmaienta de clonacion [partimage, partclone, ntfsclone]
189#@param 4  [opcional] str_levelcompresor nivel de compresion. [0 -none-, 1-lzop-, 2-gzip]
190#@return  cadena con el comando que se debe ejecutar.
191#@exception OG_ERR_FORMAT    formato incorrecto.
192#@warning En pruebas iniciales
193#@TODO    introducir las herramientas fsarchiver, dd
194#@TODO    introducir el nivel de compresion gzip
195#@version 1.0 - Primeras pruebas
196#@author  Antonio J. Doblas Viso. Universidad de Málaga
197#@date    2010/02/08
198#*/ ##
199
200ogRestoreImageSyntax ()
201{
202local TOOL COMPRESSOR LEVEL PART IMGFILE FILEHEAD INFOIMG
203
204
205# Si se solicita, mostrar ayuda.
206if [ "$*" == "help" ]; then
207    ogHelp "$FUNCNAME" "$FUNCNAME  filename partition [tool] [levelcompresor]" \
208           "$FUNCNAME  /opt/opengnsys/images/prueba.img /dev/sda1 [partclone] [lzop]"
209    return
210fi
211
212# Error si no se reciben al menos 2 parámetros.
213[ $# -lt 2 ] && ogRaiseError $OG_ERR_FORMAT && return $?
214
215# controlamos que el parametro 1 (imagen) es tipo file.
216[ -f $1 ] || ogRaiseError $OG_ERR_NOTFOUND "$1" || return $?
217
218# Si 2 parametros (file-origen-, device-destino-) = ogGetImageFull($1)
219if [ "$#" -eq 2 ]; then
220        IMGFILE=$1
221        PART=$2
222        INFOIMG=$(ogGetImageInfo $IMGFILE) || ogRaiseError $OG_ERR_NOTFOUND "No Image $1" || return $?
223        TOOL=`echo $INFOIMG | cut -f1 -d:`
224        COMPRESSOR=`echo $INFOIMG | cut -f2 -d:`
225        ogRestoreImageSyntax $IMGFILE $PART $TOOL $COMPRESSOR
226fi
227
228
229# Si cuatro parametros genera sintaxis
230if [ "$#" -eq 4 ]; then
231        IMGFILE=$1
232        PART=$2
233        # comprobamos parametro herramienta compresion.
234        TOOL=$(echo $3 | tr [A-Z] [a-z])       
235        #ogCheckProgram $TOOL
236        #comprobar parámetro compresor.
237        LEVEL=$(echo $4 | tr [A-Z] [a-z])
238        #ogCheckProgram $LEVEL
239       
240        case "$LEVEL" in
241        "0"|"none")
242                COMPRESSOR=" "
243        ;;
244        "1"|"lzop" | "LZOP")
245                COMPRESSOR=" lzop -dc "
246        ;;
247        "2"|"gzip" | "GZIP")
248                COMPRESSOR=" gzip -dc "
249        ;;
250        "3"|"bzip" | "BZIP" )
251                COMPRESSOR=" bzip -dc "
252        ;;
253        *)
254                ogRaiseError $OG_ERR_NOTFOUND "Compressor no valid $TOOL" || return $?
255        ;;
256        esac
257    #comprobar mbuffer
258        which mbuffer > /dev/null && MBUFFER="| mbuffer -q -m 40M " || MBUFFER=" "
259
260        case "$TOOL" in
261                "ntfsclone" | "NTFSCLONE")
262                        TOOL="| ntfsclone --restore-image --overwrite $PART -"
263                ;;
264                "partimage"| "PARTIMAGE")
265                        TOOL="| partimage -f3 -B gui=no restore $PART stdin"
266                ;;
267                "partclone" | "PARTCLONE")
268                    # -C para que no compruebe tamaños
269                        TOOL="| partclone.restore -o $PART"
270                ;;
271                *)
272                ogRaiseError $OG_ERR_NOTFOUND "Tools imaging no valid $TOOL" || return $?
273        ;;
274        esac
275
276        echo "$COMPRESSOR $IMGFILE $MBUFFER $TOOL"
277fi
278
279}
280
281
282
283
284#/**
285#         ogCreateImage int_ndisk int_npartition str_repo path_image
286#@brief   Crea una imagen a partir de una partición.
287#@param   int_ndisk      nº de orden del disco
288#@param   int_npartition nº de orden de la partición
289#@param   str_repo       repositorio de imágenes (remoto o caché local)
290#@param   path_image     camino de la imagen (sin extensión)
291#@return  (nada, por determinar)
292#@note    repo = { REPO, CACHE }
293#@exception OG_ERR_FORMAT    formato incorrecto.
294#@exception OG_ERR_NOTFOUND  fichero o dispositivo no encontrado.
295#@exception OG_ERR_PARTITION partición no accesible o no soportada.
296#@exception OG_ERR_LOCKED    particion bloqueada por otra operación.
297#@exception OG_ERR_IMAGE     error al crear la imagen del sistema.
298#@warning En pruebas iniciales
299#@todo    Comprobaciones, control de errores, definir parámetros, etc.
300#@version 0.1 -  Integracion para Opengnsys  -  HIDRA:CrearImagen{EXT3, NTFS}.sh;  EAC: CreateImageFromPartition () en Deploy.lib
301#@author Ramon Gomez, ETSII Universidad de Sevilla
302#@Date    2008/05/13
303#@author  Antonio J. Doblas Viso. Universidad de Malaga
304#@date    2008/10/27
305#@version 0.9 - Versión en pruebas para OpenGnSys
306#@author  Ramon Gomez, ETSII Universidad de Sevilla
307#@date    2009/10/07
308#*/ ##
309function ogCreateImage ()
310{
311# Variables locales
312local PART PROGRAM IMGDIR IMGFILE IMGTYPE ERRCODE
313
314# Si se solicita, mostrar ayuda.
315if [ "$*" == "help" ]; then
316    ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npart path_dir str_image" \
317           "$FUNCNAME 1 1 REPO /aula1/winxp"
318    return
319fi
320# Error si no se reciben 4 parámetros.
321[ $# -lt 4 ] && ogRaiseError $OG_ERR_FORMAT && return $?
322
323# Comprobar que no está bloqueada ni la partición, ni la imagen.
324PART="$(ogDiskToDev $1 $2)" || return $?
325if ogIsLocked $1 $2; then
326    ogRaiseError $OG_ERR_LOCKED "$MSG_PARTITION $1, $2"
327    return $?
328fi
329IMGTYPE="img"                   # Extensión genérica de imágenes.
330IMGDIR=$(ogGetParentPath "$3" "$4")
331
332[ -n "$IMGDIR" ] || ogRaiseError $OG_ERR_NOTFOUND "$3 $(dirname $4)" || return $?
333IMGFILE="$IMGDIR/$(basename "$4").$IMGTYPE"
334if ogIsImageLocked "$IMGFILE"; then
335    ogRaiseError $OG_ERR_LOCKED "$MSG_IMAGE $3, $4"
336    return $?
337fi
338# Desmontar partición, bloquear partición e imagen.
339ogUnmount $1 $2 2>/dev/null
340ogLock $1 $2 || return $?
341ogLockImage "$3" "$4.$IMGTYPE" || return $?
342
343# Crear Imagen.
344trap "ogUnlock $1 $2; ogUnlockImage "$3" "$4.$IMGTYPE"; rm -f $IMGFILE" 1 2 3 6 9
345#Solicitamos la generación de la instruccion a ejecutar
346PROGRAM=`ogCreateImageSyntax $PART $IMGFILE $5 $6`
347echo $PROGRAM
348eval $PROGRAM
349
350# Controlar salida de error y desbloquear partición.
351ERRCODE=$?
352if [ $ERRCODE != 0 ]; then
353    ogRaiseError $OG_ERR_IMAGE "$1 $2 $IMGFILE"
354    rm -f "$IMGFILE"
355fi
356# Desbloquear partición e imagen.
357ogUnlock $1 $2
358ogUnlockImage "$3" "$4.$IMGTYPE"
359return $ERRCODE
360}
361
362
363
364
365
366
367
368
369#/**
370#         ogRestoreImage str_repo path_image int_ndisk int_npartition
371#@brief   Restaura una imagen de sistema de archivos en una partición.
372#@param   str_repo       repositorio de imágenes o caché local
373#@param   path_image     camino de la imagen
374#@param   int_ndisk      nº de orden del disco
375#@param   int_npartition nº de orden de la partición
376#@return  (por determinar)
377#@exception OG_ERR_FORMAT   formato incorrecto.
378#@exception OG_ERR_NOTFOUND fichero de imagen o partición no detectados.
379#@exception OG_ERR_LOCKED   partición bloqueada por otra operación.
380#@exception OG_ERR_IMAGE    error al restaurar la imagen del sistema.
381#@todo    Comprobar incongruencias partición-imagen, control de errores, definir parámetros, caché/repositorio, etc.
382#@version 0.1 -  Integracion para Opengnsys  - HIDRA:RestaurarImagen{EXT3, NTFS}.sh;  EAC: RestorePartitionFromImage() en Deploy.lib
383#@author Ramon Gomez, ETSII Universidad de Sevilla
384#@Date    2008/05/13
385#@author  Antonio J. Doblas Viso. Universidad de Malaga
386#@date    2008/10/27
387#@version 0.9 - Primera version muy en pruebas para OpenGnSys
388#@author  Ramon Gomez, ETSII Universidad de Sevilla
389#@date    2009/09/10
390#*/ ##
391function ogRestoreImage ()
392{
393# Variables locales
394local PART PARTSIZE IMGFILE IMGTYPE IMGSIZE FSTYPE
395
396# Si se solicita, mostrar ayuda.
397if [ "$*" == "help" ]; then
398    ogHelp "$FUNCNAME" "$FUNCNAME path_dir str_image int_ndisk int_npart" \
399           "$FUNCNAME REPO /aula1/winxp 1 1"
400    return
401fi
402# Error si no se reciben 4 parámetros.
403[ $# -lt 4 ] && ogRaiseError $OG_ERR_FORMAT && return $?
404# Procesar parámetros.
405PART="$(ogDiskToDev "$3" "$4")" || return $?
406#IMGTYPE=$(ogGetImageType "$1" "$2")
407IMGTYPE=img
408IMGFILE=$(ogGetPath "$1" "$2.$IMGTYPE")
409[ -r "$IMGFILE" ] || ogRaiseError OG_ERR_NOTFOUND "$IMGFILE" || return $?
410# Error si la imagen no cabe en la particion.
411IMGSIZE=$(ogGetImageSize "$1" "$2")
412PARTSIZE=$(ogGetPartitionSize $3 $4)
413if [ $IMGSIZE -gt $PARTSIZE ]; then
414    ogRaiseError $OG_ERR_PARTITION "$IMGSIZE > $PARTSIZE"
415    return $?
416fi
417
418# Comprobar el bloqueo de la imagen y de la partición.
419if ogIsImageLocked "$IMGFILE"; then
420    ogRaiseError $OG_ERR_LOCKED "$MSG_IMAGE $1, $2.$IMGTYPE"
421    return $?
422fi
423if ogIsLocked $3 $4; then
424    ogRaiseError $OG_ERR_LOCKED "$MSG_PARTITION $3, $4"
425    return $?
426fi
427# Desmontar y bloquear partición.
428ogUnmount $3 $4 2>/dev/null || return $?
429ogLock $3 $4 || return $?
430trap "ogUnlock $3 $4" 1 2 3 6 9
431
432# Restaurar según el tipo de imagen.
433# Atención: no se comprueba el tipo de sistema de archivos.
434# Atención: no se comprueba incongruencia entre partición e imagen.
435#Solicitamos la generación de la instruccion a ejecutar
436PROGRAM=`ogRestoreImageSyntax  $IMGFILE $PART`
437echo $PROGRAM
438eval $PROGRAM
439
440ERRCODE=$?
441if [ $ERRCODE != 0 ]; then
442    ogRaiseError $OG_ERR_IMAGE "$IMGFILE, $3, $4"
443fi
444ogUnlock $3 $4
445return $ERRCODE
446}
447
448
449#/**
450#         ogGetImageInfo filename
451#@brief   muestra información sobre la imagen monolitica.
452#@param 1   filename           path absoluto del fichero imagen
453#@return  cadena compuesta por clonacion:compresor:sistemaarchivos:tamañoKB
454#@exception OG_ERR_FORMAT    formato incorrecto.
455#@exception OG_ERR_NOTFOUND   fichero no encontrado.
456#@exception OG_ERR_IMAGE        "Image format is not valid $IMGFILE"
457#@warning En pruebas iniciales
458#@TODO    Definir sintaxis de salida (herramienta y compresor en minuscula)
459#@TODO    Arreglar loop para ntfsclone
460#@TODO    insertar parametros entrada tipo OG
461#@version 1.0 - Primeras pruebas
462#@author  Antonio J. Doblas Viso. Universidad de Málaga
463#@date    2010/02/08
464#*/ ##
465
466function ogGetImageInfo () {
467# Si se solicita, mostrar ayuda.
468if [ "$*" == "help" ]; then
469    ogHelp "$FUNCNAME" "$FUNCNAME  filename " \
470           "$FUNCNAME  /opt/opengnsys/images/prueba.img "
471    return
472fi
473
474# Error si no se reciben 1 parámetros.
475[ $# == 1 ] || ogRaiseError $OG_ERR_FORMAT || return $?
476
477#comprobando que el parametro uno es un file.
478[ -f $1 ] || ogRaiseError $OG_ERR_NOTFOUND "$1" || return $?
479
480local TOOLS COMPRESSOR IMGFILE FILEHEAD FS SIZE PARTIMAGEINFO PARTCLONEINFO NTFSCLONEINFO IMGDETECT
481IMGDETECT="FALSE"
482
483IMGFILE=$1
484FILEHEAD=/tmp/`basename $IMGFILE`.infohead
485COMPRESSOR=`file $IMGFILE | awk '{print $2}'`
486ogCheckStringInGroup "$COMPRESSOR" "gzip lzop" || ogRaiseError $OG_ERR_IMAGE "Image format is not valid $IMGFILE" || return $?
487$($COMPRESSOR -dc $IMGFILE 2>/dev/null | head > $FILEHEAD) || ogRaiseError $OG_ERR_IMAGE "Image format is not valid $IMGFILE" || return $?
488
489## buscando Primera opción.
490if [ "$IMGDETECT" == "FALSE" ]
491then
492        PARTCLONEINFO=$(partclone.info $FILEHEAD 2>&1)
493        if `echo $PARTCLONEINFO | grep size > /dev/null`
494        then
495                TOOLS=PARTCLONE
496                FS=$(echo $PARTCLONEINFO | awk '{gsub(/\: /,"\n"); print toupper($8);}')
497                SIZE=$(echo $PARTCLONEINFO | awk '{gsub(/\: /,"\n"); printf "%d\n", $11*1024*1024;}')
498                IMGDETECT="TRUE"
499        fi
500fi
501#buscando segunda opcion.
502if [ "$IMGDETECT" == "FALSE" -a ! -f /dev/loop2  ]
503then
504        cat $FILEHEAD | grep -w ntfsclone-image > /dev/null && NTFSCLONEINFO=$(cat $FILEHEAD | ntfsclone --restore --overwrite /dev/loop2 - 2>&1)
505        if `echo $NTFSCLONEINFO | grep ntfsclone > /dev/null` 
506        then
507                TOOLS=NTFSCLONE
508                SIZE=$(echo $NTFSCLONEINFO | awk '{gsub(/\(|\)|\./,""); printf "%d\n",$17/1000;}')
509                FS=NTFS
510                IMGDETECT="TRUE"
511        fi
512fi
513## buscando Tercer opción.
514if [ "$IMGDETECT" == "FALSE" ]
515then
516        PARTIMAGEINFO=$(partimage -B gui=no imginfo "$FILEHEAD" 2>&1)
517        if `echo $PARTIMAGEINFO | grep Partition > /dev/null`
518        then   
519                TOOLS=PARTIMAGE
520                FS=$(echo $PARTIMAGEINFO | awk '{gsub(/ /,"\n"); print $17;}' | awk '{sub(/\.\.+/," "); print toupper($2)}')
521                SIZE=$( echo $PARTIMAGEINFO | awk '{gsub(/ /,"\n"); print $36;}' | awk '{sub(/\.\.+/," "); printf "%d\n",$2*1024*1024;}')
522                IMGDETECT="TRUE"
523        fi     
524fi
525#comprobamos valores #Chequeamos los valores devueltos.
526if [ -z "$TOOLS" -o -z "$COMPRESSOR" -o "$IMGDETECT" == "FALSE" ]
527then
528        ogRaiseError $OG_ERR_IMAGE "Image format is not valid $IMGFILE" || return $?
529else
530        COMPRESSOR=$(echo $COMPRESSOR | tr [a-z] [A-Z])
531        echo $TOOLS:$COMPRESSOR:$FS:$SIZE
532fi
533}
534
535function ogGetImageProgram ()
536{
537local IMGFILE
538IMGFILE=$(ogGetPath "$1" "$2.img") || return $?
539[ -r "$IMGFILE" ] || ogRaiseError OG_ERR_NOTFOUND "$IMGFILE" || return $?
540ogGetImageInfo $IMGFILE | awk -F: '{print $1}'
541
542}
543
544function ogGetImageCompressor ()
545{
546local IMGFILE
547IMGFILE=$(ogGetPath "$1" "$2.img") || return $?
548[ -r "$IMGFILE" ] || ogRaiseError OG_ERR_NOTFOUND "$IMGFILE" || return $?
549ogGetImageInfo $IMGFILE | awk -F: '{print $2}'
550}
551
552function ogGetImageType ()
553{
554local IMGFILE
555IMGFILE=$(ogGetPath "$1" "$2.img") || return $?
556[ -r "$IMGFILE" ] || ogRaiseError OG_ERR_NOTFOUND "$IMGFILE" || return $?
557#partimage -B gui=no imginfo "$IMGFILE" 2>&1 | \
558#        awk '/^Filesystem/ {sub(/\.\.+/," "); sub(/fs$/,""); print toupper($2);}'
559ogGetImageInfo $IMGFILE | awk -F: '{print $3}'
560
561}
562
563function ogGetImageSize ()
564{
565# Variables locales
566local IMGFILE
567
568# Si se solicita, mostrar ayuda.
569if [ "$*" == "help" ]; then
570    ogHelp "$FUNCNAME" "$FUNCNAME testing path_dir str_image int_ndisk int_npart" \
571           "$FUNCNAME 1 1 REPO /aula1/winxp  ==>  5642158"
572    return
573fi
574# Error si no se reciben menos de 2 parámetros.
575[ $# -lt 2 ] && ogRaiseError $OG_ERR_FORMAT && return $?
576# Error si el fichero de imagen no es accesible.
577IMGFILE=$(ogGetPath "$1" "$2.img") || return $?
578[ -r "$IMGFILE" ] || ogRaiseError OG_ERR_NOTFOUND "$IMGFILE" || return $?
579
580# Devuelve el tamaño de la imagen en KB.
581#partimage -B gui=no imginfo "$IMGFILE" 2>&1 | \
582#        awk '/Partition size/ {sub(/\.\.+/," "); printf "%d\n",$3*1024*1024;}'
583ogGetImageInfo $IMGFILE | awk -F: '{print $4}'
584}
585
586
587#/**
588#         ogGetImageFs str_repo path_image
589#@brief   Devuelve el tipo de sistema de archivos almacenado en un fichero de imagen.
590#@param   str_repo    repositorio de imágenes o caché local
591#@param   path_image  camino de la imagen
592#@return  str_imgtype - mnemónico del tipo de sistema de archivos
593#@exception OG_ERR_FORMAT   formato incorrecto.
594#@exception OG_ERR_NOTFOUND fichero de imagen no encontrado.
595#@todo    Comprobar salidas para todos los tipos de sistemas de archivos.
596#/**
597function ogGetImageFsUS ()
598{
599local IMGFILE IMGTYPE
600IMGTYPE=$(ogGetImageType "$1" "$2")
601IMGFILE=$(ogGetPath "$1" "$2.$IMGTYPE") || return $?
602[ -r "$IMGFILE" ] || ogRaiseError OG_ERR_NOTFOUND "$IMGFILE" || return $?
603case "$IMGTYPE" in
604    img) # Partimage.
605         partimage -B gui=no imginfo "$IMGFILE" 2>&1 | \
606                awk '/^Filesystem/ {sub(/\.\.+/," "); if ($2=="ntfs") print NTFS;
607                                                      else { sub(/fs$/,""); print toupper($2);} }'
608         ;;
609    pgz) # Partclone / GZip
610         gzip -dc "$IMGFILE" | partclone.chkimg -C -s - 2>&1 | \
611                awk '/^File system/ {if ($2=="EXTFS") print "EXT3"; else print $3;}'
612         ;;
613    *)   # Error si el fichero de imagen no es accesible.
614         ogRaiseError OG_ERR_NOTFOUND "$IMGFILE"
615         return $?  ;;
616esac
617}
618
619
620#         ogGetImageSize str_repo path_image
621#@brief   Devuelve el tamaño del sistema de archivos almacenado en un fichero de imagen.
622#@param   str_repo    repositorio de imágenes o caché local
623#@param   path_image  camino de la imagen
624#@return  int_size  - tamaño (en KB)
625#@exception OG_ERR_FORMAT   formato incorrecto.
626#@exception OG_ERR_NOTFOUND fichero de imagen no encontrado.
627#*/
628#@warning En pruebas iniciales
629#@todo    Definición de parámetros y salidas.
630#@version 0.1 - Primera versión muy en pruebas para OpenGNSys
631#@author  Ramon Gomez, ETSII Universidad de Sevilla
632#@date    2009/09/11
633#*/ ##
634function ogGetImageSizeUS ()
635{
636# Variables locales
637local IMGFILE IMGTYPE
638
639# Si se solicita, mostrar ayuda.
640if [ "$*" == "help" ]; then
641    ogHelp "$FUNCNAME" "$FUNCNAME path_dir str_image int_ndisk int_npart" \
642           "$FUNCNAME 1 1 REPO /aula1/winxp  ==>  5642158"
643    return
644fi
645# Error si no se reciben menos de 2 parámetros.
646[ $# -ne 2 ] && ogRaiseError $OG_ERR_FORMAT && return $?
647# Devuelve el tamaño de la imagen en KB.
648IMGTYPE=$(ogGetImageType "$1" "$2")
649IMGFILE=$(ogGetPath "$1" "$2.$IMGTYPE")
650case "$IMGTYPE" in
651    img) # Partimage.
652         partimage -B gui=no imginfo "$IMGFILE" 2>&1 | \
653                awk '/Partition size/ {sub(/\.\.+/," "); ps=$3} END {print ps*1024*1024;}'
654         ;;
655    pgz) # Partclone / GZip
656         gzip -dc "$IMGFILE" | partclone.chkimg -C -s - 2>&1 | \
657                awk -F: '/Block size/ {bs=$2} /Used block/ {ub=$2} END {print bs*ub/1024}'
658         ;;
659    *)   # Error si el fichero de imagen no es accesible.
660         ogRaiseError OG_ERR_NOTFOUND "$IMGFILE"
661         return $?  ;;
662esac
663}
664
665
666#### PRUEBAS
667# Obtener tipo de imagen
668function ogGetImageTypeUS ()
669{
670local IMGFILE IMGTYPE EXT
671for EXT in img pgz; do
672     IMGFILE=$(ogGetPath "$1" "$2.$EXT")
673     [ -r "$IMGFILE" ] && IMGTYPE="$EXT"
674done
675echo $IMGTYPE
676}
Note: See TracBrowser for help on using the repository browser.