]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/machine-id-setup.c
license: LGPL-2.1+ -> LGPL-2.1-or-later
[thirdparty/systemd.git] / src / core / machine-id-setup.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
d7ccca2e 2
d7ccca2e 3#include <fcntl.h>
618234a5 4#include <sched.h>
d7ccca2e 5#include <sys/mount.h>
618234a5 6#include <unistd.h>
d7ccca2e 7
618234a5 8#include "sd-id128.h"
81527be1 9
b5efdb8a 10#include "alloc-util.h"
3ffd4af2 11#include "fd-util.h"
f4f15635 12#include "fs-util.h"
910fd145 13#include "id128-util.h"
3023f2fe 14#include "io-util.h"
618234a5 15#include "log.h"
3ffd4af2 16#include "machine-id-setup.h"
d7ccca2e 17#include "macro.h"
49e942b2 18#include "mkdir.h"
21935150 19#include "mount-util.h"
049af8ad 20#include "mountpoint-util.h"
0cb8e3d1 21#include "namespace-util.h"
fe970a8a 22#include "path-util.h"
0b452006 23#include "process-util.h"
8fcde012 24#include "stat-util.h"
07630cea 25#include "string-util.h"
affb60b1 26#include "umask-util.h"
618234a5
LP
27#include "util.h"
28#include "virt.h"
8d41a963 29
4b1afed0 30static int generate_machine_id(const char *root, sd_id128_t *ret) {
75f86906 31 const char *dbus_machine_id;
4b1afed0
LP
32 _cleanup_close_ int fd = -1;
33 int r;
d7ccca2e 34
4b1afed0 35 assert(ret);
92f2f92e 36
d7ccca2e 37 /* First, try reading the D-Bus machine id, unless it is a symlink */
4b1afed0 38 dbus_machine_id = prefix_roota(root, "/var/lib/dbus/machine-id");
92f2f92e 39 fd = open(dbus_machine_id, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
8d41a963 40 if (fd >= 0) {
4b1afed0 41 if (id128_read_fd(fd, ID128_PLAIN, ret) >= 0) {
c6ac7e4b
LP
42 log_info("Initializing machine ID from D-Bus machine ID.");
43 return 0;
d7ccca2e 44 }
4b1afed0
LP
45
46 fd = safe_close(fd);
d7ccca2e
LP
47 }
48
1f894e68 49 if (isempty(root) && running_in_chroot() <= 0) {
5dd6d0f8
LP
50 /* If that didn't work, see if we are running in a container,
51 * and a machine ID was passed in via $container_uuid the way
52 * libvirt/LXC does it */
75f86906
LP
53
54 if (detect_container() > 0) {
5dd6d0f8 55 _cleanup_free_ char *e = NULL;
0b36bbc4 56
4b1afed0
LP
57 if (getenv_for_pid(1, "container_uuid", &e) > 0 &&
58 sd_id128_from_string(e, ret) >= 0) {
59 log_info("Initializing machine ID from container UUID.");
60 return 0;
0b36bbc4 61 }
5dd6d0f8 62
75f86906
LP
63 } else if (detect_vm() == VIRTUALIZATION_KVM) {
64
5dd6d0f8
LP
65 /* If we are not running in a container, see if we are
66 * running in qemu/kvm and a machine ID was passed in
67 * via -uuid on the qemu/kvm command line */
68
4b1afed0
LP
69 if (id128_read("/sys/class/dmi/id/product_uuid", ID128_UUID, ret) >= 0) {
70 log_info("Initializing machine ID from KVM UUID.");
71 return 0;
5dd6d0f8 72 }
8fa0de65
DJL
73 /* on POWER, it's exported here instead */
74 if (id128_read("/sys/firmware/devicetree/base/vm,uuid", ID128_UUID, ret) >= 0) {
75 log_info("Initializing machine ID from KVM UUID.");
76 return 0;
77 }
0b36bbc4 78 }
d4eb120a
LP
79 }
80
d7ccca2e 81 /* If that didn't work, generate a random machine id */
4b1afed0 82 r = sd_id128_randomize(ret);
23bbb0de 83 if (r < 0)
5e1ee764 84 return log_error_errno(r, "Failed to generate randomized machine ID: %m");
d7ccca2e
LP
85
86 log_info("Initializing machine ID from random generator.");
d7ccca2e
LP
87 return 0;
88}
89
3023f2fe 90int machine_id_setup(const char *root, bool force_transient, sd_id128_t machine_id, sd_id128_t *ret) {
c6ac7e4b
LP
91 const char *etc_machine_id, *run_machine_id;
92 _cleanup_close_ int fd = -1;
4b1afed0 93 bool writable;
c6ac7e4b 94 int r;
9496e375 95
f5e754e0 96 etc_machine_id = prefix_roota(root, "/etc/machine-id");
9496e375 97
c6ac7e4b
LP
98 RUN_WITH_UMASK(0000) {
99 /* We create this 0444, to indicate that this isn't really
100 * something you should ever modify. Of course, since the file
101 * will be owned by root it doesn't matter much, but maybe
102 * people look. */
9496e375 103
4b1afed0 104 (void) mkdir_parents(etc_machine_id, 0755);
c6ac7e4b
LP
105 fd = open(etc_machine_id, O_RDWR|O_CREAT|O_CLOEXEC|O_NOCTTY, 0444);
106 if (fd < 0) {
107 int old_errno = errno;
108
109 fd = open(etc_machine_id, O_RDONLY|O_CLOEXEC|O_NOCTTY);
110 if (fd < 0) {
111 if (old_errno == EROFS && errno == ENOENT)
d49a7143 112 return log_error_errno(errno,
c6ac7e4b
LP
113 "System cannot boot: Missing /etc/machine-id and /etc is mounted read-only.\n"
114 "Booting up is supported only when:\n"
115 "1) /etc/machine-id exists and is populated.\n"
116 "2) /etc/machine-id exists and is empty.\n"
117 "3) /etc/machine-id is missing and /etc is writable.\n");
118 else
5e1ee764 119 return log_error_errno(errno, "Cannot open %s: %m", etc_machine_id);
c6ac7e4b 120 }
9496e375 121
c6ac7e4b 122 writable = false;
4b1afed0
LP
123 } else
124 writable = true;
c6ac7e4b
LP
125 }
126
4b1afed0
LP
127 /* A we got a valid machine ID argument, that's what counts */
128 if (sd_id128_is_null(machine_id)) {
9496e375 129
4b1afed0
LP
130 /* Try to read any existing machine ID */
131 if (id128_read_fd(fd, ID128_PLAIN, ret) >= 0)
132 return 0;
c6ac7e4b 133
4b1afed0
LP
134 /* Hmm, so, the id currently stored is not useful, then let's generate one */
135 r = generate_machine_id(root, &machine_id);
ee48dbd5
NC
136 if (r < 0)
137 return r;
fcb24270 138 }
4b1afed0 139
fcb24270 140 if (writable) {
4b1afed0 141 if (lseek(fd, 0, SEEK_SET) == (off_t) -1)
fcb24270
EV
142 return log_error_errno(errno, "Failed to seek %s: %m", etc_machine_id);
143
144 if (ftruncate(fd, 0) < 0)
145 return log_error_errno(errno, "Failed to truncate %s: %m", etc_machine_id);
c6ac7e4b 146
3023f2fe
HS
147 /* If the caller requested a transient machine-id, write the string "uninitialized\n" to
148 * disk and overmount it with a transient file.
149 *
150 * Otherwise write the machine-id directly to disk. */
151 if (force_transient) {
152 r = loop_write(fd, "uninitialized\n", strlen("uninitialized\n"), false);
153 if (r < 0)
154 return log_error_errno(r, "Failed to write uninitialized %s: %m", etc_machine_id);
155
156 r = fsync_full(fd);
157 if (r < 0)
158 return log_error_errno(r, "Failed to sync %s: %m", etc_machine_id);
159 } else {
160 r = id128_write_fd(fd, ID128_PLAIN, machine_id, true);
161 if (r < 0)
162 return log_error_errno(r, "Failed to write %s: %m", etc_machine_id);
163 else
164 goto finish;
165 }
fcb24270 166 }
c6ac7e4b
LP
167
168 fd = safe_close(fd);
169
3023f2fe
HS
170 /* Hmm, we couldn't or shouldn't write the machine-id to /etc?
171 * So let's write it to /run/machine-id as a replacement */
c6ac7e4b 172
4b1afed0
LP
173 run_machine_id = prefix_roota(root, "/run/machine-id");
174
175 RUN_WITH_UMASK(0022)
176 r = id128_write(run_machine_id, ID128_PLAIN, machine_id, false);
177 if (r < 0) {
178 (void) unlink(run_machine_id);
179 return log_error_errno(r, "Cannot write %s: %m", run_machine_id);
c6ac7e4b
LP
180 }
181
182 /* And now, let's mount it over */
21935150
LP
183 r = mount_follow_verbose(LOG_ERR, run_machine_id, etc_machine_id, NULL, MS_BIND, NULL);
184 if (r < 0) {
185 (void) unlink(run_machine_id);
186 return r;
c6ac7e4b
LP
187 }
188
3023f2fe 189 log_full(force_transient ? LOG_DEBUG : LOG_INFO, "Installed transient %s file.", etc_machine_id);
c6ac7e4b
LP
190
191 /* Mark the mount read-only */
21935150
LP
192 r = mount_follow_verbose(LOG_WARNING, NULL, etc_machine_id, NULL, MS_BIND|MS_RDONLY|MS_REMOUNT, NULL);
193 if (r < 0)
194 return r;
4b1afed0
LP
195
196finish:
197 if (ret)
198 *ret = machine_id;
c6ac7e4b
LP
199
200 return 0;
9496e375
DR
201}
202
979ef53a
DR
203int machine_id_commit(const char *root) {
204 _cleanup_close_ int fd = -1, initial_mntns_fd = -1;
c261a5d0 205 const char *etc_machine_id, *sync_path;
15b1248a 206 sd_id128_t id;
979ef53a
DR
207 int r;
208
c261a5d0
HS
209 /* Before doing anything, sync everything to ensure any changes by first-boot units are persisted.
210 *
211 * First, explicitly sync the file systems we care about and check if it worked. */
212 FOREACH_STRING(sync_path, "/etc/", "/var/") {
213 r = syncfs_path(AT_FDCWD, sync_path);
214 if (r < 0)
215 return log_error_errno(r, "Cannot sync %s: %m", sync_path);
216 }
217
218 /* Afterwards, sync() the rest too, but we can't check the return value for these. */
219 sync();
220
15b1248a
LP
221 /* Replaces a tmpfs bind mount of /etc/machine-id by a proper file, atomically. For this, the umount is removed
222 * in a mount namespace, a new file is created at the right place. Afterwards the mount is also removed in the
223 * original mount namespace, thus revealing the file that was just created. */
224
f5e754e0 225 etc_machine_id = prefix_roota(root, "/etc/machine-id");
979ef53a 226
e1873695 227 r = path_is_mount_point(etc_machine_id, NULL, 0);
979ef53a 228 if (r < 0)
f131770b 229 return log_error_errno(r, "Failed to determine whether %s is a mount point: %m", etc_machine_id);
979ef53a 230 if (r == 0) {
61233823 231 log_debug("%s is not a mount point. Nothing to do.", etc_machine_id);
979ef53a
DR
232 return 0;
233 }
234
235 /* Read existing machine-id */
236 fd = open(etc_machine_id, O_RDONLY|O_CLOEXEC|O_NOCTTY);
237 if (fd < 0)
238 return log_error_errno(errno, "Cannot open %s: %m", etc_machine_id);
239
c6878637 240 r = fd_is_temporary_fs(fd);
979ef53a
DR
241 if (r < 0)
242 return log_error_errno(r, "Failed to determine whether %s is on a temporary file system: %m", etc_machine_id);
baaa35ad
ZJS
243 if (r == 0)
244 return log_error_errno(SYNTHETIC_ERRNO(EROFS),
245 "%s is not on a temporary file system.",
246 etc_machine_id);
979ef53a 247
15b1248a
LP
248 r = id128_read_fd(fd, ID128_PLAIN, &id);
249 if (r < 0)
5e1ee764 250 return log_error_errno(r, "We didn't find a valid machine ID in %s: %m", etc_machine_id);
15b1248a 251
979ef53a
DR
252 fd = safe_close(fd);
253
254 /* Store current mount namespace */
671c3419 255 r = namespace_open(0, NULL, &initial_mntns_fd, NULL, NULL, NULL);
979ef53a
DR
256 if (r < 0)
257 return log_error_errno(r, "Can't fetch current mount namespace: %m");
258
259 /* Switch to a new mount namespace, isolate ourself and unmount etc_machine_id in our new namespace */
e2ec9c4d
LP
260 r = detach_mount_namespace();
261 if (r < 0)
262 return log_error_errno(r, "Failed to set up new mount namespace: %m");
979ef53a 263
21935150
LP
264 r = umount_verbose(LOG_ERR, etc_machine_id, 0);
265 if (r < 0)
266 return r;
979ef53a
DR
267
268 /* Update a persistent version of etc_machine_id */
15b1248a 269 r = id128_write(etc_machine_id, ID128_PLAIN, id, true);
979ef53a 270 if (r < 0)
15b1248a 271 return log_error_errno(r, "Cannot write %s. This is mandatory to get a persistent machine ID: %m", etc_machine_id);
979ef53a
DR
272
273 /* Return to initial namespace and proceed a lazy tmpfs unmount */
671c3419 274 r = namespace_enter(-1, initial_mntns_fd, -1, -1, -1);
979ef53a
DR
275 if (r < 0)
276 return log_warning_errno(r, "Failed to switch back to initial mount namespace: %m.\nWe'll keep transient %s file until next reboot.", etc_machine_id);
277
278 if (umount2(etc_machine_id, MNT_DETACH) < 0)
279 return log_warning_errno(errno, "Failed to unmount transient %s file: %m.\nWe keep that mount until next reboot.", etc_machine_id);
280
281 return 0;
282}