]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/machine-id-setup.c
tree-wide: use -EBADF for fd initialization
[thirdparty/systemd.git] / src / shared / machine-id-setup.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
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"
910fd145 12#include "id128-util.h"
3023f2fe 13#include "io-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"
bf819d3a 25#include "sync-util.h"
affb60b1 26#include "umask-util.h"
618234a5 27#include "virt.h"
8d41a963 28
4b1afed0 29static int generate_machine_id(const char *root, sd_id128_t *ret) {
75f86906 30 const char *dbus_machine_id;
254d1313 31 _cleanup_close_ int fd = -EBADF;
4b1afed0 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) {
057bf780 40 if (id128_read_fd(fd, ID128_FORMAT_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
382a46d1 62 } else if (IN_SET(detect_vm(), VIRTUALIZATION_KVM, VIRTUALIZATION_AMAZON, VIRTUALIZATION_QEMU)) {
75f86906 63
382a46d1
BJ
64 /* If we are not running in a container, see if we are running in a VM that provides
65 * a system UUID via the SMBIOS/DMI interfaces. Such environments include QEMU/KVM
66 * with the -uuid on the qemu command line or the Amazon EC2 Nitro hypervisor. */
5dd6d0f8 67
b4be4ff8 68 if (id128_get_product(ret) >= 0) {
382a46d1 69 log_info("Initializing machine ID from VM UUID.");
8fa0de65
DJL
70 return 0;
71 }
0b36bbc4 72 }
d4eb120a
LP
73 }
74
d7ccca2e 75 /* If that didn't work, generate a random machine id */
4b1afed0 76 r = sd_id128_randomize(ret);
23bbb0de 77 if (r < 0)
5e1ee764 78 return log_error_errno(r, "Failed to generate randomized machine ID: %m");
d7ccca2e
LP
79
80 log_info("Initializing machine ID from random generator.");
d7ccca2e
LP
81 return 0;
82}
83
3023f2fe 84int machine_id_setup(const char *root, bool force_transient, sd_id128_t machine_id, sd_id128_t *ret) {
c6ac7e4b 85 const char *etc_machine_id, *run_machine_id;
254d1313 86 _cleanup_close_ int fd = -EBADF;
4b1afed0 87 bool writable;
c6ac7e4b 88 int r;
9496e375 89
f5e754e0 90 etc_machine_id = prefix_roota(root, "/etc/machine-id");
9496e375 91
2053593f 92 WITH_UMASK(0000) {
c6ac7e4b
LP
93 /* We create this 0444, to indicate that this isn't really
94 * something you should ever modify. Of course, since the file
95 * will be owned by root it doesn't matter much, but maybe
96 * people look. */
9496e375 97
4b1afed0 98 (void) mkdir_parents(etc_machine_id, 0755);
c6ac7e4b
LP
99 fd = open(etc_machine_id, O_RDWR|O_CREAT|O_CLOEXEC|O_NOCTTY, 0444);
100 if (fd < 0) {
101 int old_errno = errno;
102
103 fd = open(etc_machine_id, O_RDONLY|O_CLOEXEC|O_NOCTTY);
104 if (fd < 0) {
105 if (old_errno == EROFS && errno == ENOENT)
d49a7143 106 return log_error_errno(errno,
c6ac7e4b
LP
107 "System cannot boot: Missing /etc/machine-id and /etc is mounted read-only.\n"
108 "Booting up is supported only when:\n"
109 "1) /etc/machine-id exists and is populated.\n"
110 "2) /etc/machine-id exists and is empty.\n"
111 "3) /etc/machine-id is missing and /etc is writable.\n");
112 else
5e1ee764 113 return log_error_errno(errno, "Cannot open %s: %m", etc_machine_id);
c6ac7e4b 114 }
9496e375 115
c6ac7e4b 116 writable = false;
4b1afed0
LP
117 } else
118 writable = true;
c6ac7e4b
LP
119 }
120
4b1afed0
LP
121 /* A we got a valid machine ID argument, that's what counts */
122 if (sd_id128_is_null(machine_id)) {
9496e375 123
4b1afed0 124 /* Try to read any existing machine ID */
057bf780 125 if (id128_read_fd(fd, ID128_FORMAT_PLAIN, ret) >= 0)
4b1afed0 126 return 0;
c6ac7e4b 127
4b1afed0
LP
128 /* Hmm, so, the id currently stored is not useful, then let's generate one */
129 r = generate_machine_id(root, &machine_id);
ee48dbd5
NC
130 if (r < 0)
131 return r;
fcb24270 132 }
4b1afed0 133
fcb24270 134 if (writable) {
4b1afed0 135 if (lseek(fd, 0, SEEK_SET) == (off_t) -1)
fcb24270
EV
136 return log_error_errno(errno, "Failed to seek %s: %m", etc_machine_id);
137
138 if (ftruncate(fd, 0) < 0)
139 return log_error_errno(errno, "Failed to truncate %s: %m", etc_machine_id);
c6ac7e4b 140
3023f2fe
HS
141 /* If the caller requested a transient machine-id, write the string "uninitialized\n" to
142 * disk and overmount it with a transient file.
143 *
144 * Otherwise write the machine-id directly to disk. */
145 if (force_transient) {
146 r = loop_write(fd, "uninitialized\n", strlen("uninitialized\n"), false);
147 if (r < 0)
148 return log_error_errno(r, "Failed to write uninitialized %s: %m", etc_machine_id);
149
150 r = fsync_full(fd);
151 if (r < 0)
152 return log_error_errno(r, "Failed to sync %s: %m", etc_machine_id);
153 } else {
b40c8ebd 154 r = id128_write_fd(fd, ID128_FORMAT_PLAIN | ID128_SYNC_ON_WRITE, machine_id);
3023f2fe
HS
155 if (r < 0)
156 return log_error_errno(r, "Failed to write %s: %m", etc_machine_id);
157 else
158 goto finish;
159 }
fcb24270 160 }
c6ac7e4b
LP
161
162 fd = safe_close(fd);
163
3023f2fe
HS
164 /* Hmm, we couldn't or shouldn't write the machine-id to /etc?
165 * So let's write it to /run/machine-id as a replacement */
c6ac7e4b 166
4b1afed0
LP
167 run_machine_id = prefix_roota(root, "/run/machine-id");
168
2053593f 169 WITH_UMASK(0022)
b40c8ebd 170 r = id128_write(run_machine_id, ID128_FORMAT_PLAIN, machine_id);
4b1afed0
LP
171 if (r < 0) {
172 (void) unlink(run_machine_id);
173 return log_error_errno(r, "Cannot write %s: %m", run_machine_id);
c6ac7e4b
LP
174 }
175
176 /* And now, let's mount it over */
21935150
LP
177 r = mount_follow_verbose(LOG_ERR, run_machine_id, etc_machine_id, NULL, MS_BIND, NULL);
178 if (r < 0) {
179 (void) unlink(run_machine_id);
180 return r;
c6ac7e4b
LP
181 }
182
3023f2fe 183 log_full(force_transient ? LOG_DEBUG : LOG_INFO, "Installed transient %s file.", etc_machine_id);
c6ac7e4b
LP
184
185 /* Mark the mount read-only */
21935150
LP
186 r = mount_follow_verbose(LOG_WARNING, NULL, etc_machine_id, NULL, MS_BIND|MS_RDONLY|MS_REMOUNT, NULL);
187 if (r < 0)
188 return r;
4b1afed0
LP
189
190finish:
191 if (ret)
192 *ret = machine_id;
c6ac7e4b
LP
193
194 return 0;
9496e375
DR
195}
196
979ef53a 197int machine_id_commit(const char *root) {
254d1313 198 _cleanup_close_ int fd = -EBADF, initial_mntns_fd = -EBADF;
5980d463 199 const char *etc_machine_id;
15b1248a 200 sd_id128_t id;
979ef53a
DR
201 int r;
202
c261a5d0
HS
203 /* Before doing anything, sync everything to ensure any changes by first-boot units are persisted.
204 *
205 * First, explicitly sync the file systems we care about and check if it worked. */
206 FOREACH_STRING(sync_path, "/etc/", "/var/") {
207 r = syncfs_path(AT_FDCWD, sync_path);
208 if (r < 0)
209 return log_error_errno(r, "Cannot sync %s: %m", sync_path);
210 }
211
212 /* Afterwards, sync() the rest too, but we can't check the return value for these. */
213 sync();
214
15b1248a
LP
215 /* Replaces a tmpfs bind mount of /etc/machine-id by a proper file, atomically. For this, the umount is removed
216 * in a mount namespace, a new file is created at the right place. Afterwards the mount is also removed in the
217 * original mount namespace, thus revealing the file that was just created. */
218
f5e754e0 219 etc_machine_id = prefix_roota(root, "/etc/machine-id");
979ef53a 220
e1873695 221 r = path_is_mount_point(etc_machine_id, NULL, 0);
979ef53a 222 if (r < 0)
f131770b 223 return log_error_errno(r, "Failed to determine whether %s is a mount point: %m", etc_machine_id);
979ef53a 224 if (r == 0) {
61233823 225 log_debug("%s is not a mount point. Nothing to do.", etc_machine_id);
979ef53a
DR
226 return 0;
227 }
228
229 /* Read existing machine-id */
230 fd = open(etc_machine_id, O_RDONLY|O_CLOEXEC|O_NOCTTY);
231 if (fd < 0)
232 return log_error_errno(errno, "Cannot open %s: %m", etc_machine_id);
233
c6878637 234 r = fd_is_temporary_fs(fd);
979ef53a
DR
235 if (r < 0)
236 return log_error_errno(r, "Failed to determine whether %s is on a temporary file system: %m", etc_machine_id);
baaa35ad
ZJS
237 if (r == 0)
238 return log_error_errno(SYNTHETIC_ERRNO(EROFS),
239 "%s is not on a temporary file system.",
240 etc_machine_id);
979ef53a 241
057bf780 242 r = id128_read_fd(fd, ID128_FORMAT_PLAIN, &id);
15b1248a 243 if (r < 0)
5e1ee764 244 return log_error_errno(r, "We didn't find a valid machine ID in %s: %m", etc_machine_id);
15b1248a 245
979ef53a
DR
246 fd = safe_close(fd);
247
248 /* Store current mount namespace */
671c3419 249 r = namespace_open(0, NULL, &initial_mntns_fd, NULL, NULL, NULL);
979ef53a
DR
250 if (r < 0)
251 return log_error_errno(r, "Can't fetch current mount namespace: %m");
252
253 /* Switch to a new mount namespace, isolate ourself and unmount etc_machine_id in our new namespace */
e2ec9c4d
LP
254 r = detach_mount_namespace();
255 if (r < 0)
256 return log_error_errno(r, "Failed to set up new mount namespace: %m");
979ef53a 257
21935150
LP
258 r = umount_verbose(LOG_ERR, etc_machine_id, 0);
259 if (r < 0)
260 return r;
979ef53a
DR
261
262 /* Update a persistent version of etc_machine_id */
b40c8ebd 263 r = id128_write(etc_machine_id, ID128_FORMAT_PLAIN | ID128_SYNC_ON_WRITE, id);
979ef53a 264 if (r < 0)
15b1248a 265 return log_error_errno(r, "Cannot write %s. This is mandatory to get a persistent machine ID: %m", etc_machine_id);
979ef53a
DR
266
267 /* Return to initial namespace and proceed a lazy tmpfs unmount */
671c3419 268 r = namespace_enter(-1, initial_mntns_fd, -1, -1, -1);
979ef53a
DR
269 if (r < 0)
270 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);
271
272 if (umount2(etc_machine_id, MNT_DETACH) < 0)
273 return log_warning_errno(errno, "Failed to unmount transient %s file: %m.\nWe keep that mount until next reboot.", etc_machine_id);
274
275 return 0;
276}