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