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