From: Martin Pitt Date: Thu, 20 Nov 2014 13:30:52 +0000 (+0100) Subject: nspawn: Add try-{host,guest} journal link modes X-Git-Tag: v218~401 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=commitdiff_plain;h=574edc90066c3faeadcf4666928ed9b0ac409c75 nspawn: Add try-{host,guest} journal link modes --link-journal={host,guest} fail if the host does not have persistent journalling enabled and /var/log/journal/ does not exist. Even worse, as there is no stdout/err any more, there is no error message to point that out. Introduce two new modes "try-host" and "try-guest" which don't fail in this case, and instead just silently skip the guest journal setup. Change -j to mean "try-guest" instead of "guest", and fix the wrong --help output for it (it said "host" before). Change systemd-nspawn@.service.in to use "try-guest" so that this unit works with both persistent and non-persistent journals on the host without failing. https://bugs.debian.org/770275 --- diff --git a/man/systemd-nspawn.xml b/man/systemd-nspawn.xml index b3a2d3285..75db65eac 100644 --- a/man/systemd-nspawn.xml +++ b/man/systemd-nspawn.xml @@ -439,7 +439,9 @@ versa). Takes one of no, host, + try-host, guest, + try-guest, auto. If no, the journal is not linked. If host, @@ -453,8 +455,11 @@ guest file system (beneath /var/log/journal/machine-id) and the subdirectory is symlinked into the host - at the same location. If - auto (the default), + at the same location. try-host + and try-guest do the same + but do not fail if the host does not have + persistant journalling enabled. + If auto (the default), and the right subdirectory of /var/log/journal exists, it will be bind mounted @@ -473,7 +478,7 @@ Equivalent to - . + . diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c index c2311b36d..b4dcf39e8 100644 --- a/src/nspawn/nspawn.c +++ b/src/nspawn/nspawn.c @@ -124,6 +124,7 @@ static bool arg_private_network = false; static bool arg_read_only = false; static bool arg_boot = false; static LinkJournal arg_link_journal = LINK_AUTO; +static bool arg_link_journal_try = false; static uint64_t arg_retain = (1ULL << CAP_CHOWN) | (1ULL << CAP_DAC_OVERRIDE) | @@ -202,8 +203,9 @@ static void help(void) { " --capability=CAP In addition to the default, retain specified\n" " capability\n" " --drop-capability=CAP Drop the specified capability from the default set\n" - " --link-journal=MODE Link up guest journal, one of no, auto, guest, host\n" - " -j Equivalent to --link-journal=host\n" + " --link-journal=MODE Link up guest journal, one of no, auto, guest, host,\n" + " try-guest, try-host\n" + " -j Equivalent to --link-journal=try-guest\n" " --read-only Mount the root directory read-only\n" " --bind=PATH[:PATH] Bind mount a file or directory from the host into\n" " the container\n" @@ -428,6 +430,7 @@ static int parse_argv(int argc, char *argv[]) { case 'j': arg_link_journal = LINK_GUEST; + arg_link_journal_try = true; break; case ARG_LINK_JOURNAL: @@ -439,7 +442,13 @@ static int parse_argv(int argc, char *argv[]) { arg_link_journal = LINK_GUEST; else if (streq(optarg, "host")) arg_link_journal = LINK_HOST; - else { + else if (streq(optarg, "try-guest")) { + arg_link_journal = LINK_GUEST; + arg_link_journal_try = true; + } else if (streq(optarg, "try-host")) { + arg_link_journal = LINK_HOST; + arg_link_journal_try = true; + } else { log_error("Failed to parse link journal mode %s", optarg); return -EINVAL; } @@ -1404,8 +1413,13 @@ static int setup_journal(const char *directory) { if (arg_link_journal == LINK_GUEST) { if (symlink(q, p) < 0) { - log_error("Failed to symlink %s to %s: %m", q, p); - return -errno; + if (arg_link_journal_try) { + log_debug("Failed to symlink %s to %s, skipping journal setup: %m", q, p); + return 0; + } else { + log_error("Failed to symlink %s to %s: %m", q, p); + return -errno; + } } r = mkdir_p(q, 0755); @@ -1415,10 +1429,17 @@ static int setup_journal(const char *directory) { } if (arg_link_journal == LINK_HOST) { - r = mkdir_p(p, 0755); + /* don't create parents here -- if the host doesn't have + * permanent journal set up, don't force it here */ + r = mkdir(p, 0755); if (r < 0) { - log_error("Failed to create %s: %m", p); - return r; + if (arg_link_journal_try) { + log_debug("Failed to create %s, skipping journal setup: %m", p); + return 0; + } else { + log_error("Failed to create %s: %m", p); + return r; + } } } else if (access(p, F_OK) < 0) diff --git a/units/systemd-nspawn@.service.in b/units/systemd-nspawn@.service.in index dec2ce7df..e3eaa5323 100644 --- a/units/systemd-nspawn@.service.in +++ b/units/systemd-nspawn@.service.in @@ -10,7 +10,7 @@ Description=Container %i Documentation=man:systemd-nspawn(1) [Service] -ExecStart=@bindir@/systemd-nspawn --quiet --keep-unit --boot --link-journal=guest --directory=/var/lib/container/%i +ExecStart=@bindir@/systemd-nspawn --quiet --keep-unit --boot --link-journal=try-guest --directory=/var/lib/container/%i KillMode=mixed Type=notify RestartForceExitStatus=133