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 | #@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) |
---|
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.9 - Versión en pruebas para OpenGNSys |
---|
29 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
30 | #@date 2009/10/07 |
---|
31 | #*/ |
---|
32 | function ogCreateImage () { |
---|
33 | |
---|
34 | # Variables locales |
---|
35 | local PART IMGDIR IMGFILE ERRCODE |
---|
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 REPO /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 | |
---|
46 | PART="$(ogDiskToDev $1 $2)" || return $? |
---|
47 | if ogIsLocked $1 $2; then |
---|
48 | ogRaiseError $OG_ERR_LOCKED "$1,$2" |
---|
49 | return $? |
---|
50 | fi |
---|
51 | IMGDIR=$(ogGetParentPath "$3" "$4") |
---|
52 | [ -n "$IMGDIR" ] || ogRaiseError $OG_ERR_NOTFOUND "$3 $(dirname $4)" || return $? |
---|
53 | IMGFILE="$IMGDIR/$(basename "$4").img" |
---|
54 | ogUnmount $1 $2 2>/dev/null |
---|
55 | |
---|
56 | ogLock $1 $2 || return $? |
---|
57 | trap "ogUnlock $1 $2; rm -f $IMGFILE" 1 2 3 6 9 |
---|
58 | |
---|
59 | TYPE="$(ogGetFsType $1 $2)" |
---|
60 | case "$TYPE" in |
---|
61 | EXT[234]|REISERFS|XFS|JFS) |
---|
62 | partimage -M -f3 -o -d -V0 -B gui=no -c -z1 save $PART "$IMGFILE" |
---|
63 | ;; |
---|
64 | NTFS|HNTFS) |
---|
65 | partimage -M -f3 -o -d -V0 -B gui=no -c -z1 save $PART "$IMGFILE" |
---|
66 | ;; |
---|
67 | FAT16|FAT32|HFAT16|HFAT32) |
---|
68 | partimage -M -f3 -o -d -V0 -B gui=no -c -z1 save $PART "$IMGFILE" |
---|
69 | ;; |
---|
70 | *) ogRaiseError $OG_ERR_PARTITION "$1 $2 $TYPE" |
---|
71 | return $? ;; |
---|
72 | esac |
---|
73 | |
---|
74 | #/// Controlar salida de error y desbloquear partición. |
---|
75 | ERRCODE=$? |
---|
76 | if [ $ERRCODE != 0 ]; then |
---|
77 | ogRaiseError $OG_ERR_IMAGE "$1 $2 $IMGFILE" |
---|
78 | rm -f "$IMGFILE" |
---|
79 | fi |
---|
80 | ogUnlock $1 $2 |
---|
81 | return $ERRCODE |
---|
82 | } |
---|
83 | |
---|
84 | |
---|
85 | #/** |
---|
86 | # ogGetImageSize str_repo path_image |
---|
87 | #@brief Devuelve el tamaño del sistema de archivos almacenado en un fichero de imagen. |
---|
88 | #@arg \c str_repo repositorio de imágenes o caché local |
---|
89 | #@arg \c path_image camino de la imagen |
---|
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" \ |
---|
108 | "$FUNCNAME 1 1 REPO /aula1/winxp ==> 5642158" |
---|
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. |
---|
114 | IMGFILE=$(ogGetPath "$1" "$2.img") || return $? |
---|
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 | |
---|
123 | #### PRUEBAS |
---|
124 | # Obtener tipo de imagen |
---|
125 | function ogGetImageType () { |
---|
126 | local IMGFILE |
---|
127 | IMGFILE=$(ogGetPath "$1" "$2.img") || return $? |
---|
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 | |
---|
134 | #/** |
---|
135 | # ogRestoreImage str_repo path_image int_ndisk int_npartition |
---|
136 | #@brief Restaura una imagen de sistema de archivos en una partición. |
---|
137 | #@arg \c str_repo repositorio de imágenes o caché local |
---|
138 | #@arg \c path_image camino de la imagen |
---|
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 |
---|
147 | #@todo Comprobar incongruencias partición-imagen, control de errores, definir parámetros, caché/repositorio, etc. |
---|
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 |
---|
155 | local PART PARTSIZE IMGFILE IMGSIZE |
---|
156 | |
---|
157 | #/// Si se solicita, mostrar ayuda. |
---|
158 | if [ "$*" == "help" ]; then |
---|
159 | ogHelp "$FUNCNAME" "$FUNCNAME path_dir str_image int_ndisk int_npart" \ |
---|
160 | "$FUNCNAME REPO /aula1/winxp 1 1" |
---|
161 | return |
---|
162 | fi |
---|
163 | #/// Error si no se reciben 4 parámetros. |
---|
164 | [ $# -lt 4 ] && ogRaiseError $OG_ERR_FORMAT && return $? |
---|
165 | # Procesar parámetros. |
---|
166 | PART="$(ogDiskToDev "$3" "$4")" || return $? |
---|
167 | IMGFILE=$(ogGetPath "$1" "$2.img") || return $? |
---|
168 | [ -r "$IMGFILE" ] || ogRaiseError OG_ERR_NOTFOUND "$IMGFILE" || return $? |
---|
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 |
---|
176 | |
---|
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 |
---|
182 | ogUnmount $3 $4 2>/dev/null || return $? |
---|
183 | ogLock $3 $4 || return $? |
---|
184 | trap "ogUnlock $3 $4" 1 2 3 6 9 |
---|
185 | |
---|
186 | #/// Restaurar según el tipo de sistema de archivos. |
---|
187 | #/// Atención: no se comprueba incongruencia entre partición e imagen. |
---|
188 | TYPE="$(ogGetFsType $3 $4)" |
---|
189 | case "$TYPE" in |
---|
190 | EXT[234]|REISERFS|XFS|JFS) |
---|
191 | partimage -M -f3 -o -d -V0 -B gui=no -c -z1 --volume=0 restore $PART "$IMGFILE" |
---|
192 | ;; |
---|
193 | NTFS|HNTFS) |
---|
194 | partimage -M -f3 -o -d -V0 -B gui=no -c -z1 --volume=0 restore $PART "$IMGFILE" |
---|
195 | ;; |
---|
196 | FAT16|FAT32|HFAT16|HFAT32) |
---|
197 | partimage -M -f3 -o -d -V0 -B gui=no -c -z1 --volume=0 restore $PART "$IMGFILE" |
---|
198 | ;; |
---|
199 | *) ogRaiseError $OG_ERR_FORMAT |
---|
200 | return $? ;; |
---|
201 | esac |
---|
202 | ERRCODE=$? |
---|
203 | if [ $ERRCODE != 0 ]; then |
---|
204 | ogRaiseError $OG_ERR_IMAGE "$IMGFILE, $3, $4" |
---|
205 | fi |
---|
206 | ogUnlock $3 $4 |
---|
207 | return $ERRCODE |
---|
208 | } |
---|
209 | |
---|
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 |
---|
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" |
---|
233 | fi |
---|
234 | if [ $# = 5 ] |
---|
235 | then |
---|
236 | CloneImageNTFS="${CLONETOOLSNTFS:-partimage}" |
---|
237 | CloneImageEXT23="${CLONETOOLSEXT:-partimage}" |
---|
238 | CompresionImage="${CLONETOOLSCOMPRESSOR:-gzip}" |
---|
239 | |
---|
240 | disco=`ogDiskToDev $1 $2` |
---|
241 | ogUnmountPartition $1 $2 |
---|
242 | fs=`ogGetFsType $1 $2` |
---|
243 | |
---|
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. |
---|
296 | case $CompresionImage in |
---|
297 | "gzip") |
---|
298 | comando="$imaged | gzip -c >" |
---|
299 | ;; |
---|
300 | "lzop") |
---|
301 | comando="$imaged | lzop >" |
---|
302 | ;; |
---|
303 | esac |
---|
304 | |
---|
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 |
---|
312 | |
---|
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 |
---|
320 | |
---|
321 | #iniciamos la creacion del torrent |
---|
322 | echo "Iniciando la creacion del punto torrent con CreateTorrentFromImage ip=$3 path=$4 image=$5-$2" echo |
---|
323 | #CreateTorrentFromImage $3 $4 $5-$2 |
---|
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 | } |
---|