summaryrefslogtreecommitdiffstats
path: root/core/modules/dhcpc-busybox/data/opt/openslx/scripts/systemd-bridge_additional_nics
blob: f508257fbe77e0d45d7259cd7209f24e04458add (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
#!/bin/bash
#
# Small script scanning sysfs for physical network interfaces
# and creating additional network bridges 'br-nic-[0-9]'.

. /opt/openslx/config

# hack, should be sent by slx-admin (2022-05-09)
[ "$SLX_RUNMODE_MODULE" = "dnbd3" ] && SLX_DHCP_OTHER_NICS="yes"

# all features disabled
[ "$SLX_BRIDGE_OTHER_NICS" != "yes" ] \
	&& [ "$SLX_DHCP_OTHER_NICS" != "yes" ] \
	&& exit 0

declare -a slaves
readarray -s 1 -t slaves < <( brctl show | awk '{print $NF}' )

is_slave() {
	for i in "${slaves[@]}"; do
		[ "$i" == "$1" ] && return 0
	done
	return 1
}

declare -g id=1
for nic in /sys/class/net/*; do
	ifname="${nic##*/}"
	# The presence of this symlink pointing to the physical device
	# seems to be a good way to detect real/physical intefaces.
	if ! [ -h "${nic}/device" ]; then
		echo "Skipping ${ifname}, not physical NIC"
		continue
	fi
	# do not handle the primary interface
	if [ "$SLX_PXE_NETIF" = "${ifname}" ]; then
		echo "Skipping ${ifname}, is boot interface"
		continue
	fi
	# already part of a bridge?
	if is_slave "${ifname}"; then
		echo "Skipping ${ifname}, already part of a bridge"
		continue
	fi

	if [ "$SLX_BRIDGE_OTHER_NICS" = "yes" ]; then
		# bridge
		# create a bridge with the same MAC
		bridge="br-nic-${id}"
		echo "Bridging ${ifname} via ${bridge}..."
		mac="$(cat "${nic}/address")"
		if ! [[ $mac =~ ^([0-9a-f]{2}:){5}[0-9a-f]{2}$ ]]; then
			echo "'$mac' does not seem like a valid MAC address." >&2
			continue
		fi

		(
		set -e
		brctl addbr "$bridge"
		brctl stp "$bridge" 0
		ip link set addr "$mac" "$bridge"
		ip link set dev "${ifname}" up
		brctl addif "$bridge" "${ifname}"
		ip link set dev "$bridge" up
		)
		ret=$?
		if [ "$ret" != 0 ]; then
			echo "Failed to setup additional bridge '$bridge' for '$ifname'." >&2
			brctl delbr "$bridge"
			continue
		fi
		# all fine, increase counter
		(( id++ ))
		# also, replace ifname with bridge name, in case we do DHCP below
		ifname="$bridge"
	fi
	# DHCP?
	if [ "$SLX_DHCP_OTHER_NICS" = "yes" ]; then
		# DHCP.
		echo "Starting DHCP service for ${ifname}..."
		ip link set dev "${ifname}" up
		systemctl --no-block start "dhcpc@${ifname}.service"
	fi
done

exit 0