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