[742] | 1 | #!/bin/sh
|
---|
[740] | 2 | #
|
---|
[741] | 3 | # $Id$
|
---|
| 4 | #
|
---|
[2380] | 5 | # This script sets up the network + Network FS environment if needed.
|
---|
[741] | 6 | #
|
---|
| 7 |
|
---|
| 8 |
|
---|
[740] | 9 | # number of ping
|
---|
| 10 | ipcount=3
|
---|
| 11 |
|
---|
| 12 | # Get info from config file
|
---|
[3271] | 13 | ipdev=`grep netfs-dev /tmp/mondorestore.cfg 2> /dev/null | cut -d' ' -f2-`
|
---|
[3433] | 14 | # MAC address in uppercases
|
---|
[3440] | 15 | hwaddr=`grep netfs-client-hwaddr /tmp/mondorestore.cfg 2> /dev/null | cut -d' ' -f2- | tr '[a-z]' '[A-Z]'`
|
---|
[3271] | 16 | ipaddress=`grep netfs-client-ipaddr /tmp/mondorestore.cfg 2> /dev/null | cut -d' ' -f2-`
|
---|
| 17 | ipnetmask=`grep netfs-client-netmask /tmp/mondorestore.cfg 2> /dev/null | cut -d' ' -f2-`
|
---|
| 18 | ipbroadcast=`grep netfs-client-broadcast /tmp/mondorestore.cfg 2> /dev/null | cut -d' ' -f2-`
|
---|
| 19 | ipgateway=`grep netfs-client-defgw /tmp/mondorestore.cfg 2> /dev/null | cut -d' ' -f2-`
|
---|
| 20 | proto=`grep netfs-proto /tmp/mondorestore.cfg 2> /dev/null | cut -d' ' -f2-`
|
---|
| 21 | iproute=`grep netfs-route /tmp/mondorestore.cfg 2> /dev/null | cut -d' ' -f2-`
|
---|
[740] | 22 | ipconf=""
|
---|
[3443] | 23 | ipmode="STATIC"
|
---|
[2255] | 24 | pre=""
|
---|
| 25 | post=""
|
---|
[3271] | 26 | export netfsmount=`grep netfs-server-mount /tmp/mondorestore.cfg 2> /dev/null | cut -d' ' -f2-`
|
---|
| 27 | export imgname=`grep iso-prefix /tmp/mondorestore.cfg 2> /dev/null | cut -d' ' -f2-`
|
---|
[740] | 28 | if [ "$imgname" = "" ]; then
|
---|
[801] | 29 | export imgname="mondorescue"
|
---|
[740] | 30 | fi
|
---|
[3278] | 31 | export dirimg=`grep iso-dir /tmp/mondorestore.cfg 2> /dev/null | cut -d' ' -f2-`
|
---|
[740] | 32 | if [ "$dirimg" = "" ]; then
|
---|
[801] | 33 | export dirimg="/"
|
---|
[740] | 34 | fi
|
---|
| 35 |
|
---|
| 36 | # info from cmdline are predominent
|
---|
| 37 | for i in `cat /proc/cmdline` ; do
|
---|
| 38 | echo $i | grep -qi ping= && ipcount=`echo $i | cut -d= -f2`
|
---|
[3440] | 39 | echo $i | grep -qi hwaddr= && hwaddr=`echo $i | cut -d= -f2 | tr '[a-z]' '[A-Z]'`
|
---|
[740] | 40 | echo $i | grep -qi ipconf= && ipconf=`echo $i | cut -d= -f2`
|
---|
[2380] | 41 | echo $i | grep -qi netfsmount= && export netfsmount=`echo $i | cut -d= -f2`
|
---|
[2498] | 42 | echo $i | grep -qi netfsopt= && export netfsopt=`echo $i | cut -d= -f2`
|
---|
[801] | 43 | echo $i | grep -qi prefix= && export imgname=`echo $i | cut -d= -f2`
|
---|
[2380] | 44 | echo $i | grep -qi netfspath= && export dirimg=`echo $i | cut -d= -f2`
|
---|
[2255] | 45 | echo $i | grep -qi pre= && pre=`echo $i | cut -d= -f2`
|
---|
| 46 | echo $i | grep -qi post= && post=`echo $i | cut -d= -f2`
|
---|
[2380] | 47 | echo $i | grep -qi proto= && proto=`echo $i | cut -d= -f2`
|
---|
[3168] | 48 | echo $i | grep -qi iproute= && iproute=`echo $i | cut -d= -f2`
|
---|
[3443] | 49 | echo $i | grep -qi ipmode= && ipmode=`echo $i | cut -d= -f2`
|
---|
[740] | 50 | done
|
---|
| 51 |
|
---|
[1830] | 52 | if [ "$ipdev" != "" ] || [ "$ipconf" != "" ]; then
|
---|
| 53 | # Network configuration stored during archive
|
---|
| 54 | # or on cmdline so network wanted => starting it
|
---|
[3444] | 55 | LogIt "INFO: Starting Network..."
|
---|
[740] | 56 |
|
---|
[1830] | 57 | # Activate loobback
|
---|
| 58 | ifconfig lo 127.0.0.1
|
---|
[740] | 59 |
|
---|
[3443] | 60 | # ipconf has priority
|
---|
| 61 | if [ "$ipconf" != "" ]; then
|
---|
| 62 | echo "$ipconf" | grep -q "dhcp"
|
---|
| 63 | if [ $? -eq 0 ]; then
|
---|
[1830] | 64 | ipdev=`echo $ipconf | cut -d: -f1`
|
---|
[3443] | 65 | ipmode="DHCP"
|
---|
| 66 | else
|
---|
| 67 | ipdev=`echo $ipconf | cut -d: -f1`
|
---|
[1830] | 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
|
---|
[3443] | 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
|
---|
[2887] | 77 |
|
---|
[3443] | 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
|
---|
[3444] | 84 | LogIt "INFO: Using device $ipdev (Mac Addr: $hwaddr_found)"
|
---|
[3443] | 85 | fi
|
---|
| 86 | else
|
---|
| 87 | # maybe there was a change of NIC numbering e.g.
|
---|
| 88 | # and the one used at backup doesn't exist anymore
|
---|
| 89 | # ipdev is not good anymore probably
|
---|
| 90 | oldipdev=$ipdev
|
---|
| 91 |
|
---|
| 92 | for hwdev in `ifconfig -a | grep HWaddr | awk '{print $1}'` ; do
|
---|
| 93 | mac=`ifconfig $hwdev | grep HWaddr | awk '{print $NF}' | tr '[a-z]' '[A-Z]'`
|
---|
| 94 | if [ "$hwaddr" = "$mac" ]; then
|
---|
| 95 | # This is the new device for the MAC address of our card at backup time
|
---|
[3444] | 96 | LogIt "INFO: Interface with MAC address $hwaddr changed from $ipdev to $hwdev"
|
---|
| 97 | LogIt "INFO: Using $hwdev for network configuration from now on"
|
---|
[3443] | 98 | ipdev=$hwdev
|
---|
| 99 | break
|
---|
[3271] | 100 | fi
|
---|
[3443] | 101 | done
|
---|
| 102 |
|
---|
| 103 | if [ "$oldipdev" = "$ipdev" ]; then
|
---|
[3445] | 104 | # We didn't find a new device for this MAC and it doesn't exist
|
---|
| 105 | # Take the first interface and hope it's good
|
---|
[3443] | 106 | ipdev=`ifconfig -a | grep HWaddr | awk '{print $1}' | head -1`
|
---|
[3445] | 107 | LogIt "WARN: Using $ipdev as no device matches the MAC address $hwaddr"
|
---|
[3443] | 108 | fi
|
---|
[2887] | 109 | fi
|
---|
[3443] | 110 | fi
|
---|
[2887] | 111 |
|
---|
[3443] | 112 | if [ "$ipmode" = "DHCP" ]; then
|
---|
[3444] | 113 | LogIt "INFO: Making DHCP request on $ipdev"
|
---|
[3443] | 114 | udhcpc -i $ipdev
|
---|
| 115 | else
|
---|
[3444] | 116 | LogIt "INFO: Configuring $ipdev statically ($ipaddress/$ipnetmask/$ipgateway)"
|
---|
[1830] | 117 | ifconfig $ipdev $ipaddress netmask $ipnetmask broadcast $ipbroadcast
|
---|
| 118 | route add default gw $ipgateway
|
---|
[740] | 119 | fi
|
---|
[3168] | 120 | # Leave time to the stack to wake up (reported by some users)
|
---|
[2887] | 121 | sleep 5
|
---|
| 122 |
|
---|
[3168] | 123 | # Handle a potential static route
|
---|
| 124 | if [ _"$iproute" != _"" ]; then
|
---|
[3440] | 125 | iproute=`echo $iproute | sed -i 's/:/ /g'`
|
---|
[3444] | 126 | LogIt "INFO: Adding static route with route add $iproute"
|
---|
[3168] | 127 | route add $iproute
|
---|
| 128 | fi
|
---|
| 129 |
|
---|
[1830] | 130 | # ping server helps waking interface up
|
---|
[3444] | 131 | LogIt "INFO: Pinging Remote server..."
|
---|
[2380] | 132 | netfs_server_ipaddr=`echo $netfsmount | cut -d: -f1 | cut -d@ -f2`
|
---|
| 133 | ping -c $ipcount $netfs_server_ipaddr
|
---|
[740] | 134 |
|
---|
[2380] | 135 | # Which is our protocol for file sharing
|
---|
| 136 | if [ "$proto" = "" ]; then
|
---|
[3444] | 137 | LogIt "WARN: No protocol specified, defaulting to NFS"
|
---|
[2709] | 138 | proto="nfs"
|
---|
[2380] | 139 | fi
|
---|
| 140 |
|
---|
| 141 | if [ "$proto" = "sshfs" ]; then
|
---|
[3444] | 142 | LogIt "INFO: Mounting SSHFS share ($netfsmount) on /tmp/isodir..."
|
---|
[2438] | 143 | # We need a correct console for ssh
|
---|
| 144 | ln -sf /dev/console /dev/tty
|
---|
[2498] | 145 | sshfs -o ro,StrictHostKeyChecking=no $netfsopt $netfsmount /tmp/isodir
|
---|
[3111] | 146 | elif [ "$proto" = "smbfs" ]; then
|
---|
[3444] | 147 | LogIt "INFO: Mounting SMBFS share ($netfsmount) on /tmp/isodir..."
|
---|
[3279] | 148 | mount -t cifs $netfsmount /tmp/isodir -o ro $netfsopt
|
---|
[2380] | 149 | elif [ "$proto" != "" ]; then
|
---|
[2951] | 150 | if [ -x /sbin/rpcbind ]; then
|
---|
[3271] | 151 | echo "Starting rpcbind daemon..."
|
---|
[3452] | 152 | mkdir -p /var/cache/rpcbind /var/lib/rpcbind
|
---|
[3261] | 153 | /sbin/rpcbind -w &
|
---|
[2951] | 154 | fi
|
---|
[3444] | 155 | LogIt "INFO: Mounting Network share ($netfsmount) on /tmp/isodir..."
|
---|
[2380] | 156 | touch /etc/mtab
|
---|
[2498] | 157 | mount -t $proto -o nolock,ro $netfsopt $netfsmount /tmp/isodir
|
---|
[2380] | 158 | else
|
---|
[3444] | 159 | LogIt "ERROR: Unknown protocol $proto"
|
---|
[2380] | 160 | fi
|
---|
[1830] | 161 |
|
---|
[3444] | 162 | LogIt "INFO: Mounting Remote image ${imgname}-1.iso in $dirimg on /mnt/cdrom in loopback"
|
---|
[1927] | 163 | mount -o ro,loop -t iso9660 /tmp/isodir/$dirimg/${imgname}-1.iso /mnt/cdrom
|
---|
[2253] | 164 |
|
---|
[2380] | 165 | # Save the scripts on the Network share locally
|
---|
[2255] | 166 | if [ "`echo $pre | grep -E '^/tmp/isodir'`" ]; then
|
---|
| 167 | cp $pre /tmp
|
---|
[3444] | 168 | LogIt "INFO: Copying $pre under /tmp"
|
---|
[2253] | 169 | fi
|
---|
[2255] | 170 | if [ "`echo $post | grep -E '^/tmp/isodir'`" ]; then
|
---|
| 171 | cp $post /tmp
|
---|
[3444] | 172 | LogIt "INFO: Copying $post under /tmp"
|
---|
[2255] | 173 | fi
|
---|
[1830] | 174 | fi
|
---|