From 0dfa3a78311376dd83811b016590343dad16ce65 Mon Sep 17 00:00:00 2001 From: torben Date: Thu, 2 Apr 2015 16:00:56 +0200 Subject: Remove old approach to get internet. Adding command line parsing for kernel parameter ip. --- testModule/module-setup.sh | 13 ++----- testModule/network.functions | 23 ++++++++++++ testModule/parse-ip.sh | 3 ++ testModule/setup_network.sh | 84 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 113 insertions(+), 10 deletions(-) create mode 100644 testModule/network.functions create mode 100644 testModule/parse-ip.sh create mode 100644 testModule/setup_network.sh (limited to 'testModule') diff --git a/testModule/module-setup.sh b/testModule/module-setup.sh index 7a4c4e2c..652602f5 100644 --- a/testModule/module-setup.sh +++ b/testModule/module-setup.sh @@ -1,7 +1,5 @@ #!/bin/bash -# dhclient -d -sf /usr/libexec/nm-dhcp-helper -pf /var/run/dhclient-enp0s3.pid -lf /var/lib/NetworkManager/dhclient-5a775495-24d5-4d13-8c06-0196cf621bf1-enp0s3.lease -cf /var/lib/NetworkManager/dhclient-enp0s3.conf enp0s3 - check() { return 0 } @@ -12,14 +10,9 @@ depends() { } install() { - inst_multiple ping ip ifconfig mount sshd htop dhclient NetworkManager - inst_simple "$moddir/ifcfg-enp0s3" /etc/sysconfig/network-scripts/ifcfg-enp0s3 - inst /etc/NetworkManager/dispatcher.d/00-netreport - inst /etc/NetworkManager/dispatcher.d/11-dhclient - inst /etc/NetworkManager/dnsmasq.d - inst /etc/NetworkManager/NetworkManager.conf - inst /etc/NetworkManager/system-connections - inst /etc/NetworkManager/VPN + inst_hook cmdline 20 "$moddir/parse-ip.sh" + inst_hook pre-mount 20 "$moddir/setup_network.sh" + inst_multiple ping ip ifconfig mount sshd htop dhclient return 0 } diff --git a/testModule/network.functions b/testModule/network.functions new file mode 100644 index 00000000..dc5dd52e --- /dev/null +++ b/testModule/network.functions @@ -0,0 +1,23 @@ +wait_for_iface() { +local DEVICE=$1 +local TIMEOUT=10 +echo -n "Waiting for interface $DEVICE: " +# Some systems don't have operstate. Seems to be hardware dependent +[ ! -e "/sys/class/net/${DEVICE}/operstate" ] && usleep 10000 +if [ -e "/sys/class/net/${DEVICE}/operstate" ]; then + while true; do + # check linkstate + [ "x$(cat "/sys/class/net/${DEVICE}/operstate")" == "xup" ] && break + TIMEOUT=$(( $TIMEOUT - 1 )) # don't wait forever, the pcnet iface of vmware will never be "up" although it's working + [ "$TIMEOUT" -le 0 ] && break + # else + echo -n "." + usleep 500000 + done +else + # we really don't have a operstate .. then just wait a sec and hope for the best. + sleep 1 +fi +echo ".$(cat "/sys/class/net/${DEVICE}/operstate" 2>/dev/null)" +} +true diff --git a/testModule/parse-ip.sh b/testModule/parse-ip.sh new file mode 100644 index 00000000..07f61d50 --- /dev/null +++ b/testModule/parse-ip.sh @@ -0,0 +1,3 @@ +for parameter in $(getargs ip=); do + COMMAND_LINE_IP="$parameter" +done diff --git a/testModule/setup_network.sh b/testModule/setup_network.sh new file mode 100644 index 00000000..b1334a2b --- /dev/null +++ b/testModule/setup_network.sh @@ -0,0 +1,84 @@ +#!/bin/ash + +echo "Setting up network..." + +echo "Main MAC address is '$MAC'" + +# setup network +source /inc/network.functions + +# set up loopback networking +echo "Setting up loopback" +ip link set dev lo up 2>/dev/null +ip addr add 127.0.0.1/8 dev lo 2>/dev/null + +echo "Setting up bridge" +BRIDGE="br0" + +# Following was supposed to prevent scripts from getting confused by multiple interfaces with same MAC - does not work though +## Flip mac address of original interface - this var is not local so init sees the changes too +#MAC="$(echo "$MAC" | awk -F ':' '{printf $1 ":" $2 ":" $5 ":" $3 ":" $6 ":" $4}')" +#ip link set addr "$MAC" "$SLAVE" + +mkdir -p "${FUTURE_ROOT}/etc/udev/rules.d" + +#IP_OUT=$(ip a | sed -r ':a;N;$!ba;s/: ([a-z0-9]+): /####\1####/g;s/ether ([a-f0-9:]+) /####\1####/g'| grep -E -o '####[^ ]+####' | sed 's/#//g' | grep -B 1 ':') +IP_OUT=$(ip a | grep -B 1 "/ether" | sed -r '/^--$/d;$!N;s#^[0-9]+: ([a-z0-9\.:]+): .*?/ether ([0-9a-fA-Z:]+) .*$#\1==\2#') + +if ! echo "$IP_OUT" | grep -q -- "$MAC"; then + drop_shell "Boot interface not found in interface list. NIC driver missing?" +fi + +for LINE in $IP_OUT; do + IFACE=$(echo "$LINE" | awk -F '==' '{printf $1}') + IFMAC=$(echo "$LINE" | awk -F '==' '{printf $2}' | tr '[A-Z]' '[a-z]') # udev requires mac addesses to be lowercase (a-f), see http://www.debianhelp.co.uk/udev.htm + echo "${IFACE} = ${IFMAC}" + + if [ "x$IFMAC" == "x$MAC" ]; then + brctl addbr "$BRIDGE" || drop_shell "Could not create bridge $BRIDGE" + brctl stp "$BRIDGE" 0 + brctl setfd "$BRIDGE" 0.000000000001 + ip link set addr "$IFMAC" "$BRIDGE" || drop_shell "Could not set mac of $BRIDGE" + ip link set dev "$IFACE" up + wait_for_iface "$IFACE" + brctl addif "$BRIDGE" "$IFACE" || drop_shell "Could not add $IFACE to $BRIDGE" + + # save our variables for retry on fail + echo "IFACE=$IFACE" > /run/network.conf + + # analyze ip information from the kernel command line and put parts + # of it into several variables + if [ -n "$CLIENTIP" ] ; then + # set static ip address + ip addr add "$CLIENTIP/$(ipcalc -s -p "$CLIENTIP" "$SUBNET_MASK" | sed "s/.*=//")" broadcast "$BROADCAST_ADDRESS" dev "$BRIDGE" + ip link set dev "$BRIDGE" up + [ -n "$GATEWAY" ] && ip route add default via "$GATEWAY" dev "$BRIDGE" + else + NOIPYET="yes" + fi + # Ignore this device later on when systemd handles network interfaces (see hacked 99-systemd.rules in systemd data dir) + echo "SUBSYSTEM==\"net\", ACTION==\"add\", KERNEL==\"eth*\", ATTR{address}==\"$IFMAC\", TAG+=\"openslxignore\"" >> "${FUTURE_ROOT}/etc/udev/rules.d/01-ignore-boot-interface.rules" + fi + # youdev + echo "SUBSYSTEM==\"net\", ACTION==\"add\", DRIVERS==\"?*\", ATTR{address}==\"$IFMAC\", ATTR{dev_id}==\"0x0\", ATTR{type}==\"1\", KERNEL==\"eth*\", NAME=\"$IFACE\"" >> "${FUTURE_ROOT}/etc/udev/rules.d/70-net-boot-nic-name.rules" + # continue... + IFACE="" +done + +wait_for_iface "$BRIDGE" + +# udhcpc +PARAM= +if [ -n "$CLIENTIP" ]; then + PARAM="-r $CLIENTIP" +fi +echo -n "$CLIENTIP" > "/run/firstip" +echo -n "$GATEWAY" > "/run/firstgw" + +# save our variables for retry on fail ff. +echo "CLIENTIP=$CLIENTIP" >> /run/network.conf +echo "GATEWAY=$GATEWAY" >> /run/network.conf +echo "BRIDGE=$BRIDGE" >> /run/network.conf + +udhcpc $PARAM -O domain -O nissrv -O nisdomain -O wpad -O search -t 5 -T 2 -s "/inc/udhcpc-trigger" -f -n -q -i "$BRIDGE" +# udhcpc return value will be return value of this script -- cgit v1.2.3-55-g7522