From 41f464be1472690ef8af3c1487a8184461ab9bbf Mon Sep 17 00:00:00 2001 From: Nik Lutz Date: Thu, 12 May 2016 17:54:03 +0200 Subject: Using systemctl to start containers. Signed-off-by: Daniel Baumann --- lib/container/start | 53 +++++++++++++++++++++++++++++++--------- share/systemd/container@.service | 11 +++++++++ 2 files changed, 53 insertions(+), 11 deletions(-) create mode 100644 share/systemd/container@.service diff --git a/lib/container/start b/lib/container/start index d0e7e6e..d026bf1 100755 --- a/lib/container/start +++ b/lib/container/start @@ -23,9 +23,12 @@ COMMAND="$(basename ${0})" CONFIG="/etc/container-tools/config" MACHINES="/var/lib/machines" +START="false" +SYSTEMCTL="true" + Parameters () { - LONG_OPTIONS="name:" + LONG_OPTIONS="name:,nspawn,start" OPTIONS="n:" PARAMETERS="$(getopt --longoptions ${LONG_OPTIONS} --name=${COMMAND} --options ${OPTIONS} --shell sh -- ${@})" @@ -46,6 +49,19 @@ Parameters () shift 2 ;; + --nspawn) + # internal option + SYSTEMCTL="false" + shift 1 + ;; + + --start) + # internal option + START="true" + SYSTEMCTL="false" + shift 1 + ;; + --) shift 1 break @@ -78,12 +94,16 @@ then exit 1 fi -STATE="$(machinectl show ${NAME} 2>&1 | awk -F= '/^State=/ { print $2 }')" +case "${START}" in + false) + STATE="$(machinectl show ${NAME} 2>&1 | awk -F= '/^State=/ { print $2 }')" -case "${STATE}" in - running) - echo "'${NAME}': container is already started" >&2 - exit 1 + case "${STATE}" in + running) + echo "'${NAME}': container is already started" >&2 + exit 1 + ;; + esac ;; esac @@ -329,14 +349,25 @@ EOF fi fi -# Run -case "${SET_PROPERTY}" in +case "${SYSTEMCTL}" in + true) + systemctl start container@${NAME}.service + # FIXME start console .. after sleep? + configuration option + exit 0 + ;; +esac + +case "${START}" in true) - ${SETARCH} systemd-nspawn ${BIND} ${BOOT} ${CAPABILITY} ${DIRECTORY} ${DROP_CAPABILITY} ${MACHINE} ${NETWORK_VETH_EXTRA} ${LINK_JOURNAL} ${REGISTER} & \ - systemctl --runtime set-property ${NAME} ${BLOCK_IO_DEVICE_WEIGHT} ${BLOCK_IO_READ_BANDWITH} ${BLOCK_IO_WEIGHT} ${BLOCK_IO_WRITE_BANDWITH} ${CPU_QUOTA} ${CPU_SHARES} ${MEMORY_LIMIT} ${TASKS_MAX} + case "${SET_PROPERTY}" in + true) + systemctl --runtime set-property ${NAME} ${BLOCK_IO_DEVICE_WEIGHT} ${BLOCK_IO_READ_BANDWITH} ${BLOCK_IO_WEIGHT} ${BLOCK_IO_WRITE_BANDWITH} ${CPU_QUOTA} ${CPU_SHARES} ${MEMORY_LIMIT} ${TASKS_MAX} + ;; + esac ;; *) - ${SETARCH} systemd-nspawn ${BIND} ${BOOT} ${CAPABILITY} ${DIRECTORY} ${DROP_CAPABILITY} ${MACHINE} ${NETWORK_VETH_EXTRA} ${LINK_JOURNAL} ${REGISTER} + # Run + ${SETARCH} systemd-nspawn --keep-unit ${BIND} ${BOOT} ${CAPABILITY} ${DIRECTORY} ${DROP_CAPABILITY} ${MACHINE} ${NETWORK_VETH_EXTRA} ${LINK_JOURNAL} ${REGISTER} ;; esac diff --git a/share/systemd/container@.service b/share/systemd/container@.service new file mode 100644 index 0000000..e69a701 --- /dev/null +++ b/share/systemd/container@.service @@ -0,0 +1,11 @@ +[Unit] +Description="Container: %i" + +[Service] +Type=simple +ExecStart=/usr/bin/container start --name %i --nspawn +ExecStartPost=/usr/bin/container start --name %i --start +KillMode=mixed + +[Install] +WantedBy=multi-user.target -- cgit v1.2.3