diff options
Diffstat (limited to 'bin')
| -rw-r--r-- | bin/run-guix-system-container.c | 39 |
1 files changed, 29 insertions, 10 deletions
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(); |
