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

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 fcb1011 was fcb1011, checked in by adv <adv@…>, 14 years ago

version 1.0.2 #404 boot-tools configuracion interfaz pxe

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

  • Property mode set to 100644
File size: 11.8 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 no encontrada y fichero /tmp/net-$DEVICE encontrados , llamamos de nuevo a ogconfigure_networking"
301        ogconfigure_networking
302fi
303
304# support ip options see linux sources
305# Documentation/filesystems/nfsroot.txt
306# Documentation/frv/booting.txt
307for ROUNDTTT in 2 3 4 6 9 16 25 36 64 100; do
308        # The NIC is to be configured if this file does not exist.
309        # Ip-Config tries to create this file and when it succeds
310        # creating the file, ipconfig is not run again.
311        if [ -e /tmp/net-"${DEVICE}".conf ]; then
312                break;
313        fi
314        case ${IP} in
315                none|off)
316                        return 0
317                ;;
318                ""|on|any)
319                        # Bring up device
320                        echo "Setting $DEVICE  with option:on|any and Variable IP= $IP: ipconfig -t ${ROUNDTTT} ${DEVICE} "
321                        ipconfig -t ${ROUNDTTT} ${DEVICE}
322                ;;
323                dhcp|bootp|rarp|both)
324                        echo "Setting $DEVICE with option:dhcp|bootp|rarp|both and Variable IP=  $IP: ipconfig -t ${ROUNDTTT} -c ${IP} -d ${DEVICE} "
325                        ipconfig -t ${ROUNDTTT} -c ${IP} -d ${DEVICE}
326                ;;
327                *)
328                        echo "Setting $DEVICE with option *  and Variable IP= $IP: ipconfig -t ${ROUNDTTT} -d $IP  "
329                        ipconfig -t ${ROUNDTTT} -d $IP
330                        # grab device entry from ip option
331                        NEW_DEVICE=${IP#*:*:*:*:*:*}
332                        if [ "${NEW_DEVICE}" != "${IP}" ]; then
333                                NEW_DEVICE=${NEW_DEVICE%:*}
334                        else
335                                # wrong parse, possibly only a partial string
336                                NEW_DEVICE=
337                        fi
338                        if [ -n "${NEW_DEVICE}" ]; then
339                                DEVICE="${NEW_DEVICE}"
340                        fi
341                ;;
342        esac
343done
344
345# source ipconfig output
346if [ -n "${DEVICE}" ]; then     
347    . /tmp/net-${DEVICE}.conf
348    DEVICECFG="/tmp/net-${DEVICE}"
349        export DEVICECFG
350        export DEVICE
351        echo "exportando variable DEVICE con valor = $DEVICE y el DEVICECFG con valor $DEVICECFG"   
352else
353    # source any interface as not exaclty specified
354    . /tmp/net-*.conf
355fi
356}
357
358   #####################################################################
359# Ask yesno question.
360#
361# Usage: yesno OPTIONS QUESTION
362#
363#   Options:
364#     --timeout N    Timeout if no input seen in N seconds.
365#     --default ANS  Use ANS as the default answer on timeout or
366#                    if an empty answer is provided.
367#
368# Exit status is the answer.  0=yes 1=no
369
370ogYesNo()
371{
372    local ans
373    local ok=0
374    local timeout=0
375    local default
376    local t
377
378    while [[ "$1" ]]
379    do
380        case "$1" in
381        --default)
382            shift
383            default=$1
384            if [[ ! "$default" ]]; then error "Missing default value"; fi
385            t=$(echo $default | tr '[:upper:]' '[:lower:]')
386
387            if [[ "$t" != 'y'  &&  "$t" != 'yes'  &&  "$t" != 'n'  &&  "$t" != 'no' ]]; then
388                error "Illegal default answer: $default"
389            fi
390            default=$t
391            shift
392            ;;
393
394        --timeout)
395            shift
396            timeout=$1
397            if [[ ! "$timeout" ]]; then error "Missing timeout value"; fi
398            #if [[ ! "$timeout" =~ ^[0-9][0-9]*$ ]]; then error "Illegal timeout value: $timeout"; fi
399            shift
400            ;;
401
402        -*)
403            error "Unrecognized option: $1"
404            ;;
405
406        *)
407            break
408            ;;
409        esac
410    done
411
412    if [[ $timeout -ne 0  &&  ! "$default" ]]; then
413        error "Non-zero timeout requires a default answer"
414    fi
415
416    if [[ ! "$*" ]]; then error "Missing question"; fi
417
418    while [[ $ok -eq 0 ]]
419    do
420        if [[ $timeout -ne 0 ]]; then
421            if ! read -t $timeout -p "$*" ans; then
422                ans=$default
423            else
424                # Turn off timeout if answer entered.
425                timeout=0
426                if [[ ! "$ans" ]]; then ans=$default; fi
427            fi
428        else
429            read -p "$*" ans
430            if [[ ! "$ans" ]]; then
431                ans=$default
432            else
433                ans=$(echo $ans | tr '[:upper:]' '[:lower:]')
434            fi
435        fi
436
437        if [[ "$ans" == 'y'  ||  "$ans" == 'yes'  ||  "$ans" == 'n'  ||  "$ans" == 'no' ]]; then
438            ok=1
439        fi
440
441        if [[ $ok -eq 0 ]]; then warning "Valid answers are: yes y no n"; fi
442    done
443    [[ "$ans" = "y" || "$ans" == "yes" ]]
444}
445     
446
Note: See TracBrowser for help on using the repository browser.