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