Skip to content

Commit 7cd51b5

Browse files
committed
Prefer managed bridges for default Incus network
Updates `incus_pick_default_network` to ignore host interfaces and only consider managed bridge networks. It now prioritizes non-NAT (LAN-style) bridges first, then NATed bridges, and keeps `incusbr0` as the final fallback. This prevents accidental default selection of physical uplinks like `enp1s0`, which can break container startup and host connectivity.
1 parent f0bec29 commit 7cd51b5

1 file changed

Lines changed: 26 additions & 18 deletions

File tree

incus/build.func

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -109,29 +109,37 @@ incus_pick_default_network() {
109109
nets=$(incus network list -f csv,noheader 2>/dev/null | cut -d, -f1 | sed '/^\s*$/d' | grep -v '^lo$' || true)
110110
[[ -z "$nets" ]] && { echo "incusbr0"; return; }
111111

112+
# Managed bridges only. `incus network list` also reports plain host
113+
# interfaces as type physical, and attaching one moves the host's NIC into
114+
# the container - which an unprivileged container cannot start with, and
115+
# which would cut the host off if it could. Preferring physical here is how
116+
# enp1s0 became the default and forklxc refused to launch. Picking the
117+
# uplink is a decision for the operator, never a default.
112118
local n cfg
119+
local -a lan=() nated=()
113120
while IFS= read -r n; do
114-
[[ -z "$n" || "$n" == "incusbr0" ]] && continue
115-
cfg=$(incus network show "$n" 2>/dev/null || true)
116-
if echo "$cfg" | grep -qE '^type:\s*(physical|macvlan|sriov)$'; then
117-
echo "$n"
118-
return
119-
fi
120-
done <<<"$nets"
121-
122-
while IFS= read -r n; do
123-
[[ -z "$n" || "$n" == "incusbr0" ]] && continue
124-
cfg=$(incus network show "$n" 2>/dev/null || true)
125-
if echo "$cfg" | grep -q '^type:\s*bridge$' &&
126-
! echo "$cfg" | grep -qE 'ipv4\.nat:\s*"true"|ipv6\.nat:\s*"true"'; then
127-
echo "$n"
128-
return
121+
[[ -z "$n" ]] && continue
122+
cfg=$(LC_ALL=C incus network show "$n" 2>/dev/null || true)
123+
grep -qE '^managed:[[:space:]]*true$' <<<"$cfg" || continue
124+
grep -qE '^type:[[:space:]]*bridge$' <<<"$cfg" || continue
125+
# No NAT usually means the bridge hands out LAN addresses, which is what
126+
# most people want; incusbr0 and friends are the NATed fallback.
127+
if grep -qE '^[[:space:]]+ipv[46]\.nat:[[:space:]]*"?true"?' <<<"$cfg"; then
128+
nated+=("$n")
129+
else
130+
lan+=("$n")
129131
fi
130132
done <<<"$nets"
131133

132-
while IFS= read -r n; do
133-
[[ -n "$n" && "$n" != "incusbr0" ]] && { echo "$n"; return; }
134-
done <<<"$nets"
134+
for n in "${lan[@]}" "${nated[@]}"; do
135+
[[ "$n" == "incusbr0" ]] && continue
136+
echo "$n"
137+
return
138+
done
139+
for n in "${lan[@]}" "${nated[@]}"; do
140+
echo "$n"
141+
return
142+
done
135143
echo "incusbr0"
136144
}
137145

0 commit comments

Comments
 (0)