Skip to content

Commit

Permalink
Optimise and fix s6
Browse files Browse the repository at this point in the history
  • Loading branch information
modem7 committed Apr 25, 2024
1 parent 37d5e7f commit 00e5516
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 11 deletions.
4 changes: 3 additions & 1 deletion base-fullbuild/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ ENV LANG='en_US.UTF-8' \
S6_LOGGING="1" \
S6_VERBOSITY="0" \
S6_CMD_WAIT_FOR_SERVICES_MAXTIME="0" \
S6_STAGE2_HOOK="/container-init" \
TZ="Europe/London"

RUN <<EOF
Expand Down Expand Up @@ -74,7 +75,8 @@ RUN --mount=type=cache,id=pip,target=/root/.cache,sharing=locked \
borgmatic --bash-completion > "$(pkg-config --variable=completionsdir bash-completion)"/borgmatic
EOF

COPY --chmod=744 --link root/ /
COPY --link root/ /
COPY --chmod=744 --link /scripts/container-init /container-init

VOLUME /root/.borgmatic
VOLUME /root/.config/borg
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
oneshot
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# This file doesn't do anything, it's just the end of the downstream image init process
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
#!/usr/bin/with-contenv bash
#!/command/with-contenv bash

# Install DockerCLI if true
if [ "${DOCKERCLI}" == "true" ]; then
echo "[custom-init] Installing Docker CLI and Compose..."
echo "[custom-init] Installing Docker CLI and Docker Compose..."
apk add -U --quiet docker-cli docker-cli-compose
else
echo "[custom-init] Docker CLI variable not set, skipping..."
fi

# Install additional packages
if [ -v EXTRA_PKGS ]
then
echo "[custom-init] Installing extra packages: $EXTRA_PKGS"
apk add -U --quiet $EXTRA_PKGS
if [ -v EXTRA_PKGS ]; then
echo "[custom-init] Installing extra packages: $EXTRA_PKGS"
apk add -U --quiet $EXTRA_PKGS
else
echo "[custom-init] No custom packages found, skipping..."
fi
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/with-contenv bash
#!/command/with-contenv bash

# Directories
SCRIPTS_DIR="/custom-cont-init.d"
Expand Down
Empty file.
4 changes: 2 additions & 2 deletions base-fullbuild/root/etc/s6-overlay/s6-rc.d/svc-cron/finish
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/command/with-contenv bash

if test "$1" -eq 256 ; then
e=$((128 + $2))
Expand All @@ -7,4 +7,4 @@ else
fi

echo "Received exit code $e."
echo "$e" > /run/s6-linux-init-container-results/exitcode
echo "$e" > /run/s6-linux-init-container-results/exitcode
3 changes: 2 additions & 1 deletion base-fullbuild/root/etc/s6-overlay/s6-rc.d/svc-cron/run
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/with-contenv bash
#!/command/with-contenv bash

# Version variables
borgver=$(borg --version)
Expand All @@ -15,6 +15,7 @@ else
fi

# Software versions
echo "-----------------------------------"
echo "Software Versions:
-----------------------------------
apprise $apprisever
Expand Down
57 changes: 57 additions & 0 deletions base-fullbuild/scripts/container-init
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/command/with-contenv bash

# Borrowed heavily from https://github.com/linuxserver/docker-mods/tree/mod-scripts

echo "[init] Initialising Container..."

# Define custom folder paths
SCRIPTS_DIR="/custom-cont-init.d"
SERVICES_DIR="/custom-services.d"

# Set executable bit on cont-init and services built into the image
set_legacy_executable_bits() {
mkdir -p /etc/{cont-init.d,services.d}
chmod +x \
/etc/cont-init.d/* \
/etc/services.d/*/* 2>/dev/null || true
}

# https://www.linuxserver.io/blog/2019-09-14-customizing-our-containers#custom-services
process_custom_services() {
# Remove all existing custom services before continuing to ensure
# we aren't running anything the user may have removed
if [[ -n "$(/bin/ls -A /etc/s6-overlay/s6-rc.d/custom-svc-* 2>/dev/null)" ]]; then
echo "[custom-init] removing existing custom services..."
rm -rf /etc/s6-overlay/s6-rc.d/custom-svc-*
rm /etc/s6-overlay/s6-rc.d/user/contents.d/custom-svc-*
fi

# Make sure custom service directory exists and has files in it
if [[ -e "${SERVICES_DIR}" ]] && [[ -n "$(/bin/ls -A ${SERVICES_DIR} 2>/dev/null)" ]]; then
echo "[custom-init] Service files found in ${SERVICES_DIR}"
for SERVICE in "${SERVICES_DIR}"/*; do
NAME="$(basename "${SERVICE}")"
if [[ -f "${SERVICE}" ]]; then
echo "[custom-init] ${NAME}: service detected, copying..."
mkdir -p /etc/s6-overlay/s6-rc.d/custom-svc-"${NAME}"/dependencies.d/
cp "${SERVICE}" /etc/s6-overlay/s6-rc.d/custom-svc-"${NAME}"/run
chmod +x /etc/s6-overlay/s6-rc.d/custom-svc-"${NAME}"/run
echo "longrun" >/etc/s6-overlay/s6-rc.d/custom-svc-"${NAME}"/type
touch /etc/s6-overlay/s6-rc.d/custom-svc-"${NAME}"/dependencies.d/init-services
touch /etc/s6-overlay/s6-rc.d/user/contents.d/custom-svc-"${NAME}"
echo "[custom-init] ${NAME}: copied"
elif [[ ! -f "${SERVICE}" ]]; then
echo "[custom-init] ${NAME}: is not a file"
fi
done
else
echo "[custom-init] No custom services found, skipping..."
fi
}

if [[ -d "${SCRIPTS_DIR}" ]] || [[ -d "${SERVICES_DIR}" ]]; then
process_custom_services
fi

# Set executable bit on legacy cont-init and services built into the image and anything legacy unpacked by mods
set_legacy_executable_bits

0 comments on commit 00e5516

Please sign in to comment.