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

systemd / systemd / 15057632786

15 May 2025 09:01PM UTC coverage: 72.267% (+0.02%) from 72.244%
15057632786

push

github

bluca
man: document how to hook stuff into system wakeup

Fixes: #6364

298523 of 413084 relevant lines covered (72.27%)

738132.88 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

5
#include "sd-varlink.h"
6

7
#include "blockdev-util.h"
8
#include "bootctl.h"
9
#include "bootctl-install.h"
10
#include "bootctl-random-seed.h"
11
#include "bootctl-reboot-to-firmware.h"
12
#include "bootctl-set-efivar.h"
13
#include "bootctl-status.h"
14
#include "bootctl-uki.h"
15
#include "build.h"
16
#include "devnum-util.h"
17
#include "dissect-image.h"
18
#include "efi-loader.h"
19
#include "escape.h"
20
#include "find-esp.h"
21
#include "main-func.h"
22
#include "mount-util.h"
23
#include "pager.h"
24
#include "parse-argument.h"
25
#include "path-util.h"
26
#include "pretty-print.h"
27
#include "utf8.h"
28
#include "varlink-io.systemd.BootControl.h"
29
#include "varlink-util.h"
30
#include "verbs.h"
31
#include "virt.h"
32

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

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

73
STATIC_DESTRUCTOR_REGISTER(arg_esp_path, freep);
251✔
74
STATIC_DESTRUCTOR_REGISTER(arg_xbootldr_path, freep);
251✔
75
STATIC_DESTRUCTOR_REGISTER(arg_install_layout, freep);
251✔
76
STATIC_DESTRUCTOR_REGISTER(arg_entry_token, freep);
251✔
77
STATIC_DESTRUCTOR_REGISTER(arg_root, freep);
251✔
78
STATIC_DESTRUCTOR_REGISTER(arg_image, freep);
251✔
79
STATIC_DESTRUCTOR_REGISTER(arg_efi_boot_option_description, freep);
251✔
80
STATIC_DESTRUCTOR_REGISTER(arg_image_policy, image_policy_freep);
251✔
81
STATIC_DESTRUCTOR_REGISTER(arg_certificate, freep);
251✔
82
STATIC_DESTRUCTOR_REGISTER(arg_certificate_source, freep);
251✔
83
STATIC_DESTRUCTOR_REGISTER(arg_private_key, freep);
251✔
84
STATIC_DESTRUCTOR_REGISTER(arg_private_key_source, freep);
251✔
85

86
int acquire_esp(
210✔
87
                int unprivileged_mode,
88
                bool graceful,
89
                uint32_t *ret_part,
90
                uint64_t *ret_pstart,
91
                uint64_t *ret_psize,
92
                sd_id128_t *ret_uuid,
93
                dev_t *ret_devid) {
94

95
        char *np;
210✔
96
        int r;
210✔
97

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

104
        r = find_esp_and_warn(arg_root, arg_esp_path, unprivileged_mode, &np, ret_part, ret_pstart, ret_psize, ret_uuid, ret_devid);
210✔
105
        if (r == -ENOKEY) {
210✔
106
                if (graceful)
5✔
107
                        return log_full_errno(arg_quiet ? LOG_DEBUG : LOG_INFO, r,
×
108
                                              "Couldn't find EFI system partition, skipping.");
109

110
                return log_error_errno(r,
5✔
111
                                       "Couldn't find EFI system partition. It is recommended to mount it to /boot/ or /efi/.\n"
112
                                       "Alternatively, use --esp-path= to specify path to mount point.");
113
        }
114
        if (r < 0)
205✔
115
                return r;
116

117
        free_and_replace(arg_esp_path, np);
204✔
118
        log_debug("Using EFI System Partition at %s.", arg_esp_path);
204✔
119

120
        return 0;
121
}
122

123
int acquire_xbootldr(
182✔
124
                int unprivileged_mode,
125
                sd_id128_t *ret_uuid,
126
                dev_t *ret_devid) {
127

128
        char *np;
182✔
129
        int r;
182✔
130

131
        r = find_xbootldr_and_warn(arg_root, arg_xbootldr_path, unprivileged_mode, &np, ret_uuid, ret_devid);
182✔
132
        if (r == -ENOKEY) {
182✔
133
                log_debug_errno(r, "Didn't find an XBOOTLDR partition, using the ESP as $BOOT.");
89✔
134
                arg_xbootldr_path = mfree(arg_xbootldr_path);
89✔
135

136
                if (ret_uuid)
89✔
137
                        *ret_uuid = SD_ID128_NULL;
12✔
138
                if (ret_devid)
89✔
139
                        *ret_devid = 0;
17✔
140
                return 0;
89✔
141
        }
142
        if (r < 0)
93✔
143
                return r;
144

145
        free_and_replace(arg_xbootldr_path, np);
92✔
146
        log_debug("Using XBOOTLDR partition at %s as $BOOT.", arg_xbootldr_path);
92✔
147

148
        return 1;
149
}
150

151
static int print_loader_or_stub_path(void) {
6✔
152
        _cleanup_free_ char *p = NULL;
6✔
153
        sd_id128_t uuid;
6✔
154
        int r;
6✔
155

156
        if (arg_print_loader_path) {
6✔
157
                r = efi_loader_get_device_part_uuid(&uuid);
×
158
                if (r == -ENOENT)
×
159
                        return log_error_errno(r, "No loader partition UUID passed.");
×
160
                if (r < 0)
×
161
                        return log_error_errno(r, "Unable to determine loader partition UUID: %m");
×
162

163
                r = efi_get_variable_path(EFI_LOADER_VARIABLE_STR("LoaderImageIdentifier"), &p);
×
164
                if (r == -ENOENT)
×
165
                        return log_error_errno(r, "No loader EFI binary path passed.");
×
166
                if (r < 0)
×
167
                        return log_error_errno(r, "Unable to determine loader EFI binary path: %m");
×
168
        } else {
169
                assert(arg_print_stub_path);
6✔
170

171
                r = efi_stub_get_device_part_uuid(&uuid);
6✔
172
                if (r == -ENOENT)
6✔
173
                        return log_error_errno(r, "No stub partition UUID passed.");
×
174
                if (r < 0)
6✔
175
                        return log_error_errno(r, "Unable to determine stub partition UUID: %m");
×
176

177
                r = efi_get_variable_path(EFI_LOADER_VARIABLE_STR("StubImageIdentifier"), &p);
6✔
178
                if (r == -ENOENT)
6✔
179
                        return log_error_errno(r, "No stub EFI binary path passed.");
×
180
                if (r < 0)
6✔
181
                        return log_error_errno(r, "Unable to determine stub EFI binary path: %m");
×
182
        }
183

184
        sd_id128_t esp_uuid;
6✔
185
        r = acquire_esp(/* unprivileged_mode= */ false, /* graceful= */ false,
6✔
186
                        /* ret_part= */ NULL, /* ret_pstart= */ NULL, /* ret_psize= */ NULL,
187
                        &esp_uuid, /* ret_devid= */ NULL);
188
        if (r < 0)
6✔
189
                return r;
190

191
        const char *found_path = NULL;
6✔
192
        if (sd_id128_equal(esp_uuid, uuid))
6✔
193
                found_path = arg_esp_path;
6✔
194
        else if (arg_print_stub_path) { /* In case of the stub, also look for things in the xbootldr partition */
×
195
                sd_id128_t xbootldr_uuid;
×
196

197
                r = acquire_xbootldr(/* unprivileged_mode= */ false, &xbootldr_uuid, /* ret_devid= */ NULL);
×
198
                if (r < 0)
×
199
                        return r;
×
200

201
                if (sd_id128_equal(xbootldr_uuid, uuid))
×
202
                        found_path = arg_xbootldr_path;
×
203
        }
204

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

208
        _cleanup_free_ char *j = path_join(found_path, p);
12✔
209
        if (!j)
6✔
210
                return log_oom();
×
211

212
        puts(j);
6✔
213
        return 0;
214
}
215

216
bool touch_variables(void) {
166✔
217
        /* If we run in a container or on a non-EFI system, automatically turn off EFI file system access,
218
         * unless explicitly overridden. */
219

220
        if (arg_touch_variables >= 0)
166✔
221
                return arg_touch_variables;
114✔
222

223
        if (arg_root) {
52✔
224
                log_once(LOG_NOTICE,
32✔
225
                         "Operating on %s, skipping EFI variable modifications.",
226
                         arg_image ? "image" : "root directory");
227
                return false;
28✔
228
        }
229

230
        if (!is_efi_boot()) { /* NB: this internally checks if we run in a container */
24✔
231
                log_once(LOG_NOTICE,
17✔
232
                         "Not booted with EFI or running in a container, skipping EFI variable modifications.");
233
                return false;
17✔
234
        }
235

236
        return true;
237
}
238

239
static int help(int argc, char *argv[], void *userdata) {
3✔
240
        _cleanup_free_ char *link = NULL;
3✔
241
        int r;
3✔
242

243
        pager_open(arg_pager_flags);
3✔
244

245
        r = terminal_urlify_man("bootctl", "1", &link);
3✔
246
        if (r < 0)
3✔
247
                return log_oom();
×
248

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

343
        return 0;
344
}
345

346
static int parse_argv(int argc, char *argv[]) {
251✔
347
        enum {
251✔
348
                ARG_ESP_PATH = 0x100,
349
                ARG_BOOT_PATH,
350
                ARG_ROOT,
351
                ARG_IMAGE,
352
                ARG_IMAGE_POLICY,
353
                ARG_INSTALL_SOURCE,
354
                ARG_VERSION,
355
                ARG_VARIABLES,
356
                ARG_NO_VARIABLES,
357
                ARG_RANDOM_SEED,
358
                ARG_NO_PAGER,
359
                ARG_GRACEFUL,
360
                ARG_MAKE_ENTRY_DIRECTORY,
361
                ARG_ENTRY_TOKEN,
362
                ARG_JSON,
363
                ARG_ARCH_ALL,
364
                ARG_EFI_BOOT_OPTION_DESCRIPTION,
365
                ARG_DRY_RUN,
366
                ARG_PRINT_LOADER_PATH,
367
                ARG_PRINT_STUB_PATH,
368
                ARG_SECURE_BOOT_AUTO_ENROLL,
369
                ARG_CERTIFICATE,
370
                ARG_CERTIFICATE_SOURCE,
371
                ARG_PRIVATE_KEY,
372
                ARG_PRIVATE_KEY_SOURCE,
373
        };
374

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

412
        int c, r;
251✔
413

414
        assert(argc >= 0);
251✔
415
        assert(argv);
251✔
416

417
        while ((c = getopt_long(argc, argv, "hpxRq", options, NULL)) >= 0)
635✔
418
                switch (c) {
390✔
419

420
                case 'h':
3✔
421
                        help(0, NULL, NULL);
3✔
422
                        return 0;
3✔
423

424
                case ARG_VERSION:
3✔
425
                        return version();
3✔
426

427
                case ARG_ESP_PATH:
3✔
428
                        r = free_and_strdup(&arg_esp_path, optarg);
3✔
429
                        if (r < 0)
3✔
430
                                return log_oom();
×
431
                        break;
432

433
                case ARG_BOOT_PATH:
3✔
434
                        r = free_and_strdup(&arg_xbootldr_path, optarg);
3✔
435
                        if (r < 0)
3✔
436
                                return log_oom();
×
437
                        break;
438

439
                case ARG_ROOT:
34✔
440
                        r = parse_path_argument(optarg, /* suppress_root= */ true, &arg_root);
34✔
441
                        if (r < 0)
34✔
442
                                return r;
443
                        break;
444

445
                case ARG_IMAGE:
29✔
446
                        r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_image);
29✔
447
                        if (r < 0)
29✔
448
                                return r;
449
                        break;
450

451
                case ARG_IMAGE_POLICY:
×
452
                        r = parse_image_policy_argument(optarg, &arg_image_policy);
×
453
                        if (r < 0)
×
454
                                return r;
455
                        break;
456

457
                case ARG_INSTALL_SOURCE:
×
458
                        if (streq(optarg, "auto"))
×
459
                                arg_install_source = ARG_INSTALL_SOURCE_AUTO;
×
460
                        else if (streq(optarg, "image"))
×
461
                                arg_install_source = ARG_INSTALL_SOURCE_IMAGE;
×
462
                        else if (streq(optarg, "host"))
×
463
                                arg_install_source = ARG_INSTALL_SOURCE_HOST;
×
464
                        else
465
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
466
                                                       "Unexpected parameter for --install-source=: %s", optarg);
467

468
                        break;
469

470
                case 'p':
10✔
471
                        arg_print_esp_path = true;
10✔
472
                        break;
10✔
473

474
                case 'x':
8✔
475
                        arg_print_dollar_boot_path = true;
8✔
476
                        break;
8✔
477

478
                case ARG_PRINT_LOADER_PATH:
×
479
                        arg_print_loader_path = true;
×
480
                        break;
×
481

482
                case ARG_PRINT_STUB_PATH:
6✔
483
                        arg_print_stub_path = true;
6✔
484
                        break;
6✔
485

486
                case 'R':
7✔
487
                        arg_print_root_device++;
7✔
488
                        break;
7✔
489

490
                case ARG_VARIABLES:
×
491
                        r = parse_tristate_argument("--variables=", optarg, &arg_touch_variables);
×
492
                        if (r < 0)
×
493
                                return r;
494
                        break;
495

496
                case ARG_NO_VARIABLES:
114✔
497
                        arg_touch_variables = false;
114✔
498
                        break;
114✔
499

500
                case ARG_RANDOM_SEED:
×
501
                        r = parse_boolean_argument("--random-seed=", optarg, &arg_install_random_seed);
×
502
                        if (r < 0)
×
503
                                return r;
504
                        break;
505

506
                case ARG_NO_PAGER:
×
507
                        arg_pager_flags |= PAGER_DISABLE;
×
508
                        break;
×
509

510
                case ARG_GRACEFUL:
130✔
511
                        arg_graceful = true;
130✔
512
                        break;
130✔
513

514
                case 'q':
6✔
515
                        arg_quiet = true;
6✔
516
                        break;
6✔
517

518
                case ARG_ENTRY_TOKEN:
×
519
                        r = parse_boot_entry_token_type(optarg, &arg_entry_token_type, &arg_entry_token);
×
520
                        if (r < 0)
×
521
                                return r;
522
                        break;
523

524
                case ARG_MAKE_ENTRY_DIRECTORY:
13✔
525
                        if (streq(optarg, "auto"))  /* retained for backwards compatibility */
13✔
526
                                arg_make_entry_directory = -1; /* yes if machine-id is permanent */
×
527
                        else {
528
                                r = parse_boolean_argument("--make-entry-directory=", optarg, NULL);
13✔
529
                                if (r < 0)
13✔
530
                                        return r;
531

532
                                arg_make_entry_directory = r;
13✔
533
                        }
534
                        break;
535

536
                case ARG_JSON:
6✔
537
                        r = parse_json_argument(optarg, &arg_json_format_flags);
6✔
538
                        if (r <= 0)
6✔
539
                                return r;
540
                        break;
541

542
                case ARG_ARCH_ALL:
12✔
543
                        arg_arch_all = true;
12✔
544
                        break;
12✔
545

546
                case ARG_EFI_BOOT_OPTION_DESCRIPTION:
×
547
                        if (isempty(optarg) || !(string_is_safe(optarg) && utf8_is_valid(optarg))) {
×
548
                                _cleanup_free_ char *escaped = NULL;
×
549

550
                                escaped = cescape(optarg);
×
551
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
552
                                                       "Invalid --efi-boot-option-description=: %s", strna(escaped));
553
                        }
554
                        if (strlen(optarg) > EFI_BOOT_OPTION_DESCRIPTION_MAX)
×
555
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
556
                                                       "--efi-boot-option-description= too long: %zu > %zu",
557
                                                       strlen(optarg), EFI_BOOT_OPTION_DESCRIPTION_MAX);
558
                        r = free_and_strdup_warn(&arg_efi_boot_option_description, optarg);
×
559
                        if (r < 0)
×
560
                                return r;
561
                        break;
562

563
                case ARG_DRY_RUN:
×
564
                        arg_dry_run = true;
×
565
                        break;
×
566

567
                case ARG_SECURE_BOOT_AUTO_ENROLL:
1✔
568
                        r = parse_boolean_argument("--secure-boot-auto-enroll=", optarg, &arg_secure_boot_auto_enroll);
1✔
569
                        if (r < 0)
1✔
570
                                return r;
571
                        break;
572

573
                case ARG_CERTIFICATE:
1✔
574
                        r = free_and_strdup_warn(&arg_certificate, optarg);
1✔
575
                        if (r < 0)
1✔
576
                                return r;
577
                        break;
578

579
                case ARG_CERTIFICATE_SOURCE:
×
580
                        r = parse_openssl_certificate_source_argument(
×
581
                                        optarg,
582
                                        &arg_certificate_source,
583
                                        &arg_certificate_source_type);
584
                        if (r < 0)
×
585
                                return r;
586
                        break;
587

588
                case ARG_PRIVATE_KEY: {
1✔
589
                        r = free_and_strdup_warn(&arg_private_key, optarg);
1✔
590
                        if (r < 0)
1✔
591
                                return r;
592
                        break;
593
                }
594

595
                case ARG_PRIVATE_KEY_SOURCE:
×
596
                        r = parse_openssl_key_source_argument(
×
597
                                        optarg,
598
                                        &arg_private_key_source,
599
                                        &arg_private_key_source_type);
600
                        if (r < 0)
×
601
                                return r;
602
                        break;
603

604
                case '?':
605
                        return -EINVAL;
606

607
                default:
×
608
                        assert_not_reached();
×
609
                }
610

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

615
        if ((arg_root || arg_image) && argv[optind] && !STR_IN_SET(argv[optind], "status", "list",
245✔
616
                        "install", "update", "remove", "is-installed", "random-seed", "unlink", "cleanup"))
617
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
618
                                       "Options --root= and --image= are not supported with verb %s.",
619
                                       argv[optind]);
620

621
        if (arg_root && arg_image)
245✔
622
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Please specify either --root= or --image=, the combination of both is not supported.");
×
623

624
        if (arg_install_source != ARG_INSTALL_SOURCE_AUTO && !arg_root && !arg_image)
245✔
625
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--install-from-host is only supported with --root= or --image=.");
×
626

627
        if (arg_dry_run && argv[optind] && !STR_IN_SET(argv[optind], "unlink", "cleanup"))
245✔
628
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--dry is only supported with --unlink or --cleanup");
×
629

630
        if (arg_secure_boot_auto_enroll && !arg_certificate)
245✔
631
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Secure boot auto-enrollment requested but no certificate provided");
×
632

633
        if (arg_secure_boot_auto_enroll && !arg_private_key)
245✔
634
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Secure boot auto-enrollment requested but no private key provided");
×
635

636
        r = sd_varlink_invocation(SD_VARLINK_ALLOW_ACCEPT);
245✔
637
        if (r < 0)
245✔
638
                return log_error_errno(r, "Failed to check if invoked in Varlink mode: %m");
×
639
        if (r > 0) {
245✔
640
                arg_varlink = true;
16✔
641
                arg_pager_flags |= PAGER_DISABLE;
16✔
642
        }
643

644
        return 1;
645
}
646

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

670
        return dispatch_verb(argc, argv, verbs, NULL);
218✔
671
}
672

673
static int run(int argc, char *argv[]) {
251✔
674
        _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
251✔
675
        _cleanup_(umount_and_freep) char *mounted_dir = NULL;
251✔
676
        int r;
251✔
677

678
        log_setup();
251✔
679

680
        r = parse_argv(argc, argv);
251✔
681
        if (r <= 0)
251✔
682
                return r;
683

684
        if (arg_varlink) {
245✔
685
                _cleanup_(sd_varlink_server_unrefp) sd_varlink_server *varlink_server = NULL;
16✔
686

687
                /* Invocation as Varlink service */
688

689
                r = varlink_server_new(&varlink_server, SD_VARLINK_SERVER_ROOT_ONLY, NULL);
16✔
690
                if (r < 0)
16✔
691
                        return log_error_errno(r, "Failed to allocate Varlink server: %m");
×
692

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

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

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

709
                return EXIT_SUCCESS;
710
        }
711

712
        if (arg_print_root_device > 0) {
229✔
713
                _cleanup_free_ char *path = NULL;
5✔
714
                dev_t devno;
5✔
715

716
                r = blockdev_get_root(LOG_ERR, &devno);
5✔
717
                if (r < 0)
5✔
718
                        return r;
719
                if (r == 0) {
5✔
720
                        log_error("Root file system not backed by a (single) whole block device.");
×
721
                        return 80; /* some recognizable error code */
×
722
                }
723

724
                if (arg_print_root_device > 1) {
5✔
725
                        r = block_get_whole_disk(devno, &devno);
2✔
726
                        if (r < 0)
2✔
727
                                log_debug_errno(r, "Unable to find whole block device for root block device, ignoring: %m");
×
728
                }
729

730
                r = device_path_make_canonical(S_IFBLK, devno, &path);
5✔
731
                if (r < 0)
5✔
732
                        return log_error_errno(r,
×
733
                                               "Failed to format canonical device path for devno '" DEVNUM_FORMAT_STR "': %m",
734
                                               DEVNUM_FORMAT_VAL(devno));
735

736
                puts(path);
5✔
737
                return 0;
738
        }
739

740
        if (arg_print_loader_path || arg_print_stub_path)
224✔
741
                return print_loader_or_stub_path();
6✔
742

743
        /* Open up and mount the image */
744
        if (arg_image) {
218✔
745
                assert(!arg_root);
26✔
746

747
                r = mount_image_privately_interactively(
26✔
748
                                arg_image,
749
                                arg_image_policy,
750
                                DISSECT_IMAGE_GENERIC_ROOT |
751
                                DISSECT_IMAGE_USR_NO_ROOT |
752
                                DISSECT_IMAGE_RELAX_VAR_CHECK |
753
                                DISSECT_IMAGE_ALLOW_USERSPACE_VERITY,
754
                                &mounted_dir,
755
                                /* ret_dir_fd= */ NULL,
756
                                &loop_device);
757
                if (r < 0)
26✔
758
                        return r;
759

760
                arg_root = strdup(mounted_dir);
26✔
761
                if (!arg_root)
26✔
762
                        return log_oom();
×
763
        }
764

765
        return bootctl_main(argc, argv);
218✔
766
}
767

768
DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);
258✔
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