source: client/boot-tools/clientstructure/etc/initramfs-tools/scripts/ogfunctions @ 15b2a4e

918-git-images-111dconfigfileconfigure-oglivegit-imageslgromero-new-oglivemainmaint-cronmount-efivarfsmultivmmultivm-ogboot-installerogClonningEngineogboot-installer-jenkinsoglive-ipv6test-python-scriptsticket-301ticket-50ticket-50-oldticket-577ticket-585ticket-611ticket-612ticket-693ticket-700ubu24tplunification2use-local-agent-oglivevarios-instalacionwebconsole3
Last change on this file since 15b2a4e was 9f58d38, checked in by adv <adv@…>, 14 years ago

version 1.0.2 #404 boot-tools configurando interfaz usada en el pxe

git-svn-id: https://opengnsys.es/svn/branches/version1.0@2031 a21b9725-9963-47de-94b9-378ad31fedc9

  • Property mode set to 100644
File size: 11.7 KB
Line 
1#@file    ogfunctions.lib
2#@brief   Librería o clase para la generación del 1erFS
3#@class   client
4#@brief   Funciones para la generación del primers sistema (initrd)
5#@version 0.91
6#@warning License: GNU GPLv3+
7
8
9
10
11ogExportKernelParameters ()
12{
13        GLOBAL="cat /proc/cmdline"
14        for i in `${GLOBAL}`
15        do
16                echo $i | grep "=" > /dev/null && export $i
17        done
18return 0
19}
20
21ogExportVarEnvironment ()
22{
23        #puntos de accesos al servidor ogprotocol=nfs
24        export NFSROOTBOOT="/var/lib/tftpboot"
25        export NFSCLIENTDIR="/opt/opengnsys/client"
26        export NFSLOGDIR="/opt/opengnsys/log/clients"
27        export NFSIMGDIR="/opt/opengnsys/images"
28        #puntos de accesos al servidor ogprotocol=smb
29        export SMBROOTBOOT="tftpboot"
30        export SMBCLIENTDIR="ogclient"
31        export SMBLOGDIR="oglog"
32        export SMBIMGDIR="ogimages"
33
34        #puntos de montaje local ram o cache
35        export LOCALCLIENTDIR="/opt/opengnsys"
36        export LOCALLOGDIR="/opt/opengnsys/log"
37        export LOCALIMGDIR="/opt/opengnsys/images"
38        export LOCALROOTBOOT="/opt/og2fs/tftpboot"              #punto de montaje del contendor tftpboot
39        export LOCALROOTIMG="/opt/og2fs/2ndfs"                  #punto de montaje donde se accede al 2nd FS mediante loop
40        export LOCALROOTRAM="/opt/og2fs/1stfs"                  #punto de montaje para unionfs
41        export LOCALROOTUNION="/opt/og2fs/unionfs"              #punto de union entreo LOCALROOTIMG y LOCALROOTRAM
42        ##INFORMACION DE OTRAS VARIABLES OBTENDIAS EN OTRAS FUNCIONES.
43        #ogGetROOTSERVER()  ip del servidor pxe, valor obtenido automaticamente desde dhcpd.
44        #IPV4DDR
45        #IPV4BROADCAST
46        #IPV4NETMASK
47        #IPV4GATEWAY
48        #DNS0   DNS1
49        #HOSTNAME
50        #ROOTSERVER  #ip del servidor pxe que ha servido el kernel
51        #REPOSERVER=ogrepo  -> ogConectROOTSERVER() ip del servidor de images para separar serviicios.
52        return 0
53}
54
55ogConfigureRamfs ()
56{
57        mkdir -p $LOCALROOTBOOT
58        mkdir -p $LOCALROOTIMG
59        mkdir -p $LOCALROOTRAM                           
60        mkdir -p $LOCALROOTUNION
61}
62
63ogLoadNetModule ()
64{
65        #cargando netmodule
66        if [ -n "$ognetmodule" ]
67        then
68                echo "Cargando modulo de red $netmodule"
69                insmod `find /lib/modules/ -name ${netmodule}*`
70        fi
71}
72
73
74ogPostConfigureFS()
75{
76        # configuramos el /etc/hostname.
77        echo $HOSTNAME > /etc/hostname
78       
79        #configuramos el /etc/hosts
80        echo "127.0.0.1 localhost" > /etc/hosts
81        echo "$IPV4ADDR $HOSTNAME" >> /etc/hosts
82       
83        #configuramos el host.conf
84        echo "order hosts,bind" > /etc/host.conf
85        echo "multi on" >> /etc/host.conf
86       
87        # configuramos el /etc/networks
88        #read -e NETIP NETDEFAULT <<<$(route -n | grep eth0 | awk -F" " '{print $1}')
89        NETIP=$(route -n | grep eth0 | awk -F" " '{print $1}') && NETIP=$(echo $NETIP | cut -f1 -d" ")
90        echo "default 0.0.0.0" > /etc/networks
91        echo "loopback 127.0.0.0" >> /etc/networks
92        echo "link-local 169.254.0.0" >> /etc/networks
93        echo "localnet $NETIP" >> /etc/networks
94        #route
95}
96
97
98
99
100ogGetROOTSERVER ()
101{
102        # get nfs root from dhcp
103        if [ "x${NFSROOT}" = "xauto" ]; then
104                # check if server ip is part of dhcp root-path
105                if [ "${ROOTPATH#*:}" = "${ROOTPATH}" ]; then
106                        NFSROOT=${ROOTSERVER}:${ROOTPATH}
107                else
108                        NFSROOT=${ROOTPATH}
109                fi
110
111        # nfsroot=[<server-ip>:]<root-dir>[,<nfs-options>]
112        elif [ -n "${NFSROOT}" ]; then
113                # nfs options are an optional arg
114                if [ "${NFSROOT#*,}" != "${NFSROOT}" ]; then
115                        NFSOPTS="-o ${NFSROOT#*,}"
116                fi
117                NFSROOT=${NFSROOT%%,*}
118                if [ "${NFSROOT#*:}" = "$NFSROOT" ]; then
119                        NFSROOT=${ROOTSERVER}:${NFSROOT}
120                fi
121        fi
122        export ROOTSERVER
123return 0
124}       
125
126ogConectROOTSERVER ()
127{
128local OPTIONS
129#params a detectar
130if [ $ogrepo ]
131then
132        # Validar si la ip es correcta
133        ROOTREPO=$ogrepo
134else
135        ROOTREPO=$ROOTSERVER
136fi
137
138case "$ogprotocol" in
139        cdrom)
140                echo  "Montar imagen de CD-ROM"
141                blkid /dev/s*
142                mount -t iso9660 LABEL=ogClient $LOCALROOTBOOT
143                ;;   
144        httfs)
145                echo "protocolo httfs aun no soportado"
146                ;;
147        sshfs)
148                echo "protocolo sshfs aun no soportado"
149                ;;
150        smb)   
151                echo "Preparando conexión con el Repositorio $ROOTSERVER por $ogprotocol"
152                OPTIONS=" -o user=opengnsys,pass=og"
153                mount.cifs //${ROOTSERVER}/${SMBCLIENTDIR} $LOCALCLIENTDIR $OPTIONS
154                mount.cifs //${ROOTSERVER}/${SMBLOGDIR} $LOCALLOGDIR $OPTIONS
155                mount.cifs //${ROOTSERVER}/${SMBROOTBOOT} $LOCALROOTBOOT $OPTIONS
156                mount.cifs //${ROOTREPO}/${SMBIMGDIR} $LOCALIMGDIR ${OPTIONS},ro
157                ;;
158        nfs)
159                echo "Preparando conexión con el Repositorio $ROOTSERVER por $ogprotocol"
160                nfsmount -o nolock,ro $ROOTSERVER:$NFSCLIENTDIR $LOCALCLIENTDIR
161                nfsmount -o nolock $ROOTSERVER:$NFSLOGDIR $LOCALLOGDIR
162                nfsmount -o nolock $ROOTSERVER:$NFSROOTBOOT $LOCALROOTBOOT
163                nfsmount -o nolock,ro $ROOTREPO:$NFSIMGDIR $LOCALIMGDIR
164                ;;
165esac
166}
167
168ogMerge2ndFile()
169{
170if [ -f $LOCALROOTBOOT/ogclient/ogclient.sqfs ]
171then   
172        cat /proc/mounts > /tmp/mtab.preunion
173        if [ "$og2nd" == "img" ]
174        then
175                #para acceder al img
176                losetup /dev/loop0 $LOCALROOTBOOT/ogclient/ogclient.img -o 32256
177                mount /dev/loop0 $LOCALROOTIMG
178        else
179                ## para acceder al squashfs
180                mount $LOCALROOTBOOT/ogclient/ogclient.sqfs $LOCALROOTIMG -t squashfs -o loop
181        fi
182        for i in etc var lib bin sbin usr root boot; do
183                unionmount $i
184        done
185        cat /tmp/mtab.preunion > /etc/mtab
186else
187        echo "Fichero imagen del cliente no encontrado"
188        return 1
189fi
190}
191
192
193unionmount()
194{
195        tmpdir=/$1              #dir
196        FUSE_OPT="-o default_permissions -o allow_other -o use_ino -o nonempty -o suid"
197        UNION_OPT="-o cow -o noinitgroups"
198        UBIN="unionfs-fuse"
199                                        #UPATH="/unionfs"
200                                        #LOCALROOTIMG="/opt/og2fs/2ndfs"       
201                                        #LOCALROOTRAM="/opt/og2fs/1stfs"                #/unionfs/host  #punto de montaje para unionfs
202                                        #LOCALROOTUNION=/opt/og2fs/unionfs/"    #/unionfs/union #punto de union entreo LOCALROOTIMG y LOCALROOTRAM
203                                        #mkdir -p $LOCALROOTRAM                          #/unionfs/host
204                                        #mkdir -p $LOCALROOTUNION                         #/unionfs/union
205        mkdir -p $LOCALROOTRAM$tmpdir
206        #mount --bind /$tmpdir $LOCALROOTRAM$tmpdir
207        U1STDIR="${LOCALROOTRAM}${tmpdir}=RW"
208        U2NDDIR="${LOCALROOTIMG}${tmpdir}=RO"
209        UNIONDIR=$LOCALROOTUNION$tmpdir
210        mkdir -p $UNIONDIR
211        $UBIN $FUSE_OPT $UNION_OPT ${U1STDIR}:${U2NDDIR} $UNIONDIR
212        mount --bind  $UNIONDIR $tmpdir
213}
214
215
216unionmountOLD()
217{
218        FUSE_OPT="-o default_permissions -o allow_other -o use_ino -o nonempty -o suid"
219        UNION_OPT="-o cow -o noinitgroups"
220        UPATH="/unionfs"
221        UBIN="unionfs-fuse"
222        mkdir -p /unionfs/host
223        mkdir -p /unionfs/union
224        dir=$1
225        mkdir -p /unionfs/host/$dir
226        #mount --bind /$dir /unionfs/host/$dir
227        mkdir -p /unionfs/union/$dir
228        host="/unionfs/host/${dir}=RW"
229        common="/opt/og2fs/${dir}=RO"
230        $UBIN $FUSE_OPT $UNION_OPT ${host}:${common} /unionfs/union/$dir
231        mount --bind  /unionfs/union/$dir /$dir
232}
233
234ogconfigure_lo()
235{
236# for the portmapper we need localhost
237ifconfig lo 127.0.0.1
238#/etc/init.d/portmap start
239}
240
241ogconfigure_networking()
242{
243echo "ogconfigure_networking: Buscando interfaz a configurar DEVICE"
244if [ -n "${BOOTIF}" ]
245then
246        echo " variable BOOTIF exportada con pxelinux.0 con valor $BOOTIF"
247        IP=$IPOPTS
248        temp_mac=${BOOTIF#*-}
249        # convert to typical mac address format by replacing "-" with ":"
250        bootif_mac=""
251        IFS='-'
252        for x in $temp_mac ; do
253                if [ -z "$bootif_mac" ]; then
254        bootif_mac="$x"
255        else
256                bootif_mac="$x:$bootif_mac"
257        fi
258        done
259        unset IFS
260        # look for devices with matching mac address, and set DEVICE to
261        # appropriate value if match is found.
262        for device in /sys/class/net/* ; do
263                if [ -f "$device/address" ]; then
264                        current_mac=$(cat "$device/address")
265                        if [ "$bootif_mac" = "$current_mac" ]; then
266                                DEVICE=${device##*/}
267                                break
268                        fi
269                fi
270        done
271else
272        echo "variable BOOTIF no exportada, intentamos detectar que interfaz se ha iniciado"
273        IP=$ip
274        #TODO Detectar que interfaz se ha iniciado
275        case ${IP} in
276                none|off)
277           return 0
278        ;;
279        ""|on|any)
280          # Bring up device
281          DEVICE=eth0
282        ;;
283        dhcp|bootp|rarp|both)
284          DEVICE=eth0
285        ;;
286        *)
287          DEVICE=`echo $IP | cut -f6 -d:`
288        ;;
289        esac
290fi
291if [ -z "${DEVICE}" ]; then
292        echo "variable DEVICE con valor $DEVICE no encontrada, llamamos de nuevo a ogconfigure_networking"
293        ogconfigure_networking
294fi
295
296if [ -n "${DEVICE}" ] && [ -e /tmp/net-"${DEVICE}".conf ]; then
297        echo "variable DEVICE con valor $DEVICE  y fichero /tmp/net-$DEVICE encontrados"
298        return 0
299else
300        echo "variable DEVICE con valor $DEVICE encontrada, procedemos a configurala y a crear el fichero /tmp/net-$DEVICE"
301fi
302
303# support ip options see linux sources
304# Documentation/filesystems/nfsroot.txt
305# Documentation/frv/booting.txt
306for ROUNDTTT in 2 3 4 6 9 16 25 36 64 100; do
307        # The NIC is to be configured if this file does not exist.
308        # Ip-Config tries to create this file and when it succeds
309        # creating the file, ipconfig is not run again.
310        if [ -e /tmp/net-"${DEVICE}".conf ]; then
311                break;
312        fi
313        case ${IP} in
314                none|off)
315                        return 0
316                ;;
317                ""|on|any)
318                        # Bring up device
319                        echo "Setting $DEVICE  with option:on|any and Variable IP= $IP: ipconfig -t ${ROUNDTTT} ${DEVICE} "
320                        ipconfig -t ${ROUNDTTT} ${DEVICE}
321                ;;
322                dhcp|bootp|rarp|both)
323                        echo "Setting $DEVICE with option:dhcp|bootp|rarp|both and Variable IP=  $IP: ipconfig -t ${ROUNDTTT} -c ${IP} -d ${DEVICE} "
324                        ipconfig -t ${ROUNDTTT} -c ${IP} -d ${DEVICE}
325                ;;
326                *)
327                        echo "Setting $DEVICE with option *  and Variable IP= $IP: ipconfig -t ${ROUNDTTT} -d $IP  "
328                        ipconfig -t ${ROUNDTTT} -d $IP
329                        # grab device entry from ip option
330                        NEW_DEVICE=${IP#*:*:*:*:*:*}
331                        if [ "${NEW_DEVICE}" != "${IP}" ]; then
332                                NEW_DEVICE=${NEW_DEVICE%:*}
333                        else
334                                # wrong parse, possibly only a partial string
335                                NEW_DEVICE=
336                        fi
337                        if [ -n "${NEW_DEVICE}" ]; then
338                                DEVICE="${NEW_DEVICE}"
339                        fi
340                ;;
341        esac
342done
343
344# source ipconfig output
345if [ -n "${DEVICE}" ]; then     
346    . /tmp/net-${DEVICE}.conf
347    DEVICECFG="/tmp/net-${DEVICE}"
348        export $DEVICECFG
349        export $DEVICE
350        echo "exportando variable DEVICE con valor = $DEVICE y el DEVICECFG con valor $DEVICECFG"   
351else
352    # source any interface as not exaclty specified
353    . /tmp/net-*.conf
354fi
355}
356
357   #####################################################################
358# Ask yesno question.
359#
360# Usage: yesno OPTIONS QUESTION
361#
362#   Options:
363#     --timeout N    Timeout if no input seen in N seconds.
364#     --default ANS  Use ANS as the default answer on timeout or
365#                    if an empty answer is provided.
366#
367# Exit status is the answer.  0=yes 1=no
368
369ogYesNo()
370{
371    local ans
372    local ok=0
373    local timeout=0
374    local default
375    local t
376
377    while [[ "$1" ]]
378    do
379        case "$1" in
380        --default)
381            shift
382            default=$1
383            if [[ ! "$default" ]]; then error "Missing default value"; fi
384            t=$(echo $default | tr '[:upper:]' '[:lower:]')
385
386            if [[ "$t" != 'y'  &&  "$t" != 'yes'  &&  "$t" != 'n'  &&  "$t" != 'no' ]]; then
387                error "Illegal default answer: $default"
388            fi
389            default=$t
390            shift
391            ;;
392
393        --timeout)
394            shift
395            timeout=$1
396            if [[ ! "$timeout" ]]; then error "Missing timeout value"; fi
397            #if [[ ! "$timeout" =~ ^[0-9][0-9]*$ ]]; then error "Illegal timeout value: $timeout"; fi
398            shift
399            ;;
400
401        -*)
402            error "Unrecognized option: $1"
403            ;;
404
405        *)
406            break
407            ;;
408        esac
409    done
410
411    if [[ $timeout -ne 0  &&  ! "$default" ]]; then
412        error "Non-zero timeout requires a default answer"
413    fi
414
415    if [[ ! "$*" ]]; then error "Missing question"; fi
416
417    while [[ $ok -eq 0 ]]
418    do
419        if [[ $timeout -ne 0 ]]; then
420            if ! read -t $timeout -p "$*" ans; then
421                ans=$default
422            else
423                # Turn off timeout if answer entered.
424                timeout=0
425                if [[ ! "$ans" ]]; then ans=$default; fi
426            fi
427        else
428            read -p "$*" ans
429            if [[ ! "$ans" ]]; then
430                ans=$default
431            else
432                ans=$(echo $ans | tr '[:upper:]' '[:lower:]')
433            fi
434        fi
435
436        if [[ "$ans" == 'y'  ||  "$ans" == 'yes'  ||  "$ans" == 'n'  ||  "$ans" == 'no' ]]; then
437            ok=1
438        fi
439
440        if [[ $ok -eq 0 ]]; then warning "Valid answers are: yes y no n"; fi
441    done
442    [[ "$ans" = "y" || "$ans" == "yes" ]]
443}
444     
445
Note: See TracBrowser for help on using the repository browser.