[715bedc] | 1 | #!/bin/bash |
---|
| 2 | #/** |
---|
| 3 | #@file Image.lib |
---|
| 4 | #@brief Librería o clase Image |
---|
| 5 | #@class Image |
---|
| 6 | #@brief Funciones para creación, restauración y clonación de imágenes de sistemas. |
---|
| 7 | #@version 0.9 |
---|
| 8 | #@warning License: GNU GPLv3+ |
---|
| 9 | #*/ |
---|
| 10 | |
---|
| 11 | |
---|
| 12 | #/** |
---|
[ebf06c7] | 13 | # ogCreateImage int_ndisk int_npartition str_repo path_image |
---|
[715bedc] | 14 | #@brief Crea una imagen a partir de una partición. |
---|
[42669ebf] | 15 | #@param int_ndisk nº de orden del disco |
---|
| 16 | #@param int_npartition nº de orden de la partición |
---|
| 17 | #@param str_repo repositorio de imágenes (remoto o caché local) |
---|
| 18 | #@param path_image camino de la imagen (sin extensión) |
---|
[715bedc] | 19 | #@return (nada, por determinar) |
---|
[ebf06c7] | 20 | #@note repo = { REPO, CACHE } |
---|
[cfeabbf] | 21 | #@exception OG_ERR_FORMAT formato incorrecto. |
---|
| 22 | #@exception OG_ERR_NOTFOUND fichero o dispositivo no encontrado. |
---|
| 23 | #@exception OG_ERR_PARTITION partición no accesible o no soportada. |
---|
| 24 | #@exception OG_ERR_LOCKED particion bloqueada por otra operación. |
---|
| 25 | #@exception OG_ERR_IMAGE error al crear la imagen del sistema. |
---|
[715bedc] | 26 | #@warning En pruebas iniciales |
---|
[3458879] | 27 | #@todo Comprobaciones, control de errores, definir parámetros, etc. |
---|
[985bef0] | 28 | #@version 0.1 - Integracion para Opengnsys - HIDRA:CrearImagen{EXT3, NTFS}.sh; EAC: CreateImageFromPartition () en Deploy.lib |
---|
| 29 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
| 30 | #@Date 2008/05/13 |
---|
| 31 | #@author Antonio J. Doblas Viso. Universidad de Malaga |
---|
| 32 | #@date 2008/10/27 |
---|
| 33 | #@version 0.9 - Version en pruebas para OpenGNSys |
---|
[715bedc] | 34 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
[cfeabbf] | 35 | #@date 2009/10/07 |
---|
[1e7eaab] | 36 | #*/ ## |
---|
[42669ebf] | 37 | function ogCreateImage () |
---|
| 38 | { |
---|
[59f9ad2] | 39 | # Variables locales |
---|
[cfeabbf] | 40 | local PART IMGDIR IMGFILE ERRCODE |
---|
[59f9ad2] | 41 | |
---|
[42669ebf] | 42 | # Si se solicita, mostrar ayuda. |
---|
[59f9ad2] | 43 | if [ "$*" == "help" ]; then |
---|
| 44 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npart path_dir str_image" \ |
---|
[3543b3e] | 45 | "$FUNCNAME 1 1 REPO /aula1/winxp" |
---|
[59f9ad2] | 46 | return |
---|
| 47 | fi |
---|
[c136ae7] | 48 | # Error si no se reciben 4 parámetros. |
---|
[59f9ad2] | 49 | [ $# -lt 4 ] && ogRaiseError $OG_ERR_FORMAT && return $? |
---|
| 50 | |
---|
[715bedc] | 51 | PART="$(ogDiskToDev $1 $2)" || return $? |
---|
[a79dd508] | 52 | if ogIsLocked $1 $2; then |
---|
| 53 | ogRaiseError $OG_ERR_LOCKED "$1,$2" |
---|
| 54 | return $? |
---|
| 55 | fi |
---|
[cfeabbf] | 56 | IMGDIR=$(ogGetParentPath "$3" "$4") |
---|
| 57 | [ -n "$IMGDIR" ] || ogRaiseError $OG_ERR_NOTFOUND "$3 $(dirname $4)" || return $? |
---|
[4395836] | 58 | IMGFILE="$IMGDIR/$(basename "$4").img" |
---|
[cfeabbf] | 59 | ogUnmount $1 $2 2>/dev/null |
---|
[715bedc] | 60 | |
---|
[a79dd508] | 61 | ogLock $1 $2 || return $? |
---|
[c2b03eb] | 62 | trap "ogUnlock $1 $2; rm -f $IMGFILE" 1 2 3 6 9 |
---|
[715bedc] | 63 | |
---|
| 64 | TYPE="$(ogGetFsType $1 $2)" |
---|
| 65 | case "$TYPE" in |
---|
[4395836] | 66 | EXT[234]|REISERFS|XFS|JFS) |
---|
[c40a6b4] | 67 | partimage -M -f3 -o -d -V0 -B gui=no -c -z1 save $PART "$IMGFILE" |
---|
[59f9ad2] | 68 | ;; |
---|
[715bedc] | 69 | NTFS|HNTFS) |
---|
[f8f4dfa] | 70 | partimage -M -f3 -o -d -V0 -B gui=no -c -z1 save $PART "$IMGFILE" |
---|
[715bedc] | 71 | ;; |
---|
[59f9ad2] | 72 | FAT16|FAT32|HFAT16|HFAT32) |
---|
[c40a6b4] | 73 | partimage -M -f3 -o -d -V0 -B gui=no -c -z1 save $PART "$IMGFILE" |
---|
[715bedc] | 74 | ;; |
---|
[cfeabbf] | 75 | *) ogRaiseError $OG_ERR_PARTITION "$1 $2 $TYPE" |
---|
[715bedc] | 76 | return $? ;; |
---|
| 77 | esac |
---|
| 78 | |
---|
[42669ebf] | 79 | # Controlar salida de error y desbloquear partición. |
---|
[cfeabbf] | 80 | ERRCODE=$? |
---|
[f5432db7] | 81 | if [ $ERRCODE != 0 ]; then |
---|
[cfeabbf] | 82 | ogRaiseError $OG_ERR_IMAGE "$1 $2 $IMGFILE" |
---|
[f5432db7] | 83 | rm -f "$IMGFILE" |
---|
[cfeabbf] | 84 | fi |
---|
[715bedc] | 85 | ogUnlock $1 $2 |
---|
[cfeabbf] | 86 | return $ERRCODE |
---|
[715bedc] | 87 | } |
---|
| 88 | |
---|
[b094c59] | 89 | |
---|
| 90 | #/** |
---|
[39277f4] | 91 | # ogGetImageSize str_repo path_image |
---|
[b094c59] | 92 | #@brief Devuelve el tamaño del sistema de archivos almacenado en un fichero de imagen. |
---|
[42669ebf] | 93 | #@param str_repo repositorio de imágenes o caché local |
---|
| 94 | #@param path_image camino de la imagen |
---|
[b094c59] | 95 | #@return tamaño (en KB) |
---|
| 96 | #@exception OG_ERR_FORMAT formato incorrecto. |
---|
| 97 | #@exception OG_ERR_NOTFOUND fichero de imagen no encontrado. |
---|
| 98 | #*/ |
---|
| 99 | #@warning En pruebas iniciales |
---|
| 100 | #@todo Definición de parámetros y salidas. |
---|
| 101 | #@version 0.1 - Primera versión muy en pruebas para OpenGNSys |
---|
| 102 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
| 103 | #@date 2009/09/11 |
---|
[1e7eaab] | 104 | #*/ ## |
---|
[42669ebf] | 105 | function ogGetImageSize () |
---|
| 106 | { |
---|
[b094c59] | 107 | # Variables locales |
---|
| 108 | local IMGFILE |
---|
| 109 | |
---|
[42669ebf] | 110 | # Si se solicita, mostrar ayuda. |
---|
[b094c59] | 111 | if [ "$*" == "help" ]; then |
---|
| 112 | ogHelp "$FUNCNAME" "$FUNCNAME path_dir str_image int_ndisk int_npart" \ |
---|
[3543b3e] | 113 | "$FUNCNAME 1 1 REPO /aula1/winxp ==> 5642158" |
---|
[b094c59] | 114 | return |
---|
| 115 | fi |
---|
[42669ebf] | 116 | # Error si no se reciben menos de 2 parámetros. |
---|
[b094c59] | 117 | [ $# -lt 2 ] && ogRaiseError $OG_ERR_FORMAT && return $? |
---|
[42669ebf] | 118 | # Error si el fichero de imagen no es accesible. |
---|
[4395836] | 119 | IMGFILE=$(ogGetPath "$1" "$2.img") || return $? |
---|
[b094c59] | 120 | [ -r "$IMGFILE" ] || ogRaiseError OG_ERR_NOTFOUND "$IMGFILE" || return $? |
---|
| 121 | |
---|
[42669ebf] | 122 | # Devuelve el tamaño de la imagen en KB. |
---|
[b094c59] | 123 | partimage -B gui=no imginfo "$IMGFILE" 2>&1 | \ |
---|
| 124 | awk '/Partition size/ {sub(/\.\.+/," "); printf "%d\n",$3*1024*1024;}' |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | |
---|
[39277f4] | 128 | #### PRUEBAS |
---|
| 129 | # Obtener tipo de imagen |
---|
[42669ebf] | 130 | function ogGetImageType () |
---|
| 131 | { |
---|
[39277f4] | 132 | local IMGFILE |
---|
[4395836] | 133 | IMGFILE=$(ogGetPath "$1" "$2.img") || return $? |
---|
[39277f4] | 134 | [ -r "$IMGFILE" ] || ogRaiseError OG_ERR_NOTFOUND "$IMGFILE" || return $? |
---|
| 135 | partimage -B gui=no imginfo "$IMGFILE" 2>&1 | \ |
---|
| 136 | awk '/^Filesystem/ {sub(/\.\.+/," "); sub(/fs$/,""); print toupper($2);}' |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | |
---|
[b094c59] | 140 | #/** |
---|
[39277f4] | 141 | # ogRestoreImage str_repo path_image int_ndisk int_npartition |
---|
[b094c59] | 142 | #@brief Restaura una imagen de sistema de archivos en una partición. |
---|
[42669ebf] | 143 | #@param str_repo repositorio de imágenes o caché local |
---|
| 144 | #@param path_image camino de la imagen |
---|
| 145 | #@param int_ndisk nº de orden del disco |
---|
| 146 | #@param int_npartition nº de orden de la partición |
---|
[b094c59] | 147 | #@return (por determinar) |
---|
| 148 | #@exception OG_ERR_FORMAT formato incorrecto. |
---|
| 149 | #@exception OG_ERR_NOTFOUND fichero de imagen o partición no detectados. |
---|
| 150 | #@exception OG_ERR_LOCKED partición bloqueada por otra operación. |
---|
| 151 | #@exception OG_ERR_IMAGE error al restaurar la imagen del sistema. |
---|
| 152 | #@warning En pruebas iniciales |
---|
[39277f4] | 153 | #@todo Comprobar incongruencias partición-imagen, control de errores, definir parámetros, caché/repositorio, etc. |
---|
[985bef0] | 154 | #@version 0.1 - Integracion para Opengnsys - HIDRA:RestaurarImagen{EXT3, NTFS}.sh; EAC: RestorePartitionFromImage() en Deploy.lib |
---|
| 155 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
| 156 | #@Date 2008/05/13 |
---|
| 157 | #@author Antonio J. Doblas Viso. Universidad de Malaga |
---|
| 158 | #@date 2008/10/27 |
---|
| 159 | #@version 0.9 - Primera version muy en pruebas para OpenGNSys |
---|
[b094c59] | 160 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
| 161 | #@date 2009/09/10 |
---|
[1e7eaab] | 162 | #*/ ## |
---|
[42669ebf] | 163 | function ogRestoreImage () |
---|
| 164 | { |
---|
[b094c59] | 165 | # Variables locales |
---|
[3458879] | 166 | local PART PARTSIZE IMGFILE IMGSIZE |
---|
[b094c59] | 167 | |
---|
[42669ebf] | 168 | # Si se solicita, mostrar ayuda. |
---|
[b094c59] | 169 | if [ "$*" == "help" ]; then |
---|
| 170 | ogHelp "$FUNCNAME" "$FUNCNAME path_dir str_image int_ndisk int_npart" \ |
---|
[3543b3e] | 171 | "$FUNCNAME REPO /aula1/winxp 1 1" |
---|
[b094c59] | 172 | return |
---|
| 173 | fi |
---|
[42669ebf] | 174 | # Error si no se reciben 4 parámetros. |
---|
[b094c59] | 175 | [ $# -lt 4 ] && ogRaiseError $OG_ERR_FORMAT && return $? |
---|
[3458879] | 176 | # Procesar parámetros. |
---|
| 177 | PART="$(ogDiskToDev "$3" "$4")" || return $? |
---|
[4395836] | 178 | IMGFILE=$(ogGetPath "$1" "$2.img") || return $? |
---|
[b094c59] | 179 | [ -r "$IMGFILE" ] || ogRaiseError OG_ERR_NOTFOUND "$IMGFILE" || return $? |
---|
[42669ebf] | 180 | # Error si la imagen no cabe en la particion. |
---|
[3458879] | 181 | IMGSIZE=$(ogGetImageSize "$1" "$2") |
---|
| 182 | PARTSIZE=$(ogGetPartitionSize $3 $4) |
---|
| 183 | if [ $IMGSIZE -gt $PARTSIZE ]; then |
---|
| 184 | ogRaiseError $OG_ERR_PARTITION "$IMGSIZE > $PARTSIZE" |
---|
| 185 | return $? |
---|
| 186 | fi |
---|
[b094c59] | 187 | |
---|
[42669ebf] | 188 | # Comprobar el bloqueo de la partición, desmontarla y bloquearla. |
---|
[39277f4] | 189 | if ogIsLocked $3 $4; then |
---|
| 190 | ogRaiseError $OG_ERR_LOCKED "$3,$4" |
---|
| 191 | return $? |
---|
| 192 | fi |
---|
[f5432db7] | 193 | ogUnmount $3 $4 2>/dev/null || return $? |
---|
[39277f4] | 194 | ogLock $3 $4 || return $? |
---|
[b094c59] | 195 | trap "ogUnlock $3 $4" 1 2 3 6 9 |
---|
| 196 | |
---|
[42669ebf] | 197 | # Restaurar según el tipo de sistema de archivos. |
---|
| 198 | # Atención: no se comprueba incongruencia entre partición e imagen. |
---|
[b094c59] | 199 | TYPE="$(ogGetFsType $3 $4)" |
---|
| 200 | case "$TYPE" in |
---|
[3458879] | 201 | EXT[234]|REISERFS|XFS|JFS) |
---|
[f5432db7] | 202 | partimage -M -f3 -o -d -V0 -B gui=no -c -z1 --volume=0 restore $PART "$IMGFILE" |
---|
[b094c59] | 203 | ;; |
---|
| 204 | NTFS|HNTFS) |
---|
[f5432db7] | 205 | partimage -M -f3 -o -d -V0 -B gui=no -c -z1 --volume=0 restore $PART "$IMGFILE" |
---|
[b094c59] | 206 | ;; |
---|
| 207 | FAT16|FAT32|HFAT16|HFAT32) |
---|
[f5432db7] | 208 | partimage -M -f3 -o -d -V0 -B gui=no -c -z1 --volume=0 restore $PART "$IMGFILE" |
---|
[b094c59] | 209 | ;; |
---|
[8f4c506] | 210 | *) ogRaiseError $OG_ERR_FORMAT |
---|
[b094c59] | 211 | return $? ;; |
---|
| 212 | esac |
---|
[f5432db7] | 213 | ERRCODE=$? |
---|
| 214 | if [ $ERRCODE != 0 ]; then |
---|
| 215 | ogRaiseError $OG_ERR_IMAGE "$IMGFILE, $3, $4" |
---|
| 216 | fi |
---|
[b094c59] | 217 | ogUnlock $3 $4 |
---|
[f5432db7] | 218 | return $ERRCODE |
---|
[b094c59] | 219 | } |
---|
| 220 | |
---|
[8f4c506] | 221 | |
---|
| 222 | function ogCreateImageFromPartition () { |
---|
| 223 | #/** @function CreateImageFromPartition: @brief Crea una imagen de la particion indicada, utilizando el programa definido en las variable $CloneImageNTFS y $CloneImageEXT23 |
---|
| 224 | #@param $1 int DiskEAC |
---|
| 225 | #@param $2 int_PartitionEAC |
---|
| 226 | #@param $3 str_Repositorio ......... parametro pasado a ConectToRepo, admite $REPO $CACHE str_IP_servidorAlterno |
---|
| 227 | #@param $4 str_pathbase .............. Pathbase, directorio relativo, de la imagen, en EAC, se ha definido que todo repositorio comience por hdimages, pero puede variar, para adaparse a usb, o cualquier otro almacenamiento. |
---|
| 228 | #@param $5 str_NameImage.str_compresion .......... (como compresion admite gzip y lzop) |
---|
| 229 | #@param ejemplo: CreateImageFromPartition 1 1 $IP hdimages/pruebas/ base.gzip |
---|
| 230 | #@return la propia de la herramienta de clonacion partimage o ntfsclone |
---|
| 231 | #@return genera la imagen con el nombre (imagen.compresion), y se le a?ade un guion y el numero de particion(parametro $2). |
---|
| 232 | #@return Tambien se solicita al servidor EAC, la creaci?n del fichero meta torrent, que tendra el nombre tal base.gzip-1.torrent, |
---|
| 233 | #@return Tambien se crea el fichero base.gzip-1.mcast con informacion para su uso con multicast, tal base.gzip-1.mcast |
---|
| 234 | #@warning Salidas de errores no determinada |
---|
| 235 | #@attention |
---|
| 236 | #@note Pendiente: que admita como segundo parametro el valor 0 (para que identifique el MBR del disco) |
---|
| 237 | #@version 0.1 Date: 27/10/2008 Author Antonio J. Doblas Viso. Universidad de Malaga |
---|
| 238 | #*/ |
---|
| 239 | if [ $# != 5 ] |
---|
| 240 | then |
---|
[8f20481] | 241 | echo "sintaxis: CreateImageFromPartition <ndisco> <nparticion> <iprespositorio> <pathbase> <nombreimagen.compresion>" |
---|
| 242 | echo "ejemplo: CreateImageFromPartition 1 1 \$IP images/pruebas/ base.gzip" |
---|
| 243 | echo " en iprepositorio admite cualquier ip de un servdior EAC, se llama a la funcion MountRepo ip" |
---|
[8f4c506] | 244 | fi |
---|
| 245 | if [ $# = 5 ] |
---|
| 246 | then |
---|
[8f20481] | 247 | CloneImageNTFS="${CLONETOOLSNTFS:-partimage}" |
---|
| 248 | CloneImageEXT23="${CLONETOOLSEXT:-partimage}" |
---|
| 249 | CompresionImage="${CLONETOOLSCOMPRESSOR:-gzip}" |
---|
| 250 | |
---|
[8f4c506] | 251 | disco=`ogDiskToDev $1 $2` |
---|
| 252 | ogUnmountPartition $1 $2 |
---|
| 253 | fs=`ogGetFsType $1 $2` |
---|
[8f20481] | 254 | |
---|
[8f4c506] | 255 | echo determinando tipo de Sistema Operativo y consulando la variable global CloneImageNTFS: $fs |
---|
| 256 | case $fs in |
---|
| 257 | "NTFS") |
---|
| 258 | case $CloneImageNTFS in |
---|
| 259 | "ntfsclone") |
---|
| 260 | #imaged="ntfsclone --force --save-image -O - $disco | " |
---|
| 261 | imaged="ntfsclone --force --save-image -O - $disco" |
---|
| 262 | program=ntfsclone |
---|
| 263 | ;; |
---|
| 264 | "partimage") |
---|
| 265 | #imaged="partimage -M -f3 -o -d -B gui=no -c -z0 --volume=0 save $disco stdout |" |
---|
| 266 | imaged="partimage -M -f3 -o -d -B gui=no -c -z0 --volume=0 save $disco stdout" |
---|
| 267 | program=partimage |
---|
| 268 | ;; |
---|
| 269 | "partimage-ng") |
---|
| 270 | #echo "partimage-ng" |
---|
| 271 | #imaged="partimage-ng save $disco stdout" |
---|
| 272 | program=partimage-ng |
---|
| 273 | ;; |
---|
| 274 | "partclone") |
---|
| 275 | #echo "partclone" |
---|
| 276 | #imaged="partclone.ntfs -c -F -s $disco | " |
---|
| 277 | imaged="partclone.ntfs -c -F -s $disco" |
---|
| 278 | program=partclone.ntfs |
---|
| 279 | #zcat ~/image_sda1.pcl.gz | partclone.ext4 -r -o /dev/sda1 |
---|
| 280 | ;; |
---|
| 281 | esac |
---|
| 282 | ;; |
---|
| 283 | EXT[234]|REISERFS) |
---|
| 284 | case $CloneImageEXT23 in |
---|
| 285 | "partimage") |
---|
| 286 | imaged="partimage -M -f3 -o -d -B gui=no -c -z0 --volume=0 save $disco stdout" |
---|
| 287 | program=partimage |
---|
| 288 | ;; |
---|
| 289 | "partimage-ng") |
---|
| 290 | #echo "partimage-ng" |
---|
| 291 | imaged="partimage-ng save $disco stdout" |
---|
| 292 | program=partimage-ng |
---|
| 293 | ;; |
---|
| 294 | "partclone") |
---|
| 295 | echo "partclone" |
---|
| 296 | imaged="partclone.ext3 -c -F -C -s $disco" |
---|
| 297 | program=partclone.ext3 |
---|
| 298 | #zcat ~/image_sda1.pcl.gz | partclone.ext4 -r -o /dev/sda1 |
---|
| 299 | ;; |
---|
| 300 | esac |
---|
| 301 | ;; |
---|
| 302 | esac |
---|
| 303 | |
---|
| 304 | # utilizando mbuffer para reducir posibles errores de acceso a discos |
---|
| 305 | imaged="$imaged | mbuffer -m 70%" |
---|
| 306 | #metodo de compresion. |
---|
[8f20481] | 307 | case $CompresionImage in |
---|
| 308 | "gzip") |
---|
| 309 | comando="$imaged | gzip -c >" |
---|
| 310 | ;; |
---|
| 311 | "lzop") |
---|
| 312 | comando="$imaged | lzop >" |
---|
| 313 | ;; |
---|
| 314 | esac |
---|
| 315 | |
---|
[8f4c506] | 316 | #. determnamos el fichero donde se almacenar? la image |
---|
| 317 | #camino=`ConnectToRepo $3 $4 $5` |
---|
| 318 | #MkdirPath $camino |
---|
| 319 | firstcamino=`ogNewPath $3 $4` |
---|
| 320 | camino=$firstcamino$5 |
---|
| 321 | echo $camino |
---|
| 322 | sleep 1 |
---|
[8f20481] | 323 | |
---|
[8f4c506] | 324 | #preparamos y ejecutamos el comanod a realizar |
---|
| 325 | echo "Creando imagen particion $disco programa $program compresion $CompresionImage en el repositorio $3 como $4 $5 - $2" |
---|
| 326 | comando="$comando ${camino}-$2" |
---|
| 327 | echo "comando: $comando" |
---|
| 328 | echo $comando > /tmp/run.sh |
---|
| 329 | sh /tmp/run.sh |
---|
| 330 | unset comando |
---|
[8f20481] | 331 | |
---|
[8f4c506] | 332 | #iniciamos la creacion del torrent |
---|
| 333 | echo "Iniciando la creacion del punto torrent con CreateTorrentFromImage ip=$3 path=$4 image=$5-$2" echo |
---|
[8f20481] | 334 | #CreateTorrentFromImage $3 $4 $5-$2 |
---|
[8f4c506] | 335 | # iniciacmos el putno mcast |
---|
| 336 | echo "creando un punto mcast ${camino}-$2.mcast" |
---|
| 337 | echo "program;$program" > ${camino}.mcast |
---|
| 338 | echo "compresion;$CompresionImage" >> ${camino}.mcast |
---|
| 339 | echo "fsimage;$fs" >> ${camino}.mcast |
---|
| 340 | |
---|
| 341 | #UmountRepo $3 |
---|
| 342 | fi |
---|
| 343 | } |
---|