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