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