about summary refs log tree commit diff
path: root/nixpkgs/nixos/tests/systemd-machinectl.nix
blob: 9d761c6d4d8b825f3799c6524b49f84e5cae256d (plain) (blame)
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
import ./make-test-python.nix ({ pkgs, ... }:
let

  container = { config, ... }: {
    # We re-use the NixOS container option ...
    boot.isContainer = true;
    # ... and revert unwanted defaults
    networking.useHostResolvConf = false;

    # use networkd to obtain systemd network setup
    networking.useNetworkd = true;
    networking.useDHCP = false;

    # systemd-nspawn expects /sbin/init
    boot.loader.initScript.enable = true;

    imports = [ ../modules/profiles/minimal.nix ];

    system.stateVersion = config.system.nixos.version;
  };

  containerSystem = (import ../lib/eval-config.nix {
    inherit (pkgs) system;
    modules = [ container ];
  }).config.system.build.toplevel;

  containerName = "container";
  containerRoot = "/var/lib/machines/${containerName}";

  containerTarball = pkgs.callPackage ../lib/make-system-tarball.nix {
    storeContents = [
      {
        object = containerSystem;
        symlink = "/nix/var/nix/profiles/system";
      }
    ];

    contents = [
      {
        source = containerSystem + "/etc/os-release";
        target = "/etc/os-release";
      }
      {
        source = containerSystem + "/init";
        target = "/sbin/init";
      }
    ];
  };
in
{
  name = "systemd-machinectl";

  nodes.machine = { lib, ... }: {
    # use networkd to obtain systemd network setup
    networking.useNetworkd = true;
    networking.useDHCP = false;

    # 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 ];

    systemd.tmpfiles.rules = [
      "d /var/lib/machines/shared-decl 0755 root root - -"
    ];
    systemd.nspawn.shared-decl = {
      execConfig = {
        Boot = false;
        Parameters = "${containerSystem}/init";
      };
      filesConfig = {
        BindReadOnly = "/nix/store";
      };
    };

    systemd.services."systemd-nspawn@${containerName}" = {
      serviceConfig.Environment = [
        # Disable tmpfs for /tmp
        "SYSTEMD_NSPAWN_TMPFS_TMP=0"
      ];
      overrideStrategy = "asDropin";
    };

    # open DHCP for container
    networking.firewall.extraCommands = ''
      ${pkgs.iptables}/bin/iptables -A nixos-fw -i ve-+ -p udp -m udp --dport 67 -j nixos-fw-accept
    '';
  };

  testScript = ''
    start_all()
    machine.wait_for_unit("default.target");

    # 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");

    # create containers root
    machine.succeed("mkdir -p ${containerRoot}");

    # 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
    # Workaround for nixos-install
    machine.succeed("chmod o+rx /var/lib/machines");
    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 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 'UID 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}")

    # 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}");
  '';
})