Skip to content

Commit 6bd02b3

Browse files
committed
fix(lib): write drop-in configs to active network file directory
When another network config generator (e.g. netplan, cloud-init) creates a .network file with a lower numerical prefix than the 70- prefix used by amazon-ec2-net-utils, systemd-networkd selects that file as the active config for the interface. Drop-in files written under 70-<iface>.network.d/ are then silently ignored because they belong to an inactive network file. This causes secondary IPv4 addresses (ec2net_alias.conf) and policy routing rules (ec2net_policy_*.conf) to never be applied, even though they are correctly fetched from IMDS and written to disk. Add _get_active_dropin_dir() which queries the systemd-networkd runtime state to discover the actual active NETWORK_FILE for an interface, then returns that file's drop-in directory. Falls back to the original 70-<iface>.network.d path when detection is unavailable (e.g. during early boot before networkd has initialised the interface). Use _get_active_dropin_dir() in create_ipv4_aliases() and create_rules() instead of the hardcoded 70- path.
1 parent d9f01f3 commit 6bd02b3

1 file changed

Lines changed: 17 additions & 2 deletions

File tree

lib/lib.sh

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,13 +230,27 @@ _install_and_reload() {
230230
fi
231231
}
232232

233+
_get_active_dropin_dir() {
234+
local iface=$1
235+
local default_dir="${unitdir}/70-${iface}.network.d"
236+
local ifindex network_file
237+
ifindex=$(cat "/sys/class/net/${iface}/ifindex" 2> /dev/null) || { echo "$default_dir"; return; }
238+
network_file=$(sed -n 's/^NETWORK_FILE=//p' "/run/systemd/netif/links/${ifindex}" 2> /dev/null) || { echo "$default_dir"; return; }
239+
if [ -n "$network_file" ]; then
240+
echo "${network_file}.d"
241+
else
242+
echo "$default_dir"
243+
fi
244+
}
245+
233246
create_ipv4_aliases() {
234247
local iface=$1
235248
local mac=$2
236249
local addresses
237250
subnet_supports_ipv4 "$iface" || return 0
238251
addresses=$(get_iface_imds $mac local-ipv4s | tail -n +2 | sort)
239-
local drop_in_dir="${unitdir}/70-${iface}.network.d"
252+
local drop_in_dir
253+
drop_in_dir=$(_get_active_dropin_dir "$iface")
240254
mkdir -p "$drop_in_dir"
241255
local file="$drop_in_dir/ec2net_alias.conf"
242256
local work="${file}.new"
@@ -295,7 +309,8 @@ create_rules() {
295309
local family=$4
296310
local addrs prefixes
297311
local local_addr_key subnet_pd_key
298-
local drop_in_dir="${unitdir}/70-${iface}.network.d"
312+
local drop_in_dir
313+
drop_in_dir=$(_get_active_dropin_dir "$iface")
299314
mkdir -p "$drop_in_dir"
300315

301316
local -i ruleid=$((device_number+rule_base+100*network_card))

0 commit comments

Comments
 (0)