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