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

systemd / systemd / 16130628215

08 Jul 2025 12:05AM UTC coverage: 72.172% (+0.07%) from 72.1%
16130628215

push

github

yuwata
man: also use title case in systemd.service(5)

Follow-up for: 172dd81e9

301188 of 417317 relevant lines covered (72.17%)

716235.63 hits per line

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

68.18
/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

41
/* EFI_BOOT_OPTION_DESCRIPTION_MAX sets the maximum length for the boot option description
42
 * stored in NVRAM. The UEFI spec does not specify a minimum or maximum length for this
43
 * string, but we limit the length to something reasonable to prevent from the firmware
44
 * having to deal with a potentially too long string. */
45
#define EFI_BOOT_OPTION_DESCRIPTION_MAX ((size_t) 255)
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_graceful = false;
58
bool arg_quiet = false;
59
int arg_make_entry_directory = false; /* tri-state: < 0 for automatic logic */
60
sd_id128_t arg_machine_id = SD_ID128_NULL;
61
char *arg_install_layout = NULL;
62
BootEntryTokenType arg_entry_token_type = BOOT_ENTRY_TOKEN_AUTO;
63
char *arg_entry_token = NULL;
64
sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF;
65
bool arg_arch_all = false;
66
char *arg_root = NULL;
67
char *arg_image = NULL;
68
InstallSource arg_install_source = ARG_INSTALL_SOURCE_AUTO;
69
char *arg_efi_boot_option_description = NULL;
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);
255✔
82
STATIC_DESTRUCTOR_REGISTER(arg_xbootldr_path, freep);
255✔
83
STATIC_DESTRUCTOR_REGISTER(arg_install_layout, freep);
255✔
84
STATIC_DESTRUCTOR_REGISTER(arg_entry_token, freep);
255✔
85
STATIC_DESTRUCTOR_REGISTER(arg_root, freep);
255✔
86
STATIC_DESTRUCTOR_REGISTER(arg_image, freep);
255✔
87
STATIC_DESTRUCTOR_REGISTER(arg_efi_boot_option_description, freep);
255✔
88
STATIC_DESTRUCTOR_REGISTER(arg_image_policy, image_policy_freep);
255✔
89
STATIC_DESTRUCTOR_REGISTER(arg_certificate, freep);
255✔
90
STATIC_DESTRUCTOR_REGISTER(arg_certificate_source, freep);
255✔
91
STATIC_DESTRUCTOR_REGISTER(arg_private_key, freep);
255✔
92
STATIC_DESTRUCTOR_REGISTER(arg_private_key_source, freep);
255✔
93

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

103
        char *np;
214✔
104
        int r;
214✔
105

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

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

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

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

128
        return 0;
129
}
130

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

136
        char *np;
186✔
137
        int r;
186✔
138

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

144
                if (ret_uuid)
92✔
145
                        *ret_uuid = SD_ID128_NULL;
12✔
146
                if (ret_devid)
92✔
147
                        *ret_devid = 0;
17✔
148
                return 0;
92✔
149
        }
150
        if (r < 0)
94✔
151
                return r;
152

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

156
        return 1;
157
}
158

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

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

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

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

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

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

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

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

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

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

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

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

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

228
        if (arg_touch_variables >= 0)
170✔
229
                return arg_touch_variables;
118✔
230

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

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

244
        return true;
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      Identify kernel image type\n"
283
               "  kernel-inspect       Prints details about the kernel image\n"
284
               "\n%3$sBlock Device Discovery Commands:%4$s\n"
285
               "  -p --print-esp-path  Print path to the EFI System Partition mount point\n"
286
               "  -x --print-boot-path Print path to the $BOOT partition mount point\n"
287
               "     --print-loader-path\n"
288
               "                       Print path to currently booted boot loader binary\n"
289
               "     --print-stub-path Print path to currently booted unified kernel binary\n"
290
               "  -R --print-root-device\n"
291
               "                       Print path to the block device node backing the\n"
292
               "                       root file system (returns e.g. /dev/nvme0n1p5)\n"
293
               "  -RR                  Print path to the whole disk block device node\n"
294
               "                       backing the root FS (returns e.g. /dev/nvme0n1)\n"
295
               "\n%3$sOptions:%4$s\n"
296
               "  -h --help            Show this help\n"
297
               "     --version         Print version\n"
298
               "     --esp-path=PATH   Path to the EFI System Partition (ESP)\n"
299
               "     --boot-path=PATH  Path to the $BOOT partition\n"
300
               "     --root=PATH       Operate on an alternate filesystem root\n"
301
               "     --image=PATH      Operate on disk image as filesystem root\n"
302
               "     --image-policy=POLICY\n"
303
               "                       Specify disk image dissection policy\n"
304
               "     --install-source=auto|image|host\n"
305
               "                       Where to pick files when using --root=/--image=\n"
306
               "     --variables=yes|no\n"
307
               "                       Whether to modify EFI variables\n"
308
               "     --random-seed=yes|no\n"
309
               "                       Whether to create random-seed file during install\n"
310
               "     --no-pager        Do not pipe output into a pager\n"
311
               "     --graceful        Don't fail when the ESP cannot be found or EFI\n"
312
               "                       variables cannot be written\n"
313
               "  -q --quiet           Suppress output\n"
314
               "     --make-entry-directory=yes|no|auto\n"
315
               "                       Create $BOOT/ENTRY-TOKEN/ directory\n"
316
               "     --entry-token=machine-id|os-id|os-image-id|auto|literal:…\n"
317
               "                       Entry token to use for this installation\n"
318
               "     --json=pretty|short|off\n"
319
               "                       Generate JSON output\n"
320
               "     --all-architectures\n"
321
               "                       Install all supported EFI architectures\n"
322
               "     --efi-boot-option-description=DESCRIPTION\n"
323
               "                       Description of the entry in the boot option list\n"
324
               "     --dry-run         Dry run (unlink and cleanup)\n"
325
               "     --secure-boot-auto-enroll\n"
326
               "                       Set up secure boot auto-enrollment\n"
327
               "     --private-key=PATH|URI\n"
328
               "                       Private key to use when setting up secure boot\n"
329
               "                       auto-enrollment or an engine or provider specific\n"
330
               "                       designation if --private-key-source= is used\n"
331
               "     --private-key-source=file|provider:PROVIDER|engine:ENGINE\n"
332
               "                       Specify how to use KEY for --private-key=. Allows\n"
333
               "                       an OpenSSL engine/provider to be used when setting\n"
334
               "                       up secure boot auto-enrollment\n"
335
               "     --certificate=PATH|URI\n"
336
               "                       PEM certificate to use when setting up Secure Boot\n"
337
               "                       auto-enrollment, or a provider specific designation\n"
338
               "                       if --certificate-source= is used\n"
339
               "     --certificate-source=file|provider:PROVIDER\n"
340
               "                       Specify how to interpret the certificate from\n"
341
               "                       --certificate=. Allows the certificate to be loaded\n"
342
               "                       from an OpenSSL provider\n"
343
               "\nSee the %2$s for details.\n",
344
               program_invocation_short_name,
345
               link,
346
               ansi_underline(),
347
               ansi_normal(),
348
               ansi_highlight(),
349
               ansi_normal());
350

351
        return 0;
352
}
353

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

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

420
        int c, r;
255✔
421

422
        assert(argc >= 0);
255✔
423
        assert(argv);
255✔
424

425
        while ((c = getopt_long(argc, argv, "hpxRq", options, NULL)) >= 0)
647✔
426
                switch (c) {
398✔
427

428
                case 'h':
3✔
429
                        help(0, NULL, NULL);
3✔
430
                        return 0;
3✔
431

432
                case ARG_VERSION:
3✔
433
                        return version();
3✔
434

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

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

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

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

459
                case ARG_IMAGE_POLICY:
×
460
                        r = parse_image_policy_argument(optarg, &arg_image_policy);
×
461
                        if (r < 0)
×
462
                                return r;
463
                        break;
464

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

476
                        break;
477

478
                case 'p':
10✔
479
                        arg_print_esp_path = true;
10✔
480
                        break;
10✔
481

482
                case 'x':
8✔
483
                        arg_print_dollar_boot_path = true;
8✔
484
                        break;
8✔
485

486
                case ARG_PRINT_LOADER_PATH:
×
487
                        arg_print_loader_path = true;
×
488
                        break;
×
489

490
                case ARG_PRINT_STUB_PATH:
6✔
491
                        arg_print_stub_path = true;
6✔
492
                        break;
6✔
493

494
                case 'R':
7✔
495
                        arg_print_root_device++;
7✔
496
                        break;
7✔
497

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

504
                case ARG_NO_VARIABLES:
118✔
505
                        arg_touch_variables = false;
118✔
506
                        break;
118✔
507

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

514
                case ARG_NO_PAGER:
×
515
                        arg_pager_flags |= PAGER_DISABLE;
×
516
                        break;
×
517

518
                case ARG_GRACEFUL:
134✔
519
                        arg_graceful = true;
134✔
520
                        break;
134✔
521

522
                case 'q':
6✔
523
                        arg_quiet = true;
6✔
524
                        break;
6✔
525

526
                case ARG_ENTRY_TOKEN:
×
527
                        r = parse_boot_entry_token_type(optarg, &arg_entry_token_type, &arg_entry_token);
×
528
                        if (r < 0)
×
529
                                return r;
530
                        break;
531

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

540
                                arg_make_entry_directory = r;
13✔
541
                        }
542
                        break;
543

544
                case ARG_JSON:
6✔
545
                        r = parse_json_argument(optarg, &arg_json_format_flags);
6✔
546
                        if (r <= 0)
6✔
547
                                return r;
548
                        break;
549

550
                case ARG_ARCH_ALL:
12✔
551
                        arg_arch_all = true;
12✔
552
                        break;
12✔
553

554
                case ARG_EFI_BOOT_OPTION_DESCRIPTION:
×
555
                        if (isempty(optarg) || !(string_is_safe(optarg) && utf8_is_valid(optarg))) {
×
556
                                _cleanup_free_ char *escaped = NULL;
×
557

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

571
                case ARG_DRY_RUN:
×
572
                        arg_dry_run = true;
×
573
                        break;
×
574

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

581
                case ARG_CERTIFICATE:
1✔
582
                        r = free_and_strdup_warn(&arg_certificate, optarg);
1✔
583
                        if (r < 0)
1✔
584
                                return r;
585
                        break;
586

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

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

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

612
                case '?':
613
                        return -EINVAL;
614

615
                default:
×
616
                        assert_not_reached();
×
617
                }
618

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

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

629
        if (arg_root && arg_image)
249✔
630
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Please specify either --root= or --image=, the combination of both is not supported.");
×
631

632
        if (arg_install_source != ARG_INSTALL_SOURCE_AUTO && !arg_root && !arg_image)
249✔
633
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--install-from-host is only supported with --root= or --image=.");
×
634

635
        if (arg_dry_run && argv[optind] && !STR_IN_SET(argv[optind], "unlink", "cleanup"))
249✔
636
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--dry is only supported with --unlink or --cleanup");
×
637

638
        if (arg_secure_boot_auto_enroll && !arg_certificate)
249✔
639
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Secure boot auto-enrollment requested but no certificate provided");
×
640

641
        if (arg_secure_boot_auto_enroll && !arg_private_key)
249✔
642
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Secure boot auto-enrollment requested but no private key provided");
×
643

644
        r = sd_varlink_invocation(SD_VARLINK_ALLOW_ACCEPT);
249✔
645
        if (r < 0)
249✔
646
                return log_error_errno(r, "Failed to check if invoked in Varlink mode: %m");
×
647
        if (r > 0) {
249✔
648
                arg_varlink = true;
16✔
649
                arg_pager_flags |= PAGER_DISABLE;
16✔
650
        }
651

652
        return 1;
653
}
654

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

678
        return dispatch_verb(argc, argv, verbs, NULL);
222✔
679
}
680

681
static int run(int argc, char *argv[]) {
255✔
682
        _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
255✔
683
        _cleanup_(umount_and_freep) char *mounted_dir = NULL;
255✔
684
        int r;
255✔
685

686
        log_setup();
255✔
687

688
        r = parse_argv(argc, argv);
255✔
689
        if (r <= 0)
255✔
690
                return r;
691

692
        if (arg_varlink) {
249✔
693
                _cleanup_(sd_varlink_server_unrefp) sd_varlink_server *varlink_server = NULL;
16✔
694

695
                /* Invocation as Varlink service */
696

697
                r = varlink_server_new(&varlink_server, SD_VARLINK_SERVER_ROOT_ONLY, NULL);
16✔
698
                if (r < 0)
16✔
699
                        return log_error_errno(r, "Failed to allocate Varlink server: %m");
×
700

701
                r = sd_varlink_server_add_interface(varlink_server, &vl_interface_io_systemd_BootControl);
16✔
702
                if (r < 0)
16✔
703
                        return log_error_errno(r, "Failed to add Varlink interface: %m");
×
704

705
                r = sd_varlink_server_bind_method_many(
16✔
706
                                varlink_server,
707
                                "io.systemd.BootControl.ListBootEntries",     vl_method_list_boot_entries,
708
                                "io.systemd.BootControl.SetRebootToFirmware", vl_method_set_reboot_to_firmware,
709
                                "io.systemd.BootControl.GetRebootToFirmware", vl_method_get_reboot_to_firmware);
710
                if (r < 0)
16✔
711
                        return log_error_errno(r, "Failed to bind Varlink methods: %m");
×
712

713
                r = sd_varlink_server_loop_auto(varlink_server);
16✔
714
                if (r < 0)
16✔
715
                        return log_error_errno(r, "Failed to run Varlink event loop: %m");
×
716

717
                return EXIT_SUCCESS;
718
        }
719

720
        if (arg_print_root_device > 0) {
233✔
721
                _cleanup_free_ char *path = NULL;
5✔
722
                dev_t devno;
5✔
723

724
                r = blockdev_get_root(LOG_ERR, &devno);
5✔
725
                if (r < 0)
5✔
726
                        return r;
727
                if (r == 0) {
5✔
728
                        log_error("Root file system not backed by a (single) whole block device.");
×
729
                        return 80; /* some recognizable error code */
×
730
                }
731

732
                if (arg_print_root_device > 1) {
5✔
733
                        r = block_get_whole_disk(devno, &devno);
2✔
734
                        if (r < 0)
2✔
735
                                log_debug_errno(r, "Unable to find whole block device for root block device, ignoring: %m");
×
736
                }
737

738
                r = device_path_make_canonical(S_IFBLK, devno, &path);
5✔
739
                if (r < 0)
5✔
740
                        return log_error_errno(r,
×
741
                                               "Failed to format canonical device path for devno '" DEVNUM_FORMAT_STR "': %m",
742
                                               DEVNUM_FORMAT_VAL(devno));
743

744
                puts(path);
5✔
745
                return 0;
746
        }
747

748
        if (arg_print_loader_path || arg_print_stub_path)
228✔
749
                return print_loader_or_stub_path();
6✔
750

751
        /* Open up and mount the image */
752
        if (arg_image) {
222✔
753
                assert(!arg_root);
26✔
754

755
                r = mount_image_privately_interactively(
26✔
756
                                arg_image,
757
                                arg_image_policy,
758
                                DISSECT_IMAGE_GENERIC_ROOT |
759
                                DISSECT_IMAGE_USR_NO_ROOT |
760
                                DISSECT_IMAGE_RELAX_VAR_CHECK |
761
                                DISSECT_IMAGE_ALLOW_USERSPACE_VERITY,
762
                                &mounted_dir,
763
                                /* ret_dir_fd= */ NULL,
764
                                &loop_device);
765
                if (r < 0)
26✔
766
                        return r;
767

768
                arg_root = strdup(mounted_dir);
26✔
769
                if (!arg_root)
26✔
770
                        return log_oom();
×
771
        }
772

773
        return bootctl_main(argc, argv);
222✔
774
}
775

776
DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);
255✔
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