summaryrefslogtreecommitdiffstats
path: root/core/rootfs/rootfs-stage31/data/inc/setup_network
blob: 8f328eb2fe816fcfec9336f6d7e795a7c625d7f0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/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"

for i in 1 1 END; do
	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#' )"
	echo "$IP_OUT" | grep -q -F -- "$MAC" && break
	[ "x$i" = "xEND" ] && break
	sleep "$i"
done

if ! echo "$IP_OUT" | grep -q -F -- "$MAC"; then
	drop_shell "---
$(ip a)
---
$IP_OUT
---

Boot interface $MAC not found in interface list. NIC driver missing?
Check output of dmesg for missing firmware (dmesg | more)"
	:
fi

ADD_NIC=1
for LINE in $IP_OUT; do
	IFACE="${LINE%==*}"
	IFMAC="$( echo "${LINE#*==}" | 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 [ -z "$MAC" ]; then
		echo "No main MAC address given, trying first interface found..."
		MAC="$IFMAC"
	fi

	if [ "x$IFMAC" == "x$MAC" ]; then
		ip link set dev "$IFACE" up
		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"
		wait_for_iface "$IFACE" 8
		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

		ip link set dev "$BRIDGE" up
		# 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"
	else
		# Additional NIC - prepare bridge in case we want to add these to a VM or do other fancy things
		ADD_BR="br-nic-$ADD_NIC"
		brctl addbr "$ADD_BR"
		brctl stp "$ADD_BR" 0
		ip link set addr "$IFMAC" "$ADD_BR"
		ip link set dev "$IFACE" up
		brctl addif "$ADD_BR" "$IFACE"
		ip link set dev "$ADD_BR" up
		ADD_NIC=$(( ADD_NIC + 1 ))
	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" 5

# See if we got all required attributes via KCL, in that case skip DHCP
# TODO: Supply DNS servers in config from boot server, so we can even work witout it
if [ -n "$ip" ] && [ -n "$router" ] && [ -n "$dns" ]; then
	echo "Skipping DHCP since we have new style KCL"
	interface="${BRIDGE}" /inc/udhcpc-trigger bound && return 0
fi

# udhcpc
PARAM=
if [ -n "$ip" ]; then
	PARAM="-r ${ip%/*}"
fi

if [ "$USE_DHCP_UUID" = "yes" ]; then
	UID=$(dmidecode -s system-uuid | sed -r 's/^(..)(..)(..)(..)-(..)(..)-(..)(..)-(....)-/00\4\3\2\1\6\5\8\7\9/')
	if [ "${#UID}" = 34 ]; then
		echo "Using SMBIOS UID for DHCP"
		PARAM="$PARAM -x 0x3d:$UID"
	fi
fi

# save our variables for retry on fail ff.
echo "ip=$ip" >> /run/network.conf
echo "router=$router" >> /run/network.conf
echo "BRIDGE=$BRIDGE" >> /run/network.conf
echo "UID=$UID" >> /run/network.conf

# Workaround some buggy e1000e versions that crap themselves if we start doing stuff right after
# the interface state changed to up
sleep 1

udhcpc $PARAM -O hostname -O ntpsrv -O domain -O search -O dns -t 5 -T 3 -A 4 -s "/inc/udhcpc-trigger" -f -n -q -i "$BRIDGE"
URET=$?
# if these were empty before, udhcpc might have filled them in
[ -z "$ip" ] && ip=$(cat /run/firstip)
[ -z "$router" ] && router=$(cat /run/firstgw)
# udhcpc return value will be return value of this script
return $URET