| 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 1.0.5 |
|---|
| 8 | #@warning License: GNU GPLv3+ |
|---|
| 9 | #*/ |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | #/** |
|---|
| 14 | # ogCreateImageSyntax path_device path_filename [str_tool] [str_compressionlevel] |
|---|
| 15 | #@brief Genera una cadena de texto con la instrucción para crear un fichero imagen |
|---|
| 16 | #@param path_device dispositivo Linux del sistema de archivos |
|---|
| 17 | #@param path_fileneme path absoluto del fichero imagen |
|---|
| 18 | #@param [opcional] str_tool herrmaienta de clonacion [partimage, partclone, ntfsclone] |
|---|
| 19 | #@param [opcional] str_compressionlevel nivel de compresion. [0 -none-, 1-lzop-, 2-gzip] |
|---|
| 20 | #@return str_command - cadena con el comando que se debe ejecutar. |
|---|
| 21 | #@warning Salida nula si se producen errores. |
|---|
| 22 | #@TODO introducir las herramientas fsarchiver, dd |
|---|
| 23 | #@version 1.0 - Primeras pruebas |
|---|
| 24 | #@author Antonio J. Doblas Viso. Universidad de Málaga |
|---|
| 25 | #@date 2010/02/08 |
|---|
| 26 | #@version 1.0.5 - Incrustar códico de antigua función ogPartcloneSyntax |
|---|
| 27 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
|---|
| 28 | #@date 2012/09/14 |
|---|
| 29 | #*/ ## |
|---|
| 30 | function ogCreateImageSyntax() |
|---|
| 31 | { |
|---|
| 32 | local FS TOOL LEVEL PART IMGFILE BUFFER PARAM1 PARAM2 PARAM3 |
|---|
| 33 | |
|---|
| 34 | # Si se solicita, mostrar ayuda. |
|---|
| 35 | if [ "$*" == "help" ]; then |
|---|
| 36 | ogHelp "$FUNCNAME" "$FUNCNAME path_device path_imagefile [str_tool] [str_compressionlevel]" \ |
|---|
| 37 | "$FUNCNAME /dev/sda1 /opt/opengnsys/images/prueba.img partclone lzop" \ |
|---|
| 38 | "$FUNCNAME /dev/sda1 /opt/opengnsys/images/prueba.img" |
|---|
| 39 | return |
|---|
| 40 | fi |
|---|
| 41 | # Error si no se reciben entre 2 y 4 parámetros. |
|---|
| 42 | [ $# -ge 2 -a $# -le 4 ] || ogRaiseError $OG_ERR_FORMAT "$*" || return $? |
|---|
| 43 | |
|---|
| 44 | # Asignación de parámetros. |
|---|
| 45 | PART="$1" |
|---|
| 46 | IMGFILE="$2" |
|---|
| 47 | case "$#" in |
|---|
| 48 | 2) # Sintaxis por defecto OG PART IMGFILE |
|---|
| 49 | TOOL="partclone" |
|---|
| 50 | LEVEL="gzip" |
|---|
| 51 | ;; |
|---|
| 52 | 4) # Sintaxis condicionada. |
|---|
| 53 | TOOL="${3,,}" |
|---|
| 54 | LEVEL="${4,,}" |
|---|
| 55 | ;; |
|---|
| 56 | esac |
|---|
| 57 | |
|---|
| 58 | case "$TOOL" in |
|---|
| 59 | ntfsclone) |
|---|
| 60 | PARAM1="ntfsclone --force --save-image -O - $PART" |
|---|
| 61 | ;; |
|---|
| 62 | partimage|default) |
|---|
| 63 | PARAM1="partimage -M -f3 -o -d -B gui=no -c -z0 --volume=0 save $PART stdout" |
|---|
| 64 | ;; |
|---|
| 65 | partclone) |
|---|
| 66 | FS="$(ogGetFsType $(ogDevToDisk $PART 2>/dev/null) 2>/dev/null)" |
|---|
| 67 | umount $PART 2>/dev/null |
|---|
| 68 | case "$FS" in |
|---|
| 69 | EXT[234]) PARAM1="partclone.extfs" ;; |
|---|
| 70 | BTRFS) PARAM1="partclone.btrfs" ;; |
|---|
| 71 | REISERFS) PARAM1="partclone.reiserfs" ;; |
|---|
| 72 | REISER4) PARAM1="partclone.reiser4" ;; |
|---|
| 73 | JFS) PARAM1="partclone.jfs" ;; |
|---|
| 74 | XFS) PARAM1="partclone.xfs" ;; |
|---|
| 75 | NTFS) PARAM1="partclone.ntfs" ;; |
|---|
| 76 | EXFAT) PARAM1="partclone.exfat" ;; |
|---|
| 77 | FAT16|FAT32) PARAM1="partclone.fat" ;; |
|---|
| 78 | HFS|HFSPLUS) PARAM1="partclone.hfsp" ;; |
|---|
| 79 | UFS) PARAM1="partclone.ufs" ;; |
|---|
| 80 | *) PARAM1="partclone.dd" ;; |
|---|
| 81 | esac |
|---|
| 82 | # Por compatibilidad, si no existe el ejecutable usar por defecto "parclone.dd". |
|---|
| 83 | which $PARAM1 &>/dev/null || PARAM1="partclone.dd" |
|---|
| 84 | PARAM1="$PARAM1 -d0 -F -c -s $PART" |
|---|
| 85 | ;; |
|---|
| 86 | esac |
|---|
| 87 | # Comprobar que existe mbuffer. |
|---|
| 88 | which mbuffer &>/dev/null && PARAM2="| mbuffer -q -m 40M " || PARAM2=" " |
|---|
| 89 | |
|---|
| 90 | # Nivel de compresion. |
|---|
| 91 | case "$LEVEL" in |
|---|
| 92 | 0|none) PARAM3=" > " ;; |
|---|
| 93 | 1|lzop) PARAM3=" | lzop > " ;; |
|---|
| 94 | 2|gzip) PARAM3=" | gzip -c > " ;; |
|---|
| 95 | 3|bzip) PARAM3=" | bzip -c > " ;; |
|---|
| 96 | esac |
|---|
| 97 | |
|---|
| 98 | # Sintaxis final. |
|---|
| 99 | [ -n "$PARAM1" ] && echo "$PARAM1 $PARAM2 $PARAM3 $IMGFILE" |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | #/** |
|---|
| 104 | # ogRestoreImageSyntax path_filename path_device [str_tools] [str_compressionlevel] |
|---|
| 105 | #@brief Genera una cadena de texto con la instrucción para crear un fichero imagen |
|---|
| 106 | #@param path_device dispositivo Linux del sistema de archivos |
|---|
| 107 | #@param path_fileneme path absoluto del fichero imagen |
|---|
| 108 | #@param [opcional] str_tools herrmaienta de clonacion [partimage, partclone, ntfsclone] |
|---|
| 109 | #@param [opcional] str_compressionlevel nivel de compresion. [0 -none-, 1-lzop-, 2-gzip] |
|---|
| 110 | #@return cadena con el comando que se debe ejecutar. |
|---|
| 111 | #@exception OG_ERR_FORMAT formato incorrecto. |
|---|
| 112 | #@warning En pruebas iniciales |
|---|
| 113 | #@TODO introducir las herramientas fsarchiver, dd |
|---|
| 114 | #@TODO introducir el nivel de compresion gzip |
|---|
| 115 | #@version 1.0 - Primeras pruebas |
|---|
| 116 | #@author Antonio J. Doblas Viso. Universidad de Málaga |
|---|
| 117 | #@date 2010/02/08 |
|---|
| 118 | #*/ ## |
|---|
| 119 | |
|---|
| 120 | ogRestoreImageSyntax () |
|---|
| 121 | { |
|---|
| 122 | local TOOL COMPRESSOR LEVEL PART IMGFILE FILEHEAD INFOIMG |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | # Si se solicita, mostrar ayuda. |
|---|
| 126 | if [ "$*" == "help" ]; then |
|---|
| 127 | ogHelp "$FUNCNAME" "$FUNCNAME filename partition [tool] [levelcompresor]" \ |
|---|
| 128 | "$FUNCNAME /opt/opengnsys/images/prueba.img /dev/sda1 [partclone] [lzop]" |
|---|
| 129 | return |
|---|
| 130 | fi |
|---|
| 131 | |
|---|
| 132 | # Error si no se reciben entre 2 y 4 parámetros. |
|---|
| 133 | [ $# -ge 2 -a $# -le 4 ] || ogRaiseError $OG_ERR_FORMAT "$*" || return $? |
|---|
| 134 | |
|---|
| 135 | # controlamos que el parametro 1 (imagen) es tipo file. |
|---|
| 136 | [ -f $1 ] || ogRaiseError $OG_ERR_NOTFOUND "$1" || return $? |
|---|
| 137 | |
|---|
| 138 | # Si 2 parametros (file-origen-, device-destino-) = ogGetImageFull($1) |
|---|
| 139 | if [ "$#" -eq 2 ]; then |
|---|
| 140 | IMGFILE=$1 |
|---|
| 141 | PART=$2 |
|---|
| 142 | INFOIMG=$(ogGetImageInfo $IMGFILE) || ogRaiseError $OG_ERR_NOTFOUND "No Image $1" || return $? |
|---|
| 143 | TOOL=`echo $INFOIMG | cut -f1 -d:` |
|---|
| 144 | COMPRESSOR=`echo $INFOIMG | cut -f2 -d:` |
|---|
| 145 | ogRestoreImageSyntax $IMGFILE $PART $TOOL $COMPRESSOR |
|---|
| 146 | fi |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | # Si cuatro parametros genera sintaxis |
|---|
| 150 | if [ "$#" -eq 4 ]; then |
|---|
| 151 | IMGFILE=$1 |
|---|
| 152 | PART=$2 |
|---|
| 153 | # comprobamos parametro herramienta compresion. |
|---|
| 154 | TOOL=$(echo $3 | tr [A-Z] [a-z]) |
|---|
| 155 | #ogCheckProgram $TOOL |
|---|
| 156 | #comprobar parámetro compresor. |
|---|
| 157 | LEVEL=$(echo $4 | tr [A-Z] [a-z]) |
|---|
| 158 | #ogCheckProgram $LEVEL |
|---|
| 159 | |
|---|
| 160 | case "$LEVEL" in |
|---|
| 161 | "0"|"none") |
|---|
| 162 | COMPRESSOR=" " |
|---|
| 163 | ;; |
|---|
| 164 | "1"|"lzop" | "LZOP") |
|---|
| 165 | COMPRESSOR=" lzop -dc " |
|---|
| 166 | ;; |
|---|
| 167 | "2"|"gzip" | "GZIP") |
|---|
| 168 | COMPRESSOR=" gzip -dc " |
|---|
| 169 | ;; |
|---|
| 170 | "3"|"bzip" | "BZIP" ) |
|---|
| 171 | COMPRESSOR=" bzip -dc " |
|---|
| 172 | ;; |
|---|
| 173 | *) |
|---|
| 174 | ogRaiseError $OG_ERR_NOTFOUND "Compressor no valid $TOOL" || return $? |
|---|
| 175 | ;; |
|---|
| 176 | esac |
|---|
| 177 | #comprobar mbuffer |
|---|
| 178 | which mbuffer > /dev/null && MBUFFER="| mbuffer -q -m 40M " || MBUFFER=" " |
|---|
| 179 | |
|---|
| 180 | case "$TOOL" in |
|---|
| 181 | "ntfsclone" | "NTFSCLONE") |
|---|
| 182 | TOOL="| ntfsclone --restore-image --overwrite $PART -" |
|---|
| 183 | ;; |
|---|
| 184 | "partimage"| "PARTIMAGE") |
|---|
| 185 | TOOL="| partimage -f3 -B gui=no restore $PART stdin" |
|---|
| 186 | ;; |
|---|
| 187 | "partclone" | "PARTCLONE") |
|---|
| 188 | # -C para que no compruebe tamaños |
|---|
| 189 | TOOL="| partclone.restore -d0 -C -I -o $PART" |
|---|
| 190 | ;; |
|---|
| 191 | *) |
|---|
| 192 | ogRaiseError $OG_ERR_NOTFOUND "Tools imaging no valid $TOOL" || return $? |
|---|
| 193 | ;; |
|---|
| 194 | esac |
|---|
| 195 | |
|---|
| 196 | echo "$COMPRESSOR $IMGFILE $MBUFFER $TOOL" |
|---|
| 197 | fi |
|---|
| 198 | |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 202 | |
|---|
| 203 | |
|---|
| 204 | #/** |
|---|
| 205 | # ogCreateImage int_ndisk int_npartition str_repo path_image [str_tools] [str_compressionlevel] |
|---|
| 206 | #@brief Crea una imagen a partir de una partición. |
|---|
| 207 | #@param int_ndisk nº de orden del disco |
|---|
| 208 | #@param int_npartition nº de orden de la partición |
|---|
| 209 | #@param str_repo repositorio de imágenes (remoto o caché local) |
|---|
| 210 | #@param path_image camino de la imagen (sin extensión) |
|---|
| 211 | #@param [opcional] str_tools herrmaienta de clonacion [partimage, partclone, ntfsclone] |
|---|
| 212 | #@param [opcional] str_compressionlevel nivel de compresion. [0 -none-, 1-lzop-, 2-gzip] |
|---|
| 213 | #@return (nada, por determinar) |
|---|
| 214 | #@note repo = { REPO, CACHE } |
|---|
| 215 | #@exception OG_ERR_FORMAT formato incorrecto. |
|---|
| 216 | #@exception OG_ERR_NOTFOUND fichero o dispositivo no encontrado. |
|---|
| 217 | #@exception OG_ERR_PARTITION partición no accesible o no soportada. |
|---|
| 218 | #@exception OG_ERR_LOCKED particion bloqueada por otra operación. |
|---|
| 219 | #@exception OG_ERR_IMAGE error al crear la imagen del sistema. |
|---|
| 220 | #@warning En pruebas iniciales |
|---|
| 221 | #@todo Comprobaciones, control de errores, definir parámetros, etc. |
|---|
| 222 | #@version 0.1 - Integracion para Opengnsys - HIDRA:CrearImagen{EXT3, NTFS}.sh; EAC: CreateImageFromPartition () en Deploy.lib |
|---|
| 223 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
|---|
| 224 | #@Date 2008/05/13 |
|---|
| 225 | #@author Antonio J. Doblas Viso. Universidad de Malaga |
|---|
| 226 | #@date 2008/10/27 |
|---|
| 227 | #@version 0.9 - Versión en pruebas para OpenGnSys |
|---|
| 228 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
|---|
| 229 | #@date 2009/10/07 |
|---|
| 230 | #@version 1.0 - Llama a función ogCreateImageSyntax para generar la llamada al comando. |
|---|
| 231 | #@author Antonio J. Doblas Viso. Universidad de Málaga |
|---|
| 232 | #@date 2010/02/08 |
|---|
| 233 | #*/ ## |
|---|
| 234 | function ogCreateImage () |
|---|
| 235 | { |
|---|
| 236 | # Variables locales |
|---|
| 237 | local PART PROGRAM IMGDIR IMGFILE IMGTYPE ERRCODE |
|---|
| 238 | |
|---|
| 239 | # Si se solicita, mostrar ayuda. |
|---|
| 240 | if [ "$*" == "help" ]; then |
|---|
| 241 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npart path_dir str_image" \ |
|---|
| 242 | "$FUNCNAME 1 1 REPO /aula1/winxp" |
|---|
| 243 | return |
|---|
| 244 | fi |
|---|
| 245 | # Error si no se reciben entre 4 y 6 parámetros. |
|---|
| 246 | [ $# -ge 4 -a $# -le 6 ] || ogRaiseError $OG_ERR_FORMAT "$*" || return $? |
|---|
| 247 | |
|---|
| 248 | # Comprobar que no está bloqueada ni la partición, ni la imagen. |
|---|
| 249 | PART="$(ogDiskToDev $1 $2)" || return $? |
|---|
| 250 | if ogIsLocked $1 $2; then |
|---|
| 251 | ogRaiseError $OG_ERR_LOCKED "$MSG_PARTITION $1, $2" |
|---|
| 252 | return $? |
|---|
| 253 | fi |
|---|
| 254 | |
|---|
| 255 | IMGTYPE="img" # Extensión genérica de imágenes. |
|---|
| 256 | IMGDIR=$(ogGetParentPath "$3" "$4") |
|---|
| 257 | [ -n "$IMGDIR" ] || ogRaiseError $OG_ERR_NOTFOUND "$3 $(dirname $4)" || return $? |
|---|
| 258 | |
|---|
| 259 | IMGFILE="$IMGDIR/$(basename "$4").$IMGTYPE" |
|---|
| 260 | if ogIsImageLocked "$IMGFILE"; then |
|---|
| 261 | ogRaiseError $OG_ERR_LOCKED "$MSG_IMAGE $3, $4" |
|---|
| 262 | return $? |
|---|
| 263 | fi |
|---|
| 264 | # Generar la instruccion a ejecutar antes de aplicar los bloqueos. |
|---|
| 265 | PROGRAM=$(ogCreateImageSyntax $PART $IMGFILE $5 $6) |
|---|
| 266 | # Desmontar partición, bloquear partición e imagen. |
|---|
| 267 | ogUnmount $1 $2 2>/dev/null |
|---|
| 268 | ogLock $1 $2 || return $? |
|---|
| 269 | ogLockImage "$3" "$4.$IMGTYPE" || return $? |
|---|
| 270 | |
|---|
| 271 | # Crear Imagen. |
|---|
| 272 | trap "ogUnlock $1 $2; ogUnlockImage "$3" "$4.$IMGTYPE"; rm -f $IMGFILE" 1 2 3 6 9 |
|---|
| 273 | eval $PROGRAM |
|---|
| 274 | |
|---|
| 275 | # Controlar salida de error y desbloquear partición. |
|---|
| 276 | ERRCODE=$? |
|---|
| 277 | if [ $ERRCODE != 0 ]; then |
|---|
| 278 | ogRaiseError $OG_ERR_IMAGE "$1 $2 $IMGFILE" |
|---|
| 279 | rm -f "$IMGFILE" |
|---|
| 280 | fi |
|---|
| 281 | # Desbloquear partición e imagen. |
|---|
| 282 | ogUnlock $1 $2 |
|---|
| 283 | ogUnlockImage "$3" "$4.$IMGTYPE" |
|---|
| 284 | return $ERRCODE |
|---|
| 285 | } |
|---|
| 286 | |
|---|
| 287 | |
|---|
| 288 | #/** |
|---|
| 289 | # ogCreateMbrImage int_ndisk str_repo path_image |
|---|
| 290 | #@brief Crea una imagen a partir del sector de arranque de un disco. |
|---|
| 291 | #@param int_ndisk nº de orden del disco |
|---|
| 292 | #@param str_repo repositorio de imágenes (remoto o caché local) |
|---|
| 293 | #@param path_image camino de la imagen (sin extensión) |
|---|
| 294 | #@return (nada, por determinar) |
|---|
| 295 | #@note repo = { REPO, CACHE } |
|---|
| 296 | #@exception OG_ERR_FORMAT formato incorrecto. |
|---|
| 297 | #@exception OG_ERR_NOTFOUND fichero o dispositivo no encontrado. |
|---|
| 298 | #@exception OG_ERR_IMAGE error al crear la imagen del sistema. |
|---|
| 299 | #@version 0.9 - Versión en pruebas para OpenGNSys |
|---|
| 300 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
|---|
| 301 | #@date 2010/01/12 |
|---|
| 302 | #@version 1.0 - Adaptación a OpenGnSys 1.0 |
|---|
| 303 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
|---|
| 304 | #@date 2011/03/10 |
|---|
| 305 | #*/ ## |
|---|
| 306 | function ogCreateMbrImage () |
|---|
| 307 | { |
|---|
| 308 | # Variables locales |
|---|
| 309 | local DISK IMGDIR IMGFILE |
|---|
| 310 | # Si se solicita, mostrar ayuda. |
|---|
| 311 | if [ "$*" == "help" ]; then |
|---|
| 312 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk path_dir str_image" \ |
|---|
| 313 | "$FUNCNAME 1 REPO /aula1/mbr" |
|---|
| 314 | return |
|---|
| 315 | fi |
|---|
| 316 | # Error si no se reciben 3 parámetros. |
|---|
| 317 | [ $# == 3 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
|---|
| 318 | |
|---|
| 319 | DISK=$(ogDiskToDev "$1") || return $? |
|---|
| 320 | IMGDIR=$(ogGetParentPath "$2" "$3") |
|---|
| 321 | [ -n "$IMGDIR" ] || ogRaiseError $OG_ERR_NOTFOUND "$2 $(dirname $3)" || return $? |
|---|
| 322 | IMGFILE="$IMGDIR/$(basename "$3").mbr" |
|---|
| 323 | |
|---|
| 324 | # Crear imagen del MBR. |
|---|
| 325 | dd if="$DISK" of="$IMGFILE" bs=512 count=1 || ogRaiseError $OG_ERR_IMAGE "$1 $IMGFILE" || return $? |
|---|
| 326 | } |
|---|
| 327 | |
|---|
| 328 | |
|---|
| 329 | #/** |
|---|
| 330 | # ogCreateBootLoaderImage int_ndisk str_repo path_image |
|---|
| 331 | #@brief Crea una imagen del boot loader a partir del sector de arranque de un disco. |
|---|
| 332 | #@param int_ndisk nº de orden del disco |
|---|
| 333 | #@param str_repo repositorio de imágenes (remoto o caché local) |
|---|
| 334 | #@param path_image camino de la imagen (sin extensión) |
|---|
| 335 | #@return (nada, por determinar) |
|---|
| 336 | #@note repo = { REPO, CACHE } |
|---|
| 337 | #@exception OG_ERR_FORMAT formato incorrecto. |
|---|
| 338 | #@exception OG_ERR_NOTFOUND fichero o dispositivo no encontrado. |
|---|
| 339 | #@exception OG_ERR_IMAGE error al crear la imagen del sistema. |
|---|
| 340 | #@version 1.0 - Adaptacion de ogCreateMbrImage para guardar solo el Boot Loader |
|---|
| 341 | #@author Juan Carlos Xifre, SICUZ Universidad de Zaragoza |
|---|
| 342 | #@date 2011/03/21 |
|---|
| 343 | #*/ ## |
|---|
| 344 | function ogCreateBootLoaderImage () |
|---|
| 345 | { |
|---|
| 346 | # Variables locales |
|---|
| 347 | local DISK IMGDIR IMGFILE |
|---|
| 348 | # Si se solicita, mostrar ayuda. |
|---|
| 349 | if [ "$*" == "help" ]; then |
|---|
| 350 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk path_dir str_image" \ |
|---|
| 351 | "$FUNCNAME 1 REPO /aula1/mbr" |
|---|
| 352 | return |
|---|
| 353 | fi |
|---|
| 354 | # Error si no se reciben 3 parámetros. |
|---|
| 355 | [ $# == 3 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
|---|
| 356 | |
|---|
| 357 | DISK=$(ogDiskToDev "$1") || return $? |
|---|
| 358 | IMGDIR=$(ogGetParentPath "$2" "$3") |
|---|
| 359 | [ -n "$IMGDIR" ] || ogRaiseError $OG_ERR_NOTFOUND "$2 $(dirname $3)" || return $? |
|---|
| 360 | IMGFILE="$IMGDIR/$(basename "$3").mbr" |
|---|
| 361 | |
|---|
| 362 | # Crear imagen del Boot Loader dentro del MBR. |
|---|
| 363 | dd if="$DISK" of="$IMGFILE" bs=446 count=1 || ogRaiseError $OG_ERR_IMAGE "$1 $IMGFILE" || return $? |
|---|
| 364 | } |
|---|
| 365 | |
|---|
| 366 | #/** |
|---|
| 367 | # ogGetSizeParameters int_num_disk int_num_part str_repo [monolit|sync|diff] |
|---|
| 368 | #@brief Devuelve el tamaño de los datos de un sistema de ficheros, el espacio necesario para la imagen y si cabe en el repositorio elegido. |
|---|
| 369 | #@param int_disk numero de disco |
|---|
| 370 | #@param int_part numero de particion |
|---|
| 371 | #@param str_repo repositorio de imágenes { REPO, CACHE } |
|---|
| 372 | #@param str_imageType Tipo de imagen: monolit (por defecto), sync o diff. (parametro opcional) |
|---|
| 373 | #@return SIZEDATA SIZEREQUIRED ISENOUGHSPACE |
|---|
| 374 | #@note si str_imageType= diff necesario /tmp/ogimg.info, que es creado por ogCreateInfoImage. |
|---|
| 375 | #@exception OG_ERR_FORMAT formato incorrecto. |
|---|
| 376 | #@author Irina Gomez, ETSII Universidad de Sevilla |
|---|
| 377 | #@date 2014/10/24 |
|---|
| 378 | #*/ ## |
|---|
| 379 | function ogGetSizeParameters () |
|---|
| 380 | { |
|---|
| 381 | local MNTDIR SIZEDATA KERNELVERSION SIZEREQUIRED FACTORGZIP FACTORLZOP SIZEFREE |
|---|
| 382 | # Si se solicita, mostrar ayuda. |
|---|
| 383 | if [ "$*" == "help" ]; then |
|---|
| 384 | ogHelp "$FUNCNAME" "$FUNCNAME num_disk num_part str_repo [monolic|sync|diff]" \ |
|---|
| 385 | "if $FUNCNAME 1 2 REPO sync ; then ...; fi" \ |
|---|
| 386 | "if $FUNCNAME 1 6 CACHE ; then ...; fi" |
|---|
| 387 | return |
|---|
| 388 | fi |
|---|
| 389 | # Error si no se reciben 1 o 2 parámetros. |
|---|
| 390 | [ $# -lt 3 ] && return $(ogRaiseError session $OG_ERR_FORMAT "$MSG_FORMAT: $PROG ndisco nparticion REPO|CACHE [monolitic|sync]" ; echo $?) |
|---|
| 391 | |
|---|
| 392 | MNTDIR=$(ogMount $1 $2) |
|---|
| 393 | if [ "$MNTDIR" == "" ]; then |
|---|
| 394 | ogRaiseError $OG_ERR_PARTITION "$1 $2" |
|---|
| 395 | return $? |
|---|
| 396 | fi |
|---|
| 397 | |
|---|
| 398 | # Datos contenidos en la particion o en la lista de archivos de contiene la diferencial. |
|---|
| 399 | if [ "_${4^^}_" == "_DIFF_" ]; then |
|---|
| 400 | [ -r /tmp/ogimg.info ] || return $(ogRaiseError session $OG_ERR_NOTFOUND "/tmp/ogimg.info"; echo $?) |
|---|
| 401 | cd $MNTDIR |
|---|
| 402 | SIZEDATA=$(grep -v "\/$" /tmp/ogimg.info | tr '\n' '\0'| du -x -c --files0-from=- 2>/dev/null|tail -n1 |cut -f1) |
|---|
| 403 | else |
|---|
| 404 | SIZEDATA=$(df -k | grep $MNTDIR | awk '{print $3}') |
|---|
| 405 | fi |
|---|
| 406 | |
|---|
| 407 | #Aplicar factor de compresion |
|---|
| 408 | if [ "_${4^^}_" == "_SYNC_" -o "_${4^^}_" == "_DIFF_" ]; then |
|---|
| 409 | |
|---|
| 410 | # Sistema de fichero de la imagen según kernel, menor que 3.7 EXT4. comparamos revision |
|---|
| 411 | KERNELVERSION=$(uname -r| awk '{printf("%d",$1);sub(/[0-9]*\./,"",$1);printf(".%02d",$1)}') |
|---|
| 412 | [ $KERNELVERSION \< 3.07 ] && IMGFS="EXT4" || IMGFS=${IMGFS:-"BTRFS"} |
|---|
| 413 | FACTORSYNC=${FACTORSYNC:-"120"} |
|---|
| 414 | # Si IMGFS="BTRFS" la compresion es mayor. |
|---|
| 415 | [ $IMGFS == "BTRFS" ] && let FACTORSYNC=$FACTORSYNC-20 |
|---|
| 416 | |
|---|
| 417 | let SIZEREQUIRED=$SIZEDATA*$FACTORSYNC/100 |
|---|
| 418 | # El tamaño mínimo del sistema de ficheros btrfs es 250M, ponemos 300 |
|---|
| 419 | [ $SIZEREQUIRED -lt 300000 ] && SIZEREQUIRED=300000 |
|---|
| 420 | |
|---|
| 421 | else |
|---|
| 422 | FACTORGZIP=55/100 |
|---|
| 423 | FACTORLZOP=65/100 |
|---|
| 424 | let SIZEREQUIRED=$SIZEDATA*$FACTORLZOP |
|---|
| 425 | fi |
|---|
| 426 | |
|---|
| 427 | #Comprobar espacio libre en el contenedor. |
|---|
| 428 | [ "${3^^}" == "CACHE" ] && SIZEFREE=$(ogGetFreeSize `ogFindCache`) |
|---|
| 429 | [ "${3^^}" == "REPO" ] && SIZEFREE=$(df -k | grep $OGIMG | awk '{print $4}') |
|---|
| 430 | |
|---|
| 431 | [ "$SIZEREQUIRED" -lt "$SIZEFREE" ] && ISENOUGHSPACE=TRUE || ISENOUGHSPACE=FALSE |
|---|
| 432 | |
|---|
| 433 | echo $SIZEDATA $SIZEREQUIRED $ISENOUGHSPACE |
|---|
| 434 | |
|---|
| 435 | |
|---|
| 436 | } |
|---|
| 437 | |
|---|
| 438 | #/** |
|---|
| 439 | # ogIsImageLocked [str_repo] path_image |
|---|
| 440 | #@brief Comprueba si una imagen está bloqueada para uso exclusivo. |
|---|
| 441 | #@param str_repo repositorio de imágenes (opcional) |
|---|
| 442 | #@param path_image camino de la imagen (sin extensión) |
|---|
| 443 | #@return Código de salida: 0 - bloqueado, 1 - sin bloquear o error. |
|---|
| 444 | #@note repo = { REPO, CACHE } |
|---|
| 445 | #@exception OG_ERR_FORMAT formato incorrecto. |
|---|
| 446 | #@version 1.0 - Adaptación a OpenGnSys 1.0 |
|---|
| 447 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
|---|
| 448 | #@date 2011/03/10 |
|---|
| 449 | #@version 1.0.1 - Devolver falso en caso de error. |
|---|
| 450 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
|---|
| 451 | #@date 2011-05-18 |
|---|
| 452 | #*/ ## |
|---|
| 453 | function ogIsImageLocked () |
|---|
| 454 | { |
|---|
| 455 | # Si se solicita, mostrar ayuda. |
|---|
| 456 | if [ "$*" == "help" ]; then |
|---|
| 457 | ogHelp "$FUNCNAME" "$FUNCNAME [str_repo] path_image" \ |
|---|
| 458 | "if $FUNCNAME /opt/opengnsys/images/aula1/winxp.img; then ...; fi" \ |
|---|
| 459 | "if $FUNCNAME REPO /aula1/winxp.img; then ...; fi" |
|---|
| 460 | return |
|---|
| 461 | fi |
|---|
| 462 | # Error si no se reciben 1 o 2 parámetros. |
|---|
| 463 | [ $# -lt 1 -o $# -gt 2 ] && return 1 |
|---|
| 464 | |
|---|
| 465 | # Comprobar si existe el fichero de bloqueo. |
|---|
| 466 | test -n "$(ogGetPath $@.lock)" |
|---|
| 467 | } |
|---|
| 468 | |
|---|
| 469 | |
|---|
| 470 | #/** |
|---|
| 471 | # ogLockImage [str_repo] path_image |
|---|
| 472 | #@brief Bloquea una imagen para uso exclusivo. |
|---|
| 473 | #@param str_repo repositorio de imágenes (opcional) |
|---|
| 474 | #@param path_image camino de la imagen (sin extensión) |
|---|
| 475 | #@return Nada. |
|---|
| 476 | #@note Se genera un fichero con extensión .lock |
|---|
| 477 | #@note repo = { REPO, CACHE } |
|---|
| 478 | #@exception OG_ERR_FORMAT formato incorrecto. |
|---|
| 479 | #@version 1.0 - Adaptación a OpenGnSys 1.0 |
|---|
| 480 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
|---|
| 481 | #@date 2011/03/10 |
|---|
| 482 | #*/ ## |
|---|
| 483 | function ogLockImage () |
|---|
| 484 | { |
|---|
| 485 | # Variables locales |
|---|
| 486 | local IMGDIR |
|---|
| 487 | |
|---|
| 488 | # Si se solicita, mostrar ayuda. |
|---|
| 489 | if [ "$*" == "help" ]; then |
|---|
| 490 | ogHelp "$FUNCNAME" "$FUNCNAME [str_repo] path_image" \ |
|---|
| 491 | "$FUNCNAME /opt/opengnsys/images/aula1/winxp.img" \ |
|---|
| 492 | "$FUNCNAME REPO /aula1/winxp.img" |
|---|
| 493 | return |
|---|
| 494 | fi |
|---|
| 495 | # Error si no se reciben 1 o 2 parámetros. |
|---|
| 496 | [ $# == 1 -o $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
|---|
| 497 | # Comprobar que existe directorio de imagen |
|---|
| 498 | IMGDIR=$(ogGetParentPath $@) || return $? |
|---|
| 499 | # Crear fichero de bloqueo. |
|---|
| 500 | touch $IMGDIR/$(basename "${!#}").lock 2>/dev/null || ogRaiseError $OG_ERR_NOTWRITE "$*" || return $? |
|---|
| 501 | } |
|---|
| 502 | |
|---|
| 503 | |
|---|
| 504 | #/** |
|---|
| 505 | # ogRestoreImage str_repo path_image int_ndisk int_npartition |
|---|
| 506 | #@brief Restaura una imagen de sistema de archivos en una partición. |
|---|
| 507 | #@param str_repo repositorio de imágenes o caché local |
|---|
| 508 | #@param path_image camino de la imagen |
|---|
| 509 | #@param int_ndisk nº de orden del disco |
|---|
| 510 | #@param int_npartition nº de orden de la partición |
|---|
| 511 | #@return (por determinar) |
|---|
| 512 | #@exception OG_ERR_FORMAT 1 formato incorrecto. |
|---|
| 513 | #@exception OG_ERR_NOTFOUND 2 fichero de imagen o partición no detectados. |
|---|
| 514 | #@exception OG_ERR_PARTITION 3 # Error en partición de disco. |
|---|
| 515 | #@exception OG_ERR_LOCKED 4 partición bloqueada por otra operación. |
|---|
| 516 | #@exception OG_ERR_IMAGE 5 error al restaurar la imagen del sistema. |
|---|
| 517 | #@exception OG_ERR_IMGSIZEPARTITION 30 Tamaño de la particion es menor al tamaño de la imagen. |
|---|
| 518 | #@todo Comprobar incongruencias partición-imagen, control de errores, definir parámetros, caché/repositorio, etc. |
|---|
| 519 | #@version 0.1 - Integracion para Opengnsys - HIDRA:RestaurarImagen{EXT3, NTFS}.sh; EAC: RestorePartitionFromImage() en Deploy.lib |
|---|
| 520 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
|---|
| 521 | #@Date 2008/05/13 |
|---|
| 522 | #@author Antonio J. Doblas Viso. Universidad de Malaga |
|---|
| 523 | #@date 2008/10/27 |
|---|
| 524 | #@version 0.9 - Primera version muy en pruebas para OpenGnSys |
|---|
| 525 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
|---|
| 526 | #@date 2009/09/10 |
|---|
| 527 | #@version 1.0 - generacion sintaxis de restauracion |
|---|
| 528 | #@author Antonio J. Doblas Viso, Universidad de Malaga |
|---|
| 529 | #@date 2011/02/01 |
|---|
| 530 | #@version 1.0.1 - Control errores, tamaño particion, fichero-imagen |
|---|
| 531 | #@author Antonio J. Doblas Viso, Universidad de Malaga |
|---|
| 532 | #@date 2011/05/11 |
|---|
| 533 | #*/ ## |
|---|
| 534 | function ogRestoreImage () |
|---|
| 535 | { |
|---|
| 536 | # Variables locales |
|---|
| 537 | local PART PARTSIZE IMGFILE IMGTYPE IMGSIZE FSTYPE PROGRAM ERRCODE |
|---|
| 538 | |
|---|
| 539 | # Si se solicita, mostrar ayuda. |
|---|
| 540 | if [ "$*" == "help" ]; then |
|---|
| 541 | ogHelp "$FUNCNAME" "$FUNCNAME path_dir str_image int_ndisk int_npart" \ |
|---|
| 542 | "$FUNCNAME REPO /aula1/winxp 1 1" |
|---|
| 543 | return |
|---|
| 544 | fi |
|---|
| 545 | # Error si no se reciben 4 parámetros. |
|---|
| 546 | [ $# == 4 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
|---|
| 547 | # Procesar parámetros. |
|---|
| 548 | PART="$(ogDiskToDev $3 $4)" || return $(ogRaiseError $OG_ERR_NOTFOUND " $3 $4"; echo $?) |
|---|
| 549 | #IMGTYPE=$(ogGetImageType "$1" "$2") |
|---|
| 550 | IMGTYPE=img |
|---|
| 551 | IMGFILE=$(ogGetPath "$1" "$2.$IMGTYPE") |
|---|
| 552 | [ -r "$IMGFILE" ] || return $(ogRaiseError $OG_ERR_NOTFOUND " $3 $4"; echo $?) |
|---|
| 553 | # comprobamos consistencia de la imagen |
|---|
| 554 | ogGetImageInfo $IMGFILE >/dev/null || return $(ogRaiseError $OG_ERR_IMAGE " $1 $2"; echo $?) |
|---|
| 555 | |
|---|
| 556 | # Error si la imagen no cabe en la particion. |
|---|
| 557 | IMGSIZE=$(ogGetImageSize "$1" "$2") || return $(ogRaiseError $OG_ERR_IMAGE " $1 $2"; echo $?) |
|---|
| 558 | #TODO: |
|---|
| 559 | #Si la particion no esta formateado o tiene problemas formateamos |
|---|
| 560 | ogMount $3 $4 || ogFormat $3 $4 |
|---|
| 561 | PARTSIZE=$(ogGetPartitionSize $3 $4) |
|---|
| 562 | if [ $IMGSIZE -gt $PARTSIZE ]; then |
|---|
| 563 | ogRaiseError $OG_ERR_IMGSIZEPARTITION " $PARTSIZE < $IMGSIZE" |
|---|
| 564 | return $? |
|---|
| 565 | fi |
|---|
| 566 | # Comprobar el bloqueo de la imagen y de la partición. |
|---|
| 567 | if ogIsImageLocked "$IMGFILE"; then |
|---|
| 568 | ogRaiseError $OG_ERR_LOCKED "$MSG_IMAGE $1, $2.$IMGTYPE" |
|---|
| 569 | return $? |
|---|
| 570 | fi |
|---|
| 571 | if ogIsLocked $3 $4; then |
|---|
| 572 | ogRaiseError $OG_ERR_LOCKED "$MSG_PARTITION $3, $4" |
|---|
| 573 | return $? |
|---|
| 574 | fi |
|---|
| 575 | |
|---|
| 576 | # Solicitamos la generación de la instruccion a ejecutar |
|---|
| 577 | # Atención: no se comprueba el tipo de sistema de archivos. |
|---|
| 578 | # Atención: no se comprueba incongruencia entre partición e imagen. |
|---|
| 579 | PROGRAM=`ogRestoreImageSyntax $IMGFILE $PART` |
|---|
| 580 | |
|---|
| 581 | # Desmontar y bloquear partición. |
|---|
| 582 | ogUnmount $3 $4 2>/dev/null || return $(ogRaiseError $OG_ERR_PARTITION " $3 $4"; echo $?) |
|---|
| 583 | ogLock $3 $4 || return $? |
|---|
| 584 | trap "ogUnlock $3 $4" 1 2 3 6 9 |
|---|
| 585 | |
|---|
| 586 | # Ejecutar restauración según el tipo de imagen. |
|---|
| 587 | eval $PROGRAM |
|---|
| 588 | |
|---|
| 589 | ERRCODE=$? |
|---|
| 590 | if [ $ERRCODE != 0 ]; then |
|---|
| 591 | ogRaiseError $OG_ERR_IMAGE "$IMGFILE, $3, $4" |
|---|
| 592 | fi |
|---|
| 593 | ogUnlock $3 $4 |
|---|
| 594 | return $ERRCODE |
|---|
| 595 | } |
|---|
| 596 | |
|---|
| 597 | |
|---|
| 598 | #/** |
|---|
| 599 | # ogRestoreMbrImage str_repo path_image int_ndisk |
|---|
| 600 | #@brief Restaura la imagen del sector de arranque de un disco. |
|---|
| 601 | #@param str_repo repositorio de imágenes o caché local |
|---|
| 602 | #@param path_image camino de la imagen |
|---|
| 603 | #@param int_ndisk nº de orden del disco |
|---|
| 604 | #@return (por determinar) |
|---|
| 605 | #@exception OG_ERR_FORMAT formato incorrecto. |
|---|
| 606 | #@exception OG_ERR_NOTFOUND fichero de imagen o partición no detectados. |
|---|
| 607 | #@exception OG_ERR_IMAGE error al restaurar la imagen del sistema. |
|---|
| 608 | #@version 0.9 - Primera versión en pruebas. |
|---|
| 609 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
|---|
| 610 | #@date 2010/01/12 |
|---|
| 611 | #@version 1.0 - Adaptación a OpenGnSys 1.0 |
|---|
| 612 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
|---|
| 613 | #@date 2011/03/10 |
|---|
| 614 | #*/ ## |
|---|
| 615 | function ogRestoreMbrImage () |
|---|
| 616 | { |
|---|
| 617 | # Variables locales |
|---|
| 618 | local DISK IMGFILE |
|---|
| 619 | # Si se solicita, mostrar ayuda. |
|---|
| 620 | if [ "$*" == "help" ]; then |
|---|
| 621 | ogHelp "$FUNCNAME" "$FUNCNAME path_dir str_image int_ndisk" \ |
|---|
| 622 | "$FUNCNAME REPO /aula1/mbr 1" |
|---|
| 623 | return |
|---|
| 624 | fi |
|---|
| 625 | # Error si no se reciben 3 parámetros. |
|---|
| 626 | [ $# == 3 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
|---|
| 627 | # Procesar parámetros. |
|---|
| 628 | DISK=$(ogDiskToDev "$3") || return $? |
|---|
| 629 | IMGFILE=$(ogGetPath "$1" "$2.mbr") || return $? |
|---|
| 630 | [ -r "$IMGFILE" ] || ogRaiseError OG_ERR_NOTFOUND "$IMGFILE" || return $? |
|---|
| 631 | |
|---|
| 632 | # Restaurar imagen del MBR. |
|---|
| 633 | dd if="$IMGFILE" of="$DISK" bs=512 count=1 || ogRaiseError $OG_ERR_IMAGE "$1 $IMGFILE" || return $? |
|---|
| 634 | } |
|---|
| 635 | |
|---|
| 636 | |
|---|
| 637 | #/** |
|---|
| 638 | # ogRestoreBootLoaderImage str_repo path_image int_ndisk |
|---|
| 639 | #@brief Restaura la imagen del boot loader del sector de arranque de un disco. |
|---|
| 640 | #@param str_repo repositorio de imágenes o caché local |
|---|
| 641 | #@param path_image camino de la imagen |
|---|
| 642 | #@param int_ndisk nº de orden del disco |
|---|
| 643 | #@return (por determinar) |
|---|
| 644 | #@exception OG_ERR_FORMAT formato incorrecto. |
|---|
| 645 | #@exception OG_ERR_NOTFOUND fichero de imagen o partición no detectados. |
|---|
| 646 | #@exception OG_ERR_IMAGE error al restaurar la imagen del sistema. |
|---|
| 647 | #@version 1.0 - Adaptacion de ogRestoreMbrImage para restaurar solo el Boot Loader |
|---|
| 648 | #@author Juan Carlos Xifre, SICUZ Universidad de Zaragoza |
|---|
| 649 | #@date 2011/03/21 |
|---|
| 650 | #*/ ## |
|---|
| 651 | function ogRestoreBootLoaderImage () |
|---|
| 652 | { |
|---|
| 653 | # Variables locales |
|---|
| 654 | local DISK IMGFILE |
|---|
| 655 | # Si se solicita, mostrar ayuda. |
|---|
| 656 | if [ "$*" == "help" ]; then |
|---|
| 657 | ogHelp "$FUNCNAME" "$FUNCNAME path_dir str_image int_ndisk" \ |
|---|
| 658 | "$FUNCNAME REPO /aula1/mbr 1" |
|---|
| 659 | return |
|---|
| 660 | fi |
|---|
| 661 | # Error si no se reciben 3 parámetros. |
|---|
| 662 | [ $# == 3 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
|---|
| 663 | # Procesar parámetros. |
|---|
| 664 | DISK=$(ogDiskToDev "$3") || return $? |
|---|
| 665 | IMGFILE=$(ogGetPath "$1" "$2.mbr") || return $? |
|---|
| 666 | [ -r "$IMGFILE" ] || ogRaiseError OG_ERR_NOTFOUND "$IMGFILE" || return $? |
|---|
| 667 | |
|---|
| 668 | # Restaurar imagen del MBR. |
|---|
| 669 | dd if="$IMGFILE" of="$DISK" bs=446 count=1 || ogRaiseError $OG_ERR_IMAGE "$1 $IMGFILE" || return $? |
|---|
| 670 | } |
|---|
| 671 | |
|---|
| 672 | #/** |
|---|
| 673 | # ogUnlockImage [str_repo] path_image |
|---|
| 674 | #@brief Desbloquea una imagen con uso exclusivo. |
|---|
| 675 | #@param str_repo repositorio de imágenes (opcional) |
|---|
| 676 | #@param path_image camino de la imagen (sin extensión) |
|---|
| 677 | #@return Nada. |
|---|
| 678 | #@note repo = { REPO, CACHE } |
|---|
| 679 | #@note Se elimina el fichero de bloqueo con extensión .lock |
|---|
| 680 | #@exception OG_ERR_FORMAT formato incorrecto. |
|---|
| 681 | #@version 1.0 - Adaptación a OpenGnSys 1.0 |
|---|
| 682 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
|---|
| 683 | #@date 2011/03/10 |
|---|
| 684 | #*/ ## |
|---|
| 685 | function ogUnlockImage () |
|---|
| 686 | { |
|---|
| 687 | # Si se solicita, mostrar ayuda. |
|---|
| 688 | if [ "$*" == "help" ]; then |
|---|
| 689 | ogHelp "$FUNCNAME" "$FUNCNAME [str_repo] path_image" \ |
|---|
| 690 | "$FUNCNAME /opt/opengnsys/images/aula1/winxp.img" \ |
|---|
| 691 | "$FUNCNAME REPO /aula1/winxp.img" |
|---|
| 692 | return |
|---|
| 693 | fi |
|---|
| 694 | # Error si no se reciben 1 o 2 parámetros. |
|---|
| 695 | [ $# == 1 -o $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
|---|
| 696 | |
|---|
| 697 | # Borrar fichero de bloqueo para la imagen. |
|---|
| 698 | rm -f $(ogGetPath $@.lock) |
|---|
| 699 | } |
|---|
| 700 | |
|---|
| 701 | |
|---|
| 702 | #/** |
|---|
| 703 | # ogGetImageInfo filename |
|---|
| 704 | #@brief muestra información sobre la imagen monolitica. |
|---|
| 705 | #@param 1 filename path absoluto del fichero imagen |
|---|
| 706 | #@return cadena compuesta por clonacion:compresor:sistemaarchivos:tamañoKB |
|---|
| 707 | #@exception OG_ERR_FORMAT formato incorrecto. |
|---|
| 708 | #@exception OG_ERR_NOTFOUND fichero no encontrado. |
|---|
| 709 | #@exception OG_ERR_IMAGE "Image format is not valid $IMGFILE" |
|---|
| 710 | #@warning En pruebas iniciales |
|---|
| 711 | #@TODO Definir sintaxis de salida (herramienta y compresor en minuscula) |
|---|
| 712 | #@TODO Arreglar loop para ntfsclone |
|---|
| 713 | #@TODO insertar parametros entrada tipo OG |
|---|
| 714 | #@version 1.0 - Primeras pruebas |
|---|
| 715 | #@author Antonio J. Doblas Viso. Universidad de Málaga |
|---|
| 716 | #@date 2010/02/08 |
|---|
| 717 | #*/ ## |
|---|
| 718 | |
|---|
| 719 | function ogGetImageInfo () |
|---|
| 720 | { |
|---|
| 721 | # Si se solicita, mostrar ayuda. |
|---|
| 722 | if [ "$*" == "help" ]; then |
|---|
| 723 | ogHelp "$FUNCNAME" "$FUNCNAME filename " \ |
|---|
| 724 | "$FUNCNAME /opt/opengnsys/images/prueba.img " |
|---|
| 725 | return |
|---|
| 726 | fi |
|---|
| 727 | |
|---|
| 728 | # Error si no se reciben 1 parámetros. |
|---|
| 729 | [ $# == 1 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
|---|
| 730 | |
|---|
| 731 | #comprobando que el parametro uno es un file. |
|---|
| 732 | [ -f $1 ] || ogRaiseError $OG_ERR_NOTFOUND "$1" || return $? |
|---|
| 733 | |
|---|
| 734 | local TOOLS COMPRESSOR IMGFILE FILEHEAD FS FSPLUS SIZE SIZEFACTOR PARTIMAGEINFO PARTCLONEINFO NTFSCLONEINFO IMGDETECT |
|---|
| 735 | IMGDETECT="FALSE" |
|---|
| 736 | |
|---|
| 737 | IMGFILE=$1 |
|---|
| 738 | FILEHEAD=/tmp/`basename $IMGFILE`.infohead |
|---|
| 739 | COMPRESSOR=`file $IMGFILE | awk '{print $2}'` |
|---|
| 740 | ogCheckStringInGroup "$COMPRESSOR" "gzip lzop" || ogRaiseError $OG_ERR_IMAGE "Image format is not valid $IMGFILE" || return $? |
|---|
| 741 | $($COMPRESSOR -dc $IMGFILE 2>/dev/null | head > $FILEHEAD) || ogRaiseError $OG_ERR_IMAGE "Image format is not valid $IMGFILE" || return $? |
|---|
| 742 | |
|---|
| 743 | ## buscando Primera opción. |
|---|
| 744 | if [ "$IMGDETECT" == "FALSE" ] |
|---|
| 745 | then |
|---|
| 746 | PARTCLONEINFO=$(LC_ALL=C partclone.info $FILEHEAD 2>&1) |
|---|
| 747 | if `echo $PARTCLONEINFO | grep size > /dev/null` |
|---|
| 748 | then |
|---|
| 749 | TOOLS=PARTCLONE |
|---|
| 750 | FS=$(echo $PARTCLONEINFO | awk '{gsub(/\: /,"\n"); print toupper($8);}') |
|---|
| 751 | if [[ "$FS" == "HFS" || "$FS" == "HFSPLUS" || "$FS" == "FAT32" ]]; then |
|---|
| 752 | FSPLUS=$(echo $PARTCLONEINFO | awk '{gsub(/\: /,"\n"); print toupper($9);}') |
|---|
| 753 | echo $PARTCLONEINFO | grep GB > /dev/null && SIZEFACTOR=1000000 || SIZEFACTOR=1024 |
|---|
| 754 | if [ "$FSPLUS" == "PLUS" ]; then |
|---|
| 755 | FS=$FS$FSPLUS |
|---|
| 756 | SIZE=$(echo $PARTCLONEINFO | awk -v FACTOR=$SIZEFACTOR '{printf "%d\n", $17*FACTOR;}') |
|---|
| 757 | else |
|---|
| 758 | SIZE=$(echo $PARTCLONEINFO | awk -v FACTOR=$SIZEFACTOR '{printf "%d\n", $16*FACTOR;}') |
|---|
| 759 | fi |
|---|
| 760 | else |
|---|
| 761 | echo $PARTCLONEINFO | grep GB > /dev/null && SIZEFACTOR=1000000 || SIZEFACTOR=1024 |
|---|
| 762 | SIZE=$(echo $PARTCLONEINFO | awk -v FACTOR=$SIZEFACTOR '{gsub(/\: /,"\n"); printf "%d\n", $11*FACTOR;}') |
|---|
| 763 | fi |
|---|
| 764 | IMGDETECT="TRUE" |
|---|
| 765 | fi |
|---|
| 766 | fi |
|---|
| 767 | #buscando segunda opcion. |
|---|
| 768 | if [ "$IMGDETECT" == "FALSE" -a ! -f /dev/loop2 ] |
|---|
| 769 | then |
|---|
| 770 | cat $FILEHEAD | grep -w ntfsclone-image > /dev/null && NTFSCLONEINFO=$(cat $FILEHEAD | ntfsclone --restore --overwrite /dev/loop2 - 2>&1) |
|---|
| 771 | if `echo $NTFSCLONEINFO | grep ntfsclone > /dev/null` |
|---|
| 772 | then |
|---|
| 773 | TOOLS=NTFSCLONE |
|---|
| 774 | SIZE=$(echo $NTFSCLONEINFO | awk '{gsub(/\(|\)|\./,""); printf "%d\n",$17/1000;}') |
|---|
| 775 | FS=NTFS |
|---|
| 776 | IMGDETECT="TRUE" |
|---|
| 777 | fi |
|---|
| 778 | fi |
|---|
| 779 | ## buscando Tercer opción. |
|---|
| 780 | if [ "$IMGDETECT" == "FALSE" ] |
|---|
| 781 | then |
|---|
| 782 | PARTIMAGEINFO=$(partimage -B gui=no imginfo "$FILEHEAD" 2>&1) |
|---|
| 783 | if `echo $PARTIMAGEINFO | grep Partition > /dev/null` |
|---|
| 784 | then |
|---|
| 785 | TOOLS=PARTIMAGE |
|---|
| 786 | FS=$(echo $PARTIMAGEINFO | awk '{gsub(/ /,"\n"); print $17;}' | awk '{sub(/\.\.+/," "); print toupper($2)}') |
|---|
| 787 | SIZE=$( echo $PARTIMAGEINFO | awk '{gsub(/ /,"\n"); print $36;}' | awk '{sub(/\.\.+/," "); printf "%d\n",$2*1024*1024;}') |
|---|
| 788 | IMGDETECT="TRUE" |
|---|
| 789 | fi |
|---|
| 790 | fi |
|---|
| 791 | #comprobamos valores #Chequeamos los valores devueltos. |
|---|
| 792 | if [ -z "$TOOLS" -o -z "$COMPRESSOR" -o "$IMGDETECT" == "FALSE" ] |
|---|
| 793 | then |
|---|
| 794 | ogRaiseError $OG_ERR_IMAGE "Image format is not valid $IMGFILE" || return $? |
|---|
| 795 | else |
|---|
| 796 | COMPRESSOR=$(echo $COMPRESSOR | tr [a-z] [A-Z]) |
|---|
| 797 | echo $TOOLS:$COMPRESSOR:$FS:$SIZE |
|---|
| 798 | fi |
|---|
| 799 | } |
|---|
| 800 | |
|---|
| 801 | function ogGetImageProgram () |
|---|
| 802 | { |
|---|
| 803 | local IMGFILE |
|---|
| 804 | IMGFILE=$(ogGetPath "$1" "$2.img") || return $? |
|---|
| 805 | [ -r "$IMGFILE" ] || ogRaiseError OG_ERR_NOTFOUND "$IMGFILE" || return $? |
|---|
| 806 | ogGetImageInfo $IMGFILE | awk -F: '{print $1}' |
|---|
| 807 | |
|---|
| 808 | } |
|---|
| 809 | |
|---|
| 810 | function ogGetImageCompressor () |
|---|
| 811 | { |
|---|
| 812 | local IMGFILE |
|---|
| 813 | IMGFILE=$(ogGetPath "$1" "$2.img") || return $? |
|---|
| 814 | [ -r "$IMGFILE" ] || ogRaiseError OG_ERR_NOTFOUND "$IMGFILE" || return $? |
|---|
| 815 | ogGetImageInfo $IMGFILE | awk -F: '{print $2}' |
|---|
| 816 | } |
|---|
| 817 | |
|---|
| 818 | function ogGetImageType () |
|---|
| 819 | { |
|---|
| 820 | local IMGFILE |
|---|
| 821 | IMGFILE=$(ogGetPath "$1" "$2.img") || return $? |
|---|
| 822 | [ -r "$IMGFILE" ] || ogRaiseError OG_ERR_NOTFOUND "$IMGFILE" || return $? |
|---|
| 823 | #partimage -B gui=no imginfo "$IMGFILE" 2>&1 | \ |
|---|
| 824 | # awk '/^Filesystem/ {sub(/\.\.+/," "); sub(/fs$/,""); print toupper($2);}' |
|---|
| 825 | ogGetImageInfo $IMGFILE | awk -F: '{print $3}' |
|---|
| 826 | |
|---|
| 827 | } |
|---|
| 828 | |
|---|
| 829 | function ogGetImageSize () |
|---|
| 830 | { |
|---|
| 831 | # Variables locales |
|---|
| 832 | local IMGFILE |
|---|
| 833 | |
|---|
| 834 | # Si se solicita, mostrar ayuda. |
|---|
| 835 | if [ "$*" == "help" ]; then |
|---|
| 836 | ogHelp "$FUNCNAME" "$FUNCNAME REPO|CACHE /str_image" \ |
|---|
| 837 | "$FUNCNAME REPO /aula1/winxp ==> 5642158" |
|---|
| 838 | return |
|---|
| 839 | fi |
|---|
| 840 | # Error si no se reciben 2 parámetros. |
|---|
| 841 | [ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
|---|
| 842 | # Error si el fichero de imagen no es accesible. |
|---|
| 843 | IMGFILE=$(ogGetPath "$1" "$2.img") || return $? |
|---|
| 844 | [ -r "$IMGFILE" ] || ogRaiseError OG_ERR_NOTFOUND "$IMGFILE" || return $? |
|---|
| 845 | |
|---|
| 846 | # Devuelve el tamaño de la imagen en KB. |
|---|
| 847 | #partimage -B gui=no imginfo "$IMGFILE" 2>&1 | \ |
|---|
| 848 | # awk '/Partition size/ {sub(/\.\.+/," "); printf "%d\n",$3*1024*1024;}' |
|---|
| 849 | ogGetImageInfo $IMGFILE | awk -F: '{print $4}' |
|---|
| 850 | } |
|---|
| 851 | |
|---|