Skip to content

Commit

Permalink
Make entrypoint systemd instead of cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
dwoz committed Nov 9, 2024
1 parent c027c12 commit 3961bad
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 20 deletions.
27 changes: 27 additions & 0 deletions custom/testing/entrypoint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#! env python3
"""
Systemd entrypoint hack.
Writes /cmd.sh from the arguments passed to the script. Then execve systemd so
systemd will be process 1. The target rescue will execute /cmd.sh using /bin/sh.
This is needed so our ENTRYPOINT can be systemd with a target and CMD can be
customized at runtime as expected.
"""
from __future__ import annotations

import os
import sys

with open("/cmd.sh", "w") as fp:
fp.write(" ".join(sys.argv[1:]))
os.environ["PYTHONPATH"] = os.pathsep.join(sys.path)
cmd = "/usr/lib/systemd/systemd"
if os.execve in os.supports_fd:
with open(cmd, "rb") as fp:
sys.stdout.flush()
sys.stderr.flush()
args = ["--systemd", "--unit=rescue.target"]
os.execve(fp.fileno(), args, os.environ)
else:
args = [cmd, "--systemd", "--unit=rescue.target"]
os.execve(cmd, args, os.environ)
4 changes: 0 additions & 4 deletions custom/testing/golden-state-tree/os/ubuntu/init.sls
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
include:
- .config
- .pkgs

{%- if pillar.get('github_actions_runner', False) %}
- github-actions-runner
{%- endif %}
10 changes: 5 additions & 5 deletions custom/testing/golden-state-tree/os/ubuntu/pkgs/init.sls
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
include:
- os.debian.pkgs.apt-utils
- os.debian.pkgs.libdpkg-perl
- os.debian.pkgs.timesync
- pkgs.bower
# - os.debian.pkgs.libdpkg-perl
# - os.debian.pkgs.timesync
# - pkgs.bower
- pkgs.curl
- pkgs.dmidecode
- pkgs.dnsutils
- pkgs.docker
# - pkgs.docker
- pkgs.gcc
- pkgs.gpg
- pkgs.ipset
Expand All @@ -20,7 +20,7 @@ include:
- pkgs.make
- pkgs.man
- pkgs.nginx
- pkgs.npm
# - pkgs.npm
- pkgs.openldap
- pkgs.openssl
- pkgs.openssl-dev
Expand Down
9 changes: 5 additions & 4 deletions custom/testing/rescue.service
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ Before=shutdown.target
Environment=HOME=/root
WorkingDirectory=-/root
ExecStart=
ExecStart=-/bin/bash
Type=idle
ExecStart=-/bin/sh /cmd.sh
ExecStartPost=/usr/bin/systemctl poweroff
Type=oneshot
StandardInput=tty-force
StandardOutput=inherit
StandardError=inherit
KillMode=process
IgnoreSIGPIPE=no
SendSIGHUP=yes
RemainAfterExit=no
#SendSIGHUP=yes
13 changes: 6 additions & 7 deletions custom/testing/systemd-ubuntu-22.04.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
FROM ubuntu:22.04

COPY 01_nodoc /etc/dpkg/dpkg.cfg.d/01_nodoc


# init and systemd are the only real requirements for systemd.
#
# tar wget x-utils can be used to fetch and extract a salt onedir.
Expand All @@ -15,12 +12,14 @@ RUN apt update -y \
&& echo 'tzdata tzdata/Areas select America' | debconf-set-selections \
&& echo 'tzdata tzdata/Zones/America select Phoenix' | debconf-set-selections \
&& DEBIAN_FRONTEND="noninteractive" apt install -y \
tree tar wget xz-utils apt-utils init systemd
tree tar wget xz-utils apt-utils systemd

COPY 01_nodoc /etc/dpkg/dpkg.cfg.d/01_nodoc
COPY entrypoint.py entrypoint.py
# Set the root password, this was done before single user mode worked.
# RUN echo "root\nroot" | passwd -q root
RUN echo "systemd.debug_shell=tty1" >> /etc/sysctl.conf
# RUN echo "systemd.debug_shell=tty1" >> /etc/sysctl.conf
COPY rescue.service /etc/systemd/system/rescue.service.d/override.conf
RUN echo "alias exit='init 0'" >> /root/.bashrc

CMD [ "/sbin/init", "1" ]
ENTRYPOINT [ "/usr/bin/python3", "entrypoint.py" ]
CMD [ "/bin/bash" ]

0 comments on commit 3961bad

Please sign in to comment.