84b277
From 99c06d718c91c91d0992869777c15b457f95443a Mon Sep 17 00:00:00 2001
84b277
From: Greg KH <gregkh@linuxfoundation.org>
84b277
Date: Fri, 14 Mar 2014 04:43:04 +0000
84b277
Subject: [PATCH] machine-id: add --root option to operate on an alternate fs
84b277
 tree
84b277
84b277
This makes it possible to initialize the /etc/machine-id file on an
84b277
arbitrary filesystem hierarchy.  This helps systems that wish to run
84b277
this at image creation time in a subdirectory, or from initramfs before
84b277
pivot-root is called.
84b277
84b277
[tomegun: converted to using _cleanup_free_ macros]
84b277
84b277
Conflicts:
84b277
	man/systemd-machine-id-setup.xml
84b277
	src/machine-id-setup/machine-id-setup-main.c
84b277
84b277
(cherry picked from commit 92f2f92edcad46ce4098ee26504edca0a1dad68e)
84b277
84b277
Related: #1111199
84b277
---
84b277
 man/systemd-machine-id-setup.xml             | 23 +++++++--------
84b277
 src/core/machine-id-setup.c                  | 44 ++++++++++++++++++----------
84b277
 src/core/machine-id-setup.h                  |  2 +-
84b277
 src/core/main.c                              |  2 +-
84b277
 src/machine-id-setup/machine-id-setup-main.c | 17 ++++++++---
84b277
 5 files changed, 53 insertions(+), 35 deletions(-)
84b277
84b277
diff --git a/man/systemd-machine-id-setup.xml b/man/systemd-machine-id-setup.xml
84b277
index 7b3aa7e..b879b40 100644
84b277
--- a/man/systemd-machine-id-setup.xml
84b277
+++ b/man/systemd-machine-id-setup.xml
84b277
@@ -21,7 +21,8 @@
84b277
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
84b277
 -->
84b277
 
84b277
-<refentry id="systemd-machine-id-setup">
84b277
+
84b277
+          xmlns:xi="http://www.w3.org/2001/XInclude">
84b277
 
84b277
         <refentryinfo>
84b277
                 <title>systemd-machine-id-setup</title>
84b277
@@ -96,19 +97,15 @@
84b277
 
84b277
                 <variablelist>
84b277
                         <varlistentry>
84b277
-                                <term><option>-h</option></term>
84b277
-                                <term><option>--help</option></term>
84b277
-
84b277
-                                <listitem><para>Prints a short help
84b277
-                                text and exits.</para></listitem>
84b277
-                        </varlistentry>
84b277
-
84b277
-                        <varlistentry>
84b277
-                                <term><option>--version</option></term>
84b277
-
84b277
-                                <listitem><para>Prints a short version
84b277
-                                string and exits.</para></listitem>
84b277
+                                <term><option>--root=ROOT</option></term>
84b277
+                                <listitem><para>Takes a directory path
84b277
+                                as an argument. All paths will be
84b277
+                                prefixed with the given alternate ROOT
84b277
+                                path, including config search paths.
84b277
+                                </para></listitem>
84b277
                         </varlistentry>
84b277
+                        <xi:include href="standard-options.xml" xpointer="help" />
84b277
+                        <xi:include href="standard-options.xml" xpointer="version" />
84b277
                 </variablelist>
84b277
 
84b277
         </refsect1>
84b277
diff --git a/src/core/machine-id-setup.c b/src/core/machine-id-setup.c
84b277
index 18e015f..291930e 100644
84b277
--- a/src/core/machine-id-setup.c
84b277
+++ b/src/core/machine-id-setup.c
84b277
@@ -59,18 +59,22 @@ static int shorten_uuid(char destination[36], const char *source) {
84b277
         return -EINVAL;
84b277
 }
84b277
 
84b277
-static int generate(char id[34]) {
84b277
+static int generate(char id[34], const char *root) {
84b277
         int fd, r;
84b277
         unsigned char *p;
84b277
         sd_id128_t buf;
84b277
         char *q;
84b277
         ssize_t k;
84b277
         const char *vm_id;
84b277
+        _cleanup_free_ char *dbus_machine_id = NULL;
84b277
 
84b277
         assert(id);
84b277
 
84b277
+        if (asprintf(&dbus_machine_id, "%s/var/lib/dbus/machine-id", root) < 0)
84b277
+                return log_oom();
84b277
+
84b277
         /* First, try reading the D-Bus machine id, unless it is a symlink */
84b277
-        fd = open("/var/lib/dbus/machine-id", O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
84b277
+        fd = open(dbus_machine_id, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
84b277
         if (fd >= 0) {
84b277
                 k = loop_read(fd, id, 33, false);
84b277
                 close_nointr_nofail(fd);
84b277
@@ -150,12 +154,20 @@ static int generate(char id[34]) {
84b277
         return 0;
84b277
 }
84b277
 
84b277
-int machine_id_setup(void) {
84b277
+int machine_id_setup(const char *root) {
84b277
         _cleanup_close_ int fd = -1;
84b277
         int r;
84b277
         bool writable;
84b277
         struct stat st;
84b277
         char id[34]; /* 32 + \n + \0 */
84b277
+        _cleanup_free_ char *etc_machine_id = NULL;
84b277
+        _cleanup_free_ char *run_machine_id = NULL;
84b277
+
84b277
+        if (asprintf(&etc_machine_id, "%s/etc/machine-id", root) < 0)
84b277
+                return log_oom();
84b277
+
84b277
+        if (asprintf(&run_machine_id, "%s/run/machine-id", root) < 0)
84b277
+                return log_oom();
84b277
 
84b277
         RUN_WITH_UMASK(0000) {
84b277
                 /* We create this 0444, to indicate that this isn't really
84b277
@@ -163,13 +175,13 @@ int machine_id_setup(void) {
84b277
                  * will be owned by root it doesn't matter much, but maybe
84b277
                  * people look. */
84b277
 
84b277
-                fd = open("/etc/machine-id", O_RDWR|O_CREAT|O_CLOEXEC|O_NOCTTY, 0444);
84b277
+                fd = open(etc_machine_id, O_RDWR|O_CREAT|O_CLOEXEC|O_NOCTTY, 0444);
84b277
                 if (fd >= 0)
84b277
                         writable = true;
84b277
                 else {
84b277
-                        fd = open("/etc/machine-id", O_RDONLY|O_CLOEXEC|O_NOCTTY);
84b277
+                        fd = open(etc_machine_id, O_RDONLY|O_CLOEXEC|O_NOCTTY);
84b277
                         if (fd < 0) {
84b277
-                                log_error("Cannot open /etc/machine-id: %m");
84b277
+                                log_error("Cannot open %s: %m", etc_machine_id);
84b277
                                 return -errno;
84b277
                         }
84b277
 
84b277
@@ -193,7 +205,7 @@ int machine_id_setup(void) {
84b277
         /* Hmm, so, the id currently stored is not useful, then let's
84b277
          * generate one */
84b277
 
84b277
-        r = generate(id);
84b277
+        r = generate(id, root);
84b277
         if (r < 0)
84b277
                 return r;
84b277
 
84b277
@@ -211,27 +223,27 @@ int machine_id_setup(void) {
84b277
          * /run/machine-id as a replacement */
84b277
 
84b277
         RUN_WITH_UMASK(0022) {
84b277
-                r = write_string_file("/run/machine-id", id);
84b277
+                r = write_string_file(run_machine_id, id);
84b277
         }
84b277
         if (r < 0) {
84b277
-                log_error("Cannot write /run/machine-id: %s", strerror(-r));
84b277
-                unlink("/run/machine-id");
84b277
+                log_error("Cannot write %s: %s", run_machine_id, strerror(-r));
84b277
+                unlink(run_machine_id);
84b277
                 return r;
84b277
         }
84b277
 
84b277
         /* And now, let's mount it over */
84b277
-        r = mount("/run/machine-id", "/etc/machine-id", NULL, MS_BIND, NULL);
84b277
+        r = mount(run_machine_id, etc_machine_id, NULL, MS_BIND, NULL);
84b277
         if (r < 0) {
84b277
-                log_error("Failed to mount /etc/machine-id: %m");
84b277
-                unlink_noerrno("/run/machine-id");
84b277
+                log_error("Failed to mount %s: %m", etc_machine_id);
84b277
+                unlink_noerrno(run_machine_id);
84b277
                 return -errno;
84b277
         }
84b277
 
84b277
-        log_info("Installed transient /etc/machine-id file.");
84b277
+        log_info("Installed transient %s file.", etc_machine_id);
84b277
 
84b277
         /* Mark the mount read-only */
84b277
-        if (mount(NULL, "/etc/machine-id", NULL, MS_BIND|MS_RDONLY|MS_REMOUNT, NULL) < 0)
84b277
-                log_warning("Failed to make transient /etc/machine-id read-only: %m");
84b277
+        if (mount(NULL, etc_machine_id, NULL, MS_BIND|MS_RDONLY|MS_REMOUNT, NULL) < 0)
84b277
+                log_warning("Failed to make transient %s read-only: %m", etc_machine_id);
84b277
 
84b277
         return 0;
84b277
 }
84b277
diff --git a/src/core/machine-id-setup.h b/src/core/machine-id-setup.h
84b277
index b9e6b4d..b0583ee 100644
84b277
--- a/src/core/machine-id-setup.h
84b277
+++ b/src/core/machine-id-setup.h
84b277
@@ -21,4 +21,4 @@
84b277
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
84b277
 ***/
84b277
 
84b277
-int machine_id_setup(void);
84b277
+int machine_id_setup(const char *root);
84b277
diff --git a/src/core/main.c b/src/core/main.c
84b277
index 00fd394..afd5f6f 100644
84b277
--- a/src/core/main.c
84b277
+++ b/src/core/main.c
84b277
@@ -1503,7 +1503,7 @@ int main(int argc, char *argv[]) {
84b277
                 kmod_setup();
84b277
 #endif
84b277
                 hostname_setup();
84b277
-                machine_id_setup();
84b277
+                machine_id_setup("");
84b277
                 loopback_setup();
84b277
 
84b277
                 test_mtab();
84b277
diff --git a/src/machine-id-setup/machine-id-setup-main.c b/src/machine-id-setup/machine-id-setup-main.c
84b277
index eb2d514..f4dc705 100644
84b277
--- a/src/machine-id-setup/machine-id-setup-main.c
84b277
+++ b/src/machine-id-setup/machine-id-setup-main.c
84b277
@@ -29,12 +29,15 @@
84b277
 #include "log.h"
84b277
 #include "build.h"
84b277
 
84b277
+static const char *arg_root = "";
84b277
+
84b277
 static int help(void) {
84b277
 
84b277
         printf("%s [OPTIONS...]\n\n"
84b277
                "Initialize /etc/machine-id from a random source.\n\n"
84b277
                "  -h --help             Show this help\n"
84b277
-               "     --version          Show package version\n",
84b277
+               "     --version          Show package version\n"
84b277
+               "     --root             Filesystem root\n",
84b277
                program_invocation_short_name);
84b277
 
84b277
         return 0;
84b277
@@ -43,13 +46,15 @@ static int help(void) {
84b277
 static int parse_argv(int argc, char *argv[]) {
84b277
 
84b277
         enum {
84b277
-                ARG_VERSION = 0x100
84b277
+                ARG_VERSION = 0x100,
84b277
+                ARG_ROOT,
84b277
         };
84b277
 
84b277
         static const struct option options[] = {
84b277
                 { "help",      no_argument,       NULL, 'h'           },
84b277
                 { "version",   no_argument,       NULL, ARG_VERSION   },
84b277
-                { NULL,        0,                 NULL, 0             }
84b277
+                { "root",      required_argument, NULL, ARG_ROOT      },
84b277
+                {}
84b277
         };
84b277
 
84b277
         int c;
84b277
@@ -70,6 +75,10 @@ static int parse_argv(int argc, char *argv[]) {
84b277
                         puts(SYSTEMD_FEATURES);
84b277
                         return 0;
84b277
 
84b277
+                case ARG_ROOT:
84b277
+                        arg_root = optarg;
84b277
+                        break;
84b277
+
84b277
                 case '?':
84b277
                         return -EINVAL;
84b277
 
84b277
@@ -97,5 +106,5 @@ int main(int argc, char *argv[]) {
84b277
         if (r <= 0)
84b277
                 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
84b277
 
84b277
-        return machine_id_setup() < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
84b277
+        return machine_id_setup(arg_root) < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
84b277
 }