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