blob: 8a525a036977a6e3710425b0767b51fca15027f5 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
|
{ pkgs, lib, ... }:
let
evalConfig =
module:
(import ../lib/eval-config.nix {
system = null;
modules = [ module ];
}).config.system.build.toplevel;
common =
{ config, ... }:
{
imports = [ ../modules/profiles/minimal.nix ];
system.stateVersion = config.system.nixos.release;
nixpkgs.pkgs = pkgs;
};
profile-host-nspawn = {
# use networkd to obtain systemd network setup
networking.useNetworkd = true;
networking.firewall.extraCommands = ''
# open DHCP for nspawn interfaces
${pkgs.iptables}/bin/iptables -A nixos-fw -i ve-+ -p udp -m udp --dport 67 -j nixos-fw-accept
'';
};
# improvement: move following profile to ../modules/profiles/nspawn-guest.nix
profile-guest-nspawn = {
# We re-use the NixOS container option ...
boot.isNspawnContainer = true;
# ... and revert unwanted defaults
networking.useHostResolvConf = false;
# systemd-nspawn expects /sbin/init
boot.loader.initScript.enable = true;
# use networkd to obtain systemd network setup
networking.useNetworkd = true;
};
profile-host-vmspawn =
{ config, pkgs, ... }:
{
# use networkd to obtain systemd network setup
networking.useNetworkd = true;
networking.firewall.extraCommands = ''
# open DHCP for vmspawn interfaces
${pkgs.iptables}/bin/iptables -A nixos-fw -i vt-+ -p udp -m udp --dport 67 -j nixos-fw-accept
'';
environment.systemPackages =
let
# improvement: following wrapper should be moved to pkgs
vmspawn-wrapped =
pkgs.runCommand "systemd-vmspawn-wrapped" { nativeBuildInputs = [ pkgs.makeWrapper ]; }
''
makeWrapper ${config.systemd.package}/bin/systemd-vmspawn $out/bin/systemd-vmspawn-wrapped \
--prefix PATH : ${
lib.makeBinPath [
pkgs.qemu
pkgs.virtiofsd
pkgs.openssh # ssh-keygen
]
} \
--prefix XDG_CONFIG_HOME : ${pkgs.qemu}/share
'';
in
[ vmspawn-wrapped ];
};
# improvement: move following profile to ../modules/profiles/vmspawn-guest.nix
profile-guest-vmspawn = {
imports = [ ../modules/profiles/qemu-guest.nix ];
boot.initrd.systemd.enable = true;
# root is defined by systemd-vmspawn
boot.initrd.systemd.root = null;
boot.loader.grub.enable = false;
# use networkd to obtain systemd network setup
networking.useNetworkd = true;
};
container = {
imports = [
common
profile-guest-nspawn
];
};
containerSystem = evalConfig container;
containerName = "container";
containerRoot = "/var/lib/machines/${containerName}";
containerTarball = pkgs.callPackage ../lib/make-system-tarball.nix {
storeContents = [
{
object = containerSystem;
symlink = "/nix/var/nix/profiles/system";
}
];
extraCommands = "mkdir -p usr";
contents = [
{
source = containerSystem + "/etc/os-release";
target = "/etc/os-release";
}
{
source = containerSystem + "/init";
target = "/sbin/init";
}
];
};
vm = {
imports = [
common
profile-guest-vmspawn
];
services.openssh.enable = true;
};
vmSystem = evalConfig vm;
vmShared = {
imports = [ vm ];
fileSystems."/nix/store" = {
device = "mnt0";
fsType = "virtiofs";
neededForBoot = true;
};
};
vmSharedSystem = evalConfig vmShared;
in
{
name = "systemd-machinectl";
meta.maintainers = with lib.maintainers; [ ck3d ];
nodes.machine =
{
lib,
...
}:
{
imports = [
profile-host-nspawn
profile-host-vmspawn
];
# do not try to access cache.nixos.org
nix.settings.substituters = lib.mkForce [ ];
# auto-start container
systemd.targets.machines.wants = [ "systemd-nspawn@${containerName}.service" ];
virtualisation.additionalPaths = [
containerSystem
containerTarball
vmSystem
vmSharedSystem
];
virtualisation.diskSize = 2048;
systemd.tmpfiles.rules = [
"d /var/lib/machines/shared-decl 0755 root root - -"
"d /var/lib/machines/shared-decl/usr 0755 root root - -"
];
systemd.nspawn.shared-decl = {
execConfig = {
Boot = false;
Parameters = "${containerSystem}/init";
};
filesConfig = {
BindReadOnly = "/nix/store";
};
};
systemd.nspawn.${containerName} = {
filesConfig = {
# workaround to fix kernel namespaces; needed for Nix sandbox
# https://github.com/systemd/systemd/issues/27994#issuecomment-1704005670
Bind = "/proc:/run/proc";
};
};
systemd.services."systemd-nspawn@${containerName}" = {
serviceConfig.Environment = [
# Disable tmpfs for /tmp
"SYSTEMD_NSPAWN_TMPFS_TMP=0"
];
overrideStrategy = "asDropin";
};
};
testScript = ''
start_all()
machine.wait_for_unit("default.target");
# Workaround for nixos-install
machine.succeed("chmod o+rx /var/lib/machines");
with subtest("vm-shared"):
machine.succeed("mkdir -p /var/lib/machines/vm-shared");
# Start vm-shared
machine.succeed("systemd-run systemd-vmspawn-wrapped --directory=/var/lib/machines/vm-shared --bind-ro=/nix/store --linux=${vmSharedSystem}/kernel --initrd=${vmSharedSystem}/initrd ${vmSharedSystem}/kernel-params init=${vmSharedSystem}/init")
machine.wait_until_succeeds("machinectl status vm-shared");
machine.wait_until_succeeds("eval $(machinectl show vm-shared --property=SSHPrivateKeyPath --property=SSHAddress) && ssh -i $SSHPrivateKeyPath $SSHAddress true")
machine.succeed("machinectl stop vm-shared");
with subtest("vm"):
machine.succeed("mkdir -p /var/lib/machines/vm");
# Install vm
machine.succeed("nixos-install --root /var/lib/machines/vm --system ${vmSystem} --no-channel-copy --no-root-passwd");
# Start vm
machine.succeed("systemd-run systemd-vmspawn-wrapped --directory=/var/lib/machines/vm --linux=${vmSystem}/kernel --initrd=${vmSystem}/initrd ${vmSystem}/kernel-params init=${vmSystem}/init")
machine.wait_until_succeeds("machinectl status vm");
machine.wait_until_succeeds("eval $(machinectl show vm --property=SSHPrivateKeyPath --property=SSHAddress) && ssh -i $SSHPrivateKeyPath $SSHAddress true")
machine.succeed("machinectl stop vm");
# Test machinectl start stop of shared-decl
machine.succeed("machinectl start shared-decl");
machine.wait_until_succeeds("systemctl -M shared-decl is-active default.target");
machine.succeed("machinectl stop shared-decl");
machine.succeed("mkdir -p ${containerRoot}/usr");
# start container with shared nix store by using same arguments as for systemd-nspawn@.service
machine.succeed("systemd-run systemd-nspawn --machine=${containerName} --network-veth -U --bind-ro=/nix/store ${containerSystem}/init")
machine.wait_until_succeeds("systemctl -M ${containerName} is-active default.target");
# Test machinectl stop
machine.succeed("machinectl stop ${containerName}");
# Install container
machine.succeed("nixos-install --root ${containerRoot} --system ${containerSystem} --no-channel-copy --no-root-passwd");
# Allow systemd-nspawn to apply user namespace on immutable files
machine.succeed("chattr -i ${containerRoot}/var/empty");
# Test machinectl start
machine.succeed("machinectl start ${containerName}");
machine.wait_until_succeeds("systemctl -M ${containerName} is-active default.target");
# Test systemd-nspawn configured unified cgroup delegation
# see also:
# https://github.com/systemd/systemd/blob/main/docs/CGROUP_DELEGATION.md#three-different-tree-setups-
machine.succeed('systemd-run --pty --wait -M ${containerName} /run/current-system/sw/bin/stat --format="%T" --file-system /sys/fs/cgroup > fstype')
machine.succeed('test $(tr -d "\\r" < fstype) = cgroup2fs')
# Test if systemd-nspawn provides a working environment for nix to build derivations
# https://nixos.org/guides/nix-pills/07-working-derivation
machine.succeed('systemd-run --pty --wait -M ${containerName} /run/current-system/sw/bin/nix-instantiate --expr \'derivation { name = "myname"; builder = "/bin/sh"; args = [ "-c" "echo foo > $out" ]; system = "${pkgs.stdenv.hostPlatform.system}"; }\' --add-root /tmp/drv')
machine.succeed('systemd-run --pty --wait -M ${containerName} /run/current-system/sw/bin/nix-store --option substitute false --realize /tmp/drv')
# Test nss_mymachines without nscd
machine.succeed('LD_LIBRARY_PATH="/run/current-system/sw/lib" getent -s hosts:mymachines hosts ${containerName}');
# Test nss_mymachines via nscd
machine.succeed("getent hosts ${containerName}");
# Test systemd-nspawn network configuration to container
machine.succeed("networkctl --json=short status ve-${containerName} | ${pkgs.jq}/bin/jq -e '.OperationalState == \"routable\"'");
# Test systemd-nspawn network configuration to host
machine.succeed("machinectl shell ${containerName} /run/current-system/sw/bin/networkctl --json=short status host0 | ${pkgs.jq}/bin/jq -r '.OperationalState == \"routable\"'");
# Test systemd-nspawn network configuration
machine.succeed("ping -n -c 1 ${containerName}");
# Test systemd-nspawn uses a user namespace
machine.succeed("test $(machinectl status ${containerName} | grep 'ID Shift: ' | wc -l) = 1")
# Test systemd-nspawn reboot
machine.succeed("machinectl shell ${containerName} /run/current-system/sw/bin/reboot");
machine.wait_until_succeeds("systemctl -M ${containerName} is-active default.target");
# Test machinectl reboot
machine.succeed("machinectl reboot ${containerName}");
machine.wait_until_succeeds("systemctl -M ${containerName} is-active default.target");
# Restart machine
machine.shutdown()
machine.start()
machine.wait_for_unit("default.target");
# Test auto-start
machine.succeed("machinectl show ${containerName}")
machine.wait_until_succeeds("systemctl -M ${containerName} is-active default.target");
# Test machinectl stop
machine.succeed("machinectl stop ${containerName}");
machine.wait_until_succeeds("test $(systemctl is-active systemd-nspawn@${containerName}) = inactive");
# Test tmpfs for /tmp
machine.fail("mountpoint /tmp");
# Show to to delete the container
machine.succeed("chattr -i ${containerRoot}/var/empty");
machine.succeed("rm -rf ${containerRoot}");
# Test import tarball, start, stop and remove
machine.succeed("machinectl import-tar ${containerTarball}/tarball/*.tar* ${containerName}");
machine.succeed("machinectl start ${containerName}");
machine.wait_until_succeeds("systemctl -M ${containerName} is-active default.target");
machine.succeed("machinectl stop ${containerName}");
machine.wait_until_succeeds("test $(systemctl is-active systemd-nspawn@${containerName}) = inactive");
machine.succeed("machinectl remove ${containerName}");
'';
}
|