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

systemd / systemd / 19020191358

02 Nov 2025 05:04PM UTC coverage: 72.222% (-0.02%) from 72.241%
19020191358

push

github

web-flow
Enhance docs for ukify and direct kernel boots (#39516)

305246 of 422650 relevant lines covered (72.22%)

1085243.28 hits per line

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

67.79
/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-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 "build.h"
18
#include "devnum-util.h"
19
#include "dissect-image.h"
20
#include "efi-loader.h"
21
#include "efivars.h"
22
#include "escape.h"
23
#include "find-esp.h"
24
#include "image-policy.h"
25
#include "log.h"
26
#include "loop-util.h"
27
#include "main-func.h"
28
#include "mount-util.h"
29
#include "openssl-util.h"
30
#include "pager.h"
31
#include "parse-argument.h"
32
#include "path-util.h"
33
#include "pretty-print.h"
34
#include "string-util.h"
35
#include "strv.h"
36
#include "utf8.h"
37
#include "varlink-io.systemd.BootControl.h"
38
#include "varlink-util.h"
39
#include "verbs.h"
40
#include "virt.h"
41

42
/* EFI_BOOT_OPTION_DESCRIPTION_MAX sets the maximum length for the boot option description
43
 * stored in NVRAM. The UEFI spec does not specify a minimum or maximum length for this
44
 * string, but we limit the length to something reasonable to prevent from the firmware
45
 * having to deal with a potentially too long string. */
46
#define EFI_BOOT_OPTION_DESCRIPTION_MAX ((size_t) 255)
47

48
static GracefulMode _arg_graceful = ARG_GRACEFUL_NO;
49

50
char *arg_esp_path = NULL;
51
char *arg_xbootldr_path = NULL;
52
bool arg_print_esp_path = false;
53
bool arg_print_dollar_boot_path = false;
54
bool arg_print_loader_path = false;
55
bool arg_print_stub_path = 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_dry_run = false;
73
ImagePolicy *arg_image_policy = NULL;
74
bool arg_varlink = false;
75
bool arg_secure_boot_auto_enroll = false;
76
char *arg_certificate = NULL;
77
CertificateSourceType arg_certificate_source_type = OPENSSL_CERTIFICATE_SOURCE_FILE;
78
char *arg_certificate_source = NULL;
79
char *arg_private_key = NULL;
80
KeySourceType arg_private_key_source_type = OPENSSL_KEY_SOURCE_FILE;
81
char *arg_private_key_source = NULL;
82

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

96
int acquire_esp(
231✔
97
                int unprivileged_mode,
98
                bool graceful,
99
                uint32_t *ret_part,
100
                uint64_t *ret_pstart,
101
                uint64_t *ret_psize,
102
                sd_id128_t *ret_uuid,
103
                dev_t *ret_devid) {
104

105
        char *np;
231✔
106
        int r;
231✔
107

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

114
        r = find_esp_and_warn(arg_root, arg_esp_path, unprivileged_mode, &np, ret_part, ret_pstart, ret_psize, ret_uuid, ret_devid);
231✔
115
        if (r == -ENOKEY) {
231✔
116
                if (graceful)
5✔
117
                        return log_full_errno(arg_quiet ? LOG_DEBUG : LOG_INFO, r,
×
118
                                              "Couldn't find EFI system partition, skipping.");
119

120
                return log_error_errno(r,
5✔
121
                                       "Couldn't find EFI system partition. It is recommended to mount it to /boot/ or /efi/.\n"
122
                                       "Alternatively, use --esp-path= to specify path to mount point.");
123
        }
124
        if (r < 0)
226✔
125
                return r;
126

127
        free_and_replace(arg_esp_path, np);
225✔
128
        log_debug("Using EFI System Partition at %s.", arg_esp_path);
225✔
129

130
        return 0;
131
}
132

133
int acquire_xbootldr(
190✔
134
                int unprivileged_mode,
135
                sd_id128_t *ret_uuid,
136
                dev_t *ret_devid) {
137

138
        char *np;
190✔
139
        int r;
190✔
140

141
        r = find_xbootldr_and_warn(arg_root, arg_xbootldr_path, unprivileged_mode, &np, ret_uuid, ret_devid);
190✔
142
        if (r == -ENOKEY) {
190✔
143
                log_debug_errno(r, "Didn't find an XBOOTLDR partition, using the ESP as $BOOT.");
96✔
144
                arg_xbootldr_path = mfree(arg_xbootldr_path);
96✔
145

146
                if (ret_uuid)
96✔
147
                        *ret_uuid = SD_ID128_NULL;
15✔
148
                if (ret_devid)
96✔
149
                        *ret_devid = 0;
20✔
150
                return 0;
96✔
151
        }
152
        if (r < 0)
94✔
153
                return r;
154

155
        free_and_replace(arg_xbootldr_path, np);
93✔
156
        log_debug("Using XBOOTLDR partition at %s as $BOOT.", arg_xbootldr_path);
93✔
157

158
        return 1;
159
}
160

161
static int print_loader_or_stub_path(void) {
6✔
162
        _cleanup_free_ char *p = NULL;
6✔
163
        sd_id128_t uuid;
6✔
164
        int r;
6✔
165

166
        if (arg_print_loader_path) {
6✔
167
                r = efi_loader_get_device_part_uuid(&uuid);
×
168
                if (r == -ENOENT)
×
169
                        return log_error_errno(r, "No loader partition UUID passed.");
×
170
                if (r < 0)
×
171
                        return log_error_errno(r, "Unable to determine loader partition UUID: %m");
×
172

173
                r = efi_get_variable_path(EFI_LOADER_VARIABLE_STR("LoaderImageIdentifier"), &p);
×
174
                if (r == -ENOENT)
×
175
                        return log_error_errno(r, "No loader EFI binary path passed.");
×
176
                if (r < 0)
×
177
                        return log_error_errno(r, "Unable to determine loader EFI binary path: %m");
×
178
        } else {
179
                assert(arg_print_stub_path);
6✔
180

181
                r = efi_stub_get_device_part_uuid(&uuid);
6✔
182
                if (r == -ENOENT)
6✔
183
                        return log_error_errno(r, "No stub partition UUID passed.");
×
184
                if (r < 0)
6✔
185
                        return log_error_errno(r, "Unable to determine stub partition UUID: %m");
×
186

187
                r = efi_get_variable_path(EFI_LOADER_VARIABLE_STR("StubImageIdentifier"), &p);
6✔
188
                if (r == -ENOENT)
6✔
189
                        return log_error_errno(r, "No stub EFI binary path passed.");
×
190
                if (r < 0)
6✔
191
                        return log_error_errno(r, "Unable to determine stub EFI binary path: %m");
×
192
        }
193

194
        sd_id128_t esp_uuid;
6✔
195
        r = acquire_esp(/* unprivileged_mode= */ false, /* graceful= */ false,
6✔
196
                        /* ret_part= */ NULL, /* ret_pstart= */ NULL, /* ret_psize= */ NULL,
197
                        &esp_uuid, /* ret_devid= */ NULL);
198
        if (r < 0)
6✔
199
                return r;
200

201
        const char *found_path = NULL;
6✔
202
        if (sd_id128_equal(esp_uuid, uuid))
6✔
203
                found_path = arg_esp_path;
6✔
204
        else if (arg_print_stub_path) { /* In case of the stub, also look for things in the xbootldr partition */
×
205
                sd_id128_t xbootldr_uuid;
×
206

207
                r = acquire_xbootldr(/* unprivileged_mode= */ false, &xbootldr_uuid, /* ret_devid= */ NULL);
×
208
                if (r < 0)
×
209
                        return r;
×
210

211
                if (sd_id128_equal(xbootldr_uuid, uuid))
×
212
                        found_path = arg_xbootldr_path;
×
213
        }
214

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

218
        _cleanup_free_ char *j = path_join(found_path, p);
12✔
219
        if (!j)
6✔
220
                return log_oom();
×
221

222
        puts(j);
6✔
223
        return 0;
224
}
225

226
bool touch_variables(void) {
173✔
227
        /* If we run in a container or on a non-EFI system, automatically turn off EFI file system access,
228
         * unless explicitly overridden. */
229

230
        if (arg_touch_variables >= 0)
173✔
231
                return arg_touch_variables;
119✔
232

233
        if (arg_root) {
54✔
234
                log_once(LOG_NOTICE,
38✔
235
                         "Operating on %s, skipping EFI variable modifications.",
236
                         arg_image ? "image" : "root directory");
237
                return false;
28✔
238
        }
239

240
        if (!is_efi_boot()) { /* NB: this internally checks if we run in a container */
26✔
241
                log_once(LOG_NOTICE,
×
242
                         "Not booted with EFI or running in a container, skipping EFI variable modifications.");
243
                return false;
×
244
        }
245

246
        return true;
247
}
248

249
GracefulMode arg_graceful(void) {
419✔
250
        static bool chroot_checked = false;
419✔
251

252
        if (!chroot_checked && running_in_chroot() > 0) {
419✔
253
                if (_arg_graceful == ARG_GRACEFUL_NO)
×
254
                        log_full(arg_quiet ? LOG_DEBUG : LOG_INFO, "Running in a chroot, enabling --graceful.");
×
255

256
                _arg_graceful = ARG_GRACEFUL_FORCE;
×
257
        }
258

259
        chroot_checked = true;
419✔
260

261
        return _arg_graceful;
419✔
262
}
263

264
static int help(int argc, char *argv[], void *userdata) {
3✔
265
        _cleanup_free_ char *link = NULL;
3✔
266
        int r;
3✔
267

268
        pager_open(arg_pager_flags);
3✔
269

270
        r = terminal_urlify_man("bootctl", "1", &link);
3✔
271
        if (r < 0)
3✔
272
                return log_oom();
×
273

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

370
        return 0;
371
}
372

373
static int parse_argv(int argc, char *argv[]) {
272✔
374
        enum {
272✔
375
                ARG_ESP_PATH = 0x100,
376
                ARG_BOOT_PATH,
377
                ARG_ROOT,
378
                ARG_IMAGE,
379
                ARG_IMAGE_POLICY,
380
                ARG_INSTALL_SOURCE,
381
                ARG_VERSION,
382
                ARG_VARIABLES,
383
                ARG_NO_VARIABLES,
384
                ARG_RANDOM_SEED,
385
                ARG_NO_PAGER,
386
                ARG_GRACEFUL,
387
                ARG_MAKE_ENTRY_DIRECTORY,
388
                ARG_ENTRY_TOKEN,
389
                ARG_JSON,
390
                ARG_ARCH_ALL,
391
                ARG_EFI_BOOT_OPTION_DESCRIPTION,
392
                ARG_DRY_RUN,
393
                ARG_PRINT_LOADER_PATH,
394
                ARG_PRINT_STUB_PATH,
395
                ARG_SECURE_BOOT_AUTO_ENROLL,
396
                ARG_CERTIFICATE,
397
                ARG_CERTIFICATE_SOURCE,
398
                ARG_PRIVATE_KEY,
399
                ARG_PRIVATE_KEY_SOURCE,
400
        };
401

402
        static const struct option options[] = {
272✔
403
                { "help",                        no_argument,       NULL, 'h'                             },
404
                { "version",                     no_argument,       NULL, ARG_VERSION                     },
405
                { "esp-path",                    required_argument, NULL, ARG_ESP_PATH                    },
406
                { "path",                        required_argument, NULL, ARG_ESP_PATH                    }, /* Compatibility alias */
407
                { "boot-path",                   required_argument, NULL, ARG_BOOT_PATH                   },
408
                { "root",                        required_argument, NULL, ARG_ROOT                        },
409
                { "image",                       required_argument, NULL, ARG_IMAGE                       },
410
                { "image-policy",                required_argument, NULL, ARG_IMAGE_POLICY                },
411
                { "install-source",              required_argument, NULL, ARG_INSTALL_SOURCE              },
412
                { "print-esp-path",              no_argument,       NULL, 'p'                             },
413
                { "print-path",                  no_argument,       NULL, 'p'                             }, /* Compatibility alias */
414
                { "print-boot-path",             no_argument,       NULL, 'x'                             },
415
                { "print-loader-path",           no_argument,       NULL, ARG_PRINT_LOADER_PATH           },
416
                { "print-stub-path",             no_argument,       NULL, ARG_PRINT_STUB_PATH             },
417
                { "print-root-device",           no_argument,       NULL, 'R'                             },
418
                { "variables",                   required_argument, NULL, ARG_VARIABLES                   },
419
                { "no-variables",                no_argument,       NULL, ARG_NO_VARIABLES                }, /* Compatibility alias */
420
                { "random-seed",                 required_argument, NULL, ARG_RANDOM_SEED                 },
421
                { "no-pager",                    no_argument,       NULL, ARG_NO_PAGER                    },
422
                { "graceful",                    no_argument,       NULL, ARG_GRACEFUL                    },
423
                { "quiet",                       no_argument,       NULL, 'q'                             },
424
                { "make-entry-directory",        required_argument, NULL, ARG_MAKE_ENTRY_DIRECTORY        },
425
                { "make-machine-id-directory",   required_argument, NULL, ARG_MAKE_ENTRY_DIRECTORY        }, /* Compatibility alias */
426
                { "entry-token",                 required_argument, NULL, ARG_ENTRY_TOKEN                 },
427
                { "json",                        required_argument, NULL, ARG_JSON                        },
428
                { "all-architectures",           no_argument,       NULL, ARG_ARCH_ALL                    },
429
                { "efi-boot-option-description", required_argument, NULL, ARG_EFI_BOOT_OPTION_DESCRIPTION },
430
                { "dry-run",                     no_argument,       NULL, ARG_DRY_RUN                     },
431
                { "secure-boot-auto-enroll",     required_argument, NULL, ARG_SECURE_BOOT_AUTO_ENROLL     },
432
                { "certificate",                 required_argument, NULL, ARG_CERTIFICATE                 },
433
                { "certificate-source",          required_argument, NULL, ARG_CERTIFICATE_SOURCE          },
434
                { "private-key",                 required_argument, NULL, ARG_PRIVATE_KEY                 },
435
                { "private-key-source",          required_argument, NULL, ARG_PRIVATE_KEY_SOURCE          },
436
                {}
437
        };
438

439
        int c, r;
272✔
440

441
        assert(argc >= 0);
272✔
442
        assert(argv);
272✔
443

444
        while ((c = getopt_long(argc, argv, "hpxRq", options, NULL)) >= 0)
681✔
445
                switch (c) {
415✔
446

447
                case 'h':
3✔
448
                        help(0, NULL, NULL);
3✔
449
                        return 0;
3✔
450

451
                case ARG_VERSION:
3✔
452
                        return version();
3✔
453

454
                case ARG_ESP_PATH:
3✔
455
                        r = free_and_strdup(&arg_esp_path, optarg);
3✔
456
                        if (r < 0)
3✔
457
                                return log_oom();
×
458
                        break;
459

460
                case ARG_BOOT_PATH:
3✔
461
                        r = free_and_strdup(&arg_xbootldr_path, optarg);
3✔
462
                        if (r < 0)
3✔
463
                                return log_oom();
×
464
                        break;
465

466
                case ARG_ROOT:
34✔
467
                        r = parse_path_argument(optarg, /* suppress_root= */ true, &arg_root);
34✔
468
                        if (r < 0)
34✔
469
                                return r;
470
                        break;
471

472
                case ARG_IMAGE:
29✔
473
                        r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_image);
29✔
474
                        if (r < 0)
29✔
475
                                return r;
476
                        break;
477

478
                case ARG_IMAGE_POLICY:
×
479
                        r = parse_image_policy_argument(optarg, &arg_image_policy);
×
480
                        if (r < 0)
×
481
                                return r;
482
                        break;
483

484
                case ARG_INSTALL_SOURCE:
×
485
                        if (streq(optarg, "auto"))
×
486
                                arg_install_source = INSTALL_SOURCE_AUTO;
×
487
                        else if (streq(optarg, "image"))
×
488
                                arg_install_source = INSTALL_SOURCE_IMAGE;
×
489
                        else if (streq(optarg, "host"))
×
490
                                arg_install_source = INSTALL_SOURCE_HOST;
×
491
                        else
492
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
493
                                                       "Unexpected parameter for --install-source=: %s", optarg);
494

495
                        break;
496

497
                case 'p':
23✔
498
                        arg_print_esp_path = true;
23✔
499
                        break;
23✔
500

501
                case 'x':
8✔
502
                        arg_print_dollar_boot_path = true;
8✔
503
                        break;
8✔
504

505
                case ARG_PRINT_LOADER_PATH:
×
506
                        arg_print_loader_path = true;
×
507
                        break;
×
508

509
                case ARG_PRINT_STUB_PATH:
6✔
510
                        arg_print_stub_path = true;
6✔
511
                        break;
6✔
512

513
                case 'R':
7✔
514
                        arg_print_root_device++;
7✔
515
                        break;
7✔
516

517
                case ARG_VARIABLES:
119✔
518
                        r = parse_tristate_argument("--variables=", optarg, &arg_touch_variables);
119✔
519
                        if (r < 0)
119✔
520
                                return r;
521
                        break;
522

523
                case ARG_NO_VARIABLES:
×
524
                        arg_touch_variables = false;
×
525
                        break;
×
526

527
                case ARG_RANDOM_SEED:
×
528
                        r = parse_boolean_argument("--random-seed=", optarg, &arg_install_random_seed);
×
529
                        if (r < 0)
×
530
                                return r;
531
                        break;
532

533
                case ARG_NO_PAGER:
×
534
                        arg_pager_flags |= PAGER_DISABLE;
×
535
                        break;
×
536

537
                case ARG_GRACEFUL:
137✔
538
                        _arg_graceful = ARG_GRACEFUL_YES;
137✔
539
                        break;
137✔
540

541
                case 'q':
6✔
542
                        arg_quiet = true;
6✔
543
                        break;
6✔
544

545
                case ARG_ENTRY_TOKEN:
×
546
                        r = parse_boot_entry_token_type(optarg, &arg_entry_token_type, &arg_entry_token);
×
547
                        if (r < 0)
×
548
                                return r;
549
                        break;
550

551
                case ARG_MAKE_ENTRY_DIRECTORY:
13✔
552
                        if (streq(optarg, "auto"))  /* retained for backwards compatibility */
13✔
553
                                arg_make_entry_directory = -1; /* yes if machine-id is permanent */
×
554
                        else {
555
                                r = parse_boolean_argument("--make-entry-directory=", optarg, NULL);
13✔
556
                                if (r < 0)
13✔
557
                                        return r;
558

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

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

569
                case ARG_ARCH_ALL:
12✔
570
                        arg_arch_all = true;
12✔
571
                        break;
12✔
572

573
                case ARG_EFI_BOOT_OPTION_DESCRIPTION:
×
574
                        if (isempty(optarg) || !(string_is_safe(optarg) && utf8_is_valid(optarg))) {
×
575
                                _cleanup_free_ char *escaped = NULL;
×
576

577
                                escaped = cescape(optarg);
×
578
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
579
                                                       "Invalid --efi-boot-option-description=: %s", strna(escaped));
580
                        }
581
                        if (strlen(optarg) > EFI_BOOT_OPTION_DESCRIPTION_MAX)
×
582
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
583
                                                       "--efi-boot-option-description= too long: %zu > %zu",
584
                                                       strlen(optarg), EFI_BOOT_OPTION_DESCRIPTION_MAX);
585
                        r = free_and_strdup_warn(&arg_efi_boot_option_description, optarg);
×
586
                        if (r < 0)
×
587
                                return r;
588
                        break;
589

590
                case ARG_DRY_RUN:
×
591
                        arg_dry_run = true;
×
592
                        break;
×
593

594
                case ARG_SECURE_BOOT_AUTO_ENROLL:
1✔
595
                        r = parse_boolean_argument("--secure-boot-auto-enroll=", optarg, &arg_secure_boot_auto_enroll);
1✔
596
                        if (r < 0)
1✔
597
                                return r;
598
                        break;
599

600
                case ARG_CERTIFICATE:
1✔
601
                        r = free_and_strdup_warn(&arg_certificate, optarg);
1✔
602
                        if (r < 0)
1✔
603
                                return r;
604
                        break;
605

606
                case ARG_CERTIFICATE_SOURCE:
×
607
                        r = parse_openssl_certificate_source_argument(
×
608
                                        optarg,
609
                                        &arg_certificate_source,
610
                                        &arg_certificate_source_type);
611
                        if (r < 0)
×
612
                                return r;
613
                        break;
614

615
                case ARG_PRIVATE_KEY: {
1✔
616
                        r = free_and_strdup_warn(&arg_private_key, optarg);
1✔
617
                        if (r < 0)
1✔
618
                                return r;
619
                        break;
620
                }
621

622
                case ARG_PRIVATE_KEY_SOURCE:
×
623
                        r = parse_openssl_key_source_argument(
×
624
                                        optarg,
625
                                        &arg_private_key_source,
626
                                        &arg_private_key_source_type);
627
                        if (r < 0)
×
628
                                return r;
629
                        break;
630

631
                case '?':
632
                        return -EINVAL;
633

634
                default:
×
635
                        assert_not_reached();
×
636
                }
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 > 1)
266✔
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 cannot be combined.");
641

642
        if ((arg_root || arg_image) && argv[optind] && !STR_IN_SET(argv[optind], "status", "list",
266✔
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
                                       argv[optind]);
647

648
        if (arg_root && arg_image)
266✔
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)
266✔
652
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--install-from-host is only supported with --root= or --image=.");
×
653

654
        if (arg_dry_run && argv[optind] && !STR_IN_SET(argv[optind], "unlink", "cleanup"))
266✔
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) {
266✔
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);
266✔
670
        if (r < 0)
266✔
671
                return log_error_errno(r, "Failed to check if invoked in Varlink mode: %m");
×
672
        if (r > 0) {
266✔
673
                arg_varlink = true;
14✔
674
                arg_pager_flags |= PAGER_DISABLE;
14✔
675
        }
676

677
        return 1;
678
}
679

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

703
        return dispatch_verb(argc, argv, verbs, NULL);
241✔
704
}
705

706
static int vl_server(void) {
14✔
707
        _cleanup_(sd_varlink_server_unrefp) sd_varlink_server *varlink_server = NULL;
14✔
708
        int r;
14✔
709

710
        /* Invocation as Varlink service */
711

712
        r = varlink_server_new(
14✔
713
                        &varlink_server,
714
                        SD_VARLINK_SERVER_ROOT_ONLY,
715
                        /* userdata= */ NULL);
716
        if (r < 0)
14✔
717
                return log_error_errno(r, "Failed to allocate Varlink server: %m");
×
718

719
        r = sd_varlink_server_add_interface(varlink_server, &vl_interface_io_systemd_BootControl);
14✔
720
        if (r < 0)
14✔
721
                return log_error_errno(r, "Failed to add Varlink interface: %m");
×
722

723
        r = sd_varlink_server_bind_method_many(
14✔
724
                        varlink_server,
725
                        "io.systemd.BootControl.ListBootEntries",     vl_method_list_boot_entries,
726
                        "io.systemd.BootControl.SetRebootToFirmware", vl_method_set_reboot_to_firmware,
727
                        "io.systemd.BootControl.GetRebootToFirmware", vl_method_get_reboot_to_firmware);
728
        if (r < 0)
14✔
729
                return log_error_errno(r, "Failed to bind Varlink methods: %m");
×
730

731
        r = sd_varlink_server_loop_auto(varlink_server);
14✔
732
        if (r < 0)
14✔
733
                return log_error_errno(r, "Failed to run Varlink event loop: %m");
×
734

735
        return 0;
736
}
737

738
static int run(int argc, char *argv[]) {
272✔
739
        _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
272✔
740
        _cleanup_(umount_and_freep) char *mounted_dir = NULL;
272✔
741
        int r;
272✔
742

743
        log_setup();
272✔
744

745
        r = parse_argv(argc, argv);
272✔
746
        if (r <= 0)
272✔
747
                return r;
748

749
        if (arg_varlink)
266✔
750
                return vl_server();
14✔
751

752
        if (arg_print_root_device > 0) {
252✔
753
                _cleanup_free_ char *path = NULL;
5✔
754
                dev_t devno;
5✔
755

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

764
                if (arg_print_root_device > 1) {
5✔
765
                        r = block_get_whole_disk(devno, &devno);
2✔
766
                        if (r < 0)
2✔
767
                                log_debug_errno(r, "Unable to find whole block device for root block device, ignoring: %m");
×
768
                }
769

770
                r = device_path_make_canonical(S_IFBLK, devno, &path);
5✔
771
                if (r < 0)
5✔
772
                        return log_error_errno(r,
×
773
                                               "Failed to format canonical device path for devno '" DEVNUM_FORMAT_STR "': %m",
774
                                               DEVNUM_FORMAT_VAL(devno));
775

776
                puts(path);
5✔
777
                return 0;
778
        }
779

780
        if (arg_print_loader_path || arg_print_stub_path)
247✔
781
                return print_loader_or_stub_path();
6✔
782

783
        /* Open up and mount the image */
784
        if (arg_image) {
241✔
785
                assert(!arg_root);
26✔
786

787
                r = mount_image_privately_interactively(
26✔
788
                                arg_image,
789
                                arg_image_policy,
790
                                DISSECT_IMAGE_GENERIC_ROOT |
791
                                DISSECT_IMAGE_USR_NO_ROOT |
792
                                DISSECT_IMAGE_RELAX_VAR_CHECK |
793
                                DISSECT_IMAGE_ALLOW_USERSPACE_VERITY,
794
                                &mounted_dir,
795
                                /* ret_dir_fd= */ NULL,
796
                                &loop_device);
797
                if (r < 0)
26✔
798
                        return r;
799

800
                arg_root = strdup(mounted_dir);
26✔
801
                if (!arg_root)
26✔
802
                        return log_oom();
×
803
        }
804

805
        return bootctl_main(argc, argv);
241✔
806
}
807

808
DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);
272✔
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