summarylogtreecommitdiffstats
path: root/0002_added_machine_id_firmware_option.patch
blob: d389ebc1de418f66d4f555667dc7393fbfb43a50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
Index: src/shared/machine-id-setup.c
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/shared/machine-id-setup.c b/src/shared/machine-id-setup.c
--- a/src/shared/machine-id-setup.c	(revision 8f4dab049074d31c31af2bb9eb76f9f4f08e3711)
+++ b/src/shared/machine-id-setup.c	(date 1714255351696)
@@ -28,7 +28,8 @@
 #include "umask-util.h"
 #include "virt.h"
 
-static int acquire_machine_id_from_credential(sd_id128_t *ret) {
+static int acquire_machine_id_from_credential(sd_id128_t *ret_machine_id, bool *ret_machine_id_from_firmware) {
+
         _cleanup_free_ char *buf = NULL;
         int r;
 
@@ -38,7 +39,12 @@
         if (r == 0) /* not found */
                 return -ENXIO;
 
-        r = sd_id128_from_string(buf, ret);
+        if (streq(buf, "firmware")) {
+                *ret_machine_id_from_firmware = true;
+                return 0;
+        }
+
+        r = sd_id128_from_string(buf, ret_machine_id);
         if (r < 0)
                 return log_warning_errno(r, "Failed to parse system.machine_id credential, ignoring: %m");
 
@@ -46,7 +52,7 @@
         return 0;
 }
 
-static int generate_machine_id(const char *root, sd_id128_t *ret) {
+static int generate_machine_id(const char *root, sd_id128_t *ret, bool machine_id_from_firmware) {
         _cleanup_close_ int fd = -EBADF;
         int r;
 
@@ -61,7 +67,7 @@
 
         if (isempty(root) && running_in_chroot() <= 0) {
                 /* Let's use a system credential for the machine ID if we can */
-                r = acquire_machine_id_from_credential(ret);
+                r = acquire_machine_id_from_credential(ret, &machine_id_from_firmware);
                 if (r >= 0)
                         return r;
 
@@ -78,14 +84,14 @@
                                 return 0;
                         }
 
-                } else if (IN_SET(detect_vm(), VIRTUALIZATION_KVM, VIRTUALIZATION_AMAZON, VIRTUALIZATION_QEMU, VIRTUALIZATION_XEN)) {
+                } else if (IN_SET(detect_vm(), VIRTUALIZATION_KVM, VIRTUALIZATION_AMAZON, VIRTUALIZATION_QEMU, VIRTUALIZATION_XEN) || machine_id_from_firmware) {
 
                         /* If we are not running in a container, see if we are running in a VM that provides
                          * a system UUID via the SMBIOS/DMI interfaces.  Such environments include QEMU/KVM
                          * with the -uuid on the qemu command line or the Amazon EC2 Nitro hypervisor. */
 
                         if (id128_get_product(ret) >= 0) {
-                                log_info("Initializing machine ID from VM UUID.");
+                                log_info("Initializing machine ID from SMBIOS/DMI UUID.");
                                 return 0;
                         }
                 }
@@ -100,12 +106,16 @@
         return 0;
 }
 
-int machine_id_setup(const char *root, bool force_transient, sd_id128_t machine_id, sd_id128_t *ret) {
+int machine_id_setup(const char *root, sd_id128_t machine_id, MachineIdSetupFlags flags, sd_id128_t *ret) {
         const char *etc_machine_id, *run_machine_id;
         _cleanup_close_ int fd = -EBADF;
         bool writable;
+        bool machine_id_from_firmware = false;
         int r;
 
+        if (flags & MACHINE_ID_SETUP_FORCE_FIRMWARE)
+                machine_id_from_firmware = true;
+
         etc_machine_id = prefix_roota(root, "/etc/machine-id");
 
         WITH_UMASK(0000) {
@@ -138,14 +148,14 @@
         }
 
         /* A we got a valid machine ID argument, that's what counts */
-        if (sd_id128_is_null(machine_id)) {
+        if (sd_id128_is_null(machine_id) || machine_id_from_firmware) {
 
                 /* Try to read any existing machine ID */
                 if (id128_read_fd(fd, ID128_FORMAT_PLAIN, ret) >= 0)
                         return 0;
 
                 /* Hmm, so, the id currently stored is not useful, then let's generate one */
-                r = generate_machine_id(root, &machine_id);
+                r = generate_machine_id(root, &machine_id, machine_id_from_firmware);
                 if (r < 0)
                         return r;
         }
@@ -161,7 +171,7 @@
                  * disk and overmount it with a transient file.
                  *
                  * Otherwise write the machine-id directly to disk. */
-                if (force_transient) {
+                if (flags & MACHINE_ID_SETUP_FORCE_TRANSIENT) {
                         r = loop_write(fd, "uninitialized\n", SIZE_MAX);
                         if (r < 0)
                                 return log_error_errno(r, "Failed to write uninitialized %s: %m", etc_machine_id);
@@ -199,7 +209,7 @@
                 return r;
         }
 
-        log_full(force_transient ? LOG_DEBUG : LOG_INFO, "Installed transient %s file.", etc_machine_id);
+        log_full(flags & MACHINE_ID_SETUP_FORCE_TRANSIENT ? LOG_DEBUG : LOG_INFO, "Installed transient %s file.", etc_machine_id);
 
         /* Mark the mount read-only */
         r = mount_follow_verbose(LOG_WARNING, NULL, etc_machine_id, NULL, MS_BIND|MS_RDONLY|MS_REMOUNT, NULL);
Index: src/core/main.c
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/core/main.c b/src/core/main.c
--- a/src/core/main.c	(revision 8f4dab049074d31c31af2bb9eb76f9f4f08e3711)
+++ b/src/core/main.c	(date 1714255351649)
@@ -144,6 +144,7 @@
 static Set* arg_syscall_archs;
 static FILE* arg_serialization;
 static sd_id128_t arg_machine_id;
+static bool arg_machine_id_from_firmware = false;
 static EmergencyAction arg_cad_burst_action;
 static CPUSet arg_cpu_affinity;
 static NUMAPolicy arg_numa_policy;
@@ -352,10 +353,15 @@
                 if (proc_cmdline_value_missing(key, value))
                         return 0;
 
-                r = id128_from_string_nonzero(value, &arg_machine_id);
-                if (r < 0)
-                        log_warning_errno(r, "MachineID '%s' is not valid, ignoring: %m", value);
-
+                if (streq(value, "firmware"))
+                        arg_machine_id_from_firmware = true;
+                else {
+                        r = id128_from_string_nonzero(value, &arg_machine_id);
+                        if (r < 0)
+                                log_warning_errno(r, "MachineID '%s' is not valid, ignoring: %m", value);
+                        else
+                                arg_machine_id_from_firmware = false;
+                }
         } else if (proc_cmdline_key_streq(key, "systemd.default_timeout_start_sec")) {
 
                 if (proc_cmdline_value_missing(key, value))
@@ -2234,8 +2240,13 @@
 
                         (void) os_release_status();
                         (void) hostname_setup(true);
-                        /* Force transient machine-id on first boot. */
-                        machine_id_setup(/* root= */ NULL, /* force_transient= */ first_boot, arg_machine_id, /* ret_machine_id */ NULL);
+
+                        /* Set up the machine ID flags */
+                        MachineIdSetupFlags machine_id_setup_flags = 0;
+                        machine_id_setup_flags = (first_boot ? MACHINE_ID_SETUP_FORCE_TRANSIENT : 0) |
+                                        (arg_machine_id_from_firmware ? MACHINE_ID_SETUP_FORCE_FIRMWARE : 0);
+
+                        machine_id_setup(/* root= */ NULL, arg_machine_id, machine_id_setup_flags, /* ret_machine_id */ NULL);
                         (void) loopback_setup();
                         bump_unix_max_dgram_qlen();
                         bump_file_max_and_nr_open();
Index: man/machine-id.xml
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/man/machine-id.xml b/man/machine-id.xml
--- a/man/machine-id.xml	(revision 8f4dab049074d31c31af2bb9eb76f9f4f08e3711)
+++ b/man/machine-id.xml	(date 1714255351569)
@@ -102,7 +102,8 @@
     value of the kernel command line option <varname>container_uuid</varname>, the KVM DMI
     <filename>product_uuid</filename> or the devicetree <filename>vm,uuid</filename>
     (on KVM systems), the Xen hypervisor <filename>uuid</filename>, and finally a randomly
-    generated UUID.</para>
+    generated UUID. <varname>systemd.machine_id=firmware</varname> can be set to generate the machine id
+    from the firmware.</para>
 
     <para>After the machine ID is established,
     <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>
Index: src/machine-id-setup/machine-id-setup-main.c
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/machine-id-setup/machine-id-setup-main.c b/src/machine-id-setup/machine-id-setup-main.c
--- a/src/machine-id-setup/machine-id-setup-main.c	(revision 8f4dab049074d31c31af2bb9eb76f9f4f08e3711)
+++ b/src/machine-id-setup/machine-id-setup-main.c	(date 1714255351673)
@@ -188,7 +188,7 @@
         } else {
                 sd_id128_t id;
 
-                r = machine_id_setup(arg_root, false, SD_ID128_NULL, &id);
+                r = machine_id_setup(arg_root, SD_ID128_NULL, 0, &id);
                 if (r < 0)
                         return r;
 
Index: src/shared/machine-id-setup.h
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/shared/machine-id-setup.h b/src/shared/machine-id-setup.h
--- a/src/shared/machine-id-setup.h	(revision 8f4dab049074d31c31af2bb9eb76f9f4f08e3711)
+++ b/src/shared/machine-id-setup.h	(date 1714255351713)
@@ -3,5 +3,10 @@
 
 #include <stdbool.h>
 
+typedef enum MachineIdSetupFlags {
+        MACHINE_ID_SETUP_FORCE_TRANSIENT = 1 << 0,
+        MACHINE_ID_SETUP_FORCE_FIRMWARE  = 1 << 1,
+} MachineIdSetupFlags;
+
 int machine_id_commit(const char *root);
-int machine_id_setup(const char *root, bool force_transient, sd_id128_t requested, sd_id128_t *ret);
+int machine_id_setup(const char *root, sd_id128_t machine_id, MachineIdSetupFlags flags, sd_id128_t *ret);