bootctl: add --root and --image
Operate on image/directory, and also take files to install from it
This commit is contained in:
@@ -108,11 +108,11 @@ static int acquire_path(void) {
|
||||
if (!strv_isempty(arg_path))
|
||||
return 0;
|
||||
|
||||
r = find_esp_and_warn(NULL, /* unprivileged_mode= */ false, &esp_path, NULL, NULL, NULL, NULL, &esp_devid);
|
||||
r = find_esp_and_warn(NULL, NULL, /* unprivileged_mode= */ false, &esp_path, NULL, NULL, NULL, NULL, &esp_devid);
|
||||
if (r < 0 && r != -ENOKEY) /* ENOKEY means not found, and is the only error the function won't log about on its own */
|
||||
return r;
|
||||
|
||||
r = find_xbootldr_and_warn(NULL, /* unprivileged_mode= */ false, &xbootldr_path, NULL, &xbootldr_devid);
|
||||
r = find_xbootldr_and_warn(NULL, NULL, /* unprivileged_mode= */ false, &xbootldr_path, NULL, &xbootldr_devid);
|
||||
if (r < 0 && r != -ENOKEY)
|
||||
return r;
|
||||
|
||||
|
||||
+133
-31
@@ -15,9 +15,11 @@
|
||||
#include "alloc-util.h"
|
||||
#include "blkid-util.h"
|
||||
#include "bootspec.h"
|
||||
#include "chase-symlinks.h"
|
||||
#include "copy.h"
|
||||
#include "devnum-util.h"
|
||||
#include "dirent-util.h"
|
||||
#include "dissect-image.h"
|
||||
#include "efi-api.h"
|
||||
#include "efi-loader.h"
|
||||
#include "efivars.h"
|
||||
@@ -31,6 +33,7 @@
|
||||
#include "glyph-util.h"
|
||||
#include "main-func.h"
|
||||
#include "mkdir.h"
|
||||
#include "mount-util.h"
|
||||
#include "os-util.h"
|
||||
#include "pager.h"
|
||||
#include "parse-argument.h"
|
||||
@@ -75,11 +78,15 @@ static enum {
|
||||
static char *arg_entry_token = NULL;
|
||||
static JsonFormatFlags arg_json_format_flags = JSON_FORMAT_OFF;
|
||||
static bool arg_arch_all = false;
|
||||
static char *arg_root = NULL;
|
||||
static char *arg_image = NULL;
|
||||
|
||||
STATIC_DESTRUCTOR_REGISTER(arg_esp_path, freep);
|
||||
STATIC_DESTRUCTOR_REGISTER(arg_xbootldr_path, freep);
|
||||
STATIC_DESTRUCTOR_REGISTER(arg_install_layout, freep);
|
||||
STATIC_DESTRUCTOR_REGISTER(arg_entry_token, freep);
|
||||
STATIC_DESTRUCTOR_REGISTER(arg_root, freep);
|
||||
STATIC_DESTRUCTOR_REGISTER(arg_image, freep);
|
||||
|
||||
static const char *arg_dollar_boot_path(void) {
|
||||
/* $BOOT shall be the XBOOTLDR partition if it exists, and otherwise the ESP */
|
||||
@@ -104,7 +111,7 @@ static int acquire_esp(
|
||||
* we simply eat up the error here, so that --list and --status work too, without noise about
|
||||
* this). */
|
||||
|
||||
r = find_esp_and_warn(arg_esp_path, unprivileged_mode, &np, ret_part, ret_pstart, ret_psize, ret_uuid, ret_devid);
|
||||
r = find_esp_and_warn(arg_root, arg_esp_path, unprivileged_mode, &np, ret_part, ret_pstart, ret_psize, ret_uuid, ret_devid);
|
||||
if (r == -ENOKEY) {
|
||||
if (graceful)
|
||||
return log_full_errno(arg_quiet ? LOG_DEBUG : LOG_INFO, r,
|
||||
@@ -131,7 +138,7 @@ static int acquire_xbootldr(
|
||||
char *np;
|
||||
int r;
|
||||
|
||||
r = find_xbootldr_and_warn(arg_xbootldr_path, unprivileged_mode, &np, ret_uuid, ret_devid);
|
||||
r = find_xbootldr_and_warn(arg_root, arg_xbootldr_path, unprivileged_mode, &np, ret_uuid, ret_devid);
|
||||
if (r == -ENOKEY) {
|
||||
log_debug_errno(r, "Didn't find an XBOOTLDR partition, using the ESP as $BOOT.");
|
||||
arg_xbootldr_path = mfree(arg_xbootldr_path);
|
||||
@@ -596,7 +603,6 @@ static int boot_config_load_and_select(
|
||||
const char *xbootldr_path,
|
||||
dev_t xbootldr_devid) {
|
||||
|
||||
_cleanup_strv_free_ char **efi_entries = NULL;
|
||||
int r;
|
||||
|
||||
/* If XBOOTLDR and ESP actually refer to the same block device, suppress XBOOTLDR, since it would
|
||||
@@ -607,15 +613,19 @@ static int boot_config_load_and_select(
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
r = efi_loader_get_entries(&efi_entries);
|
||||
if (r == -ENOENT || ERRNO_IS_NOT_SUPPORTED(r))
|
||||
log_debug_errno(r, "Boot loader reported no entries.");
|
||||
else if (r < 0)
|
||||
log_warning_errno(r, "Failed to determine entries reported by boot loader, ignoring: %m");
|
||||
else
|
||||
(void) boot_config_augment_from_loader(config, efi_entries, /* only_auto= */ false);
|
||||
if (!arg_root) {
|
||||
_cleanup_strv_free_ char **efi_entries = NULL;
|
||||
|
||||
return boot_config_select_special_entries(config);
|
||||
r = efi_loader_get_entries(&efi_entries);
|
||||
if (r == -ENOENT || ERRNO_IS_NOT_SUPPORTED(r))
|
||||
log_debug_errno(r, "Boot loader reported no entries.");
|
||||
else if (r < 0)
|
||||
log_warning_errno(r, "Failed to determine entries reported by boot loader, ignoring: %m");
|
||||
else
|
||||
(void) boot_config_augment_from_loader(config, efi_entries, /* only_auto= */ false);
|
||||
}
|
||||
|
||||
return boot_config_select_special_entries(config, /* skip_efivars= */ !!arg_root);
|
||||
}
|
||||
|
||||
static int status_entries(
|
||||
@@ -833,48 +843,74 @@ static int create_subdirs(const char *root, const char * const *subdirs) {
|
||||
}
|
||||
|
||||
static int copy_one_file(const char *esp_path, const char *name, bool force) {
|
||||
_cleanup_free_ char *source_path = NULL, *dest_path = NULL, *p = NULL, *q = NULL;
|
||||
const char *e;
|
||||
char *p, *q, *dest_name, *s;
|
||||
int r;
|
||||
char *dest_name, *s;
|
||||
int r, ret;
|
||||
|
||||
dest_name = strdupa_safe(name);
|
||||
s = endswith_no_case(dest_name, ".signed");
|
||||
if (s)
|
||||
*s = 0;
|
||||
|
||||
p = strjoina(BOOTLIBDIR "/", name);
|
||||
q = strjoina(esp_path, "/EFI/systemd/", dest_name);
|
||||
r = copy_file_with_version_check(p, q, force);
|
||||
p = path_join(BOOTLIBDIR, name);
|
||||
if (!p)
|
||||
return log_oom();
|
||||
|
||||
r = chase_symlinks(p, arg_root, CHASE_PREFIX_ROOT, &source_path, NULL);
|
||||
if (r < 0)
|
||||
return log_error_errno(r,
|
||||
"Failed to resolve path %s%s%s: %m",
|
||||
p,
|
||||
arg_root ? " under directory " : "",
|
||||
arg_root ? arg_root : "");
|
||||
|
||||
q = path_join("/EFI/systemd/", dest_name);
|
||||
if (!q)
|
||||
return log_oom();
|
||||
|
||||
r = chase_symlinks(q, esp_path, CHASE_PREFIX_ROOT | CHASE_NONEXISTENT, &dest_path, NULL);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to resolve path %s under directory %s: %m", q, esp_path);
|
||||
|
||||
/* Note that if this fails we do the second copy anyway, but return this error code,
|
||||
* so we stash it away in a separate variable. */
|
||||
ret = copy_file_with_version_check(source_path, dest_path, force);
|
||||
|
||||
e = startswith(dest_name, "systemd-boot");
|
||||
if (e) {
|
||||
int k;
|
||||
_cleanup_free_ char *default_dest_path = NULL;
|
||||
char *v;
|
||||
|
||||
/* Create the EFI default boot loader name (specified for removable devices) */
|
||||
v = strjoina(esp_path, "/EFI/BOOT/BOOT", e);
|
||||
v = strjoina("/EFI/BOOT/BOOT", e);
|
||||
ascii_strupper(strrchr(v, '/') + 1);
|
||||
|
||||
k = copy_file_with_version_check(p, v, force);
|
||||
if (k < 0 && r == 0)
|
||||
r = k;
|
||||
r = chase_symlinks(v, esp_path, CHASE_PREFIX_ROOT | CHASE_NONEXISTENT, &default_dest_path, NULL);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to resolve path %s under directory %s: %m", v, esp_path);
|
||||
|
||||
r = copy_file_with_version_check(source_path, default_dest_path, force);
|
||||
if (r < 0 && ret == 0)
|
||||
ret = r;
|
||||
}
|
||||
|
||||
return r;
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int install_binaries(const char *esp_path, const char *arch, bool force) {
|
||||
_cleanup_closedir_ DIR *d = NULL;
|
||||
int r = 0;
|
||||
_cleanup_free_ char *path = NULL;
|
||||
int r;
|
||||
|
||||
d = opendir(BOOTLIBDIR);
|
||||
if (!d)
|
||||
return log_error_errno(errno, "Failed to open \""BOOTLIBDIR"\": %m");
|
||||
r = chase_symlinks_and_opendir(BOOTLIBDIR, arg_root, CHASE_PREFIX_ROOT, &path, &d);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to open boot loader directory %s: %m", BOOTLIBDIR);
|
||||
|
||||
const char *suffix = strjoina(arch, ".efi");
|
||||
const char *suffix_signed = strjoina(arch, ".efi.signed");
|
||||
|
||||
FOREACH_DIRENT(de, d, return log_error_errno(errno, "Failed to read \""BOOTLIBDIR"\": %m")) {
|
||||
FOREACH_DIRENT(de, d, return log_error_errno(errno, "Failed to read \"%s\": %m", path)) {
|
||||
int k;
|
||||
|
||||
if (!endswith_no_case(de->d_name, suffix) && !endswith_no_case(de->d_name, suffix_signed))
|
||||
@@ -1022,6 +1058,12 @@ static int install_variables(const char *esp_path,
|
||||
uint16_t slot;
|
||||
int r;
|
||||
|
||||
if (arg_root) {
|
||||
log_info("Acting on %s, skipping EFI variable setup.",
|
||||
arg_image ? "image" : "root directory");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!is_efi_boot()) {
|
||||
log_warning("Not booted with EFI, skipping EFI variable setup.");
|
||||
return 0;
|
||||
@@ -1178,7 +1220,7 @@ static int remove_variables(sd_id128_t uuid, const char *path, bool in_order) {
|
||||
uint16_t slot;
|
||||
int r;
|
||||
|
||||
if (!is_efi_boot())
|
||||
if (arg_root || !is_efi_boot())
|
||||
return 0;
|
||||
|
||||
r = find_slot(uuid, path, &slot);
|
||||
@@ -1350,6 +1392,8 @@ static int help(int argc, char *argv[], void *userdata) {
|
||||
" --version Print version\n"
|
||||
" --esp-path=PATH Path to the EFI System Partition (ESP)\n"
|
||||
" --boot-path=PATH Path to the $BOOT partition\n"
|
||||
" --root=PATH Operate on an alternate filesystem root\n"
|
||||
" --image=PATH Operate on disk image as filesystem root\n"
|
||||
" -p --print-esp-path Print path to the EFI System Partition\n"
|
||||
" -x --print-boot-path Print path to the $BOOT partition\n"
|
||||
" --no-variables Don't touch EFI variables\n"
|
||||
@@ -1380,6 +1424,8 @@ static int parse_argv(int argc, char *argv[]) {
|
||||
enum {
|
||||
ARG_ESP_PATH = 0x100,
|
||||
ARG_BOOT_PATH,
|
||||
ARG_ROOT,
|
||||
ARG_IMAGE,
|
||||
ARG_VERSION,
|
||||
ARG_NO_VARIABLES,
|
||||
ARG_NO_PAGER,
|
||||
@@ -1396,6 +1442,8 @@ static int parse_argv(int argc, char *argv[]) {
|
||||
{ "esp-path", required_argument, NULL, ARG_ESP_PATH },
|
||||
{ "path", required_argument, NULL, ARG_ESP_PATH }, /* Compatibility alias */
|
||||
{ "boot-path", required_argument, NULL, ARG_BOOT_PATH },
|
||||
{ "root", required_argument, NULL, ARG_ROOT },
|
||||
{ "image", required_argument, NULL, ARG_IMAGE },
|
||||
{ "print-esp-path", no_argument, NULL, 'p' },
|
||||
{ "print-path", no_argument, NULL, 'p' }, /* Compatibility alias */
|
||||
{ "print-boot-path", no_argument, NULL, 'x' },
|
||||
@@ -1439,6 +1487,18 @@ static int parse_argv(int argc, char *argv[]) {
|
||||
return log_oom();
|
||||
break;
|
||||
|
||||
case ARG_ROOT:
|
||||
r = parse_path_argument(optarg, /* suppress_root= */ true, &arg_root);
|
||||
if (r < 0)
|
||||
return r;
|
||||
break;
|
||||
|
||||
case ARG_IMAGE:
|
||||
r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_image);
|
||||
if (r < 0)
|
||||
return r;
|
||||
break;
|
||||
|
||||
case 'p':
|
||||
if (arg_print_dollar_boot_path)
|
||||
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
||||
@@ -1523,6 +1583,15 @@ static int parse_argv(int argc, char *argv[]) {
|
||||
assert_not_reached();
|
||||
}
|
||||
|
||||
if ((arg_root || arg_image) && argv[optind] && !STR_IN_SET(argv[optind], "status", "list",
|
||||
"install", "update", "remove", "is-installed", "random-seed"))
|
||||
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
||||
"Options --root= and --image= are not supported with verb %s.",
|
||||
argv[optind]);
|
||||
|
||||
if (arg_root && arg_image)
|
||||
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Please specify either --root= or --image=, the combination of both is not supported.");
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -1608,7 +1677,7 @@ static int verb_status(int argc, char *argv[], void *userdata) {
|
||||
|
||||
pager_open(arg_pager_flags);
|
||||
|
||||
if (is_efi_boot()) {
|
||||
if (!arg_root && is_efi_boot()) {
|
||||
static const struct {
|
||||
uint64_t flag;
|
||||
const char *name;
|
||||
@@ -1722,7 +1791,7 @@ static int verb_status(int argc, char *argv[], void *userdata) {
|
||||
r = k;
|
||||
}
|
||||
|
||||
if (is_efi_boot()) {
|
||||
if (!arg_root && is_efi_boot()) {
|
||||
k = status_variables();
|
||||
if (k < 0)
|
||||
r = k;
|
||||
@@ -1846,6 +1915,12 @@ static int install_random_seed(const char *esp) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (arg_root) {
|
||||
log_warning("Acting on %s, skipping EFI variable setup.",
|
||||
arg_image ? "image" : "root directory");
|
||||
return 0;
|
||||
}
|
||||
|
||||
r = getenv_bool("SYSTEMD_WRITE_SYSTEM_TOKEN");
|
||||
if (r < 0) {
|
||||
if (r != -ENXIO)
|
||||
@@ -2204,6 +2279,11 @@ static int parse_loader_entry_target_arg(const char *arg1, char16_t **ret_target
|
||||
static int verb_set_efivar(int argc, char *argv[], void *userdata) {
|
||||
int r;
|
||||
|
||||
if (arg_root)
|
||||
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
|
||||
"Acting on %s, skipping EFI variable setup.",
|
||||
arg_image ? "image" : "root directory");
|
||||
|
||||
if (!is_efi_boot())
|
||||
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
|
||||
"Not booted with UEFI.");
|
||||
@@ -2267,7 +2347,7 @@ static int verb_set_efivar(int argc, char *argv[], void *userdata) {
|
||||
static int verb_random_seed(int argc, char *argv[], void *userdata) {
|
||||
int r;
|
||||
|
||||
r = find_esp_and_warn(arg_esp_path, false, &arg_esp_path, NULL, NULL, NULL, NULL, NULL);
|
||||
r = find_esp_and_warn(arg_root, arg_esp_path, false, &arg_esp_path, NULL, NULL, NULL, NULL, NULL);
|
||||
if (r == -ENOKEY) {
|
||||
/* find_esp_and_warn() doesn't warn about ENOKEY, so let's do that on our own */
|
||||
if (!arg_graceful)
|
||||
@@ -2375,6 +2455,9 @@ static int bootctl_main(int argc, char *argv[]) {
|
||||
}
|
||||
|
||||
static int run(int argc, char *argv[]) {
|
||||
_cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
|
||||
_cleanup_(decrypted_image_unrefp) DecryptedImage *decrypted_image = NULL;
|
||||
_cleanup_(umount_and_rmdir_and_freep) char *unlink_dir = NULL;
|
||||
int r;
|
||||
|
||||
log_parse_environment();
|
||||
@@ -2388,6 +2471,25 @@ static int run(int argc, char *argv[]) {
|
||||
if (r <= 0)
|
||||
return r;
|
||||
|
||||
/* Open up and mount the image */
|
||||
if (arg_image) {
|
||||
assert(!arg_root);
|
||||
|
||||
r = mount_image_privately_interactively(
|
||||
arg_image,
|
||||
DISSECT_IMAGE_GENERIC_ROOT |
|
||||
DISSECT_IMAGE_RELAX_VAR_CHECK,
|
||||
&unlink_dir,
|
||||
&loop_device,
|
||||
&decrypted_image);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
arg_root = strdup(unlink_dir);
|
||||
if (!arg_root)
|
||||
return log_oom();
|
||||
}
|
||||
|
||||
return bootctl_main(argc, argv);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user