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