From: Lennart Poettering Date: Fri, 12 Dec 2014 16:26:31 +0000 (+0100) Subject: nspawn: when booting in ephemeral mode, append random token to machine name X-Git-Tag: v219~1023 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=commitdiff_plain;h=b9ba4dabbab8a58a044ec42655e11e65bd3ecc47 nspawn: when booting in ephemeral mode, append random token to machine name Also, when booting up an ephemeral container of / use the system hostname as default machine name. This way specifiyng -M is unnecessary when booting up an ephemeral container, while allowing any number of ephemeral containers to run from the same tree. --- diff --git a/man/systemd-nspawn.xml b/man/systemd-nspawn.xml index b66c34df0..d7d60e52c 100644 --- a/man/systemd-nspawn.xml +++ b/man/systemd-nspawn.xml @@ -231,8 +231,10 @@ its root directory (as configured with ), that is removed immediately when the - container terminates. May not be - specified together with + container terminates. This option is + only supported if the root file system + is btrfs. May not + be specified together with or . @@ -303,13 +305,22 @@ Sets the machine name for this container. This name may be - used to identify this container on the - host, and is used to initialize the - container's hostname (which the - container can choose to override, - however). If not specified, the last - component of the root directory of the - container is used. + used to identify this container during + its runtime (for example in tools like + machinectl1 + and similar), and is used to + initialize the container's hostname + (which the container can choose to + override, however). If not specified, + the last component of the root + directory path of the container is + used, possibly suffixed with a random + identifier in case + mode is + selected. If the root directory + selected is the host's root directory + the host's hostname is used as default + instead. @@ -814,13 +825,16 @@ - Boot into a <literal>btrfs</literal> snapshot of the host system + Boot into an ephemeral <literal>btrfs</literal> snapshot of the host system - # btrfs subvolume snapshot / /.tmp -# systemd-nspawn --private-network -D /.tmp -b + # systemd-nspawn -D / -xb This runs a copy of the host system in a - btrfs snapshot. + btrfs snapshot which is + removed immediately when the container + exits. All file system changes made during + runtime will be lost on shutdown, + hence. @@ -847,7 +861,8 @@ debootstrap8, pacman8, systemd.slice5, - machinectl1 + machinectl1, + btrfs8 diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c index 651a45126..9ca53cd1b 100644 --- a/src/nspawn/nspawn.c +++ b/src/nspawn/nspawn.c @@ -2875,7 +2875,11 @@ static int determine_names(void) { } if (!arg_machine) { - arg_machine = strdup(basename(arg_image ?: arg_directory)); + if (arg_directory && path_equal(arg_directory, "/")) + arg_machine = gethostname_malloc(); + else + arg_machine = strdup(basename(arg_image ?: arg_directory)); + if (!arg_machine) return log_oom(); @@ -2884,6 +2888,21 @@ static int determine_names(void) { log_error("Failed to determine machine name automatically, please use -M."); return -EINVAL; } + + if (arg_ephemeral) { + char *b; + + /* Add a random suffix when this is an + * ephemeral machine, so that we can run many + * instances at once without manually having + * to specify -M each time. */ + + if (asprintf(&b, "%s-%016" PRIx64, arg_machine, random_u64()) < 0) + return log_oom(); + + free(arg_machine); + arg_machine = b; + } } return 0;