source: client/shared/scripts/createDiffImage @ ee4beb4

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 ee4beb4 was 332bbb1, checked in by irina <irinagomez@…>, 11 years ago

#565 Compatibilidad con el comando file de Ubuntu 14.04 para obtener correctamente la versión del Kernel del cliente ogLive.

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

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