diff options
| -rw-r--r-- | README.md | 17 | ||||
| -rw-r--r-- | bin/run-guix-system-container.c | 39 | ||||
| -rwxr-xr-x | test/run.sh | 30 |
3 files changed, 70 insertions, 16 deletions
diff --git a/README.md b/README.md index 562b0ed..5d322b5 100644 --- a/README.md +++ b/README.md @@ -153,11 +153,15 @@ error: run-guix-system-container must run as root (uid 1000): container START fails ``` -Container boot output goes to `/var/log/guix-containers`. If that -directory cannot be created or written (read-only `/var`, hardened -unit) the daemon only warns -- containers still run -- but a boot -failure then leaves no log to read. With `ProtectSystem=strict` set -`LogsDirectory=guix-containers` (or `ReadWritePaths=/var/log/guix-containers`). +Container boot output goes to `/var/log/guix-containers`, or wherever +`--log-dir DIR` (or `$GUIX_CONTAINER_LOG_DIR`) points -- the flag wins over +the variable, and the default above applies when neither is given. If that +directory cannot be created or written (read-only `/var`, hardened unit, a +daemon that is not system-wide) the daemon only warns -- containers still +run -- but a boot failure then leaves no log to read. With +`ProtectSystem=strict` either point `--log-dir` at a writable path or set +`LogsDirectory=guix-containers` (equivalently +`ReadWritePaths=/var/log/guix-containers`). ## Running it @@ -434,7 +438,8 @@ guix package -p /tmp/rootless-test/profile -i coreutils # built via the host d guix shell -C -p /tmp/rootless-test/profile -- /bin/sh -c 'id' ``` -Inside such a container you are still yourself (`uid=1000(wrk)`), there +Inside such a container you are still yourself (your own uid, e.g. +`uid=1000`), there is no shepherd, no sshd and no `guix system` services — but a stack of foreground daemons (nginx + php-fpm + octave, ports > 1024, data via `--share=...`) runs fine for fast zero-privilege smoke tests. diff --git a/bin/run-guix-system-container.c b/bin/run-guix-system-container.c index d14a702..4336458 100644 --- a/bin/run-guix-system-container.c +++ b/bin/run-guix-system-container.c @@ -81,8 +81,12 @@ #define DEFAULT_PORT 8989 #define DEFAULT_LISTEN "127.0.0.1" #define MAX_LINE 256 -/* Container stdout/stderr is appended to LOG_DIR/<name>.log. */ -#define LOG_DIR "/var/log/guix-containers" +/* Container stdout/stderr is appended to LOG_DIR/<name>.log. The + * default suits a system-wide install; --log-dir (or + * GUIX_CONTAINER_LOG_DIR) takes a directory the daemon can write, for + * hosts where /var/log is read-only or the daemon is not system-wide. */ +#define DEFAULT_LOG_DIR "/var/log/guix-containers" +static const char *log_dir = DEFAULT_LOG_DIR; /* The two capabilities container creation needs, from * <linux/capability.h> (not included to keep the build dependency-free). @@ -179,16 +183,16 @@ static void check_log_dir(void) char path[MAX_LINE]; int fd; - snprintf(path, sizeof(path), "%s/.writable", LOG_DIR); - if (mkdir(LOG_DIR, 0755) < 0 && errno != EEXIST) { + snprintf(path, sizeof(path), "%s/.writable", log_dir); + if (mkdir(log_dir, 0755) < 0 && errno != EEXIST) { logmsg("warning: cannot create %s: %s -- container boot " - "output will be lost", LOG_DIR, strerror(errno)); + "output will be lost", log_dir, strerror(errno)); return; } fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644); if (fd < 0) { logmsg("warning: cannot write to %s: %s -- container boot " - "output will be lost", LOG_DIR, strerror(errno)); + "output will be lost", log_dir, strerror(errno)); return; } close(fd); @@ -667,7 +671,7 @@ static void send_log(int fd, const char *arg) } } - snprintf(logpath, sizeof(logpath), "%s/%s.log", LOG_DIR, base); + snprintf(logpath, sizeof(logpath), "%s/%s.log", log_dir, base); log = open(logpath, O_RDONLY); if (log < 0) { dprintf(fd, "ERR no log for %s (%s)\n", base, @@ -697,7 +701,7 @@ static pid_t start_container(const char *path) /* Prepared by the parent (running as root); ignore errors — we * fall back to /dev/null below. */ -mkdir(LOG_DIR, 0755); +mkdir(log_dir, 0755); pid = fork(); if (pid < 0) @@ -710,7 +714,7 @@ mkdir(LOG_DIR, 0755); * which fingerprint) started it, so it can show it. */ setenv("GUIX_CONTAINER_FINGERPRINT", daemon_fingerprint, 1); - snprintf(logpath, sizeof(logpath), "%s/%s.log", LOG_DIR, base); + snprintf(logpath, sizeof(logpath), "%s/%s.log", log_dir, base); log = open(logpath, O_WRONLY | O_CREAT | O_APPEND, 0644); if (log >= 0) { dup2(log, STDOUT_FILENO); @@ -921,6 +925,10 @@ int main(int argc, char **argv) const char *fp_opt = NULL; int port = DEFAULT_PORT; int i; + const char *env_log_dir = getenv("GUIX_CONTAINER_LOG_DIR"); + + if (env_log_dir && *env_log_dir) + log_dir = env_log_dir; for (i = 1; i < argc; i++) { if (strcmp(argv[i], "--listen") == 0 && i + 1 < argc) { @@ -934,14 +942,20 @@ int main(int argc, char **argv) return 1; } port = p; + } else if (strcmp(argv[i], "--log-dir") == 0 && i + 1 < argc) { + log_dir = argv[++i]; } else if (strcmp(argv[i], "--fingerprint") == 0 && i + 1 < argc) { fp_opt = argv[++i]; } else { fprintf(stderr, - "usage: %s [--listen IP] [--port N] [--fingerprint WORD]\n" + "usage: %s [--listen IP] [--port N] [--log-dir DIR] " + "[--fingerprint WORD]\n" " --listen IP address to listen on (default " DEFAULT_LISTEN ")\n" " --port N port (default %d)\n" + " --log-dir DIR where container boot output goes\n" + " (default " DEFAULT_LOG_DIR ", or\n" + " $GUIX_CONTAINER_LOG_DIR)\n" " --fingerprint WORD short word or version (e.g. v0.1)\n" " identifying this daemon; containers\n" " started here get it in the\n" @@ -964,6 +978,11 @@ int main(int argc, char **argv) random_fingerprint(); } + /* Say where boot output will go before anything can fail: it is the + * first thing an operator needs when a container does not come up, and + * it makes --log-dir observable without root. */ + logmsg("container logs: %s", log_dir); + if (!have_container_privileges()) return 1; check_log_dir(); diff --git a/test/run.sh b/test/run.sh index 1316bae..915c728 100755 --- a/test/run.sh +++ b/test/run.sh @@ -16,5 +16,35 @@ if [ -n "$container" ]; then fi cc -O2 -Wall -o "$tmp/test" "$here/test.c" +cc -O2 -Wall -o "$tmp/daemon" "$here/../bin/run-guix-system-container.c" + +# The daemon's own switches: --log-dir must stay advertised (it is how a host +# with a read-only /var keeps container logs), and anything unknown must +# print the usage rather than start a half-configured daemon. +echo "daemon switches:" +if "$tmp/daemon" --help 2>&1 | grep -q -- "--log-dir"; then + echo " ok --log-dir advertised" +else + echo " FAIL --log-dir missing from --help"; exit 1 +fi +# ... and the chosen directory must actually reach the daemon: it names it at +# startup, which is observable without root. +if "$tmp/daemon" --log-dir /tmp/rgsc-test-logs --port 8994 2>&1 \ + | grep -q "container logs: /tmp/rgsc-test-logs"; then + echo " ok --log-dir reaches the daemon" +else + echo " FAIL --log-dir did not reach the daemon"; exit 1 +fi +if GUIX_CONTAINER_LOG_DIR=/tmp/rgsc-env-logs "$tmp/daemon" --port 8994 2>&1 \ + | grep -q "container logs: /tmp/rgsc-env-logs"; then + echo " ok GUIX_CONTAINER_LOG_DIR reaches the daemon" +else + echo " FAIL GUIX_CONTAINER_LOG_DIR did not reach the daemon"; exit 1 +fi +if "$tmp/daemon" --no-such-option >/dev/null 2>&1; then + echo " FAIL unknown option accepted"; exit 1 +else + echo " ok unknown option refused" +fi "$tmp/test" "$container" "$tmp" |
