From 208fd1eadd4a49dc927d19ca64521330e49dec57 Mon Sep 17 00:00:00 2001 From: Spencer Heywood Date: Wed, 30 Oct 2024 13:38:38 -0600 Subject: [PATCH] added systemd and openrc containers --- openrc/Dockerfile.alpine | 11 +++++++ openrc/README.md | 3 ++ openrc/openrc.sh | 3 ++ openrc/test-openrc.sh | 8 +++++ systemd/Dockerfile.archlinux | 4 +++ systemd/Dockerfile.centos | 3 ++ systemd/Dockerfile.debian | 6 ++++ systemd/README.md | 5 +++ systemd/build.sh | 12 +++++++ systemd/systemd.sh | 62 ++++++++++++++++++++++++++++++++++++ systemd/test-systemd.sh | 12 +++++++ 11 files changed, 129 insertions(+) create mode 100644 openrc/Dockerfile.alpine create mode 100644 openrc/README.md create mode 100755 openrc/openrc.sh create mode 100755 openrc/test-openrc.sh create mode 100644 systemd/Dockerfile.archlinux create mode 100644 systemd/Dockerfile.centos create mode 100644 systemd/Dockerfile.debian create mode 100644 systemd/README.md create mode 100755 systemd/build.sh create mode 100755 systemd/systemd.sh create mode 100755 systemd/test-systemd.sh diff --git a/openrc/Dockerfile.alpine b/openrc/Dockerfile.alpine new file mode 100644 index 0000000..5d7462a --- /dev/null +++ b/openrc/Dockerfile.alpine @@ -0,0 +1,11 @@ +FROM docker.io/alpine + +RUN apk add openrc +RUN mkdir -p /run/openrc && touch /run/openrc/softlevel +RUN sed -i 's/VSERVER/DOCKER/Ig' /lib/rc/sh/init.sh + +# configured /etc/rc.conf for docker +RUN sed -i '/getty/d' /etc/inittab + +COPY openrc.sh /openrc.sh +ENTRYPOINT ["/openrc.sh"] diff --git a/openrc/README.md b/openrc/README.md new file mode 100644 index 0000000..30413f3 --- /dev/null +++ b/openrc/README.md @@ -0,0 +1,3 @@ +# Credit + +https://github.com/robertdebock/docker-alpine-openrc/tree/master diff --git a/openrc/openrc.sh b/openrc/openrc.sh new file mode 100755 index 0000000..5dc1337 --- /dev/null +++ b/openrc/openrc.sh @@ -0,0 +1,3 @@ +#!/bin/ash +openrc default +exec "$@" diff --git a/openrc/test-openrc.sh b/openrc/test-openrc.sh new file mode 100755 index 0000000..b603c3c --- /dev/null +++ b/openrc/test-openrc.sh @@ -0,0 +1,8 @@ +#!/bin/sh +dir=$(dirname -- "$( readlink -f -- "$0"; )";) +dockerfile="Dockerfile.alpine" +set -ex +echo "=== Testing ${dockerfile} ===" +iid=$(docker build -q -f ${dir}/${dockerfile} ${dir}) +docker run -t --rm --privileged $iid ash -c "rc-status && printf 'OpenRC running!\n'" +docker image rm $iid diff --git a/systemd/Dockerfile.archlinux b/systemd/Dockerfile.archlinux new file mode 100644 index 0000000..022f930 --- /dev/null +++ b/systemd/Dockerfile.archlinux @@ -0,0 +1,4 @@ +FROM docker.io/heywoodlh/archlinux:latest +RUN pacman -Sy --noconfirm systemd systemd-sysvcompat +COPY systemd.sh /docker-entrypoint.sh +ENTRYPOINT ["/docker-entrypoint.sh"] diff --git a/systemd/Dockerfile.centos b/systemd/Dockerfile.centos new file mode 100644 index 0000000..39047eb --- /dev/null +++ b/systemd/Dockerfile.centos @@ -0,0 +1,3 @@ +FROM docker.io/centos:latest +COPY systemd.sh /docker-entrypoint.sh +ENTRYPOINT ["/docker-entrypoint.sh"] diff --git a/systemd/Dockerfile.debian b/systemd/Dockerfile.debian new file mode 100644 index 0000000..6f449c5 --- /dev/null +++ b/systemd/Dockerfile.debian @@ -0,0 +1,6 @@ +FROM debian:latest +RUN apt-get update && \ + apt-get install -y --no-install-recommends \ + systemd systemd-sysv dbus dbus-user-session +COPY systemd.sh /docker-entrypoint.sh +ENTRYPOINT ["/docker-entrypoint.sh"] diff --git a/systemd/README.md b/systemd/README.md new file mode 100644 index 0000000..aa06c9f --- /dev/null +++ b/systemd/README.md @@ -0,0 +1,5 @@ +Container images running systemd for testing + +# Credit + +https://github.com/AkihiroSuda/containerized-systemd diff --git a/systemd/build.sh b/systemd/build.sh new file mode 100755 index 0000000..20b73fa --- /dev/null +++ b/systemd/build.sh @@ -0,0 +1,12 @@ +#!/usr/bin/env bash +operating_systems=("debian" "centos" "archlinux") +dir=$(dirname -- "$( readlink -f -- "$0"; )";) +date_tag=$(date +%Y_%m_snapshot) + +for os in "${operating_systems[@]}" +do + docker buildx build --no-cache --platform "amd64,arm64" --squash -t "heywoodlh/systemd:${os}" -t "docker.io/heywoodlh/systemd:${os}_${date_tag}" -f ${dir}/Dockerfile.${os} . --push +done + +# Set default image to Debian +docker buildx build --no-cache --platform "amd64,arm64" --squash -t "heywoodlh/systemd:latest" -t "docker.io/heywoodlh/systemd:${date_tag}" -f ${dir}/Dockerfile.debian . --push diff --git a/systemd/systemd.sh b/systemd/systemd.sh new file mode 100755 index 0000000..7af2111 --- /dev/null +++ b/systemd/systemd.sh @@ -0,0 +1,62 @@ +#!/bin/bash +set -e +container=docker +export container + +if [ $# -eq 0 ]; then + echo >&2 'ERROR: No command specified. You probably want to run `journalctl -f`, or maybe `bash`?' + exit 1 +fi + +if [ ! -t 0 ]; then + echo >&2 'ERROR: TTY needs to be enabled (`docker run -t ...`).' + exit 1 +fi + +env >/etc/docker-entrypoint-env + +cat >/etc/systemd/system/docker-entrypoint.target </etc/docker-entrypoint-cmd + +cat >/etc/systemd/system/docker-entrypoint.service < /dev/null; then echo >&2 \"got signal \${EXIT_STATUS}\"; systemctl exit \$(( 128 + \$( kill -l \${EXIT_STATUS} ) )); else systemctl exit \${EXIT_STATUS}; fi" +StandardInput=tty-force +StandardOutput=inherit +StandardError=inherit +WorkingDirectory=$(pwd) +EnvironmentFile=/etc/docker-entrypoint-env + +[Install] +WantedBy=multi-user.target +EOF + +systemctl mask systemd-firstboot.service systemd-udevd.service systemd-modules-load.service +systemctl unmask systemd-logind +systemctl enable docker-entrypoint.service + +systemd= +if [ -x /lib/systemd/systemd ]; then + systemd=/lib/systemd/systemd +elif [ -x /usr/lib/systemd/systemd ]; then + systemd=/usr/lib/systemd/systemd +elif [ -x /sbin/init ]; then + systemd=/sbin/init +else + echo >&2 'ERROR: systemd is not installed' + exit 1 +fi +systemd_args="--show-status=false --unit=docker-entrypoint.target" +echo "$0: starting $systemd $systemd_args" +exec $systemd $systemd_args diff --git a/systemd/test-systemd.sh b/systemd/test-systemd.sh new file mode 100755 index 0000000..79c3eef --- /dev/null +++ b/systemd/test-systemd.sh @@ -0,0 +1,12 @@ +#!/bin/bash +dir=$(dirname -- "$( readlink -f -- "$0"; )";) +dockerfiles=("Dockerfile.centos" "Dockerfile.debian") + +set -ex +for dockerfile in "${dockerfiles[@]}"; do + echo "=== Testing ${dockerfile} ===" + iid=$(docker build -q -f ${dir}/${dockerfile} ${dir}) + # NOTE: old version of systemd doesn't wait for `systemctl is-system-running --wait`, so we have `until` loop here. + docker run -t --rm --privileged $iid sh -exc "until systemctl is-system-running --wait; do sleep 1; done; systemctl status --no-pager" + docker image rm $iid +done