[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 |
---|
| 14 | #@exception OG_ERR_IMAGE # 5 Error en funcion ogCreateImage o ogRestoreImage. |
---|
| 15 | #@exception OG_ERR_NOTWRITE # 14 error de escritura |
---|
| 16 | #@exception OG_ERR_NOTCACHE # 15 si cache no existe 15 |
---|
| 17 | #@exception OG_ERR_CACHESIZE # 16 si espacio de la cache local o remota no tiene espacio 16 |
---|
| 18 | #@note se toma como punto de partida el script createImage, cambiando solo lo especifico para la imagen squash |
---|
| 19 | #@todo: que hacer, si el tamaño de la cache es sufciente, pero no tiene espacio libre |
---|
| 20 | #@version 1.0 - creación imagen con mksquasfs |
---|
| 21 | #@author |
---|
| 22 | #@date 2012-12-04 |
---|
| 23 | #*/ ## |
---|
| 24 | trap "ogUnlockImage "CACHE" "/$4.$IMGEXT"; ogUnlock $1 $2; umount $DIRMOUNT; exit 1" 1 2 3 6 9 14 15 |
---|
| 25 | |
---|
| 26 | declare -A IMGDIR |
---|
| 27 | declare -A IMGFILE |
---|
| 28 | |
---|
| 29 | TIME1=$SECONDS |
---|
| 30 | |
---|
| 31 | #Carga el configurador del engine desde el fichero engine.cfg |
---|
| 32 | [ -z $OGENGINECONFIGURATE ] && source /opt/opengnsys/etc/engine.cfg |
---|
| 33 | |
---|
| 34 | PROG="$(basename $0)" |
---|
| 35 | if [ $# -ne 4 ]; then |
---|
| 36 | ogRaiseError $OG_ERR_FORMAT "$MSG_FORMAT: $PROG ndisco nparticion REPO|CACHE imagen" |
---|
| 37 | exit $? |
---|
| 38 | fi |
---|
| 39 | |
---|
| 40 | # Valores por defecto en etc/engine.cfg |
---|
| 41 | IMGEXT="img" |
---|
| 42 | |
---|
| 43 | echo "[1] $MSG_SCRIPTS_START $0 $*" | tee -a $OGLOGSESSION $OGLOGFILE |
---|
| 44 | |
---|
| 45 | # Necesitamos hacer la imagen en la cache -> siempre comprobamos que exista |
---|
| 46 | ! ogFindCache >/dev/null && exit $(ogRaiseError $OG_ERR_NOTCACHE "CACHE "; echo $?) |
---|
| 47 | |
---|
| 48 | echo " " > $OGLOGCOMMAND |
---|
| 49 | |
---|
| 50 | # Obtener información de los parámetros de entrada. |
---|
| 51 | PART=$(ogDiskToDev "$1" "$2" 2>/dev/null) || exit $(ogRaiseError $OG_ERR_PARTITION "$1 $2"; echo $?) |
---|
| 52 | |
---|
| 53 | echo " " > $OGLOGCOMMAND |
---|
| 54 | |
---|
| 55 | # Si el destino es REPO, hemos de crear la imagen en cache y luego enviarla al repositorio |
---|
| 56 | [ "$3" == "REPO" ] && REPOS="REPO CACHE" || REPOS="CACHE" |
---|
| 57 | for DEST in $REPOS; do |
---|
| 58 | #Comprobamos acceso de escritura. |
---|
| 59 | DIRTEMP=$(date +%Y%m%d-%H%M%S) |
---|
| 60 | ogMakeDir $DEST /$4$DIRTEMP 2>/dev/null || exit $(ogRaiseError $OG_ERR_NOTWRITE "$DEST"; echo $?) && ogDeleteTree $DEST /$4$DIRTEMP |
---|
| 61 | |
---|
| 62 | IMGDIR[$DEST]=$(ogGetParentPath "$DEST" "/$4") |
---|
| 63 | # Si no existe, crear subdirectorio de la imagen. |
---|
| 64 | if [ $? != 0 ]; then |
---|
| 65 | echo "[5] $MSG_HELP_ogMakeDir \"$DEST $(dirname "$4")." | tee -a $OGLOGSESSION $OGLOGFILE |
---|
| 66 | ogMakeDir "$DEST" $(dirname "/$4") || exit $(ogRaiseError $OG_ERR_NOTWRITE "$DEST /$4"; echo $?) |
---|
| 67 | IMGDIR[$DEST]=$(ogGetParentPath "$DEST" "/$4") || exit $(ogRaiseError $OG_ERR_NOTWRITE "$3 /$4"; echo $?) |
---|
| 68 | fi |
---|
| 69 | |
---|
| 70 | IMGFILE[$DEST]=${IMGDIR[$DEST]}/$(basename "/$4").$IMGEXT |
---|
| 71 | # Renombrar el fichero de imagen si ya existe. |
---|
| 72 | if [ -f "${IMGFILE[$DEST]}" ]; then |
---|
| 73 | echo "[10] $MSG_SCRIPTS_FILE_RENAME \"${IMGFILE[$DEST]}\" -> \"${IMGFILE[$DEST]}.ant\"." | tee -a $OGLOGSESSION $OGLOGFILE |
---|
| 74 | mv -f "${IMGFILE[$DEST]}" "${IMGFILE[$DEST]}.ant" |
---|
| 75 | mv -f "${IMGFILE[$DEST]}.torrent" "${IMGFILE[$DEST]}.torrent.ant" 2>/dev/null |
---|
| 76 | fi |
---|
| 77 | done |
---|
| 78 | |
---|
| 79 | |
---|
| 80 | #Comprobar espacio que requerira la imagen para ser almacenada |
---|
| 81 | echo " " > $OGLOGCOMMAND |
---|
| 82 | if ogMount $1 $2 &>/dev/null |
---|
| 83 | then |
---|
| 84 | SIZEDATA=$(df -k | grep $PART | awk '{print $3}') |
---|
| 85 | #Aplicar factor de compresion |
---|
| 86 | FACTORGZIP=55/100 |
---|
| 87 | FACTORLZOP=65/100 |
---|
| 88 | let SIZEREQUIRED=$SIZEDATA*$FACTORLZOP |
---|
| 89 | #Comprobar espacio libre en el contenedor. |
---|
| 90 | #[ "$3" == "CACHE" ] && SIZEFREE=$(ogGetFreeSize `ogFindCache`) |
---|
| 91 | SIZEFREE=$(ogGetFreeSize `ogFindCache`) |
---|
| 92 | if [ "$3" == "REPO" ]; then |
---|
| 93 | SIZEFREEREPO=$(df -k | grep $OGIMG | awk '{print $4}') |
---|
| 94 | [ $SIZEFREEREPO -lt $SIZEFREE ] && SIZEFREE=$SIZEFREEREPO |
---|
| 95 | fi |
---|
| 96 | |
---|
| 97 | else |
---|
| 98 | ogRaiseError $OG_ERR_PARTITION "$1 $2" |
---|
| 99 | exit $? |
---|
| 100 | fi |
---|
| 101 | echo "[16] $PROG: $MSG_SCRIPTS_CREATE_SIZE $SIZEREQUIRED $SIZEFREE" | tee -a $OGLOGSESSION $OGLOGFILE |
---|
| 102 | [ "$SIZEREQUIRED" -gt "$SIZEFREE" ] && exit $(ogRaiseError $OG_ERR_CACHESIZE "$3" || echo $?) |
---|
| 103 | |
---|
| 104 | |
---|
| 105 | # Comprobar consistencia del sistema de archivos. |
---|
| 106 | echo " " > $OGLOGCOMMAND |
---|
| 107 | SIZEFS=$(ogGetFsSize $1 $2) |
---|
| 108 | echo "[20] $MSG_HELP_ogCheckFs $PART $SIZEFS (KB) " | tee -a $OGLOGSESSION $OGLOGFILE |
---|
| 109 | ogUnmount $1 $2 |
---|
| 110 | ogCheckFs $1 $2 || exit $(ogRaiseError $OG_ERR_PARTITION "ogCheckFs $1 $2" && echo $?) |
---|
| 111 | |
---|
| 112 | # Crear la imagen. |
---|
| 113 | echo " " > $OGLOGCOMMAND |
---|
| 114 | TIME2=$SECONDS |
---|
| 115 | echo "[40] $MSG_HELP_ogCreateImage $1 $2 $3 $4 " | tee -a $OGLOGSESSION $OGLOGFILE |
---|
| 116 | |
---|
| 117 | # Creamos la lista del contenido y lo situamos en la particion a copiar. |
---|
| 118 | DIRMOUNT="/tmp/mount$$" |
---|
| 119 | IMGINFO="/tmp/ogimg.info" |
---|
| 120 | IMGACL="/tmp/ogimg.acl" |
---|
| 121 | FSTYPE=$(ogGetFsType $1 $2) |
---|
| 122 | mkdir $DIRMOUNT |
---|
| 123 | |
---|
| 124 | # Guardamos el contenido de las acl (Solo win) Necesario particion desmontada (esta asi) |
---|
| 125 | [ $FSTYPE == "NTFS" ] && echo ntfs-3g.secaudit -b $PART / && ntfs-3g.secaudit -b $PART / > $IMGACL |
---|
| 126 | # Información provisional, se quitará en la version final |
---|
| 127 | TIMEAUX2=$[SECONDS-TIME2] |
---|
| 128 | echo " Fin acl- ntfs-3g.secaudit : $[TIMEAUX2/60]m $[TIMEAUX2%60]s" | tee -a $OGLOGSESSION $OGLOGFILE |
---|
| 129 | |
---|
| 130 | # Guardamos el listado de los archivos y la informacion de la imagen |
---|
| 131 | ORIG=$(ogMount $1 $2) |
---|
| 132 | echo "#SQUASH::$FSTYPE:$SIZEDATA"> $IMGINFO |
---|
| 133 | echo " rsync -aHAXvn --delete $ORIG/ $DIRMOUNT >> $IMGINFO" | tee -a $OGLOGSESSION $OGLOGFILE |
---|
| 134 | rsync -aHAXvn --delete $ORIG/ $DIRMOUNT >> $IMGINFO |
---|
| 135 | sed -i -e s/"^sent.*.bytes\/sec"//g -e s/^total.*.speedup.*.$//g -e s/"sending.*.list"//g $IMGINFO |
---|
| 136 | sed -i '/^\.\//d' $IMGINFO |
---|
| 137 | mv $IMGINFO $IMGACL $ORIG 2>/dev/null |
---|
| 138 | |
---|
| 139 | TIMEAUX3=$[SECONDS-TIME2] |
---|
| 140 | echo " Fin listado contenido y lista de control de acceso: $[TIMEAUX3/60]m $[TIMEAUX3%60]s" | tee -a $OGLOGSESSION $OGLOGFILE |
---|
| 141 | |
---|
| 142 | # Nos situamos en la cache y creamos la imagen. |
---|
| 143 | ogLock $1 $2 |
---|
| 144 | ogLockImage "CACHE" "/$4.$IMGEXT" |
---|
| 145 | echo " mksquashfs $ORIG ${IMGFILE["CACHE"]}" | tee -a $OGLOGSESSION $OGLOGFILE |
---|
| 146 | mksquashfs $ORIG ${IMGFILE["CACHE"]} &> $OGLOGCOMMAND |
---|
| 147 | ogUnlockImage "CACHE" "/$4.$IMGEXT" |
---|
| 148 | ogUnlock $1 $2 |
---|
| 149 | |
---|
| 150 | TIMEAUX4=$[SECONDS-TIMEAUX3] |
---|
| 151 | echo " Fin mksquashfs: $[TIMEAUX4/60]m $[TIMEAUX4%60]s" | tee -a $OGLOGSESSION $OGLOGFILE |
---|
| 152 | # No necesario bloquear imagen, rsync la manda a un archivo oculto |
---|
| 153 | if [ "$3" == "REPO" ]; then |
---|
| 154 | rsync ${IMGFILE["CACHE"]} ${IMGFILE["REPO"]} &> $OGLOGCOMMAND |
---|
| 155 | TIMEAUX5=$[SECONDS-TIMEAUX4] |
---|
| 156 | echo " Fin envio imagen a REPO: $[TIMEAUX5/60]m $[TIMEAUX5%60]s" | tee -a $OGLOGSESSION $OGLOGFILE |
---|
| 157 | fi |
---|
| 158 | # Comprobamos que la imagen esta bien montandola |
---|
| 159 | mount -t squashfs -o loop ${IMGFILE["$3"]} $DIRMOUNT 1>/dev/null |
---|
| 160 | [ $? == 0 ] || exit $(ogRaiseError $OG_ERR_IMAGE "CACHE $4"; echo $?) |
---|
| 161 | umount $DIRMOUNT |
---|
| 162 | |
---|
| 163 | TIMEAUX6=$[SECONDS-TIME2] |
---|
| 164 | echo " $MSG_SCRIPTS_TIME_PARTIAL : $[TIMEAUX6/60]m $[TIMEAUX6%60]s" | tee -a $OGLOGSESSION $OGLOGFILE |
---|
| 165 | |
---|
| 166 | |
---|
| 167 | |
---|
| 168 | #resumen de la operacion |
---|
| 169 | IMGSIZE=$(ls -s `ogGetPath $3 $4.img`| cut -f1 -d" ") |
---|
| 170 | # TODO: Hay que cambiar la funcion ogGetImageInfo |
---|
| 171 | #IMGOS=$(ogGetImageInfo `ogGetPath $3 $4.img`) NO FUNCIONA Hay que modificar la funcion para las imagenes squash |
---|
| 172 | |
---|
| 173 | TIME=$[SECONDS-TIME1] |
---|
| 174 | echo "[100] $MSG_SCRIPTS_TIME_TOTAL $[TIME/60]m $[TIME%60]s" | tee -a $OGLOGSESSION $OGLOGFILE |
---|
| 175 | echo " FileSystem $PART with $NEWSIZEFS KB data created onto file-image as $4 and used $IMGSIZE KB acros DFS $ogprotocol " | tee -a $OGLOGSESSION $OGLOGFILE |
---|
| 176 | echo " Image-file $4 metada: $IMGOS " | tee -a $OGLOGSESSION $OGLOGFILE |
---|