1 | #@file ogfunctions.lib |
---|
2 | #@brief Librería o clase para la gestion del sistema operativo de los clientes OpenGnsys |
---|
3 | #@class client |
---|
4 | #@brief Librería o clase para la gestion del sistema operativo de los clientes OpenGnsys |
---|
5 | #@version 0.91 |
---|
6 | #@warning License: GNU GPLv3+ |
---|
7 | |
---|
8 | #/** |
---|
9 | # ogExportKernelParameters |
---|
10 | #@brief Exporta los parametros pasados al kernel |
---|
11 | #@param |
---|
12 | #@return |
---|
13 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
14 | #@version 0.7 - |
---|
15 | #@author Antonio J. Doblas. Universidad de Malaga. |
---|
16 | #@date 2010/05/24 |
---|
17 | #*/ ## |
---|
18 | ogExportKernelParameters () |
---|
19 | { |
---|
20 | GLOBAL="cat /proc/cmdline" |
---|
21 | for i in `${GLOBAL}` |
---|
22 | do |
---|
23 | echo $i | grep "=" > /dev/null && export $i |
---|
24 | done |
---|
25 | return 0 |
---|
26 | } |
---|
27 | |
---|
28 | #/** |
---|
29 | # ogExportVarEnvironment |
---|
30 | #@brief Exporta las variables usadas en el proceso de inicio OpenGnsys y las almacena en /tmp |
---|
31 | #@param |
---|
32 | #@return |
---|
33 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
34 | #@version 0.9 |
---|
35 | #@author Antonio J. Doblas. Universidad de Malaga. |
---|
36 | #@date 2011/05/24 |
---|
37 | #*/ ## |
---|
38 | ogExportVarEnvironment () |
---|
39 | { |
---|
40 | export CFGINITRD="/tmp/initrd.cfg" |
---|
41 | OGPROTOCOL="${ogprotocol:-smb}" |
---|
42 | case "$OGPROTOCOL" in |
---|
43 | nfs|NFS) |
---|
44 | export SRCOGLIVE="/var/lib/tftpboot" && echo "SRCOGLIVE=$SRCOGLIVE" >> $CFGINITRD |
---|
45 | export SRCOGSHARE="/opt/opengnsys/client" && echo "SRCOGSHARE=$SRCOGSHARE" >> $CFGINITRD |
---|
46 | export SRCOGLOG="/opt/opengnsys/log/clients" && echo "SRCOGLOG=$SRCOGLOG" >> $CFGINITRD |
---|
47 | export SRCOGIMAGES="/opt/opengnsys/images" && echo "SRCOGIMAGES=$SRCOGIMAGES" >> $CFGINITRD |
---|
48 | ;; |
---|
49 | smb|SMB|cifs|CIFS|samba|SAMBA) |
---|
50 | export OPTIONS=" -o user=opengnsys,pass=og" |
---|
51 | export SRCOGLIVE="tftpboot" && echo "SRCOGLIVE=$SRCOGLIVE" >> $CFGINITRD |
---|
52 | export SRCOGSHARE="ogclient" && echo "SRCOGSHARE=$SRCOGSHARE" >> $CFGINITRD |
---|
53 | export SRCOGLOG="oglog" && echo "SRCOGLOG=$SRCOGLOG" >> $CFGINITRD |
---|
54 | export SRCOGIMAGES="ogimages" && echo "SRCOGIMAGES=$SRCOGIMAGES" >> $CFGINITRD |
---|
55 | ;; |
---|
56 | local|LOCAL) |
---|
57 | export SRCOGLIVE="local" |
---|
58 | ;; |
---|
59 | esac |
---|
60 | #punto de acceso al boot-tools live |
---|
61 | export DSTOGLIVE="/opt/oglive/tftpboot" |
---|
62 | #punto de montaje para unionfs |
---|
63 | export OGLIVERAMFS="/opt/oglive/ramfs" && echo "OGLIVERAMFS=$OGLIVERAMFS" >> $CFGINITRD |
---|
64 | #punto de montaje donde se accede al 2nd FS mediante loop |
---|
65 | export OGLIVEROOTFS="/opt/oglive/rootfs" && echo "OGLIVEROOTFS=$OGLIVEROOTFS" >> $CFGINITRD |
---|
66 | #punto de union entre LOCALROOTIMG y LOCALROOTRAM |
---|
67 | export OGLIVEUNIONFS="/opt/oglive/unionfs" && echo "OGLIVEUNIONFS=$OGLIVEUNIONFS" >> $CFGINITRD |
---|
68 | #etiquta para los dispositivos offline |
---|
69 | export OGLIVELABEL="ogClient" |
---|
70 | |
---|
71 | #echo "puntos de montajes para los demas accesos" |
---|
72 | #echo "acceso al client, engine, scritps, interfaz" |
---|
73 | export DSTOGSHARE="/opt/opengnsys" && echo "DSTOGSHARE=$DSTOGSHARE" >> $CFGINITRD |
---|
74 | export DSTOGLOG="/opt/opengnsys/log" && echo "DSTOGLOG=$DSTOGLOG" >> $CFGINITRD |
---|
75 | export DSTOGIMAGES="/opt/opengnsys/images" && echo "DSTOGIMAGES=$DSTOGIMAGES" >> $CFGINITRD |
---|
76 | |
---|
77 | ##INFORMACION DE OTRAS VARIABLES OBTENDIAS EN OTRAS FUNCIONES ogConfigureNetwork. |
---|
78 | #DEVICE |
---|
79 | #IPV4DDR |
---|
80 | #IPV4BROADCAST |
---|
81 | #IPV4NETMASK |
---|
82 | #IPV4GATEWAY |
---|
83 | #HOSTNAME |
---|
84 | #INFORMACION de otras variasbles obteneidas desde ogGetROOTSERVER |
---|
85 | #ROOTSERVER si ip=dhcp -> ROOTSERVER=NEXT-SERVER; si ip=host:rootserver:gw:mask:hostname:interfaz -> ROOTSERVER=rootserver |
---|
86 | #BOOTIF -> si el gestor remoto es pxelinux.0 y se añade una linea más tipo "IPAPPEND 2" esta variable tendrá la mac de la interfaz. |
---|
87 | #$OGSERVERLIVE |
---|
88 | #$OGSERVERSHARE |
---|
89 | #$OGSERVERLOG |
---|
90 | #$OGSERVERIMAGES |
---|
91 | return 0 |
---|
92 | } |
---|
93 | |
---|
94 | |
---|
95 | #/** |
---|
96 | # ogConfigureRamfs |
---|
97 | #@brief Configura el initrd para adaptarlo al sistema raiz. |
---|
98 | #@param |
---|
99 | #@return |
---|
100 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
101 | #@version 0.9 |
---|
102 | #@author Antonio J. Doblas. Universidad de Malaga. |
---|
103 | #@date 2010/05/24 |
---|
104 | #*/ ## |
---|
105 | ogConfigureRamfs () |
---|
106 | { |
---|
107 | mkdir -p $DSTOGLIVE |
---|
108 | mkdir -p $OGLIVERAMFS |
---|
109 | mkdir -p $OGLIVEROOTFS |
---|
110 | mkdir -p $OGLIVEUNIONFS |
---|
111 | |
---|
112 | touch /etc/fstab |
---|
113 | } |
---|
114 | |
---|
115 | |
---|
116 | #/** |
---|
117 | # ogLoadNetModule |
---|
118 | #@brief Carga en un demerminado modulo de red, requiere compilación previo del modulo |
---|
119 | #@param |
---|
120 | #@return |
---|
121 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
122 | #@version 0.9 |
---|
123 | #@author Antonio J. Doblas. Universidad de Malaga. |
---|
124 | #@date 2010/05/24 |
---|
125 | #*/ ## |
---|
126 | ogLoadNetModule () |
---|
127 | { |
---|
128 | if [ -n "$ognetmodule" ] |
---|
129 | then |
---|
130 | echo "Cargando modulo de red $netmodule" |
---|
131 | insmod `find /lib/modules/ -name ${netmodule}*` |
---|
132 | fi |
---|
133 | } |
---|
134 | |
---|
135 | |
---|
136 | #/** |
---|
137 | # ogPostConfigureFS |
---|
138 | #@brief Configura el sistema raiz, para independizarlo entre los clientes. |
---|
139 | #@param |
---|
140 | #@return |
---|
141 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
142 | #@version 0.9 |
---|
143 | #@author Antonio J. Doblas. Universidad de Malaga. |
---|
144 | #@date 2010/05/24 |
---|
145 | #*/ ## |
---|
146 | ogPostConfigureFS() |
---|
147 | { |
---|
148 | # configuramos el /etc/hostname. |
---|
149 | echo $HOSTNAME > /etc/hostname |
---|
150 | |
---|
151 | #configuramos el /etc/hosts |
---|
152 | echo "127.0.0.1 localhost" > /etc/hosts |
---|
153 | echo "$IPV4ADDR $HOSTNAME" >> /etc/hosts |
---|
154 | |
---|
155 | #configuramos el host.conf |
---|
156 | echo "order hosts,bind" > /etc/host.conf |
---|
157 | echo "multi on" >> /etc/host.conf |
---|
158 | |
---|
159 | #configuramos el dns |
---|
160 | echo "nameserver $ogdns" > /etc/resolv.conf |
---|
161 | |
---|
162 | |
---|
163 | # configuramos el /etc/networks |
---|
164 | #read -e NETIP NETDEFAULT <<<$(route -n | grep eth0 | awk -F" " '{print $1}') |
---|
165 | NETIP=$(route -n | grep eth0 | awk -F" " '{print $1}') && NETIP=$(echo $NETIP | cut -f1 -d" ") |
---|
166 | echo "default 0.0.0.0" > /etc/networks |
---|
167 | echo "loopback 127.0.0.0" >> /etc/networks |
---|
168 | echo "link-local 169.254.0.0" >> /etc/networks |
---|
169 | echo "localnet $NETIP" >> /etc/networks |
---|
170 | #route |
---|
171 | |
---|
172 | #echo "ogLive1.0.2" > /etc/debian_chroot |
---|
173 | |
---|
174 | #enlace si iniciamos desde ogprotocolo=local { cdrom, usb, cache } . |
---|
175 | # monta el raiz del dispositivo local en /opt/og2fs/tftpboot - acceso al fichero .sqfs |
---|
176 | # y monta el sistema root sqfs en /opt/og2fs/2ndfs |
---|
177 | [ "$LOCALMEDIA" == "CACHE" ] && ln -s $DSTOGLIVE /opt/opengnsys/cache |
---|
178 | [ "$ogprotocol" == "local" ] && ln -s ${OGLIVEROOTFS}/opt/opengnsys/* /opt/opengnsys/ |
---|
179 | |
---|
180 | #Montamos un directorio temporal para el apt-get |
---|
181 | mount tmpfs /var/cache/apt/archives -t tmpfs -o size=15M |
---|
182 | mkdir -p /var/cache/apt/archives/partial |
---|
183 | |
---|
184 | } |
---|
185 | |
---|
186 | |
---|
187 | #/** |
---|
188 | # ogGetROOTSERVER |
---|
189 | #@brief Determina los puntos de accesos a los distintos recursos. |
---|
190 | #Requiere ogConfigureNetworking. |
---|
191 | #Exporta ROOTSERVER |
---|
192 | # si la red ha sido configurada con dhcp el valor de ROOTSERVER será el valor de next-server del dhcp |
---|
193 | # si la red ha sido configurada con el parametro de kernel ip, será el segundo valor. |
---|
194 | ## ip=iphost:ipnext-server:ipgateway:netmask:hostname:iface:none |
---|
195 | ## ip=172.17.36.21:62.36.225.150:172.17.36.254:255.255.255.0:prueba1:eth0:none |
---|
196 | #@param |
---|
197 | #@return |
---|
198 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
199 | #@version 0.9 |
---|
200 | #@author Antonio J. Doblas. Universidad de Malaga. |
---|
201 | #@date 2010/05/24 |
---|
202 | #*/ ## |
---|
203 | ogGetROOTSERVER () |
---|
204 | { |
---|
205 | # get nfs root from dhcp |
---|
206 | if [ "x${NFSROOT}" = "xauto" ]; then |
---|
207 | # check if server ip is part of dhcp root-path |
---|
208 | if [ "${ROOTPATH#*:}" = "${ROOTPATH}" ]; then |
---|
209 | NFSROOT=${ROOTSERVER}:${ROOTPATH} |
---|
210 | else |
---|
211 | NFSROOT=${ROOTPATH} |
---|
212 | fi |
---|
213 | |
---|
214 | # nfsroot=[<server-ip>:]<root-dir>[,<nfs-options>] |
---|
215 | elif [ -n "${NFSROOT}" ]; then |
---|
216 | # nfs options are an optional arg |
---|
217 | if [ "${NFSROOT#*,}" != "${NFSROOT}" ]; then |
---|
218 | NFSOPTS="-o ${NFSROOT#*,}" |
---|
219 | fi |
---|
220 | NFSROOT=${NFSROOT%%,*} |
---|
221 | if [ "${NFSROOT#*:}" = "$NFSROOT" ]; then |
---|
222 | NFSROOT=${ROOTSERVER}:${NFSROOT} |
---|
223 | fi |
---|
224 | fi |
---|
225 | export ROOTSERVER |
---|
226 | echo "ROOTSERVER=$ROOTSERVER" >> $CFGINITRD |
---|
227 | |
---|
228 | #si oglive no oglive=R |
---|
229 | export OGSERVERIMAGES="${ogrepo:-$ROOTSERVER}" && echo "OGSERVERIMAGES=$OGSERVERIMAGES" >> $CFGINITRD |
---|
230 | export OGSERVERSHARE="${ogshare:-$ROOTSERVER}" && echo "OGSERVERSHARE=$OGSERVERSHARE" >> $CFGINITRD |
---|
231 | export OGSERVERLOG="${oglog:-$ROOTSERVER}" && echo "OGSERVERLOG=$OGSERVERLOG" >> $CFGINITRD |
---|
232 | export OGSERVERLIVE="${oglive:-$OGSERVERIMAGES}" && echo "OGSERVERLIVE=$OGSERVERLIVE" >> $CFGINITRD |
---|
233 | |
---|
234 | return 0 |
---|
235 | } |
---|
236 | |
---|
237 | |
---|
238 | |
---|
239 | # ogUpdateInitrd |
---|
240 | #@brief Actualiza el intird de la cache desde el servidor. Si el arranque ha disdo desde cache, compueba desde el servidor nueva version del initird. |
---|
241 | #@param1 |
---|
242 | #@return |
---|
243 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
244 | #@version 0.9 |
---|
245 | #@author Antonio J. Doblas. Universidad de Malaga. |
---|
246 | #@date 2011/05/24 |
---|
247 | #*/ ## |
---|
248 | |
---|
249 | ogUpdateInitrd () |
---|
250 | { |
---|
251 | cd /tmp |
---|
252 | mkdir /tmp/cache |
---|
253 | TYPE=$(blkid | grep CACHE | awk -F"TYPE=" '{print $2}' | tr -d \") |
---|
254 | mount -t $TYPE LABEL=CACHE /tmp/cache || return |
---|
255 | mkdir /tmp/cache/boot |
---|
256 | |
---|
257 | |
---|
258 | # comparamos los del server |
---|
259 | busybox tftp -g -r ogvmlinuz.sum $ROOTSERVER |
---|
260 | busybox tftp -g -r oginitrd.img.sum $ROOTSERVER |
---|
261 | SERVERVMLINUZ=`cat ogvmlinuz.sum` |
---|
262 | SERVERINITRD=`cat oginitrd.img.sum` |
---|
263 | |
---|
264 | |
---|
265 | #comparamos los de la cache |
---|
266 | CACHEVMLINUZ=`cat /tmp/cache/boot/ogvmlinuz.sum` |
---|
267 | CACHEINITRD=`cat /tmp/cache/boot/oginitrd.img.sum` |
---|
268 | |
---|
269 | echo "MD5 on SERVER: $SERVERVMLINUZ $SERVERINITRD" |
---|
270 | echo "MD5 on CACHE: $CACHEVMLINUZ $CACHEINITRD" |
---|
271 | |
---|
272 | cd /tmp/cache/boot |
---|
273 | |
---|
274 | if [ "$CACHEVMLINUZ" != "$SERVERVMLINUZ" ] |
---|
275 | then |
---|
276 | echo "ogvmlinuz updating" |
---|
277 | busybox tftp -g -r ogvmlinuz $ROOTSERVER |
---|
278 | busybox tftp -g -r ogvmlinuz.sum $ROOTSERVER |
---|
279 | DOREBOOT=true |
---|
280 | fi |
---|
281 | if [ "$CACHEINITRD" != "$SERVERINITRD" ] |
---|
282 | then |
---|
283 | echo "oginitrd updating" |
---|
284 | busybox tftp -g -r oginitrd.img $ROOTSERVER |
---|
285 | busybox tftp -g -r oginitrd.img.sum $ROOTSERVER |
---|
286 | DOREBOOT=true |
---|
287 | fi |
---|
288 | |
---|
289 | cd /; umount /tmp/cache |
---|
290 | |
---|
291 | [ "$DOREBOOT" == "true" ] && busybox reboot -f |
---|
292 | |
---|
293 | } |
---|
294 | |
---|
295 | #/** |
---|
296 | # ogConnect |
---|
297 | #@brief Conecta con los recursos necesarios para opengnsys |
---|
298 | #@param1 ip del servidor TODO:dns |
---|
299 | #@param2 protocolo |
---|
300 | #@param3 punto de acceso remoto |
---|
301 | #@param4 punto de montaje local |
---|
302 | #@param5 acceso de lectura tipo ",ro" |
---|
303 | #@return |
---|
304 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
305 | #@version 0.9 |
---|
306 | #@author Antonio J. Doblas. Universidad de Malaga. |
---|
307 | #@date 2011/05/24 |
---|
308 | #*/ ## |
---|
309 | |
---|
310 | ogConnect () |
---|
311 | { |
---|
312 | SERVER=$1 |
---|
313 | PROTOCOL=$2 |
---|
314 | SRC=$3 |
---|
315 | DST=$4 |
---|
316 | READONLY=$5 |
---|
317 | |
---|
318 | case "$PROTOCOL" in |
---|
319 | nfs) |
---|
320 | nfsmount ${SERVER}:${SRC} ${DST} -o nolock${READONLY} 2> /dev/null || mount.nfs ${SERVER}:${SRC} ${DST} -o nolock${READONLY} |
---|
321 | ;; |
---|
322 | smb) |
---|
323 | mount.cifs //${SERVER}/${SRC} ${DST} ${OPTIONS}${READONLY} |
---|
324 | ;; |
---|
325 | esac |
---|
326 | } |
---|
327 | |
---|
328 | |
---|
329 | #/** |
---|
330 | # ogConnectOgLive |
---|
331 | #@brief Conecta con el recurso para usar el sistema raiz externo, remoto o local |
---|
332 | #@param1 |
---|
333 | #@return |
---|
334 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
335 | #@version 0.9 |
---|
336 | #@author Antonio J. Doblas. Universidad de Malaga. |
---|
337 | #@date 2011/05/24 |
---|
338 | ogConnectOgLive () |
---|
339 | { |
---|
340 | # Si ogprotocol=local, la funcion ogExportVar => SRCOGLIVE=local |
---|
341 | if [ "$SRCOGLIVE" == "local" ] |
---|
342 | then |
---|
343 | echo "Montar imagen del sistema root desde dispositivo local" |
---|
344 | for i in $(blkid /dev/s* | grep $OGLIVELABEL | awk -F: '{print $2}' | tr -d \"); do export $i; done |
---|
345 | # si local usb| cd con partcion es identificada como label $OGLIVELABEL |
---|
346 | mount -t $TYPE LABEL=$OGLIVELABEL $DSTOGLIVE |
---|
347 | if [ $? != 0 ] |
---|
348 | then |
---|
349 | # Si local es particion CACHE es identificada como CACHE |
---|
350 | mount LABEL=CACHE $DSTOGLIVE |
---|
351 | export LOCALMEDIA=CACHE |
---|
352 | fi |
---|
353 | else |
---|
354 | # Si ogprotocol es remoto. TODO en smb rw y en nfs ro?? |
---|
355 | ogConnect $OGSERVERLIVE $OGPROTOCOL $SRCOGLIVE $DSTOGLIVE |
---|
356 | fi |
---|
357 | # Si el montaje ha sido correcto, tanto en local como en remoto. Procedemos con la union |
---|
358 | ogMergeLive |
---|
359 | } |
---|
360 | |
---|
361 | |
---|
362 | #/** |
---|
363 | # ogMergeLive |
---|
364 | #@brief Metafuncion para fusionar el initrd con el sistema raiz. |
---|
365 | #@param1 |
---|
366 | #@return |
---|
367 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
368 | #@version 0.9 |
---|
369 | #@author Antonio J. Doblas. Universidad de Malaga. |
---|
370 | #@date 2011/05/24 |
---|
371 | ogMergeLive() |
---|
372 | { |
---|
373 | #Si existe en el punto de acceso del del oglive el fichero ogclient.sqfs |
---|
374 | if [ -f $DSTOGLIVE/ogclient/ogclient.sqfs ] |
---|
375 | then |
---|
376 | cat /proc/mounts > /tmp/mtab.preunion |
---|
377 | if [ "$og2nd" == "img" ] |
---|
378 | then |
---|
379 | #Montamos el ROOTFS tipo img, para desarrolladores |
---|
380 | #TODO: comprobar que se tiene acceso de escritura |
---|
381 | losetup /dev/loop0 $DSTOGLIVE/ogclient/ogclient.img -o 32256 |
---|
382 | mount /dev/loop0 $OGLIVEROOTFS |
---|
383 | else |
---|
384 | ## Montamos el ROOTFS tipo squashfs |
---|
385 | mount $DSTOGLIVE/ogclient/ogclient.sqfs $OGLIVEROOTFS -t squashfs -o loop |
---|
386 | fi |
---|
387 | # Realizamos la union entre el ogliveram(initrd) y el ogliverootfs(ogclient.sqfs) |
---|
388 | for i in etc var lib bin sbin usr root boot; do |
---|
389 | ogUnionLiveDir $i |
---|
390 | done |
---|
391 | cat /tmp/mtab.preunion > /etc/mtab |
---|
392 | else |
---|
393 | echo "Fichero imagen del cliente no encontrado" |
---|
394 | return 1 |
---|
395 | fi |
---|
396 | } |
---|
397 | |
---|
398 | |
---|
399 | |
---|
400 | #/** |
---|
401 | # ogUnionLiveDir |
---|
402 | #@brief fusiona dos directorios con unionfs |
---|
403 | #@param1 |
---|
404 | #@return |
---|
405 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
406 | #@version 0.9 |
---|
407 | #@author Antonio J. Doblas. Universidad de Malaga. |
---|
408 | #@date 2011/05/24 |
---|
409 | ogUnionLiveDir() |
---|
410 | { |
---|
411 | TMPDIR=/$1 #dir |
---|
412 | FUSE_OPT="-o default_permissions -o allow_other -o use_ino -o nonempty -o suid" |
---|
413 | UNION_OPT="-o cow -o noinitgroups" |
---|
414 | UBIN="unionfs-fuse" |
---|
415 | |
---|
416 | mkdir -p $OGLIVERAMFS$TMPDIR |
---|
417 | U1STDIR="${OGLIVERAMFS}${TMPDIR}=RW" |
---|
418 | U2NDDIR="${OGLIVEROOTFS}${TMPDIR}=RO" |
---|
419 | UNIONDIR=${OGLIVEUNIONFS}${TMPDIR} |
---|
420 | mkdir -p $UNIONDIR |
---|
421 | $UBIN $FUSE_OPT $UNION_OPT ${U1STDIR}:${U2NDDIR} $UNIONDIR |
---|
422 | mount --bind $UNIONDIR $TMPDIR |
---|
423 | } |
---|
424 | |
---|
425 | |
---|
426 | |
---|
427 | #/** |
---|
428 | # ogConfigureLoopback |
---|
429 | #@brief Configura la interfaz loopback para cliente torrent |
---|
430 | #@param |
---|
431 | #@return |
---|
432 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
433 | #@version 0.9 Usando funciones generales de ubuntu |
---|
434 | #@author Antonio J. Doblas. Universidad de Malaga. |
---|
435 | #@date 2010/05/24 |
---|
436 | #@version 1.0.1 Deteccion automatica de interfaz con enlace activo. |
---|
437 | #@author Antonio J. Doblas. Universidad de Malaga. |
---|
438 | #@date 2011/05/24 |
---|
439 | #*/ ## |
---|
440 | ogConfigureLoopback() |
---|
441 | { |
---|
442 | # for the portmapper we need localhost |
---|
443 | ifconfig lo 127.0.0.1 |
---|
444 | #/etc/init.d/portmap start |
---|
445 | } |
---|
446 | |
---|
447 | #/** |
---|
448 | # ogConfigureNetworking |
---|
449 | #@brief Configura la interfaz de red usada en el pxe |
---|
450 | #@param |
---|
451 | #@return |
---|
452 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
453 | #@version 0.9 |
---|
454 | #@author Antonio J. Doblas. Universidad de Malaga. |
---|
455 | #@date 2010/05/24 |
---|
456 | #*/ ## |
---|
457 | ogConfigureNetworking() |
---|
458 | { |
---|
459 | #echo "ogConfigureNetworking: Buscando interfaz a configurar DEVICE" |
---|
460 | if [ -n "${BOOTIF}" ] |
---|
461 | then |
---|
462 | #echo " variable BOOTIF exportada con pxelinux.0 con valor $BOOTIF" |
---|
463 | IP=$IPOPTS |
---|
464 | temp_mac=${BOOTIF#*-} |
---|
465 | # convert to typical mac address format by replacing "-" with ":" |
---|
466 | bootif_mac="" |
---|
467 | IFS='-' |
---|
468 | for x in $temp_mac ; do |
---|
469 | if [ -z "$bootif_mac" ]; then |
---|
470 | bootif_mac="$x" |
---|
471 | else |
---|
472 | bootif_mac="$x:$bootif_mac" |
---|
473 | fi |
---|
474 | done |
---|
475 | unset IFS |
---|
476 | # look for devices with matching mac address, and set DEVICE to |
---|
477 | # appropriate value if match is found. |
---|
478 | for device in /sys/class/net/* ; do |
---|
479 | if [ -f "$device/address" ]; then |
---|
480 | current_mac=$(cat "$device/address") |
---|
481 | if [ "$bootif_mac" = "$current_mac" ]; then |
---|
482 | DEVICE=${device##*/} |
---|
483 | break |
---|
484 | fi |
---|
485 | fi |
---|
486 | done |
---|
487 | else |
---|
488 | #echo "variable BOOTIF no exportada, intentamos detectar que interfaz se ha iniciado" |
---|
489 | IP=$ip |
---|
490 | #TODO Detectar que interfaz se ha iniciado |
---|
491 | case ${IP} in |
---|
492 | none|off) |
---|
493 | return 0 |
---|
494 | ;; |
---|
495 | ""|on|any) |
---|
496 | # Bring up device |
---|
497 | DEVICE=eth0 |
---|
498 | ;; |
---|
499 | dhcp|bootp|rarp|both) |
---|
500 | DEVICE=eth0 |
---|
501 | ;; |
---|
502 | *) |
---|
503 | DEVICE=`echo $IP | cut -f6 -d:` |
---|
504 | ;; |
---|
505 | esac |
---|
506 | fi |
---|
507 | if [ -z "${DEVICE}" ]; then |
---|
508 | echo "variable DEVICE con valor $DEVICE no encontrada, llamamos de nuevo a ogconfigure_networking" |
---|
509 | ogConfigureNetworking |
---|
510 | fi |
---|
511 | |
---|
512 | [ -n "${DEVICE}" ] && [ -e /tmp/net-"${DEVICE}".conf ] && return 0 |
---|
513 | #if [ -n "${DEVICE}" ] && [ -e /tmp/net-"${DEVICE}".conf ]; then |
---|
514 | # echo "variable DEVICE con valor $DEVICE y fichero /tmp/net-$DEVICE encontrados" |
---|
515 | # return 0 |
---|
516 | #else |
---|
517 | # echo "variable DEVICE con valor $DEVICE encontrada, procedemos a configurala y a crear el fichero /tmp/net-$DEVICE" |
---|
518 | #fi |
---|
519 | |
---|
520 | # Activamos la interfaz antes de configurar. |
---|
521 | ip address flush $DEVICE |
---|
522 | ip link set dev $DEVICE up |
---|
523 | # Si no se detecta señal portadora volver a configurar. |
---|
524 | sleep 1 |
---|
525 | CARRIER=$(cat /sys/class/net/${DEVICE}/carrier) |
---|
526 | if [ "$CARRIER" != "1" ] |
---|
527 | then |
---|
528 | ogConfigureNetworking |
---|
529 | fi |
---|
530 | |
---|
531 | # support ip options see linux sources |
---|
532 | # Documentation/filesystems/nfsroot.txt |
---|
533 | # Documentation/frv/booting.txt |
---|
534 | for ROUNDTTT in 2 3 4 6 9 16 25 36 64 100; do |
---|
535 | # The NIC is to be configured if this file does not exist. |
---|
536 | # Ip-Config tries to create this file and when it succeds |
---|
537 | # creating the file, ipconfig is not run again. |
---|
538 | if [ -e /tmp/net-"${DEVICE}".conf ]; then |
---|
539 | break; |
---|
540 | fi |
---|
541 | case ${IP} in |
---|
542 | none|off) |
---|
543 | return 0 |
---|
544 | ;; |
---|
545 | ""|on|any) |
---|
546 | # Bring up device |
---|
547 | echo "Setting $DEVICE with option:on|any and Variable IP= $IP: ipconfig -t ${ROUNDTTT} ${DEVICE} " |
---|
548 | ipconfig -t ${ROUNDTTT} ${DEVICE} |
---|
549 | ;; |
---|
550 | dhcp|bootp|rarp|both) |
---|
551 | echo "Setting $DEVICE with option:dhcp|bootp|rarp|both and Variable IP= $IP: ipconfig -t ${ROUNDTTT} -c ${IP} -d ${DEVICE} " |
---|
552 | ipconfig -t ${ROUNDTTT} -c ${IP} -d ${DEVICE} |
---|
553 | ;; |
---|
554 | *) |
---|
555 | echo "Setting $DEVICE with option * and Variable IP= $IP: ipconfig -t ${ROUNDTTT} -d $IP " |
---|
556 | ipconfig -t ${ROUNDTTT} -d $IP |
---|
557 | # grab device entry from ip option |
---|
558 | NEW_DEVICE=${IP#*:*:*:*:*:*} |
---|
559 | if [ "${NEW_DEVICE}" != "${IP}" ]; then |
---|
560 | NEW_DEVICE=${NEW_DEVICE%:*} |
---|
561 | else |
---|
562 | # wrong parse, possibly only a partial string |
---|
563 | NEW_DEVICE= |
---|
564 | fi |
---|
565 | if [ -n "${NEW_DEVICE}" ]; then |
---|
566 | DEVICE="${NEW_DEVICE}" |
---|
567 | fi |
---|
568 | ;; |
---|
569 | esac |
---|
570 | done |
---|
571 | |
---|
572 | # source ipconfig output |
---|
573 | if [ -n "${DEVICE}" ]; then |
---|
574 | . /tmp/net-${DEVICE}.conf |
---|
575 | DEVICECFG="/tmp/net-${DEVICE}.conf" |
---|
576 | export DEVICECFG |
---|
577 | export DEVICE |
---|
578 | echo "DEVICE=$DEVICE" >> $CFGINITRD |
---|
579 | echo "DEVICECFG=$DEVICECFG" >> $CFGINITRD |
---|
580 | echo "exportando variable DEVICE con valor = $DEVICE y el DEVICECFG con valor $DEVICECFG" |
---|
581 | else |
---|
582 | # source any interface as not exaclty specified |
---|
583 | . /tmp/net-*.conf |
---|
584 | fi |
---|
585 | } |
---|
586 | |
---|
587 | |
---|
588 | #/** |
---|
589 | # ogYesNo |
---|
590 | #@brief Gestion de peticiones de usuario en modo ogdebug=true |
---|
591 | #@param1 OPTIONS --timeout N --default ANSWER |
---|
592 | #@param1 Questions |
---|
593 | #@return 1=yes 0=no |
---|
594 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
595 | #@version 0.9 |
---|
596 | #@author: |
---|
597 | #@date 2010/05/24 |
---|
598 | #*/ ## |
---|
599 | ogYesNo() |
---|
600 | { |
---|
601 | local ans |
---|
602 | local ok=0 |
---|
603 | local timeout=0 |
---|
604 | local default |
---|
605 | local t |
---|
606 | |
---|
607 | while [[ "$1" ]] |
---|
608 | do |
---|
609 | case "$1" in |
---|
610 | --default) |
---|
611 | shift |
---|
612 | default=$1 |
---|
613 | if [[ ! "$default" ]]; then error "Missing default value"; fi |
---|
614 | t=$(echo $default | tr '[:upper:]' '[:lower:]') |
---|
615 | |
---|
616 | if [[ "$t" != 'y' && "$t" != 'yes' && "$t" != 'n' && "$t" != 'no' ]]; then |
---|
617 | error "Illegal default answer: $default" |
---|
618 | fi |
---|
619 | default=$t |
---|
620 | shift |
---|
621 | ;; |
---|
622 | |
---|
623 | --timeout) |
---|
624 | shift |
---|
625 | timeout=$1 |
---|
626 | if [[ ! "$timeout" ]]; then error "Missing timeout value"; fi |
---|
627 | #if [[ ! "$timeout" =~ ^[0-9][0-9]*$ ]]; then error "Illegal timeout value: $timeout"; fi |
---|
628 | shift |
---|
629 | ;; |
---|
630 | |
---|
631 | -*) |
---|
632 | error "Unrecognized option: $1" |
---|
633 | ;; |
---|
634 | |
---|
635 | *) |
---|
636 | break |
---|
637 | ;; |
---|
638 | esac |
---|
639 | done |
---|
640 | |
---|
641 | if [[ $timeout -ne 0 && ! "$default" ]]; then |
---|
642 | error "Non-zero timeout requires a default answer" |
---|
643 | fi |
---|
644 | |
---|
645 | if [[ ! "$*" ]]; then error "Missing question"; fi |
---|
646 | |
---|
647 | while [[ $ok -eq 0 ]] |
---|
648 | do |
---|
649 | if [[ $timeout -ne 0 ]]; then |
---|
650 | if ! read -t $timeout -p "$*" ans; then |
---|
651 | ans=$default |
---|
652 | else |
---|
653 | # Turn off timeout if answer entered. |
---|
654 | timeout=0 |
---|
655 | if [[ ! "$ans" ]]; then ans=$default; fi |
---|
656 | fi |
---|
657 | else |
---|
658 | read -p "$*" ans |
---|
659 | if [[ ! "$ans" ]]; then |
---|
660 | ans=$default |
---|
661 | else |
---|
662 | ans=$(echo $ans | tr '[:upper:]' '[:lower:]') |
---|
663 | fi |
---|
664 | fi |
---|
665 | |
---|
666 | if [[ "$ans" == 'y' || "$ans" == 'yes' || "$ans" == 'n' || "$ans" == 'no' ]]; then |
---|
667 | ok=1 |
---|
668 | fi |
---|
669 | |
---|
670 | if [[ $ok -eq 0 ]]; then warning "Valid answers are: yes y no n"; fi |
---|
671 | done |
---|
672 | [[ "$ans" = "y" || "$ans" == "yes" ]] |
---|
673 | } |
---|
674 | |
---|