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

systemd / systemd / 24809677062

22 Apr 2026 10:05PM UTC coverage: 71.989% (-0.2%) from 72.23%
24809677062

push

github

bluca
ukify: fix default path for hwids

The documentation and commit that added this seem to suggest this should
be under /usr/lib/systemd

fixes 117ec9db7

321862 of 447097 relevant lines covered (71.99%)

1201427.36 hits per line

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

73.93
/src/bootctl/bootctl.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <sys/stat.h>
4

5
#include "sd-varlink.h"
6

7
#include "blockdev-util.h"
8
#include "boot-entry.h"
9
#include "bootctl.h"
10
#include "bootctl-cleanup.h"
11
#include "bootctl-install.h"
12
#include "bootctl-random-seed.h"
13
#include "bootctl-reboot-to-firmware.h"
14
#include "bootctl-set-efivar.h"
15
#include "bootctl-status.h"
16
#include "bootctl-uki.h"
17
#include "bootctl-unlink.h"
18
#include "bootctl-util.h"
19
#include "build.h"
20
#include "devnum-util.h"
21
#include "dissect-image.h"
22
#include "efi-loader.h"
23
#include "efivars.h"
24
#include "escape.h"
25
#include "fd-util.h"
26
#include "find-esp.h"
27
#include "format-table.h"
28
#include "image-policy.h"
29
#include "log.h"
30
#include "loop-util.h"
31
#include "main-func.h"
32
#include "mount-util.h"
33
#include "openssl-util.h"
34
#include "options.h"
35
#include "pager.h"
36
#include "parse-argument.h"
37
#include "path-util.h"
38
#include "pretty-print.h"
39
#include "string-table.h"
40
#include "string-util.h"
41
#include "strv.h"
42
#include "varlink-io.systemd.BootControl.h"
43
#include "varlink-util.h"
44
#include "verbs.h"
45
#include "virt.h"
46

47
static GracefulMode _arg_graceful = ARG_GRACEFUL_NO;
48

49
char *arg_esp_path = NULL;
50
char *arg_xbootldr_path = NULL;
51
bool arg_print_esp_path = false;
52
bool arg_print_dollar_boot_path = false;
53
bool arg_print_loader_path = false;
54
bool arg_print_stub_path = false;
55
bool arg_print_efi_architecture = false;
56
unsigned arg_print_root_device = 0;
57
int arg_touch_variables = -1;
58
bool arg_install_random_seed = true;
59
PagerFlags arg_pager_flags = 0;
60
bool arg_quiet = false;
61
int arg_make_entry_directory = false; /* tri-state: < 0 for automatic logic */
62
sd_id128_t arg_machine_id = SD_ID128_NULL;
63
char *arg_install_layout = NULL;
64
BootEntryTokenType arg_entry_token_type = BOOT_ENTRY_TOKEN_AUTO;
65
char *arg_entry_token = NULL;
66
sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF;
67
bool arg_arch_all = false;
68
char *arg_root = NULL;
69
char *arg_image = NULL;
70
InstallSource arg_install_source = INSTALL_SOURCE_AUTO;
71
char *arg_efi_boot_option_description = NULL;
72
bool arg_efi_boot_option_description_with_device = false;
73
bool arg_dry_run = false;
74
ImagePolicy *arg_image_policy = NULL;
75
bool arg_varlink = false;
76
bool arg_secure_boot_auto_enroll = false;
77
char *arg_certificate = NULL;
78
CertificateSourceType arg_certificate_source_type = OPENSSL_CERTIFICATE_SOURCE_FILE;
79
char *arg_certificate_source = NULL;
80
char *arg_private_key = NULL;
81
KeySourceType arg_private_key_source_type = OPENSSL_KEY_SOURCE_FILE;
82
char *arg_private_key_source = NULL;
83

84
STATIC_DESTRUCTOR_REGISTER(arg_esp_path, freep);
305✔
85
STATIC_DESTRUCTOR_REGISTER(arg_xbootldr_path, freep);
305✔
86
STATIC_DESTRUCTOR_REGISTER(arg_install_layout, freep);
305✔
87
STATIC_DESTRUCTOR_REGISTER(arg_entry_token, freep);
305✔
88
STATIC_DESTRUCTOR_REGISTER(arg_root, freep);
305✔
89
STATIC_DESTRUCTOR_REGISTER(arg_image, freep);
305✔
90
STATIC_DESTRUCTOR_REGISTER(arg_efi_boot_option_description, freep);
305✔
91
STATIC_DESTRUCTOR_REGISTER(arg_image_policy, image_policy_freep);
305✔
92
STATIC_DESTRUCTOR_REGISTER(arg_certificate, freep);
305✔
93
STATIC_DESTRUCTOR_REGISTER(arg_certificate_source, freep);
305✔
94
STATIC_DESTRUCTOR_REGISTER(arg_private_key, freep);
305✔
95
STATIC_DESTRUCTOR_REGISTER(arg_private_key_source, freep);
305✔
96

97
static const char* const install_source_table[_INSTALL_SOURCE_MAX] = {
98
        [INSTALL_SOURCE_IMAGE] = "image",
99
        [INSTALL_SOURCE_HOST]  = "host",
100
        [INSTALL_SOURCE_AUTO]  = "auto",
101
};
102

103
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(install_source, InstallSource);
×
104

105
int acquire_esp(int unprivileged_mode,
261✔
106
                bool graceful,
107
                int *ret_fd,
108
                uint32_t *ret_part,
109
                uint64_t *ret_pstart,
110
                uint64_t *ret_psize,
111
                sd_id128_t *ret_uuid,
112
                dev_t *ret_devid) {
113

114
        _cleanup_free_ char *np = NULL;
261✔
115
        int r;
261✔
116

117
        /* Find the ESP, and log about errors. Note that find_esp_and_warn() will log in all error cases on
118
         * its own, except for ENOKEY (which is good, we want to show our own message in that case,
119
         * suggesting use of --esp-path=) and EACCESS (only when we request unprivileged mode; in this case
120
         * we simply eat up the error here, so that --list and --status work too, without noise about
121
         * this). */
122

123
        r = find_esp_and_warn_full(arg_root, arg_esp_path, unprivileged_mode, &np, ret_fd, ret_part, ret_pstart, ret_psize, ret_uuid, ret_devid);
261✔
124
        if (r == -ENOKEY) {
261✔
125
                if (graceful)
5✔
126
                        return log_full_errno(arg_quiet ? LOG_DEBUG : LOG_INFO, r,
×
127
                                              "Couldn't find EFI system partition, skipping.");
128

129
                return log_error_errno(r,
5✔
130
                                       "Couldn't find EFI system partition. It is recommended to mount it to /boot/ or /efi/.\n"
131
                                       "Alternatively, use --esp-path= to specify path to mount point.");
132
        }
133
        if (r < 0)
256✔
134
                return r;
135

136
        free_and_replace(arg_esp_path, np);
255✔
137
        log_debug("Using EFI System Partition at %s.", arg_esp_path);
255✔
138

139
        return 1; /* for symmetry with acquire_xbootldr() below: found */
140
}
141

142
int acquire_xbootldr(
205✔
143
                int unprivileged_mode,
144
                int *ret_fd,
145
                sd_id128_t *ret_uuid,
146
                dev_t *ret_devid) {
147

148
        int r;
205✔
149

150
        _cleanup_free_ char *np = NULL;
205✔
151
        _cleanup_close_ int fd = -EBADF;
205✔
152
        r = find_xbootldr_and_warn_full(
245✔
153
                        arg_root,
154
                        arg_xbootldr_path,
155
                        unprivileged_mode,
156
                        &np,
157
                        ret_fd ? &fd : NULL,
158
                        ret_uuid,
159
                        ret_devid);
160
        if (r == -ENOKEY || (r >= 0 && arg_esp_path && path_equal(np, arg_esp_path))) {
205✔
161

162
                if (arg_esp_path)
158✔
163
                        log_debug("Didn't find an XBOOTLDR partition, using the ESP as $BOOT.");
157✔
164
                else
165
                        log_debug("Found neither an XBOOTLDR partition, nor an ESP.");
1✔
166

167
                arg_xbootldr_path = mfree(arg_xbootldr_path);
158✔
168

169
                if (ret_fd)
158✔
170
                        *ret_fd = -EBADF;
137✔
171
                if (ret_uuid)
158✔
172
                        *ret_uuid = SD_ID128_NULL;
16✔
173
                if (ret_devid)
158✔
174
                        *ret_devid = 0;
21✔
175

176
                return 0; /* not found */
158✔
177
        }
178
        if (r < 0)
46✔
179
                return r;
180

181
        free_and_replace(arg_xbootldr_path, np);
46✔
182
        log_debug("Using XBOOTLDR partition at %s as $BOOT.", arg_xbootldr_path);
46✔
183

184
        if (ret_fd)
46✔
185
                *ret_fd = TAKE_FD(fd);
28✔
186

187
        return 1; /* found */
188
}
189

190
static int print_loader_or_stub_path(void) {
8✔
191
        _cleanup_free_ char *p = NULL;
8✔
192
        sd_id128_t uuid;
8✔
193
        int r;
8✔
194

195
        if (arg_print_loader_path) {
8✔
196
                r = efi_loader_get_device_part_uuid(&uuid);
1✔
197
                if (r == -ENOENT)
1✔
198
                        return log_error_errno(r, "No loader partition UUID passed.");
×
199
                if (r < 0)
1✔
200
                        return log_error_errno(r, "Unable to determine loader partition UUID: %m");
×
201

202
                r = efi_get_variable_path(EFI_LOADER_VARIABLE_STR("LoaderImageIdentifier"), &p);
1✔
203
                if (r == -ENOENT)
1✔
204
                        return log_error_errno(r, "No loader EFI binary path passed.");
×
205
                if (r < 0)
1✔
206
                        return log_error_errno(r, "Unable to determine loader EFI binary path: %m");
×
207
        } else {
208
                assert(arg_print_stub_path);
7✔
209

210
                r = efi_stub_get_device_part_uuid(&uuid);
7✔
211
                if (r == -ENOENT)
7✔
212
                        return log_error_errno(r, "No stub partition UUID passed.");
×
213
                if (r < 0)
7✔
214
                        return log_error_errno(r, "Unable to determine stub partition UUID: %m");
×
215

216
                r = efi_get_variable_path(EFI_LOADER_VARIABLE_STR("StubImageIdentifier"), &p);
7✔
217
                if (r == -ENOENT)
7✔
218
                        return log_error_errno(r, "No stub EFI binary path passed.");
×
219
                if (r < 0)
7✔
220
                        return log_error_errno(r, "Unable to determine stub EFI binary path: %m");
×
221
        }
222

223
        sd_id128_t esp_uuid;
8✔
224
        r = acquire_esp(/* unprivileged_mode= */ false,
8✔
225
                        /* graceful= */ false,
226
                        /* ret_fd= */ NULL,
227
                        /* ret_part= */ NULL,
228
                        /* ret_pstart= */ NULL,
229
                        /* ret_psize= */ NULL,
230
                        &esp_uuid,
231
                        /* ret_devid= */ NULL);
232
        if (r < 0)
8✔
233
                return r;
234

235
        const char *found_path = NULL;
8✔
236
        if (sd_id128_equal(esp_uuid, uuid))
8✔
237
                found_path = arg_esp_path;
8✔
238
        else if (arg_print_stub_path) { /* In case of the stub, also look for things in the xbootldr partition */
×
239
                sd_id128_t xbootldr_uuid;
×
240

241
                r = acquire_xbootldr(/* unprivileged_mode= */ false,
×
242
                                     /* ret_fd= */ NULL,
243
                                     &xbootldr_uuid,
244
                                     /* ret_devid= */ NULL);
245
                if (r < 0)
×
246
                        return r;
×
247

248
                if (sd_id128_equal(xbootldr_uuid, uuid))
×
249
                        found_path = arg_xbootldr_path;
×
250
        }
251

252
        if (!found_path)
8✔
253
                return log_error_errno(SYNTHETIC_ERRNO(ENOENT), "Failed to discover partition " SD_ID128_FORMAT_STR " among mounted boot partitions.", SD_ID128_FORMAT_VAL(uuid));
×
254

255
        _cleanup_free_ char *j = path_join(found_path, p);
16✔
256
        if (!j)
8✔
257
                return log_oom();
×
258

259
        puts(j);
8✔
260
        return 0;
261
}
262

263
GracefulMode arg_graceful(void) {
290✔
264
        static bool chroot_checked = false;
290✔
265

266
        if (!chroot_checked && running_in_chroot() > 0) {
290✔
267
                if (_arg_graceful == ARG_GRACEFUL_NO)
×
268
                        log_full(arg_quiet ? LOG_DEBUG : LOG_INFO, "Running in a chroot, enabling --graceful.");
×
269

270
                _arg_graceful = ARG_GRACEFUL_FORCE;
×
271
        }
272

273
        chroot_checked = true;
290✔
274

275
        return _arg_graceful;
290✔
276
}
277

278
static int help(void) {
3✔
279
        _cleanup_free_ char *link = NULL;
3✔
280
        int r;
3✔
281

282
        pager_open(arg_pager_flags);
3✔
283

284
        r = terminal_urlify_man("bootctl", "1", &link);
3✔
285
        if (r < 0)
3✔
286
                return log_oom();
×
287

288
        static const char *const verb_groups[] = {
3✔
289
                "Generic EFI Firmware/Boot Loader Commands",
290
                "Boot Loader Specification Commands",
291
                "Boot Loader Interface Commands",
292
                "systemd-boot Commands",
293
                "Kernel Image Commands",
294
        };
295

296
        static const char *const option_groups[] = {
3✔
297
                "Block Device Discovery Commands",
298
                "Options",
299
        };
300

301
        _cleanup_(table_unref_many) Table *verb_tables[ELEMENTSOF(verb_groups) + 1] = {};
3✔
302
        _cleanup_(table_unref_many) Table *option_tables[ELEMENTSOF(option_groups) + 1] = {};
3✔
303

304
        for (size_t i = 0; i < ELEMENTSOF(verb_groups); i++) {
18✔
305
                r = verbs_get_help_table_group(verb_groups[i], &verb_tables[i]);
15✔
306
                if (r < 0)
15✔
307
                        return r;
308
        }
309

310
        for (size_t i = 0; i < ELEMENTSOF(option_groups); i++) {
9✔
311
                r = option_parser_get_help_table_group(option_groups[i], &option_tables[i]);
6✔
312
                if (r < 0)
6✔
313
                        return r;
314
        }
315

316
        (void) table_sync_column_widths(0,
3✔
317
                                        verb_tables[0], verb_tables[1], verb_tables[2],
318
                                        verb_tables[3], verb_tables[4],
319
                                        option_tables[0], option_tables[1]);
320

321
        printf("%s [OPTIONS...] COMMAND ...\n"
6✔
322
               "\n%sControl EFI firmware boot settings and manage boot loader.%s\n",
323
               program_invocation_short_name,
324
               ansi_highlight(),
325
               ansi_normal());
326

327
        for (size_t i = 0; i < ELEMENTSOF(verb_groups); i++) {
18✔
328
                printf("\n%s%s:%s\n", ansi_underline(), verb_groups[i], ansi_normal());
45✔
329

330
                r = table_print_or_warn(verb_tables[i]);
15✔
331
                if (r < 0)
15✔
332
                        return r;
333
        }
334

335
        for (size_t i = 0; i < ELEMENTSOF(option_groups); i++) {
9✔
336
                printf("\n%s%s:%s\n", ansi_underline(), option_groups[i], ansi_normal());
18✔
337

338
                r = table_print_or_warn(option_tables[i]);
6✔
339
                if (r < 0)
6✔
340
                        return r;
341
        }
342

343
        printf("\nSee the %s for details.\n", link);
3✔
344
        return 0;
345
}
346

347
VERB_COMMON_HELP(help);
×
348

349
VERB_GROUP("Generic EFI Firmware/Boot Loader Commands");
350

351
VERB_SCOPE(, verb_status, "status", NULL, VERB_ANY, 1, VERB_DEFAULT,
352
           "Show status of installed boot loader and EFI variables");
353

354
VERB_SCOPE(, verb_reboot_to_firmware, "reboot-to-firmware", "[BOOL]", VERB_ANY, 2, 0,
355
           "Query or set reboot-to-firmware EFI flag");
356

357
VERB_GROUP("Boot Loader Specification Commands");
358

359
VERB_SCOPE_NOARG(, verb_list, "list",
360
           "List boot loader entries");
361

362
VERB_SCOPE(, verb_unlink, "unlink", "ID", 2, 2, 0,
363
           "Remove boot loader entry");
364

365
VERB_SCOPE_NOARG(, verb_cleanup, "cleanup",
366
           "Remove files in ESP not referenced in any boot entry");
367

368
VERB_GROUP("Boot Loader Interface Commands");
369

370
VERB_SCOPE(, verb_set_efivar, "set-default", "ID", 2, 2, 0,
371
           "Set default boot loader entry");
372

373
VERB_SCOPE(, verb_set_efivar, "set-oneshot", "ID", 2, 2, 0,
374
           "Set default boot loader entry, for next boot only");
375

376
VERB_SCOPE(, verb_set_efivar, "set-sysfail", "ID", 2, 2, 0,
377
           "Set boot loader entry used in case of a system failure");
378

379
VERB_SCOPE(, verb_set_efivar, "set-timeout", "SECONDS", 2, 2, 0,
380
           "Set the menu timeout");
381

382
VERB_SCOPE(, verb_set_efivar, "set-timeout-oneshot", "SECONDS", 2, 2, 0,
383
           "Set the menu timeout for the next boot only");
384

385
VERB_SCOPE(, verb_set_efivar, "set-preferred", "ID", 2, 2, 0,
386
           /* help= */ NULL);
387

388
VERB_GROUP("systemd-boot Commands");
389

390
VERB_SCOPE(, verb_install, "install", NULL, VERB_ANY, 1, 0,
391
           "Install systemd-boot to the ESP and EFI variables");
392

393
VERB_SCOPE(, verb_install, "update", NULL, VERB_ANY, 1, 0,
394
           "Update systemd-boot in the ESP and EFI variables");
395

396
VERB_SCOPE_NOARG(, verb_remove, "remove",
397
           "Remove systemd-boot from the ESP and EFI variables");
398

399
VERB_SCOPE_NOARG(, verb_is_installed, "is-installed",
400
           "Test whether systemd-boot is installed in the ESP");
401

402
VERB_SCOPE_NOARG(, verb_random_seed, "random-seed",
403
           "Initialize or refresh random seed in ESP and EFI variables");
404

405
VERB_GROUP("Kernel Image Commands");
406

407
VERB_SCOPE(, verb_kernel_identify, "kernel-identify", "KERNEL-IMAGE", 2, 2, 0,
408
           "Identify kernel image type");
409

410
VERB_SCOPE(, verb_kernel_inspect, "kernel-inspect", "KERNEL-IMAGE", 2, 2, 0,
411
           "Prints details about the kernel image");
412

413
static int parse_argv(int argc, char *argv[], char ***ret_args) {
305✔
414
        int r;
305✔
415

416
        assert(argc >= 0);
305✔
417
        assert(argv);
305✔
418

419
        OptionParser state = { argc, argv };
305✔
420
        const char *arg;
305✔
421

422
        FOREACH_OPTION(&state, c, &arg, /* on_error= */ return c)
1,355✔
423
                switch (c) {
446✔
424

425
                OPTION_GROUP("Block Device Discovery Commands"):
426
                        break;
427

428
                OPTION('p', "print-esp-path", NULL, "Print path to the EFI System Partition mount point"): {}
48✔
429
                OPTION_LONG("print-path", NULL, /* help= */ NULL):  /* Compatibility alias */
48✔
430
                        arg_print_esp_path = true;
48✔
431
                        break;
48✔
432

433
                OPTION('x', "print-boot-path", NULL, "Print path to the $BOOT partition mount point"):
9✔
434
                        arg_print_dollar_boot_path = true;
9✔
435
                        break;
9✔
436

437
                OPTION_LONG("print-loader-path", NULL, "Print path to currently booted boot loader binary"):
1✔
438
                        arg_print_loader_path = true;
1✔
439
                        break;
1✔
440

441
                OPTION_LONG("print-stub-path", NULL, "Print path to currently booted unified kernel binary"):
7✔
442
                        arg_print_stub_path = true;
7✔
443
                        break;
7✔
444

445
                OPTION('R', "print-root-device", NULL,
9✔
446
                       "Print path to the block device node backing the root file system"
447
                       " (returns e.g. /dev/nvme0n1p5)"): {}
9✔
448
                OPTION_HELP_VERBATIM("-RR",
9✔
449
                                     "Print path to the whole disk block device node backing the root FS"
450
                                     " (returns e.g. /dev/nvme0n1)"):
451
                        arg_print_root_device++;
9✔
452
                        break;
9✔
453

454
                OPTION_LONG("print-efi-architecture", NULL, "Print the local EFI architecture string"):
1✔
455
                        arg_print_efi_architecture = true;
1✔
456
                        break;
1✔
457

458
                OPTION_GROUP("Options"):
459
                        break;
460

461
                OPTION_COMMON_HELP:
3✔
462
                        return help();
3✔
463

464
                OPTION_COMMON_VERSION:
3✔
465
                        return version();
3✔
466

467
                OPTION_LONG("esp-path", "PATH", "Path to the EFI System Partition (ESP)"): {}
3✔
468
                OPTION_LONG("path", "PATH", /* help= */ NULL):  /* Compatibility alias */
3✔
469
                        r = free_and_strdup(&arg_esp_path, arg);
3✔
470
                        if (r < 0)
3✔
471
                                return log_oom();
×
472
                        break;
473

474
                OPTION_LONG("boot-path", "PATH", "Path to the $BOOT partition"):
3✔
475
                        r = free_and_strdup(&arg_xbootldr_path, arg);
3✔
476
                        if (r < 0)
3✔
477
                                return log_oom();
×
478
                        break;
479

480
                OPTION_LONG("root", "PATH", "Operate on an alternate filesystem root"):
34✔
481
                        r = parse_path_argument(arg, /* suppress_root= */ true, &arg_root);
34✔
482
                        if (r < 0)
34✔
483
                                return r;
484
                        break;
485

486
                OPTION_LONG("image", "PATH", "Operate on disk image as filesystem root"):
29✔
487
                        r = parse_path_argument(arg, /* suppress_root= */ false, &arg_image);
29✔
488
                        if (r < 0)
29✔
489
                                return r;
490
                        break;
491

492
                OPTION_LONG("image-policy", "POLICY", "Specify disk image dissection policy"):
×
493
                        r = parse_image_policy_argument(arg, &arg_image_policy);
×
494
                        if (r < 0)
×
495
                                return r;
496
                        break;
497

498
                OPTION_LONG("install-source", "SOURCE",
×
499
                            "Where to pick files when using --root=/--image= (auto, image, host)"): {
500
                        InstallSource is = install_source_from_string(arg);
×
501
                        if (is < 0)
×
502
                                return log_error_errno(is, "Unexpected parameter for --install-source=: %s", arg);
×
503

504
                        arg_install_source = is;
×
505
                        break;
×
506
                }
507

508
                OPTION_LONG("variables", "BOOL", "Whether to modify EFI variables"):
119✔
509
                        r = parse_tristate_argument_with_auto("--variables=", arg, &arg_touch_variables);
119✔
510
                        if (r < 0)
119✔
511
                                return r;
512
#if !ENABLE_EFI
513
                        if (arg_touch_variables > 0)
514
                                return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
515
                                                       "Compiled without support for EFI, --variables=%s cannot be specified.", arg);
516
#endif
517
                        break;
518

519
                OPTION_LONG("no-variables", NULL, /* help= */ NULL):  /* Compatibility alias */
×
520
                        arg_touch_variables = false;
×
521
                        break;
×
522

523
                OPTION_LONG("random-seed", "BOOL", "Whether to create random-seed file during install"):
×
524
                        r = parse_boolean_argument("--random-seed=", arg, &arg_install_random_seed);
×
525
                        if (r < 0)
×
526
                                return r;
527
                        break;
528

529
                OPTION_COMMON_NO_PAGER:
×
530
                        arg_pager_flags |= PAGER_DISABLE;
×
531
                        break;
×
532

533
                OPTION_LONG("graceful", NULL,
137✔
534
                            "Don't fail when the ESP cannot be found or EFI variables cannot be written"):
535
                        _arg_graceful = ARG_GRACEFUL_YES;
137✔
536
                        break;
137✔
537

538
                OPTION('q', "quiet", NULL, "Suppress output"):
6✔
539
                        arg_quiet = true;
6✔
540
                        break;
6✔
541

542
                OPTION_LONG("entry-token", "TOKEN",
×
543
                            "Entry token to use for this installation"
544
                            " (machine-id, os-id, os-image-id, auto, literal:…)"):
545
                        r = parse_boot_entry_token_type(arg, &arg_entry_token_type, &arg_entry_token);
×
546
                        if (r < 0)
×
547
                                return r;
548
                        break;
549

550
                OPTION_LONG("make-entry-directory", "yes|no|auto", "Create $BOOT/ENTRY-TOKEN/ directory"): {}
13✔
551
                OPTION_LONG("make-machine-id-directory", "BOOL", /* help= */ NULL):  /* Compatibility alias */
13✔
552
                        if (streq(arg, "auto"))  /* retained for backwards compatibility */
13✔
553
                                arg_make_entry_directory = -1; /* yes if machine-id is permanent */
×
554
                        else {
555
                                r = parse_boolean_argument("--make-entry-directory=", arg, NULL);
13✔
556
                                if (r < 0)
13✔
557
                                        return r;
558

559
                                arg_make_entry_directory = r;
13✔
560
                        }
561
                        break;
562

563
                OPTION_COMMON_JSON:
6✔
564
                        r = parse_json_argument(arg, &arg_json_format_flags);
6✔
565
                        if (r <= 0)
6✔
566
                                return r;
567
                        break;
568

569
                OPTION_LONG("all-architectures", NULL, "Install all supported EFI architectures"):
12✔
570
                        arg_arch_all = true;
12✔
571
                        break;
12✔
572

573
                OPTION_LONG("efi-boot-option-description", "DESCRIPTION",
×
574
                            "Description of the entry in the boot option list"):
575
                        if (!string_is_safe(arg, STRING_ALLOW_BACKSLASHES|STRING_ALLOW_QUOTES|STRING_ALLOW_GLOBS)) {
×
576
                                _cleanup_free_ char *escaped = cescape(arg);
×
577
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
578
                                                       "Invalid --efi-boot-option-description=: %s", strna(escaped));
579
                        }
580
                        if (strlen(arg) > EFI_BOOT_OPTION_DESCRIPTION_MAX)
×
581
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
582
                                                       "--efi-boot-option-description= too long: %zu > %zu",
583
                                                       strlen(arg), EFI_BOOT_OPTION_DESCRIPTION_MAX);
584
                        r = free_and_strdup_warn(&arg_efi_boot_option_description, arg);
×
585
                        if (r < 0)
×
586
                                return r;
587
                        break;
588

589
                OPTION_LONG("efi-boot-option-description-with-device", "BOOL",
×
590
                            "Suffix description with disk vendor/model/serial"):
591
                        r = parse_boolean_argument("--efi-boot-option-description-with-device=", arg,
×
592
                                                   &arg_efi_boot_option_description_with_device);
593
                        if (r < 0)
×
594
                                return r;
595
                        break;
596

597
                OPTION_LONG("dry-run", NULL, "Dry run (unlink and cleanup)"):
×
598
                        arg_dry_run = true;
×
599
                        break;
×
600

601
                OPTION_LONG("secure-boot-auto-enroll", "BOOL", "Set up secure boot auto-enrollment"):
1✔
602
                        r = parse_boolean_argument("--secure-boot-auto-enroll=", arg,
1✔
603
                                                   &arg_secure_boot_auto_enroll);
604
                        if (r < 0)
1✔
605
                                return r;
606
                        break;
607

608
                OPTION_LONG("private-key", "PATH|URI",
1✔
609
                            "Private key for Secure Boot auto-enrollment"):
610
                        r = free_and_strdup_warn(&arg_private_key, arg);
1✔
611
                        if (r < 0)
1✔
612
                                return r;
613
                        break;
614

615
                OPTION_LONG("private-key-source", "SOURCE",
×
616
                            "Specify how to use the private key "
617
                            "(file, provider:PROVIDER, engine:ENGINE)"):
618
                        r = parse_openssl_key_source_argument(arg,
×
619
                                                              &arg_private_key_source,
620
                                                              &arg_private_key_source_type);
621
                        if (r < 0)
×
622
                                return r;
623
                        break;
624

625
                OPTION_LONG("certificate", "PATH|URI",
1✔
626
                            "PEM certificate to use when setting up Secure Boot auto-enrollment, "
627
                            "or a provider specific designation if --certificate-source= is used"):
628
                        r = free_and_strdup_warn(&arg_certificate, arg);
1✔
629
                        if (r < 0)
1✔
630
                                return r;
631
                        break;
632

633
                OPTION_LONG("certificate-source", "SOURCE",
×
634
                            "Specify how to interpret the certificate from --certificate=. "
635
                            "Allows the certificate to be loaded from an OpenSSL provider "
636
                            "(file, provider:PROVIDER)"):
637
                        r = parse_openssl_certificate_source_argument(arg,
×
638
                                                                      &arg_certificate_source,
639
                                                                      &arg_certificate_source_type);
640
                        if (r < 0)
×
641
                                return r;
642
                        break;
643
                }
644

645
        char **args = option_parser_get_args(&state);
299✔
646

647
        if (!!arg_print_esp_path + !!arg_print_dollar_boot_path + (arg_print_root_device > 0) + arg_print_loader_path + arg_print_stub_path + arg_print_efi_architecture > 1)
299✔
648
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
649
                                                       "--print-esp-path/-p, --print-boot-path/-x, --print-root-device=/-R, --print-loader-path, --print-stub-path, --print-efi-architecture cannot be combined.");
650

651
        if ((arg_root || arg_image) && args[0] && !STR_IN_SET(args[0], "status", "list",
299✔
652
                        "install", "update", "remove", "is-installed", "random-seed", "unlink", "cleanup"))
653
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
654
                                       "Options --root= and --image= are not supported with verb %s.",
655
                                       args[0]);
656

657
        if (arg_root && arg_image)
299✔
658
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Please specify either --root= or --image=, the combination of both is not supported.");
×
659

660
        if (arg_install_source != INSTALL_SOURCE_AUTO && !arg_root && !arg_image)
299✔
661
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--install-from-host is only supported with --root= or --image=.");
×
662

663
        if (arg_dry_run && args[0] && !STR_IN_SET(args[0], "unlink", "cleanup"))
299✔
664
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--dry-run is only supported with --unlink or --cleanup");
×
665

666
        if (arg_secure_boot_auto_enroll) {
299✔
667
#if HAVE_OPENSSL
668
                if (!arg_certificate)
1✔
669
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Secure boot auto-enrollment requested but no certificate provided.");
×
670

671
                if (!arg_private_key)
1✔
672
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Secure boot auto-enrollment requested but no private key provided.");
×
673
#else
674
                return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Secure boot auto-enrollment requested but OpenSSL support is disabled.");
675
#endif
676
        }
677

678
        r = sd_varlink_invocation(SD_VARLINK_ALLOW_ACCEPT);
299✔
679
        if (r < 0)
299✔
680
                return log_error_errno(r, "Failed to check if invoked in Varlink mode: %m");
×
681
        if (r > 0) {
299✔
682
                arg_varlink = true;
15✔
683
                arg_pager_flags |= PAGER_DISABLE;
15✔
684
        }
685

686
        *ret_args = args;
299✔
687
        return 1;
299✔
688
}
689

690
static int vl_server(void) {
15✔
691
        _cleanup_(sd_varlink_server_unrefp) sd_varlink_server *varlink_server = NULL;
15✔
692
        int r;
15✔
693

694
        /* Invocation as Varlink service */
695

696
        r = varlink_server_new(
15✔
697
                        &varlink_server,
698
                        SD_VARLINK_SERVER_ROOT_ONLY|SD_VARLINK_SERVER_ALLOW_FD_PASSING_INPUT,
699
                        /* userdata= */ NULL);
700
        if (r < 0)
15✔
701
                return log_error_errno(r, "Failed to allocate Varlink server: %m");
×
702

703
        r = sd_varlink_server_add_interface(varlink_server, &vl_interface_io_systemd_BootControl);
15✔
704
        if (r < 0)
15✔
705
                return log_error_errno(r, "Failed to add Varlink interface: %m");
×
706

707
        r = sd_varlink_server_bind_method_many(
15✔
708
                        varlink_server,
709
                        "io.systemd.BootControl.ListBootEntries",     vl_method_list_boot_entries,
710
                        "io.systemd.BootControl.SetRebootToFirmware", vl_method_set_reboot_to_firmware,
711
                        "io.systemd.BootControl.GetRebootToFirmware", vl_method_get_reboot_to_firmware,
712
                        "io.systemd.BootControl.Install",             vl_method_install);
713
        if (r < 0)
15✔
714
                return log_error_errno(r, "Failed to bind Varlink methods: %m");
×
715

716
        r = sd_varlink_server_loop_auto(varlink_server);
15✔
717
        if (r < 0)
15✔
718
                return log_error_errno(r, "Failed to run Varlink event loop: %m");
×
719

720
        return 0;
721
}
722

723
static int run(int argc, char *argv[]) {
305✔
724
        _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
305✔
725
        _cleanup_(umount_and_freep) char *mounted_dir = NULL;
305✔
726
        int r;
305✔
727

728
        log_setup();
305✔
729

730
        char **args = NULL;
305✔
731
        r = parse_argv(argc, argv, &args);
305✔
732
        if (r <= 0)
305✔
733
                return r;
734

735
        if (arg_varlink)
299✔
736
                return vl_server();
15✔
737

738
        if (arg_print_root_device > 0) {
284✔
739
                _cleanup_free_ char *path = NULL;
6✔
740
                dev_t devno;
6✔
741

742
                r = blockdev_get_root(LOG_ERR, &devno);
6✔
743
                if (r < 0)
6✔
744
                        return r;
745
                if (r == 0) {
6✔
746
                        log_error("Root file system not backed by a (single) whole block device.");
×
747
                        return 80; /* some recognizable error code */
×
748
                }
749

750
                if (arg_print_root_device > 1) {
6✔
751
                        r = block_get_whole_disk(devno, &devno);
3✔
752
                        if (r < 0)
3✔
753
                                log_debug_errno(r, "Unable to find whole block device for root block device, ignoring: %m");
×
754
                }
755

756
                r = device_path_make_canonical(S_IFBLK, devno, &path);
6✔
757
                if (r < 0)
6✔
758
                        return log_error_errno(r,
×
759
                                               "Failed to format canonical device path for devno '" DEVNUM_FORMAT_STR "': %m",
760
                                               DEVNUM_FORMAT_VAL(devno));
761

762
                puts(path);
6✔
763
                return 0;
764
        }
765

766
        if (arg_print_loader_path || arg_print_stub_path)
278✔
767
                return print_loader_or_stub_path();
8✔
768

769
        if (arg_print_efi_architecture) {
270✔
770
                printf("%s\n", get_efi_arch());
1✔
771
                return 0;
772
        }
773

774
        /* Open up and mount the image */
775
        if (arg_image) {
269✔
776
                assert(!arg_root);
26✔
777

778
                r = mount_image_privately_interactively(
26✔
779
                                arg_image,
780
                                arg_image_policy,
781
                                DISSECT_IMAGE_GENERIC_ROOT |
782
                                DISSECT_IMAGE_USR_NO_ROOT |
783
                                DISSECT_IMAGE_RELAX_VAR_CHECK |
784
                                DISSECT_IMAGE_ALLOW_USERSPACE_VERITY,
785
                                &mounted_dir,
786
                                /* ret_dir_fd= */ NULL,
787
                                &loop_device);
788
                if (r < 0)
26✔
789
                        return r;
790

791
                arg_root = strdup(mounted_dir);
26✔
792
                if (!arg_root)
26✔
793
                        return log_oom();
×
794
        }
795

796
        return dispatch_verb_with_args(args, NULL);
269✔
797
}
798

799
DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);
305✔
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