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

systemd / systemd / 16979578868

14 Aug 2025 05:18PM UTC coverage: 72.036% (-0.2%) from 72.212%
16979578868

push

github

bluca
mkosi: install util-linux-script on F44

Once F41 is EOL we can just move this to the main list and
stop doing this dance every 6 months

301650 of 418750 relevant lines covered (72.04%)

661496.21 hits per line

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

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

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

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

104
        char *np;
235✔
105
        int r;
235✔
106

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

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

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

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

129
        return 0;
130
}
131

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

137
        char *np;
194✔
138
        int r;
194✔
139

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

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

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

157
        return 1;
158
}
159

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

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

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

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

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

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

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

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

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

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

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

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

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

229
        if (arg_touch_variables >= 0)
177✔
230
                return arg_touch_variables;
123✔
231

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

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

245
        return true;
246
}
247

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

252
        pager_open(arg_pager_flags);
3✔
253

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

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

354
        return 0;
355
}
356

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

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

423
        int c, r;
276✔
424

425
        assert(argc >= 0);
276✔
426
        assert(argv);
276✔
427

428
        while ((c = getopt_long(argc, argv, "hpxRq", options, NULL)) >= 0)
693✔
429
                switch (c) {
423✔
430

431
                case 'h':
3✔
432
                        help(0, NULL, NULL);
3✔
433
                        return 0;
3✔
434

435
                case ARG_VERSION:
3✔
436
                        return version();
3✔
437

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

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

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

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

462
                case ARG_IMAGE_POLICY:
×
463
                        r = parse_image_policy_argument(optarg, &arg_image_policy);
×
464
                        if (r < 0)
×
465
                                return r;
466
                        break;
467

468
                case ARG_INSTALL_SOURCE:
×
469
                        if (streq(optarg, "auto"))
×
470
                                arg_install_source = ARG_INSTALL_SOURCE_AUTO;
×
471
                        else if (streq(optarg, "image"))
×
472
                                arg_install_source = ARG_INSTALL_SOURCE_IMAGE;
×
473
                        else if (streq(optarg, "host"))
×
474
                                arg_install_source = ARG_INSTALL_SOURCE_HOST;
×
475
                        else
476
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
477
                                                       "Unexpected parameter for --install-source=: %s", optarg);
478

479
                        break;
480

481
                case 'p':
23✔
482
                        arg_print_esp_path = true;
23✔
483
                        break;
23✔
484

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

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

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

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

501
                case ARG_VARIABLES:
×
502
                        r = parse_tristate_argument("--variables=", optarg, &arg_touch_variables);
×
503
                        if (r < 0)
×
504
                                return r;
505
                        break;
506

507
                case ARG_NO_VARIABLES:
123✔
508
                        arg_touch_variables = false;
123✔
509
                        break;
123✔
510

511
                case ARG_RANDOM_SEED:
×
512
                        r = parse_boolean_argument("--random-seed=", optarg, &arg_install_random_seed);
×
513
                        if (r < 0)
×
514
                                return r;
515
                        break;
516

517
                case ARG_NO_PAGER:
×
518
                        arg_pager_flags |= PAGER_DISABLE;
×
519
                        break;
×
520

521
                case ARG_GRACEFUL:
141✔
522
                        arg_graceful = true;
141✔
523
                        break;
141✔
524

525
                case 'q':
6✔
526
                        arg_quiet = true;
6✔
527
                        break;
6✔
528

529
                case ARG_ENTRY_TOKEN:
×
530
                        r = parse_boot_entry_token_type(optarg, &arg_entry_token_type, &arg_entry_token);
×
531
                        if (r < 0)
×
532
                                return r;
533
                        break;
534

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

543
                                arg_make_entry_directory = r;
13✔
544
                        }
545
                        break;
546

547
                case ARG_JSON:
6✔
548
                        r = parse_json_argument(optarg, &arg_json_format_flags);
6✔
549
                        if (r <= 0)
6✔
550
                                return r;
551
                        break;
552

553
                case ARG_ARCH_ALL:
12✔
554
                        arg_arch_all = true;
12✔
555
                        break;
12✔
556

557
                case ARG_EFI_BOOT_OPTION_DESCRIPTION:
×
558
                        if (isempty(optarg) || !(string_is_safe(optarg) && utf8_is_valid(optarg))) {
×
559
                                _cleanup_free_ char *escaped = NULL;
×
560

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

574
                case ARG_DRY_RUN:
×
575
                        arg_dry_run = true;
×
576
                        break;
×
577

578
                case ARG_SECURE_BOOT_AUTO_ENROLL:
1✔
579
                        r = parse_boolean_argument("--secure-boot-auto-enroll=", optarg, &arg_secure_boot_auto_enroll);
1✔
580
                        if (r < 0)
1✔
581
                                return r;
582
                        break;
583

584
                case ARG_CERTIFICATE:
1✔
585
                        r = free_and_strdup_warn(&arg_certificate, optarg);
1✔
586
                        if (r < 0)
1✔
587
                                return r;
588
                        break;
589

590
                case ARG_CERTIFICATE_SOURCE:
×
591
                        r = parse_openssl_certificate_source_argument(
×
592
                                        optarg,
593
                                        &arg_certificate_source,
594
                                        &arg_certificate_source_type);
595
                        if (r < 0)
×
596
                                return r;
597
                        break;
598

599
                case ARG_PRIVATE_KEY: {
1✔
600
                        r = free_and_strdup_warn(&arg_private_key, optarg);
1✔
601
                        if (r < 0)
1✔
602
                                return r;
603
                        break;
604
                }
605

606
                case ARG_PRIVATE_KEY_SOURCE:
×
607
                        r = parse_openssl_key_source_argument(
×
608
                                        optarg,
609
                                        &arg_private_key_source,
610
                                        &arg_private_key_source_type);
611
                        if (r < 0)
×
612
                                return r;
613
                        break;
614

615
                case '?':
616
                        return -EINVAL;
617

618
                default:
×
619
                        assert_not_reached();
×
620
                }
621

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

626
        if ((arg_root || arg_image) && argv[optind] && !STR_IN_SET(argv[optind], "status", "list",
270✔
627
                        "install", "update", "remove", "is-installed", "random-seed", "unlink", "cleanup"))
628
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
629
                                       "Options --root= and --image= are not supported with verb %s.",
630
                                       argv[optind]);
631

632
        if (arg_root && arg_image)
270✔
633
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Please specify either --root= or --image=, the combination of both is not supported.");
×
634

635
        if (arg_install_source != ARG_INSTALL_SOURCE_AUTO && !arg_root && !arg_image)
270✔
636
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--install-from-host is only supported with --root= or --image=.");
×
637

638
        if (arg_dry_run && argv[optind] && !STR_IN_SET(argv[optind], "unlink", "cleanup"))
270✔
639
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--dry is only supported with --unlink or --cleanup");
×
640

641
        if (arg_secure_boot_auto_enroll && !arg_certificate)
270✔
642
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Secure boot auto-enrollment requested but no certificate provided");
×
643

644
        if (arg_secure_boot_auto_enroll && !arg_private_key)
270✔
645
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Secure boot auto-enrollment requested but no private key provided");
×
646

647
        if (!arg_graceful && running_in_chroot() > 0) {
270✔
648
                log_full(arg_quiet ? LOG_DEBUG : LOG_INFO, "Running in a chroot, enabling --graceful.");
×
649
                arg_graceful = true;
×
650
        }
651

652
        r = sd_varlink_invocation(SD_VARLINK_ALLOW_ACCEPT);
270✔
653
        if (r < 0)
270✔
654
                return log_error_errno(r, "Failed to check if invoked in Varlink mode: %m");
×
655
        if (r > 0) {
270✔
656
                arg_varlink = true;
14✔
657
                arg_pager_flags |= PAGER_DISABLE;
14✔
658
        }
659

660
        return 1;
661
}
662

663
static int bootctl_main(int argc, char *argv[]) {
245✔
664
        static const Verb verbs[] = {
245✔
665
                { "help",                VERB_ANY, VERB_ANY, 0,            help                     },
666
                { "status",              VERB_ANY, 1,        VERB_DEFAULT, verb_status              },
667
                { "install",             VERB_ANY, 1,        0,            verb_install             },
668
                { "update",              VERB_ANY, 1,        0,            verb_install             },
669
                { "remove",              VERB_ANY, 1,        0,            verb_remove              },
670
                { "is-installed",        VERB_ANY, 1,        0,            verb_is_installed        },
671
                { "kernel-identify",     2,        2,        0,            verb_kernel_identify     },
672
                { "kernel-inspect",      2,        2,        0,            verb_kernel_inspect      },
673
                { "list",                VERB_ANY, 1,        0,            verb_list                },
674
                { "unlink",              2,        2,        0,            verb_unlink              },
675
                { "cleanup",             VERB_ANY, 1,        0,            verb_list                },
676
                { "set-default",         2,        2,        0,            verb_set_efivar          },
677
                { "set-oneshot",         2,        2,        0,            verb_set_efivar          },
678
                { "set-timeout",         2,        2,        0,            verb_set_efivar          },
679
                { "set-timeout-oneshot", 2,        2,        0,            verb_set_efivar          },
680
                { "set-sysfail",         2,        2,        0,            verb_set_efivar          },
681
                { "random-seed",         VERB_ANY, 1,        0,            verb_random_seed         },
682
                { "reboot-to-firmware",  VERB_ANY, 2,        0,            verb_reboot_to_firmware  },
683
                {}
684
        };
685

686
        return dispatch_verb(argc, argv, verbs, NULL);
245✔
687
}
688

689
static int run(int argc, char *argv[]) {
276✔
690
        _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
276✔
691
        _cleanup_(umount_and_freep) char *mounted_dir = NULL;
276✔
692
        int r;
276✔
693

694
        log_setup();
276✔
695

696
        r = parse_argv(argc, argv);
276✔
697
        if (r <= 0)
276✔
698
                return r;
699

700
        if (arg_varlink) {
270✔
701
                _cleanup_(sd_varlink_server_unrefp) sd_varlink_server *varlink_server = NULL;
14✔
702

703
                /* Invocation as Varlink service */
704

705
                r = varlink_server_new(&varlink_server, SD_VARLINK_SERVER_ROOT_ONLY, NULL);
14✔
706
                if (r < 0)
14✔
707
                        return log_error_errno(r, "Failed to allocate Varlink server: %m");
×
708

709
                r = sd_varlink_server_add_interface(varlink_server, &vl_interface_io_systemd_BootControl);
14✔
710
                if (r < 0)
14✔
711
                        return log_error_errno(r, "Failed to add Varlink interface: %m");
×
712

713
                r = sd_varlink_server_bind_method_many(
14✔
714
                                varlink_server,
715
                                "io.systemd.BootControl.ListBootEntries",     vl_method_list_boot_entries,
716
                                "io.systemd.BootControl.SetRebootToFirmware", vl_method_set_reboot_to_firmware,
717
                                "io.systemd.BootControl.GetRebootToFirmware", vl_method_get_reboot_to_firmware);
718
                if (r < 0)
14✔
719
                        return log_error_errno(r, "Failed to bind Varlink methods: %m");
×
720

721
                r = sd_varlink_server_loop_auto(varlink_server);
14✔
722
                if (r < 0)
14✔
723
                        return log_error_errno(r, "Failed to run Varlink event loop: %m");
×
724

725
                return EXIT_SUCCESS;
726
        }
727

728
        if (arg_print_root_device > 0) {
256✔
729
                _cleanup_free_ char *path = NULL;
5✔
730
                dev_t devno;
5✔
731

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

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

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

752
                puts(path);
5✔
753
                return 0;
754
        }
755

756
        if (arg_print_loader_path || arg_print_stub_path)
251✔
757
                return print_loader_or_stub_path();
6✔
758

759
        /* Open up and mount the image */
760
        if (arg_image) {
245✔
761
                assert(!arg_root);
26✔
762

763
                r = mount_image_privately_interactively(
26✔
764
                                arg_image,
765
                                arg_image_policy,
766
                                DISSECT_IMAGE_GENERIC_ROOT |
767
                                DISSECT_IMAGE_USR_NO_ROOT |
768
                                DISSECT_IMAGE_RELAX_VAR_CHECK |
769
                                DISSECT_IMAGE_ALLOW_USERSPACE_VERITY,
770
                                &mounted_dir,
771
                                /* ret_dir_fd= */ NULL,
772
                                &loop_device);
773
                if (r < 0)
26✔
774
                        return r;
775

776
                arg_root = strdup(mounted_dir);
26✔
777
                if (!arg_root)
26✔
778
                        return log_oom();
×
779
        }
780

781
        return bootctl_main(argc, argv);
245✔
782
}
783

784
DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);
276✔
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