source: client/shared/scripts/cloneRemoteFromMaster

qndtest
Last change on this file was 588d1eb, checked in by Irina Gómez <irinagomez@…>, 5 years ago

#802 #889 cloneRemoteFromMaster script in master, when computer has Efi active and operating system is Windows, copies boot loader folder from ESP to system partition.

  • Property mode set to 100755
File size: 14.4 KB
Line 
1#!/bin/bash
2
3#/**
4#         cloneremoteFromMaster
5#@brief   Restaura una particion o imagen sobre las particiones de equipos cliente remotos
6#@param 1 str_origen admite dirección IP del equipo Master.
7#@param 2 str_origen  admite int_disk    str_REPO|str_CACHE
8#@param 3 str_origen  admite int partorigen   stre_imagen
9#@param 4 str_sesion multicast|unicast
10#@param $5 int_disco_destino
11#@param $6 init_particion_destino
12#@param $7 str_tool_clone
13#@param $8 str_tool_compresor
14#@param ejemplo: cloneRemoteFromMaster 172.17.36.11 1 1 9000:full-duplex:239.194.17.36:70M:50:100 1 1 partclone lzop
15#@param ejemplo: cloneRemoteFromMaster 172.17.36.11 REPO /imagen1 9000:full-duplex:239.194.17.36:70M:50:100 1 1 partclone lzop
16#@param ejemplo: cloneRemoteFromMaster 172.17.36.11 CACHE /imagen1 9000:full-duplex:239.194.17.36:70M:50:100 1 1 partclone lzop
17#@return 
18#@exception OG_ERR_FORMAT     formato incorrecto.
19#@exception $OG_ERR_IMGSIZEPARTITION=30   #Imagen demasiado pequeña para ser clonada
20#@exception OG_ERR_REDUCEFS=17     #error al reducir sistema de archivos.
21#@exception OG_ERR_EXTENDFS=18     #errror al expandir el sistema de archivos.
22#@exception OG_ERR_UCASTSYNTAXT=50  # Error en la generación de sintaxis de transferenica unicast
23#@exception OG_ERR_UCASTSENDPARTITION=51  # Error en envio UNICAST de una particion
24#@exception OG_ERR_UCASTSENDFILE=52  # Error en envio UNICAST de un fichero
25#@exception OG_ERR_UCASTRECEIVERPARTITION=53  #Error en la recepcion UNICAST de una particion
26#@exception OG_ERR_UCASTRECEIVERFILE=54  #Error en la recepcion UNICAST de un fichero
27#@exception OG_ERR_MCASTSYNTAXT=55 # Error en la generacion de sintaxis de transferenica Multicast.
28#@exception OG_ERR_MCASTSENDFILE=56  # Error en envio MULTICAST de un fichero
29#@exception OG_ERR_MCASTRECEIVERFILE=57  #Error en la recepcion MULTICAST de un fichero
30#@exception OG_ERR_MCASTSENDPARTITION=58  # Error en envio MULTICAST de una particion
31#@exception OG_ERR_MCASTRECEIVERPARTITION=59  # Error en la recepcion MULTICAST de una particion
32#@exception OG_ERR_PROTOCOLJOINMASTER=60 # Error en la conexion de una sesion UNICAST|MULTICAST con el MASTER
33#@note   
34#@todo:
35#@version 0.9.1 - integración con OpenGnsys
36#@author  Antonio J. Doblas Viso. Universidad de Malaga.
37#@date   2009/03/17
38#@version 0.9.2 - adaptacion a OpenGnsys
39#@author  Antonio J. Doblas Viso. Universidad de Malaga.
40#@date    2010/07/27
41#@version 0.1.0 - gestion unicast
42#@author  Antonio J. Doblas Viso. Universidad de Malaga.
43#@date    2011/01/26
44#@version 1.0 - control de errores para el ogAdmServer
45#@author  Antonio J. Doblas Viso. Universidad de Malaga.
46#@date    2011/04/24
47#@version 1.0 - Uso de parted para controlar tamaño particion destino. Requiere Formateo FS previo -parted usa FS para el tamanyo".
48#@author  Antonio J. Doblas Viso. Universidad de Malaga.
49#@date    2011/04/24
50#@version 1.0.1 - Se elimina la operación de reducir la particion.
51#@author  Antonio J. Doblas Viso. Universidad de Malaga.
52#@date    2011/05/16
53#@version 1.0.3 - se integra con httpd-log.
54#@version 1.0.3 - Habilita el uso de la variable OGWINREDUCE=TRUE|TRUE para reducir el sistema de archivos a enviar
55#@author  Antonio J. Doblas Viso. Universidad de Malaga.
56#@date    2011/12/22
57#@version 1.0.6 - Uso de la funcion ogExecuteAndLog
58#@author  Antonio J. Doblas Viso. Universidad de Malaga.
59#@date    2012/02/12
60#@version 1.1.0.a - sesion multicast cliente puerto:master:0:0 (ticket #872)
61#@author  Antonio J. Doblas Viso
62#@date    2018/09/11
63#*/ ##
64#*/ ##
65
66#test 1. cliente sin particiones.    Detectado  TODO: crear estrucutras de particiones
67#test 2. cliente con particion mas pequeña. Detectado.
68#test 3. cleinte con particion destinio no formateado.  Detectado.
69#test 4. cliente con particion destino ocupado por el usuario pwd .  FALLO.
70#test 5. master sin origen particion.
71#test 6. master sin origen fichero.
72
73TIME1=$SECONDS
74PROG="$(basename $0)"
75
76trap "pkill faucet; exit 1" 0 1 2 3 6 9 14 15
77
78#AYUDA
79if [ $# -lt 1 -o "$1" == "help" ]; then
80        echo "cloneRemoteFromMaster ipmaster disk init [MULTICAST|UNICAST] session disk part tool comp"
81        exit 1
82fi
83
84#ERROR
85if [ $# -lt 6 ]; then
86    ogRaiseError session $OG_ERR_FORMAT "$MSG_FORMAT: $PROG ipMaster SOURCE_disco SOURCE_particion [MULTICAST|UNICAST] SESSION TARGET_disk TARGET_partition"
87    exit $?
88fi
89
90#Load engine configurator from engine.cfg file.
91#Carga el configurador del engine desde el fichero engine.cfg
92# Valores por defecto: #IMGPROG="partclone" ; #IMGCOMP="lzop" ; #IMGEXT="img" #IMGREDUCE="TRUE"
93[ -z $OGENGINECONFIGURATE ] && source /opt/opengnsys/etc/engine.cfg
94
95# Clear temporary file used as log track by httpdlog
96# Limpia los ficheros temporales usados como log de seguimiento para httpdlog
97echo " " > $OGLOGCOMMAND
98
99ogEcho log session "[1] $MSG_SCRIPTS_START $0 $*"
100
101# Procesar parámetros de entrada
102HOSTIP=`ogGetIpAddress`
103if [ -z "$HOSTIP" ] 
104then
105        source /tmp/net-eth*
106        HOSTIP=`ogGetIpAddress`
107fi
108MASTERIP="$1"
109PROTOCOL="$4"
110case "${PROTOCOL^^}" in
111        MULTICAST)
112                SESSIONMCASTSERVER=$5
113                SESSIONMCASTCLIENT=`echo $5 | awk -F: '{print $1}'`:$MASTERIP:0:0
114        ;;
115        UNICAST) 
116                SESSIONUCASTSERVER=$5
117                SESSIONUCASTCLIENT=`echo $5 | awk -F: '{print $1}'`:$MASTERIP
118        ;;
119        *)
120                exit $(ogRaiseError $OG_ERR_FORMAT "Protocolo $PROTOCOL no soportado en esta operacion"; echo $?)
121;;
122esac
123# contenedor destino (disco particion)
124DISKTARGET="$6"
125PARTTARGET="$7"
126#herramienta de compresión.
127TOOLCLONE="$8"
128COMPRESOR="$9"
129
130pkill faucet
131
132# Preparando Instrucción según sea Master o Client
133case $MASTERIP in
134        $HOSTIP)
135                ogEcho log session  "[1] Equipo Master preparando el origen de los datos a enviar"
136                ogGetPath $2 $3.img &>/dev/null; RC=$?; 
137                ogDiskToDev $2 $3 &>/dev/null; RETVAL=$?; 
138
139                if [ "$RC" == "0" ]; then 
140                        IMG=$(ogGetPath $2 $3.img); MODEMASTER=SENDFILE
141                elif [ "$RETVAL" == "0" ]; then
142                        DISKSOURCE=$2; PARTSOURCE=$3; MODEMASTER=SENDPARTITION
143                else
144                        ogRaiseError $OG_ERR_NOTFOUND "$2 $3"; exit $?
145                fi 
146                echo $MODEMASTER
147                # Preparando instrucción del Master segun $MODEMASTER{SENDPARTITION SENDFILE}                                   
148                case "$MODEMASTER" in
149                        SENDPARTITION)
150                                ogEcho log session "[5] Master en modo $MODEMASTER: informacion inicial a los clientes-slaves"
151                                if ps aux | grep -v grep | grep "faucet 4000" 
152                                then
153                                        ogRaiseError $OG_ERR_NOTFOUND "MASTER: puerto en uso: 118"; exit $?     
154                                else
155                                        faucet 4000 --out echo "WORKING" &     
156                                fi                             
157                                ogEcho log session "[10]: Desmontando DISK:$DISKSOURCE PARTITION:$PARTSOURCE"
158                                ogUnmount $2 $3 ||  exit $(ogRaiseError $OG_ERR_LOCKED "no se puede desmontar $2 $3  l124"; echo $?)
159                                #Obtener tamaño de la partición.
160                                SIZE=$(ogGetPartitionSize $2 $3)  || exit $(ogRaiseError $OG_ERR_REDUCEFS "Error al detectar tamaño partcion $2 $3  l127"; echo $?)
161                               
162                                # Si es UEFI copio el cargador de arranque a la partición
163                                OSTYPE="$(ogGetOsType $2 $3)"
164                                if ogIsEfiActive && [ "$OSTYPE" == "Windows" ]; then
165                                        ogEcho log session "[12] $MSG_HELP_ogCopyEfiBootLoader"
166                                        ogCopyEfiBootLoader $2 $3
167                                fi
168
169                                if [ "$OGWINREDUCE" == "TRUE" ]
170                                then 
171                                        ogEcho log session "[15]: Calculando la reduccion del sistema de archivos DISK:$DISKSOURCE PARTITION:$PARTSOURCE SIZE:$SIZE"
172                                        ogReduceFs $2 $3 || exit $(ogRaiseError $OG_ERR_REDUCEFS "Error al reducir el FS $2 $3  l129"; echo $?)
173                                fi
174                                       
175                                REDSIZE=$(ogGetFsSize $2 $3) || exit $(ogRaiseError $OG_ERR_REDUCEFS "Error al detectar el nuevo FS $2 $3  l130"; echo $?)     
176                                TIMEAUX=$[SECONDS-TIME1]
177                                ogEcho log session "[20]: Preparada para enviar el sistema de archivos: Tamaño datos: $REDSIZE  origne: $DISKSOURCE $PARTSOURCE"
178                                ogEcho log session "      tiempo de reducción del sistema de archivos: $[TIMEAUX/60]m $[TIMEAUX%60]s"
179                               
180                                #if [ $REDSIZE -lt $SIZE ]; then
181                        #               echo "[24] Redimensionar partición a $REDSIZE KB."
182                        #               ogSetPartitionSize $2 $3 $REDSIZE
183                                #fi
184
185                                ogEcho log session "[25] Master en Modo $MODEMASTER: informacion de transferencia a los clientes-slaves $PROTOCOL $TOOLCLONE $COMPRESOR $REDSIZE"
186                                pkill faucet
187                                if ps aux | grep -v grep | grep "faucet 4000" 
188                                then
189                                        ogRaiseError $OG_ERR_NOTFOUND "MASTER: puerto en uso: 140"; exit $?     
190                                else
191                                        faucet 4000 --out echo "READY $TOOLCLONE $COMPRESOR $REDSIZE" &
192                                fi
193                                case "${PROTOCOL^^}" in
194                                        MULTICAST)
195                                                ogEcho log session "[29] Transferencia Multicast: ogMcastSendPartition $DISKSOURCE $PARTSOURCE $SESSIONMCASTSERVER $TOOLCLONE $COMPRESOR "
196                                                ogExecAndLog session "ogMcastSendPartition" "$DISKSOURCE" "$PARTSOURCE" "$SESSIONMCASTSERVER" "$TOOLCLONE" "$COMPRESOR" || RETVAL=$?
197                                        ;;
198                                        UNICAST)
199                                                sleep 60
200                                                ogEcho log session "[29] Transferencia Unicast: ogUcastSendPartition $DISKSOURCE $PARTSOURCE $SESSIONUCASTSERVER $TOOLCLONE $COMPRESOR"
201                                                ogExecAndLog session "ogUcastSendPartition" "$DISKSOURCE" "$PARTSOURCE" "$SESSIONUCASTSERVER" "$TOOLCLONE" "$COMPRESOR" || RETVAL=$?
202                                        ;;
203                                esac
204                               
205                                #if [ $REDSIZE -lt $SIZE ]; then
206                                #       echo "[85] Redimensionar partición a $SIZE KB."
207                                #       ogSetPartitionSize $2 $3 $SIZE
208                                        ogEcho log session "[90] Extender sistema de archivos."
209                                        ogExtendFs $2 $3
210                                #fi
211                                pkill faucet
212                                if [ $RETVAL == 0 ]
213                                then 
214                                        exit 0
215                                else
216                                    exit $(ogRaiseError $OG_ERR_MCASTSENDPARTITION "Error al enviar la particion  $2 $3 con protocolo $PROTOCOL l167"; echo $?)
217                                fi
218
219                        ;;
220                        SENDFILE)                               
221                                ogEcho log session "[5] Master en modo  $MODEMASTER: informacion inicial a los clientes-slaves"
222                                ogEcho log session "[10]: Preparando Imagen: $IMG"
223                                TOOLCLONE=$(ogGetImageProgram $2 $3) || exit $(ogRaiseError $OG_ERR_LOCKED "TOOLCLONE no detectado l174"; echo $?)
224                                COMPRESOR=$(ogGetImageCompressor $2 $3) || exit $(ogRaiseError $OG_ERR_LOCKED "COMPRESOR NO DETECTADO l175"; echo $?)
225                                REDSIZE=$(ogGetImageSize $2 $3) || exit $(ogRaiseError $OG_ERR_LOCKED "REDSIZE NO DETECTADO l176"; echo $?)
226                                ogEcho log session "[25] Master en Modo $MODEMASTER"
227                                ogEcho log "Informacion de transferencia a los clientes-slaves $PROTOCOL $TOOLCLONE $COMPRESOR $REDSIZE"
228                                if ps aux | grep -v grep | grep "faucet 4000" 
229                                then
230                                        ogRaiseError $OG_ERR_NOTFOUND "MASTER: puerto en uso: 140"; exit $?     
231                                else
232                                        faucet 4000 --out echo "READY $TOOLCLONE $COMPRESOR $REDSIZE" &
233                                fi
234                                case "${PROTOCOL^^}" in
235                                        MULTICAST)
236                                                echo "[29] ogMcastSendFile $2 $3.img $SESSIONMCASTSERVER "
237                                                ogMcastSendFile  $2 $3.img $SESSIONMCASTSERVER  || RETVAL=$?
238                                                ;;
239                                        UNICAST)
240                                                sleep 60
241                                                echo "[29] ogUcastSendFile $2 $3.img $SESSIONUCASTSERVER"
242                                                ogUcastSendFile $2 $3.img $SESSIONUCASTSERVER || RETVAL=$?
243                                        ;;
244                                esac                                                   
245                                pkill faucet   
246                                if [ $RETVAL == 0 ]
247                                then 
248                                        exit 0
249                                else
250                                        exit $(ogRaiseError $OG_ERR_MCASTSENDFILE "Error al enviar la image $2 $3.img con protocolo $PROTOCOL l200"; echo $?)
251                                fi
252                        ;;
253                esac
254                # FIN Preparando instrucción del Master segun $MODEMASTER{SENDPARTITION SENDFILE}
255        ;;
256        *)     
257                ogEcho log session "[1] Equipo -Client- preparando para recibir datos $PROTOCOL"
258                ogDiskToDev $DISKTARGET $PARTTARGET &>/dev/null || exit $(ogRaiseError $OG_ERR_LOCKED "El cliente no tiene esas particiones $DISKTARGET $PARTTARGET l211"; echo $?)     
259                # TODO: si el cliente no está en la lista de clientes UNICAST salir.
260                case "${PROTOCOL^^}" in
261                        UNICAST)
262                                echo "$SESSIONUCASTSERVER" | grep $HOSTIP || exit $(ogRaiseError $OG_ERR_PROTOCOLJOINMASTER "Este cliente no pertence a la sesion UNICAST l214"; echo $?)
263                        ;;
264                esac   
265                ogEcho log session "[2] Desmontando particion destino"
266                ogUnmount $DISKTARGET $PARTTARGET || exit $(ogRaiseError $OG_ERR_LOCKED "no se puede desmontar la particion destino $2 $3  l218"; echo $?)
267                ogEcho log session  "[25] Buscando informacion extra sobre la clonacion con el master $MASTERIP: "
268                sleep 10
269                TIMEWAITMASTER=120
270                TIMEWAITING=0
271                GETINFO="NONE"
272                while [ "${GETINFO}" != "OK" ]
273                do
274                        INFOEXTRA=`hose $MASTERIP 4000 --in cat 2>/dev/null`
275                        sleep 10; echo -n "."
276                        #echo comienza el timeout $TIMEWAITMASTER para abortar
277                        [ -z "$INFOEXTRA" ] && let TIMEWAITMASTER=$TIMEWAITMASTER-10
278                        [ "$TIMEWAITMASTER" -gt "0" ] || exit $(ogRaiseError $OG_ERR_PROTOCOLJOINMASTER " l230 "; echo $?)
279                        #Si primer parametro desde el server es READY, salimos del bucle
280                        GETINFO=$(echo $INFOEXTRA | awk '{print $1}')
281                        [ "$GETINFO" == "READY" ] && GETINFO="OK"
282                done           
283                echo $INFOEXTRA
284                TOOLCLONE=$(echo $INFOEXTRA | awk '{print $2}')
285                COMPRESOR=$(echo $INFOEXTRA | awk '{print $3}')
286                SIZEIMAGE=$(echo $INFOEXTRA | awk '{print $4}')
287                ogEcho log "$INFOEXTRA = herramienta= $TOOLCLONE  compresor= $COMPRESOR size= $SIZEIMAGE"
288                ogMount $DISKTARGET $PARTTARGET || ogFormat $DISKTARGET $PARTTARGET
289                #SIZEPARTTARGET=$(parted `ogDiskToDev $DISKTARGET $PARTTARGET` unit kB print | grep Disk | awk -F" " '{print $3}' | tr -d kB);
290                SIZEPARTTARGET=$(ogGetPartitionSize $DISKTARGET $PARTTARGET )
291                ogEcho log session  "[28] comprobando que el tamaño de la imagen $SIZEIMAGE es menor que el de la particion destino $SIZEPARTTARGET"
292                # comprobamos que el tamaño de a imagen es menor que la del cliente.
293                if [ "$SIZEIMAGE" -lt "$SIZEPARTTARGET" ]
294                then                           
295                        ogEcho log session "[30] Iniciando Cliente $PROTOCOL "
296                                case "${PROTOCOL^^}" in
297                                        MULTICAST)
298                                                ogEcho log session  "ogMcastReceiverPartition $DISKTARGET $PARTTARGET $SESSIONMCASTCLIENT $TOOLCLONE $COMPRESOR"
299                                                ogExecAndLog command "ogMcastReceiverPartition" "$DISKTARGET" "$PARTTARGET" "$SESSIONMCASTCLIENT" "$TOOLCLONE" "$COMPRESOR"  || exit $(ogRaiseError $OG_ERR_MCASTRECEIVERPARTITION " l251 "; echo $?)
300                                        ;;
301                                        UNICAST)
302                                                ogEcho log session "ogUcastReceiverPartition $DISKTARGET $PARTTARGET $SESSIONUCASTCLIENT $TOOLCLONE $COMPRESOR"
303                                                ogExecAndLog command "ogUcastReceiverPartition" "$DISKTARGET" "$PARTTARGET" "$SESSIONUCASTCLIENT" "$TOOLCLONE" "$COMPRESOR" || exit $(ogRaiseError $OG_ERR_UCASTRECEIVERPARTITION " l230 "; echo $?)
304                                        ;;
305                                esac                                           
306                        if which configureOsCustom &>/dev/null; then
307                                ogEcho log session "[90] configureOsCustom $DISKTARGET $PARTTARGET"
308                                # Si $2 = num_disk las varibles REPO IMGNAME estan vacias
309                                ! [[  $2 =~ ^[0-9]+$ ]] && REPO="$2" && IMGNAME="$3"
310                                configureOsCustom $DISKTARGET $PARTTARGET $REPO $IMGNAME
311                        else
312                                ogEcho log session "[90] $MSG_SCRIPTS_OS_CONFIGURE $DISKTARGET $PARTTARGET"
313                                configureOs $DISKTARGET $PARTTARGET                     
314                        fi
315
316                else
317                    # Si el tamaño de los datos recibidos es más grande que la particion destino
318                        ogRaiseError $OG_ERR_IMGSIZEPARTITION "ERROR tamanio particion= $SIZEPARTTARGET  menor que la imagen= $SIZEIMAGE"; exit $?
319                fi
320        ;;
321esac
322TIME=$[SECONDS-TIME1]
323ogEcho log session "[100] Duración de la operación $[TIME/60]m $[TIME%60]s"
324
Note: See TracBrowser for help on using the repository browser.