: nply/foundationpkgs

commit 26ccfcf6aa61db7ddaccb4b4556fc02afb0e5ed0
parent ca519b2bfb283e6ccd5450e1783fd7b09b259e1a
Author: Bharatvaj Hemanth <bharatvaj@yahoo.com>
Date: Fri, 30 Oct 2026 23:05:53 +0530

Stage checked systemd-to-OpenRC service packaging during builds
10 files changed, 214 insertions(+), 8 deletions(-)
M
.config/Jobfile
|
3
+++
M
docs/ci.md
|
15
++++++++++-----
M
docs/usage.md
|
56
++++++++++++++++++++++++++++++++++++++++++++++++++++++++
M
noarch/initify/APKBUILD
|
10
++++++++--
A
noarch/initify/check-package.sh
|
43
+++++++++++++++++++++++++++++++++++++++++++
A
noarch/initify/initify-package
|
72
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
M
scripts/fpkgs-ci.sh
|
2
+-
A
scripts/prepare-ci-tools.sh
|
16
++++++++++++++++
M
tests/apk-backend.sh
|
3
+++
M
tests/initify-recipe.sh
|
2
++
diff --git a/.config/Jobfile b/.config/Jobfile
@@ -28,6 +28,9 @@ plan() {
 packages() {
 	# /tmp is a small tmpfs on the CI host; Go build work belongs on disk.
 	mkdir -p "$workdir/tmp"
+	# Build/stage the native converter from this revision; no host installation.
+	ci_tools=$(TMPDIR="$workdir/tmp" sh "$srcdir/scripts/prepare-ci-tools.sh" "$workdir/ci-tools") || return 1
+	export PATH="$ci_tools:$PATH"
 	for format in apk deb; do
 		case "$format" in apk) selected=$ci_apk_packages ;; deb) selected=$ci_deb_packages ;; esac
 		[ -n "$selected" ] || continue
diff --git a/docs/ci.md b/docs/ci.md
@@ -36,10 +36,13 @@ target, and revision rather than inferring availability from overall job state.
 ## Init-system conversion tool
 
 `noarch/initify/APKBUILD` pins [goose121/initify](https://github.com/goose121/initify)
-and packages `initify` and `cronify`. It supports APK, opt-in Debian, and legacy
-archive output. The required CI build makes the tool available as an artifact;
-it does **not** install packages into the running CI host. Perl is in the host
-preparation manifest. Installing the resulting tool follows the normal reviewed
+and packages `initify`, `cronify`, and the checked `initify-package` staging helper.
+It supports APK, opt-in Debian, and legacy
+archive output. The required CI build makes the tool available as an artifact. Before package
+builds, `scripts/prepare-ci-tools.sh` also builds the converter from the same
+revision and stages its executables on the native build PATH, allowing service
+recipes to call `initify-package`. Neither step installs into the running CI
+host. Perl is in the host preparation manifest. Installing the resulting tool follows the normal reviewed
 APT workflow after publication through `foundationpkgs-binaries`.
 
 Use `initify example.service` in a disposable directory: it writes an OpenRC

@@ -52,7 +55,9 @@ sandboxing, or equivalent oneshot semantics. Timer conversion is also limited;
 the recipe exercises only a daily calendar fixture. Never assume the stagit
 queue service can be deployed safely by passing it through this converter.
 Review generated output and supply platform-specific behavior separately.
-No automatic conversion/install/activation is added to package recipes here.
+Service recipes opt into checked build-time conversion with `initify-package`;
+see [service packaging](usage.md#service-packaging). Arbitrary upstream units are
+not automatically converted, and packages never enable/start a generated service.
 
 ## Prepare the Debian CI host
 
diff --git a/docs/usage.md b/docs/usage.md
@@ -144,6 +144,62 @@ Published `.deb` files are not themselves OpenPGP-signed: the separate
 `foundationpkgs-binaries` publisher retains them locally and signs APT metadata
 with a local-only archive key. See that repository's `docs/apt.md`.
 
+## Service packaging
+
+Author systemd units as the **single canonical source**. Do not maintain a second
+handwritten init.d definition. Service recipes use the native build dependency
+`initify` and call its checked staging helper from `package()`:
+
+```sh
+# APKBUILD (include example.service in source/checksums):
+makedepends="initify"
+package() {
+    # Install application files first, then stage the target service integration.
+    initify-package "${FPKGS_INIT_SYSTEM:?select systemd or openrc}" \
+        "$pkgdir" "$srcdir/example.service"
+}
+```
+
+Select the init system independently of package format/ABI, for example:
+
+```sh
+FPKGS_INIT_SYSTEM=openrc scripts/build.sh -f apk path/to/APKBUILD
+FPKGS_INIT_SYSTEM=systemd scripts/build.sh -f deb -t x86_64-unknown-linux-gnu path/to/APKBUILD
+```
+
+`systemd` installs original units as mode 644 under `/usr/lib/systemd/system/`.
+`openrc` converts services during the build and installs mode-755 scripts under
+`/etc/init.d/`. There is no conversion on client installation and no automatic
+service enablement. `FPKGS_INIT_SYSTEM` is part of the CI build key. Do not publish
+different init payloads under an identical package name/version to the same
+repository: choose one init variant per release channel, or distinct integration
+package names when distributing both. No default is inferred from APK vs DEB.
+
+The helper validates **all units before installing any**. For OpenRC it allows
+only a conservative subset: `Description`, a single absolute `ExecStart` with
+unquoted literal arguments, optional `/run/` `PIDFile`, and `Type=simple` (also the
+systemd default when absent). `WantedBy=multi-user.target` is accepted only as
+administrator enablement metadata; no runlevel is enabled. It runs `initify` in
+private staging, adds foreground background/PID tracking for OpenRC, checks the
+result with `sh -n`, and never modifies the canonical source. Unsupported types,
+directives, shell expansions, instance specifiers, duplicate assignments, and
+non-service OpenRC inputs fail the build. Whitespace normalization and quoting
+outside this subset are intentionally rejected, not guessed.
+
+In particular, `.path` watchers, timers, user/group changes, working directories,
+environment files, pre-start commands and systemd sandbox settings need explicit
+converter support before they can be packaged for OpenRC. The current stagit
+queue units are **not supported** by this converter. systemd builds retain their
+original behavior because those units are installed unchanged.
+
+The foundationpkgs CI package job builds initify from the current revision and
+stages it on its native PATH with `scripts/prepare-ci-tools.sh`; nothing is
+installed into the host. The Debian and APK backends inherit that PATH. For
+manual builds, use `toolbin=$(sh scripts/prepare-ci-tools.sh /absolute/work/path)`
+and prepend it to PATH. A legacy archive build can also stage a previously built
+initify archive from the same `-o` directory via `makedepends`. Other dependency
+provisioning remains separate; do not add sudo or APT calls to recipes.
+
 ## CI/package planner
 
 Plan work for a revision:
diff --git a/noarch/initify/APKBUILD b/noarch/initify/APKBUILD
@@ -1,6 +1,6 @@
 pkgname=initify
 pkgver=0_git20220129
-pkgrel=0
+pkgrel=1
 deb_pkgver=0+git20220129
 deb_depends="perl"
 pkgdesc="Convert simple systemd services to OpenRC init scripts"

@@ -12,11 +12,15 @@ makedepends="perl"
 _commit=fa83de1aa5b5bd2477f0a0b86263c6c0d1339f0d
 source="$url/archive/$_commit.tar.gz
 	remove-unused-switch.patch
-	check.sh"
+	check.sh
+	initify-package
+	check-package.sh"
 sha256sums="
 3863890668a943a246f7a4463f6a82f08f0e02b999208976bb9f1dfd781e117b  $_commit.tar.gz
 f7a311c7a976a2c05925bd61da0f597ebcfb25f89757fb86b3f27362737020d1  remove-unused-switch.patch
 c06f7a0e2a2e156217f96b56f77959e5ba439a60e0b8b2a666a22d76def430aa  check.sh
+88bcae7bf2e4cdad1fb4f286118ab0744ff2ff15485251b9833e528c861a7785  initify-package
+15b7bdd0e1bd8c5a75fd3c4b0f892b025e313fa985050871d592742dd6badfed  check-package.sh
 "
 builddir="$srcdir/$pkgname-$_commit"
 

@@ -28,12 +32,14 @@ build() {
 check() {
 	cd "$builddir"
 	sh "$srcdir/check.sh"
+	sh "$srcdir/check-package.sh" "$srcdir/initify-package"
 }
 
 package() {
 	cd "$builddir"
 	mkdir -p "$pkgdir/usr/bin" "$pkgdir/usr/share/licenses/$pkgname" "$pkgdir/usr/share/doc/$pkgname"
 	install -m755 initify.pl "$pkgdir/usr/bin/initify"
+	install -m755 "$srcdir/initify-package" "$pkgdir/usr/bin/initify-package"
 	# Upstream cronify uses /bin/env, which is not portable to Debian hosts.
 	sed '1s|.*|#!/usr/bin/perl|' cronify.pl > "$pkgdir/usr/bin/cronify"
 	chmod 755 "$pkgdir/usr/bin/cronify"
diff --git a/noarch/initify/check-package.sh b/noarch/initify/check-package.sh
@@ -0,0 +1,43 @@
+#!/bin/sh
+set -eu
+work=$(mktemp -d)
+trap 'rm -rf "$work"' 0
+mkdir -p "$work/bin"
+cp initify.pl "$work/bin/initify"
+chmod 755 "$work/bin/initify"
+export PATH="$work/bin:$PATH"
+helper=$1
+cat > "$work/example.service" <<'UNIT'
+[Unit]
+Description=Example foreground service
+[Service]
+Type=simple
+ExecStart=/bin/sleep 300
+[Install]
+WantedBy=multi-user.target
+UNIT
+sh "$helper" systemd "$work/systemd" "$work/example.service"
+cmp "$work/example.service" "$work/systemd/usr/lib/systemd/system/example.service"
+[ ! -d "$work/systemd/etc/init.d" ]
+sh "$helper" openrc "$work/openrc" "$work/example.service"
+script=$work/openrc/etc/init.d/example
+[ -x "$script" ]
+grep -Fx 'command=/bin/sleep' "$script"
+grep -Fx 'command_background=true' "$script"
+grep -Fx 'pidfile=/run/example.pid' "$script"
+[ ! -d "$work/openrc/usr/lib/systemd" ]
+sh -n "$script"
+for directive in 'User=nobody' 'EnvironmentFile=/etc/example' 'WorkingDirectory=/tmp' 'ExecStartPre=/bin/true' 'Type=oneshot' 'ProtectSystem=strict' 'ExecStart=/bin/sleep ${UNSAFE}' 'ExecStart=/bin/sleep %i' 'ExecStart=/bin/sleep 600'; do
+    # Duplicate Type/ExecStart are also rejected rather than guessed.
+    { cat "$work/example.service"; printf '\n[Service]\n%s\n' "$directive"; } > "$work/bad.service"
+    if sh "$helper" openrc "$work/rejected" "$work/bad.service" > "$work/error" 2>&1; then
+        echo "Accepted unsupported directive: $directive" >&2; exit 1
+    fi
+    [ ! -d "$work/rejected/etc/init.d" ]
+done
+printf '[Path]\nPathExists=/tmp/pending\n' > "$work/example.path"
+sh "$helper" systemd "$work/systemd" "$work/example.path"
+if sh "$helper" openrc "$work/mixed" "$work/example.service" "$work/example.path" > "$work/error" 2>&1; then exit 1; fi
+[ ! -d "$work/mixed/etc/init.d" ]
+if sh "$helper" unknown "$work/unknown" "$work/example.service" > "$work/error" 2>&1; then exit 1; fi
+echo 'PASS: systemd originals, OpenRC foreground tracking, unsupported-directive rejection, no partial unit set'
diff --git a/noarch/initify/initify-package b/noarch/initify/initify-package
@@ -0,0 +1,72 @@
+#!/bin/sh
+# Build-time packaging only: never enable or start services.
+set -eu
+[ "$#" -ge 3 ] || { echo 'usage: initify-package systemd|openrc PKGDIR UNIT...' >&2; exit 1; }
+init=$1
+pkgdir=$2
+shift 2
+case "$init" in systemd|openrc) ;; *) echo "Unsupported init system: $init" >&2; exit 1;; esac
+[ -n "$pkgdir" ] && [ "$pkgdir" != / ] || { echo 'A staging PKGDIR is required' >&2; exit 1; }
+mkdir -p "$pkgdir"
+pkgdir=$(cd "$pkgdir" && pwd -P)
+[ "$pkgdir" != / ] || exit 1
+work=$(mktemp -d)
+trap 'rm -rf "$work"' 0
+trap 'exit 1' HUP INT TERM
+
+for unit do
+    name=$(basename "$unit")
+    case "$name" in ''|*[!a-zA-Z0-9_.-]*|-*|.*) echo "Unsafe unit name: $name" >&2; exit 1;; esac
+    [ ! -e "$work/$name" ] || { echo "Duplicate unit: $name" >&2; exit 1; }
+    [ -f "$unit" ] || { echo "Missing unit: $unit" >&2; exit 1; }
+    cp "$unit" "$work/$name"
+    if [ "$init" = systemd ]; then
+        case "$name" in *.service|*.timer|*.path|*.socket|*.target) ;; *) echo "Unsupported unit file: $name" >&2; exit 1;; esac
+        continue
+    fi
+    case "$name" in *.service) ;; *) echo "OpenRC conversion does not support $name" >&2; exit 1;; esac
+    # initify silently ignores unknown directives. Allow only the subset whose
+    # execution semantics this wrapper handles; reject expansions/shell syntax.
+    awk '
+    function reject() { print FILENAME ":" NR ": unsupported: " $0 > "/dev/stderr"; bad=1 }
+    /^[[:space:]]*([#;]|$)/ { next }
+    /^\[(Unit|Service|Install)\]$/ { section=$0; next }
+    {
+        n=index($0,"="); if (!n) { reject(); next }
+        key=substr($0,1,n-1); value=substr($0,n+1)
+        if (seen[section,key]++) { reject(); next }
+        if (section=="[Unit]" && key=="Description" && value ~ /^[a-zA-Z0-9 .,:_()+\/-]+$/) next
+        if (section=="[Service]" && key=="Type" && value=="simple") next
+        if (section=="[Service]" && key=="ExecStart" && value ~ /^\/[a-zA-Z0-9_.\/-]+( [a-zA-Z0-9_.\/:,@=+-]+)*$/) { start++; next }
+        if (section=="[Service]" && key=="PIDFile" && value ~ /^\/run\/[a-zA-Z0-9_.\/-]+$/ && value !~ /\.\./) next
+        # Enablement is administrator policy; installation never enables a unit.
+        if (section=="[Install]" && key=="WantedBy" && value=="multi-user.target") next
+        reject()
+    }
+    END { if (start!=1) { print FILENAME ": exactly one ExecStart is required" > "/dev/stderr"; bad=1 }; exit bad }
+    ' "$work/$name"
+    command -v initify >/dev/null 2>&1 || { echo 'initify is required as a native build dependency' >&2; exit 1; }
+    initify --name "${name%.service}" "$work/$name"
+    generated=$work/${name%.service}
+    [ -s "$generated" ] || { echo "initify did not generate $name" >&2; exit 1; }
+    grep -Fx '#!/sbin/openrc-run' "$generated" >/dev/null
+    # systemd Type=simple is a foreground process. OpenRC must background it
+    # and track a PID; initify alone does not emit this setting.
+    printf '\ncommand_background=true\n' >> "$generated"
+    if ! grep -Eq '^pidfile=.' "$generated"; then
+        printf 'pidfile=/run/%s.pid\n' "${name%.service}" >> "$generated"
+    fi
+    sh -n "$generated"
+done
+
+# Do not leave a partially installed set when validation/conversion fails.
+for unit do
+    name=$(basename "$unit")
+    if [ "$init" = systemd ]; then
+        mkdir -p "$pkgdir/usr/lib/systemd/system"
+        install -m644 "$work/$name" "$pkgdir/usr/lib/systemd/system/$name"
+    else
+        mkdir -p "$pkgdir/etc/init.d"
+        install -m755 "$work/${name%.service}" "$pkgdir/etc/init.d/${name%.service}"
+    fi
+done
diff --git a/scripts/fpkgs-ci.sh b/scripts/fpkgs-ci.sh
@@ -133,7 +133,7 @@ local_sources() {
 
 build_env_fingerprint() {
 	printf 'format=%s\n' "$(package_format)"
-	for name in MAKEFLAGS CFLAGS CPPFLAGS CXXFLAGS LDFLAGS PKG_CONFIG_PATH PKG_CONFIG_LIBDIR PKG_CONFIG_SYSROOT_DIR CC CXX AR STRIP CHOST CBUILD CTARGET CARCH FPKGS_ABUILD APK CBUILDROOT SRCDEST options PACKAGER PACKAGER_PUBKEY; do
+	for name in MAKEFLAGS CFLAGS CPPFLAGS CXXFLAGS LDFLAGS PKG_CONFIG_PATH PKG_CONFIG_LIBDIR PKG_CONFIG_SYSROOT_DIR CC CXX AR STRIP CHOST CBUILD CTARGET CARCH FPKGS_INIT_SYSTEM FPKGS_ABUILD APK CBUILDROOT SRCDEST options PACKAGER PACKAGER_PUBKEY; do
 		eval val=\${$name:-}
 		printf '%s=%s\n' "$name" "$val"
 	done
diff --git a/scripts/prepare-ci-tools.sh b/scripts/prepare-ci-tools.sh
@@ -0,0 +1,16 @@
+#!/bin/sh
+# Stage native build tools in CI work space, never install into the live host.
+set -eu
+[ "$#" = 1 ] || { echo "usage: $0 WORKDIR" >&2; exit 1; }
+root=$(CDPATH= cd -- "$(dirname "$0")/.." && pwd)
+mkdir -p "$1"
+base=$(CDPATH= cd -- "$1" && pwd)
+work=$(mktemp -d "$base/initify.XXXXXX")
+"$root/scripts/build.sh" -f tar.gz -o "$work/out" -s "$work/sysroot" \
+    -n "$work/native" "$root/noarch/initify/APKBUILD" >&2
+set -- "$work"/out/noarch/initify-*.tar.gz
+[ "$#" = 1 ] && [ -f "$1" ] || { echo 'Missing or ambiguous CI tool archive' >&2; exit 1; }
+mkdir "$work/stage"
+tar -xzf "$1" -C "$work/stage"
+[ -x "$work/stage/usr/bin/initify" ] && [ -x "$work/stage/usr/bin/initify-package" ]
+printf '%s\n' "$work/stage/usr/bin"
diff --git a/tests/apk-backend.sh b/tests/apk-backend.sh
@@ -91,6 +91,9 @@ cli=$root/scripts/fpkgs-ci.sh
 apk_key=$(sh "$cli" key noarch/fixture/APKBUILD "$FPKGS_TARGETS" "$rev")
 tar_key=$(FPKGS_FORMAT=tar.gz sh "$cli" key noarch/fixture/APKBUILD "$FPKGS_TARGETS" "$rev")
 [ "$apk_key" != "$tar_key" ]
+systemd_key=$(FPKGS_INIT_SYSTEM=systemd sh "$cli" key noarch/fixture/APKBUILD "$FPKGS_TARGETS" "$rev")
+openrc_key=$(FPKGS_INIT_SYSTEM=openrc sh "$cli" key noarch/fixture/APKBUILD "$FPKGS_TARGETS" "$rev")
+[ "$systemd_key" != "$openrc_key" ]
 FPKGS_PACKAGES=not-selected sh "$cli" plan "$rev" >"$work/filter"
 [ ! -s "$work/filter" ]
 FPKGS_PACKAGES=fixture sh "$cli" plan "$rev" >"$work/filter"
diff --git a/tests/initify-recipe.sh b/tests/initify-recipe.sh
@@ -10,12 +10,14 @@ trap 'rm -rf "$work"' 0
     . "$root/noarch/initify/APKBUILD"
     [ "$arch" = noarch ] && [ "$deb_depends" = perl ]
     mkdir -p "$builddir"
+    cp "$root/noarch/initify/initify-package" "$srcdir/initify-package"
     printf '#!/usr/bin/perl\nprint "initify";\n' > "$builddir/initify.pl"
     printf '#!/bin/env perl\nprint "cronify";\n' > "$builddir/cronify.pl"
     printf 'MIT\n' > "$builddir/LICENSE"
     printf '# Initify\n' > "$builddir/README.md"
     package
     [ -x "$pkgdir/usr/bin/initify" ] && [ -x "$pkgdir/usr/bin/cronify" ]
+    [ -x "$pkgdir/usr/bin/initify-package" ]
     [ "$(head -n1 "$pkgdir/usr/bin/cronify")" = '#!/usr/bin/perl' ]
     [ -f "$pkgdir/usr/share/licenses/initify/LICENSE" ]
 )