valeriyvdovin / rpms / systemd

Forked from rpms/systemd 4 years ago
Clone

Blame SOURCES/0262-machine-id-add-root-option-to-operate-on-an-alternat.patch

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