]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/machine-id-setup.c
Merge pull request #3078 from poettering/get-processes
[thirdparty/systemd.git] / src / core / machine-id-setup.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2010 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <sched.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <sys/mount.h>
26 #include <unistd.h>
27
28 #include "sd-id128.h"
29
30 #include "alloc-util.h"
31 #include "fd-util.h"
32 #include "fileio.h"
33 #include "fs-util.h"
34 #include "hexdecoct.h"
35 #include "io-util.h"
36 #include "log.h"
37 #include "machine-id-setup.h"
38 #include "macro.h"
39 #include "mkdir.h"
40 #include "mount-util.h"
41 #include "path-util.h"
42 #include "process-util.h"
43 #include "stat-util.h"
44 #include "string-util.h"
45 #include "umask-util.h"
46 #include "util.h"
47 #include "virt.h"
48
49 static int shorten_uuid(char destination[34], const char source[36]) {
50 unsigned i, j;
51
52 assert(destination);
53 assert(source);
54
55 /* Converts a UUID into a machine ID, by lowercasing it and
56 * removing dashes. Validates everything. */
57
58 for (i = 0, j = 0; i < 36 && j < 32; i++) {
59 int t;
60
61 t = unhexchar(source[i]);
62 if (t < 0)
63 continue;
64
65 destination[j++] = hexchar(t);
66 }
67
68 if (i != 36 || j != 32)
69 return -EINVAL;
70
71 destination[32] = '\n';
72 destination[33] = 0;
73 return 0;
74 }
75
76 static int read_machine_id(int fd, char id[34]) {
77 char id_to_validate[34];
78 int r;
79
80 assert(fd >= 0);
81 assert(id);
82
83 /* Reads a machine ID from a file, validates it, and returns
84 * it. The returned ID ends in a newline. */
85
86 r = loop_read_exact(fd, id_to_validate, 33, false);
87 if (r < 0)
88 return r;
89
90 if (id_to_validate[32] != '\n')
91 return -EINVAL;
92
93 id_to_validate[32] = 0;
94
95 if (!id128_is_valid(id_to_validate))
96 return -EINVAL;
97
98 memcpy(id, id_to_validate, 32);
99 id[32] = '\n';
100 id[33] = 0;
101 return 0;
102 }
103
104 static int write_machine_id(int fd, const char id[34]) {
105 int r;
106
107 assert(fd >= 0);
108 assert(id);
109
110 if (lseek(fd, 0, SEEK_SET) < 0)
111 return -errno;
112
113 r = loop_write(fd, id, 33, false);
114 if (r < 0)
115 return r;
116
117 if (fsync(fd) < 0)
118 return -errno;
119
120 return 0;
121 }
122
123 static int generate_machine_id(char id[34], const char *root) {
124 int fd, r;
125 unsigned char *p;
126 sd_id128_t buf;
127 char *q;
128 const char *dbus_machine_id;
129
130 assert(id);
131
132 dbus_machine_id = prefix_roota(root, "/var/lib/dbus/machine-id");
133
134 /* First, try reading the D-Bus machine id, unless it is a symlink */
135 fd = open(dbus_machine_id, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
136 if (fd >= 0) {
137 r = read_machine_id(fd, id);
138 safe_close(fd);
139
140 if (r >= 0) {
141 log_info("Initializing machine ID from D-Bus machine ID.");
142 return 0;
143 }
144 }
145
146 if (isempty(root)) {
147 /* If that didn't work, see if we are running in a container,
148 * and a machine ID was passed in via $container_uuid the way
149 * libvirt/LXC does it */
150
151 if (detect_container() > 0) {
152 _cleanup_free_ char *e = NULL;
153
154 r = getenv_for_pid(1, "container_uuid", &e);
155 if (r > 0) {
156 r = shorten_uuid(id, e);
157 if (r >= 0) {
158 log_info("Initializing machine ID from container UUID.");
159 return 0;
160 }
161 }
162
163 } else if (detect_vm() == VIRTUALIZATION_KVM) {
164
165 /* If we are not running in a container, see if we are
166 * running in qemu/kvm and a machine ID was passed in
167 * via -uuid on the qemu/kvm command line */
168
169 char uuid[36];
170
171 fd = open("/sys/class/dmi/id/product_uuid", O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
172 if (fd >= 0) {
173 r = loop_read_exact(fd, uuid, 36, false);
174 safe_close(fd);
175
176 if (r >= 0) {
177 r = shorten_uuid(id, uuid);
178 if (r >= 0) {
179 log_info("Initializing machine ID from KVM UUID.");
180 return 0;
181 }
182 }
183 }
184 }
185 }
186
187 /* If that didn't work, generate a random machine id */
188 r = sd_id128_randomize(&buf);
189 if (r < 0)
190 return log_error_errno(r, "Failed to open /dev/urandom: %m");
191
192 for (p = buf.bytes, q = id; p < buf.bytes + sizeof(buf); p++, q += 2) {
193 q[0] = hexchar(*p >> 4);
194 q[1] = hexchar(*p & 15);
195 }
196
197 id[32] = '\n';
198 id[33] = 0;
199
200 log_info("Initializing machine ID from random generator.");
201
202 return 0;
203 }
204
205 int machine_id_setup(const char *root, sd_id128_t machine_id) {
206 const char *etc_machine_id, *run_machine_id;
207 _cleanup_close_ int fd = -1;
208 bool writable = true;
209 char id[34]; /* 32 + \n + \0 */
210 int r;
211
212 etc_machine_id = prefix_roota(root, "/etc/machine-id");
213 run_machine_id = prefix_roota(root, "/run/machine-id");
214
215 RUN_WITH_UMASK(0000) {
216 /* We create this 0444, to indicate that this isn't really
217 * something you should ever modify. Of course, since the file
218 * will be owned by root it doesn't matter much, but maybe
219 * people look. */
220
221 mkdir_parents(etc_machine_id, 0755);
222 fd = open(etc_machine_id, O_RDWR|O_CREAT|O_CLOEXEC|O_NOCTTY, 0444);
223 if (fd < 0) {
224 int old_errno = errno;
225
226 fd = open(etc_machine_id, O_RDONLY|O_CLOEXEC|O_NOCTTY);
227 if (fd < 0) {
228 if (old_errno == EROFS && errno == ENOENT)
229 log_error_errno(errno,
230 "System cannot boot: Missing /etc/machine-id and /etc is mounted read-only.\n"
231 "Booting up is supported only when:\n"
232 "1) /etc/machine-id exists and is populated.\n"
233 "2) /etc/machine-id exists and is empty.\n"
234 "3) /etc/machine-id is missing and /etc is writable.\n");
235 else
236 log_error_errno(errno, "Cannot open %s: %m", etc_machine_id);
237
238 return -errno;
239 }
240
241 writable = false;
242 }
243 }
244
245 /* A machine id argument overrides all other machined-ids */
246 if (!sd_id128_is_null(machine_id)) {
247 sd_id128_to_string(machine_id, id);
248 id[32] = '\n';
249 id[33] = 0;
250 } else {
251 if (read_machine_id(fd, id) >= 0)
252 return 0;
253
254 /* Hmm, so, the id currently stored is not useful, then let's
255 * generate one */
256
257 r = generate_machine_id(id, root);
258 if (r < 0)
259 return r;
260 }
261
262 if (writable)
263 if (write_machine_id(fd, id) >= 0)
264 return 0;
265
266 fd = safe_close(fd);
267
268 /* Hmm, we couldn't write it? So let's write it to
269 * /run/machine-id as a replacement */
270
271 RUN_WITH_UMASK(0022) {
272 r = write_string_file(run_machine_id, id, WRITE_STRING_FILE_CREATE);
273 if (r < 0) {
274 (void) unlink(run_machine_id);
275 return log_error_errno(r, "Cannot write %s: %m", run_machine_id);
276 }
277 }
278
279 /* And now, let's mount it over */
280 if (mount(run_machine_id, etc_machine_id, NULL, MS_BIND, NULL) < 0) {
281 (void) unlink_noerrno(run_machine_id);
282 return log_error_errno(errno, "Failed to mount %s: %m", etc_machine_id);
283 }
284
285 log_info("Installed transient %s file.", etc_machine_id);
286
287 /* Mark the mount read-only */
288 if (mount(NULL, etc_machine_id, NULL, MS_BIND|MS_RDONLY|MS_REMOUNT, NULL) < 0)
289 log_warning_errno(errno, "Failed to make transient %s read-only: %m", etc_machine_id);
290
291 return 0;
292 }
293
294 int machine_id_commit(const char *root) {
295 _cleanup_close_ int fd = -1, initial_mntns_fd = -1;
296 const char *etc_machine_id;
297 char id[34]; /* 32 + \n + \0 */
298 int r;
299
300 etc_machine_id = prefix_roota(root, "/etc/machine-id");
301
302 r = path_is_mount_point(etc_machine_id, 0);
303 if (r < 0)
304 return log_error_errno(r, "Failed to determine whether %s is a mount point: %m", etc_machine_id);
305 if (r == 0) {
306 log_debug("%s is is not a mount point. Nothing to do.", etc_machine_id);
307 return 0;
308 }
309
310 /* Read existing machine-id */
311 fd = open(etc_machine_id, O_RDONLY|O_CLOEXEC|O_NOCTTY);
312 if (fd < 0)
313 return log_error_errno(errno, "Cannot open %s: %m", etc_machine_id);
314
315 r = read_machine_id(fd, id);
316 if (r < 0)
317 return log_error_errno(r, "We didn't find a valid machine ID in %s.", etc_machine_id);
318
319 r = fd_is_temporary_fs(fd);
320 if (r < 0)
321 return log_error_errno(r, "Failed to determine whether %s is on a temporary file system: %m", etc_machine_id);
322 if (r == 0) {
323 log_error("%s is not on a temporary file system.", etc_machine_id);
324 return -EROFS;
325 }
326
327 fd = safe_close(fd);
328
329 /* Store current mount namespace */
330 r = namespace_open(0, NULL, &initial_mntns_fd, NULL, NULL, NULL);
331 if (r < 0)
332 return log_error_errno(r, "Can't fetch current mount namespace: %m");
333
334 /* Switch to a new mount namespace, isolate ourself and unmount etc_machine_id in our new namespace */
335 if (unshare(CLONE_NEWNS) < 0)
336 return log_error_errno(errno, "Failed to enter new namespace: %m");
337
338 if (mount(NULL, "/", NULL, MS_SLAVE | MS_REC, NULL) < 0)
339 return log_error_errno(errno, "Couldn't make-rslave / mountpoint in our private namespace: %m");
340
341 if (umount(etc_machine_id) < 0)
342 return log_error_errno(errno, "Failed to unmount transient %s file in our private namespace: %m", etc_machine_id);
343
344 /* Update a persistent version of etc_machine_id */
345 fd = open(etc_machine_id, O_RDWR|O_CREAT|O_CLOEXEC|O_NOCTTY, 0444);
346 if (fd < 0)
347 return log_error_errno(errno, "Cannot open for writing %s. This is mandatory to get a persistent machine-id: %m", etc_machine_id);
348
349 r = write_machine_id(fd, id);
350 if (r < 0)
351 return log_error_errno(r, "Cannot write %s: %m", etc_machine_id);
352
353 fd = safe_close(fd);
354
355 /* Return to initial namespace and proceed a lazy tmpfs unmount */
356 r = namespace_enter(-1, initial_mntns_fd, -1, -1, -1);
357 if (r < 0)
358 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);
359
360 if (umount2(etc_machine_id, MNT_DETACH) < 0)
361 return log_warning_errno(errno, "Failed to unmount transient %s file: %m.\nWe keep that mount until next reboot.", etc_machine_id);
362
363 return 0;
364 }