source: client/shared/scripts/createBaseImage @ 0b91489

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

#565 Se controla el error en la transferencia de rsync y se muestra aviso

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

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