From 88a1b868a8a9dd2696656b273936ea923d625e13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Molinero=20Fern=C3=A1ndez?= Date: Tue, 2 Nov 2021 19:23:18 +0100 Subject: [PATCH] Shutdown services gracefully --- Dockerfile.m4 | 4 +++- scripts/bin/container-init | 15 +++++++++++++-- scripts/service/qemu/control/t | 5 +++++ scripts/service/qemu/run | 2 +- 4 files changed, 22 insertions(+), 4 deletions(-) create mode 100755 scripts/service/qemu/control/t diff --git a/Dockerfile.m4 b/Dockerfile.m4 index dcbe894..ca5d300 100644 --- a/Dockerfile.m4 +++ b/Dockerfile.m4 @@ -50,12 +50,14 @@ RUN export DEBIAN_FRONTEND=noninteractive \ && apt-get update \ && apt-get install -y --no-install-recommends \ net-tools \ + netcat-openbsd \ procps \ python3 \ qemu-kvm \ qemu-system-x86 \ qemu-utils \ runit \ + tini \ && rm -rf /var/lib/apt/lists/* # Environment @@ -90,4 +92,4 @@ COPY --chown=root:root ./scripts/bin/ /usr/local/bin/ RUN find /usr/local/bin/ -type d -not -perm 0755 -exec chmod 0755 '{}' ';' RUN find /usr/local/bin/ -type f -not -perm 0755 -exec chmod 0755 '{}' ';' -ENTRYPOINT ["/usr/local/bin/container-init"] +ENTRYPOINT ["/usr/bin/tini", "--", "/usr/local/bin/container-init"] diff --git a/scripts/bin/container-init b/scripts/bin/container-init index fc88c9a..d1ef86f 100755 --- a/scripts/bin/container-init +++ b/scripts/bin/container-init @@ -7,5 +7,16 @@ if [ ! -f /var/lib/qemu/images/reactos.img ]; then qemu-img create -f qcow2 /var/lib/qemu/images/reactos.img "${VM_DISK_SIZE:?}" fi -# Start all services -exec runsvdir -P /etc/service/ +stop() { + for s in "${SVDIR:?}"/*; do sv force-stop "${s:?}" >/dev/null 2>&1; done + kill -HUP "${RUNSVDIRPID:?}"; wait "${RUNSVDIRPID:?}" +} +trap stop EXIT TERM INT HUP + +if [ "$#" -gt 0 ] || [ -t 0 ] || [ -t 1 ]; then + runsvdir -P "${SVDIR:?}" >/dev/null 2>&1 & RUNSVDIRPID=$! + "$@" +else + runsvdir -P "${SVDIR:?}" & RUNSVDIRPID=$! + wait "${RUNSVDIRPID:?}" +fi diff --git a/scripts/service/qemu/control/t b/scripts/service/qemu/control/t new file mode 100755 index 0000000..08854b0 --- /dev/null +++ b/scripts/service/qemu/control/t @@ -0,0 +1,5 @@ +#!/bin/sh + +set -eu + +printf '%s\n' 'system_powerdown' | nc -U /run/qemu-monitor diff --git a/scripts/service/qemu/run b/scripts/service/qemu/run index f7ad789..d1cb0a0 100755 --- a/scripts/service/qemu/run +++ b/scripts/service/qemu/run @@ -4,7 +4,7 @@ set -eu set -- set -- "$@" -smp "${VM_CPU:?}" -m "${VM_RAM:?}" -set -- "$@" -serial stdio -device VGA -display vnc=:0 +set -- "$@" -serial stdio -monitor unix:/run/qemu-monitor,server,nowait -device VGA -display vnc=:0 set -- "$@" -drive file=/var/lib/qemu/images/reactos.img,index=0,media=disk,format=qcow2 set -- "$@" -drive file=/var/lib/qemu/iso/reactos.iso,index=2,media=cdrom,format=raw set -- "$@" -boot order="${VM_BOOT_ORDER:?}",menu=off