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