From e620be97fe212b43ee42865dd4d3c8bba7f26fa9 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Fri, 15 Aug 2014 04:04:28 +0200 Subject: Containers: Set up /etc/resolv.conf Systemd-nspawn is supposed to do this, but doesn't if any of the --network-* flags are used. --- nixos/modules/virtualisation/containers.nix | 2 ++ 1 file changed, 2 insertions(+) (limited to 'nixos/modules/virtualisation/containers.nix') diff --git a/nixos/modules/virtualisation/containers.nix b/nixos/modules/virtualisation/containers.nix index d0d04d9a1e5d..7f545a9d3031 100644 --- a/nixos/modules/virtualisation/containers.nix +++ b/nixos/modules/virtualisation/containers.nix @@ -187,6 +187,8 @@ in "/nix/var/nix/profiles/per-container/$INSTANCE" \ "/nix/var/nix/gcroots/per-container/$INSTANCE" + cp -f /etc/resolv.conf "$root/etc/resolv.conf" + if [ "$PRIVATE_NETWORK" = 1 ]; then extraFlags+=" --network-veth" fi -- cgit 1.4.1 From 2337a85fc3a0228ede448d74ed8a29e2b1190f8f Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Mon, 18 Aug 2014 14:04:39 +0200 Subject: Autostart containers at boot time --- nixos/modules/virtualisation/containers.nix | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'nixos/modules/virtualisation/containers.nix') diff --git a/nixos/modules/virtualisation/containers.nix b/nixos/modules/virtualisation/containers.nix index 7f545a9d3031..01f63315b9cb 100644 --- a/nixos/modules/virtualisation/containers.nix +++ b/nixos/modules/virtualisation/containers.nix @@ -292,5 +292,27 @@ in environment.systemPackages = [ nixos-container ]; + # Start containers at boot time. + systemd.services.all-containers = + { description = "All Containers"; + + wantedBy = [ "multi-user.target" ]; + + serviceConfig.Type = "oneshot"; + + script = + '' + res=0 + for i in /etc/containers/*.conf; do + AUTO_START= + source "$i" + if [ "$AUTO_START" = 1 ]; then + systemctl start "container@$(basename "$i" .conf).service" || res=1 + fi + done + exit $res + ''; # */ + }; + }; } -- cgit 1.4.1 From 32b977d4a7f4615859827780513fdcd7cb21ade7 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Tue, 12 Aug 2014 02:33:30 +0200 Subject: Containers: Fix reboot and poweroff Previously "machinectl reboot/poweroff" brutally killed the container, as did "systemctl stop/restart". And reboot didn't actually work. Now everything is fine. --- nixos/modules/virtualisation/containers.nix | 43 +++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 12 deletions(-) (limited to 'nixos/modules/virtualisation/containers.nix') diff --git a/nixos/modules/virtualisation/containers.nix b/nixos/modules/virtualisation/containers.nix index 01f63315b9cb..6131d75e478f 100644 --- a/nixos/modules/virtualisation/containers.nix +++ b/nixos/modules/virtualisation/containers.nix @@ -168,6 +168,9 @@ in preStart = '' + # Clean up existing machined registration. + machinectl terminate "$INSTANCE" 2> /dev/null || true + mkdir -p -m 0755 $root/var/lib # Create a named pipe to get a signal when the container @@ -205,6 +208,7 @@ in fi ''} + EXIT_ON_REBOOT=1 \ exec ${config.systemd.package}/bin/systemd-nspawn \ --keep-unit \ -M "$INSTANCE" -D "$root" $extraFlags \ @@ -242,23 +246,38 @@ in preStop = '' - machinectl poweroff "$INSTANCE" + machinectl poweroff "$INSTANCE" || true ''; restartIfChanged = false; #reloadIfChanged = true; # FIXME - serviceConfig.ExecReload = pkgs.writeScript "reload-container" - '' - #! ${pkgs.stdenv.shell} -e - SYSTEM_PATH=/nix/var/nix/profiles/system - echo $SYSTEM_PATH/bin/switch-to-configuration test | \ - ${pkgs.socat}/bin/socat unix:$root/var/lib/run-command.socket - - ''; - - serviceConfig.SyslogIdentifier = "container %i"; - - serviceConfig.EnvironmentFile = "-/etc/containers/%i.conf"; + serviceConfig = { + ExecReload = pkgs.writeScript "reload-container" + '' + #! ${pkgs.stdenv.shell} -e + SYSTEM_PATH=/nix/var/nix/profiles/system + echo $SYSTEM_PATH/bin/switch-to-configuration test | \ + ${pkgs.socat}/bin/socat unix:$root/var/lib/run-command.socket - + ''; + + SyslogIdentifier = "container %i"; + + EnvironmentFile = "-/etc/containers/%i.conf"; + + # Note that on reboot, systemd-nspawn returns 10, so this + # unit will be restarted. On poweroff, it returns 0, so the + # unit won't be restarted. + Restart = "on-failure"; + + # Hack: we don't want to kill systemd-nspawn, since we call + # "machinectl poweroff" in preStop to shut down the + # container cleanly. But systemd requires sending a signal + # (at least if we want remaining processes to be killed + # after the timeout). So send an ignored signal. + KillMode = "mixed"; + KillSignal = "WINCH"; + }; }; # Generate a configuration file in /etc/containers for each -- cgit 1.4.1 From ceb67cc9ef9c4e2e825a2a192c564e8208e263eb Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Tue, 12 Aug 2014 03:05:27 +0200 Subject: Containers: Clean up veth interfaces --- nixos/modules/virtualisation/containers.nix | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'nixos/modules/virtualisation/containers.nix') diff --git a/nixos/modules/virtualisation/containers.nix b/nixos/modules/virtualisation/containers.nix index 6131d75e478f..21ef0d0ff966 100644 --- a/nixos/modules/virtualisation/containers.nix +++ b/nixos/modules/virtualisation/containers.nix @@ -168,9 +168,13 @@ in preStart = '' - # Clean up existing machined registration. + # Clean up existing machined registration and interfaces. machinectl terminate "$INSTANCE" 2> /dev/null || true + if [ "$PRIVATE_NETWORK" = 1 ]; then + ip link del dev "ve-$INSTANCE" 2> /dev/null || true + fi + mkdir -p -m 0755 $root/var/lib # Create a named pipe to get a signal when the container -- cgit 1.4.1 From 11d99048c1dc992a68579da7a2958ec57e718ac1 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Wed, 13 Aug 2014 00:45:36 +0200 Subject: Containers: Use systemd-nspawn startup notification --- nixos/modules/virtualisation/containers.nix | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'nixos/modules/virtualisation/containers.nix') diff --git a/nixos/modules/virtualisation/containers.nix b/nixos/modules/virtualisation/containers.nix index 21ef0d0ff966..99329c1f1dda 100644 --- a/nixos/modules/virtualisation/containers.nix +++ b/nixos/modules/virtualisation/containers.nix @@ -230,12 +230,6 @@ in postStart = '' - # This blocks until the container-startup-done service - # writes something to this pipe. FIXME: it also hangs - # until the start timeout expires if systemd-nspawn exits. - read x < $root/var/lib/startup-done - rm -f $root/var/lib/startup-done - if [ "$PRIVATE_NETWORK" = 1 ]; then ifaceHost=ve-$INSTANCE ip link set dev $ifaceHost up @@ -246,6 +240,12 @@ in ip route add $LOCAL_ADDRESS dev $ifaceHost fi fi + + # This blocks until the container-startup-done service + # writes something to this pipe. FIXME: it also hangs + # until the start timeout expires if systemd-nspawn exits. + read x < $root/var/lib/startup-done + rm -f $root/var/lib/startup-done ''; preStop = @@ -269,6 +269,8 @@ in EnvironmentFile = "-/etc/containers/%i.conf"; + Type = "notify"; + # Note that on reboot, systemd-nspawn returns 10, so this # unit will be restarted. On poweroff, it returns 0, so the # unit won't be restarted. -- cgit 1.4.1 From 2c899859bffc3018d3e8fd625647edda01130913 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Mon, 18 Aug 2014 16:57:05 +0200 Subject: Containers: Use systemd startup notification Systemd in a container will call sd_notify when it has finished booting, so we can use that to signal that the container is ready. This does require some fiddling with $NOTIFY_SOCKET. --- nixos/modules/virtualisation/containers.nix | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) (limited to 'nixos/modules/virtualisation/containers.nix') diff --git a/nixos/modules/virtualisation/containers.nix b/nixos/modules/virtualisation/containers.nix index 99329c1f1dda..847c1eddab2a 100644 --- a/nixos/modules/virtualisation/containers.nix +++ b/nixos/modules/virtualisation/containers.nix @@ -32,7 +32,10 @@ let fi fi - exec "$1" + # Start the regular stage 1 script, passing the bind-mounted + # notification socket from the host to allow the container + # systemd to signal readiness to the host systemd. + NOTIFY_SOCKET=/var/lib/private/host-notify exec "$1" ''; system = config.nixpkgs.system; @@ -174,18 +177,12 @@ in if [ "$PRIVATE_NETWORK" = 1 ]; then ip link del dev "ve-$INSTANCE" 2> /dev/null || true fi - - mkdir -p -m 0755 $root/var/lib - - # Create a named pipe to get a signal when the container - # has finished booting. - rm -f $root/var/lib/startup-done - mkfifo -m 0600 $root/var/lib/startup-done ''; script = '' mkdir -p -m 0755 "$root/etc" "$root/var/lib" + mkdir -p -m 0700 "$root/var/lib/private" if ! [ -e "$root/etc/os-release" ]; then touch "$root/etc/os-release" fi @@ -212,13 +209,16 @@ in fi ''} - EXIT_ON_REBOOT=1 \ + # Run systemd-nspawn without startup notification (we'll + # wait for the container systemd to signal readiness). + EXIT_ON_REBOOT=1 NOTIFY_SOCKET= \ exec ${config.systemd.package}/bin/systemd-nspawn \ --keep-unit \ -M "$INSTANCE" -D "$root" $extraFlags \ --bind-ro=/nix/store \ --bind-ro=/nix/var/nix/db \ --bind-ro=/nix/var/nix/daemon-socket \ + --bind=/run/systemd/notify:/var/lib/private/host-notify \ --bind="/nix/var/nix/profiles/per-container/$INSTANCE:/nix/var/nix/profiles" \ --bind="/nix/var/nix/gcroots/per-container/$INSTANCE:/nix/var/nix/gcroots" \ --setenv PRIVATE_NETWORK="$PRIVATE_NETWORK" \ @@ -240,12 +240,6 @@ in ip route add $LOCAL_ADDRESS dev $ifaceHost fi fi - - # This blocks until the container-startup-done service - # writes something to this pipe. FIXME: it also hangs - # until the start timeout expires if systemd-nspawn exits. - read x < $root/var/lib/startup-done - rm -f $root/var/lib/startup-done ''; preStop = @@ -271,6 +265,8 @@ in Type = "notify"; + NotifyAccess = "all"; + # Note that on reboot, systemd-nspawn returns 10, so this # unit will be restarted. On poweroff, it returns 0, so the # unit won't be restarted. -- cgit 1.4.1 From 23db49cf98d6553c56aca99c174c90d84249a2e5 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Tue, 19 Aug 2014 10:06:16 +0200 Subject: Containers: Fix all-containers.service start Fixes #3662. --- nixos/modules/virtualisation/containers.nix | 3 +++ 1 file changed, 3 insertions(+) (limited to 'nixos/modules/virtualisation/containers.nix') diff --git a/nixos/modules/virtualisation/containers.nix b/nixos/modules/virtualisation/containers.nix index 847c1eddab2a..292b96e6eb24 100644 --- a/nixos/modules/virtualisation/containers.nix +++ b/nixos/modules/virtualisation/containers.nix @@ -319,11 +319,14 @@ in wantedBy = [ "multi-user.target" ]; + unitConfig.ConditionDirectoryNotEmpty = "/etc/containers"; + serviceConfig.Type = "oneshot"; script = '' res=0 + shopt -s nullglob for i in /etc/containers/*.conf; do AUTO_START= source "$i" -- cgit 1.4.1