git.iko.soy/typeable/nixpkgs
on master help sign in
Merge pull request #66846 from uvNikita/containers/ephemeral

nixos/containers: add 'ephemeral' option

commit94c51859dfd24da0fa80d734ce8922c8e43d760c authorMarek Mahut <marek.mahut@gmail.com> dateMon 19 Aug 2019, 20:55 (7 years ago) parentc03dee3c740f0d
3 files changed+86−7
nixos/modules/virtualisation/containers.nix+29−7 nixos/tests/all-tests.nix+1 nixos/tests/containers-ephemeral.nix+56
nixos/modules/virtualisation/containers.nix+29−7
@@ -70,7 +70,7 @@ let
7070 startScript = cfg:
7171 ''
7272 mkdir -p -m 0755 "$root/etc" "$root/var/lib"
73− mkdir -p -m 0700 "$root/var/lib/private" "$root/root" /run/containers
73+ mkdir -p -m 0700 "$root/var/lib/private" "$root/root"
7474 if ! [ -e "$root/etc/os-release" ]; then
7575 touch "$root/etc/os-release"
7676 fi
@@ -138,7 +138,7 @@ let
138138 --bind-ro=/nix/var/nix/daemon-socket \
139139 --bind="/nix/var/nix/profiles/per-container/$INSTANCE:/nix/var/nix/profiles" \
140140 --bind="/nix/var/nix/gcroots/per-container/$INSTANCE:/nix/var/nix/gcroots" \
141− --link-journal=try-guest \
141+ ${optionalString (!cfg.ephemeral) "--link-journal=try-guest"} \
142142 --setenv PRIVATE_NETWORK="$PRIVATE_NETWORK" \
143143 --setenv HOST_BRIDGE="$HOST_BRIDGE" \
144144 --setenv HOST_ADDRESS="$HOST_ADDRESS" \
@@ -147,6 +147,7 @@ let
147147 --setenv LOCAL_ADDRESS6="$LOCAL_ADDRESS6" \
148148 --setenv HOST_PORT="$HOST_PORT" \
149149 --setenv PATH="$PATH" \
150+ ${optionalString cfg.ephemeral "--ephemeral"} \
150151 ${if cfg.additionalCapabilities != null && cfg.additionalCapabilities != [] then
151152 ''--capability="${concatStringsSep " " cfg.additionalCapabilities}"'' else ""
152153 } \
@@ -247,6 +248,8 @@ let
247248
248249 Type = "notify";
249250
251+ RuntimeDirectory = [ "containers" ] ++ lib.optional cfg.ephemeral "containers/%i";
252+
250253 # Note that on reboot, systemd-nspawn returns 133, so this
251254 # unit will be restarted. On poweroff, it returns 0, so the
252255 # unit won't be restarted.
@@ -419,6 +422,7 @@ let
419422 {
420423 extraVeths = {};
421424 additionalCapabilities = [];
425+ ephemeral = false;
422426 allowedDevices = [];
423427 hostAddress = null;
424428 hostAddress6 = null;
@@ -511,6 +515,26 @@ in
511515 information.
512516 '';
513517 };
518+
519+ ephemeral = mkOption {
520+ type = types.bool;
521+ default = false;
522+ description = ''
523+ Runs container in ephemeral mode with the empty root filesystem at boot.
524+ This way container will be bootstrapped from scratch on each boot
525+ and will be cleaned up on shutdown leaving no traces behind.
526+ Useful for completely stateless, reproducible containers.
527+
528+ Note that this option might require to do some adjustments to the container configuration,
529+ e.g. you might want to set
530+ <varname>systemd.network.networks.$interface.dhcpConfig.ClientIdentifier</varname> to "mac"
531+ if you use <varname>macvlans</varname> option.
532+ This way dhcp client identifier will be stable between the container restarts.
533+
534+ Note that the container journal will not be linked to the host if this option is enabled.
535+ '';
536+ };
537+
514538 enableTun = mkOption {
515539 type = types.bool;
516540 default = false;
@@ -659,13 +683,8 @@ in
659683 unit = {
660684 description = "Container '%i'";
661685
662− unitConfig.RequiresMountsFor = [ "/var/lib/containers/%i" ];
663−
664686 path = [ pkgs.iproute ];
665687
666− environment.INSTANCE = "%i";
667− environment.root = "/var/lib/containers/%i";
668−
669688 preStart = preStartScript dummyConfig;
670689
671690 script = startScript dummyConfig;
@@ -708,6 +727,9 @@ in
708727 script = startScript containerConfig;
709728 postStart = postStartScript containerConfig;
710729 serviceConfig = serviceDirectives containerConfig;
730+ unitConfig.RequiresMountsFor = lib.optional (!containerConfig.ephemeral) "/var/lib/containers/%i";
731+ environment.root = if containerConfig.ephemeral then "/run/containers/%i" else "/var/lib/containers/%i";
732+ environment.INSTANCE = "%i";
711733 } // (
712734 if containerConfig.autoStart then
713735 {
nixos/tests/all-tests.nix+1
@@ -47,6 +47,7 @@ in
4747 codimd = handleTest ./codimd.nix {};
4848 colord = handleTest ./colord.nix {};
4949 containers-bridge = handleTest ./containers-bridge.nix {};
50+ containers-ephemeral = handleTest ./containers-ephemeral.nix {};
5051 containers-extra_veth = handleTest ./containers-extra_veth.nix {};
5152 containers-hosts = handleTest ./containers-hosts.nix {};
5253 containers-imperative = handleTest ./containers-imperative.nix {};
nixos/tests/containers-ephemeral.nixnew file+56
@@ -0,0 +1,56 @@
1+# Test for NixOS' container support.
2+
3+import ./make-test.nix ({ pkgs, ...} : {
4+ name = "containers-ephemeral";
5+
6+ machine = { pkgs, ... }: {
7+ virtualisation.memorySize = 768;
8+ virtualisation.writableStore = true;
9+
10+ containers.webserver = {
11+ ephemeral = true;
12+ privateNetwork = true;
13+ hostAddress = "10.231.136.1";
14+ localAddress = "10.231.136.2";
15+ config = {
16+ services.nginx = {
17+ enable = true;
18+ virtualHosts.localhost = {
19+ root = (pkgs.runCommand "localhost" {} ''
20+ mkdir "$out"
21+ echo hello world > "$out/index.html"
22+ '');
23+ };
24+ };
25+ networking.firewall.allowedTCPPorts = [ 80 ];
26+ };
27+ };
28+ };
29+
30+ testScript = ''
31+ $machine->succeed("nixos-container list") =~ /webserver/ or die;
32+
33+ # Start the webserver container.
34+ $machine->succeed("nixos-container start webserver");
35+
36+ # Check that container got its own root folder
37+ $machine->succeed("ls /run/containers/webserver");
38+
39+ # Check that container persistent directory is not created
40+ $machine->fail("ls /var/lib/containers/webserver");
41+
42+ # Since "start" returns after the container has reached
43+ # multi-user.target, we should now be able to access it.
44+ my $ip = $machine->succeed("nixos-container show-ip webserver");
45+ chomp $ip;
46+ $machine->succeed("ping -n -c1 $ip");
47+ $machine->succeed("curl --fail http://$ip/ > /dev/null");
48+
49+ # Stop the container.
50+ $machine->succeed("nixos-container stop webserver");
51+ $machine->fail("curl --fail --connect-timeout 2 http://$ip/ > /dev/null");
52+
53+ # Check that container's root folder was removed
54+ $machine->fail("ls /run/containers/webserver");
55+ '';
56+})