Make starting a container synchronous

So now "systemctl start container@foo" will only return after the
container has reached multi-user.target.
This commit is contained in:
Eelco Dolstra 2014-04-01 16:02:53 +02:00
parent 269926df0d
commit 1ad9a654be
2 changed files with 36 additions and 3 deletions

View File

@ -53,6 +53,20 @@ with lib;
};
};
systemd.services.container-startup-done =
{ description = "Container Startup Notification";
wantedBy = [ "multi-user.target" ];
after = [ "multi-user.target" ];
script =
''
if [ -p /var/lib/startup-done ]; then
echo done > /var/lib/startup-done
fi
'';
serviceConfig.Type = "oneshot";
serviceConfig.RemainAfterExit = true;
};
};
}

View File

@ -150,11 +150,21 @@ in
path = [ pkgs.iproute ];
environment.INSTANCE = "%i";
environment.root = "/var/lib/containers/%i";
preStart =
''
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 $root/var/lib/startup-done
'';
script =
''
root="/var/lib/containers/$INSTANCE"
mkdir -p -m 0755 "$root/etc"
mkdir -p -m 0755 "$root/etc" "$root/var/lib"
if ! [ -e "$root/etc/os-release" ]; then
touch "$root/etc/os-release"
fi
@ -209,6 +219,13 @@ in
"$SYSTEM_PATH/init"
'';
postStart =
''
# This blocks until the container-startup-done service
# writes something to this pipe.
read x < $root/var/lib/startup-done
'';
preStop =
''
pid="$(cat /sys/fs/cgroup/systemd/machine/$INSTANCE.nspawn/system/tasks 2> /dev/null)"
@ -238,8 +255,10 @@ in
. "/etc/containers/$INSTANCE.conf"
fi
echo $SYSTEM_PATH/bin/switch-to-configuration test | \
${pkgs.socat}/bin/socat unix:/var/lib/containers/$INSTANCE/var/lib/root-shell.socket -
${pkgs.socat}/bin/socat unix:$root/var/lib/root-shell.socket -
'';
serviceConfig.SyslogIdentifier = "container %i";
};
# Generate a configuration file in /etc/containers for each