From 1b20d88987d0785da7e694fd0e002c14142e08e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Wed, 26 Sep 2018 07:45:56 +0200 Subject: [PATCH] Move logic to find default sd-boot entry from systemctl to shared In preparation for use in other places. No functional change. --- src/shared/bootspec.c | 33 +++++++++++++++++++++++++++++++++ src/shared/bootspec.h | 2 ++ src/systemctl/systemctl.c | 17 ++++------------- 3 files changed, 39 insertions(+), 13 deletions(-) diff --git a/src/shared/bootspec.c b/src/shared/bootspec.c index fc0a6728ab..7a6399eaa7 100644 --- a/src/shared/bootspec.c +++ b/src/shared/bootspec.c @@ -598,3 +598,36 @@ found: return 0; } + +int find_default_boot_entry( + const char *esp_path, + char **esp_where, + BootConfig *config, + const BootEntry **e) { + + _cleanup_free_ char *where = NULL; + int r; + + assert(config); + assert(e); + + r = find_esp_and_warn(esp_path, false, &where, NULL, NULL, NULL, NULL); + if (r < 0) + return r; + + r = boot_entries_load_config(where, config); + if (r < 0) + return log_error_errno(r, "Failed to load bootspec config from \"%s/loader\": %m", where); + + if (config->default_entry < 0) { + log_error("No entry suitable as default, refusing to guess."); + return -ENOENT; + } + + *e = &config->entries[config->default_entry]; + + if (esp_where) + *esp_where = TAKE_PTR(where); + + return 0; +} diff --git a/src/shared/bootspec.h b/src/shared/bootspec.h index 614df698e6..e0fcaaea6f 100644 --- a/src/shared/bootspec.h +++ b/src/shared/bootspec.h @@ -51,3 +51,5 @@ static inline const char* boot_entry_title(const BootEntry *entry) { } int find_esp_and_warn(const char *path, bool unprivileged_mode, char **ret_path, uint32_t *ret_part, uint64_t *ret_pstart, uint64_t *ret_psize, sd_id128_t *ret_uuid); + +int find_default_boot_entry(const char *esp_path, char **esp_where, BootConfig *config, const BootEntry **e); diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c index ce5cbe7c13..be1b7375af 100644 --- a/src/systemctl/systemctl.c +++ b/src/systemctl/systemctl.c @@ -3455,21 +3455,12 @@ static int load_kexec_kernel(void) { if (access(KEXEC, X_OK) < 0) return log_error_errno(errno, KEXEC" is not available: %m"); - r = find_esp_and_warn(arg_esp_path, false, &where, NULL, NULL, NULL, NULL); - if (r == -ENOKEY) /* find_esp_and_warn() doesn't warn about this case */ + r = find_default_boot_entry(arg_esp_path, &where, &config, &e); + if (r == -ENOKEY) /* find_default_boot_entry() doesn't warn about this case */ return log_error_errno(r, "Cannot find the ESP partition mount point."); - if (r < 0) /* But it logs about all these cases, hence don't log here again */ - return r; - - r = boot_entries_load_config(where, &config); if (r < 0) - return log_error_errno(r, "Failed to load bootspec config from \"%s/loader\": %m", where); - - if (config.default_entry < 0) { - log_error("No entry suitable as default, refusing to guess."); - return -ENOENT; - } - e = &config.entries[config.default_entry]; + /* But it logs about all these cases, hence don't log here again */ + return r; if (strv_length(e->initrd) > 1) { log_error("Boot entry specifies multiple initrds, which is not supported currently.");