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

systemd / systemd / 22508817992

27 Feb 2026 10:57PM UTC coverage: 72.532% (-0.04%) from 72.575%
22508817992

push

github

poettering
NEWS: add various more features added in v260, and introduce more sections

315698 of 435256 relevant lines covered (72.53%)

1132137.23 hits per line

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

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

3
#include <getopt.h>
4
#include <sys/stat.h>
5

6
#include "sd-varlink.h"
7

8
#include "blockdev-util.h"
9
#include "boot-entry.h"
10
#include "bootctl.h"
11
#include "bootctl-cleanup.h"
12
#include "bootctl-install.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 "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 "find-esp.h"
26
#include "image-policy.h"
27
#include "log.h"
28
#include "loop-util.h"
29
#include "main-func.h"
30
#include "mount-util.h"
31
#include "openssl-util.h"
32
#include "pager.h"
33
#include "parse-argument.h"
34
#include "path-util.h"
35
#include "pretty-print.h"
36
#include "string-table.h"
37
#include "string-util.h"
38
#include "strv.h"
39
#include "utf8.h"
40
#include "varlink-io.systemd.BootControl.h"
41
#include "varlink-util.h"
42
#include "verbs.h"
43
#include "virt.h"
44

45
static GracefulMode _arg_graceful = ARG_GRACEFUL_NO;
46

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

81
STATIC_DESTRUCTOR_REGISTER(arg_esp_path, freep);
297✔
82
STATIC_DESTRUCTOR_REGISTER(arg_xbootldr_path, freep);
297✔
83
STATIC_DESTRUCTOR_REGISTER(arg_install_layout, freep);
297✔
84
STATIC_DESTRUCTOR_REGISTER(arg_entry_token, freep);
297✔
85
STATIC_DESTRUCTOR_REGISTER(arg_root, freep);
297✔
86
STATIC_DESTRUCTOR_REGISTER(arg_image, freep);
297✔
87
STATIC_DESTRUCTOR_REGISTER(arg_efi_boot_option_description, freep);
297✔
88
STATIC_DESTRUCTOR_REGISTER(arg_image_policy, image_policy_freep);
297✔
89
STATIC_DESTRUCTOR_REGISTER(arg_certificate, freep);
297✔
90
STATIC_DESTRUCTOR_REGISTER(arg_certificate_source, freep);
297✔
91
STATIC_DESTRUCTOR_REGISTER(arg_private_key, freep);
297✔
92
STATIC_DESTRUCTOR_REGISTER(arg_private_key_source, freep);
297✔
93

94
static const char* const install_source_table[_INSTALL_SOURCE_MAX] = {
95
        [INSTALL_SOURCE_IMAGE] = "image",
96
        [INSTALL_SOURCE_HOST]  = "host",
97
        [INSTALL_SOURCE_AUTO]  = "auto",
98
};
99

100
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(install_source, InstallSource);
×
101

102
int acquire_esp(
256✔
103
                int unprivileged_mode,
104
                bool graceful,
105
                uint32_t *ret_part,
106
                uint64_t *ret_pstart,
107
                uint64_t *ret_psize,
108
                sd_id128_t *ret_uuid,
109
                dev_t *ret_devid) {
110

111
        char *np;
256✔
112
        int r;
256✔
113

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

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

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

133
        free_and_replace(arg_esp_path, np);
250✔
134
        log_debug("Using EFI System Partition at %s.", arg_esp_path);
250✔
135

136
        return 0;
137
}
138

139
int acquire_xbootldr(
203✔
140
                int unprivileged_mode,
141
                sd_id128_t *ret_uuid,
142
                dev_t *ret_devid) {
143

144
        char *np;
203✔
145
        int r;
203✔
146

147
        r = find_xbootldr_and_warn(arg_root, arg_xbootldr_path, unprivileged_mode, &np, ret_uuid, ret_devid);
203✔
148
        if (r == -ENOKEY || path_equal(np, arg_esp_path)) {
203✔
149
                log_debug("Didn't find an XBOOTLDR partition, using the ESP as $BOOT.");
156✔
150
                arg_xbootldr_path = mfree(arg_xbootldr_path);
156✔
151

152
                if (ret_uuid)
156✔
153
                        *ret_uuid = SD_ID128_NULL;
15✔
154
                if (ret_devid)
156✔
155
                        *ret_devid = 0;
20✔
156
                return 0;
156✔
157
        }
158
        if (r < 0)
47✔
159
                return r;
160

161
        free_and_replace(arg_xbootldr_path, np);
46✔
162
        log_debug("Using XBOOTLDR partition at %s as $BOOT.", arg_xbootldr_path);
46✔
163

164
        return 1;
165
}
166

167
static int print_loader_or_stub_path(void) {
6✔
168
        _cleanup_free_ char *p = NULL;
6✔
169
        sd_id128_t uuid;
6✔
170
        int r;
6✔
171

172
        if (arg_print_loader_path) {
6✔
173
                r = efi_loader_get_device_part_uuid(&uuid);
×
174
                if (r == -ENOENT)
×
175
                        return log_error_errno(r, "No loader partition UUID passed.");
×
176
                if (r < 0)
×
177
                        return log_error_errno(r, "Unable to determine loader partition UUID: %m");
×
178

179
                r = efi_get_variable_path(EFI_LOADER_VARIABLE_STR("LoaderImageIdentifier"), &p);
×
180
                if (r == -ENOENT)
×
181
                        return log_error_errno(r, "No loader EFI binary path passed.");
×
182
                if (r < 0)
×
183
                        return log_error_errno(r, "Unable to determine loader EFI binary path: %m");
×
184
        } else {
185
                assert(arg_print_stub_path);
6✔
186

187
                r = efi_stub_get_device_part_uuid(&uuid);
6✔
188
                if (r == -ENOENT)
6✔
189
                        return log_error_errno(r, "No stub partition UUID passed.");
×
190
                if (r < 0)
6✔
191
                        return log_error_errno(r, "Unable to determine stub partition UUID: %m");
×
192

193
                r = efi_get_variable_path(EFI_LOADER_VARIABLE_STR("StubImageIdentifier"), &p);
6✔
194
                if (r == -ENOENT)
6✔
195
                        return log_error_errno(r, "No stub EFI binary path passed.");
×
196
                if (r < 0)
6✔
197
                        return log_error_errno(r, "Unable to determine stub EFI binary path: %m");
×
198
        }
199

200
        sd_id128_t esp_uuid;
6✔
201
        r = acquire_esp(/* unprivileged_mode= */ false, /* graceful= */ false,
6✔
202
                        /* ret_part= */ NULL, /* ret_pstart= */ NULL, /* ret_psize= */ NULL,
203
                        &esp_uuid, /* ret_devid= */ NULL);
204
        if (r < 0)
6✔
205
                return r;
206

207
        const char *found_path = NULL;
6✔
208
        if (sd_id128_equal(esp_uuid, uuid))
6✔
209
                found_path = arg_esp_path;
6✔
210
        else if (arg_print_stub_path) { /* In case of the stub, also look for things in the xbootldr partition */
×
211
                sd_id128_t xbootldr_uuid;
×
212

213
                r = acquire_xbootldr(/* unprivileged_mode= */ false, &xbootldr_uuid, /* ret_devid= */ NULL);
×
214
                if (r < 0)
×
215
                        return r;
×
216

217
                if (sd_id128_equal(xbootldr_uuid, uuid))
×
218
                        found_path = arg_xbootldr_path;
×
219
        }
220

221
        if (!found_path)
6✔
222
                return log_error_errno(SYNTHETIC_ERRNO(ENOENT), "Failed to discover partition " SD_ID128_FORMAT_STR " among mounted boot partitions.", SD_ID128_FORMAT_VAL(uuid));
×
223

224
        _cleanup_free_ char *j = path_join(found_path, p);
12✔
225
        if (!j)
6✔
226
                return log_oom();
×
227

228
        puts(j);
6✔
229
        return 0;
230
}
231

232
GracefulMode arg_graceful(void) {
288✔
233
        static bool chroot_checked = false;
288✔
234

235
        if (!chroot_checked && running_in_chroot() > 0) {
288✔
236
                if (_arg_graceful == ARG_GRACEFUL_NO)
×
237
                        log_full(arg_quiet ? LOG_DEBUG : LOG_INFO, "Running in a chroot, enabling --graceful.");
×
238

239
                _arg_graceful = ARG_GRACEFUL_FORCE;
×
240
        }
241

242
        chroot_checked = true;
288✔
243

244
        return _arg_graceful;
288✔
245
}
246

247
static int help(int argc, char *argv[], void *userdata) {
3✔
248
        _cleanup_free_ char *link = NULL;
3✔
249
        int r;
3✔
250

251
        pager_open(arg_pager_flags);
3✔
252

253
        r = terminal_urlify_man("bootctl", "1", &link);
3✔
254
        if (r < 0)
3✔
255
                return log_oom();
×
256

257
        printf("%1$s [OPTIONS...] COMMAND ...\n"
6✔
258
               "\n%5$sControl EFI firmware boot settings and manage boot loader.%6$s\n"
259
               "\n%3$sGeneric EFI Firmware/Boot Loader Commands:%4$s\n"
260
               "  status               Show status of installed boot loader and EFI variables\n"
261
               "  reboot-to-firmware [BOOL]\n"
262
               "                       Query or set reboot-to-firmware EFI flag\n"
263
               "\n%3$sBoot Loader Specification Commands:%4$s\n"
264
               "  list                 List boot loader entries\n"
265
               "  unlink ID            Remove boot loader entry\n"
266
               "  cleanup              Remove files in ESP not referenced in any boot entry\n"
267
               "\n%3$sBoot Loader Interface Commands:%4$s\n"
268
               "  set-default ID       Set default boot loader entry\n"
269
               "  set-oneshot ID       Set default boot loader entry, for next boot only\n"
270
               "  set-sysfail ID       Set boot loader entry used in case of a system failure\n"
271
               "  set-timeout SECONDS  Set the menu timeout\n"
272
               "  set-timeout-oneshot SECONDS\n"
273
               "                       Set the menu timeout for the next boot only\n"
274
               "\n%3$ssystemd-boot Commands:%4$s\n"
275
               "  install              Install systemd-boot to the ESP and EFI variables\n"
276
               "  update               Update systemd-boot in the ESP and EFI variables\n"
277
               "  remove               Remove systemd-boot from the ESP and EFI variables\n"
278
               "  is-installed         Test whether systemd-boot is installed in the ESP\n"
279
               "  random-seed          Initialize or refresh random seed in ESP and EFI\n"
280
               "                       variables\n"
281
               "\n%3$sKernel Image Commands:%4$s\n"
282
               "  kernel-identify KERNEL-IMAGE\n"
283
               "                       Identify kernel image type\n"
284
               "  kernel-inspect KERNEL-IMAGE\n"
285
               "                       Prints details about the kernel image\n"
286
               "\n%3$sBlock Device Discovery Commands:%4$s\n"
287
               "  -p --print-esp-path  Print path to the EFI System Partition mount point\n"
288
               "  -x --print-boot-path Print path to the $BOOT partition mount point\n"
289
               "     --print-loader-path\n"
290
               "                       Print path to currently booted boot loader binary\n"
291
               "     --print-stub-path Print path to currently booted unified kernel binary\n"
292
               "  -R --print-root-device\n"
293
               "                       Print path to the block device node backing the\n"
294
               "                       root file system (returns e.g. /dev/nvme0n1p5)\n"
295
               "  -RR                  Print path to the whole disk block device node\n"
296
               "                       backing the root FS (returns e.g. /dev/nvme0n1)\n"
297
               "\n%3$sOptions:%4$s\n"
298
               "  -h --help            Show this help\n"
299
               "     --version         Print version\n"
300
               "     --esp-path=PATH   Path to the EFI System Partition (ESP)\n"
301
               "     --boot-path=PATH  Path to the $BOOT partition\n"
302
               "     --root=PATH       Operate on an alternate filesystem root\n"
303
               "     --image=PATH      Operate on disk image as filesystem root\n"
304
               "     --image-policy=POLICY\n"
305
               "                       Specify disk image dissection policy\n"
306
               "     --install-source=auto|image|host\n"
307
               "                       Where to pick files when using --root=/--image=\n"
308
               "     --variables=yes|no\n"
309
               "                       Whether to modify EFI variables\n"
310
               "     --random-seed=yes|no\n"
311
               "                       Whether to create random-seed file during install\n"
312
               "     --no-pager        Do not pipe output into a pager\n"
313
               "     --graceful        Don't fail when the ESP cannot be found or EFI\n"
314
               "                       variables cannot be written\n"
315
               "  -q --quiet           Suppress output\n"
316
               "     --make-entry-directory=yes|no|auto\n"
317
               "                       Create $BOOT/ENTRY-TOKEN/ directory\n"
318
               "     --entry-token=machine-id|os-id|os-image-id|auto|literal:…\n"
319
               "                       Entry token to use for this installation\n"
320
               "     --json=pretty|short|off\n"
321
               "                       Generate JSON output\n"
322
               "     --all-architectures\n"
323
               "                       Install all supported EFI architectures\n"
324
               "     --efi-boot-option-description=DESCRIPTION\n"
325
               "                       Description of the entry in the boot option list\n"
326
               "     --efi-boot-option-description-with-device=yes\n"
327
               "                       Suffix description with disk vendor/model/serial\n"
328
               "     --dry-run         Dry run (unlink and cleanup)\n"
329
               "     --secure-boot-auto-enroll=yes|no\n"
330
               "                       Set up secure boot auto-enrollment\n"
331
               "     --private-key=PATH|URI\n"
332
               "                       Private key to use when setting up secure boot\n"
333
               "                       auto-enrollment or an engine or provider specific\n"
334
               "                       designation if --private-key-source= is used\n"
335
               "     --private-key-source=file|provider:PROVIDER|engine:ENGINE\n"
336
               "                       Specify how to use KEY for --private-key=. Allows\n"
337
               "                       an OpenSSL engine/provider to be used when setting\n"
338
               "                       up secure boot auto-enrollment\n"
339
               "     --certificate=PATH|URI\n"
340
               "                       PEM certificate to use when setting up Secure Boot\n"
341
               "                       auto-enrollment, or a provider specific designation\n"
342
               "                       if --certificate-source= is used\n"
343
               "     --certificate-source=file|provider:PROVIDER\n"
344
               "                       Specify how to interpret the certificate from\n"
345
               "                       --certificate=. Allows the certificate to be loaded\n"
346
               "                       from an OpenSSL provider\n"
347
               "\nSee the %2$s for details.\n",
348
               program_invocation_short_name,
349
               link,
350
               ansi_underline(),
351
               ansi_normal(),
352
               ansi_highlight(),
353
               ansi_normal());
354

355
        return 0;
356
}
357

358
static int parse_argv(int argc, char *argv[]) {
297✔
359
        enum {
297✔
360
                ARG_ESP_PATH = 0x100,
361
                ARG_BOOT_PATH,
362
                ARG_ROOT,
363
                ARG_IMAGE,
364
                ARG_IMAGE_POLICY,
365
                ARG_INSTALL_SOURCE,
366
                ARG_VERSION,
367
                ARG_VARIABLES,
368
                ARG_NO_VARIABLES,
369
                ARG_RANDOM_SEED,
370
                ARG_NO_PAGER,
371
                ARG_GRACEFUL,
372
                ARG_MAKE_ENTRY_DIRECTORY,
373
                ARG_ENTRY_TOKEN,
374
                ARG_JSON,
375
                ARG_ARCH_ALL,
376
                ARG_EFI_BOOT_OPTION_DESCRIPTION,
377
                ARG_EFI_BOOT_OPTION_DESCRIPTION_WITH_DEVICE,
378
                ARG_DRY_RUN,
379
                ARG_PRINT_LOADER_PATH,
380
                ARG_PRINT_STUB_PATH,
381
                ARG_SECURE_BOOT_AUTO_ENROLL,
382
                ARG_CERTIFICATE,
383
                ARG_CERTIFICATE_SOURCE,
384
                ARG_PRIVATE_KEY,
385
                ARG_PRIVATE_KEY_SOURCE,
386
        };
387

388
        static const struct option options[] = {
297✔
389
                { "help",                                    no_argument,       NULL, 'h'                                         },
390
                { "version",                                 no_argument,       NULL, ARG_VERSION                                 },
391
                { "esp-path",                                required_argument, NULL, ARG_ESP_PATH                                },
392
                { "path",                                    required_argument, NULL, ARG_ESP_PATH                                }, /* Compatibility alias */
393
                { "boot-path",                               required_argument, NULL, ARG_BOOT_PATH                               },
394
                { "root",                                    required_argument, NULL, ARG_ROOT                                    },
395
                { "image",                                   required_argument, NULL, ARG_IMAGE                                   },
396
                { "image-policy",                            required_argument, NULL, ARG_IMAGE_POLICY                            },
397
                { "install-source",                          required_argument, NULL, ARG_INSTALL_SOURCE                          },
398
                { "print-esp-path",                          no_argument,       NULL, 'p'                                         },
399
                { "print-path",                              no_argument,       NULL, 'p'                                         }, /* Compatibility alias */
400
                { "print-boot-path",                         no_argument,       NULL, 'x'                                         },
401
                { "print-loader-path",                       no_argument,       NULL, ARG_PRINT_LOADER_PATH                       },
402
                { "print-stub-path",                         no_argument,       NULL, ARG_PRINT_STUB_PATH                         },
403
                { "print-root-device",                       no_argument,       NULL, 'R'                                         },
404
                { "variables",                               required_argument, NULL, ARG_VARIABLES                               },
405
                { "no-variables",                            no_argument,       NULL, ARG_NO_VARIABLES                            }, /* Compatibility alias */
406
                { "random-seed",                             required_argument, NULL, ARG_RANDOM_SEED                             },
407
                { "no-pager",                                no_argument,       NULL, ARG_NO_PAGER                                },
408
                { "graceful",                                no_argument,       NULL, ARG_GRACEFUL                                },
409
                { "quiet",                                   no_argument,       NULL, 'q'                                         },
410
                { "make-entry-directory",                    required_argument, NULL, ARG_MAKE_ENTRY_DIRECTORY                    },
411
                { "make-machine-id-directory",               required_argument, NULL, ARG_MAKE_ENTRY_DIRECTORY                    }, /* Compatibility alias */
412
                { "entry-token",                             required_argument, NULL, ARG_ENTRY_TOKEN                             },
413
                { "json",                                    required_argument, NULL, ARG_JSON                                    },
414
                { "all-architectures",                       no_argument,       NULL, ARG_ARCH_ALL                                },
415
                { "efi-boot-option-description",             required_argument, NULL, ARG_EFI_BOOT_OPTION_DESCRIPTION             },
416
                { "efi-boot-option-description-with-device", required_argument, NULL, ARG_EFI_BOOT_OPTION_DESCRIPTION_WITH_DEVICE },
417
                { "dry-run",                                 no_argument,       NULL, ARG_DRY_RUN                                 },
418
                { "secure-boot-auto-enroll",                 required_argument, NULL, ARG_SECURE_BOOT_AUTO_ENROLL                 },
419
                { "certificate",                             required_argument, NULL, ARG_CERTIFICATE                             },
420
                { "certificate-source",                      required_argument, NULL, ARG_CERTIFICATE_SOURCE                      },
421
                { "private-key",                             required_argument, NULL, ARG_PRIVATE_KEY                             },
422
                { "private-key-source",                      required_argument, NULL, ARG_PRIVATE_KEY_SOURCE                      },
423
                {}
424
        };
425

426
        int c, r;
297✔
427

428
        assert(argc >= 0);
297✔
429
        assert(argv);
297✔
430

431
        while ((c = getopt_long(argc, argv, "hpxRq", options, NULL)) >= 0)
728✔
432
                switch (c) {
437✔
433

434
                case 'h':
3✔
435
                        help(0, NULL, NULL);
3✔
436
                        return 0;
3✔
437

438
                case ARG_VERSION:
3✔
439
                        return version();
3✔
440

441
                case ARG_ESP_PATH:
3✔
442
                        r = free_and_strdup(&arg_esp_path, optarg);
3✔
443
                        if (r < 0)
3✔
444
                                return log_oom();
×
445
                        break;
446

447
                case ARG_BOOT_PATH:
3✔
448
                        r = free_and_strdup(&arg_xbootldr_path, optarg);
3✔
449
                        if (r < 0)
3✔
450
                                return log_oom();
×
451
                        break;
452

453
                case ARG_ROOT:
34✔
454
                        r = parse_path_argument(optarg, /* suppress_root= */ true, &arg_root);
34✔
455
                        if (r < 0)
34✔
456
                                return r;
457
                        break;
458

459
                case ARG_IMAGE:
29✔
460
                        r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_image);
29✔
461
                        if (r < 0)
29✔
462
                                return r;
463
                        break;
464

465
                case ARG_IMAGE_POLICY:
×
466
                        r = parse_image_policy_argument(optarg, &arg_image_policy);
×
467
                        if (r < 0)
×
468
                                return r;
469
                        break;
470

471
                case ARG_INSTALL_SOURCE: {
×
472
                        InstallSource is = install_source_from_string(optarg);
×
473
                        if (is < 0)
×
474
                                return log_error_errno(is, "Unexpected parameter for --install-source=: %s", optarg);
×
475

476
                        arg_install_source = is;
×
477
                        break;
×
478
                }
479

480
                case 'p':
47✔
481
                        arg_print_esp_path = true;
47✔
482
                        break;
47✔
483

484
                case 'x':
8✔
485
                        arg_print_dollar_boot_path = true;
8✔
486
                        break;
8✔
487

488
                case ARG_PRINT_LOADER_PATH:
×
489
                        arg_print_loader_path = true;
×
490
                        break;
×
491

492
                case ARG_PRINT_STUB_PATH:
6✔
493
                        arg_print_stub_path = true;
6✔
494
                        break;
6✔
495

496
                case 'R':
7✔
497
                        arg_print_root_device++;
7✔
498
                        break;
7✔
499

500
                case ARG_VARIABLES:
118✔
501
                        r = parse_tristate_argument_with_auto("--variables=", optarg, &arg_touch_variables);
118✔
502
                        if (r < 0)
118✔
503
                                return r;
504
#if !ENABLE_EFI
505
                        if (arg_touch_variables > 0)
506
                                return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
507
                                                       "Compiled without support for EFI, --variables=%s cannot be specified.", optarg);
508
#endif
509
                        break;
510

511
                case ARG_NO_VARIABLES:
×
512
                        arg_touch_variables = false;
×
513
                        break;
×
514

515
                case ARG_RANDOM_SEED:
×
516
                        r = parse_boolean_argument("--random-seed=", optarg, &arg_install_random_seed);
×
517
                        if (r < 0)
×
518
                                return r;
519
                        break;
520

521
                case ARG_NO_PAGER:
×
522
                        arg_pager_flags |= PAGER_DISABLE;
×
523
                        break;
×
524

525
                case ARG_GRACEFUL:
136✔
526
                        _arg_graceful = ARG_GRACEFUL_YES;
136✔
527
                        break;
136✔
528

529
                case 'q':
6✔
530
                        arg_quiet = true;
6✔
531
                        break;
6✔
532

533
                case ARG_ENTRY_TOKEN:
×
534
                        r = parse_boot_entry_token_type(optarg, &arg_entry_token_type, &arg_entry_token);
×
535
                        if (r < 0)
×
536
                                return r;
537
                        break;
538

539
                case ARG_MAKE_ENTRY_DIRECTORY:
13✔
540
                        if (streq(optarg, "auto"))  /* retained for backwards compatibility */
13✔
541
                                arg_make_entry_directory = -1; /* yes if machine-id is permanent */
×
542
                        else {
543
                                r = parse_boolean_argument("--make-entry-directory=", optarg, NULL);
13✔
544
                                if (r < 0)
13✔
545
                                        return r;
546

547
                                arg_make_entry_directory = r;
13✔
548
                        }
549
                        break;
550

551
                case ARG_JSON:
6✔
552
                        r = parse_json_argument(optarg, &arg_json_format_flags);
6✔
553
                        if (r <= 0)
6✔
554
                                return r;
555
                        break;
556

557
                case ARG_ARCH_ALL:
12✔
558
                        arg_arch_all = true;
12✔
559
                        break;
12✔
560

561
                case ARG_EFI_BOOT_OPTION_DESCRIPTION:
×
562
                        if (isempty(optarg) || !(string_is_safe(optarg) && utf8_is_valid(optarg))) {
×
563
                                _cleanup_free_ char *escaped = cescape(optarg);
×
564
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
565
                                                       "Invalid --efi-boot-option-description=: %s", strna(escaped));
566
                        }
567
                        if (strlen(optarg) > EFI_BOOT_OPTION_DESCRIPTION_MAX)
×
568
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
569
                                                       "--efi-boot-option-description= too long: %zu > %zu",
570
                                                       strlen(optarg), EFI_BOOT_OPTION_DESCRIPTION_MAX);
571
                        r = free_and_strdup_warn(&arg_efi_boot_option_description, optarg);
×
572
                        if (r < 0)
×
573
                                return r;
574
                        break;
575

576
                case ARG_EFI_BOOT_OPTION_DESCRIPTION_WITH_DEVICE:
×
577
                        r = parse_boolean_argument("--efi-boot-option-description-with-device=", optarg, &arg_efi_boot_option_description_with_device);
×
578
                        if (r < 0)
×
579
                                return r;
580

581
                        break;
582

583
                case ARG_DRY_RUN:
×
584
                        arg_dry_run = true;
×
585
                        break;
×
586

587
                case ARG_SECURE_BOOT_AUTO_ENROLL:
1✔
588
                        r = parse_boolean_argument("--secure-boot-auto-enroll=", optarg, &arg_secure_boot_auto_enroll);
1✔
589
                        if (r < 0)
1✔
590
                                return r;
591
                        break;
592

593
                case ARG_CERTIFICATE:
1✔
594
                        r = free_and_strdup_warn(&arg_certificate, optarg);
1✔
595
                        if (r < 0)
1✔
596
                                return r;
597
                        break;
598

599
                case ARG_CERTIFICATE_SOURCE:
×
600
                        r = parse_openssl_certificate_source_argument(
×
601
                                        optarg,
602
                                        &arg_certificate_source,
603
                                        &arg_certificate_source_type);
604
                        if (r < 0)
×
605
                                return r;
606
                        break;
607

608
                case ARG_PRIVATE_KEY: {
1✔
609
                        r = free_and_strdup_warn(&arg_private_key, optarg);
1✔
610
                        if (r < 0)
1✔
611
                                return r;
612
                        break;
613
                }
614

615
                case ARG_PRIVATE_KEY_SOURCE:
×
616
                        r = parse_openssl_key_source_argument(
×
617
                                        optarg,
618
                                        &arg_private_key_source,
619
                                        &arg_private_key_source_type);
620
                        if (r < 0)
×
621
                                return r;
622
                        break;
623

624
                case '?':
625
                        return -EINVAL;
626

627
                default:
×
628
                        assert_not_reached();
×
629
                }
630

631
        if (!!arg_print_esp_path + !!arg_print_dollar_boot_path + (arg_print_root_device > 0) + arg_print_loader_path + arg_print_stub_path > 1)
291✔
632
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
633
                                                       "--print-esp-path/-p, --print-boot-path/-x, --print-root-device=/-R, --print-loader-path, --print-stub-path cannot be combined.");
634

635
        if ((arg_root || arg_image) && argv[optind] && !STR_IN_SET(argv[optind], "status", "list",
291✔
636
                        "install", "update", "remove", "is-installed", "random-seed", "unlink", "cleanup"))
637
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
638
                                       "Options --root= and --image= are not supported with verb %s.",
639
                                       argv[optind]);
640

641
        if (arg_root && arg_image)
291✔
642
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Please specify either --root= or --image=, the combination of both is not supported.");
×
643

644
        if (arg_install_source != INSTALL_SOURCE_AUTO && !arg_root && !arg_image)
291✔
645
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--install-from-host is only supported with --root= or --image=.");
×
646

647
        if (arg_dry_run && argv[optind] && !STR_IN_SET(argv[optind], "unlink", "cleanup"))
291✔
648
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--dry-run is only supported with --unlink or --cleanup");
×
649

650
        if (arg_secure_boot_auto_enroll) {
291✔
651
#if HAVE_OPENSSL
652
                if (!arg_certificate)
1✔
653
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Secure boot auto-enrollment requested but no certificate provided.");
×
654

655
                if (!arg_private_key)
1✔
656
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Secure boot auto-enrollment requested but no private key provided.");
×
657
#else
658
                return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Secure boot auto-enrollment requested but OpenSSL support is disabled.");
659
#endif
660
        }
661

662
        r = sd_varlink_invocation(SD_VARLINK_ALLOW_ACCEPT);
291✔
663
        if (r < 0)
291✔
664
                return log_error_errno(r, "Failed to check if invoked in Varlink mode: %m");
×
665
        if (r > 0) {
291✔
666
                arg_varlink = true;
14✔
667
                arg_pager_flags |= PAGER_DISABLE;
14✔
668
        }
669

670
        return 1;
671
}
672

673
static int bootctl_main(int argc, char *argv[]) {
266✔
674
        static const Verb verbs[] = {
266✔
675
                { "help",                VERB_ANY, VERB_ANY, 0,            help                     },
676
                { "status",              VERB_ANY, 1,        VERB_DEFAULT, verb_status              },
677
                { "install",             VERB_ANY, 1,        0,            verb_install             },
678
                { "update",              VERB_ANY, 1,        0,            verb_install             },
679
                { "remove",              VERB_ANY, 1,        0,            verb_remove              },
680
                { "is-installed",        VERB_ANY, 1,        0,            verb_is_installed        },
681
                { "kernel-identify",     2,        2,        0,            verb_kernel_identify     },
682
                { "kernel-inspect",      2,        2,        0,            verb_kernel_inspect      },
683
                { "list",                VERB_ANY, 1,        0,            verb_list                },
684
                { "unlink",              2,        2,        0,            verb_unlink              },
685
                { "cleanup",             VERB_ANY, 1,        0,            verb_cleanup             },
686
                { "set-default",         2,        2,        0,            verb_set_efivar          },
687
                { "set-preferred",       2,        2,        0,            verb_set_efivar          },
688
                { "set-oneshot",         2,        2,        0,            verb_set_efivar          },
689
                { "set-timeout",         2,        2,        0,            verb_set_efivar          },
690
                { "set-timeout-oneshot", 2,        2,        0,            verb_set_efivar          },
691
                { "set-sysfail",         2,        2,        0,            verb_set_efivar          },
692
                { "random-seed",         VERB_ANY, 1,        0,            verb_random_seed         },
693
                { "reboot-to-firmware",  VERB_ANY, 2,        0,            verb_reboot_to_firmware  },
694
                {}
695
        };
696

697
        return dispatch_verb(argc, argv, verbs, NULL);
266✔
698
}
699

700
static int vl_server(void) {
14✔
701
        _cleanup_(sd_varlink_server_unrefp) sd_varlink_server *varlink_server = NULL;
14✔
702
        int r;
14✔
703

704
        /* Invocation as Varlink service */
705

706
        r = varlink_server_new(
14✔
707
                        &varlink_server,
708
                        SD_VARLINK_SERVER_ROOT_ONLY|SD_VARLINK_SERVER_ALLOW_FD_PASSING_INPUT,
709
                        /* userdata= */ NULL);
710
        if (r < 0)
14✔
711
                return log_error_errno(r, "Failed to allocate Varlink server: %m");
×
712

713
        r = sd_varlink_server_add_interface(varlink_server, &vl_interface_io_systemd_BootControl);
14✔
714
        if (r < 0)
14✔
715
                return log_error_errno(r, "Failed to add Varlink interface: %m");
×
716

717
        r = sd_varlink_server_bind_method_many(
14✔
718
                        varlink_server,
719
                        "io.systemd.BootControl.ListBootEntries",     vl_method_list_boot_entries,
720
                        "io.systemd.BootControl.SetRebootToFirmware", vl_method_set_reboot_to_firmware,
721
                        "io.systemd.BootControl.GetRebootToFirmware", vl_method_get_reboot_to_firmware,
722
                        "io.systemd.BootControl.Install",             vl_method_install);
723
        if (r < 0)
14✔
724
                return log_error_errno(r, "Failed to bind Varlink methods: %m");
×
725

726
        r = sd_varlink_server_loop_auto(varlink_server);
14✔
727
        if (r < 0)
14✔
728
                return log_error_errno(r, "Failed to run Varlink event loop: %m");
×
729

730
        return 0;
731
}
732

733
static int run(int argc, char *argv[]) {
297✔
734
        _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
297✔
735
        _cleanup_(umount_and_freep) char *mounted_dir = NULL;
297✔
736
        int r;
297✔
737

738
        log_setup();
297✔
739

740
        r = parse_argv(argc, argv);
297✔
741
        if (r <= 0)
297✔
742
                return r;
743

744
        if (arg_varlink)
291✔
745
                return vl_server();
14✔
746

747
        if (arg_print_root_device > 0) {
277✔
748
                _cleanup_free_ char *path = NULL;
5✔
749
                dev_t devno;
5✔
750

751
                r = blockdev_get_root(LOG_ERR, &devno);
5✔
752
                if (r < 0)
5✔
753
                        return r;
754
                if (r == 0) {
5✔
755
                        log_error("Root file system not backed by a (single) whole block device.");
×
756
                        return 80; /* some recognizable error code */
×
757
                }
758

759
                if (arg_print_root_device > 1) {
5✔
760
                        r = block_get_whole_disk(devno, &devno);
2✔
761
                        if (r < 0)
2✔
762
                                log_debug_errno(r, "Unable to find whole block device for root block device, ignoring: %m");
×
763
                }
764

765
                r = device_path_make_canonical(S_IFBLK, devno, &path);
5✔
766
                if (r < 0)
5✔
767
                        return log_error_errno(r,
×
768
                                               "Failed to format canonical device path for devno '" DEVNUM_FORMAT_STR "': %m",
769
                                               DEVNUM_FORMAT_VAL(devno));
770

771
                puts(path);
5✔
772
                return 0;
773
        }
774

775
        if (arg_print_loader_path || arg_print_stub_path)
272✔
776
                return print_loader_or_stub_path();
6✔
777

778
        /* Open up and mount the image */
779
        if (arg_image) {
266✔
780
                assert(!arg_root);
26✔
781

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

795
                arg_root = strdup(mounted_dir);
26✔
796
                if (!arg_root)
26✔
797
                        return log_oom();
×
798
        }
799

800
        return bootctl_main(argc, argv);
266✔
801
}
802

803
DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);
297✔
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