daandemeyer / rpms / systemd

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