source: client/shared/scripts/createBaseImage @ 292982d

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-instalacion
Last change on this file since 292982d was 348b5c9, checked in by Irina Gómez <irinagomez@…>, 6 years ago

#855 #679 Los script de crear o restaurar imágenes sincronizadas permiten cambiar de repositorio. ogChangeRepo: se resuelve errata por la que no tomaba los permisos (ro|rw) del repositorio.

  • 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.
[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
[348b5c9]9#@param 3 REPO|CACHE|IPREPO
[1a632ba]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
[e784187]25#@version 1.1.0 - Se muestra el espacio necesario para alojar la imagen y el disponible (ticket #771)
26#@author  Irina Gomez - ETSII Universidad de Sevilla
27#@date    2017-03-28
[348b5c9]28#@version 1.1.1 - Varios repositorios para un mismo cliente (ticket #679).
29#@author  Irina Gomez - ETSII Universidad de Sevilla
30#@date    2018/11/06
[1a632ba]31#*/ ##
32
[348b5c9]33trap "onexit $1 $2 $IMGFILE" 1 2 3 6 9 14 15 EXIT
[1a632ba]34
[e27c4f4]35# Si salimos con error demontamos la imagen y desbloqueamos la imagen y la particion
36function onexit() {
37    local exit_status=$?
[f754a8f]38    if [ $exit_status -ne 4 ]; then
[348b5c9]39        ogUnlockImage "$IMGFILE"
[f754a8f]40        ogUnlock $1 $2
41    fi
[e27c4f4]42    exit $exit_status
43}
[1a632ba]44
[d2b8d24]45TIME1=$SECONDS
[1a632ba]46#Carga el configurador del engine desde el fichero engine.cfg
47[ -z $OGENGINECONFIGURATE ] && source /opt/opengnsys/etc/engine.cfg
48
49PROG="$(basename $0)"
[e27c4f4]50# Si se solicita, mostrar ayuda.
51if [ "$*" == "help" ]; then
52    ogHelp  "$PROG: $MSG_HELP_createBaseImage" \
53            "$PROG ndisco nparticion REPO|CACHE base_image" \
54            "$PROG 1 1 REPO Windows7"
[791d013]55    exit 0
[1a632ba]56fi
57
[251c9e4]58[ $# -ne 4 ] && exit $(ogRaiseError session $OG_ERR_FORMAT "$MSG_FORMAT: $PROG ndisco nparticion REPO|CACHE imagen" ; echo $?)
[1a632ba]59
[1ee5d4d3]60# Limpiamos fichero de log
[ef938d2]61echo  " " > $OGLOGCOMMAND
[d3dc88d]62[ "$(ogGetCaller)" == "CrearImagenBasica" ] || echo  -n ""> $OGLOGSESSION
[1a632ba]63
[251c9e4]64ogEcho log session "[1] $MSG_SCRIPTS_START $0 $*"
[1a632ba]65
[ef938d2]66# Valores por defecto en etc/engine.cfg
[d3dc88d]67IMGEXT=${IMGEXT:-"img"}
[348b5c9]68REPO="${3^^}"
69# No permite directorios diferentes para OU
70OGUNIT=""
71
72# Si es una ip y es igual a la del equipo restaura desde cache
73[ "$REPO" == "$(ogGetIpAddress)" ] && REPO="CACHE"
74# Si es una ip y es distinta a la del recurso samba cambiamos de REPO.
75ogCheckIpAddress $REPO
76if [ $? == 0 -o $REPO == "REPO" ] ; then
77        # Si falla el cambio -> salimos con error repositorio no valido
78        ogChangeRepo $REPO $OGUNIT || exit $(ogRaiseError $OG_ERR_NOTFOUND '$REPO'; echo $?)
79        REPO="REPO"
80fi
[ef938d2]81
[d2b8d24]82# Comprobamos si la imagen o la particion estan bloqueada:
[348b5c9]83ogIsImageLocked "$REPO" "$4.$IMGEXT" && exit $(ogRaiseError session $OG_ERR_LOCKED "$REPO $4.$IMGEXT"; echo $?)
[251c9e4]84ogIsLocked "$1" "$2" && exit $(ogRaiseError session $OG_ERR_LOCKED  "$1 $2"; echo $?)
[1a632ba]85
[d2b8d24]86# Si el repositorio es CACHE comprobamos que exista
[348b5c9]87if [ "$REPO" == "CACHE" -o "$REPO" == "cache" ]; then
[251c9e4]88        ! ogFindCache >/dev/null && exit $(ogRaiseError session $OG_ERR_NOTCACHE "CACHE "; echo $?)
[d2b8d24]89fi
[1a632ba]90
[d2b8d24]91# Obtener información de los parámetros de entrada.
[251c9e4]92PART=$(ogDiskToDev "$1" "$2" 2>/dev/null) || exit $(ogRaiseError session $OG_ERR_PARTITION "$1 $2" ; echo $?)
[791d013]93# Comprobar consistencia del sistema de archivos.
94echo " " > $OGLOGCOMMAND
95SIZEFS=$(ogGetFsSize  $1 $2)
[251c9e4]96ogEcho log session "[20] $MSG_HELP_ogCheckFs  $PART $SIZEFS (KB) "
[791d013]97ogUnmount $1 $2
[251c9e4]98ogCheckFs $1 $2 &> $OGLOGCOMMAND || exit $(ogRaiseError session $OG_ERR_PARTITION "ogCheckFs $1 $2"; echo $?)
[791d013]99
[cd1f048]100# Comprobamos que la particion se puede montar
[251c9e4]101ORIG=$(ogMount $1 $2) || exit $(ogRaiseError session $OG_ERR_PARTITION "$1 $2" ; echo $?)
[1a632ba]102
[cd1f048]103# Borramos ficheros de paginacion y configuracion
[d3dc88d]104ogCleanOs $1 $2
[cd1f048]105
[1a632ba]106#Comprobar espacio que requerira la imagen para ser almacenada
[348b5c9]107read SIZEDATA SIZEREQUIRED SIZEFREE ISENOUGHSPACE <<< $(ogGetSizeParameters $1 $2 "$REPO" "$4" SYNC)
[d3dc88d]108
[e784187]109ogEcho log session "[16] $PROG: $MSG_SCRIPTS_CREATE_SIZE $SIZEREQUIRED $SIZEFREE"
[348b5c9]110[ "$ISENOUGHSPACE" ==  "TRUE" ] || exit $(ogRaiseError session $OG_ERR_CACHESIZE "$REPO"; echo $?)
[d3dc88d]111
[348b5c9]112IMGDIR="$(ogGetParentPath "$REPO" "/$4")"
113IMGFILE=${IMGDIR}/$(basename "/$4").$IMGEXT
[d2b8d24]114
[1a632ba]115# Crear la imagen.
116echo " " > $OGLOGCOMMAND
117TIME2=$SECONDS
[1ee5d4d3]118
[348b5c9]119ogEcho log session "[40] $MSG_HELP_ogCreateImage $1 $2 $REPO $4 "
[d3dc88d]120
[1ee5d4d3]121# Si existe el fichero de la imagen se hace copia de seguridad y se redimensiona, si  no existe se crea.
[1a2fa9d8]122# Bloqueo la imagen. Si esta en modo lectura dara error y nos salimos
[251c9e4]123ogEcho log session "[50] $MSG_HELP_ogCreateFileImage."
[348b5c9]124ogLockImage "$REPO"  "/$4.$IMGEXT" || exit $?
125ogCreateFileImage $REPO "$4" $IMGEXT $SIZEREQUIRED
[1a632ba]126
127# Creamos la lista del contenido y lo situamos en la particion a copiar.
[251c9e4]128ogEcho log session "[60] $MSG_HELP_ogCreateInfoImage"
[d2b8d24]129ogCreateInfoImage $1 $2 $IMGEXT
[1a632ba]130
131TIMEAUX3=$[SECONDS-TIME2]
[251c9e4]132ogEcho log session "      $MSG_SCRIPTS_TASK_END, $MSG_SCRIPTS_TIME_PARTIAL : $[TIMEAUX3/60]m $[TIMEAUX3%60]s"
[1a632ba]133
[d2b8d24]134# Esperamos que el servidor termine de crear y montar la imagen
[348b5c9]135ogWaitSyncImage "$REPO" "$4" $IMGEXT "mounted" $SIZEREQUIRED || exit $(ogRaiseError session $OG_ERR_DONTMOUNT_IMAGE "$REPO $4 $IMGEXT: time_out."; echo $?)
[d2b8d24]136
137# Sincronizamos los datos de la particion con la imagen.
[251c9e4]138ogEcho log session "[70] $MSG_HELP_ogSyncCreate."
[348b5c9]139ogSyncCreate $1 $2 $REPO "$4"  $IMGEXT
[c4db9c1]140RETVAL=$?
141[ $RETVAL == 0 ] || ogEcho session warning "$MSG_ERR_SYNCHRONIZING"
[d2b8d24]142
[791d013]143TIMEAUX5=$[SECONDS-TIMEAUX3]
[251c9e4]144ogEcho log session "      $MSG_SCRIPTS_TASK_END, $MSG_SCRIPTS_TIME_PARTIAL: $[TIMEAUX5/60]m $[TIMEAUX5%60]s"
[791d013]145
[251c9e4]146# Reducimos la imagen: solo para kernel <= 3.7, imagenes con FS ext4. (Desmonta y desbloquea la imagen)
[348b5c9]147ogEcho log session "[80] $MSG_HELP_ogReduceImage: $REPO /$4.$IMGEXT"
148ogReduceImage $REPO "$4" $IMGEXT
[c8bbcdc]149# Esperamos que el servidor termine de reducir la imagen
[348b5c9]150ogWaitSyncImage "$REPO" "$4" $IMGEXT "reduced" $SIZEREQUIRED || exit $(ogRaiseError session $OG_ERR_DONTMOUNT_IMAGE "$REPO $4 $IMGEXT: time_out."; echo $?)
[1ee5d4d3]151
[791d013]152echo  " " > $OGLOGCOMMAND
[0d4cef22]153sleep 2
[d2b8d24]154# Comprobamos que la imagen esta bien detectacdo que es un sistema de ficheros.
[251c9e4]155ogEcho log session "[95] $MSG_HELP_ogCheckSyncImage"
[348b5c9]156ogCheckSyncImage $REPO "$4" "img" || exit $(ogRaiseError session $OG_ERR_IMAGE "$REPO $4 img" ; echo $?)
[1a632ba]157
158#resumen de la operacion
[654d744]159IMGSIZE=$(ls -l --block-size=1024 "$IMGFILE" | cut -f5 -d" ")
[1a632ba]160
161TIME=$[SECONDS-TIME1]
[251c9e4]162ogEcho log session "[100] $MSG_SCRIPTS_TIME_TOTAL $[TIME/60]m $[TIME%60]s"
163ogEcho log session "      FileSystem $PART with $SIZEDATA KB data created onto file-image as $4 and used $IMGSIZE KB acros DFS rsync "
[c4db9c1]164
165# Si ha habido problema al sincronizar nos salimos con error
166[ $RETVAL == 0 ] || exit $OG_ERR_SYNCHRONIZING
Note: See TracBrowser for help on using the repository browser.