]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core: Add machine-id setting 490/head
authorNils Carlson <pyssling@ludd.ltu.se>
Sun, 5 Jul 2015 22:00:59 +0000 (00:00 +0200)
committerNils Carlson <pyssling@ludd.ltu.se>
Tue, 12 Jan 2016 22:10:41 +0000 (22:10 +0000)
Allow for overriding all other machine-ids which may be present on
the system using a kernel command line systemd.machine_id or
--machine-id= option.

This is especially useful for network booted systems where the
machine-id needs to be static, or for containers where a specific
machine-id is wanted.

man/kernel-command-line.xml
man/machine-id.xml
man/systemd.xml
src/core/machine-id-setup.c
src/core/machine-id-setup.h
src/core/main.c
src/machine-id-setup/machine-id-setup-main.c

index 309220632e3c5877b98c2c9890fbaae8b38d1b1e..42d5e006bb77736a91572ff6625cb88621b4a205 100644 (file)
@@ -91,6 +91,7 @@
         <term><varname>systemd.default_standard_output=</varname></term>
         <term><varname>systemd.default_standard_error=</varname></term>
         <term><varname>systemd.setenv=</varname></term>
+        <term><varname>systemd.machine_id=</varname></term>
         <listitem>
           <para>Parameters understood by the system and service
           manager to control system behavior. For details, see
index db72c2a01cad8a879276bb4afdd090e67d820a9c..d318ec54eca78335b06e5a1cc582866db7b2508b 100644 (file)
     at install time. Use
     <citerefentry><refentrytitle>systemd-firstboot</refentrytitle><manvolnum>1</manvolnum></citerefentry>
     to initialize it on mounted (but not booted) system images.</para>
+
+    <para>The machine-id may also be set, for example when network
+    booting, by setting the <varname>systemd.machine_id=</varname>
+    kernel command line parameter or passing the option
+    <option>--machine-id=</option> to systemd. A machine-id may not
+    be set to all zeros.</para>
   </refsect1>
 
   <refsect1>
index 6de18f8294c6db374a645ba90269bd0b8fcffcc9..367972099b687b6fd1e46c12845bf0e63ddd0da9 100644 (file)
         <option>inherit</option>.</para></listitem>
       </varlistentry>
 
+      <varlistentry>
+        <term><option>--machine-id=</option></term>
+
+        <listitem><para>Override the machine-id set on the hard drive,
+        userful for network booting or for containers. May not be set
+        to all zeros.</para></listitem>
+      </varlistentry>
+
       <xi:include href="standard-options.xml" xpointer="help" />
       <xi:include href="standard-options.xml" xpointer="version" />
     </variablelist>
         than once to set multiple variables.</para></listitem>
       </varlistentry>
 
+      <varlistentry>
+        <term><varname>systemd.machine_id=</varname></term>
+
+        <listitem><para>Takes a 32 character hex value to be
+        used for setting the machine-id. Intended mostly for
+        network booting where the same machine-id is desired
+        for every boot.</para></listitem>
+      </varlistentry>
+
       <varlistentry>
         <term><varname>quiet</varname></term>
 
index 145ba2a28df62f98bf6c6334d419b746b64866e7..09b0449c80965763e014cb52780a60a5ebfbe04e 100644 (file)
@@ -198,7 +198,7 @@ static int generate_machine_id(char id[34], const char *root) {
         return 0;
 }
 
-int machine_id_setup(const char *root) {
+int machine_id_setup(const char *root, sd_id128_t machine_id) {
         const char *etc_machine_id, *run_machine_id;
         _cleanup_close_ int fd = -1;
         bool writable = true;
@@ -248,15 +248,22 @@ int machine_id_setup(const char *root) {
                 }
         }
 
-        if (read_machine_id(fd, id) >= 0)
-                return 0;
+        /* A machine id argument overrides all other machined-ids */
+        if (!sd_id128_is_null(machine_id)) {
+                sd_id128_to_string(machine_id, id);
+                id[32] = '\n';
+                id[33] = 0;
+        } else {
+                if (read_machine_id(fd, id) >= 0)
+                        return 0;
 
-        /* Hmm, so, the id currently stored is not useful, then let's
-         * generate one */
+                /* Hmm, so, the id currently stored is not useful, then let's
+                 * generate one */
 
-        r = generate_machine_id(id, root);
-        if (r < 0)
-                return r;
+                r = generate_machine_id(id, root);
+                if (r < 0)
+                        return r;
+        }
 
         if (writable)
                 if (write_machine_id(fd, id) >= 0)
index f7707c3bf9f6d6d2959e0af88d222b289d37854c..a2168a8d4ae5ad2b95b6fe8c6e4942ef810df49a 100644 (file)
@@ -22,4 +22,4 @@
 ***/
 
 int machine_id_commit(const char *root);
-int machine_id_setup(const char *root);
+int machine_id_setup(const char *root, sd_id128_t machine_id);
index f9de54028e017daa6e8907b5354d19ec340e682b..e415d1c5b80bda49d2da529fa304bbe1dfb906f3 100644 (file)
@@ -127,6 +127,7 @@ static bool arg_default_blockio_accounting = false;
 static bool arg_default_memory_accounting = false;
 static bool arg_default_tasks_accounting = true;
 static uint64_t arg_default_tasks_max = UINT64_C(512);
+static sd_id128_t arg_machine_id = {};
 
 static void pager_open_if_enabled(void) {
 
@@ -300,6 +301,17 @@ static int parse_crash_chvt(const char *value) {
         return 0;
 }
 
+static int set_machine_id(const char *m) {
+
+        if (sd_id128_from_string(m, &arg_machine_id) < 0)
+                return -EINVAL;
+
+        if (sd_id128_is_null(arg_machine_id))
+                return -EINVAL;
+
+        return 0;
+}
+
 static int parse_proc_cmdline_item(const char *key, const char *value) {
 
         int r;
@@ -388,6 +400,12 @@ static int parse_proc_cmdline_item(const char *key, const char *value) {
                 } else
                         log_warning("Environment variable name '%s' is not valid. Ignoring.", value);
 
+        } else if (streq(key, "systemd.machine_id") && value) {
+
+               r = set_machine_id(value);
+               if (r < 0)
+                       log_warning("MachineID '%s' is not valid. Ignoring.", value);
+
         } else if (streq(key, "quiet") && !value) {
 
                 if (arg_show_status == _SHOW_STATUS_UNSET)
@@ -743,7 +761,8 @@ static int parse_argv(int argc, char *argv[]) {
                 ARG_DESERIALIZE,
                 ARG_SWITCHED_ROOT,
                 ARG_DEFAULT_STD_OUTPUT,
-                ARG_DEFAULT_STD_ERROR
+                ARG_DEFAULT_STD_ERROR,
+                ARG_MACHINE_ID
         };
 
         static const struct option options[] = {
@@ -769,6 +788,7 @@ static int parse_argv(int argc, char *argv[]) {
                 { "switched-root",            no_argument,       NULL, ARG_SWITCHED_ROOT            },
                 { "default-standard-output",  required_argument, NULL, ARG_DEFAULT_STD_OUTPUT,      },
                 { "default-standard-error",   required_argument, NULL, ARG_DEFAULT_STD_ERROR,       },
+                { "machine-id",               required_argument, NULL, ARG_MACHINE_ID               },
                 {}
         };
 
@@ -964,6 +984,14 @@ static int parse_argv(int argc, char *argv[]) {
                         arg_switched_root = true;
                         break;
 
+                case ARG_MACHINE_ID:
+                        r = set_machine_id(optarg);
+                        if (r < 0) {
+                                log_error("MachineID '%s' is not valid.", optarg);
+                                return r;
+                        }
+                        break;
+
                 case 'h':
                         arg_action = ACTION_HELP;
                         if (arg_no_pager < 0)
@@ -1617,7 +1645,7 @@ int main(int argc, char *argv[]) {
                         status_welcome();
 
                 hostname_setup();
-                machine_id_setup(NULL);
+                machine_id_setup(NULL, arg_machine_id);
                 loopback_setup();
                 bump_unix_max_dgram_qlen();
 
index d805bcfdca7d837d9515d5b400400377da5f4be6..9d19307236bf03fa9e2d3d52b5b573a67ce40a88 100644 (file)
@@ -112,7 +112,7 @@ int main(int argc, char *argv[]) {
         if (arg_commit)
                 r = machine_id_commit(arg_root);
         else
-                r = machine_id_setup(arg_root);
+                r = machine_id_setup(arg_root, SD_ID128_NULL);
 
 finish:
         free(arg_root);