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