source: client/engine/Rsync.lib @ a86c559

918-git-images-111dconfigfileconfigure-oglivegit-imageslgromero-new-oglivemainmaint-cronmount-efivarfsmultivmmultivm-ogboot-installerogClonningEngineogboot-installer-jenkinsoglive-ipv6test-python-scriptsticket-301ticket-50ticket-50-oldticket-577ticket-585ticket-611ticket-612ticket-693ticket-700ubu24tplunification2use-local-agent-oglivevarios-instalacionwebconsole3
Last change on this file since a86c559 was 8361974, checked in by irina <irinagomez@…>, 12 years ago

#565 comandos para manipular las imagenes sincronizadas desde el servidor

git-svn-id: https://opengnsys.es/svn/branches/version1.0@3817 a21b9725-9963-47de-94b9-378ad31fedc9

  • Property mode set to 100755
File size: 20.3 KB
Line 
1#!/bin/bash
2#/**
3# ogCreateFileImage  [ REPO | CACHE ] image_name extension size
4#@brief   Crear el archivo
5#@param 1 Repositorio  [ REPO | CACHE ]
6#@param 2 Nombre Imagen
7#@param 3 Extensión [ img |diff ]
8#@param 4 Tamaño de la imagen
9#@return  instrucción para ser ejecutada.
10#*/
11
12function ogCreateFileImage () {
13local SIZEREQUIRED IMGDIR IMGFILE DIRMOUNT LOOPDEVICE  IMGSIZE
14
15if [ "$*" == "help" ]; then
16    ogHelp "$FUNCNAME" "$FUNCNAME [ REPO|CACHE ] image_name extension  size(K)" \
17           "$FUNCNAME REPO Ubuntu12 img 300000" \
18           "$FUNCNAME CACHE Windows7 diff 20000000"
19    return
20fi
21
22
23if [ $# -lt 4 ]; then
24    ogRaiseError $OG_ERR_FORMAT "$MSG_FORMAT: $FUNCNAME  [ REPO|CACHE ] image_name extension  size(k)"
25    return $?
26fi
27
28SIZEREQUIRED=$4
29[ $SIZEREQUIRED -lt 300000 ] && SIZEREQUIRED=300000
30
31if [ "$1" == "CACHE" -o "$1" == "cache" ]; then
32        IMGDIR=$(ogGetParentPath "$1" "/$2")
33        IMGFILE=${IMGDIR}/$(basename "/$2").$3
34        ## Si no existe, crear subdirectorio de la imagen.
35        if [ $? != 0 ]; then
36            echo "      $MSG_HELP_ogMakeDir \"$1 $(dirname "$2")." | tee -a $OGLOGSESSION $OGLOGFILE
37            ogMakeDir "$1" $(dirname "/$2") || return $(ogRaiseError $OG_ERR_NOTWRITE "$3 /$4"; echo $?)
38            IMGDIR=$(ogGetParentPath "$1" "/$2") || return $(ogRaiseError $OG_ERR_NOTWRITE "$3 /$4"; echo $?)
39        fi
40        DIRMOUNT=/tmp/$(ogGetMountDir "$2" "$3")
41        mkdir -p "$DIRMOUNT"
42        LOOPDEVICE=$(losetup -f)
43        # Si existe el fichero de la imagen se hace copia de seguridad, si  no existe se crea.
44        if [ -f "$IMGFILE" ]; then
45                # Si la imagen esta montada la desmonto
46                if [ -r "$DIRMOUNT/ogimg.info" ]; then
47                    echolog "umount $DIRMOUNT"
48                    umount "$DIRMOUNT"
49                    [ $? -ne 0 ] && return $(ogRaiseError $OG_ERR_DONTUNMOUNT_IMAGE "$1 $2.$3"; echo $?)
50                fi
51
52                if [ "$BACKUP" == "true" ]; then
53                    # Copia seguridad
54                    echo "     $MSG_SCRIPTS_FILE_RENAME \"$IMGFILE\" ->  \"$IMGFILE.ant\"." | tee -a $OGLOGSESSION $OGLOGFILE
55                    cp -f  "$IMGFILE" "$IMGFILE.ant"  &> $OGLOGCOMMAND
56                    mv -f "$IMGFILE.torrent" "$IMGFILE.torrent.ant" 2>/dev/null
57                    rm -f "$IMGFILE.sum"
58                fi
59
60                IMGSIZE=$(ls -lk $IMGFILE | awk '{print $5}')
61                if [ $IMGSIZE -lt $SIZEREQUIRED ];then
62                   echo "      $MSG_SYNC_RESIZE" | tee -a $OGLOGSESSION $OGLOGFILE
63                   echo "      truncate --size=>$SIZEREQUIRED k $IMGFILE" | tee -a $OGLOGSESSION $OGLOGFILE
64                   truncate --size=">$SIZEREQUIRED"k $IMGFILE &> $OGLOGCOMMAND
65                   echo "      mount -o compress=lzo $IMGFILE $DIRMOUNT"
66                   mount -o compress=lzo "$IMGFILE" "$DIRMOUNT" &> $OGLOGCOMMAND
67                   echo "      btrfs filesystem resize max $DIRMOUNT"
68                   btrfs filesystem resize max "$DIRMOUNT" &> $OGLOGCOMMAND
69                fi
70        else
71                echo "      dd if=/dev/zero of=$IMGFILE bs=1024 count=$SIZEREQUIRED"
72                dd if=/dev/zero of="$IMGFILE" bs=1024 count=$SIZEREQUIRED &> $OGLOGCOMMAND
73                #Formateamos imagen
74                losetup $LOOPDEVICE $IMGFILE
75                echo "      mkfs.btrfs  -L ${2##*\/} $LOOPDEVICE "
76                mkfs.btrfs  -L "${2##*\/}" $LOOPDEVICE &> $OGLOGCOMMAND
77        fi
78                echo "      mount -o compress=lzo $IMGFILE $DIRMOUNT"
79                mount -o compress=lzo "$IMGFILE" "$DIRMOUNT" &> $OGLOGCOMMAND
80                # si ya esta montado  $? = 32
81                [ $? -eq 0 -o $? -eq 32 ] || ogRaiseError $OG_ERR_IMAGE "$3 $4"
82                touch "$DIRMOUNT/ogimg.info"
83
84        #fi
85        # Si existe dispositivo de loop lo borro.
86        [ $LOOPDEVICE ] && losetup -d $LOOPDEVICE 2>&1 &>/dev/null
87
88else
89        [ -z $REPOIP ] && REPOIP=$(ogGetRepoIp)
90        echo "      hose $REPOIP 2009 --out sh -c \"echo -ne CREATE_IMAGE $2 $3 $SIZEREQUIRED \"" | tee -a $OGLOGSESSION $OGLOGFILE
91        hose $REPOIP 2009 --out sh -c "echo -ne CREATE_IMAGE \"$2\" $3 $SIZEREQUIRED"
92fi
93
94}
95
96#/**
97# ogSetSizeInfo [REPO|CACHE] nombre_imagen extension tamaño
98#@brief Configuramos la informacion del tamaño de los datos, para diferenciales
99#@param 1 Repositorio  [ REPO | CACHE ] (opcional en las completas)
100#@param 2 Nombre Imagen Basica (opcional en las completas)
101#@param 3 Extensión [ img | diff ]
102#@param 4 Tamaño de los datos.
103function ogSetSizeInfo () {
104local DIRMOUNT UMOUNT
105
106if [ "$*" == "help" ]; then
107        ogHelp "$FUNCNAME" "$FUNCNAME [ REPO|CACHE ] image_name  extension size_data  " \
108               "base image -> $FUNCNAME REPO Ubuntu12 img 34000000" \
109               "diff image -> $FUNCNAME CACHE Windows7 diff 500000"
110        return
111fi
112
113if [ $# -lt 4 ]; then
114    ogRaiseError $OG_ERR_FORMAT "$FUNCNAME  [ REPO|CACHE ] image_name  extension size_data "
115    return $?
116fi
117
118DIRMOUNT=$(ogGetMountDir  "$2" $3)
119[ "$1" == "CACHE" -o "$1" == "cache" ] && DIRMOUNT="/tmp/$DIRMOUNT" || DIRMOUNT="$OGIMG/$DIRMOUNT"
120
121# Si no esta montada la imagen se monta.
122[ -r $DIRMOUNT/ogimg.info ] && UMOUNT=false || ogMountImage $1 "$2" $3
123# se configura el tamaño
124echo sed -i s/SIZEDATA/"$4"/g $DIRMOUNT/ogimg.info
125sed -i s/SIZEDATA/"$4"/g $DIRMOUNT/ogimg.info
126# se desmonta, si no estaba montada.
127[ "$UMOUNT" == false ] || ogUnmountImage $1 "$2" $3
128
129
130}
131
132function ogCreateInfoImage () {
133#/**
134#  ogCreateInfoImage
135#@brief   Crear listados con la informacion de la imagen, los situa en /tmp.
136#@param 1 num_disk
137#@param 2 num_part
138#@param 3 Repositorio  [ REPO | CACHE ] (opcional en las completas)
139#@param 4 Nombre Imagen Basica (opcional en las completas)
140#@param 5 Extensión [ img | diff ]
141#*/
142local  IMGEXT IMGDIRAUX DIRMOUNT DESTRSYNC OPTRSYNC USERRSYNC ORIG FSTYPE PART DIREMPTY IMGLIST IMGINFO IMGACL
143
144# Ayuda o menos de 5 parametros y la imagen no es basica
145if [ "$*" == "help" -o   $# -lt 5 -a "$3" != "img"  ]; then
146        ogHelp "$FUNCNAME" "$FUNCNAME num_disk num_part [ REPO|CACHE ] [ base_image_name ] extension  " \
147               "base image -> $FUNCNAME 1 2 img" \
148               "diff image -> $FUNCNAME 1 1 CACHE Windows7 diff "
149        return
150fi
151
152if [ $# -lt 3 ]; then
153    ogRaiseError $OG_ERR_FORMAT "$MSG_FORMAT: $FUNCNAME  num_disk num_part [ REPO|CACHE ] [ base_image_name]  extension  "
154    return $?
155fi
156
157if [ $3 == "img" ]; then
158   IMGEXT="img"
159else
160   # Comprobamos que las extension sea valida
161   ogCheckStringInGroup $5 "img diff" || ogRaiseError  $OG_ERR_FORMAT $MSG_SYNC_EXTENSION
162   IMGEXT=$5
163   if [ "$IMGEXT" == "diff" ]; then
164        # Imagen completa con la que comparo la particion.
165        IMGDIRAUX=$(ogGetMountDir "$4" "img")
166        if [ "$3" == "CACHE" -o "$3" == "cache" ]; then
167           DIRMOUNT="/tmp/$IMGDIRAUX"
168           DESTRSYNC=$DIRMOUNT
169           OPTRSYNC=""
170        else
171           [ -z $REPOIP ] && REPOIP=$(ogGetRepoIp)
172           DIRMOUNT="$OGIMG/$IMGDIRAUX"
173           USERRSYNC="opengnsys"
174           OPTRSYNC="$OPTRSYNC --password-file=/scripts/passrsync"
175           DESTRSYNC="$USERRSYNC@$REPOIP::ogimages/$IMGDIRAUX"
176        fi
177   fi
178fi
179
180
181ORIG=$(ogMount $1 $2)
182FSTYPE=$(ogGetFsType $1 $2)
183PART=$(ogDiskToDev "$1" "$2" 2>/dev/null)
184
185# Creamos la lista del contenido y lo situamos en la particion a copiar.
186DIREMPTY="/tmp/empty$$"
187IMGLIST="/tmp/ogimg.list"
188IMGINFO="/tmp/ogimg.info"
189IMGACL="/tmp/ogimg.acl"
190
191# Borramos archivos antiguos.
192rm /tmp/ogimg.* 2>/dev/null
193rm $ORIG/ogimg.* 2>/dev/null
194
195# En las diferenciales no sabemos el tamaño -> ponemos una constante.
196SIZEDATA=${SIZEDATA:-"SIZEDATA"}
197
198echo "#BRTFS:LZO:$FSTYPE:$SIZEDATA" > $IMGINFO
199if [ "$IMGEXT" == "img" ]; then
200        # Imagen Basica
201        echo "      rsync -aHAXvn --delete $ORIG/ $DIREMPTY >> $IMGINFO" | tee -a $OGLOGSESSION $OGLOGFILE
202        rsync -aHAXvn --delete $ORIG/ $DIREMPTY>> $IMGINFO
203        sed -i -e s/"^sent.*.bytes\/sec"//g -e s/^total.*.speedup.*.$//g -e s/"sending.*.list"//g $IMGINFO
204        sed -i  '/^\.\//d' $IMGINFO
205
206else
207        # Imagen Diferencial
208        # TODO en el echo quitar la $OPTRSYNC  para que no se vea el fichero de claves
209        echo "      rsync -aHAXvn$OPTRSYNC  --delete  $ORIG/ $DESTRSYNC a $IMGLIST" | tee -a $OGLOGSESSION $OGLOGFILE
210        rsync -aHAXvn$OPTRSYNC   --delete  "$ORIG/" "$DESTRSYNC" >> $IMGLIST
211        sed -i -e s/"^sent.*.bytes\/sec"//g -e s/^total.*.speedup.*.$//g -e s/"sending.*.list"//g $IMGLIST
212        sed -i  '/^\.\//d' $IMGLIST
213
214        # Creamos informacion de la imagen
215        grep -e '\->' -e  '\=>' $IMGLIST > /tmp/ogimg.ln
216        grep -e  ^deleting  $IMGLIST | sed s/^deleting\ //g | grep -v ^ogimg  > /tmp/ogimg.rm
217        #grep -v -e '\->' -e  '\=>'  -e ^deleting  $IMGLIST >> $IMGINFO
218        grep -v -e '\->' -e  '\=>'  -e ^deleting -e ^created  $IMGLIST >> $IMGINFO
219
220        rm $IMGLIST
221
222fi
223
224# Guardamos el contenido de las acl (Solo win) Necesario particion desmontada (esta asi)
225ogUnmount $1 $2
226if [ $FSTYPE == "NTFS" ]; then
227        echo "     ntfs-3g.secaudit -b $PART /" |tee -a $OGLOGSESSION $OGLOGFILE
228        ntfs-3g.secaudit -b $PART / > $IMGACL
229fi
230
231}
232
233#/**
234#  ogRestoreInfoImage
235#@brief Crear o modificar enlaces y restaurar las ACL.
236#@param 1 num_disk
237#@param 2 num_part
238#*/
239function ogRestoreInfoImage () {
240local  DEST PART IMGACL IMGLN OPTLN LINEA DESTLN ORIGLN TYPELN
241
242# Ayuda o menos de 5 parametros y la imagen no es basica
243if [ "$*" == "help" ]; then
244        ogHelp "$FUNCNAME" "$FUNCNAME num_disk num_part" \
245               "base image -> $FUNCNAME 1 2 " \
246               "diff image -> $FUNCNAME 1 1 "
247        return
248fi
249
250if [ $# -lt 2 ]; then
251    ogRaiseError $OG_ERR_FORMAT "$MSG_FORMAT: $FUNCNAME num_disk num_part  "
252    return $?
253fi
254
255DEST=$(ogMount $1 $2)
256PART=$(ogDiskToDev "$1" "$2" 2>/dev/null)
257IMGACL="ogimg.acl"
258IMGLN="ogimg.ln"
259
260#Creamos o modificamos los enlaces.
261# La imagen diferencial tiene ogimg.ln
262# para la completa lo generamos con los enlaces que contengan  /mnt/
263[ -r $DEST/$IMGLN ] || grep -e "->" -e "=>" $DEST/ogimg.info|grep "/mnt/" > $DEST/$IMGLN
264if [ $(wc -l $DEST/$IMGLN|cut -f1 -d" ") -ne 0 ]; then
265   while read LINEA
266   do
267        ORIGLN="${LINEA#*> }"
268        # Si origen hace referencia a la particion lo modificamos
269        echo $ORIGLN|grep "/mnt/"> /dev/null && ORIGLN="$DEST/${ORIGLN#/mnt/*/}"
270        # rsync marca - los enlaces simbolicos y = enlaces "duros"
271        LINEA="${LINEA%>*}"
272        TYPELN="${LINEA##* }"
273        DESTLN="${LINEA% *}"
274
275        if [ "$TYPELN" == "-" ]
276        then
277           OPTLN='-s'
278        else
279           OPTLN=''
280        fi
281        cd "$DEST/$(dirname $DESTLN)"
282        rm "$(basename $DESTLN)"
283        ln $OPTLN "$ORIGLN" "$(basename $DESTLN)"
284        echo -n "."
285   done < $DEST/$IMGLN  2>/dev/null
286   echo ""
287
288fi
289
290# Restauramos acl
291if [ "$(ogGetFsType $1 $2)" == "NTFS" ] ; then
292        cd /
293        cp $DEST/$IMGACL /tmp
294        ogUnmount "$1" "$2"
295        echo "      ntfs-3g.secaudit -se $PART" |tee -a $OGLOGSESSION $OGLOGFILE
296        ntfs-3g.secaudit -se $PART /tmp/$IMGACL
297fi
298
299}
300
301function ogSyncCreate () {
302#/**
303# ogSyncCreate
304#@brief   sincroniza los datos de la partición a la imagen para crearla.
305#@param 1 num_disk
306#@param 2 num_part
307#@param 3 Repositorio  [ REPO | CACHE ]
308#@param 4 Nombre Imagen
309#@param 5 Extensión [ img | diff ]
310#*/
311local ORIG DIRAUX DIRMOUNT DESTRSYNC USERRSYNC OPTRSYNC
312
313if [ "$*" == "help" ]; then
314        ogHelp "$FUNCNAME" "$FUNCNAME num_disk num_part [ REPO|CACHE ] image_name extension  " \
315               "$FUNCNAME 1 2 REPO Ubuntu12 img" \
316               "$FUNCNAME 1 1 CACHE Windows7 diff "
317        return
318fi
319
320
321if [ $# -lt 4 ]; then
322    ogRaiseError $OG_ERR_FORMAT "$MSG_FORMAT: $FUNCNAME  num_disk num_part [ REPO|CACHE ] image_name extension  "
323    return $?
324fi
325
326ORIG=$(ogMount $1 $2)
327DIRMOUNT=$(ogGetMountDir "$4" $5)
328[ "$5" == "diff" ] && FILESFROM=" --files-from=/tmp/ogimg.info" || FILESFROM=""
329
330if [ "$3" == "CACHE" -o "$3" == "cache" ]; then
331        DESTRSYNC="/tmp/$DIRMOUNT"
332else
333        [ -z $REPOIP ] && REPOIP=$(ogGetRepoIp)
334        OPTRSYNC=" --password-file=/scripts/passrsync"
335        [ "$ogcompress" ==  "true" ] && OPTRSYNC="z $OPTRSYNC"
336        USERRSYNC="opengnsys"
337        DESTRSYNC="$USERRSYNC@$REPOIP::ogimages/$DIRMOUNT"
338fi
339# Sincronizamos los datos de la partición a la imagen
340echo "      rsync -aHAX$OPTRSYNC --inplace --delete $FILESFROM  $ORIG/ $DESTRSYNC" | tee -a $OGLOGSESSION $OGLOGFILE
341rsync -aHAXq$OPTRSYNC --inplace --delete $FILESFROM  "$ORIG/" "$DESTRSYNC"
342echo "      rsync -aHAX$OPTRSYNC --inplace /tmp/ogimg* $DESTRSYNC" | tee -a $OGLOGSESSION $OGLOGFILE
343rsync -aHAXq$OPTRSYNC --inplace /tmp/ogimg* $DESTRSYNC
344
345}
346
347
348#/**
349# ogSyncRestore
350#@brief   sincroniza los datos de la imagen a la partición para restaurarla.
351#@param 1 Repositorio  [ REPO | CACHE ]
352#@param 2 Nombre Imagen
353#@param 3 Extensión [ img | diff ]
354#@param 4 num_disk
355#@param 5 num_part
356#*/
357function ogSyncRestore () {
358local DIRMOUNT ORIG DESTRSYNC OPTRSYNC USERRSYNC IMGINFO FILESFROM
359
360if [ "$*" == "help" ]; then
361        ogHelp "$FUNCNAME" "$FUNCNAME [ REPO|CACHE ] image_name extension  num_disk num_part  " \
362               "$FUNCNAME REPO Ubuntu12 img 1 2" \
363               "$FUNCNAME CACHE Windows7 diff 1 1"
364        return
365fi
366
367
368if [ $# -lt 5 ]; then
369    ogRaiseError $OG_ERR_FORMAT "$MSG_FORMAT: $FUNCNAME  [ REPO|CACHE ] image_name extension   num_disk num_part "
370    return $?
371fi
372
373
374DIRMOUNT=$(ogGetMountDir "$2" "$3")
375DESTRSYNC=$(ogGetMountPoint $4 $5)
376
377# Borramos ficheros de informacion de restauraciones antiguas
378rm -rf $DESTRSYNC/ogimg.*
379
380# Origen y destino de la sincronizacion y en REPO opciones rsync
381if [ "$1" == "CACHE" -o "$1" == "cache" ]; then
382        ORIG="/tmp/$DIRMOUNT"
383else
384        [ -z $REPOIP ] && REPOIP=$(ogGetRepoIp)
385        OPTRSYNC=" --password-file=/scripts/passrsync"
386        [ "$ogcompress" ==  "true" ] && OPTRSYNC="z $OPTRSYNC"
387        USERRSYNC="opengnsys"
388        ORIG="$USERRSYNC@$REPOIP::ogimages/$DIRMOUNT"
389fi
390
391# Opciones rsync en cache y repo
392[ "$3" == "img" ] && [ "$ogrsyncdel" != "false" ] && OPTRSYNC="$OPTRSYNC --delete"
393
394# Nos traemos listado ficheros y bajamos la imagen
395
396echo "      $MSG_SYNC_RESTORE" |tee -a  $OGLOGSESSION   $OGLOGFILE
397
398# Si la imagen es diferencial nos traemos los archivos de informacion de la imagen.
399if [ "$3" == "diff" ]; then
400        # Lista de archivos a copiar:
401        IMGINFO="ogimg.info"
402        FILESFROM=" --files-from=$DESTRSYNC/$IMGINFO"
403
404        echo "      rsync -aHAXq$OPTRSYNC  $ORIG/ogimg* $DESTRSYNC" |tee -a  $OGLOGFILE
405        rsync -aHAXq$OPTRSYNC  $ORIG/ogimg* $DESTRSYNC
406        # Borramos linea de información de la imagen, sino busca un fichero con ese nombre
407        sed -i   '/^\#/d' $DESTRSYNC/$IMGINFO
408
409        cd $DESTRSYNC
410        # Diferencial: Borramos archivos sobrantes.
411        echo "      $MSG_SYNC_DELETE"  |tee -a  $OGLOGFILE
412        cat $DESTRSYNC/ogimg.rm 2>/dev/null | xargs rm -rf
413
414fi
415
416echo "      rsync -aHAX$OPTRSYNC  $FILESFROM  $ORIG/ $DESTRSYNC" | tee -a $OGLOGSESSION $OGLOGFILE
417rsync -aHAXq$OPTRSYNC  $FILESFROM  "$ORIG/" "$DESTRSYNC"
418
419
420}
421
422function ogMountImage () {
423#/**
424# ogMountImage
425#@brief   Monta la imagen para sincronizar.
426#@param 1 Repositorio  [ REPO | CACHE ]
427#@param 2 Nombre Imagen
428#@param 3 Extensión [ img |diff ]
429#@return punto de montaje
430#*/
431local IMGEXT IMGFILE DIRMOUNT
432
433if [ "$*" == "help" ]; then
434    ogHelp "$FUNCNAME" "$FUNCNAME [ REPO|CACHE ] image_name [ extension ]" \
435           "$FUNCNAME REPO Ubuntu12" \
436           "$FUNCNAME CACHE Windows7 diff"
437    return
438fi
439
440
441if [ $# -lt 2 ]; then
442    ogRaiseError $OG_ERR_FORMAT "$MSG_FORMAT: $FUNCNAME [ REPO|CACHE ]  image_name [ extension ]"
443    return $?
444fi
445
446[ "$3" == "" ] && IMGEXT="img" || IMGEXT="$3"
447
448DIRMOUNT=$(ogGetMountDir "$2" $IMGEXT)
449
450if [ "$1" == "CACHE" -o "$1" == "cache" ]; then
451        IMGFILE=$(ogGetPath "$1" /"$2.$IMGEXT")
452        mkdir -p "/tmp/$DIRMOUNT"
453        mount -o compress=lzo  "$IMGFILE" "/tmp/$DIRMOUNT" 1>/dev/null
454        echo "/tmp/$DIRMOUNT"
455
456else
457        [ -z $REPOIP ] && REPOIP=$(ogGetRepoIp)
458        hose $REPOIP 2009 --out sh -c "echo -ne MOUNT_IMAGE \"$2\" $IMGEXT"
459        echo "$OGIMG/$DIRMOUNT"
460
461fi
462
463}
464
465
466
467
468
469function ogUnmountImage () {
470#/**
471# ogUnmountImage  [ REPO | CACHE ] Image_name [ extension ]
472#@brief   Desmonta la imagen para sincronizar.
473#@param 1 Repositorio  [ REPO | CACHE ]
474#@param 2 Nombre Imagen
475#@param 3 Extensión [ img |diff ]
476#*/
477local IMGEXT DIRMOUNT
478
479if [ "$*" == "help" ]; then
480    ogHelp "$FUNCNAME" "$FUNCNAME [ REPO|CACHE ] image_name [ extension ]" \
481           "$FUNCNAME REPO Ubuntu12" \
482           "$FUNCNAME CACHE Windows7 diff"
483    return
484fi
485
486if [ $# -lt 2 ]; then
487    ogRaiseError $OG_ERR_FORMAT "$MSG_FORMAT: $FUNCNAME  [ REPO|CACHE ]  image_name [ extension ]"
488    return $?
489fi
490
491[ "$3" == "" ] && IMGEXT="img" || IMGEXT="$3"
492
493if [ "$1" == "CACHE" -o "$1" == "cache" ]; then
494        DIRMOUNT=/tmp/$(ogGetMountDir "$2" $IMGEXT)
495        umount "$DIRMOUNT"
496        rmdir "$DIRMOUNT"
497else
498        [ -z $REPOIP ] && REPOIP=$(ogGetRepoIp)
499        echo "      hose $REPOIP 2009 --out sh -c echo -ne UMOUNT_IMAGE \"$2\" $IMGEXT" |tee -a  $OGLOGFILE
500        hose $REPOIP 2009 --out sh -c "echo -ne UMOUNT_IMAGE \"$2\" $IMGEXT"
501fi
502
503}
504
505function ogGetMountDir () {
506#/**
507# ogGetMountDir
508#@brief   Devuelve el directorio de montaje de la imagen.
509#@param 1 Nombre Imagen
510#@param 2 Extensión [ img |diff ]
511#*/
512local DIRMOUNT
513if [ "$*" == "help" ]; then
514    ogHelp "$FUNCNAME" "$FUNCNAME image_name [ extension ]" \
515           "$FUNCNAME Ubuntu12" \
516           "$FUNCNAME Windows7 diff"
517    return
518fi
519
520
521if [ $# -lt 1 ]; then
522    ogRaiseError $OG_ERR_FORMAT "$MSG_FORMAT: $FUNCNAME image_name [ extension ]"
523    return $?
524fi
525
526
527DIRMOUNT="mount/$1"
528[ "$2" == "diff" ] && DIRMOUNT="$DIRMOUNT.diff"
529echo $DIRMOUNT
530
531
532}
533
534
535#/**
536#  ogWaitMountImage image_name extension imagen_size
537#@brief Se espera un tiempo a que se monte la imagen en el servidor.
538#@brief Cuando se esta creando la imagen hay que dar el tamaño, para que espere el tiempo de creación. 
539#@param 1 Respositorio [ REPO | CACHE ]
540#@param 2 Nombre Imagen
541#@param 3 Extensión [ img | diff ]
542#@param 4 Tamaño imagen (opcional)
543#*/
544function ogWaitMountImage () {
545local SIZE TIME DIRMOUNT TIMEOUT TIMEAUX
546
547TIME=$SECONDS
548
549# Ayuda o menos de 5 parametros y la imagen no es basica
550if [ "$*" == "help" ]; then
551        ogHelp "$FUNCNAME" "$FUNCNAME [ REPO | CACHE ] image_name extension [ image_size ] " \
552               "$FUNCNAME REPO Ubuntu12 img 30000000" \
553               "$FUNCNAME CACHE Windows7 diff "
554        return
555fi
556
557if [ $# -lt 2 ]; then
558    ogRaiseError $OG_ERR_FORMAT "$MSG_FORMAT: $FUNCNAME [ REPO | CACHE ]  image_name extension [ image_size ] "
559    return $?
560fi
561
562
563SIZE=${4:-"300000"}
564
565if [ "$1" == "CACHE" -o "$1" == "cache" ]; then
566        DIRMOUNT="/tmp/$(ogGetMountDir "$2" $3)"
567else
568        DIRMOUNT="$OGIMG/$(ogGetMountDir "$2" $3)"
569fi
570
571echo -n -e "      $MSG_SYNC_SLEEP: $DIRMOUNT\n      #"
572
573# time-out segun el tamaño de la imagen. por defecto: 100000k -> 3s
574let TIMEOUT=$SIZE/$CREATESPEED
575[ $TIMEOUT -lt 60 ] && TIMEOUT=60
576until [ -f "$DIRMOUNT/ogimg.info" ] ; do
577        TIMEAUX=$[SECONDS-TIME]
578        #[  "$TIMEAUX" -gt "$TIMEOUT" ] && return $(ogRaiseError "$MSG_ERR_DONTMOUNT_IMAGE $1 $2 $3: time_out $TIMEAUX seg."; echo $?)
579        # TODO definir el error
580        [  "$TIMEAUX" -gt "$TIMEOUT" ] &&  return 2
581        echo -n "#"
582        sleep 5
583done
584echo  ""
585
586}
587
588function ogReduceImage () {
589#/**
590# ogReduceImage
591#@brief   Reduce el archivo de la imagen a tamaño datos + 500M
592#@param 1 Repositorio  [ REPO | CACHE ]
593#@param 2 Nombre Imagen
594#@param 3 Extensión [ img |diff ]
595#@return
596#@exception OG_ERR_FORMAT     # 1 formato incorrecto.
597#@exception OG_ERR_NOTFOUND        # 2 Fichero o dispositivo no encontrado.
598#*/
599local IMGEXT DIRMOUNT AVAILABLE USED IMGDIR IMGFILE ENDSIZE
600if [ "$*" == "help" ]; then
601    ogHelp "$FUNCNAME" "$FUNCNAME [ REPO|CACHE ] image_name [ extension ]" \
602           "$FUNCNAME REPO Ubuntu12" \
603           "$FUNCNAME CACHE Windows7 diff"
604    return
605fi
606
607if [ $# -lt 2 ]; then
608    ogRaiseError $OG_ERR_FORMAT "$MSG_FORMAT: $FUNCNAME [ REPO|CACHE ] image_name [ extension ]"
609    return $?
610fi
611
612[ "$3" == "" ] && IMGEXT="img" || IMGEXT="$3"
613
614if [ "$1" == "CACHE" -o "$1" == "cache" ]; then
615        # Al montar se comprueba la existencia de la imagen
616        DIRMOUNT=$(ogMountImage $1 $2 $IMGEXT)
617
618        AVAILABLE=$(df -k|grep $DIRMOUNT|awk '{print $4}')
619        # Si el espacio libre menor que 500Mb nos salimos
620        [ $AVAILABLE -lt 500000 ] && return 0
621
622        # Reducimos el archivo de imagen:
623        IMGDIR=$(ogGetParentPath "$1" "/$2")
624        IMGFILE=${IMGDIR}/$(basename "/$2").$IMGEXT
625        USED=$(df -k|grep $DIRMOUNT|awk '{print $3}')
626        let ENDSIZE=USED+500000
627        echo -e -n "      btrfs filesystem resize "$ENDSIZE"k $DIRMOUNT \n      "
628        btrfs filesystem resize "$ENDSIZE"k $DIRMOUNT
629        ogUnmountImage $1 $2 $IMGEXT
630        echo "      truncate --size=\"$ENDSIZE\"k $IMGFILE  "
631        truncate --size="$ENDSIZE"k $IMGFILE
632else
633        echo "      hose $REPOIP 2009 --out sh -c echo -ne REDUCE_IMAGE \"$2\" $IMGEXT"
634        hose $REPOIP 2009 --out sh -c "echo -ne REDUCE_IMAGE \"$2\" $IMGEXT"
635fi
636
637
638}
639
640
641
642
643
644
645
646
Note: See TracBrowser for help on using the repository browser.