• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 15232239991

24 May 2025 08:01PM UTC coverage: 72.053% (-0.02%) from 72.07%
15232239991

push

github

web-flow
docs: add man pages for sd_device_enumerator_[new,ref,unref,unrefp] (#37586)

For #20929.

299160 of 415197 relevant lines covered (72.05%)

703671.29 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

71.79
/src/shared/machine-id-setup.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <fcntl.h>
4
#include <sys/mount.h>
5
#include <unistd.h>
6

7
#include "sd-daemon.h"
8
#include "sd-id128.h"
9

10
#include "alloc-util.h"
11
#include "chase.h"
12
#include "creds-util.h"
13
#include "fd-util.h"
14
#include "fs-util.h"
15
#include "id128-util.h"
16
#include "initrd-util.h"
17
#include "io-util.h"
18
#include "log.h"
19
#include "machine-id-setup.h"
20
#include "mount-util.h"
21
#include "mountpoint-util.h"
22
#include "namespace-util.h"
23
#include "path-util.h"
24
#include "process-util.h"
25
#include "stat-util.h"
26
#include "string-util.h"
27
#include "strv.h"
28
#include "sync-util.h"
29
#include "umask-util.h"
30
#include "virt.h"
31

32
static int acquire_machine_id_from_credential(sd_id128_t *ret_machine_id) {
26✔
33
        _cleanup_free_ char *buf = NULL;
26✔
34
        int r;
26✔
35

36
        assert(ret_machine_id);
26✔
37

38
        r = read_credential_with_decryption("system.machine_id", (void**) &buf, /* ret_size= */ NULL);
26✔
39
        if (r < 0)
26✔
40
                return log_warning_errno(r, "Failed to read system.machine_id credential, ignoring: %m");
×
41
        if (r == 0) {
26✔
42
                /* not found */
43
                *ret_machine_id = SD_ID128_NULL;
26✔
44
                return 0;
26✔
45
        }
46

47
        if (streq(buf, "firmware")) {
×
48
                *ret_machine_id = SD_ID128_NULL;
×
49
                return 1;
×
50
        }
51

52
        r = sd_id128_from_string(buf, ret_machine_id);
×
53
        if (r < 0)
×
54
                return log_warning_errno(r, "Failed to parse system.machine_id credential, ignoring: %m");
×
55

56
        log_info("Initializing machine ID from credential.");
×
57
        return 1;
58
}
59

60
static int acquire_machine_id(const char *root, bool machine_id_from_firmware, sd_id128_t *ret) {
30✔
61
        _cleanup_close_ int fd = -EBADF;
30✔
62
        int r;
30✔
63

64
        assert(ret);
30✔
65

66
        /* First, try reading the machine ID from /run/machine-id, which may not be mounted on
67
         * /etc/machine-id yet. This is important on switching root especially on soft-reboot, Otherwise,
68
         * machine ID may be changed after the transition. */
69
        if (isempty(root) && running_in_chroot() <= 0 &&
30✔
70
            id128_read("/run/machine-id", ID128_FORMAT_PLAIN, ret) >= 0) {
26✔
71
                log_info("Reusing machine ID stored in /run/machine-id.");
×
72
                return 1; /* Indicate that the machine ID is reused. */
×
73
        }
74

75
        /* Then, try reading the D-Bus machine ID, unless it is a symlink */
76
        fd = chase_and_open("/var/lib/dbus/machine-id", root, CHASE_PREFIX_ROOT|CHASE_NOFOLLOW|CHASE_MUST_BE_REGULAR, O_RDONLY|O_CLOEXEC|O_NOCTTY, NULL);
30✔
77
        if (fd >= 0 && id128_read_fd(fd, ID128_FORMAT_PLAIN | ID128_REFUSE_NULL, ret) >= 0) {
30✔
78
                log_info("Initializing machine ID from D-Bus machine ID.");
×
79
                return 0;
×
80
        }
81

82
        if (isempty(root) && running_in_chroot() <= 0) {
30✔
83
                /* Let's use a system credential for the machine ID if we can */
84
                sd_id128_t from_credential;
26✔
85
                r = acquire_machine_id_from_credential(&from_credential);
26✔
86
                if (r > 0) {
26✔
87
                        if (!sd_id128_is_null(from_credential)) {
×
88
                                /* got a valid machine id from creds */
89
                                *ret = from_credential;
×
90
                                return 0;
12✔
91
                        }
92

93
                        /* We got a credential, and it was set to "firmware", hence definitely try that */
94
                        machine_id_from_firmware = true;
95
                }
96

97
                /* If that didn't work, see if we are running in a container,
98
                 * and a machine ID was passed in via $container_uuid the way
99
                 * libvirt/LXC does it */
100

101
                if (detect_container() > 0) {
26✔
102
                        _cleanup_free_ char *e = NULL;
12✔
103

104
                        if (getenv_for_pid(1, "container_uuid", &e) > 0 &&
24✔
105
                            sd_id128_from_string(e, ret) >= 0) {
12✔
106
                                log_info("Initializing machine ID from container UUID.");
12✔
107
                                return 0;
12✔
108
                        }
109

110
                } else if (IN_SET(detect_vm(), VIRTUALIZATION_KVM, VIRTUALIZATION_AMAZON, VIRTUALIZATION_QEMU, VIRTUALIZATION_XEN, VIRTUALIZATION_BHYVE) || machine_id_from_firmware) {
14✔
111

112
                        /* If we are not running in a container, see if we are running in a VM that provides
113
                         * a system UUID via the SMBIOS/DMI interfaces.  Such environments include QEMU/KVM
114
                         * with the -uuid on the qemu command line or the Amazon EC2 Nitro hypervisor. */
115

116
                        if (id128_get_product(ret) >= 0) {
14✔
117
                                log_info("Initializing machine ID from SMBIOS/DMI UUID.");
×
118
                                return 0;
×
119
                        }
120
                }
121
        }
122

123
        /* If that didn't work, generate a random machine ID */
124
        r = sd_id128_randomize(ret);
18✔
125
        if (r < 0)
18✔
126
                return log_error_errno(r, "Failed to generate randomized machine ID: %m");
×
127

128
        log_info("Initializing machine ID from random generator.");
18✔
129
        return 0;
130
}
131

132
int machine_id_setup(const char *root, sd_id128_t machine_id, MachineIdSetupFlags flags, sd_id128_t *ret) {
41✔
133
        _cleanup_free_ char *etc_machine_id = NULL, *run_machine_id = NULL;
41✔
134
        bool writable, write_run_machine_id = true;
41✔
135
        _cleanup_close_ int fd = -EBADF, run_fd = -EBADF;
82✔
136
        bool unlink_run_machine_id = false;
41✔
137
        int r;
41✔
138

139
        WITH_UMASK(0000) {
82✔
140
                _cleanup_close_ int inode_fd = -EBADF;
41✔
141

142
                r = chase("/etc/machine-id", root, CHASE_PREFIX_ROOT|CHASE_MUST_BE_REGULAR, &etc_machine_id, &inode_fd);
41✔
143
                if (r == -ENOENT) {
41✔
144
                        _cleanup_close_ int etc_fd = -EBADF;
2✔
145
                        _cleanup_free_ char *etc = NULL;
2✔
146

147
                        r = chase("/etc/", root, CHASE_PREFIX_ROOT|CHASE_MKDIR_0755|CHASE_MUST_BE_DIRECTORY, &etc, &etc_fd);
2✔
148
                        if (r < 0)
2✔
149
                                return log_error_errno(r, "Failed to open '/etc/': %m");
×
150

151
                        etc_machine_id = path_join(etc, "machine-id");
2✔
152
                        if (!etc_machine_id)
2✔
153
                                return log_oom();
×
154

155
                        /* We create this 0444, to indicate that this isn't really something you should ever
156
                         * modify. Of course, since the file will be owned by root it doesn't matter much, but maybe
157
                         * people look. */
158

159
                        fd = openat(etc_fd, "machine-id", O_CREAT|O_EXCL|O_RDWR|O_NOFOLLOW|O_CLOEXEC, 0444);
2✔
160
                        if (fd < 0) {
2✔
161
                                if (errno == EROFS)
×
162
                                        return log_error_errno(errno,
×
163
                                                               "System cannot boot: Missing %s and %s/ is read-only.\n"
164
                                                               "Booting up is supported only when:\n"
165
                                                               "1) /etc/machine-id exists and is populated.\n"
166
                                                               "2) /etc/machine-id exists and is empty.\n"
167
                                                               "3) /etc/machine-id is missing and /etc/ is writable.",
168
                                                               etc_machine_id,
169
                                                               etc);
170

171
                                return log_error_errno(errno, "Cannot create '%s': %m", etc_machine_id);
×
172
                        }
173

174
                        log_debug("Successfully opened new '%s' file.", etc_machine_id);
2✔
175
                        writable = true;
2✔
176
                } else if (r < 0)
39✔
177
                        return log_error_errno(r, "Cannot open '/etc/machine-id': %m");
×
178
                else {
179
                        /* We pinned the inode, now try to convert it into a writable file */
180

181
                        fd = xopenat_full(inode_fd, /* path= */ NULL, O_RDWR|O_CLOEXEC, XO_REGULAR, 0444);
39✔
182
                        if (fd < 0) {
39✔
183
                                log_debug_errno(fd, "Failed to open '%s' in writable mode, retrying in read-only mode: %m", etc_machine_id);
1✔
184

185
                                /* If that didn't work, convert it into a readable file */
186
                                fd = xopenat_full(inode_fd, /* path= */ NULL, O_RDONLY|O_CLOEXEC, XO_REGULAR, MODE_INVALID);
1✔
187
                                if (fd < 0)
1✔
188
                                        return log_error_errno(fd, "Cannot open '%s' in neither writable nor read-only mode: %m", etc_machine_id);
×
189

190
                                log_debug("Successfully opened existing '%s' file in read-only mode.", etc_machine_id);
1✔
191
                                writable = false;
192
                        } else {
193
                                log_debug("Successfully opened existing '%s' file in writable mode.", etc_machine_id);
38✔
194
                                writable = true;
195
                        }
196
                }
197
        }
198

199
        /* A we got a valid machine ID argument, that's what counts */
200
        if (sd_id128_is_null(machine_id) || FLAGS_SET(flags, MACHINE_ID_SETUP_FORCE_FIRMWARE)) {
82✔
201

202
                /* Try to read any existing machine ID */
203
                r = id128_read_fd(fd, ID128_FORMAT_PLAIN, &machine_id);
41✔
204
                if (r >= 0)
41✔
205
                        goto finish;
11✔
206

207
                log_debug_errno(r, "Unable to read current machine ID, acquiring new one: %m");
30✔
208

209
                /* Hmm, so, the id currently stored is not useful, then let's acquire one. */
210
                r = acquire_machine_id(root, FLAGS_SET(flags, MACHINE_ID_SETUP_FORCE_FIRMWARE), &machine_id);
30✔
211
                if (r < 0)
30✔
212
                        return r;
213

214
                write_run_machine_id = !r; /* acquire_machine_id() returns 1 in case we read this machine ID
30✔
215
                                            * from /run/machine-id */
216
        }
217

218
        if (writable) {
30✔
219
                if (lseek(fd, 0, SEEK_SET) < 0)
29✔
220
                        return log_error_errno(errno, "Failed to seek %s: %m", etc_machine_id);
×
221

222
                if (ftruncate(fd, 0) < 0)
29✔
223
                        return log_error_errno(errno, "Failed to truncate %s: %m", etc_machine_id);
×
224

225
                /* If the caller requested a transient machine-id, write the string "uninitialized\n" to
226
                 * disk and overmount it with a transient file.
227
                 *
228
                 * Otherwise write the machine-id directly to disk. */
229
                if (FLAGS_SET(flags, MACHINE_ID_SETUP_FORCE_TRANSIENT)) {
29✔
230
                        r = loop_write(fd, "uninitialized\n", SIZE_MAX);
17✔
231
                        if (r < 0)
17✔
232
                                return log_error_errno(r, "Failed to write uninitialized %s: %m", etc_machine_id);
×
233

234
                        r = fsync_full(fd);
17✔
235
                        if (r < 0)
17✔
236
                                return log_error_errno(r, "Failed to sync %s: %m", etc_machine_id);
×
237
                } else {
238
                        r = id128_write_fd(fd, ID128_FORMAT_PLAIN | ID128_SYNC_ON_WRITE, machine_id);
12✔
239
                        if (r < 0)
12✔
240
                                return log_error_errno(r, "Failed to write %s: %m", etc_machine_id);
×
241

242
                        goto finish;
12✔
243
                }
244
        }
245

246
        /* Hmm, we couldn't or shouldn't write the machine-id to /etc/? So let's write it to /run/machine-id
247
         * as a replacement */
248

249
        if (write_run_machine_id) {
18✔
250
                _cleanup_free_ char *run = NULL;
18✔
251

252
                r = chase("/run/", root, CHASE_PREFIX_ROOT|CHASE_MKDIR_0755|CHASE_MUST_BE_DIRECTORY, &run, &run_fd);
18✔
253
                if (r < 0)
18✔
254
                        return log_error_errno(r, "Failed to open '/run/': %m");
×
255

256
                run_machine_id = path_join(run, "machine-id");
18✔
257
                if (!run_machine_id)
18✔
258
                        return log_oom();
×
259

260
                WITH_UMASK(0022) {
36✔
261
                        r = id128_write_at(run_fd, "machine-id", ID128_FORMAT_PLAIN, machine_id);
18✔
262
                        if (r < 0) {
18✔
263
                                (void) unlinkat(run_fd, "machine-id", /* flags = */ 0);
×
264
                                return log_error_errno(r, "Cannot write '%s': %m", run_machine_id);
×
265
                        }
266
                }
267

268
                unlink_run_machine_id = true;
18✔
269
        } else {
270
                r = chase("/run/machine-id", root, CHASE_PREFIX_ROOT|CHASE_MUST_BE_REGULAR, &run_machine_id, /* ret_fd= */ NULL);
×
271
                if (r < 0)
×
272
                        return log_error_errno(r, "Failed to open '/run/machine-id': %m");
×
273
        }
274

275
        /* And now, let's mount it over */
276
        r = mount_follow_verbose(LOG_ERR, run_machine_id, FORMAT_PROC_FD_PATH(fd), /* fstype= */ NULL, MS_BIND, /* options= */ NULL);
18✔
277
        if (r < 0) {
18✔
278
                if (unlink_run_machine_id)
×
279
                        (void) unlinkat(ASSERT_FD(run_fd), "machine-id", /* flags = */ 0);
×
280
                return r;
×
281
        }
282

283
        log_full(FLAGS_SET(flags, MACHINE_ID_SETUP_FORCE_TRANSIENT) ? LOG_DEBUG : LOG_INFO, "Installed transient '%s' file.", etc_machine_id);
18✔
284

285
        /* Mark the mount read-only (note: we are not going via FORMAT_PROC_FD_PATH() here because that fd is not updated to our new bind mount) */
286
        (void) mount_follow_verbose(LOG_WARNING, /* what= */ NULL, etc_machine_id, /* fstype= */ NULL, MS_BIND|MS_RDONLY|MS_REMOUNT, /* options= */ NULL);
18✔
287

288
finish:
41✔
289
        if (!in_initrd())
41✔
290
                (void) sd_notifyf(/* unset_environment= */ false, "X_SYSTEMD_MACHINE_ID=" SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(machine_id));
32✔
291

292
        if (ret)
41✔
293
                *ret = machine_id;
8✔
294

295
        return 0;
296
}
297

298
int machine_id_commit(const char *root) {
101✔
299
        sd_id128_t id;
101✔
300
        int r;
101✔
301

302
        if (empty_or_root(root)) {
101✔
303
                /* Before doing anything, sync everything to ensure any changes by first-boot units are
304
                 * persisted.
305
                 *
306
                 * First, explicitly sync the file systems we care about and check if it worked. */
307
                FOREACH_STRING(sync_path, "/etc/", "/var/") {
300✔
308
                        r = syncfs_path(AT_FDCWD, sync_path);
200✔
309
                        if (r < 0)
200✔
310
                                return log_error_errno(r, "Cannot sync %s: %m", sync_path);
×
311
                }
312

313
                /* Afterwards, sync() the rest too, but we can't check the return value for these. */
314
                sync();
100✔
315
        }
316

317
        /* Replaces a tmpfs bind mount of /etc/machine-id by a proper file, atomically. For this, the umount is removed
318
         * in a mount namespace, a new file is created at the right place. Afterwards the mount is also removed in the
319
         * original mount namespace, thus revealing the file that was just created. */
320

321
        _cleanup_close_ int etc_fd = -EBADF;
101✔
322
        _cleanup_free_ char *etc = NULL;
101✔
323
        r = chase("/etc/", root, CHASE_PREFIX_ROOT|CHASE_MUST_BE_DIRECTORY, &etc, &etc_fd);
101✔
324
        if (r < 0)
101✔
325
                return log_error_errno(r, "Failed to open /etc/: %m");
×
326

327
        _cleanup_free_ char *etc_machine_id = path_join(etc, "machine-id");
202✔
328
        if (!etc_machine_id)
101✔
329
                return log_oom();
×
330

331
        r = is_mount_point_at(etc_fd, "machine-id", /* flags= */ 0);
101✔
332
        if (r < 0)
101✔
333
                return log_error_errno(r, "Failed to determine whether %s is a mount point: %m", etc_machine_id);
×
334
        if (r == 0) {
101✔
335
                log_debug("%s is not a mount point. Nothing to do.", etc_machine_id);
×
336
                return 0;
×
337
        }
338

339
        /* Read existing machine-id */
340

341
        _cleanup_close_ int fd = xopenat_full(etc_fd, "machine-id", O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, XO_REGULAR, MODE_INVALID);
202✔
342
        if (fd < 0)
101✔
343
                return log_error_errno(fd, "Cannot open %s: %m", etc_machine_id);
×
344

345
        etc_fd = safe_close(etc_fd);
101✔
346

347
        r = fd_is_temporary_fs(fd);
101✔
348
        if (r < 0)
101✔
349
                return log_error_errno(r, "Failed to determine whether %s is on a temporary file system: %m", etc_machine_id);
×
350
        if (r == 0)
101✔
351
                return log_error_errno(SYNTHETIC_ERRNO(EROFS),
×
352
                                       "%s is not on a temporary file system.",
353
                                       etc_machine_id);
354

355
        r = id128_read_fd(fd, ID128_FORMAT_PLAIN, &id);
101✔
356
        if (r < 0)
101✔
357
                return log_error_errno(r, "We didn't find a valid machine ID in %s: %m", etc_machine_id);
×
358

359
        /* Store current mount namespace */
360
        _cleanup_close_ int initial_mntns_fd = namespace_open_by_type(NAMESPACE_MOUNT);
202✔
361
        if (initial_mntns_fd < 0)
101✔
362
                return log_error_errno(initial_mntns_fd, "Can't fetch current mount namespace: %m");
×
363

364
        /* Switch to a new mount namespace, isolate ourself and unmount etc_machine_id in our new namespace */
365
        r = detach_mount_namespace();
101✔
366
        if (r < 0)
101✔
367
                return log_error_errno(r, "Failed to set up new mount namespace: %m");
×
368

369
        /* Open /etc/ again after we transitioned into our own private mount namespace */
370
        _cleanup_close_ int etc_fd_again = -EBADF;
101✔
371
        r = chase("/etc/", root, CHASE_PREFIX_ROOT|CHASE_MUST_BE_DIRECTORY, /* ret_path= */ NULL, &etc_fd_again);
101✔
372
        if (r < 0)
101✔
373
                return log_error_errno(r, "Failed to open /etc/: %m");
×
374

375
        r = umountat_detach_verbose(LOG_ERR, etc_fd_again, "machine-id");
101✔
376
        if (r < 0)
101✔
377
                return r;
378

379
        /* Update a persistent version of etc_machine_id */
380
        r = id128_write_at(etc_fd_again, "machine-id", ID128_FORMAT_PLAIN | ID128_SYNC_ON_WRITE, id);
101✔
381
        if (r < 0)
101✔
382
                return log_error_errno(r, "Cannot write %s. This is mandatory to get a persistent machine ID: %m", etc_machine_id);
×
383

384
        etc_fd_again = safe_close(etc_fd_again);
101✔
385

386
        /* Return to initial namespace and proceed a lazy tmpfs unmount */
387
        r = namespace_enter(/* pidns_fd = */ -EBADF,
101✔
388
                            initial_mntns_fd,
389
                            /* netns_fd = */ -EBADF,
390
                            /* userns_fd = */ -EBADF,
391
                            /* root_fd = */ -EBADF);
392
        if (r < 0)
101✔
393
                return log_warning_errno(r,
×
394
                                         "Failed to switch back to initial mount namespace: %m.\n"
395
                                         "We'll keep transient %s file until next reboot.", etc_machine_id);
396

397
        r = umountat_detach_verbose(LOG_DEBUG, fd, /* where= */ NULL);
101✔
398
        if (r < 0)
101✔
399
                return log_warning_errno(r,
×
400
                                         "Failed to unmount transient %s file: %m.\n"
401
                                         "We keep that mount until next reboot.", etc_machine_id);
402

403
        return 0;
404
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc