1 | #!/bin/bash |
---|
2 | |
---|
3 | #/** |
---|
4 | #@file createImage |
---|
5 | #@brief Scirpt 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 | #@exception OG_ERR_REDUCEFS # 17 error al reducir sistema de archivos. |
---|
19 | #@exception OG_ERR_EXTENDFS # 18 Errror al expandir el sistema de archivos. |
---|
20 | #@note |
---|
21 | #@todo: que hacer, si el tamaño de la cache es sufciente, pero no tiene espacio libre |
---|
22 | #@todo: que hacer, si hay una imagen con igual nombre en la cache |
---|
23 | #@version 1.0 - control de errores para el ogAdmServer |
---|
24 | #@author |
---|
25 | #@date 2011-04-10 |
---|
26 | #@version 1.0.1 - Control de espacio requerido |
---|
27 | #@author Antonio J.Doblas Viso |
---|
28 | #@date 2011-05-10 |
---|
29 | #@version 1.0.2 - Separacion de log |
---|
30 | #@author Antonio J.Doblas Viso |
---|
31 | #@date 2011-08-4 |
---|
32 | #*/ ## |
---|
33 | |
---|
34 | # Test 1. crear una imagen en un REPO sin espacio libre. |
---|
35 | # test 2. crear una imagen en un REPO en modo solo lectura. |
---|
36 | # test 3. intentar crear una imagen en la cache de un equipo que no la disponga. |
---|
37 | # test 4. crear una imagen en la Cache sin espacio sufiente. |
---|
38 | # test 5. intentar crear una imagen, en la que no se puede reducir el FS. |
---|
39 | |
---|
40 | |
---|
41 | PROG="$(basename $0)" |
---|
42 | if [ $# -ne 4 ]; then |
---|
43 | ogRaiseError $OG_ERR_FORMAT "$MSG_FORMAT: $PROG ndisco nparticion REPO|CACHE imagen" |
---|
44 | exit $? |
---|
45 | fi |
---|
46 | |
---|
47 | TIME1=$SECONDS |
---|
48 | |
---|
49 | #Load engine configurator from engine.cfg file. |
---|
50 | #Carga el configurador del engine desde el fichero engine.cfg |
---|
51 | [ -z $OGENGINECONFIGURATE ] && source /opt/opengnsys/etc/engine.cfg |
---|
52 | |
---|
53 | # Valores por defecto en etc/engine.cfg |
---|
54 | #IMGPROG="partclone" |
---|
55 | #IMGCOMP="lzop" |
---|
56 | IMGEXT=${IMGEXT:-"img"} |
---|
57 | #IMGREDUCE="TRUE" |
---|
58 | |
---|
59 | # Clear temporary file used as log track by httpdlog |
---|
60 | # Limpia los ficheros temporales usados como log de seguimiento para httpdlog |
---|
61 | echo " " > $OGLOGSESSION; echo " " > $OGLOGCOMMAND; echo " " > ${OGLOGCOMMAND}.tmp |
---|
62 | |
---|
63 | ogEcho log session "[1] $MSG_SCRIPTS_START $0 $*" |
---|
64 | |
---|
65 | # Si el repositorio es CACHE comprobamos que exista |
---|
66 | if [ "${3^^}" == "CACHE" ]; then |
---|
67 | ! ogFindCache >/dev/null && exit $(ogRaiseError $OG_ERR_NOTCACHE "CACHE "; echo $?) |
---|
68 | fi |
---|
69 | |
---|
70 | # Obtener información de los parámetros de entrada. |
---|
71 | PART=$(ogDiskToDev "$1" "$2" 2>/dev/null) || exit $(ogRaiseError $OG_ERR_PARTITION "$1 $2"; echo $?) |
---|
72 | |
---|
73 | #Comprobamos acceso de escritura. |
---|
74 | DIRTEMP=$(date +%Y%m%d-%H%M%S) |
---|
75 | ogMakeDir $3 /$4$DIRTEMP 2>/dev/null || exit $(ogRaiseError $OG_ERR_NOTWRITE "$3"; echo $?) && ogDeleteTree $3 /$4$DIRTEMP |
---|
76 | |
---|
77 | IMGDIR=$(ogGetParentPath "$3" "/$4") |
---|
78 | # Si no existe, crear subdirectorio de la imagen. |
---|
79 | if [ $? != 0 ]; then |
---|
80 | ogEcho log session "[5] $MSG_HELP_ogMakeDir \"$3 $(dirname "$4")." |
---|
81 | ogMakeDir "$3" $(dirname "/$4") || exit $(ogRaiseError $OG_ERR_NOTWRITE "$3 /$4"; echo $?) |
---|
82 | IMGDIR=$(ogGetParentPath "$3" "/$4") || exit $(ogRaiseError $OG_ERR_NOTWRITE "$3 /$4"; echo $?) |
---|
83 | fi |
---|
84 | IMGFILE=$IMGDIR/$(basename "/$4").$IMGEXT |
---|
85 | |
---|
86 | # Renombrar el fichero de imagen si ya existe. |
---|
87 | if [ -f "$IMGFILE" ]; then |
---|
88 | ogEcho log session "[10] $MSG_SCRIPTS_FILE_RENAME \"$IMGFILE\" -> \"$IMGFILE.ant\"." |
---|
89 | mv "$IMGFILE" "$IMGFILE.ant" |
---|
90 | mv "$IMGFILE.torrent" "$IMGFILE.torrent.ant" 2>/dev/null |
---|
91 | mv "$IMGFILE.sum" "$IMGFILE.sum.ant" 2>/dev/null |
---|
92 | fi |
---|
93 | |
---|
94 | echo " " > $OGLOGCOMMAND |
---|
95 | # Borramos ficheros de paginacion y configuracion |
---|
96 | ogCleanOs $1 $2 |
---|
97 | |
---|
98 | #Comprobar espacio que requerira la imagen para ser almacenada |
---|
99 | read SIZEDATA SIZEREQUIRED ISENOUGHSPACE <<< $(ogGetSizeParameters $1 $2 "$3") |
---|
100 | |
---|
101 | ogEcho log session "[16] $PROG: $MSG_SCRIPTS_CREATE_SIZE $SIZEREQUIRED" |
---|
102 | [ "$ISENOUGHSPACE" == "TRUE" ] || exit $(ogRaiseError session $OG_ERR_CACHESIZE "$3"; echo $?) |
---|
103 | |
---|
104 | # Ejecuta script personalizado de preconfiguración del sistema operativo modelo. |
---|
105 | if which createImageCustomPre &>/dev/null; then |
---|
106 | createImageCustomPre "$1" "$2" "$3" "$4" |
---|
107 | fi |
---|
108 | |
---|
109 | # Comprobar consistencia del sistema de archivos. |
---|
110 | echo " " > $OGLOGCOMMAND |
---|
111 | SIZEFS=$(ogGetFsSize $1 $2) |
---|
112 | ogEcho log session "[20] $MSG_HELP_ogCheckFs $PART $SIZEFS (KB)" |
---|
113 | ogUnmount $1 $2 2>/dev/null |
---|
114 | ogCheckFs $1 $2 || exit $(ogRaiseError $OG_ERR_PARTITION "ogCheckFs $1 $2" && echo $?) |
---|
115 | |
---|
116 | # Evaluar variable de engine.cfg para reducir el sistema de archivos en la creacion |
---|
117 | if [ "$IMGREDUCE" == "TRUE" ] |
---|
118 | then |
---|
119 | ogEcho log session "[30]: $MSG_HELP_ogReduceFs" |
---|
120 | ogReduceFs $1 $2 &>> $OGLOGCOMMAND || exit $(ogRaiseError $OG_ERR_REDUCEFS "$1 $2"; echo $?) |
---|
121 | NEWSIZEFS=$(ogGetFsSize $1 $2) |
---|
122 | TIMEAUX=$[SECONDS-TIME1] |
---|
123 | ogEcho log session " $MSG_SCRIPTS_TIME_PARTIAL ( $NEWSIZEFS KB ) : $[TIMEAUX/60]m $[TIMEAUX%60]s" |
---|
124 | fi |
---|
125 | |
---|
126 | # Crear la imagen. |
---|
127 | echo " " > $OGLOGCOMMAND |
---|
128 | TIME2=$SECONDS |
---|
129 | ogEcho log session "[40] $MSG_HELP_ogCreateImage : ogCreateImage $1 $2 $3 $4 $IMGPROG $IMGCOMP" |
---|
130 | ogCreateImage "$1" "$2" "$3" "/$4" "$IMGPROG" "$IMGCOMP" &>> $OGLOGCOMMAND || exit $(ogRaiseError $OG_ERR_IMAGE "ogCreteImage"; echo $?) |
---|
131 | RESUMECREATEIMAGE=$(grep "Total Time:" $OGLOGCOMMAND) |
---|
132 | TIMEAUX2=$[SECONDS-TIME2] |
---|
133 | ogEcho log session " $RESUMECREATEIMAGE " |
---|
134 | ogEcho log session " $MSG_SCRIPTS_TIME_PARTIAL : $[TIMEAUX2/60]m $[TIMEAUX2%60]s" |
---|
135 | |
---|
136 | # Extender sistema de archivos |
---|
137 | TIME3=$SECONDS |
---|
138 | ogEcho log session "[90] Extender sistema de archivos." |
---|
139 | ogExtendFs $1 $2 || exit $(ogRaiseError $OG_ERR_EXTENDFS "$1 $2"; echo $?) |
---|
140 | SIZEFS2=$(ogGetFsSize $1 $2) |
---|
141 | TIMEAUX3=$[SECONDS-TIME3] |
---|
142 | ogEcho log session " $MSG_HELP_ogExtendFs $NEWSIZEFS -> $SIZEFS = $SIZEFS2: $[TIMEAUX3/60]m $[TIMEAUX3%60]s" |
---|
143 | |
---|
144 | #TODO que hacer si error al extender sistemade archivos |
---|
145 | |
---|
146 | #resumen de la operacion |
---|
147 | IMGSIZE=$(ls -s `ogGetPath $3 /$4.$IMGEXT`| cut -f1 -d" ") |
---|
148 | IMGOS=$(ogGetImageInfo `ogGetPath $3 /$4.$IMGEXT`) |
---|
149 | |
---|
150 | TIME=$[SECONDS-TIME1] |
---|
151 | ogEcho log session "[100] $MSG_SCRIPTS_TIME_TOTAL $[TIME/60]m $[TIME%60]s" |
---|
152 | ogEcho log session " FileSystem $PART with $NEWSIZEFS KB data created onto file-image as $4 and used $IMGSIZE KB across DFS $ogprotocol" |
---|
153 | ogEcho log session " Image-file $4 metada: $IMGOS" |
---|
154 | |
---|
155 | # Ejecuta scripts personalizado de postconfiguracion del sistema operativo modelo. |
---|
156 | if which createImageCustomPost &>/dev/null; then |
---|
157 | createImageCustomPost "$1" "$2" "$3" "$4" |
---|
158 | fi |
---|
159 | |
---|