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