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

systemd / systemd / 21846209963

09 Feb 2026 03:52PM UTC coverage: 72.697% (-0.02%) from 72.716%
21846209963

push

github

daandemeyer
meson: guard symlinks in sysconfdir behind install_sysconfidr

Symlinks to files inside sysconfdir are now only installed if
ìnstall_sysconfdir=true (which is the default).

If sshconfdir,sshdconfdir,shellprofiledir are not inside sysconfdir and
install_sysconfidr=false, these symlinks are still installed to the
configured directory.

311951 of 429113 relevant lines covered (72.7%)

1156102.48 hits per line

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

67.22
/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-table.h"
35
#include "string-util.h"
36
#include "strv.h"
37
#include "utf8.h"
38
#include "varlink-io.systemd.BootControl.h"
39
#include "varlink-util.h"
40
#include "verbs.h"
41
#include "virt.h"
42

43
static GracefulMode _arg_graceful = ARG_GRACEFUL_NO;
44

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

79
STATIC_DESTRUCTOR_REGISTER(arg_esp_path, freep);
297✔
80
STATIC_DESTRUCTOR_REGISTER(arg_xbootldr_path, freep);
297✔
81
STATIC_DESTRUCTOR_REGISTER(arg_install_layout, freep);
297✔
82
STATIC_DESTRUCTOR_REGISTER(arg_entry_token, freep);
297✔
83
STATIC_DESTRUCTOR_REGISTER(arg_root, freep);
297✔
84
STATIC_DESTRUCTOR_REGISTER(arg_image, freep);
297✔
85
STATIC_DESTRUCTOR_REGISTER(arg_efi_boot_option_description, freep);
297✔
86
STATIC_DESTRUCTOR_REGISTER(arg_image_policy, image_policy_freep);
297✔
87
STATIC_DESTRUCTOR_REGISTER(arg_certificate, freep);
297✔
88
STATIC_DESTRUCTOR_REGISTER(arg_certificate_source, freep);
297✔
89
STATIC_DESTRUCTOR_REGISTER(arg_private_key, freep);
297✔
90
STATIC_DESTRUCTOR_REGISTER(arg_private_key_source, freep);
297✔
91

92
static const char* const install_source_table[_INSTALL_SOURCE_MAX] = {
93
        [INSTALL_SOURCE_IMAGE] = "image",
94
        [INSTALL_SOURCE_HOST]  = "host",
95
        [INSTALL_SOURCE_AUTO]  = "auto",
96
};
97

98
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(install_source, InstallSource);
×
99

100
int acquire_esp(
256✔
101
                int unprivileged_mode,
102
                bool graceful,
103
                uint32_t *ret_part,
104
                uint64_t *ret_pstart,
105
                uint64_t *ret_psize,
106
                sd_id128_t *ret_uuid,
107
                dev_t *ret_devid) {
108

109
        char *np;
256✔
110
        int r;
256✔
111

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

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

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

131
        free_and_replace(arg_esp_path, np);
250✔
132
        log_debug("Using EFI System Partition at %s.", arg_esp_path);
250✔
133

134
        return 0;
135
}
136

137
int acquire_xbootldr(
203✔
138
                int unprivileged_mode,
139
                sd_id128_t *ret_uuid,
140
                dev_t *ret_devid) {
141

142
        char *np;
203✔
143
        int r;
203✔
144

145
        r = find_xbootldr_and_warn(arg_root, arg_xbootldr_path, unprivileged_mode, &np, ret_uuid, ret_devid);
203✔
146
        if (r == -ENOKEY) {
203✔
147
                log_debug_errno(r, "Didn't find an XBOOTLDR partition, using the ESP as $BOOT.");
101✔
148
                arg_xbootldr_path = mfree(arg_xbootldr_path);
101✔
149

150
                if (ret_uuid)
101✔
151
                        *ret_uuid = SD_ID128_NULL;
15✔
152
                if (ret_devid)
101✔
153
                        *ret_devid = 0;
20✔
154
                return 0;
101✔
155
        }
156
        if (r < 0)
102✔
157
                return r;
158

159
        free_and_replace(arg_xbootldr_path, np);
101✔
160
        log_debug("Using XBOOTLDR partition at %s as $BOOT.", arg_xbootldr_path);
101✔
161

162
        return 1;
163
}
164

165
static int print_loader_or_stub_path(void) {
6✔
166
        _cleanup_free_ char *p = NULL;
6✔
167
        sd_id128_t uuid;
6✔
168
        int r;
6✔
169

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

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

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

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

198
        sd_id128_t esp_uuid;
6✔
199
        r = acquire_esp(/* unprivileged_mode= */ false, /* graceful= */ false,
6✔
200
                        /* ret_part= */ NULL, /* ret_pstart= */ NULL, /* ret_psize= */ NULL,
201
                        &esp_uuid, /* ret_devid= */ NULL);
202
        if (r < 0)
6✔
203
                return r;
204

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

211
                r = acquire_xbootldr(/* unprivileged_mode= */ false, &xbootldr_uuid, /* ret_devid= */ NULL);
×
212
                if (r < 0)
×
213
                        return r;
×
214

215
                if (sd_id128_equal(xbootldr_uuid, uuid))
×
216
                        found_path = arg_xbootldr_path;
×
217
        }
218

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

222
        _cleanup_free_ char *j = path_join(found_path, p);
12✔
223
        if (!j)
6✔
224
                return log_oom();
×
225

226
        puts(j);
6✔
227
        return 0;
228
}
229

230
bool touch_variables(void) {
17✔
231
        /* If we run in a container or on a non-EFI system, automatically turn off EFI file system access,
232
         * unless explicitly overridden. */
233

234
        if (arg_touch_variables >= 0)
17✔
235
                return arg_touch_variables;
×
236

237
        if (arg_root) {
17✔
238
                log_once(LOG_NOTICE,
3✔
239
                         "Operating on %s, skipping EFI variable modifications.",
240
                         arg_image ? "image" : "root directory");
241
                return false;
2✔
242
        }
243

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

250
        return true;
251
}
252

253
GracefulMode arg_graceful(void) {
288✔
254
        static bool chroot_checked = false;
288✔
255

256
        if (!chroot_checked && running_in_chroot() > 0) {
288✔
257
                if (_arg_graceful == ARG_GRACEFUL_NO)
×
258
                        log_full(arg_quiet ? LOG_DEBUG : LOG_INFO, "Running in a chroot, enabling --graceful.");
×
259

260
                _arg_graceful = ARG_GRACEFUL_FORCE;
×
261
        }
262

263
        chroot_checked = true;
288✔
264

265
        return _arg_graceful;
288✔
266
}
267

268
static int help(int argc, char *argv[], void *userdata) {
3✔
269
        _cleanup_free_ char *link = NULL;
3✔
270
        int r;
3✔
271

272
        pager_open(arg_pager_flags);
3✔
273

274
        r = terminal_urlify_man("bootctl", "1", &link);
3✔
275
        if (r < 0)
3✔
276
                return log_oom();
×
277

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

376
        return 0;
377
}
378

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

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

447
        int c, r;
297✔
448

449
        assert(argc >= 0);
297✔
450
        assert(argv);
297✔
451

452
        while ((c = getopt_long(argc, argv, "hpxRq", options, NULL)) >= 0)
728✔
453
                switch (c) {
437✔
454

455
                case 'h':
3✔
456
                        help(0, NULL, NULL);
3✔
457
                        return 0;
3✔
458

459
                case ARG_VERSION:
3✔
460
                        return version();
3✔
461

462
                case ARG_ESP_PATH:
3✔
463
                        r = free_and_strdup(&arg_esp_path, optarg);
3✔
464
                        if (r < 0)
3✔
465
                                return log_oom();
×
466
                        break;
467

468
                case ARG_BOOT_PATH:
3✔
469
                        r = free_and_strdup(&arg_xbootldr_path, optarg);
3✔
470
                        if (r < 0)
3✔
471
                                return log_oom();
×
472
                        break;
473

474
                case ARG_ROOT:
34✔
475
                        r = parse_path_argument(optarg, /* suppress_root= */ true, &arg_root);
34✔
476
                        if (r < 0)
34✔
477
                                return r;
478
                        break;
479

480
                case ARG_IMAGE:
29✔
481
                        r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_image);
29✔
482
                        if (r < 0)
29✔
483
                                return r;
484
                        break;
485

486
                case ARG_IMAGE_POLICY:
×
487
                        r = parse_image_policy_argument(optarg, &arg_image_policy);
×
488
                        if (r < 0)
×
489
                                return r;
490
                        break;
491

492
                case ARG_INSTALL_SOURCE: {
×
493
                        InstallSource is = install_source_from_string(optarg);
×
494
                        if (is < 0)
×
495
                                return log_error_errno(is, "Unexpected parameter for --install-source=: %s", optarg);
×
496

497
                        arg_install_source = is;
×
498
                        break;
×
499
                }
500

501
                case 'p':
47✔
502
                        arg_print_esp_path = true;
47✔
503
                        break;
47✔
504

505
                case 'x':
8✔
506
                        arg_print_dollar_boot_path = true;
8✔
507
                        break;
8✔
508

509
                case ARG_PRINT_LOADER_PATH:
×
510
                        arg_print_loader_path = true;
×
511
                        break;
×
512

513
                case ARG_PRINT_STUB_PATH:
6✔
514
                        arg_print_stub_path = true;
6✔
515
                        break;
6✔
516

517
                case 'R':
7✔
518
                        arg_print_root_device++;
7✔
519
                        break;
7✔
520

521
                case ARG_VARIABLES:
118✔
522
                        r = parse_tristate_argument("--variables=", optarg, &arg_touch_variables);
118✔
523
                        if (r < 0)
118✔
524
                                return r;
525
                        break;
526

527
                case ARG_NO_VARIABLES:
×
528
                        arg_touch_variables = false;
×
529
                        break;
×
530

531
                case ARG_RANDOM_SEED:
×
532
                        r = parse_boolean_argument("--random-seed=", optarg, &arg_install_random_seed);
×
533
                        if (r < 0)
×
534
                                return r;
535
                        break;
536

537
                case ARG_NO_PAGER:
×
538
                        arg_pager_flags |= PAGER_DISABLE;
×
539
                        break;
×
540

541
                case ARG_GRACEFUL:
136✔
542
                        _arg_graceful = ARG_GRACEFUL_YES;
136✔
543
                        break;
136✔
544

545
                case 'q':
6✔
546
                        arg_quiet = true;
6✔
547
                        break;
6✔
548

549
                case ARG_ENTRY_TOKEN:
×
550
                        r = parse_boot_entry_token_type(optarg, &arg_entry_token_type, &arg_entry_token);
×
551
                        if (r < 0)
×
552
                                return r;
553
                        break;
554

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

563
                                arg_make_entry_directory = r;
13✔
564
                        }
565
                        break;
566

567
                case ARG_JSON:
6✔
568
                        r = parse_json_argument(optarg, &arg_json_format_flags);
6✔
569
                        if (r <= 0)
6✔
570
                                return r;
571
                        break;
572

573
                case ARG_ARCH_ALL:
12✔
574
                        arg_arch_all = true;
12✔
575
                        break;
12✔
576

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

592
                case ARG_EFI_BOOT_OPTION_DESCRIPTION_WITH_DEVICE:
×
593
                        r = parse_boolean_argument("--efi-boot-option-description-with-device=", optarg, &arg_efi_boot_option_description_with_device);
×
594
                        if (r < 0)
×
595
                                return r;
596

597
                        break;
598

599
                case ARG_DRY_RUN:
×
600
                        arg_dry_run = true;
×
601
                        break;
×
602

603
                case ARG_SECURE_BOOT_AUTO_ENROLL:
1✔
604
                        r = parse_boolean_argument("--secure-boot-auto-enroll=", optarg, &arg_secure_boot_auto_enroll);
1✔
605
                        if (r < 0)
1✔
606
                                return r;
607
                        break;
608

609
                case ARG_CERTIFICATE:
1✔
610
                        r = free_and_strdup_warn(&arg_certificate, optarg);
1✔
611
                        if (r < 0)
1✔
612
                                return r;
613
                        break;
614

615
                case ARG_CERTIFICATE_SOURCE:
×
616
                        r = parse_openssl_certificate_source_argument(
×
617
                                        optarg,
618
                                        &arg_certificate_source,
619
                                        &arg_certificate_source_type);
620
                        if (r < 0)
×
621
                                return r;
622
                        break;
623

624
                case ARG_PRIVATE_KEY: {
1✔
625
                        r = free_and_strdup_warn(&arg_private_key, optarg);
1✔
626
                        if (r < 0)
1✔
627
                                return r;
628
                        break;
629
                }
630

631
                case ARG_PRIVATE_KEY_SOURCE:
×
632
                        r = parse_openssl_key_source_argument(
×
633
                                        optarg,
634
                                        &arg_private_key_source,
635
                                        &arg_private_key_source_type);
636
                        if (r < 0)
×
637
                                return r;
638
                        break;
639

640
                case '?':
641
                        return -EINVAL;
642

643
                default:
×
644
                        assert_not_reached();
×
645
                }
646

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

651
        if ((arg_root || arg_image) && argv[optind] && !STR_IN_SET(argv[optind], "status", "list",
291✔
652
                        "install", "update", "remove", "is-installed", "random-seed", "unlink", "cleanup"))
653
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
654
                                       "Options --root= and --image= are not supported with verb %s.",
655
                                       argv[optind]);
656

657
        if (arg_root && arg_image)
291✔
658
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Please specify either --root= or --image=, the combination of both is not supported.");
×
659

660
        if (arg_install_source != INSTALL_SOURCE_AUTO && !arg_root && !arg_image)
291✔
661
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--install-from-host is only supported with --root= or --image=.");
×
662

663
        if (arg_dry_run && argv[optind] && !STR_IN_SET(argv[optind], "unlink", "cleanup"))
291✔
664
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--dry-run is only supported with --unlink or --cleanup");
×
665

666
        if (arg_secure_boot_auto_enroll) {
291✔
667
#if HAVE_OPENSSL
668
                if (!arg_certificate)
1✔
669
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Secure boot auto-enrollment requested but no certificate provided.");
×
670

671
                if (!arg_private_key)
1✔
672
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Secure boot auto-enrollment requested but no private key provided.");
×
673
#else
674
                return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Secure boot auto-enrollment requested but OpenSSL support is disabled.");
675
#endif
676
        }
677

678
        r = sd_varlink_invocation(SD_VARLINK_ALLOW_ACCEPT);
291✔
679
        if (r < 0)
291✔
680
                return log_error_errno(r, "Failed to check if invoked in Varlink mode: %m");
×
681
        if (r > 0) {
291✔
682
                arg_varlink = true;
14✔
683
                arg_pager_flags |= PAGER_DISABLE;
14✔
684
        }
685

686
        return 1;
687
}
688

689
static int bootctl_main(int argc, char *argv[]) {
266✔
690
        static const Verb verbs[] = {
266✔
691
                { "help",                VERB_ANY, VERB_ANY, 0,            help                     },
692
                { "status",              VERB_ANY, 1,        VERB_DEFAULT, verb_status              },
693
                { "install",             VERB_ANY, 1,        0,            verb_install             },
694
                { "update",              VERB_ANY, 1,        0,            verb_install             },
695
                { "remove",              VERB_ANY, 1,        0,            verb_remove              },
696
                { "is-installed",        VERB_ANY, 1,        0,            verb_is_installed        },
697
                { "kernel-identify",     2,        2,        0,            verb_kernel_identify     },
698
                { "kernel-inspect",      2,        2,        0,            verb_kernel_inspect      },
699
                { "list",                VERB_ANY, 1,        0,            verb_list                },
700
                { "unlink",              2,        2,        0,            verb_list                },
701
                { "cleanup",             VERB_ANY, 1,        0,            verb_list                },
702
                { "set-default",         2,        2,        0,            verb_set_efivar          },
703
                { "set-oneshot",         2,        2,        0,            verb_set_efivar          },
704
                { "set-timeout",         2,        2,        0,            verb_set_efivar          },
705
                { "set-timeout-oneshot", 2,        2,        0,            verb_set_efivar          },
706
                { "set-sysfail",         2,        2,        0,            verb_set_efivar          },
707
                { "random-seed",         VERB_ANY, 1,        0,            verb_random_seed         },
708
                { "reboot-to-firmware",  VERB_ANY, 2,        0,            verb_reboot_to_firmware  },
709
                {}
710
        };
711

712
        return dispatch_verb(argc, argv, verbs, NULL);
266✔
713
}
714

715
static int vl_server(void) {
14✔
716
        _cleanup_(sd_varlink_server_unrefp) sd_varlink_server *varlink_server = NULL;
14✔
717
        int r;
14✔
718

719
        /* Invocation as Varlink service */
720

721
        r = varlink_server_new(
14✔
722
                        &varlink_server,
723
                        SD_VARLINK_SERVER_ROOT_ONLY|SD_VARLINK_SERVER_ALLOW_FD_PASSING_INPUT,
724
                        /* userdata= */ NULL);
725
        if (r < 0)
14✔
726
                return log_error_errno(r, "Failed to allocate Varlink server: %m");
×
727

728
        r = sd_varlink_server_add_interface(varlink_server, &vl_interface_io_systemd_BootControl);
14✔
729
        if (r < 0)
14✔
730
                return log_error_errno(r, "Failed to add Varlink interface: %m");
×
731

732
        r = sd_varlink_server_bind_method_many(
14✔
733
                        varlink_server,
734
                        "io.systemd.BootControl.ListBootEntries",     vl_method_list_boot_entries,
735
                        "io.systemd.BootControl.SetRebootToFirmware", vl_method_set_reboot_to_firmware,
736
                        "io.systemd.BootControl.GetRebootToFirmware", vl_method_get_reboot_to_firmware,
737
                        "io.systemd.BootControl.Install",             vl_method_install);
738
        if (r < 0)
14✔
739
                return log_error_errno(r, "Failed to bind Varlink methods: %m");
×
740

741
        r = sd_varlink_server_loop_auto(varlink_server);
14✔
742
        if (r < 0)
14✔
743
                return log_error_errno(r, "Failed to run Varlink event loop: %m");
×
744

745
        return 0;
746
}
747

748
static int run(int argc, char *argv[]) {
297✔
749
        _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
297✔
750
        _cleanup_(umount_and_freep) char *mounted_dir = NULL;
297✔
751
        int r;
297✔
752

753
        log_setup();
297✔
754

755
        r = parse_argv(argc, argv);
297✔
756
        if (r <= 0)
297✔
757
                return r;
758

759
        if (arg_varlink)
291✔
760
                return vl_server();
14✔
761

762
        if (arg_print_root_device > 0) {
277✔
763
                _cleanup_free_ char *path = NULL;
5✔
764
                dev_t devno;
5✔
765

766
                r = blockdev_get_root(LOG_ERR, &devno);
5✔
767
                if (r < 0)
5✔
768
                        return r;
769
                if (r == 0) {
5✔
770
                        log_error("Root file system not backed by a (single) whole block device.");
×
771
                        return 80; /* some recognizable error code */
×
772
                }
773

774
                if (arg_print_root_device > 1) {
5✔
775
                        r = block_get_whole_disk(devno, &devno);
2✔
776
                        if (r < 0)
2✔
777
                                log_debug_errno(r, "Unable to find whole block device for root block device, ignoring: %m");
×
778
                }
779

780
                r = device_path_make_canonical(S_IFBLK, devno, &path);
5✔
781
                if (r < 0)
5✔
782
                        return log_error_errno(r,
×
783
                                               "Failed to format canonical device path for devno '" DEVNUM_FORMAT_STR "': %m",
784
                                               DEVNUM_FORMAT_VAL(devno));
785

786
                puts(path);
5✔
787
                return 0;
788
        }
789

790
        if (arg_print_loader_path || arg_print_stub_path)
272✔
791
                return print_loader_or_stub_path();
6✔
792

793
        /* Open up and mount the image */
794
        if (arg_image) {
266✔
795
                assert(!arg_root);
26✔
796

797
                r = mount_image_privately_interactively(
26✔
798
                                arg_image,
799
                                arg_image_policy,
800
                                DISSECT_IMAGE_GENERIC_ROOT |
801
                                DISSECT_IMAGE_USR_NO_ROOT |
802
                                DISSECT_IMAGE_RELAX_VAR_CHECK |
803
                                DISSECT_IMAGE_ALLOW_USERSPACE_VERITY,
804
                                &mounted_dir,
805
                                /* ret_dir_fd= */ NULL,
806
                                &loop_device);
807
                if (r < 0)
26✔
808
                        return r;
809

810
                arg_root = strdup(mounted_dir);
26✔
811
                if (!arg_root)
26✔
812
                        return log_oom();
×
813
        }
814

815
        return bootctl_main(argc, argv);
266✔
816
}
817

818
DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);
297✔
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