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 1.1.0 |
---|
8 | #@warning License: GNU GPLv3+ |
---|
9 | #*/ |
---|
10 | |
---|
11 | |
---|
12 | #/** |
---|
13 | # ogCreateImageSyntax path_device path_filename [str_tool] [str_compressionlevel] |
---|
14 | #@brief Genera una cadena de texto con la instrucción para crear un fichero imagen |
---|
15 | #@param path_device dispositivo Linux del sistema de archivos |
---|
16 | #@param path_fileneme path absoluto del fichero imagen |
---|
17 | #@param [opcional] str_tool herrmaienta de clonacion [partimage, partclone, ntfsclone] |
---|
18 | #@param [opcional] str_compressionlevel nivel de compresion. [0 -none-, 1-lzop-, 2-gzip] |
---|
19 | #@return str_command - cadena con el comando que se debe ejecutar. |
---|
20 | #@warning Salida nula si se producen errores. |
---|
21 | #@TODO introducir las herramientas fsarchiver, dd |
---|
22 | #@version 1.0 - Primeras pruebas |
---|
23 | #@author Antonio J. Doblas Viso. Universidad de Málaga |
---|
24 | #@date 2010/02/08 |
---|
25 | #@version 1.0.5 - Incrustar códico de antigua función ogPartcloneSyntax |
---|
26 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
27 | #@date 2012/09/14 |
---|
28 | #*/ ## |
---|
29 | function ogCreateImageSyntax() |
---|
30 | { |
---|
31 | local FS TOOL LEVEL DEV IMGFILE BUFFER PARAM1 PARAM2 PARAM3 |
---|
32 | |
---|
33 | # Si se solicita, mostrar ayuda. |
---|
34 | if [ "$*" == "help" ]; then |
---|
35 | ogHelp "$FUNCNAME" "$FUNCNAME path_device path_imagefile [str_tool] [str_compressionlevel]" \ |
---|
36 | "$FUNCNAME /dev/sda1 /opt/opengnsys/images/prueba.img partclone lzop" \ |
---|
37 | "$FUNCNAME /dev/sda1 /opt/opengnsys/images/prueba.img" |
---|
38 | return |
---|
39 | fi |
---|
40 | # Error si no se reciben entre 2 y 4 parámetros. |
---|
41 | [ $# -ge 2 -a $# -le 4 ] || ogRaiseError $OG_ERR_FORMAT "$*" || return $? |
---|
42 | |
---|
43 | # Asignación de parámetros. |
---|
44 | DEV="$1" |
---|
45 | IMGFILE="$2" |
---|
46 | case "$#" in |
---|
47 | 2) # Sintaxis por defecto OG DEV IMGFILE |
---|
48 | TOOL="partclone" |
---|
49 | LEVEL="gzip" |
---|
50 | ;; |
---|
51 | 4) # Sintaxis condicionada. |
---|
52 | TOOL="${3,,}" |
---|
53 | LEVEL="${4,,}" |
---|
54 | ;; |
---|
55 | esac |
---|
56 | |
---|
57 | case "$TOOL" in |
---|
58 | ntfsclone) |
---|
59 | PARAM1="ntfsclone --force --save-image -O - $DEV" |
---|
60 | ;; |
---|
61 | partimage|default) |
---|
62 | PARAM1="partimage -M -f3 -o -d -B gui=no -c -z0 --volume=0 save $DEV stdout" |
---|
63 | ;; |
---|
64 | partclone) |
---|
65 | FS="$(ogGetFsType $(ogDevToDisk $DEV 2>/dev/null) 2>/dev/null)" |
---|
66 | case "$FS" in |
---|
67 | EXT[234]) PARAM1="partclone.extfs" ;; |
---|
68 | BTRFS) PARAM1="partclone.btrfs" ;; |
---|
69 | REISERFS) PARAM1="partclone.reiserfs" ;; |
---|
70 | REISER4) PARAM1="partclone.reiser4" ;; |
---|
71 | JFS) PARAM1="partclone.jfs" ;; |
---|
72 | XFS) PARAM1="partclone.xfs" ;; |
---|
73 | NTFS) PARAM1="partclone.ntfs" ;; |
---|
74 | EXFAT) PARAM1="partclone.exfat" ;; |
---|
75 | FAT16|FAT32) PARAM1="partclone.fat" ;; |
---|
76 | HFS|HFSPLUS) PARAM1="partclone.hfsp" ;; |
---|
77 | UFS) PARAM1="partclone.ufs" ;; |
---|
78 | *) PARAM1="partclone.dd" ;; |
---|
79 | esac |
---|
80 | # Por compatibilidad, si no existe el ejecutable usar por defecto "parclone.dd". |
---|
81 | which $PARAM1 &>/dev/null || PARAM1="partclone.dd" |
---|
82 | PARAM1="$PARAM1 -d0 -F -c -s $DEV" |
---|
83 | # El programa partclone.dd no tiene opción "-c". |
---|
84 | [[ "$PARAM1" =~ ^partclone.dd ]] && PARAM1="${PARAM1/ -c / }" |
---|
85 | ;; |
---|
86 | esac |
---|
87 | # Comprobar que existe mbuffer. |
---|
88 | which mbuffer &>/dev/null && PARAM2="| mbuffer -q -m 40M " || PARAM2=" " |
---|
89 | |
---|
90 | # Nivel de compresion. |
---|
91 | case "$LEVEL" in |
---|
92 | 0|none) PARAM3=" > " ;; |
---|
93 | 1|lzop) PARAM3=" | lzop > " ;; |
---|
94 | 2|gzip) PARAM3=" | gzip -c > " ;; |
---|
95 | 3|bzip) PARAM3=" | bzip -c > " ;; |
---|
96 | esac |
---|
97 | |
---|
98 | # Sintaxis final. |
---|
99 | [ -n "$PARAM1" ] && echo "$PARAM1 $PARAM2 $PARAM3 $IMGFILE" |
---|
100 | } |
---|
101 | |
---|
102 | |
---|
103 | #/** |
---|
104 | # ogRestoreImageSyntax path_filename path_device [str_tools] [str_compressionlevel] |
---|
105 | #@brief Genera una cadena de texto con la instrucción para crear un fichero imagen |
---|
106 | #@param path_device dispositivo Linux del sistema de archivos |
---|
107 | #@param path_fileneme path absoluto del fichero imagen |
---|
108 | #@param [opcional] str_tools herrmaienta de clonacion [partimage, partclone, ntfsclone] |
---|
109 | #@param [opcional] str_compressionlevel nivel de compresion. [0 -none-, 1-lzop-, 2-gzip] |
---|
110 | #@return cadena con el comando que se debe ejecutar. |
---|
111 | #@exception OG_ERR_FORMAT formato incorrecto. |
---|
112 | #@warning En pruebas iniciales |
---|
113 | #@TODO introducir las herramientas fsarchiver, dd |
---|
114 | #@TODO introducir el nivel de compresion gzip |
---|
115 | #@version 1.0 - Primeras pruebas |
---|
116 | #@author Antonio J. Doblas Viso. Universidad de Málaga |
---|
117 | #@date 2010/02/08 |
---|
118 | #*/ ## |
---|
119 | function ogRestoreImageSyntax () |
---|
120 | { |
---|
121 | local TOOL COMPRESSOR LEVEL PART IMGFILE FILEHEAD INFOIMG |
---|
122 | |
---|
123 | |
---|
124 | # Si se solicita, mostrar ayuda. |
---|
125 | if [ "$*" == "help" ]; then |
---|
126 | ogHelp "$FUNCNAME" "$FUNCNAME filename partition [tool] [levelcompresor]" \ |
---|
127 | "$FUNCNAME /opt/opengnsys/images/prueba.img /dev/sda1 [partclone] [lzop]" |
---|
128 | return |
---|
129 | fi |
---|
130 | |
---|
131 | # Error si no se reciben entre 2 y 4 parámetros. |
---|
132 | [ $# -ge 2 -a $# -le 4 ] || ogRaiseError $OG_ERR_FORMAT "$*" || return $? |
---|
133 | |
---|
134 | # controlamos que el parametro 1 (imagen) es tipo file. |
---|
135 | [ -f $1 ] || ogRaiseError $OG_ERR_NOTFOUND "$1" || return $? |
---|
136 | |
---|
137 | # Si 2 parametros (file-origen-, device-destino-) = ogGetImageFull($1) |
---|
138 | if [ "$#" -eq 2 ]; then |
---|
139 | IMGFILE=$1 |
---|
140 | PART=$2 |
---|
141 | INFOIMG=$(ogGetImageInfo $IMGFILE) || ogRaiseError $OG_ERR_NOTFOUND "No Image $1" || return $? |
---|
142 | TOOL=`echo $INFOIMG | cut -f1 -d:` |
---|
143 | COMPRESSOR=`echo $INFOIMG | cut -f2 -d:` |
---|
144 | ogRestoreImageSyntax $IMGFILE $PART $TOOL $COMPRESSOR |
---|
145 | fi |
---|
146 | |
---|
147 | |
---|
148 | # Si cuatro parametros genera sintaxis |
---|
149 | if [ "$#" -eq 4 ]; then |
---|
150 | IMGFILE=$1 |
---|
151 | PART=$2 |
---|
152 | # comprobamos parametro herramienta compresion. |
---|
153 | TOOL=$(echo $3 | tr [A-Z] [a-z]) |
---|
154 | #ogCheckProgram $TOOL |
---|
155 | #comprobar parámetro compresor. |
---|
156 | LEVEL=$(echo $4 | tr [A-Z] [a-z]) |
---|
157 | #ogCheckProgram $LEVEL |
---|
158 | |
---|
159 | case "$LEVEL" in |
---|
160 | "0"|"none") |
---|
161 | COMPRESSOR=" " |
---|
162 | ;; |
---|
163 | "1"|"lzop" | "LZOP") |
---|
164 | COMPRESSOR=" lzop -dc " |
---|
165 | ;; |
---|
166 | "2"|"gzip" | "GZIP") |
---|
167 | COMPRESSOR=" gzip -dc " |
---|
168 | ;; |
---|
169 | "3"|"bzip" | "BZIP" ) |
---|
170 | COMPRESSOR=" bzip -dc " |
---|
171 | ;; |
---|
172 | *) |
---|
173 | ogRaiseError $OG_ERR_NOTFOUND "Compressor no valid $TOOL" || return $? |
---|
174 | ;; |
---|
175 | esac |
---|
176 | #comprobar mbuffer |
---|
177 | which mbuffer > /dev/null && MBUFFER="| mbuffer -q -m 40M " || MBUFFER=" " |
---|
178 | |
---|
179 | case "$TOOL" in |
---|
180 | "ntfsclone" | "NTFSCLONE") |
---|
181 | TOOL="| ntfsclone --restore-image --overwrite $PART -" |
---|
182 | ;; |
---|
183 | "partimage"| "PARTIMAGE") |
---|
184 | TOOL="| partimage -f3 -B gui=no restore $PART stdin" |
---|
185 | ;; |
---|
186 | "partclone" | "PARTCLONE") |
---|
187 | # -C para que no compruebe tamaños |
---|
188 | TOOL="| partclone.restore -d0 -C -I -o $PART" |
---|
189 | ;; |
---|
190 | partclone.dd) |
---|
191 | TOOL="| pv | dd conv=sync,noerror bs=1M of=$PART" |
---|
192 | ;; |
---|
193 | *) |
---|
194 | ogRaiseError $OG_ERR_NOTFOUND "Tools imaging no valid $TOOL" || return $? |
---|
195 | ;; |
---|
196 | esac |
---|
197 | |
---|
198 | echo "$COMPRESSOR $IMGFILE $MBUFFER $TOOL" |
---|
199 | fi |
---|
200 | |
---|
201 | } |
---|
202 | |
---|
203 | |
---|
204 | |
---|
205 | |
---|
206 | #/** |
---|
207 | # ogCreateDiskImage int_ndisk str_repo path_image [str_tools] [str_compressionlevel] |
---|
208 | #@brief Crea una imagen (copia de seguridad) de un disco completo. |
---|
209 | #@param int_ndisk nº de orden del disco |
---|
210 | #@param str_repo repositorio de imágenes (remoto o caché local) |
---|
211 | #@param path_image camino de la imagen (sin extensión) |
---|
212 | #@return (nada, por determinar) |
---|
213 | #@note repo = { REPO, CACHE } |
---|
214 | #@note Esta primera versión crea imágenes con dd comprimidas con gzip. |
---|
215 | #@exception OG_ERR_FORMAT formato incorrecto. |
---|
216 | #@exception OG_ERR_NOTFOUND fichero o dispositivo no encontrado. |
---|
217 | #@exception OG_ERR_LOCKED particion bloqueada por otra operación. |
---|
218 | #@exception OG_ERR_IMAGE error al crear la imagen del sistema. |
---|
219 | #@warning En pruebas iniciales |
---|
220 | #@todo Gestión de bloqueos de disco |
---|
221 | #@todo Comprobar si debe desmontarse la caché local |
---|
222 | #@todo Comprobar que no se crea la imagen en el propio disco |
---|
223 | #@version 1.1.0 - Primera versión para OpenGnsys con herramientas prefijadas. |
---|
224 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
225 | #@Date 2016/04/08 |
---|
226 | #*/ ## |
---|
227 | function ogCreateDiskImage () |
---|
228 | { |
---|
229 | # Variables locales |
---|
230 | local DISK PROGRAM IMGDIR IMGFILE IMGTYPE ERRCODE |
---|
231 | |
---|
232 | # Si se solicita, mostrar ayuda. |
---|
233 | if [ "$*" == "help" ]; then |
---|
234 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk path_dir str_image" \ |
---|
235 | "$FUNCNAME 1 REPO /disk1" |
---|
236 | return |
---|
237 | fi |
---|
238 | # Error si no se reciben entre 3 y 5 parámetros. |
---|
239 | [ $# -ge 3 -a $# -le 5 ] || ogRaiseError $OG_ERR_FORMAT "$*" || return $? |
---|
240 | |
---|
241 | # Comprobar que no está bloqueada ni la partición, ni la imagen. |
---|
242 | DISK="$(ogDiskToDev $1)" || return $? |
---|
243 | if ogIsDiskLocked $1; then |
---|
244 | ogRaiseError $OG_ERR_LOCKED "$MSG_LOCKED $1" |
---|
245 | return $? |
---|
246 | fi |
---|
247 | IMGTYPE="dsk" # Extensión genérica de imágenes de disco. |
---|
248 | IMGDIR=$(ogGetParentPath "$2" "$3") |
---|
249 | [ -n "$IMGDIR" ] || ogRaiseError $OG_ERR_NOTFOUND "$2 $(dirname $3)" || return $? |
---|
250 | IMGFILE="$IMGDIR/$(basename "$3").$IMGTYPE" |
---|
251 | if ogIsImageLocked "$IMGFILE"; then |
---|
252 | ogRaiseError $OG_ERR_LOCKED "$MSG_IMAGE $3, $4" |
---|
253 | return $? |
---|
254 | fi |
---|
255 | |
---|
256 | # No guardar imagen en el propio disco (disco no incluido en el camino del repositorio). |
---|
257 | if [[ $(ogGetPath "$2" /) =~ ^$DISK ]]; then |
---|
258 | ogRaiseError $OG_ERR_IMAGE "$2 = $DISK" |
---|
259 | return $? |
---|
260 | fi |
---|
261 | |
---|
262 | # Generar la instruccion a ejecutar antes de aplicar los bloqueos. |
---|
263 | PROGRAM=$(ogCreateImageSyntax $DISK $IMGFILE) |
---|
264 | # Desmontar todos los sistemas de archivos del disco, bloquear disco e imagen. |
---|
265 | ogUnmountAll $1 2>/dev/null |
---|
266 | ogLockDisk $1 || return $? |
---|
267 | ogLockImage "$2" "$3.$IMGTYPE" || return $? |
---|
268 | |
---|
269 | # Crear Imagen. |
---|
270 | trap "ogUnlockDisk $1; ogUnlockImage "$3" "$4.$IMGTYPE"; rm -f $IMGFILE" 1 2 3 6 9 |
---|
271 | eval $PROGRAM |
---|
272 | |
---|
273 | # Controlar salida de error y desbloquear partición. |
---|
274 | ERRCODE=$? |
---|
275 | if [ $ERRCODE != 0 ]; then |
---|
276 | ogRaiseError $OG_ERR_IMAGE "$1 $2 $IMGFILE" |
---|
277 | rm -f "$IMGFILE" |
---|
278 | fi |
---|
279 | # Desbloquear disco e imagen. |
---|
280 | ogUnlockDisk $1 |
---|
281 | ogUnlockImage "$2" "$3.$IMGTYPE" |
---|
282 | return $ERRCODE |
---|
283 | } |
---|
284 | |
---|
285 | |
---|
286 | |
---|
287 | #/** |
---|
288 | # ogCreateImage int_ndisk int_npartition str_repo path_image [str_tools] [str_compressionlevel] |
---|
289 | #@brief Crea una imagen a partir de una partición. |
---|
290 | #@param int_ndisk nº de orden del disco |
---|
291 | #@param int_npartition nº de orden de la partición |
---|
292 | #@param str_repo repositorio de imágenes (remoto o caché local) |
---|
293 | #@param path_image camino de la imagen (sin extensión) |
---|
294 | #@param [opcional] str_tools herrmaienta de clonacion [partimage, partclone, ntfsclone] |
---|
295 | #@param [opcional] str_compressionlevel nivel de compresion. [0 -none-, 1-lzop-, 2-gzip] |
---|
296 | #@return (nada, por determinar) |
---|
297 | #@note repo = { REPO, CACHE } |
---|
298 | #@exception OG_ERR_FORMAT formato incorrecto. |
---|
299 | #@exception OG_ERR_NOTFOUND fichero o dispositivo no encontrado. |
---|
300 | #@exception OG_ERR_PARTITION partición no accesible o no soportada. |
---|
301 | #@exception OG_ERR_LOCKED particion bloqueada por otra operación. |
---|
302 | #@exception OG_ERR_IMAGE error al crear la imagen del sistema. |
---|
303 | #@todo Comprobaciones, control de errores, definir parámetros, etc. |
---|
304 | #@version 0.1 - Integracion para Opengnsys - HIDRA:CrearImagen{EXT3, NTFS}.sh; EAC: CreateImageFromPartition () en Deploy.lib |
---|
305 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
306 | #@Date 2008/05/13 |
---|
307 | #@author Antonio J. Doblas Viso. Universidad de Malaga |
---|
308 | #@date 2008/10/27 |
---|
309 | #@version 0.9 - Versión en pruebas para OpenGnSys |
---|
310 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
311 | #@date 2009/10/07 |
---|
312 | #@version 1.0 - Llama a función ogCreateImageSyntax para generar la llamada al comando. |
---|
313 | #@author Antonio J. Doblas Viso. Universidad de Málaga |
---|
314 | #@date 2010/02/08 |
---|
315 | #*/ ## |
---|
316 | function ogCreateImage () |
---|
317 | { |
---|
318 | # Variables locales |
---|
319 | local PART PROGRAM IMGDIR IMGFILE IMGTYPE ERRCODE |
---|
320 | |
---|
321 | # Si se solicita, mostrar ayuda. |
---|
322 | if [ "$*" == "help" ]; then |
---|
323 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npart path_dir str_image" \ |
---|
324 | "$FUNCNAME 1 1 REPO /aula1/winxp" |
---|
325 | return |
---|
326 | fi |
---|
327 | # Error si no se reciben entre 4 y 6 parámetros. |
---|
328 | [ $# -ge 4 -a $# -le 6 ] || ogRaiseError $OG_ERR_FORMAT "$*" || return $? |
---|
329 | |
---|
330 | # Comprobar que no está bloqueada ni la partición, ni la imagen. |
---|
331 | PART="$(ogDiskToDev $1 $2)" || return $? |
---|
332 | if ogIsLocked $1 $2; then |
---|
333 | ogRaiseError $OG_ERR_LOCKED "$MSG_LOCKED $1, $2" |
---|
334 | return $? |
---|
335 | fi |
---|
336 | |
---|
337 | IMGTYPE="img" # Extensión genérica de imágenes. |
---|
338 | IMGDIR=$(ogGetParentPath "$3" "$4") |
---|
339 | [ -n "$IMGDIR" ] || ogRaiseError $OG_ERR_NOTFOUND "$3 $(dirname $4)" || return $? |
---|
340 | |
---|
341 | IMGFILE="$IMGDIR/$(basename "$4").$IMGTYPE" |
---|
342 | if ogIsImageLocked "$IMGFILE"; then |
---|
343 | ogRaiseError $OG_ERR_LOCKED "$MSG_IMAGE $3, $4" |
---|
344 | return $? |
---|
345 | fi |
---|
346 | # Generar la instruccion a ejecutar antes de aplicar los bloqueos. |
---|
347 | PROGRAM=$(ogCreateImageSyntax $PART $IMGFILE $5 $6) |
---|
348 | # Desmontar partición, bloquear partición e imagen. |
---|
349 | ogUnmount $1 $2 2>/dev/null |
---|
350 | ogLock $1 $2 || return $? |
---|
351 | ogLockImage "$3" "$4.$IMGTYPE" || return $? |
---|
352 | |
---|
353 | # Crear Imagen. |
---|
354 | trap "ogUnlock $1 $2; ogUnlockImage "$3" "$4.$IMGTYPE"; rm -f $IMGFILE" 1 2 3 6 9 |
---|
355 | eval $PROGRAM |
---|
356 | |
---|
357 | # Controlar salida de error y desbloquear partición. |
---|
358 | ERRCODE=$? |
---|
359 | if [ $ERRCODE != 0 ]; then |
---|
360 | ogRaiseError $OG_ERR_IMAGE "$1 $2 $IMGFILE" |
---|
361 | rm -f "$IMGFILE" |
---|
362 | fi |
---|
363 | # Desbloquear partición e imagen. |
---|
364 | ogUnlock $1 $2 |
---|
365 | ogUnlockImage "$3" "$4.$IMGTYPE" |
---|
366 | return $ERRCODE |
---|
367 | } |
---|
368 | |
---|
369 | |
---|
370 | #/** |
---|
371 | # ogCreateMbrImage int_ndisk str_repo path_image |
---|
372 | #@brief Crea una imagen a partir del sector de arranque de un disco. |
---|
373 | #@param int_ndisk nº de orden del disco |
---|
374 | #@param str_repo repositorio de imágenes (remoto o caché local) |
---|
375 | #@param path_image camino de la imagen (sin extensión) |
---|
376 | #@return (nada, por determinar) |
---|
377 | #@note repo = { REPO, CACHE } |
---|
378 | #@exception OG_ERR_FORMAT formato incorrecto. |
---|
379 | #@exception OG_ERR_NOTFOUND fichero o dispositivo no encontrado. |
---|
380 | #@exception OG_ERR_IMAGE error al crear la imagen del sistema. |
---|
381 | #@version 0.9 - Versión en pruebas para OpenGNSys |
---|
382 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
383 | #@date 2010/01/12 |
---|
384 | #@version 1.0 - Adaptación a OpenGnSys 1.0 |
---|
385 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
386 | #@date 2011/03/10 |
---|
387 | #*/ ## |
---|
388 | function ogCreateMbrImage () |
---|
389 | { |
---|
390 | # Variables locales |
---|
391 | local DISK IMGDIR IMGFILE |
---|
392 | # Si se solicita, mostrar ayuda. |
---|
393 | if [ "$*" == "help" ]; then |
---|
394 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk path_dir str_image" \ |
---|
395 | "$FUNCNAME 1 REPO /aula1/mbr" |
---|
396 | return |
---|
397 | fi |
---|
398 | # Error si no se reciben 3 parámetros. |
---|
399 | [ $# == 3 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
400 | |
---|
401 | DISK=$(ogDiskToDev "$1") || return $? |
---|
402 | IMGDIR=$(ogGetParentPath "$2" "$3") |
---|
403 | [ -n "$IMGDIR" ] || ogRaiseError $OG_ERR_NOTFOUND "$2 $(dirname $3)" || return $? |
---|
404 | IMGFILE="$IMGDIR/$(basename "$3").mbr" |
---|
405 | |
---|
406 | # Crear imagen del MBR. |
---|
407 | dd if="$DISK" of="$IMGFILE" bs=512 count=1 || ogRaiseError $OG_ERR_IMAGE "$1 $IMGFILE" || return $? |
---|
408 | } |
---|
409 | |
---|
410 | |
---|
411 | #/** |
---|
412 | # ogCreateBootLoaderImage int_ndisk str_repo path_image |
---|
413 | #@brief Crea una imagen del boot loader a partir del sector de arranque de un disco. |
---|
414 | #@param int_ndisk nº de orden del disco |
---|
415 | #@param str_repo repositorio de imágenes (remoto o caché local) |
---|
416 | #@param path_image camino de la imagen (sin extensión) |
---|
417 | #@return (nada, por determinar) |
---|
418 | #@note repo = { REPO, CACHE } |
---|
419 | #@exception OG_ERR_FORMAT formato incorrecto. |
---|
420 | #@exception OG_ERR_NOTFOUND fichero o dispositivo no encontrado. |
---|
421 | #@exception OG_ERR_IMAGE error al crear la imagen del sistema. |
---|
422 | #@version 1.0 - Adaptacion de ogCreateMbrImage para guardar solo el Boot Loader |
---|
423 | #@author Juan Carlos Xifre, SICUZ Universidad de Zaragoza |
---|
424 | #@date 2011/03/21 |
---|
425 | #*/ ## |
---|
426 | function ogCreateBootLoaderImage () |
---|
427 | { |
---|
428 | # Variables locales |
---|
429 | local DISK IMGDIR IMGFILE |
---|
430 | # Si se solicita, mostrar ayuda. |
---|
431 | if [ "$*" == "help" ]; then |
---|
432 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk path_dir str_image" \ |
---|
433 | "$FUNCNAME 1 REPO /aula1/mbr" |
---|
434 | return |
---|
435 | fi |
---|
436 | # Error si no se reciben 3 parámetros. |
---|
437 | [ $# == 3 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
438 | |
---|
439 | DISK=$(ogDiskToDev "$1") || return $? |
---|
440 | IMGDIR=$(ogGetParentPath "$2" "$3") |
---|
441 | [ -n "$IMGDIR" ] || ogRaiseError $OG_ERR_NOTFOUND "$2 $(dirname $3)" || return $? |
---|
442 | IMGFILE="$IMGDIR/$(basename "$3").mbr" |
---|
443 | |
---|
444 | # Crear imagen del Boot Loader dentro del MBR. |
---|
445 | dd if="$DISK" of="$IMGFILE" bs=446 count=1 || ogRaiseError $OG_ERR_IMAGE "$1 $IMGFILE" || return $? |
---|
446 | } |
---|
447 | |
---|
448 | #/** |
---|
449 | # ogGetSizeParameters int_num_disk int_num_part str_repo [monolit|sync|diff] |
---|
450 | #@brief Devuelve el tamaño de los datos de un sistema de ficheros, el espacio necesario para la imagen y si cabe en el repositorio elegido. |
---|
451 | #@param int_disk numero de disco |
---|
452 | #@param int_part numero de particion |
---|
453 | #@param str_repo repositorio de imágenes { REPO, CACHE } |
---|
454 | #@param str_imageType Tipo de imagen: monolit (por defecto), sync o diff. (parametro opcional) |
---|
455 | #@return SIZEDATA SIZEREQUIRED ISENOUGHSPACE |
---|
456 | #@note si str_imageType= diff necesario /tmp/ogimg.info, que es creado por ogCreateInfoImage. |
---|
457 | #@exception OG_ERR_FORMAT formato incorrecto. |
---|
458 | #@author Irina Gomez, ETSII Universidad de Sevilla |
---|
459 | #@date 2014/10/24 |
---|
460 | #*/ ## |
---|
461 | function ogGetSizeParameters () |
---|
462 | { |
---|
463 | local MNTDIR SIZEDATA KERNELVERSION SIZEREQUIRED FACTORGZIP FACTORLZOP SIZEFREE |
---|
464 | # Si se solicita, mostrar ayuda. |
---|
465 | if [ "$*" == "help" ]; then |
---|
466 | ogHelp "$FUNCNAME" "$FUNCNAME num_disk num_part str_repo [monolic|sync|diff]" \ |
---|
467 | "if $FUNCNAME 1 2 REPO sync ; then ...; fi" \ |
---|
468 | "if $FUNCNAME 1 6 CACHE ; then ...; fi" |
---|
469 | return |
---|
470 | fi |
---|
471 | # Error si no se reciben 1 o 2 parámetros. |
---|
472 | [ $# -lt 3 ] && return $(ogRaiseError session $OG_ERR_FORMAT "$MSG_FORMAT: $PROG ndisco nparticion REPO|CACHE [monolitic|sync]" ; echo $?) |
---|
473 | |
---|
474 | MNTDIR=$(ogMount $1 $2) |
---|
475 | if [ "$MNTDIR" == "" ]; then |
---|
476 | ogRaiseError $OG_ERR_PARTITION "$1 $2" |
---|
477 | return $? |
---|
478 | fi |
---|
479 | |
---|
480 | # Datos contenidos en la particion o en la lista de archivos de contiene la diferencial. |
---|
481 | if [ "_${4^^}_" == "_DIFF_" ]; then |
---|
482 | [ -r /tmp/ogimg.info ] || return $(ogRaiseError session $OG_ERR_NOTFOUND "/tmp/ogimg.info"; echo $?) |
---|
483 | cd $MNTDIR |
---|
484 | SIZEDATA=$(grep -v "\/$" /tmp/ogimg.info | tr '\n' '\0'| du -x -c --files0-from=- 2>/dev/null|tail -n1 |cut -f1) |
---|
485 | else |
---|
486 | SIZEDATA=$(df -k | grep $MNTDIR | awk '{print $3}') |
---|
487 | fi |
---|
488 | |
---|
489 | #Aplicar factor de compresion |
---|
490 | if [ "_${4^^}_" == "_SYNC_" -o "_${4^^}_" == "_DIFF_" ]; then |
---|
491 | |
---|
492 | # Sistema de fichero de la imagen según kernel, menor que 3.7 EXT4. comparamos revision |
---|
493 | KERNELVERSION=$(uname -r| awk '{printf("%d",$1);sub(/[0-9]*\./,"",$1);printf(".%02d",$1)}') |
---|
494 | [ $KERNELVERSION \< 3.07 ] && IMGFS="EXT4" || IMGFS=${IMGFS:-"BTRFS"} |
---|
495 | FACTORSYNC=${FACTORSYNC:-"120"} |
---|
496 | # Si IMGFS="BTRFS" la compresion es mayor. |
---|
497 | [ $IMGFS == "BTRFS" ] && let FACTORSYNC=$FACTORSYNC-20 |
---|
498 | |
---|
499 | let SIZEREQUIRED=$SIZEDATA*$FACTORSYNC/100 |
---|
500 | # El tamaño mínimo del sistema de ficheros btrfs es 250M, ponemos 300 |
---|
501 | [ $SIZEREQUIRED -lt 300000 ] && SIZEREQUIRED=300000 |
---|
502 | |
---|
503 | else |
---|
504 | FACTORGZIP=55/100 |
---|
505 | FACTORLZOP=65/100 |
---|
506 | let SIZEREQUIRED=$SIZEDATA*$FACTORLZOP |
---|
507 | fi |
---|
508 | |
---|
509 | #Comprobar espacio libre en el contenedor. |
---|
510 | [ "${3^^}" == "CACHE" ] && SIZEFREE=$(ogGetFreeSize `ogFindCache`) |
---|
511 | [ "${3^^}" == "REPO" ] && SIZEFREE=$(df -k | grep $OGIMG | awk '{print $4}') |
---|
512 | |
---|
513 | [ "$SIZEREQUIRED" -lt "$SIZEFREE" ] && ISENOUGHSPACE=TRUE || ISENOUGHSPACE=FALSE |
---|
514 | |
---|
515 | echo $SIZEDATA $SIZEREQUIRED $ISENOUGHSPACE |
---|
516 | |
---|
517 | |
---|
518 | } |
---|
519 | |
---|
520 | #/** |
---|
521 | # ogIsImageLocked [str_repo] path_image |
---|
522 | #@brief Comprueba si una imagen está bloqueada para uso exclusivo. |
---|
523 | #@param str_repo repositorio de imágenes (opcional) |
---|
524 | #@param path_image camino de la imagen (sin extensión) |
---|
525 | #@return Código de salida: 0 - bloqueado, 1 - sin bloquear o error. |
---|
526 | #@note repo = { REPO, CACHE } |
---|
527 | #@exception OG_ERR_FORMAT formato incorrecto. |
---|
528 | #@version 1.0 - Adaptación a OpenGnSys 1.0 |
---|
529 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
530 | #@date 2011/03/10 |
---|
531 | #@version 1.0.1 - Devolver falso en caso de error. |
---|
532 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
533 | #@date 2011-05-18 |
---|
534 | #*/ ## |
---|
535 | function ogIsImageLocked () |
---|
536 | { |
---|
537 | # Si se solicita, mostrar ayuda. |
---|
538 | if [ "$*" == "help" ]; then |
---|
539 | ogHelp "$FUNCNAME" "$FUNCNAME [str_repo] path_image" \ |
---|
540 | "if $FUNCNAME /opt/opengnsys/images/aula1/winxp.img; then ...; fi" \ |
---|
541 | "if $FUNCNAME REPO /aula1/winxp.img; then ...; fi" |
---|
542 | return |
---|
543 | fi |
---|
544 | # Error si no se reciben 1 o 2 parámetros. |
---|
545 | [ $# -lt 1 -o $# -gt 2 ] && return 1 |
---|
546 | |
---|
547 | # Comprobar si existe el fichero de bloqueo. |
---|
548 | test -n "$(ogGetPath $@.lock)" |
---|
549 | } |
---|
550 | |
---|
551 | |
---|
552 | #/** |
---|
553 | # ogLockImage [str_repo] path_image |
---|
554 | #@brief Bloquea una imagen para uso exclusivo. |
---|
555 | #@param str_repo repositorio de imágenes (opcional) |
---|
556 | #@param path_image camino de la imagen (sin extensión) |
---|
557 | #@return Nada. |
---|
558 | #@note Se genera un fichero con extensión .lock |
---|
559 | #@note repo = { REPO, CACHE } |
---|
560 | #@exception OG_ERR_FORMAT formato incorrecto. |
---|
561 | #@version 1.0 - Adaptación a OpenGnSys 1.0 |
---|
562 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
563 | #@date 2011/03/10 |
---|
564 | #*/ ## |
---|
565 | function ogLockImage () |
---|
566 | { |
---|
567 | # Variables locales |
---|
568 | local IMGDIR |
---|
569 | |
---|
570 | # Si se solicita, mostrar ayuda. |
---|
571 | if [ "$*" == "help" ]; then |
---|
572 | ogHelp "$FUNCNAME" "$FUNCNAME [str_repo] path_image" \ |
---|
573 | "$FUNCNAME /opt/opengnsys/images/aula1/winxp.img" \ |
---|
574 | "$FUNCNAME REPO /aula1/winxp.img" |
---|
575 | return |
---|
576 | fi |
---|
577 | # Error si no se reciben 1 o 2 parámetros. |
---|
578 | [ $# == 1 -o $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
579 | # Comprobar que existe directorio de imagen |
---|
580 | IMGDIR=$(ogGetParentPath $@) || return $? |
---|
581 | # Crear fichero de bloqueo. |
---|
582 | touch $IMGDIR/$(basename "${!#}").lock 2>/dev/null || ogRaiseError $OG_ERR_NOTWRITE "$*" || return $? |
---|
583 | } |
---|
584 | |
---|
585 | |
---|
586 | #/** |
---|
587 | # ogRestoreDiskImage str_repo path_image int_npartition |
---|
588 | #@brief Restaura (recupera) una imagen de un disco completo. |
---|
589 | #@param str_repo repositorio de imágenes o caché local |
---|
590 | #@param path_image camino de la imagen |
---|
591 | #@param int_ndisk nº de orden del disco |
---|
592 | #@return (por determinar) |
---|
593 | #@warning Primera versión en pruebas |
---|
594 | #@todo Gestionar bloqueos de disco |
---|
595 | #@todo Comprobar que no se intenta restaurar de la caché sobre el mismo disco |
---|
596 | #@exception OG_ERR_FORMAT formato incorrecto. |
---|
597 | #@exception OG_ERR_NOTFOUND fichero de imagen o partición no detectados. |
---|
598 | #@exception OG_ERR_LOCKED partición bloqueada por otra operación. |
---|
599 | #@exception OG_ERR_IMAGE error al restaurar la imagen del sistema. |
---|
600 | #@exception OG_ERR_IMGSIZEPARTITION Tamaño de la particion es menor al tamaño de la imagen. |
---|
601 | #@version 1.1.0 - Primera versión para OpenGnsys. |
---|
602 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
603 | #@Date 2016/04/08 |
---|
604 | #*/ ## |
---|
605 | function ogRestoreDiskImage () |
---|
606 | { |
---|
607 | # Variables locales |
---|
608 | local DISK DISKSIZE IMGFILE IMGTYPE IMGSIZE PROGRAM ERRCODE |
---|
609 | |
---|
610 | # Si se solicita, mostrar ayuda. |
---|
611 | if [ "$*" == "help" ]; then |
---|
612 | ogHelp "$FUNCNAME" "$FUNCNAME path_dir str_image int_ndisk" \ |
---|
613 | "$FUNCNAME REPO /aula1/winxp 1" |
---|
614 | return |
---|
615 | fi |
---|
616 | # Error si no se reciben 4 parámetros. |
---|
617 | [ $# == 3 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
618 | # Procesar parámetros. |
---|
619 | DISK="$(ogDiskToDev $3)" || return $(ogRaiseError $OG_ERR_NOTFOUND " $3 $4"; echo $?) |
---|
620 | IMGTYPE="dsk" |
---|
621 | IMGFILE=$(ogGetPath "$1" "$2.$IMGTYPE") |
---|
622 | [ -r "$IMGFILE" ] || return $(ogRaiseError $OG_ERR_NOTFOUND " $3 $4"; echo $?) |
---|
623 | |
---|
624 | # comprobamos consistencia de la imagen |
---|
625 | ogGetImageInfo $IMGFILE >/dev/null || return $(ogRaiseError $OG_ERR_IMAGE " $1 $2"; echo $?) |
---|
626 | # Error si la imagen no cabe en la particion. |
---|
627 | #IMGSIZE=$(ogGetImageSize "$1" "$2") || return $(ogRaiseError $OG_ERR_IMAGE " $1 $2"; echo $?) |
---|
628 | #DISKSIZE=$(ogGetDiskSize $3) |
---|
629 | #if [ $IMGSIZE -gt $DISKSIZE ]; then |
---|
630 | # ogRaiseError $OG_ERR_IMGSIZEPARTITION "$DISKSIZE < $IMGSIZE" |
---|
631 | # return $? |
---|
632 | #fi |
---|
633 | # Comprobar el bloqueo de la imagen y de la partición. |
---|
634 | if ogIsImageLocked "$IMGFILE"; then |
---|
635 | ogRaiseError $OG_ERR_LOCKED "$MSG_IMAGE $1, $2.$IMGTYPE" |
---|
636 | return $? |
---|
637 | fi |
---|
638 | if ogIsDiskLocked $3; then |
---|
639 | ogRaiseError $OG_ERR_LOCKED "$MSG_DISK $3" |
---|
640 | return $? |
---|
641 | fi |
---|
642 | # Solicitamos la generación de la instruccion a ejecutar |
---|
643 | PROGRAM=$(ogRestoreImageSyntax $IMGFILE $DISK) |
---|
644 | |
---|
645 | # Bloquear el disco |
---|
646 | ogLockDisk $3 || return $? |
---|
647 | trap "ogUnlockDisk $3" 1 2 3 6 9 |
---|
648 | |
---|
649 | # Ejecutar restauración según el tipo de imagen. |
---|
650 | eval $PROGRAM |
---|
651 | |
---|
652 | ERRCODE=$? |
---|
653 | if [ $ERRCODE != 0 ]; then |
---|
654 | ogRaiseError $OG_ERR_IMAGE "$IMGFILE, $3, $4" |
---|
655 | fi |
---|
656 | ogUnlockDisk $3 $4 |
---|
657 | return $ERRCODE |
---|
658 | } |
---|
659 | |
---|
660 | |
---|
661 | #/** |
---|
662 | # ogRestoreImage str_repo path_image int_ndisk int_npartition |
---|
663 | #@brief Restaura una imagen de sistema de archivos en una partición. |
---|
664 | #@param str_repo repositorio de imágenes o caché local |
---|
665 | #@param path_image camino de la imagen |
---|
666 | #@param int_ndisk nº de orden del disco |
---|
667 | #@param int_npartition nº de orden de la partición |
---|
668 | #@return (por determinar) |
---|
669 | #@exception OG_ERR_FORMAT 1 formato incorrecto. |
---|
670 | #@exception OG_ERR_NOTFOUND 2 fichero de imagen o partición no detectados. |
---|
671 | #@exception OG_ERR_PARTITION 3 # Error en partición de disco. |
---|
672 | #@exception OG_ERR_LOCKED 4 partición bloqueada por otra operación. |
---|
673 | #@exception OG_ERR_IMAGE 5 error al restaurar la imagen del sistema. |
---|
674 | #@exception OG_ERR_IMGSIZEPARTITION 30 Tamaño de la particion es menor al tamaño de la imagen. |
---|
675 | #@todo Comprobar incongruencias partición-imagen, control de errores, definir parámetros, caché/repositorio, etc. |
---|
676 | #@version 0.1 - Integracion para Opengnsys - HIDRA:RestaurarImagen{EXT3, NTFS}.sh; EAC: RestorePartitionFromImage() en Deploy.lib |
---|
677 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
678 | #@Date 2008/05/13 |
---|
679 | #@author Antonio J. Doblas Viso. Universidad de Malaga |
---|
680 | #@date 2008/10/27 |
---|
681 | #@version 0.9 - Primera version muy en pruebas para OpenGnSys |
---|
682 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
683 | #@date 2009/09/10 |
---|
684 | #@version 1.0 - generacion sintaxis de restauracion |
---|
685 | #@author Antonio J. Doblas Viso, Universidad de Malaga |
---|
686 | #@date 2011/02/01 |
---|
687 | #@version 1.0.1 - Control errores, tamaño particion, fichero-imagen |
---|
688 | #@author Antonio J. Doblas Viso, Universidad de Malaga |
---|
689 | #@date 2011/05/11 |
---|
690 | #*/ ## |
---|
691 | function ogRestoreImage () |
---|
692 | { |
---|
693 | # Variables locales |
---|
694 | local PART PARTSIZE IMGFILE IMGTYPE IMGSIZE FSTYPE PROGRAM ERRCODE |
---|
695 | |
---|
696 | # Si se solicita, mostrar ayuda. |
---|
697 | if [ "$*" == "help" ]; then |
---|
698 | ogHelp "$FUNCNAME" "$FUNCNAME path_dir str_image int_ndisk int_npart" \ |
---|
699 | "$FUNCNAME REPO /aula1/winxp 1 1" |
---|
700 | return |
---|
701 | fi |
---|
702 | # Error si no se reciben 4 parámetros. |
---|
703 | [ $# == 4 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
704 | # Procesar parámetros. |
---|
705 | PART="$(ogDiskToDev $3 $4)" || return $(ogRaiseError $OG_ERR_NOTFOUND " $3 $4"; echo $?) |
---|
706 | #IMGTYPE=$(ogGetImageType "$1" "$2") |
---|
707 | IMGTYPE=img |
---|
708 | IMGFILE=$(ogGetPath "$1" "$2.$IMGTYPE") |
---|
709 | [ -r "$IMGFILE" ] || return $(ogRaiseError $OG_ERR_NOTFOUND " $3 $4"; echo $?) |
---|
710 | # comprobamos consistencia de la imagen |
---|
711 | ogGetImageInfo $IMGFILE >/dev/null || return $(ogRaiseError $OG_ERR_IMAGE " $1 $2"; echo $?) |
---|
712 | |
---|
713 | # Error si la imagen no cabe en la particion. |
---|
714 | IMGSIZE=$(ogGetImageSize "$1" "$2") || return $(ogRaiseError $OG_ERR_IMAGE " $1 $2"; echo $?) |
---|
715 | #TODO: |
---|
716 | #Si la particion no esta formateado o tiene problemas formateamos |
---|
717 | ogMount $3 $4 || ogFormat $3 $4 |
---|
718 | PARTSIZE=$(ogGetPartitionSize $3 $4) |
---|
719 | if [ $IMGSIZE -gt $PARTSIZE ]; then |
---|
720 | ogRaiseError $OG_ERR_IMGSIZEPARTITION " $PARTSIZE < $IMGSIZE" |
---|
721 | return $? |
---|
722 | fi |
---|
723 | # Comprobar el bloqueo de la imagen y de la partición. |
---|
724 | if ogIsImageLocked "$IMGFILE"; then |
---|
725 | ogRaiseError $OG_ERR_LOCKED "$MSG_IMAGE $1, $2.$IMGTYPE" |
---|
726 | return $? |
---|
727 | fi |
---|
728 | if ogIsLocked $3 $4; then |
---|
729 | ogRaiseError $OG_ERR_LOCKED "$MSG_PARTITION $3, $4" |
---|
730 | return $? |
---|
731 | fi |
---|
732 | |
---|
733 | # Solicitamos la generación de la instruccion a ejecutar |
---|
734 | # Atención: no se comprueba el tipo de sistema de archivos. |
---|
735 | # Atención: no se comprueba incongruencia entre partición e imagen. |
---|
736 | PROGRAM=`ogRestoreImageSyntax $IMGFILE $PART` |
---|
737 | |
---|
738 | # Desmontar y bloquear partición. |
---|
739 | ogUnmount $3 $4 2>/dev/null || return $(ogRaiseError $OG_ERR_PARTITION " $3 $4"; echo $?) |
---|
740 | ogLock $3 $4 || return $? |
---|
741 | trap "ogUnlock $3 $4" 1 2 3 6 9 |
---|
742 | |
---|
743 | # Ejecutar restauración según el tipo de imagen. |
---|
744 | eval $PROGRAM |
---|
745 | |
---|
746 | ERRCODE=$? |
---|
747 | if [ $ERRCODE != 0 ]; then |
---|
748 | ogRaiseError $OG_ERR_IMAGE "$IMGFILE, $3, $4" |
---|
749 | fi |
---|
750 | ogUnlock $3 $4 |
---|
751 | return $ERRCODE |
---|
752 | } |
---|
753 | |
---|
754 | |
---|
755 | #/** |
---|
756 | # ogRestoreMbrImage str_repo path_image int_ndisk |
---|
757 | #@brief Restaura la imagen del sector de arranque de un disco. |
---|
758 | #@param str_repo repositorio de imágenes o caché local |
---|
759 | #@param path_image camino de la imagen |
---|
760 | #@param int_ndisk nº de orden del disco |
---|
761 | #@return (por determinar) |
---|
762 | #@exception OG_ERR_FORMAT formato incorrecto. |
---|
763 | #@exception OG_ERR_NOTFOUND fichero de imagen o partición no detectados. |
---|
764 | #@exception OG_ERR_IMAGE error al restaurar la imagen del sistema. |
---|
765 | #@version 0.9 - Primera versión en pruebas. |
---|
766 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
767 | #@date 2010/01/12 |
---|
768 | #@version 1.0 - Adaptación a OpenGnSys 1.0 |
---|
769 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
770 | #@date 2011/03/10 |
---|
771 | #*/ ## |
---|
772 | function ogRestoreMbrImage () |
---|
773 | { |
---|
774 | # Variables locales |
---|
775 | local DISK IMGFILE |
---|
776 | # Si se solicita, mostrar ayuda. |
---|
777 | if [ "$*" == "help" ]; then |
---|
778 | ogHelp "$FUNCNAME" "$FUNCNAME path_dir str_image int_ndisk" \ |
---|
779 | "$FUNCNAME REPO /aula1/mbr 1" |
---|
780 | return |
---|
781 | fi |
---|
782 | # Error si no se reciben 3 parámetros. |
---|
783 | [ $# == 3 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
784 | # Procesar parámetros. |
---|
785 | DISK=$(ogDiskToDev "$3") || return $? |
---|
786 | IMGFILE=$(ogGetPath "$1" "$2.mbr") || return $? |
---|
787 | [ -r "$IMGFILE" ] || ogRaiseError $OG_ERR_NOTFOUND "$IMGFILE" || return $? |
---|
788 | |
---|
789 | # Restaurar imagen del MBR. |
---|
790 | dd if="$IMGFILE" of="$DISK" bs=512 count=1 || ogRaiseError $OG_ERR_IMAGE "$1 $IMGFILE" || return $? |
---|
791 | } |
---|
792 | |
---|
793 | |
---|
794 | #/** |
---|
795 | # ogRestoreBootLoaderImage str_repo path_image int_ndisk |
---|
796 | #@brief Restaura la imagen del boot loader del sector de arranque de un disco. |
---|
797 | #@param str_repo repositorio de imágenes o caché local |
---|
798 | #@param path_image camino de la imagen |
---|
799 | #@param int_ndisk nº de orden del disco |
---|
800 | #@return (por determinar) |
---|
801 | #@exception OG_ERR_FORMAT formato incorrecto. |
---|
802 | #@exception OG_ERR_NOTFOUND fichero de imagen o partición no detectados. |
---|
803 | #@exception OG_ERR_IMAGE error al restaurar la imagen del sistema. |
---|
804 | #@version 1.0 - Adaptacion de ogRestoreMbrImage para restaurar solo el Boot Loader |
---|
805 | #@author Juan Carlos Xifre, SICUZ Universidad de Zaragoza |
---|
806 | #@date 2011/03/21 |
---|
807 | #*/ ## |
---|
808 | function ogRestoreBootLoaderImage () |
---|
809 | { |
---|
810 | # Variables locales |
---|
811 | local DISK IMGFILE |
---|
812 | # Si se solicita, mostrar ayuda. |
---|
813 | if [ "$*" == "help" ]; then |
---|
814 | ogHelp "$FUNCNAME" "$FUNCNAME path_dir str_image int_ndisk" \ |
---|
815 | "$FUNCNAME REPO /aula1/mbr 1" |
---|
816 | return |
---|
817 | fi |
---|
818 | # Error si no se reciben 3 parámetros. |
---|
819 | [ $# == 3 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
820 | # Procesar parámetros. |
---|
821 | DISK=$(ogDiskToDev "$3") || return $? |
---|
822 | IMGFILE=$(ogGetPath "$1" "$2.mbr") || return $? |
---|
823 | [ -r "$IMGFILE" ] || ogRaiseError $OG_ERR_NOTFOUND "$IMGFILE" || return $? |
---|
824 | |
---|
825 | # Restaurar imagen del MBR. |
---|
826 | dd if="$IMGFILE" of="$DISK" bs=446 count=1 || ogRaiseError $OG_ERR_IMAGE "$1 $IMGFILE" || return $? |
---|
827 | } |
---|
828 | |
---|
829 | #/** |
---|
830 | # ogUnlockImage [str_repo] path_image |
---|
831 | #@brief Desbloquea una imagen con uso exclusivo. |
---|
832 | #@param str_repo repositorio de imágenes (opcional) |
---|
833 | #@param path_image camino de la imagen (sin extensión) |
---|
834 | #@return Nada. |
---|
835 | #@note repo = { REPO, CACHE } |
---|
836 | #@note Se elimina el fichero de bloqueo con extensión .lock |
---|
837 | #@exception OG_ERR_FORMAT formato incorrecto. |
---|
838 | #@version 1.0 - Adaptación a OpenGnSys 1.0 |
---|
839 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
840 | #@date 2011/03/10 |
---|
841 | #*/ ## |
---|
842 | function ogUnlockImage () |
---|
843 | { |
---|
844 | # Si se solicita, mostrar ayuda. |
---|
845 | if [ "$*" == "help" ]; then |
---|
846 | ogHelp "$FUNCNAME" "$FUNCNAME [str_repo] path_image" \ |
---|
847 | "$FUNCNAME /opt/opengnsys/images/aula1/winxp.img" \ |
---|
848 | "$FUNCNAME REPO /aula1/winxp.img" |
---|
849 | return |
---|
850 | fi |
---|
851 | # Error si no se reciben 1 o 2 parámetros. |
---|
852 | [ $# == 1 -o $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
853 | |
---|
854 | # Borrar fichero de bloqueo para la imagen. |
---|
855 | rm -f $(ogGetPath $@.lock) |
---|
856 | } |
---|
857 | |
---|
858 | |
---|
859 | #/** |
---|
860 | # ogGetImageInfo filename |
---|
861 | #@brief muestra información sobre la imagen monolitica. |
---|
862 | #@param 1 filename path absoluto del fichero imagen |
---|
863 | #@return cadena compuesta por clonacion:compresor:sistemaarchivos:tamañoKB |
---|
864 | #@exception OG_ERR_FORMAT formato incorrecto. |
---|
865 | #@exception OG_ERR_NOTFOUND fichero no encontrado. |
---|
866 | #@exception OG_ERR_IMAGE "Image format is not valid $IMGFILE" |
---|
867 | #@warning En pruebas iniciales |
---|
868 | #@TODO Definir sintaxis de salida (herramienta y compresor en minuscula) |
---|
869 | #@TODO Arreglar loop para ntfsclone |
---|
870 | #@TODO insertar parametros entrada tipo OG |
---|
871 | #@version 1.0 - Primeras pruebas |
---|
872 | #@author Antonio J. Doblas Viso. Universidad de Málaga |
---|
873 | #@date 2010/02/08 |
---|
874 | #*/ ## |
---|
875 | |
---|
876 | function ogGetImageInfo () |
---|
877 | { |
---|
878 | # Si se solicita, mostrar ayuda. |
---|
879 | if [ "$*" == "help" ]; then |
---|
880 | ogHelp "$FUNCNAME" "$FUNCNAME filename " \ |
---|
881 | "$FUNCNAME /opt/opengnsys/images/prueba.img " |
---|
882 | return |
---|
883 | fi |
---|
884 | |
---|
885 | # Error si no se reciben 1 parámetros. |
---|
886 | [ $# == 1 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
887 | |
---|
888 | #comprobando que el parametro uno es un file. |
---|
889 | [ -f $1 ] || ogRaiseError $OG_ERR_NOTFOUND "$1" || return $? |
---|
890 | |
---|
891 | local TOOLS COMPRESSOR IMGFILE FILEHEAD FS FSPLUS SIZE SIZEFACTOR PARTIMAGEINFO PARTCLONEINFO NTFSCLONEINFO IMGDETECT |
---|
892 | IMGDETECT="FALSE" |
---|
893 | |
---|
894 | IMGFILE=$1 |
---|
895 | FILEHEAD=/tmp/`basename $IMGFILE`.infohead |
---|
896 | COMPRESSOR=`file $IMGFILE | awk '{print $2}'` |
---|
897 | ogCheckStringInGroup "$COMPRESSOR" "gzip lzop" || ogRaiseError $OG_ERR_IMAGE "Image format is not valid $IMGFILE" || return $? |
---|
898 | $($COMPRESSOR -dc $IMGFILE 2>/dev/null | head > $FILEHEAD) || ogRaiseError $OG_ERR_IMAGE "Image format is not valid $IMGFILE" || return $? |
---|
899 | |
---|
900 | ## buscando Primera opción. |
---|
901 | if [ "$IMGDETECT" == "FALSE" ] |
---|
902 | then |
---|
903 | PARTCLONEINFO=$(LC_ALL=C partclone.info $FILEHEAD 2>&1) |
---|
904 | if `echo $PARTCLONEINFO | grep size > /dev/null` |
---|
905 | then |
---|
906 | TOOLS=PARTCLONE |
---|
907 | FS=$(echo $PARTCLONEINFO | awk '{gsub(/\: /,"\n"); print toupper($8);}') |
---|
908 | if [[ "$FS" == "HFS" || "$FS" == "HFSPLUS" || "$FS" == "FAT32" ]]; then |
---|
909 | FSPLUS=$(echo $PARTCLONEINFO | awk '{gsub(/\: /,"\n"); print toupper($9);}') |
---|
910 | echo $PARTCLONEINFO | grep GB > /dev/null && SIZEFACTOR=1000000 || SIZEFACTOR=1024 |
---|
911 | if [ "$FSPLUS" == "PLUS" ]; then |
---|
912 | FS=$FS$FSPLUS |
---|
913 | SIZE=$(echo $PARTCLONEINFO | awk -v FACTOR=$SIZEFACTOR '{printf "%d\n", $17*FACTOR;}') |
---|
914 | else |
---|
915 | SIZE=$(echo $PARTCLONEINFO | awk -v FACTOR=$SIZEFACTOR '{printf "%d\n", $16*FACTOR;}') |
---|
916 | fi |
---|
917 | else |
---|
918 | echo $PARTCLONEINFO | grep GB > /dev/null && SIZEFACTOR=1000000 || SIZEFACTOR=1024 |
---|
919 | SIZE=$(echo $PARTCLONEINFO | awk -v FACTOR=$SIZEFACTOR '{gsub(/\: /,"\n"); printf "%d\n", $11*FACTOR;}') |
---|
920 | fi |
---|
921 | IMGDETECT="TRUE" |
---|
922 | fi |
---|
923 | fi |
---|
924 | #buscando segunda opcion. |
---|
925 | if [ "$IMGDETECT" == "FALSE" -a ! -f /dev/loop2 ] |
---|
926 | then |
---|
927 | cat $FILEHEAD | grep -w ntfsclone-image > /dev/null && NTFSCLONEINFO=$(cat $FILEHEAD | ntfsclone --restore --overwrite /dev/loop2 - 2>&1) |
---|
928 | if `echo $NTFSCLONEINFO | grep ntfsclone > /dev/null` |
---|
929 | then |
---|
930 | TOOLS=NTFSCLONE |
---|
931 | SIZE=$(echo $NTFSCLONEINFO | awk '{gsub(/\(|\)|\./,""); printf "%d\n",$17/1000;}') |
---|
932 | FS=NTFS |
---|
933 | IMGDETECT="TRUE" |
---|
934 | fi |
---|
935 | fi |
---|
936 | ## buscando Tercer opción. |
---|
937 | if [ "$IMGDETECT" == "FALSE" ] |
---|
938 | then |
---|
939 | PARTIMAGEINFO=$(partimage -B gui=no imginfo "$FILEHEAD" 2>&1) |
---|
940 | if `echo $PARTIMAGEINFO | grep Partition > /dev/null` |
---|
941 | then |
---|
942 | TOOLS=PARTIMAGE |
---|
943 | FS=$(echo $PARTIMAGEINFO | awk '{gsub(/ /,"\n"); print $17;}' | awk '{sub(/\.\.+/," "); print toupper($2)}') |
---|
944 | SIZE=$( echo $PARTIMAGEINFO | awk '{gsub(/ /,"\n"); print $36;}' | awk '{sub(/\.\.+/," "); printf "%d\n",$2*1024*1024;}') |
---|
945 | IMGDETECT="TRUE" |
---|
946 | fi |
---|
947 | if file $FILEHEAD 2> /dev/null | grep -q "boot sector"; then |
---|
948 | TOOLS="partclone.dd" |
---|
949 | FS= |
---|
950 | SIZE= |
---|
951 | IMGDETECT="TRUE" |
---|
952 | fi |
---|
953 | fi |
---|
954 | #comprobamos valores #Chequeamos los valores devueltos. |
---|
955 | if [ -z "$TOOLS" -o -z "$COMPRESSOR" -o "$IMGDETECT" == "FALSE" ] |
---|
956 | then |
---|
957 | ogRaiseError $OG_ERR_IMAGE "Image format is not valid $IMGFILE" || return $? |
---|
958 | else |
---|
959 | COMPRESSOR=$(echo $COMPRESSOR | tr [a-z] [A-Z]) |
---|
960 | echo $TOOLS:$COMPRESSOR:$FS:$SIZE |
---|
961 | fi |
---|
962 | } |
---|
963 | |
---|
964 | function ogGetImageProgram () |
---|
965 | { |
---|
966 | local IMGFILE |
---|
967 | IMGFILE=$(ogGetPath "$1" "$2.img") || return $? |
---|
968 | [ -r "$IMGFILE" ] || ogRaiseError $OG_ERR_NOTFOUND "$IMGFILE" || return $? |
---|
969 | ogGetImageInfo $IMGFILE | awk -F: '{print $1}' |
---|
970 | |
---|
971 | } |
---|
972 | |
---|
973 | function ogGetImageCompressor () |
---|
974 | { |
---|
975 | local IMGFILE |
---|
976 | IMGFILE=$(ogGetPath "$1" "$2.img") || return $? |
---|
977 | [ -r "$IMGFILE" ] || ogRaiseError $OG_ERR_NOTFOUND "$IMGFILE" || return $? |
---|
978 | ogGetImageInfo $IMGFILE | awk -F: '{print $2}' |
---|
979 | } |
---|
980 | |
---|
981 | function ogGetImageType () |
---|
982 | { |
---|
983 | local IMGFILE |
---|
984 | IMGFILE=$(ogGetPath "$1" "$2.img") || return $? |
---|
985 | [ -r "$IMGFILE" ] || ogRaiseError $OG_ERR_NOTFOUND "$IMGFILE" || return $? |
---|
986 | #partimage -B gui=no imginfo "$IMGFILE" 2>&1 | \ |
---|
987 | # awk '/^Filesystem/ {sub(/\.\.+/," "); sub(/fs$/,""); print toupper($2);}' |
---|
988 | ogGetImageInfo $IMGFILE | awk -F: '{print $3}' |
---|
989 | |
---|
990 | } |
---|
991 | |
---|
992 | function ogGetImageSize () |
---|
993 | { |
---|
994 | # Variables locales |
---|
995 | local IMGFILE |
---|
996 | |
---|
997 | # Si se solicita, mostrar ayuda. |
---|
998 | if [ "$*" == "help" ]; then |
---|
999 | ogHelp "$FUNCNAME" "$FUNCNAME REPO|CACHE /str_image" \ |
---|
1000 | "$FUNCNAME REPO /aula1/winxp ==> 5642158" |
---|
1001 | return |
---|
1002 | fi |
---|
1003 | # Error si no se reciben 2 parámetros. |
---|
1004 | [ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
1005 | # Error si el fichero de imagen no es accesible. |
---|
1006 | IMGFILE=$(ogGetPath "$1" "$2.img") || return $? |
---|
1007 | [ -r "$IMGFILE" ] || ogRaiseError $OG_ERR_NOTFOUND "$IMGFILE" || return $? |
---|
1008 | |
---|
1009 | # Devuelve el tamaño de la imagen en KB. |
---|
1010 | #partimage -B gui=no imginfo "$IMGFILE" 2>&1 | \ |
---|
1011 | # awk '/Partition size/ {sub(/\.\.+/," "); printf "%d\n",$3*1024*1024;}' |
---|
1012 | ogGetImageInfo $IMGFILE | awk -F: '{print $4}' |
---|
1013 | } |
---|
1014 | |
---|