source: branches/3.2/mindi/rootfs/usr/sbin/start-netfs @ 3443

Last change on this file since 3443 was 3443, checked in by bruno, 4 years ago
  • Adds an ipmode configuration parameter at boot time to typically force start-netfs to make a DHCP request, despite the conf file doesn't precise it (parm can be DHCP or STATIC)
  • Review again the strategy when the simple case of NIC unchanged and IP conf either is not verified. Trust ipconf first, and then use mondorestore.cfg content, trying to find the right NIC withthe corresponding MAC address in the file, if the stored NIC isn't named identically anymore.
  • Property svn:executable set to *
File size: 6.2 KB
Line 
1#!/bin/sh
2#
3# $Id$
4#
5# This script sets up the network + Network FS environment if needed.
6#
7
8
9# number of ping
10ipcount=3
11
12# Get info from config file
13ipdev=`grep netfs-dev /tmp/mondorestore.cfg 2> /dev/null | cut -d' ' -f2-`
14# MAC address in uppercases
15hwaddr=`grep netfs-client-hwaddr /tmp/mondorestore.cfg 2> /dev/null | cut -d' ' -f2- | tr '[a-z]' '[A-Z]'`
16ipaddress=`grep netfs-client-ipaddr /tmp/mondorestore.cfg 2> /dev/null | cut -d' ' -f2-`
17ipnetmask=`grep netfs-client-netmask /tmp/mondorestore.cfg 2> /dev/null | cut -d' ' -f2-`
18ipbroadcast=`grep netfs-client-broadcast /tmp/mondorestore.cfg 2> /dev/null | cut -d' ' -f2-`
19ipgateway=`grep netfs-client-defgw /tmp/mondorestore.cfg 2> /dev/null | cut -d' ' -f2-`
20proto=`grep netfs-proto /tmp/mondorestore.cfg 2> /dev/null | cut -d' ' -f2-`
21iproute=`grep netfs-route /tmp/mondorestore.cfg 2> /dev/null | cut -d' ' -f2-`
22ipconf=""
23ipmode="STATIC"
24pre=""
25post=""
26export netfsmount=`grep netfs-server-mount /tmp/mondorestore.cfg 2> /dev/null | cut -d' ' -f2-`
27export imgname=`grep iso-prefix /tmp/mondorestore.cfg 2> /dev/null | cut -d' ' -f2-`
28if [ "$imgname" = "" ]; then
29    export imgname="mondorescue"
30fi
31export dirimg=`grep iso-dir /tmp/mondorestore.cfg 2> /dev/null | cut -d' ' -f2-`
32if [ "$dirimg" = "" ]; then
33    export dirimg="/"
34fi
35
36# info from cmdline are predominent
37for i in `cat /proc/cmdline` ; do
38    echo $i | grep -qi ping= && ipcount=`echo $i | cut -d= -f2`
39    echo $i | grep -qi hwaddr= && hwaddr=`echo $i | cut -d= -f2  | tr '[a-z]' '[A-Z]'`
40    echo $i | grep -qi ipconf= && ipconf=`echo $i | cut -d= -f2`
41    echo $i | grep -qi netfsmount= && export netfsmount=`echo $i | cut -d= -f2`
42    echo $i | grep -qi netfsopt= && export netfsopt=`echo $i | cut -d= -f2`
43    echo $i | grep -qi prefix= && export imgname=`echo $i | cut -d= -f2`
44    echo $i | grep -qi netfspath= && export dirimg=`echo $i | cut -d= -f2`
45    echo $i | grep -qi pre= && pre=`echo $i | cut -d= -f2`
46    echo $i | grep -qi post= && post=`echo $i | cut -d= -f2`
47    echo $i | grep -qi proto= && proto=`echo $i | cut -d= -f2`
48    echo $i | grep -qi iproute= && iproute=`echo $i | cut -d= -f2`
49    echo $i | grep -qi ipmode= && ipmode=`echo $i | cut -d= -f2`
50done
51
52if [ "$ipdev" != "" ] || [ "$ipconf" != "" ]; then
53    # Network configuration stored during archive
54    # or on cmdline so network wanted => starting it
55    LogIt "Starting Network..."
56
57    # Activate loobback
58    ifconfig lo 127.0.0.1
59
60    # ipconf has priority
61    if [ "$ipconf" != "" ]; then
62        echo "$ipconf" | grep -q "dhcp"
63        if [ $? -eq 0 ]; then
64            ipdev=`echo $ipconf | cut -d: -f1`
65            ipmode="DHCP"
66        else
67            ipdev=`echo $ipconf | cut -d: -f1`
68            ipaddress=`echo $ipconf | cut -d: -f2`
69            ipnetmask=`echo $ipconf | cut -d: -f3`
70            ipbroadcast=`echo $ipconf | cut -d: -f4`
71            ipgateway=`echo $ipconf | cut -d: -f5`
72        fi
73    else
74        # Now relying on ipdev which isn't empty
75        # ipmode is either STATIC by default or DHCP if a parameter was passed
76        # If same system, map to the right MAC address
77
78        # Look at our previous device
79        hwaddr_found=`ifconfig $ipdev 2> /dev/null | grep HWaddr | awk '{print $NF}' | tr '[a-z]' '[A-Z]'`
80        if [ "$hwaddr_found" != "" ]; then
81            # That device always exists
82            if [ "$hwaddr" = "$hwaddr_found" ]; then
83                # No renaming took place use it
84            fi
85        else
86            # maybe there was a change of NIC numbering e.g.
87            # and the one used at backup doesn't exist anymore
88            # ipdev is not good anymore probably
89            oldipdev=$ipdev
90
91            for hwdev in `ifconfig -a | grep HWaddr | awk '{print $1}'` ; do
92                mac=`ifconfig $hwdev | grep HWaddr | awk '{print $NF}' | tr '[a-z]' '[A-Z]'`
93                if [ "$hwaddr" = "$mac" ]; then
94                    # This is the new device for the MAC address of our card at backup time
95                    LogIt "Interface with MAC address $hwaddr changed from $ipdev to $hwdev"
96                    LogIt "Using $hwdev for network configuration from now on"
97                    ipdev=$hwdev
98                    break
99                fi
100            done
101
102            if [ "$oldipdev" = "$ipdev" ]; then
103                # We didn't find a new device for this MAC
104                LogIt "NOTE: Interface $ipdev kept despite it doesn't match the $hwaddr MAC address"
105                ipdev=$oldipdev
106            fi
107
108            # TODO: Should we do that ?
109            if [ "$hwaddr" = "" ]; then
110                # No hwaddr stored at backup
111                # take the first interface and hope it's good
112                ipdev=`ifconfig -a | grep HWaddr | awk '{print $1}' | head -1`
113            fi
114        fi
115    fi
116
117    if [ "$ipmode" = "DHCP" ]; then
118        LogIt "Making DHCP request on $ipdev"
119        udhcpc -i $ipdev
120    else
121        LogIt "Configuring $ipdev statically ($ipaddress/$ipnetmask/$ipgateway)"
122        ifconfig $ipdev $ipaddress netmask $ipnetmask broadcast $ipbroadcast
123        route add default gw $ipgateway
124    fi
125    # Leave time to the stack to wake up (reported by some users)
126    sleep 5
127
128    # Handle a potential static route
129    if [ _"$iproute" != _"" ]; then
130        iproute=`echo $iproute | sed -i 's/:/ /g'`
131        LogIt "Adding static route with route add $iproute"
132        route add $iproute
133    fi
134
135    # ping server helps waking interface up
136    LogIt "Pinging Remote server..."
137    netfs_server_ipaddr=`echo $netfsmount | cut -d: -f1 | cut -d@ -f2`
138    ping -c $ipcount $netfs_server_ipaddr 
139
140    # Which is our protocol for file sharing
141    if [ "$proto" = "" ]; then
142        LogIt "No protocol specified, defaulting to NFS"
143        proto="nfs"
144    fi
145
146    if [ "$proto" = "sshfs" ]; then
147        LogIt "Mounting SSHFS share ($netfsmount) on /tmp/isodir..."
148        # We need a correct console for ssh
149        ln -sf /dev/console /dev/tty
150        sshfs -o ro,StrictHostKeyChecking=no $netfsopt $netfsmount /tmp/isodir
151    elif [ "$proto" = "smbfs" ]; then
152        LogIt "Mounting SMBFS share ($netfsmount) on /tmp/isodir..."
153        mount -t cifs $netfsmount /tmp/isodir -o ro $netfsopt 
154    elif [ "$proto" != "" ]; then
155        if [ -x /sbin/rpcbind ]; then
156            echo "Starting rpcbind daemon..."
157            /sbin/rpcbind -w &
158        fi
159        LogIt "Mounting Network share ($netfsmount) on /tmp/isodir..."
160        touch /etc/mtab
161        mount -t $proto -o nolock,ro $netfsopt $netfsmount /tmp/isodir
162    else
163        LogIt "Unknown protocol $proto"
164    fi
165   
166    LogIt "Mounting Remote image ${imgname}-1.iso in $dirimg on /mnt/cdrom in loopback"
167    mount -o ro,loop -t iso9660 /tmp/isodir/$dirimg/${imgname}-1.iso /mnt/cdrom
168
169    # Save the scripts on the Network share locally
170    if [ "`echo $pre | grep -E '^/tmp/isodir'`" ]; then
171        cp $pre /tmp
172        LogIt "Copying $pre under /tmp"
173    fi
174    if [ "`echo $post | grep -E '^/tmp/isodir'`" ]; then
175        cp $post /tmp
176        LogIt "Copying $post under /tmp"
177    fi
178fi
Note: See TracBrowser for help on using the repository browser.