[715bedc] | 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 | |
---|
[914d834] | 11 | # Bloquear imagen para creación. |
---|
| 12 | function ogLockImage () |
---|
| 13 | { |
---|
| 14 | # Variables locales |
---|
| 15 | local IMGDIR |
---|
| 16 | |
---|
| 17 | # Si se solicita, mostrar ayuda. |
---|
| 18 | if [ "$*" == "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 |
---|
| 23 | fi |
---|
| 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 |
---|
| 27 | IMGDIR=$(ogGetParentPath $@) || return $? |
---|
| 28 | # Crear fichero de bloqueo. |
---|
| 29 | touch $IMGDIR/$(basename "${!#}").lock |
---|
| 30 | } |
---|
| 31 | |
---|
| 32 | # Desbloquear imagen (borrar fichero de bloqueo). |
---|
| 33 | function ogUnlockImage () |
---|
| 34 | { |
---|
| 35 | # Variables locales |
---|
| 36 | local IMGDIR |
---|
| 37 | |
---|
| 38 | # Si se solicita, mostrar ayuda. |
---|
| 39 | if [ "$*" == "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 |
---|
| 44 | fi |
---|
| 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. |
---|
| 48 | rm -f $(ogGetPath $@.lock) |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | # Comprobar si el fichero de bloqueo para una imagen. |
---|
| 52 | function ogIsImageLocked () |
---|
| 53 | { |
---|
| 54 | # Variables locales |
---|
| 55 | local IMGDIR |
---|
| 56 | |
---|
| 57 | # Si se solicita, mostrar ayuda. |
---|
| 58 | if [ "$*" == "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 |
---|
| 63 | fi |
---|
| 64 | # Comprobar si existe el fichero de bloqueo. |
---|
| 65 | test -n "$(ogGetPath $@.lock)" |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | |
---|
| 69 | function ogPartcloneSyntax () |
---|
| 70 | { |
---|
| 71 | #TODO: comprobar como unico parametro particion /dev/sda1 |
---|
| 72 | #COMPAR="partclone.$FS --clone --force --source $PART" |
---|
| 73 | COMPAR="-F -c -s " |
---|
| 74 | TYPE="$(ogGetFsType `ogDevToDisk $1`)" |
---|
| 75 | case "$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 $? ;; |
---|
| 93 | esac |
---|
| 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 | |
---|
| 113 | function ogCreateImageSyntax() |
---|
| 114 | { |
---|
| 115 | local FS TOOL LEVEL PART IMGFILE BUFFER |
---|
| 116 | |
---|
| 117 | # Si se solicita, mostrar ayuda. |
---|
| 118 | if [ "$*" == "help" ]; then |
---|
| 119 | ogHelp "$FUNCNAME" "$FUNCNAME partition filename [tool] [levelcompresor]" \ |
---|
| 120 | "$FUNCNAME /dev/sda1 /opt/opengnsys/images/prueba.img partclone lzop" |
---|
| 121 | return |
---|
| 122 | fi |
---|
| 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 | |
---|
| 127 | PART=`echo $1` |
---|
| 128 | IMGFILE=`echo $2` |
---|
| 129 | |
---|
| 130 | case "$#" 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 | ;; |
---|
| 179 | esac |
---|
| 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 | |
---|
| 200 | ogRestoreImageSyntax () |
---|
| 201 | { |
---|
| 202 | local TOOL COMPRESSOR LEVEL PART IMGFILE FILEHEAD INFOIMG |
---|
| 203 | |
---|
| 204 | |
---|
| 205 | # Si se solicita, mostrar ayuda. |
---|
| 206 | if [ "$*" == "help" ]; then |
---|
| 207 | ogHelp "$FUNCNAME" "$FUNCNAME filename partition [tool] [levelcompresor]" \ |
---|
| 208 | "$FUNCNAME /opt/opengnsys/images/prueba.img /dev/sda1 [partclone] [lzop]" |
---|
| 209 | return |
---|
| 210 | fi |
---|
| 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) |
---|
| 219 | if [ "$#" -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 |
---|
| 226 | fi |
---|
| 227 | |
---|
| 228 | |
---|
| 229 | # Si cuatro parametros genera sintaxis |
---|
| 230 | if [ "$#" -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" |
---|
| 277 | fi |
---|
| 278 | |
---|
| 279 | } |
---|
| 280 | |
---|
| 281 | |
---|
| 282 | |
---|
[715bedc] | 283 | |
---|
| 284 | #/** |
---|
[ebf06c7] | 285 | # ogCreateImage int_ndisk int_npartition str_repo path_image |
---|
[715bedc] | 286 | #@brief Crea una imagen a partir de una partición. |
---|
[42669ebf] | 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) |
---|
[715bedc] | 291 | #@return (nada, por determinar) |
---|
[ebf06c7] | 292 | #@note repo = { REPO, CACHE } |
---|
[cfeabbf] | 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. |
---|
[715bedc] | 298 | #@warning En pruebas iniciales |
---|
[3458879] | 299 | #@todo Comprobaciones, control de errores, definir parámetros, etc. |
---|
[985bef0] | 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 |
---|
[0fbc05e] | 305 | #@version 0.9 - Versión en pruebas para OpenGnSys |
---|
[715bedc] | 306 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
[cfeabbf] | 307 | #@date 2009/10/07 |
---|
[1e7eaab] | 308 | #*/ ## |
---|
[42669ebf] | 309 | function ogCreateImage () |
---|
| 310 | { |
---|
[59f9ad2] | 311 | # Variables locales |
---|
[08b941f] | 312 | local PART PROGRAM IMGDIR IMGFILE IMGTYPE ERRCODE |
---|
[59f9ad2] | 313 | |
---|
[42669ebf] | 314 | # Si se solicita, mostrar ayuda. |
---|
[59f9ad2] | 315 | if [ "$*" == "help" ]; then |
---|
| 316 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npart path_dir str_image" \ |
---|
[3543b3e] | 317 | "$FUNCNAME 1 1 REPO /aula1/winxp" |
---|
[59f9ad2] | 318 | return |
---|
| 319 | fi |
---|
[c136ae7] | 320 | # Error si no se reciben 4 parámetros. |
---|
[914d834] | 321 | [ $# -lt 4 ] && ogRaiseError $OG_ERR_FORMAT && return $? |
---|
[59f9ad2] | 322 | |
---|
[08b941f] | 323 | # Comprobar que no está bloqueada ni la partición, ni la imagen. |
---|
[715bedc] | 324 | PART="$(ogDiskToDev $1 $2)" || return $? |
---|
[a79dd508] | 325 | if ogIsLocked $1 $2; then |
---|
[08b941f] | 326 | ogRaiseError $OG_ERR_LOCKED "$MSG_PARTITION $1, $2" |
---|
[a79dd508] | 327 | return $? |
---|
| 328 | fi |
---|
[914d834] | 329 | IMGTYPE="img" # Extensión genérica de imágenes. |
---|
[cfeabbf] | 330 | IMGDIR=$(ogGetParentPath "$3" "$4") |
---|
[914d834] | 331 | |
---|
[cfeabbf] | 332 | [ -n "$IMGDIR" ] || ogRaiseError $OG_ERR_NOTFOUND "$3 $(dirname $4)" || return $? |
---|
[08b941f] | 333 | IMGFILE="$IMGDIR/$(basename "$4").$IMGTYPE" |
---|
| 334 | if ogIsImageLocked "$IMGFILE"; then |
---|
| 335 | ogRaiseError $OG_ERR_LOCKED "$MSG_IMAGE $3, $4" |
---|
| 336 | return $? |
---|
| 337 | fi |
---|
| 338 | # Desmontar partición, bloquear partición e imagen. |
---|
[cfeabbf] | 339 | ogUnmount $1 $2 2>/dev/null |
---|
[a79dd508] | 340 | ogLock $1 $2 || return $? |
---|
[08b941f] | 341 | ogLockImage "$3" "$4.$IMGTYPE" || return $? |
---|
[715bedc] | 342 | |
---|
[08b941f] | 343 | # Crear Imagen. |
---|
| 344 | trap "ogUnlock $1 $2; ogUnlockImage "$3" "$4.$IMGTYPE"; rm -f $IMGFILE" 1 2 3 6 9 |
---|
[914d834] | 345 | #Solicitamos la generación de la instruccion a ejecutar |
---|
| 346 | PROGRAM=`ogCreateImageSyntax $PART $IMGFILE $5 $6` |
---|
| 347 | echo $PROGRAM |
---|
| 348 | eval $PROGRAM |
---|
[08b941f] | 349 | |
---|
[42669ebf] | 350 | # Controlar salida de error y desbloquear partición. |
---|
[cfeabbf] | 351 | ERRCODE=$? |
---|
[f5432db7] | 352 | if [ $ERRCODE != 0 ]; then |
---|
[cfeabbf] | 353 | ogRaiseError $OG_ERR_IMAGE "$1 $2 $IMGFILE" |
---|
[f5432db7] | 354 | rm -f "$IMGFILE" |
---|
[cfeabbf] | 355 | fi |
---|
[08b941f] | 356 | # Desbloquear partición e imagen. |
---|
[715bedc] | 357 | ogUnlock $1 $2 |
---|
[08b941f] | 358 | ogUnlockImage "$3" "$4.$IMGTYPE" |
---|
[cfeabbf] | 359 | return $ERRCODE |
---|
[715bedc] | 360 | } |
---|
| 361 | |
---|
[b094c59] | 362 | |
---|
[914d834] | 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 | #*/ ## |
---|
| 391 | function ogRestoreImage () |
---|
| 392 | { |
---|
| 393 | # Variables locales |
---|
| 394 | local PART PARTSIZE IMGFILE IMGTYPE IMGSIZE FSTYPE |
---|
| 395 | |
---|
| 396 | # Si se solicita, mostrar ayuda. |
---|
| 397 | if [ "$*" == "help" ]; then |
---|
| 398 | ogHelp "$FUNCNAME" "$FUNCNAME path_dir str_image int_ndisk int_npart" \ |
---|
| 399 | "$FUNCNAME REPO /aula1/winxp 1 1" |
---|
| 400 | return |
---|
| 401 | fi |
---|
| 402 | # Error si no se reciben 4 parámetros. |
---|
| 403 | [ $# -lt 4 ] && ogRaiseError $OG_ERR_FORMAT && return $? |
---|
| 404 | # Procesar parámetros. |
---|
| 405 | PART="$(ogDiskToDev "$3" "$4")" || return $? |
---|
| 406 | #IMGTYPE=$(ogGetImageType "$1" "$2") |
---|
| 407 | IMGTYPE=img |
---|
| 408 | IMGFILE=$(ogGetPath "$1" "$2.$IMGTYPE") |
---|
| 409 | [ -r "$IMGFILE" ] || ogRaiseError OG_ERR_NOTFOUND "$IMGFILE" || return $? |
---|
| 410 | # Error si la imagen no cabe en la particion. |
---|
| 411 | IMGSIZE=$(ogGetImageSize "$1" "$2") |
---|
| 412 | PARTSIZE=$(ogGetPartitionSize $3 $4) |
---|
| 413 | if [ $IMGSIZE -gt $PARTSIZE ]; then |
---|
| 414 | ogRaiseError $OG_ERR_PARTITION "$IMGSIZE > $PARTSIZE" |
---|
| 415 | return $? |
---|
| 416 | fi |
---|
| 417 | |
---|
| 418 | # Comprobar el bloqueo de la imagen y de la partición. |
---|
| 419 | if ogIsImageLocked "$IMGFILE"; then |
---|
| 420 | ogRaiseError $OG_ERR_LOCKED "$MSG_IMAGE $1, $2.$IMGTYPE" |
---|
| 421 | return $? |
---|
| 422 | fi |
---|
| 423 | if ogIsLocked $3 $4; then |
---|
| 424 | ogRaiseError $OG_ERR_LOCKED "$MSG_PARTITION $3, $4" |
---|
| 425 | return $? |
---|
| 426 | fi |
---|
| 427 | # Desmontar y bloquear partición. |
---|
| 428 | ogUnmount $3 $4 2>/dev/null || return $? |
---|
| 429 | ogLock $3 $4 || return $? |
---|
| 430 | trap "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 |
---|
| 436 | PROGRAM=`ogRestoreImageSyntax $IMGFILE $PART` |
---|
| 437 | echo $PROGRAM |
---|
| 438 | eval $PROGRAM |
---|
| 439 | |
---|
| 440 | ERRCODE=$? |
---|
| 441 | if [ $ERRCODE != 0 ]; then |
---|
| 442 | ogRaiseError $OG_ERR_IMAGE "$IMGFILE, $3, $4" |
---|
| 443 | fi |
---|
| 444 | ogUnlock $3 $4 |
---|
| 445 | return $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 | |
---|
| 466 | function ogGetImageInfo () { |
---|
| 467 | # Si se solicita, mostrar ayuda. |
---|
| 468 | if [ "$*" == "help" ]; then |
---|
| 469 | ogHelp "$FUNCNAME" "$FUNCNAME filename " \ |
---|
| 470 | "$FUNCNAME /opt/opengnsys/images/prueba.img " |
---|
| 471 | return |
---|
| 472 | fi |
---|
| 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 | |
---|
| 480 | local TOOLS COMPRESSOR IMGFILE FILEHEAD FS SIZE PARTIMAGEINFO PARTCLONEINFO NTFSCLONEINFO IMGDETECT |
---|
| 481 | IMGDETECT="FALSE" |
---|
| 482 | |
---|
| 483 | IMGFILE=$1 |
---|
| 484 | FILEHEAD=/tmp/`basename $IMGFILE`.infohead |
---|
| 485 | COMPRESSOR=`file $IMGFILE | awk '{print $2}'` |
---|
| 486 | ogCheckStringInGroup "$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. |
---|
| 490 | if [ "$IMGDETECT" == "FALSE" ] |
---|
| 491 | then |
---|
| 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 |
---|
| 500 | fi |
---|
| 501 | #buscando segunda opcion. |
---|
| 502 | if [ "$IMGDETECT" == "FALSE" -a ! -f /dev/loop2 ] |
---|
| 503 | then |
---|
| 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 |
---|
| 512 | fi |
---|
| 513 | ## buscando Tercer opción. |
---|
| 514 | if [ "$IMGDETECT" == "FALSE" ] |
---|
| 515 | then |
---|
| 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 |
---|
| 524 | fi |
---|
| 525 | #comprobamos valores #Chequeamos los valores devueltos. |
---|
| 526 | if [ -z "$TOOLS" -o -z "$COMPRESSOR" -o "$IMGDETECT" == "FALSE" ] |
---|
| 527 | then |
---|
| 528 | ogRaiseError $OG_ERR_IMAGE "Image format is not valid $IMGFILE" || return $? |
---|
| 529 | else |
---|
| 530 | COMPRESSOR=$(echo $COMPRESSOR | tr [a-z] [A-Z]) |
---|
| 531 | echo $TOOLS:$COMPRESSOR:$FS:$SIZE |
---|
| 532 | fi |
---|
| 533 | } |
---|
| 534 | |
---|
| 535 | function ogGetImageProgram () |
---|
| 536 | { |
---|
| 537 | local IMGFILE |
---|
| 538 | IMGFILE=$(ogGetPath "$1" "$2.img") || return $? |
---|
| 539 | [ -r "$IMGFILE" ] || ogRaiseError OG_ERR_NOTFOUND "$IMGFILE" || return $? |
---|
| 540 | ogGetImageInfo $IMGFILE | awk -F: '{print $1}' |
---|
| 541 | |
---|
| 542 | } |
---|
| 543 | |
---|
| 544 | function ogGetImageCompressor () |
---|
| 545 | { |
---|
| 546 | local IMGFILE |
---|
| 547 | IMGFILE=$(ogGetPath "$1" "$2.img") || return $? |
---|
| 548 | [ -r "$IMGFILE" ] || ogRaiseError OG_ERR_NOTFOUND "$IMGFILE" || return $? |
---|
| 549 | ogGetImageInfo $IMGFILE | awk -F: '{print $2}' |
---|
| 550 | } |
---|
| 551 | |
---|
| 552 | function ogGetImageType () |
---|
| 553 | { |
---|
| 554 | local IMGFILE |
---|
| 555 | IMGFILE=$(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);}' |
---|
| 559 | ogGetImageInfo $IMGFILE | awk -F: '{print $3}' |
---|
| 560 | |
---|
| 561 | } |
---|
| 562 | |
---|
| 563 | function ogGetImageSize () |
---|
| 564 | { |
---|
| 565 | # Variables locales |
---|
| 566 | local IMGFILE |
---|
| 567 | |
---|
| 568 | # Si se solicita, mostrar ayuda. |
---|
| 569 | if [ "$*" == "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 |
---|
| 573 | fi |
---|
| 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. |
---|
| 577 | IMGFILE=$(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;}' |
---|
| 583 | ogGetImageInfo $IMGFILE | awk -F: '{print $4}' |
---|
| 584 | } |
---|
| 585 | |
---|
| 586 | |
---|
[b094c59] | 587 | #/** |
---|
[0fbc05e] | 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 | #/** |
---|
[914d834] | 597 | function ogGetImageFsUS () |
---|
[0fbc05e] | 598 | { |
---|
| 599 | local IMGFILE IMGTYPE |
---|
| 600 | IMGTYPE=$(ogGetImageType "$1" "$2") |
---|
| 601 | IMGFILE=$(ogGetPath "$1" "$2.$IMGTYPE") || return $? |
---|
| 602 | [ -r "$IMGFILE" ] || ogRaiseError OG_ERR_NOTFOUND "$IMGFILE" || return $? |
---|
| 603 | case "$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 $? ;; |
---|
| 616 | esac |
---|
| 617 | } |
---|
| 618 | |
---|
| 619 | |
---|
[39277f4] | 620 | # ogGetImageSize str_repo path_image |
---|
[b094c59] | 621 | #@brief Devuelve el tamaño del sistema de archivos almacenado en un fichero de imagen. |
---|
[42669ebf] | 622 | #@param str_repo repositorio de imágenes o caché local |
---|
| 623 | #@param path_image camino de la imagen |
---|
[0fbc05e] | 624 | #@return int_size - tamaño (en KB) |
---|
[b094c59] | 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 |
---|
[1e7eaab] | 633 | #*/ ## |
---|
[914d834] | 634 | function ogGetImageSizeUS () |
---|
[42669ebf] | 635 | { |
---|
[b094c59] | 636 | # Variables locales |
---|
[0fbc05e] | 637 | local IMGFILE IMGTYPE |
---|
[b094c59] | 638 | |
---|
[42669ebf] | 639 | # Si se solicita, mostrar ayuda. |
---|
[b094c59] | 640 | if [ "$*" == "help" ]; then |
---|
| 641 | ogHelp "$FUNCNAME" "$FUNCNAME path_dir str_image int_ndisk int_npart" \ |
---|
[3543b3e] | 642 | "$FUNCNAME 1 1 REPO /aula1/winxp ==> 5642158" |
---|
[b094c59] | 643 | return |
---|
| 644 | fi |
---|
[42669ebf] | 645 | # Error si no se reciben menos de 2 parámetros. |
---|
[0fbc05e] | 646 | [ $# -ne 2 ] && ogRaiseError $OG_ERR_FORMAT && return $? |
---|
[42669ebf] | 647 | # Devuelve el tamaño de la imagen en KB. |
---|
[0fbc05e] | 648 | IMGTYPE=$(ogGetImageType "$1" "$2") |
---|
| 649 | IMGFILE=$(ogGetPath "$1" "$2.$IMGTYPE") |
---|
| 650 | case "$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 $? ;; |
---|
| 662 | esac |
---|
[b094c59] | 663 | } |
---|
| 664 | |
---|
| 665 | |
---|
[39277f4] | 666 | #### PRUEBAS |
---|
| 667 | # Obtener tipo de imagen |
---|
[914d834] | 668 | function ogGetImageTypeUS () |
---|
[42669ebf] | 669 | { |
---|
[0fbc05e] | 670 | local IMGFILE IMGTYPE EXT |
---|
| 671 | for EXT in img pgz; do |
---|
| 672 | IMGFILE=$(ogGetPath "$1" "$2.$EXT") |
---|
| 673 | [ -r "$IMGFILE" ] && IMGTYPE="$EXT" |
---|
| 674 | done |
---|
| 675 | echo $IMGTYPE |
---|
[39277f4] | 676 | } |
---|