Skip to content

Commit 526b4a3

Browse files
authored
hook: fix vlan handling (#238)
#### hook: vlan.sh: fix 'parse_cmdline' bug; if no hw_addr specified, default ifname to eth0 - `parse_cmdline` is actually `parse_kernel_cmdline_for` - no reason to double-newline results - allow for simple vlan_id=xxx without hwaddr for single-interface or first-interface VLAN scenarios Signed-off-by: Ricardo Pardini <[email protected]> #### hook: introduce hook-ip container for vlan.sh - Based on linuxkit/ip pkg, sans wireguard stuff; add GNU sed needed for /proc/cmdline parsing Signed-off-by: Ricardo Pardini <[email protected]>
2 parents 9dc7c7d + 145a877 commit 526b4a3

File tree

5 files changed

+48
-21
lines changed

5 files changed

+48
-21
lines changed

bash/hook-lk-containers.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ function build_all_hook_linuxkit_containers() {
77
# when adding new container builds here you'll also want to add them to the
88
# `linuxkit_build` function in the linuxkit.sh file.
99
# # NOTE: linuxkit containers must be in the images/ directory
10+
build_hook_linuxkit_container hook-ip HOOK_CONTAINER_IP_IMAGE
1011
build_hook_linuxkit_container hook-bootkit HOOK_CONTAINER_BOOTKIT_IMAGE
1112
build_hook_linuxkit_container hook-docker HOOK_CONTAINER_DOCKER_IMAGE
1213
build_hook_linuxkit_container hook-mdev HOOK_CONTAINER_MDEV_IMAGE

bash/linuxkit.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function linuxkit_build() {
5050
fi
5151

5252
# Build the containers in this repo used in the LinuxKit YAML;
53-
build_all_hook_linuxkit_containers # sets HOOK_CONTAINER_BOOTKIT_IMAGE, HOOK_CONTAINER_DOCKER_IMAGE, HOOK_CONTAINER_MDEV_IMAGE, HOOK_CONTAINER_CONTAINERD_IMAGE
53+
build_all_hook_linuxkit_containers # sets HOOK_CONTAINER_IP_IMAGE, HOOK_CONTAINER_BOOTKIT_IMAGE, HOOK_CONTAINER_DOCKER_IMAGE, HOOK_CONTAINER_MDEV_IMAGE, HOOK_CONTAINER_CONTAINERD_IMAGE
5454

5555
# Template the linuxkit configuration file.
5656
# - You'd think linuxkit would take --build-args or something by now, but no.
@@ -64,12 +64,13 @@ function linuxkit_build() {
6464
# shellcheck disable=SC2016 # I'm using single quotes to avoid shell expansion, envsubst wants the dollar signs.
6565
cat "linuxkit-templates/${kernel_info['TEMPLATE']}.template.yaml" |
6666
HOOK_KERNEL_IMAGE="${kernel_oci_image}" HOOK_KERNEL_ID="${inventory_id}" HOOK_KERNEL_VERSION="${kernel_oci_version}" \
67+
HOOK_CONTAINER_IP_IMAGE="${HOOK_CONTAINER_IP_IMAGE}" \
6768
HOOK_CONTAINER_BOOTKIT_IMAGE="${HOOK_CONTAINER_BOOTKIT_IMAGE}" \
6869
HOOK_CONTAINER_DOCKER_IMAGE="${HOOK_CONTAINER_DOCKER_IMAGE}" \
6970
HOOK_CONTAINER_MDEV_IMAGE="${HOOK_CONTAINER_MDEV_IMAGE}" \
7071
HOOK_CONTAINER_CONTAINERD_IMAGE="${HOOK_CONTAINER_CONTAINERD_IMAGE}" \
7172
HOOK_CONTAINER_RUNC_IMAGE="${HOOK_CONTAINER_RUNC_IMAGE}" \
72-
envsubst '$HOOK_VERSION $HOOK_KERNEL_IMAGE $HOOK_KERNEL_ID $HOOK_KERNEL_VERSION $HOOK_CONTAINER_BOOTKIT_IMAGE $HOOK_CONTAINER_DOCKER_IMAGE $HOOK_CONTAINER_MDEV_IMAGE $HOOK_CONTAINER_CONTAINERD_IMAGE $HOOK_CONTAINER_RUNC_IMAGE' \
73+
envsubst '$HOOK_VERSION $HOOK_KERNEL_IMAGE $HOOK_KERNEL_ID $HOOK_KERNEL_VERSION $HOOK_CONTAINER_IP_IMAGE $HOOK_CONTAINER_BOOTKIT_IMAGE $HOOK_CONTAINER_DOCKER_IMAGE $HOOK_CONTAINER_MDEV_IMAGE $HOOK_CONTAINER_CONTAINERD_IMAGE $HOOK_CONTAINER_RUNC_IMAGE' \
7374
> "hook.${inventory_id}.yaml"
7475

7576
declare -g linuxkit_bin=""

files/vlan.sh

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function parse_kernel_cmdline_for() {
2626
if [ -z "${result}" ]; then
2727
return 1
2828
else
29-
printf "%s\n" "$result"
29+
printf "%s" "$result"
3030
fi
3131
}
3232

@@ -43,15 +43,14 @@ function add_vlan_interface() {
4343

4444
# check if hw_addr are set in the kernel commandline, otherwise return.
4545
if ! kernel_cmdline_exists hw_addr; then
46-
echo "No hw_addr=xx:xx:xx:xx:xx:xx set in kernel commandline; no VLAN handling." >&2
47-
return
46+
echo "No hw_addr=xx:xx:xx:xx:xx:xx set in kernel commandline." >&2
4847
fi
4948

5049
echo "Starting VLAN handling, parsing..." >&2
5150

5251
declare vlan_id hw_addr
53-
vlan_id="$(parse_cmdline vlan_id)"
54-
hw_addr="$(parse_cmdline hw_addr)"
52+
vlan_id="$(parse_kernel_cmdline_for vlan_id)"
53+
hw_addr="$(parse_kernel_cmdline_for hw_addr)"
5554

5655
echo "VLAN handling - vlan_id: '${vlan_id}', hw_addr: '${hw_addr}'" >&2
5756

@@ -60,21 +59,23 @@ function add_vlan_interface() {
6059
echo "VLAN handling - vlan_id: '${vlan_id}', hw_addr: '${hw_addr}', searching for interface..." >&2
6160
ifname="$(ip -br link | awk '$3 ~ /'"${hw_addr}"'/ {print $1}')"
6261
echo "VLAN handling - vlan_id: '${vlan_id}', hw_addr: '${hw_addr}', found interface: '${ifname}'" >&2
63-
if [ -n "$ifname" ]; then
64-
echo "VLAN handling - vlan_id: '${vlan_id}', hw_addr: '${hw_addr}', adding VLAN interface..." >&2
65-
ip link set dev "${ifname}" up || true
66-
ip link add link "${ifname}" name "${ifname}.${vlan_id}" type vlan id "${vlan_id}" || true
67-
ip link set "${ifname}.${vlan_id}" up || true
68-
echo "VLAN handling - vlan_id: '${vlan_id}', hw_addr: '${hw_addr}', added VLAN interface: '${ifname}.${vlan_id}'" >&2
69-
return 0
70-
else
71-
echo "VLAN handling - vlan_id: '${vlan_id}', hw_addr: '${hw_addr}', no interface found for hw_addr." >&2
72-
return 3
73-
fi
7462
else
75-
echo "VLAN handling - vlan_id: '${vlan_id}', hw_addr: '${hw_addr}', no hw_addr found in kernel commandline." >&2
76-
return 2
63+
echo "VLAN handling - vlan_id: '${vlan_id}', hw_addr: '${hw_addr}', no hw_addr found in kernel commandline; default ifname to eth0." >&2
64+
ifname="eth0"
7765
fi
66+
67+
if [ -n "$ifname" ]; then
68+
echo "VLAN handling - vlan_id: '${vlan_id}', hw_addr: '${hw_addr}', adding VLAN interface..." >&2
69+
ip link set dev "${ifname}" up || true
70+
ip link add link "${ifname}" name "${ifname}.${vlan_id}" type vlan id "${vlan_id}" || true
71+
ip link set "${ifname}.${vlan_id}" up || true
72+
echo "VLAN handling - vlan_id: '${vlan_id}', hw_addr: '${hw_addr}', added VLAN interface: '${ifname}.${vlan_id}'" >&2
73+
return 0
74+
else
75+
echo "VLAN handling - vlan_id: '${vlan_id}', hw_addr: '${hw_addr}', no interface found for hw_addr." >&2
76+
return 3
77+
fi
78+
7879
else
7980
echo "VLAN handling - vlan_id: '${vlan_id}', hw_addr: '${hw_addr}', no vlan_id found in kernel commandline." >&2
8081
return 1

images/hook-ip/Dockerfile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
FROM linuxkit/alpine:146f540f25cd92ec8ff0c5b0c98342a9a95e479e AS mirror
2+
RUN mkdir -p /out/etc/apk && cp -r /etc/apk/* /out/etc/apk/
3+
RUN apk add curl
4+
RUN apk add --no-cache --initdb -p /out \
5+
alpine-baselayout \
6+
bash \
7+
busybox \
8+
iproute2 \
9+
iptables \
10+
ebtables \
11+
ipvsadm \
12+
bridge-utils \
13+
musl \
14+
sed
15+
16+
# Remove apk residuals
17+
RUN rm -rf /out/etc/apk /out/lib/apk /out/var/cache
18+
19+
FROM scratch
20+
ENTRYPOINT []
21+
CMD []
22+
WORKDIR /
23+
COPY --from=mirror /out/ /

linuxkit-templates/hook.template.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# - HOOK_KERNEL_IMAGE: ${HOOK_KERNEL_IMAGE}
44
# - HOOK_KERNEL_ID: ${HOOK_KERNEL_ID}
55
# - HOOK_KERNEL_VERSION: ${HOOK_KERNEL_VERSION}
6+
# - HOOK_CONTAINER_IP_IMAGE: ${HOOK_CONTAINER_IP_IMAGE}
67
# - HOOK_CONTAINER_BOOTKIT_IMAGE: ${HOOK_CONTAINER_BOOTKIT_IMAGE}
78
# - HOOK_CONTAINER_DOCKER_IMAGE: ${HOOK_CONTAINER_DOCKER_IMAGE}
89
# - HOOK_CONTAINER_MDEV_IMAGE: ${HOOK_CONTAINER_MDEV_IMAGE}
@@ -38,7 +39,7 @@ onboot:
3839
command: [ "modprobe", "cdc_ncm" ] # for usb ethernet dongles
3940

4041
- name: vlan
41-
image: linuxkit/ip:v1.0.0
42+
image: "${HOOK_CONTAINER_IP_IMAGE}"
4243
capabilities:
4344
- all
4445
binds.add:

0 commit comments

Comments
 (0)