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