Skip to content

Commit a8240ca

Browse files
Remove address from non tagged interface:
Boots send kernel command line parameter "ip=dhcp", this causes the system to configure the network interface(s) with DHCP. When an environment's network configuration has this machine connected a trunked interface with a default/native VLAN, the interface will be configured on the default/native VLAN because we haven't yet configured the VLAN interface. Boots will respond to this DHCP request because in this scenario it is not VLAN aware. Also in this scenario, the machine will end up being configured with 2 default routes. To resolve this, we remove the default route and IP that kernel added and let the dhcpcd handle setting the route. Signed-off-by: Jacob Weinstock <jakobweinstock@gmail.com>
1 parent 27ec830 commit a8240ca

File tree

4 files changed

+100
-19
lines changed

4 files changed

+100
-19
lines changed

files/dhcp.sh

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/bin/sh
2+
3+
# This script will run the dhcp client. If `vlan_id=` in `/proc/cmdline` has a value, it will run the dhcp client only on the
4+
# VLAN interface.
5+
# This script accepts an input parameter of true or false.
6+
# true: run the dhcp client with the one shot option
7+
# false: run the dhcp client as a service
8+
set -x
9+
10+
run_dhcp_client() {
11+
one_shot="$1"
12+
al="eth*"
13+
14+
# shellcheck disable=SC2013
15+
for x in $(cat /proc/cmdline); do
16+
# shellcheck disable=SC2022
17+
echo "$x" | grep -qe 'vlan_id*' || continue
18+
vlan_id="${x#vlan_id=}"
19+
if [ -n "$vlan_id" ]; then
20+
al="eth*.*"
21+
fi
22+
done
23+
24+
# Boots send kernel command line parameter "ip=dhcp", this causes the system to configure the network interface(s) with DHCP.
25+
# When an environment's network configuration has this machine connected a trunked interface with a default/native VLAN, the
26+
# interface will be configured on the default/native VLAN because we haven't yet configured the VLAN interface. Boots will respond
27+
# to this DHCP request because in this scenario it is not VLAN aware. Also in this scenario, the machine will end up being configured
28+
# with 2 default routes. To resolve this, we remove the default route and IP that kernel added and let the dhcpcd handle setting the route.
29+
ip route del default || true
30+
ipa=$(ip -4 -o addr show dev eth0 | awk '{print $4}')
31+
ip addr del dev eth0 "$ipa" || true
32+
33+
if [ "$one_shot" = "true" ]; then
34+
/sbin/dhcpcd --nobackground -f /dhcpcd.conf --allowinterfaces "${al}" -1
35+
else
36+
/sbin/dhcpcd --nobackground -f /dhcpcd.conf --allowinterfaces "${al}"
37+
fi
38+
39+
40+
}
41+
42+
# we always return true so that a failure here doesn't block the next container service from starting. Ideally, we always
43+
# want the getty service to start so we can debug failures.
44+
run_dhcp_client "$1" || true

files/dhcpcd.conf

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Default values for dhcpcd from linuxkit/dhcpcd:v0.8 with `allowinterfaces eth*` removed
2+
# This allows the `--allowinterfaces` flag of dhcpcd to specify the allowinterfaces.
3+
hostname
4+
clientid
5+
persistent
6+
option rapid_commit
7+
option domain_name_servers, domain_name, domain_search, host_name
8+
option classless_static_routes
9+
option ntp_servers
10+
option interface_mtu
11+
require dhcp_server_identifier
12+
slaac private
13+
nodelay
14+
noarp
15+
waitip 4

files/vlan.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/bin/sh
2+
3+
# This script will set up VLAN interfaces if `vlan_id=` in `/proc/cmdline` has a value
4+
set -x
5+
6+
add_vlan_interface() {
7+
# shellcheck disable=SC2013
8+
for param in $(cat /proc/cmdline); do
9+
# shellcheck disable=SC2022
10+
echo "$param" | grep -qe 'vlan_id*' || continue
11+
vlan_id="${param#vlan_id=}"
12+
if [ -n "$vlan_id" ]; then
13+
for ifname in $(ip -4 -o link show | awk -F': ' '{print $2}'); do
14+
[ "$ifname" = "lo" ] && continue
15+
[ "$ifname" = "docker0" ] && continue
16+
ip link add link "$ifname" name "$ifname.$vlan_id" type vlan id "$vlan_id"
17+
ip link set "$ifname.$vlan_id" up
18+
done
19+
return
20+
fi
21+
done
22+
}
23+
24+
# we always return true so that a failure here doesn't block the next container service from starting. Ideally, we always
25+
# want the getty service to start so we can debug failures.
26+
add_vlan_interface || true

hook.yaml

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ onboot:
2323

2424
- name: dhcpcd
2525
image: linuxkit/dhcpcd:v0.8
26-
command: ["/sbin/dhcpcd", "--nobackground", "-f", "/dhcpcd.conf", "-1"]
26+
command: ["/etc/ip/dhcp.sh", "true"]
2727
binds.add:
2828
- /var/lib/dhcpcd:/var/lib/dhcpcd
2929
- /run:/run
30+
- /etc/ip/dhcp.sh:/etc/ip/dhcp.sh
31+
- /dhcpcd.conf:/dhcpcd.conf
3032
runtime:
3133
mkdir:
3234
- /var/lib/dhcpcd
@@ -44,9 +46,12 @@ services:
4446

4547
- name: dhcpcd
4648
image: linuxkit/dhcpcd:v0.8
49+
command: ["/etc/ip/dhcp.sh", "false"]
4750
binds.add:
4851
- /var/lib/dhcpcd:/var/lib/dhcpcd
4952
- /run:/run
53+
- /etc/ip/dhcp.sh:/etc/ip/dhcp.sh
54+
- /dhcpcd.conf:/dhcpcd.conf
5055
runtime:
5156
mkdir:
5257
- /var/lib/dhcpcd
@@ -104,26 +109,17 @@ files:
104109
mode: "0644"
105110

106111
- path: etc/ip/vlan.sh
107-
contents: |
108-
#!/bin/sh
109-
# This script will set up VLAN interfaces if `vlan_id=` in `/proc/cmdline` has a value
110-
set -x
111-
112-
for x in $(cat /proc/cmdline); do
113-
echo "$x" | grep -qe 'vlan_id*' || continue
114-
vlan_id="${x#vlan_id=}"
115-
if [ ! -z "$vlan_id" ]; then
116-
for x in $(ip -4 -o link show | awk -F': ' '{print $2}'); do
117-
[ "$x" == "lo" ] && continue
118-
[ "$x" == "docker0" ] && continue
119-
ip link add link "$x" name "$x.$vlan_id" type vlan id "$vlan_id"
120-
ip link set "$x.$vlan_id" up
121-
done
122-
exit 0
123-
fi
124-
done
112+
source: "files/vlan.sh"
113+
mode: "0777"
114+
115+
- path: etc/ip/dhcp.sh
116+
source: "files/dhcp.sh"
125117
mode: "0777"
126118

119+
- path: dhcpcd.conf
120+
source: "files/dhcpcd.conf"
121+
mode: "0644"
122+
127123
#dbg - path: root/.ssh/authorized_keys
128124
#dbg source: ~/.ssh/id_rsa.pub
129125
#dbg mode: "0600"

0 commit comments

Comments
 (0)