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

systemd / systemd / 16610200353

29 Jul 2025 11:26PM UTC coverage: 72.278% (+0.06%) from 72.215%
16610200353

push

github

yuwata
po: Translated using Weblate (Chinese (Simplified) (zh_CN))

Currently translated at 100.0% (264 of 264 strings)

Co-authored-by: Jesse Guo <jesseguotech@outlook.com>
Translate-URL: https://translate.fedoraproject.org/projects/systemd/main/zh_CN/
Translation: systemd/main

303003 of 419221 relevant lines covered (72.28%)

740914.66 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);
270✔
83
STATIC_DESTRUCTOR_REGISTER(arg_xbootldr_path, freep);
270✔
84
STATIC_DESTRUCTOR_REGISTER(arg_install_layout, freep);
270✔
85
STATIC_DESTRUCTOR_REGISTER(arg_entry_token, freep);
270✔
86
STATIC_DESTRUCTOR_REGISTER(arg_root, freep);
270✔
87
STATIC_DESTRUCTOR_REGISTER(arg_image, freep);
270✔
88
STATIC_DESTRUCTOR_REGISTER(arg_efi_boot_option_description, freep);
270✔
89
STATIC_DESTRUCTOR_REGISTER(arg_image_policy, image_policy_freep);
270✔
90
STATIC_DESTRUCTOR_REGISTER(arg_certificate, freep);
270✔
91
STATIC_DESTRUCTOR_REGISTER(arg_certificate_source, freep);
270✔
92
STATIC_DESTRUCTOR_REGISTER(arg_private_key, freep);
270✔
93
STATIC_DESTRUCTOR_REGISTER(arg_private_key_source, freep);
270✔
94

95
int acquire_esp(
230✔
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;
230✔
105
        int r;
230✔
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);
230✔
114
        if (r == -ENOKEY) {
230✔
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)
225✔
124
                return r;
125

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

129
        return 0;
130
}
131

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

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

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

145
                if (ret_uuid)
94✔
146
                        *ret_uuid = SD_ID128_NULL;
15✔
147
                if (ret_devid)
94✔
148
                        *ret_devid = 0;
20✔
149
                return 0;
94✔
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) {
171✔
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)
171✔
230
                return arg_touch_variables;
118✔
231

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

352
        return 0;
353
}
354

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

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

421
        int c, r;
270✔
422

423
        assert(argc >= 0);
270✔
424
        assert(argv);
270✔
425

426
        while ((c = getopt_long(argc, argv, "hpxRq", options, NULL)) >= 0)
676✔
427
                switch (c) {
412✔
428

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

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

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

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

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

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

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

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

477
                        break;
478

479
                case 'p':
23✔
480
                        arg_print_esp_path = true;
23✔
481
                        break;
23✔
482

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

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

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

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

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

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

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

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

519
                case ARG_GRACEFUL:
135✔
520
                        arg_graceful = true;
135✔
521
                        break;
135✔
522

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

658
        return 1;
659
}
660

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

684
        return dispatch_verb(argc, argv, verbs, NULL);
239✔
685
}
686

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

692
        log_setup();
270✔
693

694
        r = parse_argv(argc, argv);
270✔
695
        if (r <= 0)
270✔
696
                return r;
697

698
        if (arg_varlink) {
264✔
699
                _cleanup_(sd_varlink_server_unrefp) sd_varlink_server *varlink_server = NULL;
14✔
700

701
                /* Invocation as Varlink service */
702

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

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

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

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

723
                return EXIT_SUCCESS;
724
        }
725

726
        if (arg_print_root_device > 0) {
250✔
727
                _cleanup_free_ char *path = NULL;
5✔
728
                dev_t devno;
5✔
729

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

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

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

750
                puts(path);
5✔
751
                return 0;
752
        }
753

754
        if (arg_print_loader_path || arg_print_stub_path)
245✔
755
                return print_loader_or_stub_path();
6✔
756

757
        /* Open up and mount the image */
758
        if (arg_image) {
239✔
759
                assert(!arg_root);
26✔
760

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

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

779
        return bootctl_main(argc, argv);
239✔
780
}
781

782
DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);
270✔
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