# The spare interface: eth1, cabled to a small lab network, # 192.168.50.0/24, where a machine answers at 192.168.50.1 (and # fd00:50::1). ip link show eth1 >/dev/null 2>&1 && refuse "this VM already has an eth1; the lab adds its own spare eth1" command -v nmcli >/dev/null && systemctl is-active --quiet NetworkManager || refuse "this lab needs NetworkManager running" mkdir -p /usr/local/lib/le-lab cat >/usr/local/lib/le-lab/spare-net.sh <<'X' #!/bin/sh # The lab's spare network: eth1 here, the gateway 192.168.50.1 in the # le-net50 namespace. Run at every boot. ip netns list | grep -qw le-net50 || ip netns add le-net50 ip -n le-net50 link set lo up ip link show eth1 >/dev/null 2>&1 || ip link add eth1 address 52:54:00:4c:45:01 type veth peer name gw netns le-net50 ip -n le-net50 addr replace 192.168.50.1/24 dev gw ip -n le-net50 addr replace fd00:50::1/64 dev gw nodad ip -n le-net50 link set gw up true X chmod 755 /usr/local/lib/le-lab/spare-net.sh cat >/etc/systemd/system/le-lab-spare-net.service <<'X' [Unit] Description=Practice lab: a spare network interface, eth1 Before=network-pre.target NetworkManager.service Wants=network-pre.target [Service] Type=oneshot RemainAfterExit=yes ExecStart=/usr/local/lib/le-lab/spare-net.sh [Install] WantedBy=multi-user.target X # NetworkManager manages eth1 but makes no profile for it on its own. A # gateway and DNS server on eth1 rank below the main interface's, so the # VM keeps its own way out. cat >/etc/NetworkManager/conf.d/91-le-lab-spare.conf <<'X' [main] no-auto-default+=interface-name:eth1 [device-le-lab-spare] match-device=interface-name:eth1 managed=1 [connection-le-lab-spare] match-device=interface-name:eth1 ipv4.route-metric=500 ipv6.route-metric=500 ipv4.dns-priority=200 ipv6.dns-priority=200 X systemctl reload NetworkManager systemctl daemon-reload systemctl enable --now --quiet le-lab-spare-net.service for i in 1 2 3 4 5 6 7 8 9 10; do nmcli -g GENERAL.STATE device show eth1 2>/dev/null | grep -qv unmanaged && break sleep 1 done nmcli connection add type ethernet ifname eth1 con-name eth1 \ ipv4.method manual ipv4.addresses 192.168.50.10/24 ipv6.method disabled >/dev/null nmcli connection up eth1 >/dev/null 2>&1 || true # A fresh start for tasks 1 and 2. state_set hostname "$(hostnamectl --static 2>/dev/null || hostname)" [ "$(hostname)" != server1.example.com ] || hostnamectl set-hostname lab-vm cp -a /etc/hosts "$LAB_STATE/hosts" sed -i -E '/^[^#]*[[:space:]]web1(\.example\.com)?([[:space:]]|$)/d' /etc/hosts true