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