source: client/shared/scripts/createBaseImage @ 3ab9022

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 3ab9022 was e2a7442, checked in by irina <irinagomez@…>, 11 years ago

#565 Sincronizadas. Se bloquean igual que las monoliticas. Al crear diferenciales se detecta si la particion es igual que la basica

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

  • Property mode set to 100755
File size: 6.9 KB
RevLine 
[1a632ba]1#!/bin/bash
2
3#/**
4#         createBaseImage
5#@brief   Script de ejemplo para crear una imagen 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
11#@return 
12#@exception OG_ERR_FORMAT     # 1 formato incorrecto.
13#@exception OG_ERR_PARTITION  # 3 Error en partición de disco o en su sistema de archivos
[56b3a42]14#@exception OG_ERR_LOCKED     # 4 Imagen o particion bloqueada
[1a632ba]15#@exception OG_ERR_IMAGE      # 5 Error en funcion ogCreateImage o ogRestoreImage.
16#@exception OG_ERR_NOTWRITE   # 14 error de escritura
17#@exception OG_ERR_NOTCACHE   # 15 si cache no existe 15
18#@exception OG_ERR_CACHESIZE  # 16 si espacio de la cache local o remota no tiene espacio 16
[e27c4f4]19#@exception OG_ERR_DONTMOUNT_IMAGE # 70 Error al montar una imagen sincronizada
[251c9e4]20#@note  No necesario permiso se escritura por samba en repo.
[1a632ba]21#@todo: que hacer, si el tamaño de la cache es sufciente, pero no tiene espacio libre
[e27c4f4]22#@version 1.0 - creación imagen con btrfs
[1a632ba]23#@author 
24#@date   2012-12-04
25#*/ ##
26
[30ad471]27trap "onexit $1 $2 $3 \"$4\"" 1 2 3 6 9 14 15 EXIT
[1a632ba]28
[e27c4f4]29# Si salimos con error demontamos la imagen y desbloqueamos la imagen y la particion
30function onexit() {
31    local exit_status=$?
[f754a8f]32    if [ $exit_status -ne 4 ]; then
[e2a7442]33        ogUnlockImage "$3" "/$4.$IMGEXT"
[f754a8f]34        ogUnlock $1 $2
35    fi
[e27c4f4]36    exit $exit_status
37}
[1a632ba]38
[d2b8d24]39TIME1=$SECONDS
[1a632ba]40#Carga el configurador del engine desde el fichero engine.cfg
41[ -z $OGENGINECONFIGURATE ] && source /opt/opengnsys/etc/engine.cfg
42
[cd1f048]43# Factor de calculo de Time out al crear imagen: valor por defecto en engine.cfg
[d2b8d24]44CREATESPEED=${CREATESPEED:-"100000*4"}
[791d013]45
[f456755]46# Sistema de fichero de la imagen según kernel, menor que 3.7 EXT4. comparamos revision
47[  $(uname -r|cut -d. -f2) -lt 7 ] &&  IMGFS="EXT4" || IMGFS="BTRFS"
[791d013]48# % de compresion para estimacion tamaño imagen IMGFS="EXT4"
49ZSYNC=${ZSYNC:-"120"}
50
51# Si IMGFS="BTRFS" la compresion es mayor.
52[ $IMGFS == "BTRFS" ] && let ZSYNC=$ZSYNC-30
[d2b8d24]53
[1a632ba]54PROG="$(basename $0)"
[e27c4f4]55# Si se solicita, mostrar ayuda.
56if [ "$*" == "help" ]; then
57    ogHelp  "$PROG: $MSG_HELP_createBaseImage" \
58            "$PROG ndisco nparticion REPO|CACHE base_image" \
59            "$PROG 1 1 REPO Windows7"
[791d013]60    exit 0
[1a632ba]61fi
62
[251c9e4]63[ $# -ne 4 ] && exit $(ogRaiseError session $OG_ERR_FORMAT "$MSG_FORMAT: $PROG ndisco nparticion REPO|CACHE imagen" ; echo $?)
[1a632ba]64
[1ee5d4d3]65# Limpiamos fichero de log
[ef938d2]66echo  " " > $OGLOGCOMMAND
[791d013]67[ "$(ogGetCaller)" == "RestaurarImagenBasica" ] || echo  -n ""> $OGLOGSESSION
[1a632ba]68
[251c9e4]69ogEcho log session "[1] $MSG_SCRIPTS_START $0 $*"
[1a632ba]70
[ef938d2]71# Valores por defecto en etc/engine.cfg
72IMGEXT="img"
73REPOIP=$(ogGetRepoIp)
74
75
[d2b8d24]76# Comprobamos si la imagen o la particion estan bloqueada:
[251c9e4]77ogIsImageLocked "$3" "$4.$IMGEXT" && exit $(ogRaiseError session $OG_ERR_LOCKED "$3 $4.$IMGEXT"; echo $?)
78ogIsLocked "$1" "$2" && exit $(ogRaiseError session $OG_ERR_LOCKED  "$1 $2"; echo $?)
[1a632ba]79
[d2b8d24]80# Si el repositorio es CACHE comprobamos que exista
81if [ "$3" == "CACHE" -o "$3" == "cache" ]; then
[251c9e4]82        ! ogFindCache >/dev/null && exit $(ogRaiseError session $OG_ERR_NOTCACHE "CACHE "; echo $?)
[d2b8d24]83fi
[1a632ba]84
[d2b8d24]85# Obtener información de los parámetros de entrada.
[251c9e4]86PART=$(ogDiskToDev "$1" "$2" 2>/dev/null) || exit $(ogRaiseError session $OG_ERR_PARTITION "$1 $2" ; echo $?)
[791d013]87# Comprobar consistencia del sistema de archivos.
88echo " " > $OGLOGCOMMAND
89SIZEFS=$(ogGetFsSize  $1 $2)
[251c9e4]90ogEcho log session "[20] $MSG_HELP_ogCheckFs  $PART $SIZEFS (KB) "
[791d013]91ogUnmount $1 $2
[251c9e4]92ogCheckFs $1 $2 &> $OGLOGCOMMAND || exit $(ogRaiseError session $OG_ERR_PARTITION "ogCheckFs $1 $2"; echo $?)
[791d013]93
[cd1f048]94# Comprobamos que la particion se puede montar
[251c9e4]95ORIG=$(ogMount $1 $2) || exit $(ogRaiseError session $OG_ERR_PARTITION "$1 $2" ; echo $?)
[1a632ba]96
[cd1f048]97# Borramos ficheros de paginacion y configuracion
98case "$(ogGetFsType $1 $2)" in
99    EXT[234])
100        ogCleanLinuxDevices $1 $2
101        rm -rf $ORIG/tmp/*
102        ;;
103    NTFS)
[791d013]104        [ $(ogGetPath $1 $2 pagefile.sys) ] && ogDeleteFile $1 $2 pagefile.sys
105        [ $(ogGetPath $1 $2 hiberfil.sys) ] && ogDeleteFile $1 $2 hiberfil.sys 
106        [ $(ogGetPath $1 $2 swapfile.sys) ] && ogDeleteFile $1 $2 swapfile.sys
[cd1f048]107        ;;
108esac
109
[1a632ba]110#Comprobar espacio que requerira la imagen para ser almacenada
[cd1f048]111SIZEDATA=$(df -k | grep $PART | awk '{print $3}')
112#Aplicar factor de compresion
[791d013]113let SIZEREQUIRED=$SIZEDATA*$ZSYNC/100
[cd1f048]114# El tamaño mínimo del sistema de ficheros btrfs es 250M, ponemos 300
115[ $SIZEREQUIRED -lt 300000 ] && SIZEREQUIRED=300000
116#Comprobar espacio libre en el contenedor.
117[ "$3" == "CACHE" -o "$3" == "cache" ] && SIZEFREE=$(ogGetFreeSize `ogFindCache`)
118[ "$3" == "REPO"  -o "$3" == "repo"  ] && SIZEFREE=$(df -k | grep $OGIMG | awk '{print $4}')
[251c9e4]119ogEcho log session "      $MSG_SCRIPTS_CREATE_SIZE  $SIZEREQUIRED $SIZEFREE"
120[ $SIZEREQUIRED -gt $SIZEFREE ] && exit $(ogRaiseError session $OG_ERR_CACHESIZE "$3"; echo $?)
[654d744]121IMGDIR="$(ogGetParentPath "$3" "/$4")"
[d2b8d24]122IMGFILE=${IMGDIR[$3]}/$(basename "/$4").$IMGEXT
123
[1a632ba]124# Crear la imagen.
125echo " " > $OGLOGCOMMAND
126TIME2=$SECONDS
[1ee5d4d3]127
[251c9e4]128ogEcho log session "[40] $MSG_HELP_ogCreateImage $1 $2 $3 $4 "
[1ee5d4d3]129# Si existe el fichero de la imagen se hace copia de seguridad y se redimensiona, si  no existe se crea.
[251c9e4]130ogEcho log session "[50] $MSG_HELP_ogCreateFileImage."
[e2a7442]131ogLockImage "$3"  "/$4.$IMGEXT"
[1ee5d4d3]132ogCreateFileImage $3 "$4" $IMGEXT $SIZEREQUIRED
[1a632ba]133
134# Creamos la lista del contenido y lo situamos en la particion a copiar.
[251c9e4]135ogEcho log session "[60] $MSG_HELP_ogCreateInfoImage"
[d2b8d24]136ogCreateInfoImage $1 $2 $IMGEXT
[1a632ba]137
138TIMEAUX3=$[SECONDS-TIME2]
[251c9e4]139ogEcho log session "      $MSG_SCRIPTS_TASK_END, $MSG_SCRIPTS_TIME_PARTIAL : $[TIMEAUX3/60]m $[TIMEAUX3%60]s"
[1a632ba]140
[d2b8d24]141# Esperamos que el servidor termine de crear y montar la imagen
[251c9e4]142ogWaitMountImage "$3" "$4" $IMGEXT $SIZEREQUIRED || exit $(ogRaiseError session $OG_ERR_DONTMOUNT_IMAGE "$3 $4 $IMGEXT: time_out."; echo $?)
[d2b8d24]143
144# Sincronizamos los datos de la particion con la imagen.
[251c9e4]145ogEcho log session "[70] $MSG_HELP_ogSyncCreate."
[0b91489]146ogSyncCreate $1 $2 $3 "$4"  $IMGEXT
[c4db9c1]147RETVAL=$?
148[ $RETVAL == 0 ] || ogEcho session warning "$MSG_ERR_SYNCHRONIZING"
[d2b8d24]149
[791d013]150TIMEAUX5=$[SECONDS-TIMEAUX3]
[251c9e4]151ogEcho log session "      $MSG_SCRIPTS_TASK_END, $MSG_SCRIPTS_TIME_PARTIAL: $[TIMEAUX5/60]m $[TIMEAUX5%60]s"
[791d013]152
[251c9e4]153# Reducimos la imagen: solo para kernel <= 3.7, imagenes con FS ext4. (Desmonta y desbloquea la imagen)
154ogEcho log session "[80] $MSG_HELP_ogReduceImage: $3 /$4.$IMGEXT"
[f456755]155ogReduceImage $3 "$4" $IMGEXT
[e2a7442]156# TODO: necesaria funcion que espere a que se reduzca la imagen para continuar el script
[1ee5d4d3]157
[791d013]158echo  " " > $OGLOGCOMMAND
[d2b8d24]159# Comprobamos que la imagen esta bien detectacdo que es un sistema de ficheros.
[251c9e4]160ogEcho log session "[95] $MSG_HELP_ogCheckSyncImage"
[e2a7442]161sleep 10
[251c9e4]162ogCheckSyncImage $3 "$4" "img" || exit $(ogRaiseError session $OG_ERR_IMAGE "$3 $4 img" ; echo $?)
[1a632ba]163
164#resumen de la operacion
[654d744]165IMGSIZE=$(ls -l --block-size=1024 "$IMGFILE" | cut -f5 -d" ")
[1a632ba]166
167TIME=$[SECONDS-TIME1]
[251c9e4]168ogEcho log session "[100] $MSG_SCRIPTS_TIME_TOTAL $[TIME/60]m $[TIME%60]s"
169ogEcho log session "      FileSystem $PART with $SIZEDATA KB data created onto file-image as $4 and used $IMGSIZE KB acros DFS rsync "
[c4db9c1]170
171# Si ha habido problema al sincronizar nos salimos con error
172[ $RETVAL == 0 ] || exit $OG_ERR_SYNCHRONIZING
Note: See TracBrowser for help on using the repository browser.