Skip to content

Commit 33ca752

Browse files
committed
use systemd for networking
1 parent 54752b3 commit 33ca752

1 file changed

Lines changed: 39 additions & 12 deletions

File tree

os/installer/kexec.nix

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,15 @@
7575
source /xnode-config/env
7676
'';
7777

78+
# https://github.com/nix-community/nixos-images/blob/main/nix/kexec-installer/restore_routes.py
79+
networking.firewall.enable = false;
80+
networking.useNetworkd = true;
81+
systemd.network.enable = true;
7882
systemd.services.apply-network-config = {
7983
wantedBy = [ "multi-user.target" ];
8084
description = "Apply run time provided network config.";
81-
wants = [ "network-online.target" ];
82-
after = [ "network-online.target" ];
85+
wants = [ "network-pre.target" ];
86+
before = [ "network-pre.target" ];
8387
serviceConfig = {
8488
Type = "oneshot";
8589
User = "root";
@@ -94,13 +98,25 @@
9498
# Extract environmental variables
9599
source /xnode-config/env
96100
101+
output="/etc/systemd/network"
97102
if [[ $NETWORK_CONFIG ]]; then
98103
interfaces=$(echo "$NETWORK_CONFIG" | jq -c '.address.[]')
99104
routes=$(echo "$NETWORK_CONFIG" | jq -c '.route.[]')
100105
for iface in $interfaces; do
101106
mac=$(echo "$iface" | jq -r '.address')
102-
og_name=$(echo "$iface" | jq -r '.ifname')
103-
name=$(grep -l "$mac" /sys/class/net/*/address | sed 's|/sys/class/net/\(.*\)/address|\1|')
107+
name=$(echo "$iface" | jq -r '.ifname')
108+
systemd="''${output}/00-''${mac}.network"
109+
110+
cat << EOF > "$systemd"
111+
[Match]
112+
MACAddress = $mac
113+
114+
[Network]
115+
DHCP = yes
116+
LLDP = yes
117+
IPv6AcceptRA = yes
118+
MulticastDNS = yes
119+
EOF
104120
105121
addresses=$(echo "$iface" | jq -c '.addr_info[]')
106122
for address in $addresses; do
@@ -111,29 +127,40 @@
111127
continue
112128
fi
113129
114-
config="$(echo $address | jq -r '.local')/$(echo $address | jq -r '.prefixlen')"
115-
ip address add $config dev $name
116-
done
130+
ip="$(echo $address | jq -r '.local')/$(echo $address | jq -r '.prefixlen')"
117131
118-
ip link set $name up
132+
cat << EOF >> "$systemd"
133+
Address = $ip
134+
EOF
135+
done
119136
120137
for route in $routes; do
121138
protocol=$(echo "$route" | jq -r '.protocol')
122139
dev=$(echo "$route" | jq -r '.dev')
123140
124-
if [ "$protocol" != "static" ] || [ "$dev" != "$og_name" ]; then
141+
if [ "$protocol" != "static" ] || [ "$dev" != "$name" ]; then
125142
continue
126143
fi
127144
128-
args=""
145+
onlink="no"
129146
flags=$(echo "$route" | jq -r '.flags')
130147
if [[ $flags == *"onlink"* ]]; then
131-
args="$args onlink"
148+
onlink="yes"
132149
fi
133150
134151
destination=$(echo $route | jq -r '.dst')
152+
if [ "$destination" == "default" ]; then
153+
destination="0.0.0.0/0"
154+
fi
135155
gateway=$(echo $route | jq -r '.gateway')
136-
ip route add $destination via $gateway $args dev $name
156+
157+
cat << EOF >> "$systemd"
158+
159+
[Route]
160+
Destination = $destination
161+
Gateway = $gateway
162+
GatewayOnLink = $onlink
163+
EOF
137164
done
138165
done
139166
fi

0 commit comments

Comments
 (0)