source: client/shared/scripts/createDiffImage @ f269659

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 f269659 was 31d4f1a5, checked in by irina <irinagomez@…>, 7 years ago

#815 ogGetSizeParameters: Al calcular el espacio libre en el repositorio tiene en cuenta si la imagen ya existe.

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

  • Property mode set to 100755
File size: 7.7 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.
[5b825b50]6#@brief   Se usa como base para el programa de creación de imágenes de OpenGnsys Admin).
[1a632ba]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
[e784187]28#@version 1.1.0 - Se muestra el espacio necesario para alojar la imagen y el disponible (ticket #771)
29#@author  Irina Gomez - ETSII Universidad de Sevilla
30#@date    2017-03-28
[1a632ba]31#*/ ##
[30ad471]32trap "onexit $1 $2 $3 \"$4\" \"$5\"" 1 2 3 6 9 14 15 EXIT
[f456755]33
[e27c4f4]34function onexit() {
35    local exit_status=$?
[e24b042]36    ogUnmountImage $3 "$5" $DIFFTYPE &>/dev/null
[e27c4f4]37    ogUnmountImage $3 "$4" $IMGEXT &>/dev/null
[3a61f94]38    if [ $exit_status -ne 4 ]; then
[e2a7442]39        ogUnlockImage "$3" "/$4.$IMGEXT" &>/dev/null
40        ogUnlockImage "$3" "/$5.$DIFFEXT" &>/dev/null
[3a61f94]41        ogUnlock $1 $2 &>/dev/null
42    fi
[e27c4f4]43    exit $exit_status
44}
[cd1f048]45
[d2b8d24]46#Carga el configurador del engine desde el fichero engine.cfg
47[ -z $OGENGINECONFIGURATE ] &&  source /opt/opengnsys/etc/engine.cfg
[cd1f048]48
[1a632ba]49TIME1=$SECONDS
50
51PROG="$(basename $0)"
[e27c4f4]52# Si se solicita, mostrar ayuda.
53if [ "$*" == "help" ]; then
54    ogHelp  "$PROG: $MSG_HELP_createDiffImage" \
55            "$PROG ndisco nparticion REPO|CACHE base_image diff_image" \
56            "$PROG 1 1 REPO Windows7 Win7aula23"
57   exit 0
[1a632ba]58fi
59
[251c9e4]60[ $# -lt 5 ] && exit $(ogRaiseError session $OG_ERR_FORMAT "$MSG_FORMAT: $PROG ndisco nparticion REPO|CACHE base_image diff_image"; echo $?)
[e27c4f4]61
[1a632ba]62# Valores por defecto en etc/engine.cfg
63IMGEXT="img"
[e24b042]64DIFFEXT="img.diff"
65DIFFTYPE="diff"
[251c9e4]66REPOIP=$(ogGetRepoIp)
[ef938d2]67
68# Limpiamos los archivo de log
[791d013]69echo -n "" >$OGLOGCOMMAND
70[ "$(ogGetCaller)" == "RestaurarSoftIncremental" ] ||  echo -n "" > $OGLOGSESSION
[ef938d2]71
[251c9e4]72ogEcho log session  "[1] $MSG_SCRIPTS_START $0 $*"
[1a632ba]73
[d2b8d24]74# Comprobamos si la imagen diferencial o la particion estan bloqueada:
[251c9e4]75ogIsImageLocked "$3" "$5.$DIFFEXT" && exit $(ogRaiseError session $OG_ERR_LOCKED "$3 $5.$DIFFEXT"; echo $?)
76ogIsLocked "$1" "$2" && exit $(ogRaiseError session $OG_ERR_LOCKED  "$1 $2"; echo $?)
[d2b8d24]77
78# Comprobar si la imagen completa existe.
[251c9e4]79IMGFILE="$(ogGetPath "$3" "$4.$IMGEXT")" || exit $(ogRaiseError session $OG_ERR_NOTFOUND "$3 $4.$IMGEXT"; echo $?)
[d2b8d24]80
[e27c4f4]81# Comprobar que la imagen completa es sincronizable
[251c9e4]82ogIsSyncImage "$3" "$4" "img" || exit $(ogRaiseError session $OG_ERR_DONTSYNC_IMAGE "$3 $4"; echo $?)
[d2b8d24]83# Comprobar que no está bloqueada
[251c9e4]84ogIsImageLocked "$3" "$4.$IMGEXT" && exit $(ogRaiseError session $OG_ERR_LOCKED "$3 $4.$IMGEXT"; echo $?)
[d2b8d24]85
86# Si el repositorio es CACHE comprobamos que exista
87if [ "$3" == "CACHE" -o "$3" == "cache" ]; then
[251c9e4]88        ! ogFindCache >/dev/null && exit $(ogRaiseError session $OG_ERR_NOTCACHE "CACHE "; echo $?)
[d2b8d24]89fi
[1a632ba]90
91echo " " > $OGLOGCOMMAND
92
93# Obtener información de los parámetros de entrada.
[251c9e4]94PART=$(ogDiskToDev "$1" "$2" 2>/dev/null) || exit $(ogRaiseError session $OG_ERR_PARTITION "$1 $2"; echo $?)
[e27c4f4]95#Comprobamos que la partición se puede montar.
[251c9e4]96ORIG=$(ogMount $1 $2) ||   exit $(ogRaiseError session $OG_ERR_PARTITION "$1 $2"; echo $?)
[1a632ba]97
[d2b8d24]98DIFFDIR="$(ogGetParentPath "$3" "/$5")"
99DIFFFILE="$DIFFDIR/$5.$DIFFEXT"
[1a632ba]100
[1a2fa9d8]101# Bloqueo las imagenes -> Si no hay acceso de escritura dara error y nos saldremos.
102ogLockImage "$3"  "/$4.$IMGEXT" || exit $?
103ogLockImage "$3"  "/$5.$DIFFEXT" || exit $?
[1a632ba]104
105# Comprobar consistencia del sistema de archivos.
106echo " " > $OGLOGCOMMAND
107SIZEFS=$(ogGetFsSize  $1 $2)
[251c9e4]108ogEcho log session "[20] $MSG_HELP_ogCheckFs  $1 $2 $SIZEFS (KB) "
[1a632ba]109ogUnmount $1 $2
[251c9e4]110ogCheckFs $1 $2 >$OGLOGCOMMAND || exit $(ogRaiseError session $OG_ERR_PARTITION "ogCheckFs $1 $2" ; echo $?)
[1a632ba]111
[cd1f048]112# Borramos ficheros de paginacion y configuracion
[d3dc88d]113ogCleanOs $1 $2
[cd1f048]114
[1a632ba]115# Crear la imagen.
116echo " " > $OGLOGCOMMAND
117TIME2=$SECONDS
[251c9e4]118ogEcho log session "[40] $MSG_HELP_createDiffImage: $1 $2 $3 $4"
[1a632ba]119# Creamos la lista del contenido y lo situamos en la particion a copiar.
120
121# Montamos imagen completa (con la que vamos a comparar)
[1ee5d4d3]122ogMountImage $3 "$4" $IMGEXT >/dev/null
[d2b8d24]123
124# Comprobar que la imagen completa se ha montado
[c8bbcdc]125ogWaitSyncImage $3 "$4" $IMGEXT "mounted" || exit $(ogRaiseError session $OG_ERR_DONTMOUNT_IMAGE "$3 $4 $IMGEXT: time_out."; echo $?)
[1ee5d4d3]126
127# Creamos la informacion de la imagen.
[d3dc88d]128ogEcho log session "[45] $MSG_HELP_ogCreateInfoImage "
[e24b042]129ogCreateInfoImage $1 $2 $3 "$4" "${DIFFTYPE}"
[e2a7442]130[ $? -eq $OG_ERR_NOTDIFFERENT ] &&  exit $(ogRaiseError session $OG_ERR_NOTDIFFERENT; echo $?)
[6c2dee2]131
[d3dc88d]132#Comprobar espacio que requerira la imagen para ser almacenada
[31d4f1a5]133read SIZEDATA SIZEREQUIRED SIZEFREE ISENOUGHSPACE <<< $(ogGetSizeParameters $1 $2 "$3" "$5" DIFF)
[d3dc88d]134
[e784187]135ogEcho log session "[50] $PROG: $MSG_SCRIPTS_CREATE_SIZE $SIZEREQUIRED $SIZEFREE"
[d3dc88d]136[ "$ISENOUGHSPACE" == "TRUE" ] || exit $(ogRaiseError session $OG_ERR_CACHESIZE "$3"; echo $?)
137
[d2b8d24]138#Calculamos el tamaño de la imagen:
[1ee5d4d3]139ogMount $1 $2 >/dev/null
[1a632ba]140ogLock $1 $2
[1ee5d4d3]141
[791d013]142TIMEAUX3=$[SECONDS-TIME2]
[251c9e4]143ogEcho log session "      $MSG_SCRIPTS_TASK_END $MSG_SCRIPTS_TIME_PARTIAL: $[TIMEAUX3/60]m $[TIMEAUX3%60]s"
[791d013]144
145TIMEAUX5=$[SECONDS-TIMEAUX3]
146
[1ee5d4d3]147# Configuro la informacion del tamaño de los datos en /tmp/ogimg.info
148sed -i s/SIZEDATA/"$SIZEDATA"/g /tmp/ogimg.info
[1a632ba]149
[d2b8d24]150# Creamos o redimensionamos la imagen
[251c9e4]151ogEcho log session "[60] $MSG_HELP_ogCreateFileImage."
[e24b042]152ogCreateFileImage $3 "$5" $DIFFTYPE $SIZEREQUIRED
[1a632ba]153
[d2b8d24]154# Esperamos que se monte la imagen despues de crarla en el servidor
[c8bbcdc]155ogWaitSyncImage "$3" "$5" "$DIFFTYPE" "mounted" $SIZEREQUIRED  || exit $(ogRaiseError session $OG_ERR_DONTMOUNT_IMAGE "$3 $5 $DIFFTYPE: time_out."; echo $?)
[1a632ba]156
[251c9e4]157ogEcho log session  "[70] $MSG_HELP_ogSyncCreate"
[d2b8d24]158# Copio los datos a la imagen diferecial :
[e24b042]159ogSyncCreate $1 $2 $3 "$5" $DIFFTYPE
[c4db9c1]160RETVAL=$?
161[ $RETVAL == 0 ] || ogEcho session warning "$MSG_ERR_SYNCHRONIZING"
[791d013]162TIMEAUX6=$[SECONDS-TIMEAUX5]
[ebc31ef5]163ogEcho log session   "      $MSG_SCRIPTS_TASK_END, $MSG_SCRIPTS_TIME_PARTIAL: $[TIMEAUX6/60]m $[TIMEAUX6%60]s"
[d2b8d24]164
[6c2dee2]165# Reducimos la imagen diferencial -> solo para kernel <= 3.7, imagenes con FS ext4
[251c9e4]166ogEcho log session "[80] $MSG_HELP_ogReduceImage: $3 /$4.$IMGEXT"
[6c2dee2]167ogReduceImage  $3 "$5" $DIFFTYPE
[c8bbcdc]168# Esperamos que el servidor termine de reducir la imagen
169ogWaitSyncImage "$3" "$5" $DIFFTYPE "reduced" $SIZEREQUIRED || exit $(ogRaiseError session $OG_ERR_DONTMOUNT_IMAGE "$3 $4 $IMGEXT: time_out."; echo $?)
[e2a7442]170# TODO: necesaria funcion que espere a que se reduzca la imagen para continuar el script
[1a632ba]171
[791d013]172echo " " > $OGLOGCOMMAND
[d2b8d24]173# Comprobamos que la imagen esta bien
[251c9e4]174ogEcho log session "[95] $MSG_HELP_ogCheckSyncImage"
175ogCheckSyncImage $3 "$5" diff ||  exit $(ogRaiseError session $OG_ERR_IMAGE "$3 $5 diff"; echo $?)
[1a632ba]176
177#resumen de la operacion
[654d744]178IMGSIZE=$(ls -l --block-size=1024 "${DIFFFILE}"| cut -f5 -d" ")
[1a632ba]179TIME=$[SECONDS-TIME1]
[251c9e4]180ogEcho log session "[100] $MSG_SCRIPTS_TIME_TOTAL $[TIME/60]m $[TIME%60]s"
181ogEcho log session "      FileSystem $PART with $SIZEDATA KB data created onto file-image as $5 and used $IMGSIZE KB acros DFS rsync "
[c4db9c1]182
183# Si ha habido problema al sincronizar nos salimos con error
184[ $RETVAL == 0 ] || exit $OG_ERR_SYNCHRONIZING
Note: See TracBrowser for help on using the repository browser.