source: client/shared/scripts/createDiffImage @ 97f6020

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 97f6020 was c8bbcdc, checked in by irina <irinagomez@…>, 11 years ago

#565 se resuelve problema de corromper la imagen sincronizada al reducirla

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

  • Property mode set to 100755
File size: 8.9 KB
Line 
1#!/bin/bash
2
3#/**
4#         createDiffImage
5#@brief   Script de ejemplo para crear una imagen diferencial de un sistema de archivos.
6#@brief (puede usarse como base para el programa de creación de imágenes usado por OpenGnSys Admin).
7#@param 1 disco
8#@param 2 particion
9#@param 3 REPO|CACHE
10#@param 4 imagen_completa
11#@param 5 imagen_diferencial
12#@return 
13#@exception OG_ERR_FORMAT     # 1 formato incorrecto.
14#@exception OG_ERR_PARTITION  # 3 Error en partición de disco o en su sistema de archivos
15#@exception OG_ERR_NOTFOUND   # Fichero de registro no encontrado.
16#@exception OG_ERR_IMAGE      # 5 Error en funcion ogCreateImage o ogRestoreImage.
17#@exception OG_ERR_NOTWRITE   # 14 error de escritura
18#@exception OG_ERR_NOTCACHE   # 15 si cache no existe 15
19#@exception OG_ERR_CACHESIZE  # 16 si espacio de la cache local o remota no tiene espacio 16
20#@exception OG_ERR_LOCKED          # 4 Partición o fichero bloqueado.
21#@exception OG_ERR_DONTMOUNT_IMAGE # 70 Error al montar una imagen sincronizada.
22#@exception OG_ERR_DONTSYNC_IMAGE  # 71 Imagen no sincronizable (es monolitica)
23#@note  Se toma como punto de partida el script createImage.
24#@todo: que hacer, si el tamaño de la cache es sufciente, pero no tiene espacio libre
25#@version 1.0 - creacion de la diferencial con rsync y btrfs
26#@author 
27#@date   2012-12-04
28#*/ ##
29trap "onexit $1 $2 $3 \"$4\" \"$5\"" 1 2 3 6 9 14 15 EXIT
30
31function onexit() {
32    local exit_status=$?
33    ogUnmountImage $3 "$5" $DIFFTYPE &>/dev/null
34    ogUnmountImage $3 "$4" $IMGEXT &>/dev/null
35    if [ $exit_status -ne 4 ]; then
36        ogUnlockImage "$3" "/$4.$IMGEXT" &>/dev/null
37        ogUnlockImage "$3" "/$5.$DIFFEXT" &>/dev/null
38        ogUnlock $1 $2 &>/dev/null
39    fi
40    exit $exit_status
41}
42
43#Carga el configurador del engine desde el fichero engine.cfg
44[ -z $OGENGINECONFIGURATE ] &&  source /opt/opengnsys/etc/engine.cfg
45
46# Factor de calculo de Time out al crear imagen: valor por defecto en engine.cfg
47CREATESPEED=${CREATESPEED:-"100000*4"}
48# Sistema de fichero de la imagen según kernel, menor que 3.7 EXT4. comparamos revision
49[  $(uname -r|cut -d. -f2) -lt 7 ] &&  IMGFS="EXT4" || IMGFS="BTRFS"
50# % de compresion para estimacion tamaño imagen IMGFS="EXT4"
51ZSYNC=${ZSYNC:-"120"}
52# Si IMGFS="BTRFS" la compresion es mayor.
53[ $IMGFS == "BTRFS" ] && let ZSYNC=$ZSYNC-30
54
55TIME1=$SECONDS
56
57PROG="$(basename $0)"
58# Si se solicita, mostrar ayuda.
59if [ "$*" == "help" ]; then
60    ogHelp  "$PROG: $MSG_HELP_createDiffImage" \
61            "$PROG ndisco nparticion REPO|CACHE base_image diff_image" \
62            "$PROG 1 1 REPO Windows7 Win7aula23"
63   exit 0
64fi
65
66[ $# -lt 5 ] && exit $(ogRaiseError session $OG_ERR_FORMAT "$MSG_FORMAT: $PROG ndisco nparticion REPO|CACHE base_image diff_image"; echo $?)
67
68# Valores por defecto en etc/engine.cfg
69IMGEXT="img"
70DIFFEXT="img.diff"
71DIFFTYPE="diff"
72REPOIP=$(ogGetRepoIp)
73
74# Limpiamos los archivo de log
75echo -n "" >$OGLOGCOMMAND
76[ "$(ogGetCaller)" == "RestaurarSoftIncremental" ] ||  echo -n "" > $OGLOGSESSION
77
78ogEcho log session  "[1] $MSG_SCRIPTS_START $0 $*"
79
80# Comprobamos si la imagen diferencial o la particion estan bloqueada:
81ogIsImageLocked "$3" "$5.$DIFFEXT" && exit $(ogRaiseError session $OG_ERR_LOCKED "$3 $5.$DIFFEXT"; echo $?)
82ogIsLocked "$1" "$2" && exit $(ogRaiseError session $OG_ERR_LOCKED  "$1 $2"; echo $?)
83
84# Comprobar si la imagen completa existe.
85IMGFILE="$(ogGetPath "$3" "$4.$IMGEXT")" || exit $(ogRaiseError session $OG_ERR_NOTFOUND "$3 $4.$IMGEXT"; echo $?)
86
87# Comprobar que la imagen completa es sincronizable
88ogIsSyncImage "$3" "$4" "img" || exit $(ogRaiseError session $OG_ERR_DONTSYNC_IMAGE "$3 $4"; echo $?)
89# Comprobar que no está bloqueada
90ogIsImageLocked "$3" "$4.$IMGEXT" && exit $(ogRaiseError session $OG_ERR_LOCKED "$3 $4.$IMGEXT"; echo $?)
91
92# Si el repositorio es CACHE comprobamos que exista
93if [ "$3" == "CACHE" -o "$3" == "cache" ]; then
94        ! ogFindCache >/dev/null && exit $(ogRaiseError session $OG_ERR_NOTCACHE "CACHE "; echo $?)
95fi
96
97echo " " > $OGLOGCOMMAND
98
99# Obtener información de los parámetros de entrada.
100PART=$(ogDiskToDev "$1" "$2" 2>/dev/null) || exit $(ogRaiseError session $OG_ERR_PARTITION "$1 $2"; echo $?)
101#Comprobamos que la partición se puede montar.
102ORIG=$(ogMount $1 $2) ||   exit $(ogRaiseError session $OG_ERR_PARTITION "$1 $2"; echo $?)
103
104DIFFDIR="$(ogGetParentPath "$3" "/$5")"
105DIFFFILE="$DIFFDIR/$5.$DIFFEXT"
106
107ogLockImage "$3"  "/$4.$IMGEXT"
108ogLockImage "$3"  "/$5.$DIFFEXT"
109
110# Comprobar consistencia del sistema de archivos.
111echo " " > $OGLOGCOMMAND
112SIZEFS=$(ogGetFsSize  $1 $2)
113ogEcho log session "[20] $MSG_HELP_ogCheckFs  $1 $2 $SIZEFS (KB) "
114ogUnmount $1 $2
115ogCheckFs $1 $2 >$OGLOGCOMMAND || exit $(ogRaiseError session $OG_ERR_PARTITION "ogCheckFs $1 $2" ; echo $?)
116
117# Borramos ficheros de paginacion y configuracion
118FSTYPE=$(ogGetFsType $1 $2)
119case "$FSTYPE" in
120    EXT[234])
121        ogCleanLinuxDevices $1 $2
122        rm -rf $ORIG/tmp/*
123        ;;
124    NTFS)
125        [ $(ogGetPath $1 $2 pagefile.sys) ] && ogDeleteFile $1 $2 pagefile.sys
126        [ $(ogGetPath $1 $2 hiberfil.sys) ] && ogDeleteFile $1 $2 hiberfil.sys
127        [ $(ogGetPath $1 $2 swapfile.sys) ] && ogDeleteFile $1 $2 swapfile.sys
128        ;;
129esac
130
131# Crear la imagen.
132echo " " > $OGLOGCOMMAND
133TIME2=$SECONDS
134ogEcho log session "[40] $MSG_HELP_createDiffImage: $1 $2 $3 $4"
135# Creamos la lista del contenido y lo situamos en la particion a copiar.
136
137# Montamos imagen completa (con la que vamos a comparar)
138ogMountImage $3 "$4" $IMGEXT >/dev/null
139
140# Comprobar que la imagen completa se ha montado
141ogWaitSyncImage $3 "$4" $IMGEXT "mounted" || exit $(ogRaiseError session $OG_ERR_DONTMOUNT_IMAGE "$3 $4 $IMGEXT: time_out."; echo $?)
142
143# Creamos la informacion de la imagen.
144ogEcho log session "[50] $MSG_HELP_ogCreateInfoImage "
145ogCreateInfoImage $1 $2 $3 "$4" "${DIFFTYPE}"
146[ $? -eq $OG_ERR_NOTDIFFERENT ] &&  exit $(ogRaiseError session $OG_ERR_NOTDIFFERENT; echo $?)
147
148#Calculamos el tamaño de la imagen:
149ogMount $1 $2 >/dev/null
150ogLock $1 $2
151ogEcho log session  "     $MSG_SCRIPTS_CREATE_SIZE"
152cd $ORIG
153SIZEDATA=$(grep -v "\/$" /tmp/ogimg.info | tr '\n' '\0'| du -x -c --files0-from=- 2>/dev/null|tail -n1 |cut -f1)
154let SIZEREQUIRED=$SIZEDATA*$ZSYNC/100
155# Tamaño minimo fichero imagen 300000
156[ $SIZEREQUIRED -lt 300000 ] && SIZEREQUIRED=300000
157
158# Comprueba que quepa en el repositorio
159[ "$3" == "CACHE" -o "$3" == "cache" ] && SIZEFREE=$(ogGetFreeSize `ogFindCache`) && ogMountCache >/dev/null
160[ "$3" == "REPO"  -o "$3" == "repo"  ] && SIZEFREE=$(df -k | grep $OGIMG | awk '{print $4}')
161ogEcho log session  "      $SIZEREQUIRED $SIZEFREE"
162[ $SIZEREQUIRED -gt $SIZEFREE ] && exit $(ogRaiseError session $OG_ERR_CACHESIZE "$3"; echo $?)
163
164TIMEAUX3=$[SECONDS-TIME2]
165ogEcho log session "      $MSG_SCRIPTS_TASK_END $MSG_SCRIPTS_TIME_PARTIAL: $[TIMEAUX3/60]m $[TIMEAUX3%60]s"
166
167# Del fichero de acl de la partición extraemos las acl de los ficheros de la diferencial
168if [ "$FSTYPE" == "NTFS" ]; then
169        ogEcho log session "[55] $MSG_HELP_ogAclFilter "
170        ogAclFilter
171        TIMEAUX4=$[SECONDS-TIMEAUX3]
172        ogEcho log session "      $MSG_SCRIPTS_TASK_END $MSG_SCRIPTS_TIME_PARTIAL: $[TIMEAUX4/60]m $[TIMEAUX4%60]s"
173fi
174TIMEAUX5=$[SECONDS-TIMEAUX3]
175
176# Configuro la informacion del tamaño de los datos en /tmp/ogimg.info
177sed -i s/SIZEDATA/"$SIZEDATA"/g /tmp/ogimg.info
178
179# Creamos o redimensionamos la imagen
180ogEcho log session "[60] $MSG_HELP_ogCreateFileImage."
181ogCreateFileImage $3 "$5" $DIFFTYPE $SIZEREQUIRED
182
183# Esperamos que se monte la imagen despues de crarla en el servidor
184ogWaitSyncImage "$3" "$5" "$DIFFTYPE" "mounted" $SIZEREQUIRED  || exit $(ogRaiseError session $OG_ERR_DONTMOUNT_IMAGE "$3 $5 $DIFFTYPE: time_out."; echo $?)
185
186ogEcho log session  "[70] $MSG_HELP_ogSyncCreate"
187# Copio los datos a la imagen diferecial :
188ogSyncCreate $1 $2 $3 "$5" $DIFFTYPE
189RETVAL=$?
190[ $RETVAL == 0 ] || ogEcho session warning "$MSG_ERR_SYNCHRONIZING"
191TIMEAUX6=$[SECONDS-TIMEAUX5]
192ogEcho log session   "      $MSG_SCRIPTS_TASK_END, $MSG_SCRIPTS_TIME_PARTIAL: $[TIMEAUX6/60]m $[TIMEAUX6%60]s"
193
194# Reducimos la imagen diferencial -> solo para kernel <= 3.7, imagenes con FS ext4
195ogEcho log session "[80] $MSG_HELP_ogReduceImage: $3 /$4.$IMGEXT"
196ogReduceImage  $3 "$5" $DIFFTYPE
197# Esperamos que el servidor termine de reducir la imagen
198ogWaitSyncImage "$3" "$5" $DIFFTYPE "reduced" $SIZEREQUIRED || exit $(ogRaiseError session $OG_ERR_DONTMOUNT_IMAGE "$3 $4 $IMGEXT: time_out."; echo $?)
199# TODO: necesaria funcion que espere a que se reduzca la imagen para continuar el script
200
201echo " " > $OGLOGCOMMAND
202# Comprobamos que la imagen esta bien
203ogEcho log session "[95] $MSG_HELP_ogCheckSyncImage"
204sleep 10
205ogCheckSyncImage $3 "$5" diff ||  exit $(ogRaiseError session $OG_ERR_IMAGE "$3 $5 diff"; echo $?)
206
207#resumen de la operacion
208IMGSIZE=$(ls -l --block-size=1024 "${DIFFFILE}"| cut -f5 -d" ")
209TIME=$[SECONDS-TIME1]
210ogEcho log session "[100] $MSG_SCRIPTS_TIME_TOTAL $[TIME/60]m $[TIME%60]s"
211ogEcho log session "      FileSystem $PART with $SIZEDATA KB data created onto file-image as $5 and used $IMGSIZE KB acros DFS rsync "
212
213# Si ha habido problema al sincronizar nos salimos con error
214[ $RETVAL == 0 ] || exit $OG_ERR_SYNCHRONIZING
Note: See TracBrowser for help on using the repository browser.