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

systemd / systemd / 29132483780

10 Jul 2026 08:43PM UTC coverage: 72.912% (+0.2%) from 72.702%
29132483780

push

github

bluca
man: run forgotten 'update-man-rules'

344600 of 472622 relevant lines covered (72.91%)

1365091.83 hits per line

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

75.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-link.h"
13
#include "bootctl-random-seed.h"
14
#include "bootctl-reboot-to-firmware.h"
15
#include "bootctl-set-efivar.h"
16
#include "bootctl-status.h"
17
#include "bootctl-uki.h"
18
#include "bootctl-unlink.h"
19
#include "bootctl-util.h"
20
#include "bootspec-util.h"
21
#include "build.h"
22
#include "crypto-util.h"
23
#include "devnum-util.h"
24
#include "dissect-image.h"
25
#include "dlopen-note.h"
26
#include "efi-loader.h"
27
#include "efivars.h"
28
#include "escape.h"
29
#include "fd-util.h"
30
#include "find-esp.h"
31
#include "format-table.h"
32
#include "image-policy.h"
33
#include "log.h"
34
#include "loop-util.h"
35
#include "main-func.h"
36
#include "mount-util.h"
37
#include "options.h"
38
#include "pager.h"
39
#include "parse-argument.h"
40
#include "parse-util.h"
41
#include "path-util.h"
42
#include "pretty-print.h"
43
#include "string-table.h"
44
#include "string-util.h"
45
#include "strv.h"
46
#include "varlink-io.systemd.BootControl.h"
47
#include "varlink-io.systemd.SysUpdate.Notify.h"
48
#include "varlink-util.h"
49
#include "verbs.h"
50
#include "virt.h"
51

52
static GracefulMode _arg_graceful = ARG_GRACEFUL_NO;
53

54
char *arg_esp_path = NULL;
55
char *arg_xbootldr_path = NULL;
56
bool arg_print_esp_path = false;
57
bool arg_print_dollar_boot_path = false;
58
bool arg_print_loader_path = false;
59
bool arg_print_stub_path = false;
60
bool arg_print_efi_architecture = false;
61
unsigned arg_print_root_device = 0;
62
int arg_touch_variables = -1;
63
bool arg_install_random_seed = true;
64
PagerFlags arg_pager_flags = 0;
65
bool arg_quiet = false;
66
int arg_make_entry_directory = false; /* tri-state: < 0 for automatic logic */
67
sd_id128_t arg_machine_id = SD_ID128_NULL;
68
char *arg_install_layout = NULL;
69
BootEntryTokenType arg_entry_token_type = BOOT_ENTRY_TOKEN_AUTO;
70
char *arg_entry_token = NULL;
71
sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF;
72
bool arg_arch_all = false;
73
char *arg_root = NULL;
74
char *arg_image = NULL;
75
InstallSource arg_install_source = INSTALL_SOURCE_AUTO;
76
char *arg_efi_boot_option_description = NULL;
77
bool arg_efi_boot_option_description_with_device = false;
78
bool arg_dry_run = false;
79
ImagePolicy *arg_image_policy = NULL;
80
bool arg_varlink = false;
81
bool arg_secure_boot_auto_enroll = false;
82
char *arg_certificate = NULL;
83
CertificateSourceType arg_certificate_source_type = OPENSSL_CERTIFICATE_SOURCE_FILE;
84
char *arg_certificate_source = NULL;
85
char *arg_private_key = NULL;
86
KeySourceType arg_private_key_source_type = OPENSSL_KEY_SOURCE_FILE;
87
char *arg_private_key_source = NULL;
88
bool arg_oldest = false;
89
uint64_t arg_keep_free = KEEP_FREE_BYTES_DEFAULT;
90
char *arg_entry_title = NULL;
91
char *arg_entry_version = NULL;
92
uint64_t arg_entry_commit = 0;
93
char **arg_extras = NULL;
94
unsigned arg_tries_left = UINT_MAX;
95

96
STATIC_DESTRUCTOR_REGISTER(arg_esp_path, freep);
293✔
97
STATIC_DESTRUCTOR_REGISTER(arg_xbootldr_path, freep);
293✔
98
STATIC_DESTRUCTOR_REGISTER(arg_install_layout, freep);
293✔
99
STATIC_DESTRUCTOR_REGISTER(arg_entry_token, freep);
293✔
100
STATIC_DESTRUCTOR_REGISTER(arg_root, freep);
293✔
101
STATIC_DESTRUCTOR_REGISTER(arg_image, freep);
293✔
102
STATIC_DESTRUCTOR_REGISTER(arg_efi_boot_option_description, freep);
293✔
103
STATIC_DESTRUCTOR_REGISTER(arg_image_policy, image_policy_freep);
293✔
104
STATIC_DESTRUCTOR_REGISTER(arg_certificate, freep);
293✔
105
STATIC_DESTRUCTOR_REGISTER(arg_certificate_source, freep);
293✔
106
STATIC_DESTRUCTOR_REGISTER(arg_private_key, freep);
293✔
107
STATIC_DESTRUCTOR_REGISTER(arg_private_key_source, freep);
293✔
108
STATIC_DESTRUCTOR_REGISTER(arg_entry_title, freep);
293✔
109
STATIC_DESTRUCTOR_REGISTER(arg_entry_version, freep);
293✔
110
STATIC_DESTRUCTOR_REGISTER(arg_extras, strv_freep);
293✔
111

112
static const char* const install_source_table[_INSTALL_SOURCE_MAX] = {
113
        [INSTALL_SOURCE_IMAGE] = "image",
114
        [INSTALL_SOURCE_HOST]  = "host",
115
        [INSTALL_SOURCE_AUTO]  = "auto",
116
};
117

118
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(install_source, InstallSource);
×
119

120
int acquire_esp(int unprivileged_mode,
230✔
121
                bool graceful,
122
                int *ret_fd,
123
                uint32_t *ret_part,
124
                uint64_t *ret_pstart,
125
                uint64_t *ret_psize,
126
                sd_id128_t *ret_uuid,
127
                dev_t *ret_devid) {
128

129
        _cleanup_free_ char *np = NULL;
230✔
130
        int r;
230✔
131

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

138
        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);
230✔
139
        if (r == -ENOKEY) {
230✔
140
                if (graceful)
5✔
141
                        return log_full_errno(arg_quiet ? LOG_DEBUG : LOG_INFO, r,
×
142
                                              "Couldn't find EFI system partition, skipping.");
143

144
                return log_error_errno(r,
5✔
145
                                       "Couldn't find EFI system partition. It is recommended to mount it to /boot/ or /efi/.\n"
146
                                       "Alternatively, use --esp-path= to specify path to mount point.");
147
        }
148
        if (r < 0)
225✔
149
                return r;
150

151
        free_and_replace(arg_esp_path, np);
224✔
152
        log_debug("Using EFI System Partition at %s.", arg_esp_path);
224✔
153

154
        return 1; /* for symmetry with acquire_xbootldr() below: found */
155
}
156

157
int acquire_xbootldr(
135✔
158
                int unprivileged_mode,
159
                int *ret_fd,
160
                sd_id128_t *ret_uuid,
161
                dev_t *ret_devid) {
162

163
        int r;
135✔
164

165
        _cleanup_free_ char *np = NULL;
135✔
166
        _cleanup_close_ int fd = -EBADF;
135✔
167
        r = find_xbootldr_and_warn_full(
178✔
168
                        arg_root,
169
                        arg_xbootldr_path,
170
                        unprivileged_mode,
171
                        &np,
172
                        ret_fd ? &fd : NULL,
173
                        ret_uuid,
174
                        ret_devid);
175
        if (r == -ENOKEY || (r >= 0 && arg_esp_path && path_equal(np, arg_esp_path))) {
135✔
176

177
                if (arg_esp_path)
88✔
178
                        log_debug("Didn't find an XBOOTLDR partition, using the ESP as $BOOT.");
75✔
179
                else
180
                        log_debug("Found neither an XBOOTLDR partition, nor an ESP.");
13✔
181

182
                arg_xbootldr_path = mfree(arg_xbootldr_path);
88✔
183

184
                if (ret_fd)
88✔
185
                        *ret_fd = -EBADF;
64✔
186
                if (ret_uuid)
88✔
187
                        *ret_uuid = SD_ID128_NULL;
18✔
188
                if (ret_devid)
88✔
189
                        *ret_devid = 0;
35✔
190

191
                return 0; /* not found */
192
        }
193
        if (r < 0)
46✔
194
                return r;
195

196
        free_and_replace(arg_xbootldr_path, np);
46✔
197
        log_debug("Using XBOOTLDR partition at %s as $BOOT.", arg_xbootldr_path);
46✔
198

199
        if (ret_fd)
46✔
200
                *ret_fd = TAKE_FD(fd);
28✔
201

202
        return 1; /* found */
203
}
204

205
static int print_loader_or_stub_path(void) {
8✔
206
        _cleanup_free_ char *p = NULL;
8✔
207
        sd_id128_t uuid;
8✔
208
        int r;
8✔
209

210
        if (arg_print_loader_path) {
8✔
211
                r = efi_loader_get_device_part_uuid(&uuid);
1✔
212
                if (r == -ENOENT)
1✔
213
                        return log_error_errno(r, "No loader partition UUID passed.");
×
214
                if (r < 0)
1✔
215
                        return log_error_errno(r, "Unable to determine loader partition UUID: %m");
×
216

217
                r = efi_get_variable_path(EFI_LOADER_VARIABLE_STR("LoaderImageIdentifier"), &p);
1✔
218
                if (r == -ENOENT)
1✔
219
                        return log_error_errno(r, "No loader EFI binary path passed.");
×
220
                if (r < 0)
1✔
221
                        return log_error_errno(r, "Unable to determine loader EFI binary path: %m");
×
222
        } else {
223
                assert(arg_print_stub_path);
7✔
224

225
                r = efi_stub_get_device_part_uuid(&uuid);
7✔
226
                if (r == -ENOENT)
7✔
227
                        return log_error_errno(r, "No stub partition UUID passed.");
×
228
                if (r < 0)
7✔
229
                        return log_error_errno(r, "Unable to determine stub partition UUID: %m");
×
230

231
                r = efi_get_variable_path(EFI_LOADER_VARIABLE_STR("StubImageIdentifier"), &p);
7✔
232
                if (r == -ENOENT)
7✔
233
                        return log_error_errno(r, "No stub EFI binary path passed.");
×
234
                if (r < 0)
7✔
235
                        return log_error_errno(r, "Unable to determine stub EFI binary path: %m");
×
236
        }
237

238
        sd_id128_t esp_uuid;
8✔
239
        r = acquire_esp(/* unprivileged_mode= */ false,
8✔
240
                        /* graceful= */ false,
241
                        /* ret_fd= */ NULL,
242
                        /* ret_part= */ NULL,
243
                        /* ret_pstart= */ NULL,
244
                        /* ret_psize= */ NULL,
245
                        &esp_uuid,
246
                        /* ret_devid= */ NULL);
247
        if (r < 0)
8✔
248
                return r;
249

250
        const char *found_path = NULL;
8✔
251
        if (sd_id128_equal(esp_uuid, uuid))
8✔
252
                found_path = arg_esp_path;
8✔
253
        else if (arg_print_stub_path) { /* In case of the stub, also look for things in the xbootldr partition */
×
254
                sd_id128_t xbootldr_uuid;
×
255

256
                r = acquire_xbootldr(/* unprivileged_mode= */ false,
×
257
                                     /* ret_fd= */ NULL,
258
                                     &xbootldr_uuid,
259
                                     /* ret_devid= */ NULL);
260
                if (r < 0)
×
261
                        return r;
×
262

263
                if (sd_id128_equal(xbootldr_uuid, uuid))
×
264
                        found_path = arg_xbootldr_path;
×
265
        }
266

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

270
        _cleanup_free_ char *j = path_join(found_path, p);
16✔
271
        if (!j)
8✔
272
                return log_oom();
×
273

274
        puts(j);
8✔
275
        return 0;
276
}
277

278
GracefulMode arg_graceful(void) {
95✔
279
        static bool chroot_checked = false;
95✔
280

281
        if (!chroot_checked && running_in_chroot() > 0) {
95✔
282
                if (_arg_graceful == ARG_GRACEFUL_NO)
×
283
                        log_full(arg_quiet ? LOG_DEBUG : LOG_INFO, "Running in a chroot, enabling --graceful.");
×
284

285
                _arg_graceful = ARG_GRACEFUL_FORCE;
×
286
        }
287

288
        chroot_checked = true;
95✔
289

290
        return _arg_graceful;
95✔
291
}
292

293
static int help(void) {
3✔
294
        _cleanup_free_ char *link = NULL;
3✔
295
        int r;
3✔
296

297
        pager_open(arg_pager_flags);
3✔
298

299
        r = terminal_urlify_man("bootctl", "1", &link);
3✔
300
        if (r < 0)
3✔
301
                return log_oom();
×
302

303
        static const char *const verb_groups[] = {
3✔
304
                "Generic EFI Firmware/Boot Loader Commands",
305
                "Boot Loader Specification Commands",
306
                "Boot Loader Interface Commands",
307
                "systemd-boot Commands",
308
                "Kernel Image Commands",
309
        };
310

311
        static const char *const option_groups[] = {
3✔
312
                "Block Device Discovery Commands",
313
                "Options",
314
        };
315

316
        Table *verb_tables[ELEMENTSOF(verb_groups)] = {};
3✔
317
        CLEANUP_ELEMENTS(verb_tables, table_unref_array_clear);
3✔
318
        Table *option_tables[ELEMENTSOF(option_groups)] = {};
3✔
319
        CLEANUP_ELEMENTS(option_tables, table_unref_array_clear);
3✔
320

321
        for (size_t i = 0; i < ELEMENTSOF(verb_groups); i++) {
18✔
322
                r = verbs_get_help_table_group(verb_groups[i], &verb_tables[i]);
15✔
323
                if (r < 0)
15✔
324
                        return r;
325
        }
326

327
        for (size_t i = 0; i < ELEMENTSOF(option_groups); i++) {
9✔
328
                r = option_parser_get_help_table_group(option_groups[i], &option_tables[i]);
6✔
329
                if (r < 0)
6✔
330
                        return r;
331
        }
332

333
        (void) table_sync_column_widths(0,
3✔
334
                                        verb_tables[0], verb_tables[1], verb_tables[2],
335
                                        verb_tables[3], verb_tables[4],
336
                                        option_tables[0], option_tables[1]);
337

338
        printf("%s [OPTIONS...] COMMAND ...\n"
6✔
339
               "\n%sControl EFI firmware boot settings and manage boot loader.%s\n",
340
               program_invocation_short_name,
341
               ansi_highlight(),
342
               ansi_normal());
343

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

347
                r = table_print_or_warn(verb_tables[i]);
15✔
348
                if (r < 0)
15✔
349
                        return r;
350
        }
351

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

355
                r = table_print_or_warn(option_tables[i]);
6✔
356
                if (r < 0)
6✔
357
                        return r;
358
        }
359

360
        printf("\nSee the %s for details.\n", link);
3✔
361
        return 0;
362
}
363

364
VERB_COMMON_HELP(help);
×
365

366
VERB_GROUP("Generic EFI Firmware/Boot Loader Commands");
367

368
VERB_SCOPE(, verb_status, "status", NULL, VERB_ANY, 1, VERB_DEFAULT,
369
           "Show status of installed boot loader and EFI variables");
370

371
VERB_SCOPE(, verb_reboot_to_firmware, "reboot-to-firmware", "[BOOL]", VERB_ANY, 2, 0,
372
           "Query or set reboot-to-firmware EFI flag");
373

374
VERB_GROUP("Boot Loader Specification Commands");
375

376
VERB_SCOPE_NOARG(, verb_list, "list",
377
           "List boot loader entries");
378

379
VERB_SCOPE(, verb_unlink, "unlink", "ID", VERB_ANY, 2, 0,
380
           "Remove boot loader entry");
381

382
VERB_SCOPE(, verb_link, "link", "KERNEL", 2, 2, 0,
383
           "Create boot loader entry for specified kernel");
384

385
VERB_SCOPE_NOARG(, verb_link_auto, "link-auto",
386
           "Create boot loader entry for the kernel and extra resources staged in /var/lib/systemd/uki/");
387

388
VERB_SCOPE_NOARG(, verb_cleanup, "cleanup",
389
           "Remove files in ESP not referenced in any boot entry");
390

391
VERB_GROUP("Boot Loader Interface Commands");
392

393
VERB_SCOPE(, verb_set_efivar, "set-default", "ID", 2, 2, 0,
394
           "Set default boot loader entry");
395

396
VERB_SCOPE(, verb_set_efivar, "set-oneshot", "ID", 2, 2, 0,
397
           "Set default boot loader entry, for next boot only");
398

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

402
VERB_SCOPE(, verb_set_efivar, "set-timeout", "SECONDS", 2, 2, 0,
403
           "Set the menu timeout");
404

405
VERB_SCOPE(, verb_set_efivar, "set-timeout-oneshot", "SECONDS", 2, 2, 0,
406
           "Set the menu timeout for the next boot only");
407

408
VERB_SCOPE(, verb_set_efivar, "set-preferred", "ID", 2, 2, 0,
409
           /* help= */ NULL);
410

411
VERB_GROUP("systemd-boot Commands");
412

413
VERB_SCOPE(, verb_install, "install", NULL, VERB_ANY, 1, 0,
414
           "Install systemd-boot to the ESP and EFI variables");
415

416
VERB_SCOPE(, verb_install, "update", NULL, VERB_ANY, 1, 0,
417
           "Update systemd-boot in the ESP and EFI variables");
418

419
VERB_SCOPE_NOARG(, verb_remove, "remove",
420
           "Remove systemd-boot from the ESP and EFI variables");
421

422
VERB_SCOPE_NOARG(, verb_is_installed, "is-installed",
423
           "Test whether systemd-boot is installed in the ESP");
424

425
VERB_SCOPE_NOARG(, verb_random_seed, "random-seed",
426
           "Initialize or refresh random seed in ESP and EFI variables");
427

428
VERB_GROUP("Kernel Image Commands");
429

430
VERB_SCOPE(, verb_kernel_identify, "kernel-identify", "KERNEL-IMAGE", 2, 2, 0,
431
           "Identify kernel image type");
432

433
VERB_SCOPE(, verb_kernel_inspect, "kernel-inspect", "KERNEL-IMAGE", 2, 2, 0,
434
           "Prints details about the kernel image");
435

436
static int parse_argv(int argc, char *argv[], char ***ret_args) {
293✔
437
        int r;
293✔
438

439
        assert(argc >= 0);
293✔
440
        assert(argv);
293✔
441

442
        OptionParser opts = { argc, argv };
293✔
443

444
        FOREACH_OPTION_OR_RETURN(c, &opts)
1,221✔
445
                switch (c) {
348✔
446

447
                OPTION_GROUP("Block Device Discovery Commands"): {}
87✔
448

449
                OPTION('p', "print-esp-path", NULL, "Print path to the EFI System Partition mount point"): {}
87✔
450
                OPTION_LONG("print-path", NULL, /* help= */ NULL):  /* Compatibility alias */
87✔
451
                        arg_print_esp_path = true;
87✔
452
                        break;
87✔
453

454
                OPTION('x', "print-boot-path", NULL, "Print path to the $BOOT partition mount point"):
9✔
455
                        arg_print_dollar_boot_path = true;
9✔
456
                        break;
9✔
457

458
                OPTION_LONG("print-loader-path", NULL, "Print path to currently booted boot loader binary"):
1✔
459
                        arg_print_loader_path = true;
1✔
460
                        break;
1✔
461

462
                OPTION_LONG("print-stub-path", NULL, "Print path to currently booted unified kernel binary"):
7✔
463
                        arg_print_stub_path = true;
7✔
464
                        break;
7✔
465

466
                OPTION('R', "print-root-device", NULL,
9✔
467
                       "Print path to the block device node backing the root file system"
468
                       " (returns e.g. /dev/nvme0n1p5)"): {}
9✔
469
                OPTION_HELP_VERBATIM("-RR",
9✔
470
                                     "Print path to the whole disk block device node backing the root FS"
471
                                     " (returns e.g. /dev/nvme0n1)"):
472
                        arg_print_root_device++;
9✔
473
                        break;
9✔
474

475
                OPTION_LONG("print-efi-architecture", NULL, "Print the local EFI architecture string"):
2✔
476
                        arg_print_efi_architecture = true;
2✔
477
                        break;
2✔
478

479
                OPTION_GROUP("Options"): {}
3✔
480

481
                OPTION_COMMON_HELP:
3✔
482
                        return help();
3✔
483

484
                OPTION_COMMON_VERSION:
3✔
485
                        return version();
3✔
486

487
                OPTION_LONG("esp-path", "PATH", "Path to the EFI System Partition (ESP)"): {}
3✔
488
                OPTION_LONG("path", "PATH", /* help= */ NULL):  /* Compatibility alias */
3✔
489
                        r = free_and_strdup(&arg_esp_path, opts.arg);
3✔
490
                        if (r < 0)
3✔
491
                                return log_oom();
×
492
                        break;
493

494
                OPTION_LONG("boot-path", "PATH", "Path to the $BOOT partition"):
3✔
495
                        r = free_and_strdup(&arg_xbootldr_path, opts.arg);
3✔
496
                        if (r < 0)
3✔
497
                                return log_oom();
×
498
                        break;
499

500
                OPTION_LONG("root", "PATH", "Operate on an alternate filesystem root"):
34✔
501
                        r = parse_path_argument(opts.arg, /* suppress_root= */ true, &arg_root);
34✔
502
                        if (r < 0)
34✔
503
                                return r;
504
                        break;
505

506
                OPTION_LONG("image", "PATH", "Operate on disk image as filesystem root"):
29✔
507
                        r = parse_path_argument(opts.arg, /* suppress_root= */ false, &arg_image);
29✔
508
                        if (r < 0)
29✔
509
                                return r;
510
                        break;
511

512
                OPTION_LONG("image-policy", "POLICY", "Specify disk image dissection policy"):
×
513
                        r = parse_image_policy_argument(opts.arg, &arg_image_policy);
×
514
                        if (r < 0)
×
515
                                return r;
516
                        break;
517

518
                OPTION_LONG("install-source", "SOURCE",
×
519
                            "Where to pick files when using --root=/--image= (auto, image, host)"): {
520
                        InstallSource is = install_source_from_string(opts.arg);
×
521
                        if (is < 0)
×
522
                                return log_error_errno(is, "Unexpected parameter for --install-source=: %s", opts.arg);
×
523

524
                        arg_install_source = is;
×
525
                        break;
×
526
                }
527

528
                OPTION_LONG("variables", "BOOL", "Whether to modify EFI variables"):
20✔
529
                        r = parse_tristate_argument_with_auto("--variables=", opts.arg, &arg_touch_variables);
20✔
530
                        if (r < 0)
20✔
531
                                return r;
532
#if !ENABLE_EFI
533
                        if (arg_touch_variables > 0)
534
                                return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
535
                                                       "Compiled without support for EFI, --variables=%s cannot be specified.", opts.arg);
536
#endif
537
                        break;
538

539
                OPTION_LONG("no-variables", NULL, /* help= */ NULL):  /* Compatibility alias */
×
540
                        arg_touch_variables = false;
×
541
                        break;
×
542

543
                OPTION_LONG("random-seed", "BOOL", "Whether to create random-seed file during install"):
×
544
                        r = parse_boolean_argument("--random-seed=", opts.arg, &arg_install_random_seed);
×
545
                        if (r < 0)
×
546
                                return r;
547
                        break;
548

549
                OPTION_COMMON_NO_PAGER:
×
550
                        arg_pager_flags |= PAGER_DISABLE;
×
551
                        break;
×
552

553
                OPTION_LONG("graceful", NULL,
49✔
554
                            "Don't fail when the ESP cannot be found or EFI variables cannot be written"):
555
                        _arg_graceful = ARG_GRACEFUL_YES;
49✔
556
                        break;
49✔
557

558
                OPTION('q', "quiet", NULL, "Suppress output"):
6✔
559
                        arg_quiet = true;
6✔
560
                        break;
6✔
561

562
                OPTION_COMMON_ENTRY_TOKEN:
27✔
563
                        r = parse_boot_entry_token_type(opts.arg, &arg_entry_token_type, &arg_entry_token);
27✔
564
                        if (r < 0)
27✔
565
                                return r;
566
                        break;
567

568
                OPTION_COMMON_MAKE_ENTRY_DIRECTORY: {}
15✔
569
                OPTION_LONG("make-machine-id-directory", "BOOL", /* help= */ NULL):  /* Compatibility alias */
15✔
570
                        if (streq(opts.arg, "auto"))  /* retained for backwards compatibility */
15✔
571
                                arg_make_entry_directory = -1; /* yes if machine-id is permanent */
×
572
                        else {
573
                                r = parse_boolean_argument("--make-entry-directory=", opts.arg, NULL);
15✔
574
                                if (r < 0)
15✔
575
                                        return r;
576

577
                                arg_make_entry_directory = r;
15✔
578
                        }
579
                        break;
580

581
                OPTION_COMMON_JSON:
7✔
582
                        r = parse_json_argument(opts.arg, &arg_json_format_flags);
7✔
583
                        if (r <= 0)
7✔
584
                                return r;
585
                        break;
586

587
                OPTION_LONG("all-architectures", NULL, "Install all supported EFI architectures"):
12✔
588
                        arg_arch_all = true;
12✔
589
                        break;
12✔
590

591
                OPTION_LONG("efi-boot-option-description", "DESCRIPTION",
×
592
                            "Description of the entry in the boot option list"):
593
                        if (!string_is_safe(opts.arg, STRING_ALLOW_BACKSLASHES|STRING_ALLOW_QUOTES|STRING_ALLOW_GLOBS)) {
×
594
                                _cleanup_free_ char *escaped = cescape(opts.arg);
×
595
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
596
                                                       "Invalid --efi-boot-option-description=: %s", strna(escaped));
597
                        }
598
                        if (strlen(opts.arg) > EFI_BOOT_OPTION_DESCRIPTION_MAX)
×
599
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
600
                                                       "--efi-boot-option-description= too long: %zu > %zu",
601
                                                       strlen(opts.arg), EFI_BOOT_OPTION_DESCRIPTION_MAX);
602
                        r = free_and_strdup_warn(&arg_efi_boot_option_description, opts.arg);
×
603
                        if (r < 0)
×
604
                                return r;
605
                        break;
606

607
                OPTION_LONG("efi-boot-option-description-with-device", "BOOL",
×
608
                            "Suffix description with disk vendor/model/serial"):
609
                        r = parse_boolean_argument("--efi-boot-option-description-with-device=", opts.arg,
×
610
                                                   &arg_efi_boot_option_description_with_device);
611
                        if (r < 0)
×
612
                                return r;
613
                        break;
614

615
                OPTION_LONG("dry-run", NULL, "Dry run (unlink and cleanup)"):
1✔
616
                        arg_dry_run = true;
1✔
617
                        break;
1✔
618

619
                OPTION_LONG("secure-boot-auto-enroll", "BOOL", "Set up secure boot auto-enrollment"):
1✔
620
                        r = parse_boolean_argument("--secure-boot-auto-enroll=", opts.arg,
1✔
621
                                                   &arg_secure_boot_auto_enroll);
622
                        if (r < 0)
1✔
623
                                return r;
624
                        break;
625

626
                OPTION_COMMON_PRIVATE_KEY("Private key for Secure Boot auto-enrollment"):
1✔
627
                        r = free_and_strdup_warn(&arg_private_key, opts.arg);
1✔
628
                        if (r < 0)
1✔
629
                                return r;
630
                        break;
631

632
                OPTION_COMMON_PRIVATE_KEY_SOURCE:
×
633
                        r = parse_openssl_key_source_argument(opts.arg,
×
634
                                                              &arg_private_key_source,
635
                                                              &arg_private_key_source_type);
636
                        if (r < 0)
×
637
                                return r;
638
                        break;
639

640
                OPTION_COMMON_CERTIFICATE("PEM certificate to use when setting up Secure Boot auto-enrollment"):
1✔
641
                        r = free_and_strdup_warn(&arg_certificate, opts.arg);
1✔
642
                        if (r < 0)
1✔
643
                                return r;
644
                        break;
645

646
                OPTION_COMMON_CERTIFICATE_SOURCE:
×
647
                        r = parse_openssl_certificate_source_argument(opts.arg,
×
648
                                                                      &arg_certificate_source,
649
                                                                      &arg_certificate_source_type);
650
                        if (r < 0)
×
651
                                return r;
652
                        break;
653

654
                OPTION_LONG("oldest", "BOOL",
3✔
655
                            "Delete oldest boot menu entry"):
656
                        r = parse_boolean_argument("--oldest=", opts.arg, &arg_oldest);
3✔
657
                        if (r < 0)
3✔
658
                                return r;
659

660
                        break;
661

662
                OPTION_LONG("keep-free", "BYTES",
1✔
663
                            "How much space to keep free on ESP/XBOOTLDR"):
664

665
                        if (isempty(opts.arg))
1✔
666
                                arg_keep_free = KEEP_FREE_BYTES_DEFAULT;
×
667
                        else {
668
                                r = parse_size(opts.arg, 1024, &arg_keep_free);
1✔
669
                                if (r < 0)
1✔
670
                                        return log_error_errno(r, "Failed to parse --keep-free=: %s", opts.arg);
×
671
                        }
672

673
                        break;
674

675
                OPTION_LONG("entry-title", "TITLE",
1✔
676
                            "Selects the entry title for the new boot menu entry"):
677

678
                        if (isempty(opts.arg)) {
1✔
679
                                arg_entry_title = mfree(arg_entry_title);
×
680
                                break;
×
681
                        }
682

683
                        if (!efi_loader_entry_title_valid(opts.arg))
1✔
684
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Not a valid boot menu entry title: %s", opts.arg);
×
685

686
                        r = free_and_strdup_warn(&arg_entry_title, opts.arg);
1✔
687
                        if (r < 0)
1✔
688
                                return r;
689
                        break;
690

691
                OPTION_LONG("entry-version", "VERSION",
1✔
692
                            "Selects the entry version for the new boot menu entry"):
693
                        if (isempty(opts.arg)) {
1✔
694
                                arg_entry_version = mfree(arg_entry_version);
×
695
                                break;
×
696
                        }
697

698
                        if (!version_is_valid_versionspec(opts.arg))
1✔
699
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Not a valid boot menu entry version: %s", opts.arg);
×
700

701
                        r = free_and_strdup_warn(&arg_entry_version, opts.arg);
1✔
702
                        if (r < 0)
1✔
703
                                return r;
704
                        break;
705

706
                OPTION_LONG("entry-commit", "NR",
8✔
707
                            "Selects the entry commit version for the new boot menu entry"): {
708
                        if (isempty(opts.arg)) {
8✔
709
                                arg_entry_commit = 0;
×
710
                                break;
8✔
711
                        }
712

713
                        uint64_t n;
8✔
714
                        r = safe_atou64(opts.arg, &n);
8✔
715
                        if (r < 0)
8✔
716
                                return log_error_errno(r, "Failed to parse --entry-commit= parameter: %s", opts.arg);
×
717
                        if (!entry_commit_valid(n))
8✔
718
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Not a valid entry commit number.");
×
719

720
                        arg_entry_commit = n;
8✔
721
                        break;
8✔
722
                }
723

724
                OPTION('X', "extra", "PATH",
3✔
725
                       "Pass extra resource (confext, sysext, credential) to the invoked UKI of the boot menu entry"): {
726

727
                        if (isempty(opts.arg)) {
3✔
728
                                arg_extras = strv_free(arg_extras);
×
729
                                break;
3✔
730
                        }
731

732
                        _cleanup_free_ char *x = NULL;
×
733
                        r = parse_path_argument(opts.arg, /* suppress_root= */ false, &x);
3✔
734
                        if (r < 0)
3✔
735
                                return r;
736

737
                        _cleanup_free_ char *fn = NULL;
3✔
738
                        r = path_extract_filename(x, &fn);
3✔
739
                        if (r < 0)
3✔
740
                                return log_error_errno(r, "Failed to extract filename from '%s': %m", x);
×
741
                        if (!efi_loader_entry_resource_filename_valid(fn))
3✔
742
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Extra filename '%s' is not suitable for reference in a boot menu entry.", fn);
×
743

744
                        r = strv_consume(&arg_extras, TAKE_PTR(x));
3✔
745
                        if (r < 0)
3✔
746
                                return log_oom();
×
747

748
                        strv_uniq(arg_extras);
3✔
749
                        break;
3✔
750
                }
751

752
                OPTION_LONG("tries-left", "NR",
1✔
753
                            "Set boot menu entries tries-left counter to the specified value"): {
754
                        if (isempty(opts.arg)) {
1✔
755
                                arg_tries_left = UINT_MAX;
×
756
                                break;
1✔
757
                        }
758

759
                        unsigned u;
1✔
760
                        r = safe_atou(opts.arg, &u);
1✔
761
                        if (r < 0)
1✔
762
                                return log_error_errno(r, "Failed to parse tries left counter: %s", opts.arg);
×
763
                        if (u >= UINT_MAX)
1✔
764
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Tries left counter too large, refusing: %u", u);
×
765

766
                        arg_tries_left = u;
1✔
767
                        break;
1✔
768
                }}
769

770
        char **args = option_parser_get_args(&opts);
287✔
771

772
        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)
287✔
773
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
774
                                                       "--print-esp-path/-p, --print-boot-path/-x, --print-root-device=/-R, --print-loader-path, --print-stub-path, --print-efi-architecture cannot be combined.");
775

776
        if ((arg_root || arg_image) && args[0] && !STR_IN_SET(args[0], "status", "list",
287✔
777
                        "install", "update", "remove", "is-installed", "random-seed", "unlink", "cleanup"))
778
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
779
                                       "Options --root= and --image= are not supported with verb %s.",
780
                                       args[0]);
781

782
        if (arg_root && arg_image)
287✔
783
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Please specify either --root= or --image=, the combination of both is not supported.");
×
784

785
        if (arg_install_source != INSTALL_SOURCE_AUTO && !arg_root && !arg_image)
287✔
786
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--install-from-host is only supported with --root= or --image=.");
×
787

788
        if (arg_dry_run && args[0] && !STR_IN_SET(args[0], "unlink", "cleanup"))
287✔
789
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--dry-run is only supported with --unlink or --cleanup");
×
790

791
        if (arg_secure_boot_auto_enroll) {
287✔
792
#if HAVE_OPENSSL
793
                if (!arg_certificate)
1✔
794
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Secure boot auto-enrollment requested but no certificate provided.");
×
795

796
                if (!arg_private_key)
1✔
797
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Secure boot auto-enrollment requested but no private key provided.");
×
798
#else
799
                return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Secure boot auto-enrollment requested but OpenSSL support is disabled.");
800
#endif
801
        }
802

803
        r = sd_varlink_invocation(SD_VARLINK_ALLOW_ACCEPT);
287✔
804
        if (r < 0)
287✔
805
                return log_error_errno(r, "Failed to check if invoked in Varlink mode: %m");
×
806
        if (r > 0) {
287✔
807
                arg_varlink = true;
17✔
808
                arg_pager_flags |= PAGER_DISABLE;
17✔
809
        }
810

811
        *ret_args = args;
287✔
812
        return 1;
287✔
813
}
814

815
static int vl_server(void) {
17✔
816
        _cleanup_(sd_varlink_server_unrefp) sd_varlink_server *varlink_server = NULL;
17✔
817
        int r;
17✔
818

819
        /* Invocation as Varlink service */
820

821
        r = varlink_server_new(
17✔
822
                        &varlink_server,
823
                        SD_VARLINK_SERVER_ROOT_ONLY |
824
                        SD_VARLINK_SERVER_MYSELF_ONLY |
825
                        SD_VARLINK_SERVER_ALLOW_FD_PASSING_INPUT,
826
                        /* userdata= */ NULL);
827
        if (r < 0)
17✔
828
                return log_error_errno(r, "Failed to allocate Varlink server: %m");
×
829

830
        r = sd_varlink_server_add_interface_many(
17✔
831
                        varlink_server,
832
                        &vl_interface_io_systemd_BootControl,
833
                        &vl_interface_io_systemd_SysUpdate_Notify);
834
        if (r < 0)
17✔
835
                return log_error_errno(r, "Failed to add Varlink interfaces: %m");
×
836

837
        r = sd_varlink_server_bind_method_many(
17✔
838
                        varlink_server,
839
                        "io.systemd.BootControl.ListBootEntries",        vl_method_list_boot_entries,
840
                        "io.systemd.BootControl.SetRebootToFirmware",    vl_method_set_reboot_to_firmware,
841
                        "io.systemd.BootControl.GetRebootToFirmware",    vl_method_get_reboot_to_firmware,
842
                        "io.systemd.BootControl.Install",                vl_method_install,
843
                        "io.systemd.BootControl.Link",                   vl_method_link,
844
                        "io.systemd.BootControl.LinkAuto",               vl_method_link_auto,
845
                        "io.systemd.BootControl.Unlink",                 vl_method_unlink,
846
                        "io.systemd.SysUpdate.Notify.OnCompletedUpdate", vl_method_on_completed_update);
847
        if (r < 0)
17✔
848
                return log_error_errno(r, "Failed to bind Varlink methods: %m");
×
849

850
        r = sd_varlink_server_loop_auto(varlink_server);
17✔
851
        if (r < 0)
17✔
852
                return log_error_errno(r, "Failed to run Varlink event loop: %m");
×
853

854
        return 0;
855
}
856

857
static int run(int argc, char *argv[]) {
293✔
858
        _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
293✔
859
        _cleanup_(umount_and_freep) char *mounted_dir = NULL;
293✔
860
        int r;
293✔
861

862
        LIBBLKID_NOTE(recommended);
293✔
863
        LIBCRYPTSETUP_NOTE(suggested);
293✔
864
        LIBMOUNT_NOTE(recommended);
293✔
865
        LIBTSS2_ESYS_NOTE(suggested);
293✔
866
        LIBTSS2_MU_NOTE(suggested);
293✔
867
        LIBTSS2_RC_NOTE(suggested);
293✔
868

869
        log_setup();
293✔
870

871
        char **args = NULL;
293✔
872
        r = parse_argv(argc, argv, &args);
293✔
873
        if (r <= 0)
293✔
874
                return r;
875

876
        if (arg_varlink)
287✔
877
                return vl_server();
17✔
878

879
        if (arg_print_root_device > 0) {
270✔
880
                _cleanup_free_ char *path = NULL;
6✔
881
                dev_t devno;
6✔
882

883
                r = blockdev_get_root(LOG_ERR, &devno);
6✔
884
                if (r < 0)
6✔
885
                        return r;
886
                if (r == 0) {
6✔
887
                        log_error("Root file system not backed by a (single) whole block device.");
×
888
                        return 80; /* some recognizable error code */
889
                }
890

891
                if (arg_print_root_device > 1) {
6✔
892
                        r = block_get_whole_disk(devno, &devno);
3✔
893
                        if (r < 0)
3✔
894
                                log_debug_errno(r, "Unable to find whole block device for root block device, ignoring: %m");
×
895
                }
896

897
                r = device_path_make_canonical(S_IFBLK, devno, &path);
6✔
898
                if (r < 0)
6✔
899
                        return log_error_errno(r,
×
900
                                               "Failed to format canonical device path for devno '" DEVNUM_FORMAT_STR "': %m",
901
                                               DEVNUM_FORMAT_VAL(devno));
902

903
                puts(path);
6✔
904
                return 0;
905
        }
906

907
        if (arg_print_loader_path || arg_print_stub_path)
264✔
908
                return print_loader_or_stub_path();
8✔
909

910
        if (arg_print_efi_architecture) {
256✔
911
                printf("%s\n", get_efi_arch());
2✔
912
                return 0;
913
        }
914

915
        /* Open up and mount the image */
916
        if (arg_image) {
254✔
917
                assert(!arg_root);
26✔
918

919
                r = mount_image_privately_interactively(
26✔
920
                                arg_image,
921
                                arg_image_policy,
922
                                DISSECT_IMAGE_GENERIC_ROOT |
923
                                DISSECT_IMAGE_USR_NO_ROOT |
924
                                DISSECT_IMAGE_RELAX_VAR_CHECK |
925
                                DISSECT_IMAGE_ALLOW_USERSPACE_VERITY,
926
                                &mounted_dir,
927
                                /* ret_dir_fd= */ NULL,
928
                                &loop_device);
929
                if (r < 0)
26✔
930
                        return r;
931

932
                arg_root = strdup(mounted_dir);
26✔
933
                if (!arg_root)
26✔
934
                        return log_oom();
×
935
        }
936

937
        return dispatch_verb(args, NULL);
254✔
938
}
939

940
DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);
293✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc