Migrate distroless rTorrent runtime to openSUSE Tumbleweed
Build and Push Docker Image / build (push) Successful in 1m23s

This commit is contained in:
Ketan Patel committed 2026-11-01 21:45:11 -05:00
1 parent 18c861f52c
commit 5295c67f77
4 files changed
+84 -25

No files matched your search

+11 -5
View File
@@ -1,7 +1,13 @@
# Keep Alpine's musl ABI and rTorrent version; export only runtime dependencies.
FROM alpine:3.24 AS runtime
RUN apk add --no-cache rtorrent ca-certificates tzdata \
build-base curl-dev lddtreepax python3
# Install the distro-packaged rTorrent/libtorrent pair; export its glibc runtime.
FROM opensuse/tumbleweed:latest AS runtime
RUN zypper --non-interactive dup --no-recommends \
&& zypper --non-interactive install --no-recommends \
rtorrent ca-certificates ca-certificates-mozilla timezone \
gcc glibc-devel libcurl-devel binutils python3 \
&& rpm -q rtorrent libtorrent55 \
&& test "$(rpm -q --qf '%{VERSION}' rtorrent)" = "$(rpm -q --qf '%{VERSION}' libtorrent55)" \
&& test "$(printf '%s\n' 0.16.27 "$(rpm -q --qf '%{VERSION}' rtorrent)" | sort -V | head -n1)" = 0.16.27 \
&& zypper clean --all
COPY install/rtorrent-entrypoint.c /build/rtorrent-entrypoint.c
RUN cc -Os -Wall -Wextra -Werror -o /usr/local/bin/rtorrent-entrypoint /build/rtorrent-entrypoint.c \
&& strip /usr/local/bin/rtorrent-entrypoint
@@ -11,7 +17,7 @@ RUN cc -Os -Wall -Wextra -Werror -o /usr/local/bin/rtorrent-public-ip /build/rto
COPY build/runtime-root.py /build/runtime-root.py
RUN python3 /build/runtime-root.py /runtime /usr/bin/rtorrent /usr/local/bin/rtorrent-entrypoint /usr/local/bin/rtorrent-public-ip
COPY tests/rtorrent-smoke.c /build/rtorrent-smoke.c
RUN cc -static -Os -Wall -Wextra -Werror -o /build/rtorrent-smoke /build/rtorrent-smoke.c
RUN cc -Os -Wall -Wextra -Werror -o /build/rtorrent-smoke /build/rtorrent-smoke.c
FROM scratch
COPY --from=runtime /runtime/ /
+22 -5
View File
@@ -1,7 +1,13 @@
# docker-rtorrent
This is plain simple rtorrent with proxy and custom rtorrent.rc
Base image: Alpine Linux 3.24 stable (`alpine:3.24`), including its latest patch updates.
Build image: official openSUSE Tumbleweed (`opensuse/tumbleweed:latest`).
The final runtime is `scratch`, containing only rTorrent and its runtime dependencies.
Both rTorrent and libtorrent come from Tumbleweed's official RPM repository;
rTorrent itself is not compiled from source. The build requires matching versions
at least 0.16.27 and records the installed package versions in the build log.
This replaces Alpine's 0.16.12 packages, which predate upstream's callback
deadlock prevention changes in 0.16.13.
Source: https://git.k2patel.in/k2patel/docker-rtorrent
@@ -27,13 +33,17 @@ are uploaded to Gitea, and no local container builds are required.
## Distroless runtime
`docker.io/k2patel/rtorrent:latest` is the only maintained image variant.
The default Dockerfile exports the Alpine 3.24 rTorrent binary, musl libraries,
CA certificates and timezone/terminal data into a `scratch` image. It contains
The default Dockerfile exports Tumbleweed's rTorrent binary, matching libtorrent,
glibc loader and libraries, CA certificates and timezone/terminal data into a
`scratch` image. Native helpers are compiled against the same Tumbleweed libraries.
The exporter resolves transitive ELF dependencies, preserves symlink targets,
and includes DNS/files NSS modules when supplied by glibc. It contains
no shell, package manager, BusyBox, or build tools. The native launcher removes
the stale session lock and execs rTorrent; it also supplies the TCP health check.
CI starts rTorrent, runs the native public-IP helper through a scheduled
`execute.capture` hook, and verifies the resulting address over SCGI before
publication.
publication. The smoke check also verifies name lookup, certificate availability,
and an SCGI request that half-closes its sending side, as Flood does.
Use the default Compose file with the existing `.env` and mounted data:
@@ -56,6 +66,13 @@ SCGI port 5001, download paths and the session volume remain supported.
Mounted `rtorrent.rc` execute hooks must use programs available in the image;
shell commands and arbitrary external utilities are unavailable.
Tumbleweed is a rolling distribution: rebuilding installs its current packaged
pair, so test the mounted configuration before replacing an existing deployment.
Recent rTorrent releases remove legacy command aliases; update old configuration
commands to their full names. In particular, use `schedule` instead of `schedule2`
for the public-IP hook below. Existing configuration and torrent/session data
remain bind-mounted and are not rewritten by the image build.
## Public-IP reporting without a shell
@@ -70,7 +87,7 @@ host file mounted at `/root/.rtorrent.rc` with the following (adjust the proxy):
```ini
method.insert = get_public_ip_address, simple|private, "execute.capture=/usr/local/bin/rtorrent-public-ip,https://ipv4.icanhazip.com,http://10.10.12.60:8118"
schedule2 = ip_tick, 0, 1800, "network.local_address.set=(get_public_ip_address)"
schedule = ip_tick, 0, 1800, "network.local_address.set=(get_public_ip_address)"
```
Remove or leave commented the old bash-based definition; do not define both.
+27 -12
View File
@@ -1,7 +1,8 @@
#!/usr/bin/env python3
"""Assemble an Alpine-derived, shell-free runtime from an explicit ELF allowlist."""
"""Assemble a Tumbleweed-derived, shell-free runtime from trusted distro ELFs."""
import os
from pathlib import Path
import re
import shutil
import subprocess
import sys
@@ -10,6 +11,7 @@ root = Path(sys.argv[1])
root.mkdir(parents=True, exist_ok=True)
copied = set()
library_directories = set()
def copy(source):
source = Path(os.path.normpath(source))
@@ -31,23 +33,30 @@ def copy(source):
else:
shutil.copy2(source, target)
for executable in sys.argv[2:]:
result = subprocess.run(['lddtreepax', '-l', executable], check=True, text=True, capture_output=True)
paths = result.stdout.splitlines()
if not paths or any(not p.startswith('/') or not Path(p).exists() for p in paths):
def copy_elf(executable):
# ldd includes transitive DT_NEEDED libraries and the ELF interpreter.
# Only invoke it on installed distro binaries and our compiled helpers.
result = subprocess.run(['ldd', executable], check=True, text=True, capture_output=True)
paths = re.findall(r'(/[^\s()]+)', result.stdout)
if 'not found' in result.stdout or not paths or any(not Path(p).exists() for p in paths):
raise SystemExit(f'Unresolved runtime dependency for {executable}: {result.stdout}')
copy(executable)
for path in paths:
copy(path)
library_directories.add(str(Path(path).parent))
# musl uses this file for nonstandard library directories (for example Lua).
for search_path in Path('/etc').glob('ld-musl-*.path'):
copy(search_path)
for executable in sys.argv[2:]:
copy_elf(executable)
for data in ['/etc/ssl/certs/ca-certificates.crt', '/etc/ssl/cert.pem',
'/etc/ssl/openssl.cnf', '/usr/lib/ossl-modules', '/usr/share/zoneinfo',
'/etc/terminfo', '/usr/share/terminfo', '/etc/services', '/etc/protocols',
'/etc/alpine-release', '/etc/os-release']:
# NSS modules can be loaded dynamically rather than appearing in DT_NEEDED.
for module in ['libnss_dns.so.2', 'libnss_files.so.2']:
for library in Path('/usr/lib64').glob(module):
copy_elf(str(library))
for data in ['/etc/ssl/ca-bundle.pem', '/etc/ssl/certs', '/etc/ssl/openssl.cnf',
'/var/lib/ca-certificates', '/usr/lib64/ossl-modules', '/usr/share/zoneinfo',
'/etc/terminfo', '/usr/share/terminfo', '/usr/share/rtorrent',
'/etc/services', '/etc/protocols', '/etc/gai.conf', '/etc/os-release']:
if Path(data).exists():
copy(data)
for directory in ['tmp', 'root', 'myconfig', 'home/nfs_download/rsession', 'server/db', 'config', 'etc']:
@@ -56,6 +65,12 @@ os.chmod(root / 'tmp', 0o1777)
(root / 'root/.rtorrent.rc').touch()
(root / 'etc/passwd').write_text('root:x:0:0:root:/root:/sbin/nologin\n')
(root / 'etc/group').write_text('root:x:0:\n')
(root / 'etc/nsswitch.conf').write_text('passwd: files\ngroup: files\nhosts: files dns\nnetworks: files\n')
# Tumbleweed may select libraries outside the loader's default directories
# (for example zlib-ng-compat). Generate a cache for the exported files only.
(root / 'etc/ld.so.conf').write_text('\n'.join(sorted(library_directories)) + '\n')
subprocess.run(['ldconfig', '-r', str(root)], check=True)
for forbidden in ['bin/sh', 'bin/bash', 'bin/busybox', 'sbin/apk', 'usr/bin/apk',
'usr/bin/zypper', 'usr/bin/rpm', 'usr/bin/gcc',
'usr/bin/npm', 'usr/local/bin/npm', 'usr/bin/python3']:
assert not (root / forbidden).exists(), f'Unexpected runtime tool: {forbidden}'
+24 -3
View File
@@ -1,6 +1,7 @@
#define _POSIX_C_SOURCE 200809L
#include <arpa/inet.h>
#include <errno.h>
#include <netdb.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
@@ -16,15 +17,34 @@ static void pause_tick(void) {
nanosleep(&delay, NULL);
}
int main(void) {
int main(int argc, char **argv) {
if (argc == 3 && strcmp(argv[1], "--resolve") == 0) {
struct addrinfo *addresses = NULL;
int error = getaddrinfo(argv[2], NULL, NULL, &addresses);
if (error) {
fprintf(stderr, "Name lookup failed for %s: %s\n", argv[2], gai_strerror(error));
return 1;
}
freeaddrinfo(addresses);
puts("Distroless name lookup passed");
return 0;
}
alarm(60); /* Fail the build if a scheduled execute hook stalls rTorrent. */
const char *forbidden[] = {"/bin/sh", "/bin/bash", "/bin/busybox", "/sbin/apk", NULL};
const char *forbidden[] = {"/bin/sh", "/bin/bash", "/bin/busybox", "/sbin/apk",
"/usr/bin/zypper", "/usr/bin/rpm", "/usr/bin/gcc", "/usr/bin/python3", NULL};
for (int i = 0; forbidden[i]; ++i) {
if (access(forbidden[i], F_OK) == 0) {
fprintf(stderr, "Unexpected runtime tool: %s\n", forbidden[i]);
return 1;
}
}
struct addrinfo *addresses = NULL;
if (getaddrinfo("localhost", NULL, NULL, &addresses) != 0) return 1;
freeaddrinfo(addresses);
if (access("/etc/ssl/ca-bundle.pem", R_OK) != 0) {
fprintf(stderr, "CA certificate bundle is missing\n");
return 1;
}
/* Exercise the production execute.capture hook without external networking. */
FILE *fixture = fopen("/tmp/rtorrent-public-ip.txt", "w");
if (!fixture) return 1;
@@ -37,7 +57,7 @@ int main(void) {
fputs("system.daemon.set = true\nnetwork.bind_address.set = 0.0.0.0\n"
"network.scgi.open_port = 127.0.0.1:5001\n"
"method.insert = get_public_ip_address, simple|private, \"execute.capture=/usr/local/bin/rtorrent-public-ip,file:///tmp/rtorrent-public-ip.txt\"\n"
"schedule2 = ip_tick, 0, 1800, \"network.local_address.set=(get_public_ip_address)\"\n", f);
"schedule = ip_tick, 0, 1800, \"network.local_address.set=(get_public_ip_address)\"\n", f);
fclose(f);
f = fopen(lock, "w");
if (!f) return 1;
@@ -83,6 +103,7 @@ int main(void) {
memcpy(request + length, body, strlen(body));
length += strlen(body);
if (send(fd, request, length, 0) != (ssize_t)length) goto cleanup;
if (shutdown(fd, SHUT_WR) != 0) goto cleanup; /* Match Flood's stream.end(request). */
size_t used = 0;
ssize_t n;
while (used < sizeof(response) - 1 && (n = recv(fd, response + used, sizeof(response) - 1 - used, 0)) > 0)