Set container DNS from the VLAN tag via a network profile (incl. DHCP) #2

Merged
l.kirchner merged 2 commits from network-profiles into main 2026-06-07 21:25:27 +02:00
Owner

Adds a network "switch" so every LXC gets the right DNS servers based on the VLAN tag entered at install time — including when DHCP is used for the IP.

What's new

  • lib/networks.conf — a small data file mapping a VLAN tag → subnet + DNS servers. Adding a network is a one-line change:
    # tag    subnet            dns
    none     192.168.0.0/24    192.168.0.4,192.168.0.5
    20       10.11.20.0/24     10.11.20.44,10.11.20.55
    
    none is the key for the untagged network (empty VLAN prompt).
  • lib/build.func — new apply_network_profile() looks up the entered VLAN tag in that file and sets --nameserver accordingly. Wired into prompt_lxc_config.

Behaviour

DNS resolution now follows a clear precedence:

  1. explicit NAMESERVER=… from env (even empty = "inherit") — respected as-is
  2. profile match for the VLAN tag — uses its DNS (this is the new bit, and it applies to DHCP too)
  3. static IP, no profile — falls back to the manual DNS prompt
  4. DHCP, no profile — inherits from the PVE host (with a warning)

Comma-separated DNS in the file is normalised to space-separated for pct --nameserver.

Why

DHCP previously meant "inherit whatever DNS the lease hands out". With two networks (untagged 192.168.0.0/24 and VLAN 20 → 10.11.20.0/24) that's not what we want — each network has its own resolvers. Keying DNS off the VLAN tag fixes this centrally for all scripts (devpi, webapp, future), since the logic lives in the shared lib. No ct/ changes needed.

Validation

shellcheck clean (only the pre-existing SC1090/SC2034 from the curl-sourced lib pattern), bash -n passes, and the lookup was unit-tested against the data file:

VLAN tag resolved subnet DNS
(empty) 192.168.0.0/24 192.168.0.4 192.168.0.5
20 10.11.20.0/24 10.11.20.44 10.11.20.55
30 (unknown) none (DHCP inherits)

Note

The profile file is fetched at runtime from main (same mechanism as build.func). For local testing, set NET_PROFILES_FILE=/path.

Adds a network "switch" so every LXC gets the right DNS servers based on the VLAN tag entered at install time — **including when DHCP is used for the IP**. ## What's new - **`lib/networks.conf`** — a small data file mapping a VLAN tag → subnet + DNS servers. Adding a network is a one-line change: ``` # tag subnet dns none 192.168.0.0/24 192.168.0.4,192.168.0.5 20 10.11.20.0/24 10.11.20.44,10.11.20.55 ``` `none` is the key for the untagged network (empty VLAN prompt). - **`lib/build.func`** — new `apply_network_profile()` looks up the entered VLAN tag in that file and sets `--nameserver` accordingly. Wired into `prompt_lxc_config`. ## Behaviour DNS resolution now follows a clear precedence: 1. explicit `NAMESERVER=…` from env (even empty = "inherit") — respected as-is 2. profile match for the VLAN tag — uses its DNS (this is the new bit, and it applies to DHCP too) 3. static IP, no profile — falls back to the manual DNS prompt 4. DHCP, no profile — inherits from the PVE host (with a warning) Comma-separated DNS in the file is normalised to space-separated for `pct --nameserver`. ## Why DHCP previously meant "inherit whatever DNS the lease hands out". With two networks (untagged 192.168.0.0/24 and VLAN 20 → 10.11.20.0/24) that's not what we want — each network has its own resolvers. Keying DNS off the VLAN tag fixes this centrally for **all** scripts (devpi, webapp, future), since the logic lives in the shared lib. No `ct/` changes needed. ## Validation shellcheck clean (only the pre-existing SC1090/SC2034 from the curl-sourced lib pattern), `bash -n` passes, and the lookup was unit-tested against the data file: | VLAN tag | resolved subnet | DNS | |---|---|---| | *(empty)* | 192.168.0.0/24 | 192.168.0.4 192.168.0.5 | | 20 | 10.11.20.0/24 | 10.11.20.44 10.11.20.55 | | 30 (unknown) | — | none (DHCP inherits) | ## Note The profile file is fetched at runtime from `main` (same mechanism as build.func). For local testing, set `NET_PROFILES_FILE=/path`.
l.kirchner added 2 commits 2026-06-07 21:23:08 +02:00
Data file mapping a VLAN tag to its DNS servers and subnet, so build.func
can set the right resolvers from the tag entered at install time — even
with DHCP. New networks are a one-line addition here.
Adds apply_network_profile(), which looks up DNS servers for the entered
VLAN tag in lib/networks.conf and sets --nameserver accordingly — even
when IP is DHCP. Precedence: explicit env NAMESERVER > profile > static
prompt > inherit. Comma-separated DNS is normalised to spaces for pct.
l.kirchner merged commit 3012245c93 into main 2026-06-07 21:25:27 +02:00
l.kirchner deleted branch network-profiles 2026-06-07 21:25:27 +02:00
Sign in to join this conversation.
No Reviewers
No labels
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: luki-net/proxmox-scripts#2