git.iko.soy/typeable/nixpkgs
on master help sign in
Don't use machinectl to shut down containers

If the host is shutting down, machinectl may fail because it's bus-activated and D-Bus will be shutting down. So just send a signal to the leader process directly. Fixes #6212.

commit307064ceb512d700a2979b7850082036969cb0ae authorEelco Dolstra <eelco.dolstra@logicblox.com> dateSat 28 Feb 2015, 19:20 (12 years ago) parente3c2b4e
1 file changed+12−2
nixos/modules/virtualisation/containers.nix+12−2
nixos/modules/virtualisation/containers.nix+12−2
@@ -203,7 +203,7 @@ in
203203 script =
204204 ''
205205 mkdir -p -m 0755 "$root/etc" "$root/var/lib"
206− mkdir -p -m 0700 "$root/var/lib/private" "$root/root"
206+ mkdir -p -m 0700 "$root/var/lib/private" "$root/root" /run/containers
207207 if ! [ -e "$root/etc/os-release" ]; then
208208 touch "$root/etc/os-release"
209209 fi
@@ -261,11 +261,21 @@ in
261261 ip route add $LOCAL_ADDRESS dev $ifaceHost
262262 fi
263263 fi
264+
265+ # Get the leader PID so that we can signal it in
266+ # preStop. We can't use machinectl there because D-Bus
267+ # might be shutting down. FIXME: in systemd 219 we can
268+ # just signal systemd-nspawn to do a clean shutdown.
269+ machinectl show "$INSTANCE" | sed 's/Leader=\(.*\)/\1/;t;d' > "/run/containers/$INSTANCE.pid"
264270 '';
265271
266272 preStop =
267273 ''
268− machinectl poweroff "$INSTANCE" || true
274+ pid="$(cat /run/containers/$INSTANCE.pid)"
275+ if [ -n "$pid" ]; then
276+ kill -RTMIN+4 "$pid"
277+ fi
278+ rm -f "/run/containers/$INSTANCE.pid"
269279 '';
270280
271281 restartIfChanged = false;