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

systemd / systemd / 25196166722

30 Apr 2026 07:30PM UTC coverage: 72.134% (+0.3%) from 71.849%
25196166722

push

github

bluca
mkosi: update debian commit reference to 1302f123d

* 1302f123d9 Restrict wildcard for new files
* a6d0098d10 Install new files for upstream build
* ce07fd7616 d/t/boot-and-services: use coreutils tunable in apparmor test (LP: #2125614)

324804 of 450278 relevant lines covered (72.13%)

1187716.32 hits per line

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

74.16
/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 "crypto-util.h"
21
#include "devnum-util.h"
22
#include "dissect-image.h"
23
#include "efi-loader.h"
24
#include "efivars.h"
25
#include "escape.h"
26
#include "fd-util.h"
27
#include "find-esp.h"
28
#include "format-table.h"
29
#include "image-policy.h"
30
#include "log.h"
31
#include "loop-util.h"
32
#include "main-func.h"
33
#include "mount-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
        Table *verb_tables[ELEMENTSOF(verb_groups)] = {};
3✔
302
        CLEANUP_ELEMENTS(verb_tables, table_unref_array_clear);
3✔
303
        Table *option_tables[ELEMENTSOF(option_groups)] = {};
3✔
304
        CLEANUP_ELEMENTS(option_tables, table_unref_array_clear);
3✔
305

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

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

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

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

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

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

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

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

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

349
VERB_COMMON_HELP(help);
×
350

351
VERB_GROUP("Generic EFI Firmware/Boot Loader Commands");
352

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

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

359
VERB_GROUP("Boot Loader Specification Commands");
360

361
VERB_SCOPE_NOARG(, verb_list, "list",
362
           "List boot loader entries");
363

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

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

370
VERB_GROUP("Boot Loader Interface Commands");
371

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

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

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

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

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

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

390
VERB_GROUP("systemd-boot Commands");
391

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

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

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

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

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

407
VERB_GROUP("Kernel Image Commands");
408

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

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

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

418
        assert(argc >= 0);
305✔
419
        assert(argv);
305✔
420

421
        OptionParser opts = { argc, argv };
305✔
422

423
        FOREACH_OPTION_OR_RETURN(c, &opts)
1,355✔
424
                switch (c) {
446✔
425

426
                OPTION_GROUP("Block Device Discovery Commands"): {}
48✔
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"): {}
3✔
459

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

607
                OPTION_COMMON_PRIVATE_KEY("Private key for Secure Boot auto-enrollment"):
1✔
608
                        r = free_and_strdup_warn(&arg_private_key, opts.arg);
1✔
609
                        if (r < 0)
1✔
610
                                return r;
611
                        break;
612

613
                OPTION_COMMON_PRIVATE_KEY_SOURCE:
×
614
                        r = parse_openssl_key_source_argument(opts.arg,
×
615
                                                              &arg_private_key_source,
616
                                                              &arg_private_key_source_type);
617
                        if (r < 0)
×
618
                                return r;
619
                        break;
620

621
                OPTION_COMMON_CERTIFICATE("PEM certificate to use when setting up Secure Boot auto-enrollment"):
1✔
622
                        r = free_and_strdup_warn(&arg_certificate, opts.arg);
1✔
623
                        if (r < 0)
1✔
624
                                return r;
625
                        break;
626

627
                OPTION_COMMON_CERTIFICATE_SOURCE:
×
628
                        r = parse_openssl_certificate_source_argument(opts.arg,
×
629
                                                                      &arg_certificate_source,
630
                                                                      &arg_certificate_source_type);
631
                        if (r < 0)
×
632
                                return r;
633
                        break;
634
                }
635

636
        char **args = option_parser_get_args(&opts);
299✔
637

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

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

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

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

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

657
        if (arg_secure_boot_auto_enroll) {
299✔
658
#if HAVE_OPENSSL
659
                if (!arg_certificate)
1✔
660
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Secure boot auto-enrollment requested but no certificate provided.");
×
661

662
                if (!arg_private_key)
1✔
663
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Secure boot auto-enrollment requested but no private key provided.");
×
664
#else
665
                return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Secure boot auto-enrollment requested but OpenSSL support is disabled.");
666
#endif
667
        }
668

669
        r = sd_varlink_invocation(SD_VARLINK_ALLOW_ACCEPT);
299✔
670
        if (r < 0)
299✔
671
                return log_error_errno(r, "Failed to check if invoked in Varlink mode: %m");
×
672
        if (r > 0) {
299✔
673
                arg_varlink = true;
15✔
674
                arg_pager_flags |= PAGER_DISABLE;
15✔
675
        }
676

677
        *ret_args = args;
299✔
678
        return 1;
299✔
679
}
680

681
static int vl_server(void) {
15✔
682
        _cleanup_(sd_varlink_server_unrefp) sd_varlink_server *varlink_server = NULL;
15✔
683
        int r;
15✔
684

685
        /* Invocation as Varlink service */
686

687
        r = varlink_server_new(
15✔
688
                        &varlink_server,
689
                        SD_VARLINK_SERVER_ROOT_ONLY|SD_VARLINK_SERVER_ALLOW_FD_PASSING_INPUT,
690
                        /* userdata= */ NULL);
691
        if (r < 0)
15✔
692
                return log_error_errno(r, "Failed to allocate Varlink server: %m");
×
693

694
        r = sd_varlink_server_add_interface(varlink_server, &vl_interface_io_systemd_BootControl);
15✔
695
        if (r < 0)
15✔
696
                return log_error_errno(r, "Failed to add Varlink interface: %m");
×
697

698
        r = sd_varlink_server_bind_method_many(
15✔
699
                        varlink_server,
700
                        "io.systemd.BootControl.ListBootEntries",     vl_method_list_boot_entries,
701
                        "io.systemd.BootControl.SetRebootToFirmware", vl_method_set_reboot_to_firmware,
702
                        "io.systemd.BootControl.GetRebootToFirmware", vl_method_get_reboot_to_firmware,
703
                        "io.systemd.BootControl.Install",             vl_method_install);
704
        if (r < 0)
15✔
705
                return log_error_errno(r, "Failed to bind Varlink methods: %m");
×
706

707
        r = sd_varlink_server_loop_auto(varlink_server);
15✔
708
        if (r < 0)
15✔
709
                return log_error_errno(r, "Failed to run Varlink event loop: %m");
×
710

711
        return 0;
712
}
713

714
static int run(int argc, char *argv[]) {
305✔
715
        _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
305✔
716
        _cleanup_(umount_and_freep) char *mounted_dir = NULL;
305✔
717
        int r;
305✔
718

719
        log_setup();
305✔
720

721
        char **args = NULL;
305✔
722
        r = parse_argv(argc, argv, &args);
305✔
723
        if (r <= 0)
305✔
724
                return r;
725

726
        if (arg_varlink)
299✔
727
                return vl_server();
15✔
728

729
        if (arg_print_root_device > 0) {
284✔
730
                _cleanup_free_ char *path = NULL;
6✔
731
                dev_t devno;
6✔
732

733
                r = blockdev_get_root(LOG_ERR, &devno);
6✔
734
                if (r < 0)
6✔
735
                        return r;
736
                if (r == 0) {
6✔
737
                        log_error("Root file system not backed by a (single) whole block device.");
×
738
                        return 80; /* some recognizable error code */
×
739
                }
740

741
                if (arg_print_root_device > 1) {
6✔
742
                        r = block_get_whole_disk(devno, &devno);
3✔
743
                        if (r < 0)
3✔
744
                                log_debug_errno(r, "Unable to find whole block device for root block device, ignoring: %m");
×
745
                }
746

747
                r = device_path_make_canonical(S_IFBLK, devno, &path);
6✔
748
                if (r < 0)
6✔
749
                        return log_error_errno(r,
×
750
                                               "Failed to format canonical device path for devno '" DEVNUM_FORMAT_STR "': %m",
751
                                               DEVNUM_FORMAT_VAL(devno));
752

753
                puts(path);
6✔
754
                return 0;
755
        }
756

757
        if (arg_print_loader_path || arg_print_stub_path)
278✔
758
                return print_loader_or_stub_path();
8✔
759

760
        if (arg_print_efi_architecture) {
270✔
761
                printf("%s\n", get_efi_arch());
1✔
762
                return 0;
763
        }
764

765
        /* Open up and mount the image */
766
        if (arg_image) {
269✔
767
                assert(!arg_root);
26✔
768

769
                r = mount_image_privately_interactively(
26✔
770
                                arg_image,
771
                                arg_image_policy,
772
                                DISSECT_IMAGE_GENERIC_ROOT |
773
                                DISSECT_IMAGE_USR_NO_ROOT |
774
                                DISSECT_IMAGE_RELAX_VAR_CHECK |
775
                                DISSECT_IMAGE_ALLOW_USERSPACE_VERITY,
776
                                &mounted_dir,
777
                                /* ret_dir_fd= */ NULL,
778
                                &loop_device);
779
                if (r < 0)
26✔
780
                        return r;
781

782
                arg_root = strdup(mounted_dir);
26✔
783
                if (!arg_root)
26✔
784
                        return log_oom();
×
785
        }
786

787
        return dispatch_verb_with_args(args, NULL);
269✔
788
}
789

790
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