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