]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
7b4d7cc0 | 2 | |
3f6fd1ba | 3 | #include <getopt.h> |
7b4d7cc0 | 4 | |
c56be2c2 | 5 | #include "blockdev-util.h" |
85f87535 | 6 | #include "bootctl.h" |
baafb202 | 7 | #include "bootctl-install.h" |
85f87535 | 8 | #include "bootctl-random-seed.h" |
34ea7e02 | 9 | #include "bootctl-reboot-to-firmware.h" |
6d8ca257 | 10 | #include "bootctl-set-efivar.h" |
da36788f LP |
11 | #include "bootctl-status.h" |
12 | #include "bootctl-systemd-efi-options.h" | |
53c368d7 | 13 | #include "bootctl-uki.h" |
d6b4d1c7 | 14 | #include "build.h" |
c56be2c2 | 15 | #include "devnum-util.h" |
80a2381d | 16 | #include "dissect-image.h" |
9dae4c8a | 17 | #include "escape.h" |
e94830c0 | 18 | #include "find-esp.h" |
608f8ec9 | 19 | #include "main-func.h" |
80a2381d | 20 | #include "mount-util.h" |
57db6f18 | 21 | #include "pager.h" |
6a3fff75 | 22 | #include "parse-argument.h" |
294bf0c3 | 23 | #include "pretty-print.h" |
d88c96ff | 24 | #include "utf8.h" |
2f2c539c LP |
25 | #include "verbs.h" |
26 | #include "virt.h" | |
7b4d7cc0 | 27 | |
d9bdb29b RH |
28 | /* EFI_BOOT_OPTION_DESCRIPTION_MAX sets the maximum length for the boot option description |
29 | * stored in NVRAM. The UEFI spec does not specify a minimum or maximum length for this | |
30 | * string, but we limit the length to something reasonable to prevent from the firmware | |
31 | * having to deal with a potentially too long string. */ | |
32 | #define EFI_BOOT_OPTION_DESCRIPTION_MAX ((size_t) 255) | |
33 | ||
85f87535 LP |
34 | char *arg_esp_path = NULL; |
35 | char *arg_xbootldr_path = NULL; | |
36 | bool arg_print_esp_path = false; | |
37 | bool arg_print_dollar_boot_path = false; | |
c56be2c2 | 38 | unsigned arg_print_root_device = 0; |
85f87535 LP |
39 | bool arg_touch_variables = true; |
40 | PagerFlags arg_pager_flags = 0; | |
41 | bool arg_graceful = false; | |
42 | bool arg_quiet = false; | |
43 | int arg_make_entry_directory = false; /* tri-state: < 0 for automatic logic */ | |
44 | sd_id128_t arg_machine_id = SD_ID128_NULL; | |
45 | char *arg_install_layout = NULL; | |
9e43296f | 46 | BootEntryTokenType arg_entry_token_type = BOOT_ENTRY_TOKEN_AUTO; |
85f87535 LP |
47 | char *arg_entry_token = NULL; |
48 | JsonFormatFlags arg_json_format_flags = JSON_FORMAT_OFF; | |
49 | bool arg_arch_all = false; | |
50 | char *arg_root = NULL; | |
51 | char *arg_image = NULL; | |
baafb202 | 52 | InstallSource arg_install_source = ARG_INSTALL_SOURCE_AUTO; |
85f87535 | 53 | char *arg_efi_boot_option_description = NULL; |
8702496b | 54 | bool arg_dry_run = false; |
84be0c71 | 55 | ImagePolicy *arg_image_policy = NULL; |
25579a43 | 56 | |
fbf45d22 LP |
57 | STATIC_DESTRUCTOR_REGISTER(arg_esp_path, freep); |
58 | STATIC_DESTRUCTOR_REGISTER(arg_xbootldr_path, freep); | |
47fb161e | 59 | STATIC_DESTRUCTOR_REGISTER(arg_install_layout, freep); |
f337f903 | 60 | STATIC_DESTRUCTOR_REGISTER(arg_entry_token, freep); |
80a2381d LB |
61 | STATIC_DESTRUCTOR_REGISTER(arg_root, freep); |
62 | STATIC_DESTRUCTOR_REGISTER(arg_image, freep); | |
d9bdb29b | 63 | STATIC_DESTRUCTOR_REGISTER(arg_efi_boot_option_description, freep); |
84be0c71 | 64 | STATIC_DESTRUCTOR_REGISTER(arg_image_policy, image_policy_freep); |
fbf45d22 | 65 | |
da36788f | 66 | int acquire_esp( |
0b2aa206 | 67 | int unprivileged_mode, |
e5a8b4b5 | 68 | bool graceful, |
5caa3167 LP |
69 | uint32_t *ret_part, |
70 | uint64_t *ret_pstart, | |
71 | uint64_t *ret_psize, | |
f63b5ad9 LP |
72 | sd_id128_t *ret_uuid, |
73 | dev_t *ret_devid) { | |
5caa3167 LP |
74 | |
75 | char *np; | |
2f2c539c LP |
76 | int r; |
77 | ||
fbf45d22 LP |
78 | /* Find the ESP, and log about errors. Note that find_esp_and_warn() will log in all error cases on |
79 | * its own, except for ENOKEY (which is good, we want to show our own message in that case, | |
80 | * suggesting use of --esp-path=) and EACCESS (only when we request unprivileged mode; in this case | |
81 | * we simply eat up the error here, so that --list and --status work too, without noise about | |
82 | * this). */ | |
5caa3167 | 83 | |
80a2381d | 84 | r = find_esp_and_warn(arg_root, arg_esp_path, unprivileged_mode, &np, ret_part, ret_pstart, ret_psize, ret_uuid, ret_devid); |
e5a8b4b5 LP |
85 | if (r == -ENOKEY) { |
86 | if (graceful) | |
14e6e444 ZJS |
87 | return log_full_errno(arg_quiet ? LOG_DEBUG : LOG_INFO, r, |
88 | "Couldn't find EFI system partition, skipping."); | |
e5a8b4b5 | 89 | |
af918182 | 90 | return log_error_errno(r, |
5caa3167 | 91 | "Couldn't find EFI system partition. It is recommended to mount it to /boot or /efi.\n" |
fbf45d22 | 92 | "Alternatively, use --esp-path= to specify path to mount point."); |
e5a8b4b5 | 93 | } |
5caa3167 LP |
94 | if (r < 0) |
95 | return r; | |
96 | ||
fbf45d22 LP |
97 | free_and_replace(arg_esp_path, np); |
98 | log_debug("Using EFI System Partition at %s.", arg_esp_path); | |
99 | ||
957b2423 | 100 | return 0; |
fbf45d22 | 101 | } |
0974a682 | 102 | |
da36788f | 103 | int acquire_xbootldr( |
0b2aa206 | 104 | int unprivileged_mode, |
f63b5ad9 LP |
105 | sd_id128_t *ret_uuid, |
106 | dev_t *ret_devid) { | |
107 | ||
fbf45d22 LP |
108 | char *np; |
109 | int r; | |
5caa3167 | 110 | |
80a2381d | 111 | r = find_xbootldr_and_warn(arg_root, arg_xbootldr_path, unprivileged_mode, &np, ret_uuid, ret_devid); |
fbf45d22 LP |
112 | if (r == -ENOKEY) { |
113 | log_debug_errno(r, "Didn't find an XBOOTLDR partition, using the ESP as $BOOT."); | |
f63b5ad9 LP |
114 | arg_xbootldr_path = mfree(arg_xbootldr_path); |
115 | ||
fbf45d22 LP |
116 | if (ret_uuid) |
117 | *ret_uuid = SD_ID128_NULL; | |
f63b5ad9 LP |
118 | if (ret_devid) |
119 | *ret_devid = 0; | |
fbf45d22 LP |
120 | return 0; |
121 | } | |
122 | if (r < 0) | |
123 | return r; | |
124 | ||
125 | free_and_replace(arg_xbootldr_path, np); | |
126 | log_debug("Using XBOOTLDR partition at %s as $BOOT.", arg_xbootldr_path); | |
127 | ||
128 | return 1; | |
7b4d7cc0 KS |
129 | } |
130 | ||
2f2c539c | 131 | static int help(int argc, char *argv[], void *userdata) { |
37ec0fdd LP |
132 | _cleanup_free_ char *link = NULL; |
133 | int r; | |
134 | ||
eb98074b ZJS |
135 | pager_open(arg_pager_flags); |
136 | ||
37ec0fdd LP |
137 | r = terminal_urlify_man("bootctl", "1", &link); |
138 | if (r < 0) | |
139 | return log_oom(); | |
2f2c539c | 140 | |
e684d2d5 | 141 | printf("%1$s [OPTIONS...] COMMAND ...\n" |
64a7fcc5 LP |
142 | "\n%5$sControl EFI firmware boot settings and manage boot loader.%6$s\n" |
143 | "\n%3$sGeneric EFI Firmware/Boot Loader Commands:%4$s\n" | |
5bbf72bb | 144 | " status Show status of installed boot loader and EFI variables\n" |
002914e6 | 145 | " reboot-to-firmware [BOOL]\n" |
5bbf72bb | 146 | " Query or set reboot-to-firmware EFI flag\n" |
64a7fcc5 | 147 | "\n%3$sBoot Loader Specification Commands:%4$s\n" |
5bbf72bb | 148 | " list List boot loader entries\n" |
eab70618 LP |
149 | " unlink ID Remove boot loader entry\n" |
150 | " cleanup Remove files in ESP not referenced in any boot entry\n" | |
151 | "\n%3$sBoot Loader Interface Commands:%4$s\n" | |
5bbf72bb LP |
152 | " set-default ID Set default boot loader entry\n" |
153 | " set-oneshot ID Set default boot loader entry, for next boot only\n" | |
154 | " set-timeout SECONDS Set the menu timeout\n" | |
39ddc32a | 155 | " set-timeout-oneshot SECONDS\n" |
5bbf72bb | 156 | " Set the menu timeout for the next boot only\n" |
64a7fcc5 | 157 | "\n%3$ssystemd-boot Commands:%4$s\n" |
5bbf72bb LP |
158 | " install Install systemd-boot to the ESP and EFI variables\n" |
159 | " update Update systemd-boot in the ESP and EFI variables\n" | |
160 | " remove Remove systemd-boot from the ESP and EFI variables\n" | |
161 | " is-installed Test whether systemd-boot is installed in the ESP\n" | |
2d085515 LP |
162 | " random-seed Initialize or refresh random seed in ESP and EFI\n" |
163 | " variables\n" | |
1e7d6cc0 | 164 | "\n%3$sKernel Image Commands:%4$s\n" |
5bbf72bb LP |
165 | " kernel-identify Identify kernel image type\n" |
166 | " kernel-inspect Prints details about the kernel image\n" | |
64a7fcc5 | 167 | "\n%3$sOptions:%4$s\n" |
e1fac8a6 ZJS |
168 | " -h --help Show this help\n" |
169 | " --version Print version\n" | |
170 | " --esp-path=PATH Path to the EFI System Partition (ESP)\n" | |
171 | " --boot-path=PATH Path to the $BOOT partition\n" | |
80a2381d LB |
172 | " --root=PATH Operate on an alternate filesystem root\n" |
173 | " --image=PATH Operate on disk image as filesystem root\n" | |
84be0c71 LP |
174 | " --image-policy=POLICY\n" |
175 | " Specify disk image dissection policy\n" | |
02d06ba1 LB |
176 | " --install-source=auto|image|host\n" |
177 | " Where to pick files when using --root=/--image=\n" | |
c56be2c2 LP |
178 | " -p --print-esp-path Print path to the EFI System Partition mount point\n" |
179 | " -x --print-boot-path Print path to the $BOOT partition mount point\n" | |
180 | " -R --print-root-device\n" | |
d5163f9c LP |
181 | " Print path to the block device node backing the\n" |
182 | " root file system (returns e.g. /dev/nvme0n1p5)\n" | |
183 | " -RR Print path to the whole disk block device node\n" | |
184 | " backing the root FS (returns e.g. /dev/nvme0n1)\n" | |
e1fac8a6 ZJS |
185 | " --no-variables Don't touch EFI variables\n" |
186 | " --no-pager Do not pipe output into a pager\n" | |
351de38e LP |
187 | " --graceful Don't fail when the ESP cannot be found or EFI\n" |
188 | " variables cannot be written\n" | |
14e6e444 | 189 | " -q --quiet Suppress output\n" |
f337f903 LP |
190 | " --make-entry-directory=yes|no|auto\n" |
191 | " Create $BOOT/ENTRY-TOKEN/ directory\n" | |
192 | " --entry-token=machine-id|os-id|os-image-id|auto|literal:…\n" | |
193 | " Entry token to use for this installation\n" | |
0d1506d4 LP |
194 | " --json=pretty|short|off\n" |
195 | " Generate JSON output\n" | |
6e916539 JJ |
196 | " --all-architectures\n" |
197 | " Install all supported EFI architectures\n" | |
d9bdb29b RH |
198 | " --efi-boot-option-description=DESCRIPTION\n" |
199 | " Description of the entry in the boot option list\n" | |
8702496b | 200 | " --dry-run Dry run (unlink and cleanup)\n" |
bc556335 DDM |
201 | "\nSee the %2$s for details.\n", |
202 | program_invocation_short_name, | |
203 | link, | |
204 | ansi_underline(), | |
205 | ansi_normal(), | |
206 | ansi_highlight(), | |
207 | ansi_normal()); | |
0974a682 KS |
208 | |
209 | return 0; | |
210 | } | |
211 | ||
0974a682 KS |
212 | static int parse_argv(int argc, char *argv[]) { |
213 | enum { | |
fbf45d22 LP |
214 | ARG_ESP_PATH = 0x100, |
215 | ARG_BOOT_PATH, | |
80a2381d LB |
216 | ARG_ROOT, |
217 | ARG_IMAGE, | |
06e78680 | 218 | ARG_IMAGE_POLICY, |
02d06ba1 | 219 | ARG_INSTALL_SOURCE, |
0974a682 KS |
220 | ARG_VERSION, |
221 | ARG_NO_VARIABLES, | |
57db6f18 | 222 | ARG_NO_PAGER, |
351de38e | 223 | ARG_GRACEFUL, |
f337f903 LP |
224 | ARG_MAKE_ENTRY_DIRECTORY, |
225 | ARG_ENTRY_TOKEN, | |
0d1506d4 | 226 | ARG_JSON, |
6e916539 | 227 | ARG_ARCH_ALL, |
d9bdb29b | 228 | ARG_EFI_BOOT_OPTION_DESCRIPTION, |
8702496b | 229 | ARG_DRY_RUN, |
7b4d7cc0 KS |
230 | }; |
231 | ||
0974a682 | 232 | static const struct option options[] = { |
d9bdb29b RH |
233 | { "help", no_argument, NULL, 'h' }, |
234 | { "version", no_argument, NULL, ARG_VERSION }, | |
235 | { "esp-path", required_argument, NULL, ARG_ESP_PATH }, | |
236 | { "path", required_argument, NULL, ARG_ESP_PATH }, /* Compatibility alias */ | |
237 | { "boot-path", required_argument, NULL, ARG_BOOT_PATH }, | |
238 | { "root", required_argument, NULL, ARG_ROOT }, | |
239 | { "image", required_argument, NULL, ARG_IMAGE }, | |
06e78680 | 240 | { "image-policy", required_argument, NULL, ARG_IMAGE_POLICY }, |
d9bdb29b RH |
241 | { "install-source", required_argument, NULL, ARG_INSTALL_SOURCE }, |
242 | { "print-esp-path", no_argument, NULL, 'p' }, | |
243 | { "print-path", no_argument, NULL, 'p' }, /* Compatibility alias */ | |
244 | { "print-boot-path", no_argument, NULL, 'x' }, | |
c56be2c2 | 245 | { "print-root-device", no_argument, NULL, 'R' }, |
d9bdb29b RH |
246 | { "no-variables", no_argument, NULL, ARG_NO_VARIABLES }, |
247 | { "no-pager", no_argument, NULL, ARG_NO_PAGER }, | |
248 | { "graceful", no_argument, NULL, ARG_GRACEFUL }, | |
249 | { "quiet", no_argument, NULL, 'q' }, | |
250 | { "make-entry-directory", required_argument, NULL, ARG_MAKE_ENTRY_DIRECTORY }, | |
251 | { "make-machine-id-directory", required_argument, NULL, ARG_MAKE_ENTRY_DIRECTORY }, /* Compatibility alias */ | |
252 | { "entry-token", required_argument, NULL, ARG_ENTRY_TOKEN }, | |
253 | { "json", required_argument, NULL, ARG_JSON }, | |
254 | { "all-architectures", no_argument, NULL, ARG_ARCH_ALL }, | |
255 | { "efi-boot-option-description", required_argument, NULL, ARG_EFI_BOOT_OPTION_DESCRIPTION }, | |
8702496b | 256 | { "dry-run", no_argument, NULL, ARG_DRY_RUN }, |
a36b411e | 257 | {} |
0974a682 KS |
258 | }; |
259 | ||
2f2c539c | 260 | int c, r; |
7b4d7cc0 KS |
261 | |
262 | assert(argc >= 0); | |
263 | assert(argv); | |
264 | ||
4697ee9f | 265 | while ((c = getopt_long(argc, argv, "hpxRq", options, NULL)) >= 0) |
0974a682 | 266 | switch (c) { |
7b4d7cc0 | 267 | |
0974a682 | 268 | case 'h': |
2f2c539c | 269 | help(0, NULL, NULL); |
7b4d7cc0 | 270 | return 0; |
7b4d7cc0 | 271 | |
0974a682 | 272 | case ARG_VERSION: |
3f6fd1ba | 273 | return version(); |
7b4d7cc0 | 274 | |
fbf45d22 LP |
275 | case ARG_ESP_PATH: |
276 | r = free_and_strdup(&arg_esp_path, optarg); | |
277 | if (r < 0) | |
278 | return log_oom(); | |
279 | break; | |
280 | ||
281 | case ARG_BOOT_PATH: | |
282 | r = free_and_strdup(&arg_xbootldr_path, optarg); | |
2f2c539c LP |
283 | if (r < 0) |
284 | return log_oom(); | |
0974a682 KS |
285 | break; |
286 | ||
80a2381d LB |
287 | case ARG_ROOT: |
288 | r = parse_path_argument(optarg, /* suppress_root= */ true, &arg_root); | |
289 | if (r < 0) | |
290 | return r; | |
291 | break; | |
292 | ||
293 | case ARG_IMAGE: | |
294 | r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_image); | |
295 | if (r < 0) | |
296 | return r; | |
297 | break; | |
298 | ||
06e78680 YW |
299 | case ARG_IMAGE_POLICY: |
300 | r = parse_image_policy_argument(optarg, &arg_image_policy); | |
301 | if (r < 0) | |
302 | return r; | |
303 | break; | |
304 | ||
02d06ba1 LB |
305 | case ARG_INSTALL_SOURCE: |
306 | if (streq(optarg, "auto")) | |
307 | arg_install_source = ARG_INSTALL_SOURCE_AUTO; | |
308 | else if (streq(optarg, "image")) | |
309 | arg_install_source = ARG_INSTALL_SOURCE_IMAGE; | |
310 | else if (streq(optarg, "host")) | |
311 | arg_install_source = ARG_INSTALL_SOURCE_HOST; | |
312 | else | |
313 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
314 | "Unexpected parameter for --install-source=: %s", optarg); | |
315 | ||
316 | break; | |
317 | ||
30b50477 | 318 | case 'p': |
fbf45d22 LP |
319 | arg_print_esp_path = true; |
320 | break; | |
321 | ||
fba4e945 | 322 | case 'x': |
fbf45d22 | 323 | arg_print_dollar_boot_path = true; |
30b50477 ZJS |
324 | break; |
325 | ||
c56be2c2 | 326 | case 'R': |
b3a9d980 | 327 | arg_print_root_device++; |
c56be2c2 LP |
328 | break; |
329 | ||
0974a682 KS |
330 | case ARG_NO_VARIABLES: |
331 | arg_touch_variables = false; | |
332 | break; | |
333 | ||
57db6f18 | 334 | case ARG_NO_PAGER: |
0221d68a | 335 | arg_pager_flags |= PAGER_DISABLE; |
57db6f18 LP |
336 | break; |
337 | ||
351de38e LP |
338 | case ARG_GRACEFUL: |
339 | arg_graceful = true; | |
340 | break; | |
341 | ||
14e6e444 ZJS |
342 | case 'q': |
343 | arg_quiet = true; | |
344 | break; | |
345 | ||
9e43296f YW |
346 | case ARG_ENTRY_TOKEN: |
347 | r = parse_boot_entry_token_type(optarg, &arg_entry_token_type, &arg_entry_token); | |
348 | if (r < 0) | |
349 | return r; | |
f337f903 | 350 | break; |
f337f903 LP |
351 | |
352 | case ARG_MAKE_ENTRY_DIRECTORY: | |
1ff493d5 | 353 | if (streq(optarg, "auto")) /* retained for backwards compatibility */ |
f337f903 | 354 | arg_make_entry_directory = -1; /* yes if machine-id is permanent */ |
6a3fff75 | 355 | else { |
51214cf4 | 356 | r = parse_boolean_argument("--make-entry-directory=", optarg, NULL); |
6a3fff75 | 357 | if (r < 0) |
358 | return r; | |
f337f903 | 359 | |
51214cf4 | 360 | arg_make_entry_directory = r; |
6a3fff75 | 361 | } |
362 | break; | |
363 | ||
0d1506d4 LP |
364 | case ARG_JSON: |
365 | r = parse_json_argument(optarg, &arg_json_format_flags); | |
366 | if (r <= 0) | |
367 | return r; | |
6e916539 | 368 | break; |
0d1506d4 | 369 | |
6e916539 JJ |
370 | case ARG_ARCH_ALL: |
371 | arg_arch_all = true; | |
0d1506d4 LP |
372 | break; |
373 | ||
d9bdb29b RH |
374 | case ARG_EFI_BOOT_OPTION_DESCRIPTION: |
375 | if (isempty(optarg) || !(string_is_safe(optarg) && utf8_is_valid(optarg))) { | |
376 | _cleanup_free_ char *escaped = NULL; | |
377 | ||
378 | escaped = cescape(optarg); | |
379 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
380 | "Invalid --efi-boot-option-description=: %s", strna(escaped)); | |
381 | } | |
382 | if (strlen(optarg) > EFI_BOOT_OPTION_DESCRIPTION_MAX) | |
383 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
51214cf4 ZJS |
384 | "--efi-boot-option-description= too long: %zu > %zu", |
385 | strlen(optarg), EFI_BOOT_OPTION_DESCRIPTION_MAX); | |
d9bdb29b RH |
386 | r = free_and_strdup_warn(&arg_efi_boot_option_description, optarg); |
387 | if (r < 0) | |
388 | return r; | |
389 | break; | |
390 | ||
8702496b LN |
391 | case ARG_DRY_RUN: |
392 | arg_dry_run = true; | |
393 | break; | |
394 | ||
0974a682 KS |
395 | case '?': |
396 | return -EINVAL; | |
397 | ||
398 | default: | |
04499a70 | 399 | assert_not_reached(); |
7b4d7cc0 | 400 | } |
7b4d7cc0 | 401 | |
c56be2c2 LP |
402 | if (!!arg_print_esp_path + !!arg_print_dollar_boot_path + (arg_print_root_device > 0) > 1) |
403 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
404 | "--print-esp-path/-p, --print-boot-path/-x, --print-root-device=/-R cannot be combined."); | |
405 | ||
80a2381d | 406 | if ((arg_root || arg_image) && argv[optind] && !STR_IN_SET(argv[optind], "status", "list", |
8702496b | 407 | "install", "update", "remove", "is-installed", "random-seed", "unlink", "cleanup")) |
80a2381d LB |
408 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
409 | "Options --root= and --image= are not supported with verb %s.", | |
410 | argv[optind]); | |
411 | ||
412 | if (arg_root && arg_image) | |
413 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Please specify either --root= or --image=, the combination of both is not supported."); | |
414 | ||
02d06ba1 LB |
415 | if (arg_install_source != ARG_INSTALL_SOURCE_AUTO && !arg_root && !arg_image) |
416 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--install-from-host is only supported with --root= or --image=."); | |
417 | ||
8702496b LN |
418 | if (arg_dry_run && argv[optind] && !STR_IN_SET(argv[optind], "unlink", "cleanup")) |
419 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--dry is only supported with --unlink or --cleanup"); | |
420 | ||
0974a682 KS |
421 | return 1; |
422 | } | |
7b4d7cc0 | 423 | |
2f2c539c | 424 | static int bootctl_main(int argc, char *argv[]) { |
2f2c539c | 425 | static const Verb verbs[] = { |
2536752d ZJS |
426 | { "help", VERB_ANY, VERB_ANY, 0, help }, |
427 | { "status", VERB_ANY, 1, VERB_DEFAULT, verb_status }, | |
428 | { "install", VERB_ANY, 1, 0, verb_install }, | |
429 | { "update", VERB_ANY, 1, 0, verb_install }, | |
430 | { "remove", VERB_ANY, 1, 0, verb_remove }, | |
431 | { "is-installed", VERB_ANY, 1, 0, verb_is_installed }, | |
53c368d7 | 432 | { "kernel-identify", 2, 2, 0, verb_kernel_identify }, |
a0525598 | 433 | { "kernel-inspect", 2, 2, 0, verb_kernel_inspect }, |
2536752d | 434 | { "list", VERB_ANY, 1, 0, verb_list }, |
8702496b LN |
435 | { "unlink", 2, 2, 0, verb_unlink }, |
436 | { "cleanup", VERB_ANY, 1, 0, verb_list }, | |
39ddc32a JJ |
437 | { "set-default", 2, 2, 0, verb_set_efivar }, |
438 | { "set-oneshot", 2, 2, 0, verb_set_efivar }, | |
439 | { "set-timeout", 2, 2, 0, verb_set_efivar }, | |
440 | { "set-timeout-oneshot", 2, 2, 0, verb_set_efivar }, | |
2536752d ZJS |
441 | { "random-seed", VERB_ANY, 1, 0, verb_random_seed }, |
442 | { "systemd-efi-options", VERB_ANY, 2, 0, verb_systemd_efi_options }, | |
002914e6 | 443 | { "reboot-to-firmware", VERB_ANY, 2, 0, verb_reboot_to_firmware }, |
2f2c539c LP |
444 | {} |
445 | }; | |
446 | ||
447 | return dispatch_verb(argc, argv, verbs, NULL); | |
448 | } | |
449 | ||
608f8ec9 | 450 | static int run(int argc, char *argv[]) { |
80a2381d | 451 | _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL; |
a4b3e942 | 452 | _cleanup_(umount_and_freep) char *mounted_dir = NULL; |
601185b4 | 453 | int r; |
7b4d7cc0 | 454 | |
87dc8b08 | 455 | log_setup(); |
7b4d7cc0 | 456 | |
341890de | 457 | /* If we run in a container, automatically turn off EFI file system access */ |
2f2c539c LP |
458 | if (detect_container() > 0) |
459 | arg_touch_variables = false; | |
460 | ||
7b4d7cc0 | 461 | r = parse_argv(argc, argv); |
601185b4 | 462 | if (r <= 0) |
608f8ec9 | 463 | return r; |
57db6f18 | 464 | |
c56be2c2 LP |
465 | if (arg_print_root_device > 0) { |
466 | _cleanup_free_ char *path = NULL; | |
467 | dev_t devno; | |
468 | ||
469 | r = blockdev_get_root(LOG_ERR, &devno); | |
470 | if (r < 0) | |
471 | return r; | |
472 | if (r == 0) { | |
473 | log_error("Root file system not backed by a (single) whole block device."); | |
474 | return 80; /* some recognizable error code */ | |
475 | } | |
476 | ||
477 | if (arg_print_root_device > 1) { | |
478 | r = block_get_whole_disk(devno, &devno); | |
479 | if (r < 0) | |
480 | log_debug_errno(r, "Unable to find whole block device for root block device, ignoring: %m"); | |
481 | } | |
482 | ||
483 | r = device_path_make_canonical(S_IFBLK, devno, &path); | |
484 | if (r < 0) | |
93d82cfc MY |
485 | return log_error_errno(r, |
486 | "Failed to format canonical device path for devno '" DEVNUM_FORMAT_STR "': %m", | |
487 | DEVNUM_FORMAT_VAL(devno)); | |
c56be2c2 LP |
488 | |
489 | puts(path); | |
03221e74 | 490 | return 0; |
c56be2c2 LP |
491 | } |
492 | ||
80a2381d LB |
493 | /* Open up and mount the image */ |
494 | if (arg_image) { | |
495 | assert(!arg_root); | |
496 | ||
497 | r = mount_image_privately_interactively( | |
498 | arg_image, | |
84be0c71 | 499 | arg_image_policy, |
80a2381d LB |
500 | DISSECT_IMAGE_GENERIC_ROOT | |
501 | DISSECT_IMAGE_RELAX_VAR_CHECK, | |
a4b3e942 | 502 | &mounted_dir, |
a133d2c3 | 503 | /* ret_dir_fd= */ NULL, |
e330f97a | 504 | &loop_device); |
80a2381d LB |
505 | if (r < 0) |
506 | return r; | |
507 | ||
a4b3e942 | 508 | arg_root = strdup(mounted_dir); |
80a2381d LB |
509 | if (!arg_root) |
510 | return log_oom(); | |
511 | } | |
512 | ||
608f8ec9 | 513 | return bootctl_main(argc, argv); |
7b4d7cc0 | 514 | } |
608f8ec9 | 515 | |
002914e6 | 516 | DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run); |