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