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

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 27dc5ff was 98db1ad, checked in by adv <adv@…>, 14 years ago

version1.0.1 #383 tiempos deteccion iface boot-tools

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

  • Property mode set to 100644
File size: 11.2 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{
243IP=$IPOPTS
244
245#Buscando interfaz a configurar DEVICE
246#BOOTIF lo exporte el pxelinux.0. con valor tipo 01-$mac_address
247if [ -n "${BOOTIF}" ]
248then
249        temp_mac=${BOOTIF#*-}
250        # convert to typical mac address format by replacing "-" with ":"
251        bootif_mac=""
252        IFS='-'
253        for x in $temp_mac ; do
254                if [ -z "$bootif_mac" ]; then
255        bootif_mac="$x"
256        else
257                bootif_mac="$x:$bootif_mac"
258        fi
259        done
260        unset IFS
261        # look for devices with matching mac address, and set DEVICE to
262        # appropriate value if match is found.
263        for device in /sys/class/net/* ; do
264                if [ -f "$device/address" ]; then
265                        current_mac=$(cat "$device/address")
266                        if [ "$bootif_mac" = "$current_mac" ]; then
267                                DEVICE=${device##*/}
268                                break
269                        fi
270                fi
271        done
272else
273        # Si no bootif, intentamos buscar que interfaz se ha iniciado.
274        IP=$ip
275        #TODO Detectar que interfaz se ha iniciado
276        case ${IP} in
277                none|off)
278           # Do nothing
279        ;;
280        ""|on|any)
281          # Bring up device
282          DEVICE=eth0
283        ;;
284        dhcp|bootp|rarp|both)
285          DEVICE=eth0
286        ;;
287        *)
288          DEVICE=`echo $IP | cut -f6 -d:`
289        ;;
290        esac
291fi
292# FIN de Busqueda de la interfaz  $DEVICE
293# networking already configured thus bail out
294[ -n "${DEVICE}" ] && [ -e /tmp/net-"${DEVICE}".conf ] && return 0
295# support ip options see linux sources
296# Documentation/filesystems/nfsroot.txt
297# Documentation/frv/booting.txt
298for ROUNDTTT in 2 3 4 6 9 16 25 36 64 100; do
299        # The NIC is to be configured if this file does not exist.
300        # Ip-Config tries to create this file and when it succeds
301        # creating the file, ipconfig is not run again.
302        if [ -e /tmp/net-"${DEVICE}".conf ]; then
303                break;
304        fi
305        case ${IP} in
306                none|off)
307                        # Do nothing
308                ;;
309                ""|on|any)
310                        # Bring up device
311                        echo "Setting $DEVICE with kernel params $IP: ipconfig -t ${ROUNDTTT} ${DEVICE} "
312                        ipconfig -t ${ROUNDTTT} ${DEVICE}
313                ;;
314                dhcp|bootp|rarp|both)
315                        echo "Setting $DEVICE with (dhcp) kernel params $IP: ipconfig -t ${ROUNDTTT} -c ${IP} -d ${DEVICE} "
316                        ipconfig -t ${ROUNDTTT} -c ${IP} -d ${DEVICE}
317                ;;
318                *)
319                        echo "Setting $DEVICE with kernel params $IP: ipconfig -t ${ROUNDTTT} -d $IP  "
320                        ipconfig -t ${ROUNDTTT} -d $IP
321                        # grab device entry from ip option
322                        NEW_DEVICE=${IP#*:*:*:*:*:*}
323                        if [ "${NEW_DEVICE}" != "${IP}" ]; then
324                                NEW_DEVICE=${NEW_DEVICE%:*}
325                        else
326                                # wrong parse, possibly only a partial string
327                                NEW_DEVICE=
328                        fi
329                        if [ -n "${NEW_DEVICE}" ]; then
330                                DEVICE="${NEW_DEVICE}"
331                        fi
332                ;;
333        esac
334done
335
336# source ipconfig output
337if [ -n "${DEVICE}" ]; then
338        # source specific bootdevice
339    . /tmp/net-${DEVICE}.conf
340else
341    # source any interface as not exaclty specified
342    . /tmp/net-*.conf
343fi
344}
345
346   #####################################################################
347# Ask yesno question.
348#
349# Usage: yesno OPTIONS QUESTION
350#
351#   Options:
352#     --timeout N    Timeout if no input seen in N seconds.
353#     --default ANS  Use ANS as the default answer on timeout or
354#                    if an empty answer is provided.
355#
356# Exit status is the answer.  0=yes 1=no
357
358ogYesNo()
359{
360    local ans
361    local ok=0
362    local timeout=0
363    local default
364    local t
365
366    while [[ "$1" ]]
367    do
368        case "$1" in
369        --default)
370            shift
371            default=$1
372            if [[ ! "$default" ]]; then error "Missing default value"; fi
373            t=$(echo $default | tr '[:upper:]' '[:lower:]')
374
375            if [[ "$t" != 'y'  &&  "$t" != 'yes'  &&  "$t" != 'n'  &&  "$t" != 'no' ]]; then
376                error "Illegal default answer: $default"
377            fi
378            default=$t
379            shift
380            ;;
381
382        --timeout)
383            shift
384            timeout=$1
385            if [[ ! "$timeout" ]]; then error "Missing timeout value"; fi
386            #if [[ ! "$timeout" =~ ^[0-9][0-9]*$ ]]; then error "Illegal timeout value: $timeout"; fi
387            shift
388            ;;
389
390        -*)
391            error "Unrecognized option: $1"
392            ;;
393
394        *)
395            break
396            ;;
397        esac
398    done
399
400    if [[ $timeout -ne 0  &&  ! "$default" ]]; then
401        error "Non-zero timeout requires a default answer"
402    fi
403
404    if [[ ! "$*" ]]; then error "Missing question"; fi
405
406    while [[ $ok -eq 0 ]]
407    do
408        if [[ $timeout -ne 0 ]]; then
409            if ! read -t $timeout -p "$*" ans; then
410                ans=$default
411            else
412                # Turn off timeout if answer entered.
413                timeout=0
414                if [[ ! "$ans" ]]; then ans=$default; fi
415            fi
416        else
417            read -p "$*" ans
418            if [[ ! "$ans" ]]; then
419                ans=$default
420            else
421                ans=$(echo $ans | tr '[:upper:]' '[:lower:]')
422            fi
423        fi
424
425        if [[ "$ans" == 'y'  ||  "$ans" == 'yes'  ||  "$ans" == 'n'  ||  "$ans" == 'no' ]]; then
426            ok=1
427        fi
428
429        if [[ $ok -eq 0 ]]; then warning "Valid answers are: yes y no n"; fi
430    done
431    [[ "$ans" = "y" || "$ans" == "yes" ]]
432}
433     
434
Note: See TracBrowser for help on using the repository browser.