[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 |
---|
| 20 | #@note se toma como punto de partida el script createImage, cambiando solo lo especifico para la imagen sincronizada |
---|
[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 | |
---|
[e27c4f4] | 27 | trap "onexit $1 $2 $3 \"$4\"" 1 2 3 6 9 14 15 ERR |
---|
[1a632ba] | 28 | |
---|
[cd1f048] | 29 | # Establecemos factor de compresion |
---|
[f456755] | 30 | # !!! Se configura en las lineas 110 y 116. |
---|
[cd1f048] | 31 | |
---|
[e27c4f4] | 32 | # Si salimos con error demontamos la imagen y desbloqueamos la imagen y la particion |
---|
| 33 | function onexit() { |
---|
| 34 | local exit_status=$? |
---|
[ef938d2] | 35 | local MSG="$(ogRaiseError $exit_status \"$1 $2 $3 $4\" 2>&1)" |
---|
| 36 | echo "$MSG" |tee -a $OGLOGFILE $OGLOGSESSION |
---|
[56b3a42] | 37 | ogUnmountImage $3 "$4" $IMGEXT &>/dev/null |
---|
[f754a8f] | 38 | if [ $exit_status -ne 4 ]; then |
---|
| 39 | ogUnlockImage "$3" "/$4.$IMGEXT" |
---|
| 40 | ogUnlock $1 $2 |
---|
| 41 | fi |
---|
[e27c4f4] | 42 | exit $exit_status |
---|
| 43 | } |
---|
[1a632ba] | 44 | |
---|
[d2b8d24] | 45 | TIME1=$SECONDS |
---|
[1a632ba] | 46 | #Carga el configurador del engine desde el fichero engine.cfg |
---|
| 47 | [ -z $OGENGINECONFIGURATE ] && source /opt/opengnsys/etc/engine.cfg |
---|
| 48 | |
---|
[cd1f048] | 49 | # Factor de calculo de Time out al crear imagen: valor por defecto en engine.cfg |
---|
[d2b8d24] | 50 | CREATESPEED=${CREATESPEED:-"100000*4"} |
---|
[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" |
---|
[d2b8d24] | 53 | |
---|
[1a632ba] | 54 | PROG="$(basename $0)" |
---|
[e27c4f4] | 55 | # Si se solicita, mostrar ayuda. |
---|
| 56 | if [ "$*" == "help" ]; then |
---|
| 57 | ogHelp "$PROG: $MSG_HELP_createBaseImage" \ |
---|
| 58 | "$PROG ndisco nparticion REPO|CACHE base_image" \ |
---|
| 59 | "$PROG 1 1 REPO Windows7" |
---|
| 60 | exit 0 |
---|
[1a632ba] | 61 | fi |
---|
| 62 | |
---|
[e27c4f4] | 63 | [ $# -ne 4 ] && ogRaiseError $OG_ERR_FORMAT "$MSG_FORMAT: $PROG ndisco nparticion REPO|CACHE imagen" |
---|
| 64 | |
---|
[1a632ba] | 65 | |
---|
[1ee5d4d3] | 66 | # Limpiamos fichero de log |
---|
| 67 | echo -n ""> $OGLOGSESSION |
---|
[ef938d2] | 68 | echo " " > $OGLOGCOMMAND |
---|
[1a632ba] | 69 | |
---|
[1ee5d4d3] | 70 | echo "[1] $MSG_SCRIPTS_START $0 $*" | tee -a $OGLOGSESSION $OGLOGFILE |
---|
[1a632ba] | 71 | |
---|
[ef938d2] | 72 | |
---|
| 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: |
---|
[e27c4f4] | 79 | ogIsImageLocked "$3" "$4.$IMGEXT" && ogRaiseError $OG_ERR_LOCKED "$3 $4.$IMGEXT" |
---|
| 80 | ogIsLocked "$1" "$2" && ogRaiseError $OG_ERR_LOCKED "$1 $2" |
---|
[1a632ba] | 81 | |
---|
[d2b8d24] | 82 | # Si el repositorio es CACHE comprobamos que exista |
---|
| 83 | if [ "$3" == "CACHE" -o "$3" == "cache" ]; then |
---|
[e27c4f4] | 84 | ! ogFindCache >/dev/null && ogRaiseError $OG_ERR_NOTCACHE "CACHE " |
---|
[d2b8d24] | 85 | fi |
---|
[1a632ba] | 86 | |
---|
| 87 | |
---|
[d2b8d24] | 88 | # Obtener información de los parámetros de entrada. |
---|
[e27c4f4] | 89 | PART=$(ogDiskToDev "$1" "$2" 2>/dev/null) || ogRaiseError $OG_ERR_PARTITION "$1 $2" |
---|
[cd1f048] | 90 | # Comprobamos que la particion se puede montar |
---|
| 91 | ORIG=$(ogMount $1 $2) || ogRaiseError $OG_ERR_PARTITION "$1 $2" |
---|
[1a632ba] | 92 | |
---|
[d2b8d24] | 93 | #Comprobamos acceso de escritura. |
---|
[654d744] | 94 | DIRTEMP="/$4$(date +%Y%m%d-%H%M%S)" |
---|
| 95 | ogMakeDir $3 "$DIRTEMP" 2>/dev/null || ogRaiseError $OG_ERR_NOTWRITE "$3" && ogDeleteTree $3 "$DIRTEMP" |
---|
[1a632ba] | 96 | |
---|
[cd1f048] | 97 | # Borramos ficheros de paginacion y configuracion |
---|
| 98 | case "$(ogGetFsType $1 $2)" in |
---|
| 99 | EXT[234]) |
---|
| 100 | ogCleanLinuxDevices $1 $2 |
---|
| 101 | rm -rf $ORIG/tmp/* |
---|
[f456755] | 102 | # Compresion linux && ext4 || btrfs |
---|
| 103 | [ $IMGFS == "EXT4" ] && COMPRESS=110/100 || COMPRESS=80/100 |
---|
[cd1f048] | 104 | ;; |
---|
| 105 | NTFS) |
---|
[4b921aa] | 106 | [ $(ogGetPath $1 $2 pagefile.sys) ] && ogDeleteFile $1 $2 pagefile.sys |
---|
| 107 | [ $(ogGetPath $1 $2 hiberfil.sys) ] && ogDeleteFile $1 $2 hiberfil.sys |
---|
[f456755] | 108 | # Compresion windows && ext4 || btrfs |
---|
| 109 | [ $IMGFS == "EXT4" ] && COMPRESS=120/100 || COMPRESS=90/100 |
---|
[cd1f048] | 110 | ;; |
---|
| 111 | esac |
---|
| 112 | |
---|
[1a632ba] | 113 | #Comprobar espacio que requerira la imagen para ser almacenada |
---|
[cd1f048] | 114 | SIZEDATA=$(df -k | grep $PART | awk '{print $3}') |
---|
| 115 | #Aplicar factor de compresion |
---|
| 116 | let SIZEREQUIRED=$SIZEDATA*$COMPRESS |
---|
| 117 | # El tamaño mínimo del sistema de ficheros btrfs es 250M, ponemos 300 |
---|
| 118 | [ $SIZEREQUIRED -lt 300000 ] && SIZEREQUIRED=300000 |
---|
| 119 | #Comprobar espacio libre en el contenedor. |
---|
| 120 | [ "$3" == "CACHE" -o "$3" == "cache" ] && SIZEFREE=$(ogGetFreeSize `ogFindCache`) |
---|
| 121 | [ "$3" == "REPO" -o "$3" == "repo" ] && SIZEFREE=$(df -k | grep $OGIMG | awk '{print $4}') |
---|
[1ee5d4d3] | 122 | echo "[16]$MSG_SCRIPTS_CREATE_SIZE $SIZEREQUIRED $SIZEFREE" | tee -a $OGLOGSESSION $OGLOGFILE |
---|
[e27c4f4] | 123 | [ $SIZEREQUIRED -gt $SIZEFREE ] && ogRaiseError $OG_ERR_CACHESIZE "$3" |
---|
[1a632ba] | 124 | |
---|
[654d744] | 125 | IMGDIR="$(ogGetParentPath "$3" "/$4")" |
---|
[d2b8d24] | 126 | IMGFILE=${IMGDIR[$3]}/$(basename "/$4").$IMGEXT |
---|
| 127 | |
---|
[1a632ba] | 128 | # Comprobar consistencia del sistema de archivos. |
---|
| 129 | echo " " > $OGLOGCOMMAND |
---|
| 130 | SIZEFS=$(ogGetFsSize $1 $2) |
---|
| 131 | echo "[20] $MSG_HELP_ogCheckFs $PART $SIZEFS (KB) " | tee -a $OGLOGSESSION $OGLOGFILE |
---|
| 132 | ogUnmount $1 $2 |
---|
[e27c4f4] | 133 | ogCheckFs $1 $2 &> $OGLOGCOMMAND || ogRaiseError $OG_ERR_PARTITION "ogCheckFs $1 $2" |
---|
[1a632ba] | 134 | |
---|
| 135 | # Crear la imagen. |
---|
| 136 | echo " " > $OGLOGCOMMAND |
---|
| 137 | TIME2=$SECONDS |
---|
[1ee5d4d3] | 138 | |
---|
| 139 | ogLockImage "$3" "/$4.$IMGEXT" |
---|
| 140 | |
---|
| 141 | # Si existe el fichero de la imagen se hace copia de seguridad y se redimensiona, si no existe se crea. |
---|
| 142 | echo "[30] $MSG_HELP_ogCreateFileImage." | tee -a $OGLOGSESSION $OGLOGFILE |
---|
| 143 | ogCreateFileImage $3 "$4" $IMGEXT $SIZEREQUIRED |
---|
[1a632ba] | 144 | |
---|
| 145 | # Creamos la lista del contenido y lo situamos en la particion a copiar. |
---|
[1ee5d4d3] | 146 | echo "[50] $MSG_HELP_ogCreateImage $1 $2 $3 $4 " | tee -a $OGLOGSESSION $OGLOGFILE |
---|
| 147 | echo " $MSG_HELP_ogCreateInfoImage" | tee -a $OGLOGSESSION $OGLOGFILE |
---|
[d2b8d24] | 148 | ogCreateInfoImage $1 $2 $IMGEXT |
---|
[1a632ba] | 149 | |
---|
| 150 | TIMEAUX3=$[SECONDS-TIME2] |
---|
[1ee5d4d3] | 151 | echo " $MSG_SCRIPTS_TASK_END, $MSG_SCRIPTS_TIME_PARTIAL : $[TIMEAUX3/60]m $[TIMEAUX3%60]s" | tee -a $OGLOGSESSION $OGLOGFILE |
---|
[1a632ba] | 152 | |
---|
[d2b8d24] | 153 | # Esperamos que el servidor termine de crear y montar la imagen |
---|
[e27c4f4] | 154 | ogWaitMountImage "$3" "$4" $IMGEXT $SIZEREQUIRED || ogRaiseError $OG_ERR_DONTMOUNT_IMAGE "$3 $4 $IMGEXT: time_out." |
---|
[1ee5d4d3] | 155 | |
---|
[d2b8d24] | 156 | |
---|
| 157 | # Sincronizamos los datos de la particion con la imagen. |
---|
[1ee5d4d3] | 158 | echo " $MSG_HELP_ogSyncCreate." | tee -a $OGLOGSESSION $OGLOGFILE |
---|
[d2b8d24] | 159 | ogSyncCreate $1 $2 $3 "$4" $IMGEXT |
---|
| 160 | |
---|
[f754a8f] | 161 | # Reducimos la imagen: solo para kernel <= 3.7, imagenes con FS ext4. |
---|
[6b0b68d] | 162 | echo "[80] $MSG_HELP_ogReduceImage: $3 /$4.$IMGEXT" | tee -a $OGLOGSESSION $OGLOGFILE |
---|
[f456755] | 163 | ogReduceImage $3 "$4" $IMGEXT |
---|
[d2b8d24] | 164 | |
---|
[e24b042] | 165 | # Desmontamos la Imagen |
---|
| 166 | ogUnmountImage $3 "$4" $IMGEXT |
---|
[d2b8d24] | 167 | ogUnlockImage "$3" "/$4.$IMGEXT" |
---|
[1a632ba] | 168 | ogUnlock $1 $2 |
---|
[d2b8d24] | 169 | TIMEAUX5=$[SECONDS-TIMEAUX3] |
---|
[1ee5d4d3] | 170 | echo " $MSG_SCRIPTS_TASK_END, $MSG_SCRIPTS_TIME_PARTIAL: $[TIMEAUX3/60]m $[TIMEAUX3%60]s" | tee -a $OGLOGSESSION $OGLOGFILE |
---|
| 171 | |
---|
[d2b8d24] | 172 | # Comprobamos que la imagen esta bien detectacdo que es un sistema de ficheros. |
---|
[6b0b68d] | 173 | echo "[95] $MSG_HELP_ogCheckSyncImage" | tee -a $OGLOGSESSION $OGLOGFILE |
---|
| 174 | ogCheckSyncImage $3 "$4" "img" | tee -a $OGLOGSESSION $OGLOGFILE |
---|
| 175 | RETVAL=${PIPESTATUS[0]} |
---|
| 176 | [ "$RETVAL" == "0" ] || ogRaiseError $OG_ERR_IMAGE "$3 $4 img" |
---|
| 177 | |
---|
[1a632ba] | 178 | |
---|
[d2b8d24] | 179 | TIMEAUX7=$[SECONDS-TIME2] |
---|
| 180 | echo " $MSG_SCRIPTS_TIME_PARTIAL : $[TIMEAUX7/60]m $[TIMEAUX7%60]s" | tee -a $OGLOGSESSION $OGLOGFILE |
---|
[1a632ba] | 181 | |
---|
| 182 | |
---|
| 183 | #resumen de la operacion |
---|
[654d744] | 184 | IMGSIZE=$(ls -l --block-size=1024 "$IMGFILE" | cut -f5 -d" ") |
---|
[1a632ba] | 185 | |
---|
| 186 | TIME=$[SECONDS-TIME1] |
---|
| 187 | echo "[100] $MSG_SCRIPTS_TIME_TOTAL $[TIME/60]m $[TIME%60]s" | tee -a $OGLOGSESSION $OGLOGFILE |
---|
[1ee5d4d3] | 188 | echo " FileSystem $PART with $SIZEDATA KB data created onto file-image as $4 and used $IMGSIZE KB acros DFS rsync " | tee -a $OGLOGSESSION $OGLOGFILE |
---|