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 | #/** |
---|
13 | # ogCreateImage int_ndisk int_npartition str_repo path_image |
---|
14 | #@brief Crea una imagen a partir de una partición. |
---|
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) |
---|
19 | #@return (nada, por determinar) |
---|
20 | #@note repo = { REPO, CACHE } |
---|
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. |
---|
26 | #@warning En pruebas iniciales |
---|
27 | #@todo Comprobaciones, control de errores, definir parámetros, etc. |
---|
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 |
---|
34 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
35 | #@date 2009/10/07 |
---|
36 | #*/ ## |
---|
37 | function ogCreateImage () |
---|
38 | { |
---|
39 | # Variables locales |
---|
40 | local PART IMGDIR IMGFILE ERRCODE |
---|
41 | |
---|
42 | # Si se solicita, mostrar ayuda. |
---|
43 | if [ "$*" == "help" ]; then |
---|
44 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npart path_dir str_image" \ |
---|
45 | "$FUNCNAME 1 1 REPO /aula1/winxp" |
---|
46 | return |
---|
47 | fi |
---|
48 | # Error si no se reciben 4 parámetros. |
---|
49 | [ $# -lt 4 ] && ogRaiseError $OG_ERR_FORMAT && return $? |
---|
50 | |
---|
51 | PART="$(ogDiskToDev $1 $2)" || return $? |
---|
52 | if ogIsLocked $1 $2; then |
---|
53 | ogRaiseError $OG_ERR_LOCKED "$1,$2" |
---|
54 | return $? |
---|
55 | fi |
---|
56 | IMGDIR=$(ogGetParentPath "$3" "$4") |
---|
57 | [ -n "$IMGDIR" ] || ogRaiseError $OG_ERR_NOTFOUND "$3 $(dirname $4)" || return $? |
---|
58 | IMGFILE="$IMGDIR/$(basename "$4").img" |
---|
59 | ogUnmount $1 $2 2>/dev/null |
---|
60 | |
---|
61 | ogLock $1 $2 || return $? |
---|
62 | trap "ogUnlock $1 $2; rm -f $IMGFILE" 1 2 3 6 9 |
---|
63 | |
---|
64 | TYPE="$(ogGetFsType $1 $2)" |
---|
65 | case "$TYPE" in |
---|
66 | EXT[234]|REISERFS|XFS|JFS) |
---|
67 | partimage -M -f3 -o -d -V0 -B gui=no -c -z1 save $PART "$IMGFILE" |
---|
68 | ;; |
---|
69 | NTFS|HNTFS) |
---|
70 | partimage -M -f3 -o -d -V0 -B gui=no -c -z1 save $PART "$IMGFILE" |
---|
71 | ;; |
---|
72 | FAT16|FAT32|HFAT16|HFAT32) |
---|
73 | partimage -M -f3 -o -d -V0 -B gui=no -c -z1 save $PART "$IMGFILE" |
---|
74 | ;; |
---|
75 | *) ogRaiseError $OG_ERR_PARTITION "$1 $2 $TYPE" |
---|
76 | return $? ;; |
---|
77 | esac |
---|
78 | |
---|
79 | # Controlar salida de error y desbloquear partición. |
---|
80 | ERRCODE=$? |
---|
81 | if [ $ERRCODE != 0 ]; then |
---|
82 | ogRaiseError $OG_ERR_IMAGE "$1 $2 $IMGFILE" |
---|
83 | rm -f "$IMGFILE" |
---|
84 | fi |
---|
85 | ogUnlock $1 $2 |
---|
86 | return $ERRCODE |
---|
87 | } |
---|
88 | |
---|
89 | |
---|
90 | #/** |
---|
91 | # ogGetImageSize str_repo path_image |
---|
92 | #@brief Devuelve el tamaño del sistema de archivos almacenado en un fichero de imagen. |
---|
93 | #@param str_repo repositorio de imágenes o caché local |
---|
94 | #@param path_image camino de la imagen |
---|
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 |
---|
104 | #*/ ## |
---|
105 | function ogGetImageSize () |
---|
106 | { |
---|
107 | # Variables locales |
---|
108 | local IMGFILE |
---|
109 | |
---|
110 | # Si se solicita, mostrar ayuda. |
---|
111 | if [ "$*" == "help" ]; then |
---|
112 | ogHelp "$FUNCNAME" "$FUNCNAME path_dir str_image int_ndisk int_npart" \ |
---|
113 | "$FUNCNAME 1 1 REPO /aula1/winxp ==> 5642158" |
---|
114 | return |
---|
115 | fi |
---|
116 | # Error si no se reciben menos de 2 parámetros. |
---|
117 | [ $# -lt 2 ] && ogRaiseError $OG_ERR_FORMAT && return $? |
---|
118 | # Error si el fichero de imagen no es accesible. |
---|
119 | IMGFILE=$(ogGetPath "$1" "$2.img") || return $? |
---|
120 | [ -r "$IMGFILE" ] || ogRaiseError OG_ERR_NOTFOUND "$IMGFILE" || return $? |
---|
121 | |
---|
122 | # Devuelve el tamaño de la imagen en KB. |
---|
123 | partimage -B gui=no imginfo "$IMGFILE" 2>&1 | \ |
---|
124 | awk '/Partition size/ {sub(/\.\.+/," "); printf "%d\n",$3*1024*1024;}' |
---|
125 | } |
---|
126 | |
---|
127 | |
---|
128 | #### PRUEBAS |
---|
129 | # Obtener tipo de imagen |
---|
130 | function ogGetImageType () |
---|
131 | { |
---|
132 | local IMGFILE |
---|
133 | IMGFILE=$(ogGetPath "$1" "$2.img") || return $? |
---|
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 | |
---|
140 | #/** |
---|
141 | # ogRestoreImage str_repo path_image int_ndisk int_npartition |
---|
142 | #@brief Restaura una imagen de sistema de archivos en una partición. |
---|
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 |
---|
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 |
---|
153 | #@todo Comprobar incongruencias partición-imagen, control de errores, definir parámetros, caché/repositorio, etc. |
---|
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 |
---|
160 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
161 | #@date 2009/09/10 |
---|
162 | #*/ ## |
---|
163 | function ogRestoreImage () |
---|
164 | { |
---|
165 | # Variables locales |
---|
166 | local PART PARTSIZE IMGFILE IMGSIZE |
---|
167 | |
---|
168 | # Si se solicita, mostrar ayuda. |
---|
169 | if [ "$*" == "help" ]; then |
---|
170 | ogHelp "$FUNCNAME" "$FUNCNAME path_dir str_image int_ndisk int_npart" \ |
---|
171 | "$FUNCNAME REPO /aula1/winxp 1 1" |
---|
172 | return |
---|
173 | fi |
---|
174 | # Error si no se reciben 4 parámetros. |
---|
175 | [ $# -lt 4 ] && ogRaiseError $OG_ERR_FORMAT && return $? |
---|
176 | # Procesar parámetros. |
---|
177 | PART="$(ogDiskToDev "$3" "$4")" || return $? |
---|
178 | IMGFILE=$(ogGetPath "$1" "$2.img") || return $? |
---|
179 | [ -r "$IMGFILE" ] || ogRaiseError OG_ERR_NOTFOUND "$IMGFILE" || return $? |
---|
180 | # Error si la imagen no cabe en la particion. |
---|
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 |
---|
187 | |
---|
188 | # Comprobar el bloqueo de la partición, desmontarla y bloquearla. |
---|
189 | if ogIsLocked $3 $4; then |
---|
190 | ogRaiseError $OG_ERR_LOCKED "$3,$4" |
---|
191 | return $? |
---|
192 | fi |
---|
193 | ogUnmount $3 $4 2>/dev/null || return $? |
---|
194 | ogLock $3 $4 || return $? |
---|
195 | trap "ogUnlock $3 $4" 1 2 3 6 9 |
---|
196 | |
---|
197 | # Restaurar según el tipo de sistema de archivos. |
---|
198 | # Atención: no se comprueba incongruencia entre partición e imagen. |
---|
199 | TYPE="$(ogGetFsType $3 $4)" |
---|
200 | case "$TYPE" in |
---|
201 | EXT[234]|REISERFS|XFS|JFS) |
---|
202 | partimage -M -f3 -o -d -V0 -B gui=no -c -z1 --volume=0 restore $PART "$IMGFILE" |
---|
203 | ;; |
---|
204 | NTFS|HNTFS) |
---|
205 | partimage -M -f3 -o -d -V0 -B gui=no -c -z1 --volume=0 restore $PART "$IMGFILE" |
---|
206 | ;; |
---|
207 | FAT16|FAT32|HFAT16|HFAT32) |
---|
208 | partimage -M -f3 -o -d -V0 -B gui=no -c -z1 --volume=0 restore $PART "$IMGFILE" |
---|
209 | ;; |
---|
210 | *) ogRaiseError $OG_ERR_FORMAT |
---|
211 | return $? ;; |
---|
212 | esac |
---|
213 | ERRCODE=$? |
---|
214 | if [ $ERRCODE != 0 ]; then |
---|
215 | ogRaiseError $OG_ERR_IMAGE "$IMGFILE, $3, $4" |
---|
216 | fi |
---|
217 | ogUnlock $3 $4 |
---|
218 | return $ERRCODE |
---|
219 | } |
---|
220 | |
---|
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 |
---|
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" |
---|
244 | fi |
---|
245 | if [ $# = 5 ] |
---|
246 | then |
---|
247 | CloneImageNTFS="${CLONETOOLSNTFS:-partimage}" |
---|
248 | CloneImageEXT23="${CLONETOOLSEXT:-partimage}" |
---|
249 | CompresionImage="${CLONETOOLSCOMPRESSOR:-gzip}" |
---|
250 | |
---|
251 | disco=`ogDiskToDev $1 $2` |
---|
252 | ogUnmountPartition $1 $2 |
---|
253 | fs=`ogGetFsType $1 $2` |
---|
254 | |
---|
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. |
---|
307 | case $CompresionImage in |
---|
308 | "gzip") |
---|
309 | comando="$imaged | gzip -c >" |
---|
310 | ;; |
---|
311 | "lzop") |
---|
312 | comando="$imaged | lzop >" |
---|
313 | ;; |
---|
314 | esac |
---|
315 | |
---|
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 |
---|
323 | |
---|
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 |
---|
331 | |
---|
332 | #iniciamos la creacion del torrent |
---|
333 | echo "Iniciando la creacion del punto torrent con CreateTorrentFromImage ip=$3 path=$4 image=$5-$2" echo |
---|
334 | #CreateTorrentFromImage $3 $4 $5-$2 |
---|
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 | } |
---|