Merge pull request #22463 from poettering/kernel-install-propagate-machine-id
kernel-install: clean-up machine-id handling
This commit is contained in:
+198
-123
@@ -28,6 +28,7 @@
|
||||
#include "glyph-util.h"
|
||||
#include "main-func.h"
|
||||
#include "mkdir.h"
|
||||
#include "os-util.h"
|
||||
#include "pager.h"
|
||||
#include "parse-argument.h"
|
||||
#include "parse-util.h"
|
||||
@@ -56,13 +57,22 @@ static bool arg_print_dollar_boot_path = false;
|
||||
static bool arg_touch_variables = true;
|
||||
static PagerFlags arg_pager_flags = 0;
|
||||
static bool arg_graceful = false;
|
||||
static int arg_make_machine_id_directory = 0;
|
||||
static int arg_make_entry_directory = false; /* tri-state: < 0 for automatic logic */
|
||||
static sd_id128_t arg_machine_id = SD_ID128_NULL;
|
||||
static char *arg_install_layout = NULL;
|
||||
static enum {
|
||||
ARG_ENTRY_TOKEN_MACHINE_ID,
|
||||
ARG_ENTRY_TOKEN_OS_IMAGE_ID,
|
||||
ARG_ENTRY_TOKEN_OS_ID,
|
||||
ARG_ENTRY_TOKEN_LITERAL,
|
||||
ARG_ENTRY_TOKEN_AUTO,
|
||||
} arg_entry_token_type = ARG_ENTRY_TOKEN_AUTO;
|
||||
static char *arg_entry_token = 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 const char *arg_dollar_boot_path(void) {
|
||||
/* $BOOT shall be the XBOOTLDR partition if it exists, and otherwise the ESP */
|
||||
@@ -170,32 +180,133 @@ static int load_install_machine_id_and_layout(void) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int settle_install_machine_id(void) {
|
||||
static int settle_entry_token(void) {
|
||||
int r;
|
||||
|
||||
switch (arg_entry_token_type) {
|
||||
|
||||
case ARG_ENTRY_TOKEN_AUTO: {
|
||||
_cleanup_free_ char *buf = NULL;
|
||||
r = read_one_line_file("/etc/kernel/entry-token", &buf);
|
||||
if (r < 0 && r != -ENOENT)
|
||||
return log_error_errno(r, "Failed to read /etc/kernel/entry-token: %m");
|
||||
|
||||
if (!isempty(buf)) {
|
||||
free_and_replace(arg_entry_token, buf);
|
||||
arg_entry_token_type = ARG_ENTRY_TOKEN_LITERAL;
|
||||
} else if (sd_id128_is_null(arg_machine_id)) {
|
||||
_cleanup_free_ char *id = NULL, *image_id = NULL;
|
||||
|
||||
r = parse_os_release(NULL,
|
||||
"IMAGE_ID", &image_id,
|
||||
"ID", &id);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to load /etc/os-release: %m");
|
||||
|
||||
if (!isempty(image_id)) {
|
||||
free_and_replace(arg_entry_token, image_id);
|
||||
arg_entry_token_type = ARG_ENTRY_TOKEN_OS_IMAGE_ID;
|
||||
} else if (!isempty(id)) {
|
||||
free_and_replace(arg_entry_token, id);
|
||||
arg_entry_token_type = ARG_ENTRY_TOKEN_OS_ID;
|
||||
} else
|
||||
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No machine ID set, and /etc/os-release carries no ID=/IMAGE_ID= fields.");
|
||||
} else {
|
||||
r = free_and_strdup_warn(&arg_entry_token, SD_ID128_TO_STRING(arg_machine_id));
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
arg_entry_token_type = ARG_ENTRY_TOKEN_MACHINE_ID;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case ARG_ENTRY_TOKEN_MACHINE_ID:
|
||||
if (sd_id128_is_null(arg_machine_id))
|
||||
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No machine ID set.");
|
||||
|
||||
r = free_and_strdup_warn(&arg_entry_token, SD_ID128_TO_STRING(arg_machine_id));
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
break;
|
||||
|
||||
case ARG_ENTRY_TOKEN_OS_IMAGE_ID: {
|
||||
_cleanup_free_ char *buf = NULL;
|
||||
|
||||
r = parse_os_release(NULL, "IMAGE_ID", &buf);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to load /etc/os-release: %m");
|
||||
|
||||
if (isempty(buf))
|
||||
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "IMAGE_ID= field not set in /etc/os-release.");
|
||||
|
||||
free_and_replace(arg_entry_token, buf);
|
||||
break;
|
||||
}
|
||||
|
||||
case ARG_ENTRY_TOKEN_OS_ID: {
|
||||
_cleanup_free_ char *buf = NULL;
|
||||
|
||||
r = parse_os_release(NULL, "ID", &buf);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to load /etc/os-release: %m");
|
||||
|
||||
if (isempty(buf))
|
||||
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "ID= field not set in /etc/os-release.");
|
||||
|
||||
free_and_replace(arg_entry_token, buf);
|
||||
break;
|
||||
}
|
||||
|
||||
case ARG_ENTRY_TOKEN_LITERAL:
|
||||
assert(!isempty(arg_entry_token)); /* already filled in by command line parser */
|
||||
break;
|
||||
}
|
||||
|
||||
if (isempty(arg_entry_token) || !string_is_safe(arg_entry_token))
|
||||
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Selected entry token not valid: %s", arg_entry_token);
|
||||
|
||||
log_debug("Using entry token: %s", arg_entry_token);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool use_boot_loader_spec_type1(void) {
|
||||
/* If the layout is not specified, or if it is set explicitly to "bls" we assume Boot Loader
|
||||
* Specification Type #1 is the chosen format for our boot loader entries */
|
||||
return !arg_install_layout || streq(arg_install_layout, "bls");
|
||||
}
|
||||
|
||||
static int settle_make_entry_directory(void) {
|
||||
int r;
|
||||
|
||||
r = load_install_machine_id_and_layout();
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
bool layout_non_bls = arg_install_layout && !streq(arg_install_layout, "bls");
|
||||
if (arg_make_machine_id_directory < 0) {
|
||||
if (layout_non_bls || sd_id128_is_null(arg_machine_id))
|
||||
arg_make_machine_id_directory = 0;
|
||||
else {
|
||||
r = path_is_temporary_fs("/etc/machine-id");
|
||||
if (r < 0)
|
||||
return log_debug_errno(r, "Couldn't determine whether /etc/machine-id is on a temporary file system: %m");
|
||||
arg_make_machine_id_directory = r == 0;
|
||||
}
|
||||
r = settle_entry_token();
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
bool layout_type1 = use_boot_loader_spec_type1();
|
||||
if (arg_make_entry_directory < 0) { /* Automatic mode */
|
||||
if (layout_type1) {
|
||||
if (arg_entry_token == ARG_ENTRY_TOKEN_MACHINE_ID) {
|
||||
r = path_is_temporary_fs("/etc/machine-id");
|
||||
if (r < 0)
|
||||
return log_debug_errno(r, "Couldn't determine whether /etc/machine-id is on a temporary file system: %m");
|
||||
|
||||
arg_make_entry_directory = r == 0;
|
||||
} else
|
||||
arg_make_entry_directory = true;
|
||||
} else
|
||||
arg_make_entry_directory = false;
|
||||
}
|
||||
|
||||
if (arg_make_machine_id_directory > 0 && sd_id128_is_null(arg_machine_id))
|
||||
if (arg_make_entry_directory > 0 && !layout_type1)
|
||||
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
||||
"Machine ID not found, but bls directory creation was requested.");
|
||||
|
||||
if (arg_make_machine_id_directory > 0 && layout_non_bls)
|
||||
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
||||
"KERNEL_INSTALL_LAYOUT=%s is configured, but bls directory creation was requested.",
|
||||
"KERNEL_INSTALL_LAYOUT=%s is configured, but Boot Loader Specification Type #1 entry directory creation was requested.",
|
||||
arg_install_layout);
|
||||
|
||||
return 0;
|
||||
@@ -1039,14 +1150,14 @@ static int remove_subdirs(const char *root, const char *const *subdirs) {
|
||||
return r < 0 ? r : q;
|
||||
}
|
||||
|
||||
static int remove_machine_id_directory(const char *root) {
|
||||
static int remove_entry_directory(const char *root) {
|
||||
assert(root);
|
||||
assert(arg_make_machine_id_directory >= 0);
|
||||
assert(arg_make_entry_directory >= 0);
|
||||
|
||||
if (!arg_make_machine_id_directory || sd_id128_is_null(arg_machine_id))
|
||||
if (!arg_make_entry_directory || !arg_entry_token)
|
||||
return 0;
|
||||
|
||||
return rmdir_one(root, SD_ID128_TO_STRING(arg_machine_id));
|
||||
return rmdir_one(root, arg_entry_token);
|
||||
}
|
||||
|
||||
static int remove_binaries(const char *esp_path) {
|
||||
@@ -1138,7 +1249,7 @@ static int install_loader_config(const char *esp_path) {
|
||||
const char *p;
|
||||
int r;
|
||||
|
||||
assert(arg_make_machine_id_directory >= 0);
|
||||
assert(arg_make_entry_directory >= 0);
|
||||
|
||||
p = prefix_roota(esp_path, "/loader/loader.conf");
|
||||
if (access(p, F_OK) >= 0) /* Silently skip creation if the file already exists (early check) */
|
||||
@@ -1155,9 +1266,9 @@ static int install_loader_config(const char *esp_path) {
|
||||
fprintf(f, "#timeout 3\n"
|
||||
"#console-mode keep\n");
|
||||
|
||||
if (arg_make_machine_id_directory) {
|
||||
assert(!sd_id128_is_null(arg_machine_id));
|
||||
fprintf(f, "default %s-*\n", SD_ID128_TO_STRING(arg_machine_id));
|
||||
if (arg_make_entry_directory) {
|
||||
assert(arg_entry_token);
|
||||
fprintf(f, "default %s-*\n", arg_entry_token);
|
||||
}
|
||||
|
||||
r = fflush_sync_and_check(f);
|
||||
@@ -1174,100 +1285,33 @@ static int install_loader_config(const char *esp_path) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int install_machine_id_directory(const char *root) {
|
||||
static int install_entry_directory(const char *root) {
|
||||
assert(root);
|
||||
assert(arg_make_machine_id_directory >= 0);
|
||||
assert(arg_make_entry_directory >= 0);
|
||||
|
||||
if (!arg_make_machine_id_directory)
|
||||
if (!arg_make_entry_directory)
|
||||
return 0;
|
||||
|
||||
assert(!sd_id128_is_null(arg_machine_id));
|
||||
return mkdir_one(root, SD_ID128_TO_STRING(arg_machine_id));
|
||||
assert(arg_entry_token);
|
||||
return mkdir_one(root, arg_entry_token);
|
||||
}
|
||||
|
||||
static int install_machine_info_config(void) {
|
||||
_cleanup_free_ char *contents = NULL;
|
||||
size_t length;
|
||||
bool need_install_layout = true, need_machine_id;
|
||||
static int install_entry_token(void) {
|
||||
int r;
|
||||
|
||||
assert(arg_make_machine_id_directory >= 0);
|
||||
assert(arg_make_entry_directory >= 0);
|
||||
assert(arg_entry_token);
|
||||
|
||||
/* We only want to save the machine-id if we created any directories using it. */
|
||||
need_machine_id = arg_make_machine_id_directory;
|
||||
/* Let's save the used entry token in /etc/kernel/entry-token if we used it to create the entry
|
||||
* directory, or if anything else but the machine ID */
|
||||
|
||||
_cleanup_fclose_ FILE *orig = fopen("/etc/machine-info", "re");
|
||||
if (!orig && errno != ENOENT)
|
||||
return log_error_errno(errno, "Failed to open /etc/machine-info: %m");
|
||||
if (!arg_make_entry_directory && arg_entry_token_type == ARG_ENTRY_TOKEN_MACHINE_ID)
|
||||
return 0;
|
||||
|
||||
if (orig) {
|
||||
_cleanup_free_ char *install_layout = NULL, *machine_id = NULL;
|
||||
|
||||
r = parse_env_file(orig, "/etc/machine-info",
|
||||
"KERNEL_INSTALL_LAYOUT", &install_layout,
|
||||
"KERNEL_INSTALL_MACHINE_ID", &machine_id);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to parse /etc/machine-info: %m");
|
||||
|
||||
rewind(orig);
|
||||
|
||||
if (!isempty(install_layout))
|
||||
need_install_layout = false;
|
||||
|
||||
if (!isempty(machine_id))
|
||||
need_machine_id = false;
|
||||
|
||||
if (!need_install_layout && !need_machine_id) {
|
||||
log_debug("/etc/machine-info already has KERNEL_INSTALL_MACHINE_ID=%s and KERNEL_INSTALL_LAYOUT=%s.",
|
||||
machine_id, install_layout);
|
||||
return 0;
|
||||
}
|
||||
|
||||
r = read_full_stream(orig, &contents, &length);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to read /etc/machine-info: %m");
|
||||
}
|
||||
|
||||
_cleanup_(unlink_and_freep) char *dst_tmp = NULL;
|
||||
_cleanup_fclose_ FILE *dst = NULL;
|
||||
r = fopen_temporary_label("/etc/machine-info", /* The path for which to the look up the label */
|
||||
"/etc/machine-info", /* Where we want the file actually to end up */
|
||||
&dst, /* The temporary file we write to */
|
||||
&dst_tmp);
|
||||
r = write_string_file("/etc/kernel/entry-token", arg_entry_token, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC|WRITE_STRING_FILE_MKDIR_0755);
|
||||
if (r < 0)
|
||||
return log_debug_errno(r, "Failed to open temporary copy of /etc/machine-info: %m");
|
||||
return log_error_errno(r, "Failed to write entry token '%s' to /etc/kernel/entry-token", arg_entry_token);
|
||||
|
||||
if (contents)
|
||||
fwrite_unlocked(contents, 1, length, dst);
|
||||
|
||||
bool no_newline = !contents || contents[length - 1] == '\n';
|
||||
|
||||
if (need_install_layout) {
|
||||
const char *line = "\nKERNEL_INSTALL_LAYOUT=bls\n" + no_newline;
|
||||
fwrite_unlocked(line, 1, strlen(line), dst);
|
||||
no_newline = false;
|
||||
}
|
||||
|
||||
const char *mid_string = SD_ID128_TO_STRING(arg_machine_id);
|
||||
if (need_machine_id)
|
||||
fprintf(dst, "%sKERNEL_INSTALL_MACHINE_ID=%s\n",
|
||||
no_newline ? "" : "\n",
|
||||
mid_string);
|
||||
|
||||
r = fflush_and_check(dst);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to write temporary copy of /etc/machine-info: %m");
|
||||
if (fchmod(fileno(dst), 0644) < 0)
|
||||
return log_debug_errno(errno, "Failed to fchmod %s: %m", dst_tmp);
|
||||
|
||||
if (rename(dst_tmp, "/etc/machine-info") < 0)
|
||||
return log_error_errno(errno, "Failed to replace /etc/machine-info: %m");
|
||||
|
||||
log_info("%s /etc/machine-info with%s%s%s",
|
||||
orig ? "Updated" : "Created",
|
||||
need_install_layout ? " KERNEL_INSTALL_LAYOUT=bls" : "",
|
||||
need_machine_id ? " KERNEL_INSTALL_MACHINE_ID=" : "",
|
||||
need_machine_id ? mid_string : "");
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1311,8 +1355,10 @@ static int help(int argc, char *argv[], void *userdata) {
|
||||
" --no-pager Do not pipe output into a pager\n"
|
||||
" --graceful Don't fail when the ESP cannot be found or EFI\n"
|
||||
" variables cannot be written\n"
|
||||
" --make-machine-id-directory=yes|no|auto\n"
|
||||
" Create $BOOT/$MACHINE_ID\n"
|
||||
" --make-entry-directory=yes|no|auto\n"
|
||||
" Create $BOOT/ENTRY-TOKEN/ directory\n"
|
||||
" --entry-token=machine-id|os-id|os-image-id|auto|literal:…\n"
|
||||
" Entry token to use for this installation\n"
|
||||
"\nSee the %2$s for details.\n",
|
||||
program_invocation_short_name,
|
||||
link,
|
||||
@@ -1332,7 +1378,8 @@ static int parse_argv(int argc, char *argv[]) {
|
||||
ARG_NO_VARIABLES,
|
||||
ARG_NO_PAGER,
|
||||
ARG_GRACEFUL,
|
||||
ARG_MAKE_MACHINE_ID_DIRECTORY,
|
||||
ARG_MAKE_ENTRY_DIRECTORY,
|
||||
ARG_ENTRY_TOKEN,
|
||||
};
|
||||
|
||||
static const struct option options[] = {
|
||||
@@ -1347,7 +1394,9 @@ static int parse_argv(int argc, char *argv[]) {
|
||||
{ "no-variables", no_argument, NULL, ARG_NO_VARIABLES },
|
||||
{ "no-pager", no_argument, NULL, ARG_NO_PAGER },
|
||||
{ "graceful", no_argument, NULL, ARG_GRACEFUL },
|
||||
{ "make-machine-id-directory", required_argument, NULL, ARG_MAKE_MACHINE_ID_DIRECTORY },
|
||||
{ "make-entry-directory", required_argument, NULL, ARG_MAKE_ENTRY_DIRECTORY },
|
||||
{ "make-machine-id-directory", required_argument, NULL, ARG_MAKE_ENTRY_DIRECTORY }, /* Compatibility alias */
|
||||
{ "entry-token", required_argument, NULL, ARG_ENTRY_TOKEN },
|
||||
{}
|
||||
};
|
||||
|
||||
@@ -1405,14 +1454,40 @@ static int parse_argv(int argc, char *argv[]) {
|
||||
arg_graceful = true;
|
||||
break;
|
||||
|
||||
case ARG_MAKE_MACHINE_ID_DIRECTORY:
|
||||
if (streq(optarg, "auto")) /* retained for backwards compatibility */
|
||||
arg_make_machine_id_directory = -1; /* yes if machine-id is permanent */
|
||||
else {
|
||||
r = parse_boolean_argument("--make-machine-id-directory=", optarg, &b);
|
||||
case ARG_ENTRY_TOKEN: {
|
||||
const char *e;
|
||||
|
||||
if (streq(optarg, "machine-id")) {
|
||||
arg_entry_token_type = ARG_ENTRY_TOKEN_MACHINE_ID;
|
||||
arg_entry_token = mfree(arg_entry_token);
|
||||
} else if (streq(optarg, "os-image-id")) {
|
||||
arg_entry_token_type = ARG_ENTRY_TOKEN_OS_IMAGE_ID;
|
||||
arg_entry_token = mfree(arg_entry_token);
|
||||
} else if (streq(optarg, "os-id")) {
|
||||
arg_entry_token_type = ARG_ENTRY_TOKEN_OS_ID;
|
||||
arg_entry_token = mfree(arg_entry_token);
|
||||
} else if ((e = startswith(optarg, "literal:"))) {
|
||||
arg_entry_token_type = ARG_ENTRY_TOKEN_LITERAL;
|
||||
|
||||
r = free_and_strdup_warn(&arg_entry_token, e);
|
||||
if (r < 0)
|
||||
return r;
|
||||
arg_make_machine_id_directory = b;
|
||||
} else
|
||||
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
||||
"Unexpected parameter for --entry-token=: %s", optarg);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case ARG_MAKE_ENTRY_DIRECTORY:
|
||||
if (streq(optarg, "auto")) /* retained for backwards compatibility */
|
||||
arg_make_entry_directory = -1; /* yes if machine-id is permanent */
|
||||
else {
|
||||
r = parse_boolean_argument("--make-entry-directory=", optarg, &b);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
arg_make_entry_directory = b;
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -1881,7 +1956,7 @@ static int verb_install(int argc, char *argv[], void *userdata) {
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
r = settle_install_machine_id();
|
||||
r = settle_make_entry_directory();
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
@@ -1908,11 +1983,11 @@ static int verb_install(int argc, char *argv[], void *userdata) {
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
r = install_machine_id_directory(arg_dollar_boot_path());
|
||||
r = install_entry_directory(arg_dollar_boot_path());
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
r = install_machine_info_config();
|
||||
r = install_entry_token();
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
@@ -1945,7 +2020,7 @@ static int verb_remove(int argc, char *argv[], void *userdata) {
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
r = settle_install_machine_id();
|
||||
r = settle_make_entry_directory();
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
@@ -1967,7 +2042,7 @@ static int verb_remove(int argc, char *argv[], void *userdata) {
|
||||
if (q < 0 && r >= 0)
|
||||
r = q;
|
||||
|
||||
q = remove_machine_id_directory(arg_esp_path);
|
||||
q = remove_entry_directory(arg_esp_path);
|
||||
if (q < 0 && r >= 0)
|
||||
r = 1;
|
||||
|
||||
@@ -1977,7 +2052,7 @@ static int verb_remove(int argc, char *argv[], void *userdata) {
|
||||
if (q < 0 && r >= 0)
|
||||
r = q;
|
||||
|
||||
q = remove_machine_id_directory(arg_xbootldr_path);
|
||||
q = remove_entry_directory(arg_xbootldr_path);
|
||||
if (q < 0 && r >= 0)
|
||||
r = q;
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ INITRD_OPTIONS_SHIFT=4
|
||||
[ "$KERNEL_INSTALL_LAYOUT" = "bls" ] || exit 0
|
||||
|
||||
MACHINE_ID="$KERNEL_INSTALL_MACHINE_ID"
|
||||
ENTRY_TOKEN="$KERNEL_INSTALL_ENTRY_TOKEN"
|
||||
BOOT_ROOT="$KERNEL_INSTALL_BOOT_ROOT"
|
||||
|
||||
BOOT_MNT="$(stat -c %m "$BOOT_ROOT")"
|
||||
@@ -41,10 +42,10 @@ fi
|
||||
case "$COMMAND" in
|
||||
remove)
|
||||
[ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && \
|
||||
echo "Removing $BOOT_ROOT/loader/entries/$MACHINE_ID-$KERNEL_VERSION*.conf"
|
||||
echo "Removing $BOOT_ROOT/loader/entries/$ENTRY_TOKEN-$KERNEL_VERSION*.conf"
|
||||
exec rm -f \
|
||||
"$BOOT_ROOT/loader/entries/$MACHINE_ID-$KERNEL_VERSION.conf" \
|
||||
"$BOOT_ROOT/loader/entries/$MACHINE_ID-$KERNEL_VERSION+"*".conf"
|
||||
"$BOOT_ROOT/loader/entries/$ENTRY_TOKEN-$KERNEL_VERSION.conf" \
|
||||
"$BOOT_ROOT/loader/entries/$ENTRY_TOKEN-$KERNEL_VERSION+"*".conf"
|
||||
;;
|
||||
add)
|
||||
;;
|
||||
@@ -68,17 +69,26 @@ elif [ -r /usr/lib/kernel/cmdline ]; then
|
||||
else
|
||||
BOOT_OPTIONS="$(tr -s "$IFS" '\n' </proc/cmdline | grep -ve '^BOOT_IMAGE=' -e '^initrd=' | tr '\n' ' ')"
|
||||
fi
|
||||
|
||||
BOOT_OPTIONS="${BOOT_OPTIONS% }"
|
||||
|
||||
# If the boot entries are named after the machine ID, then suffix the kernel
|
||||
# command line with the machine ID we use, so that the machine ID remains
|
||||
# stable, even during factory reset, in the initrd (where the system's machine
|
||||
# ID is not directly accessible yet), and if the root file system is volatile.
|
||||
if [ "$ENTRY_TOKEN" = "$MACHINE_ID" ]; then
|
||||
BOOT_OPTIONS="$BOOT_OPTIONS systemd.machine_id=$MACHINE_ID"
|
||||
fi
|
||||
|
||||
if [ -r /etc/kernel/tries ]; then
|
||||
read -r TRIES </etc/kernel/tries
|
||||
if ! echo "$TRIES" | grep -q '^[0-9][0-9]*$'; then
|
||||
echo "/etc/kernel/tries does not contain an integer." >&2
|
||||
exit 1
|
||||
fi
|
||||
LOADER_ENTRY="$BOOT_ROOT/loader/entries/$MACHINE_ID-$KERNEL_VERSION+$TRIES.conf"
|
||||
LOADER_ENTRY="$BOOT_ROOT/loader/entries/$ENTRY_TOKEN-$KERNEL_VERSION+$TRIES.conf"
|
||||
else
|
||||
LOADER_ENTRY="$BOOT_ROOT/loader/entries/$MACHINE_ID-$KERNEL_VERSION.conf"
|
||||
LOADER_ENTRY="$BOOT_ROOT/loader/entries/$ENTRY_TOKEN-$KERNEL_VERSION.conf"
|
||||
fi
|
||||
|
||||
if ! [ -d "$ENTRY_DIR_ABS" ]; then
|
||||
@@ -116,7 +126,10 @@ mkdir -p "${LOADER_ENTRY%/*}" || {
|
||||
{
|
||||
echo "title $PRETTY_NAME"
|
||||
echo "version $KERNEL_VERSION"
|
||||
echo "machine-id $MACHINE_ID"
|
||||
if [ "$ENTRY_TOKEN" = "$MACHINE_ID" ]; then
|
||||
# See similar logic above for the systemd.machine_id= kernel command line option
|
||||
echo "machine-id $MACHINE_ID"
|
||||
fi
|
||||
echo "options $BOOT_OPTIONS"
|
||||
echo "linux $ENTRY_DIR/linux"
|
||||
|
||||
|
||||
@@ -25,6 +25,7 @@ usage()
|
||||
echo "Usage:"
|
||||
echo " $0 [OPTIONS...] add KERNEL-VERSION KERNEL-IMAGE [INITRD-FILE ...]"
|
||||
echo " $0 [OPTIONS...] remove KERNEL-VERSION"
|
||||
echo " $0 [OPTIONS...] inspect"
|
||||
echo "Options:"
|
||||
echo " -h, --help Print this help"
|
||||
echo " -v, --verbose Increase verbosity"
|
||||
@@ -72,13 +73,17 @@ else
|
||||
[ $# -ge 1 ] && shift
|
||||
fi
|
||||
|
||||
if [ $# -lt 1 ]; then
|
||||
echo "Error: not enough arguments" >&2
|
||||
exit 1
|
||||
fi
|
||||
if [ "$COMMAND" = "inspect" ]; then
|
||||
KERNEL_VERSION=""
|
||||
else
|
||||
if [ $# -lt 1 ]; then
|
||||
echo "Error: not enough arguments" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
KERNEL_VERSION="$1"
|
||||
shift
|
||||
KERNEL_VERSION="$1"
|
||||
shift
|
||||
fi
|
||||
|
||||
layout=
|
||||
initrd_generator=
|
||||
@@ -89,44 +94,75 @@ elif [ -r "/usr/lib/kernel/install.conf" ]; then
|
||||
. /usr/lib/kernel/install.conf
|
||||
fi
|
||||
|
||||
# Prefer to use an existing machine ID from /etc/machine-info or /etc/machine-id. If we're using the machine
|
||||
# ID /etc/machine-id, try to persist it in /etc/machine-info. If no machine ID is found, try to generate
|
||||
# a new machine ID in /etc/machine-info. If that fails, use "Default".
|
||||
[ -z "$MACHINE_ID" ] && [ -r /etc/machine-info ] && . /etc/machine-info && MACHINE_ID="$KERNEL_INSTALL_MACHINE_ID"
|
||||
[ -z "$MACHINE_ID" ] && [ -r /etc/machine-id ] && read -r MACHINE_ID </etc/machine-id
|
||||
[ -n "$MACHINE_ID" ] && [ -z "$KERNEL_INSTALL_MACHINE_ID" ] && echo "KERNEL_INSTALL_MACHINE_ID=$MACHINE_ID" >>/etc/machine-info
|
||||
[ -z "$MACHINE_ID" ] && NEW_MACHINE_ID="$(systemd-id128 new)" && echo "KERNEL_INSTALL_MACHINE_ID=$NEW_MACHINE_ID" >>/etc/machine-info
|
||||
[ -z "$MACHINE_ID" ] && [ -r /etc/machine-info ] && . /etc/machine-info && MACHINE_ID="$KERNEL_INSTALL_MACHINE_ID"
|
||||
[ -z "$MACHINE_ID" ] && MACHINE_ID="Default"
|
||||
# If /etc/machine-id is initialized we'll use it, otherwise we'll use a freshly
|
||||
# generated one. If the user configured an explicit machine ID to use in
|
||||
# /etc/machine-info to use for our purpose, we'll use that instead (for
|
||||
# compatibility).
|
||||
[ -z "$MACHINE_ID" ] && [ -r /etc/machine-info ] && . /etc/machine-info && MACHINE_ID="$KERNEL_INSTALL_MACHINE_ID"
|
||||
[ -z "$MACHINE_ID" ] && [ -r /etc/machine-id ] && read -r MACHINE_ID </etc/machine-id
|
||||
[ -z "$MACHINE_ID" ] && MACHINE_ID="$(systemd-id128 new)"
|
||||
|
||||
[ -z "$BOOT_ROOT" ] && for suff in "$MACHINE_ID" "loader/entries"; do
|
||||
for pref in "/efi" "/boot" "/boot/efi" ; do
|
||||
# Now that we determined the machine ID to use, let's determine the "token" for
|
||||
# the boot loader entry to generate. We use that for naming the directory below
|
||||
# $BOOT where we want to place the kernel/initrd and related resources, as well
|
||||
# for naming the .conf boot loader spec entry. Typically this is just the
|
||||
# machine ID, but it can be anything else, too, if we are told so.
|
||||
[ -z "$ENTRY_TOKEN" ] && [ -r /etc/kernel/entry-token ] && read -r ENTRY_TOKEN </etc/kernel/entry-token
|
||||
if [ -z "$ENTRY_TOKEN" ]; then
|
||||
# If not configured explicitly, then use a few candidates: the machine ID,
|
||||
# the IMAGE_ID= and ID= fields from /etc/os-release and finally the fixed
|
||||
# string "Default"
|
||||
ENTRY_TOKEN_SEARCH="$MACHINE_ID"
|
||||
[ -r /etc/os-release ] && . /etc/os-release
|
||||
[ -n "$IMAGE_ID" ] && ENTRY_TOKEN_SEARCH="$ENTRY_TOKEN_SEARCH $IMAGE_ID"
|
||||
[ -n "$ID" ] && ENTRY_TOKEN_SEARCH="$ENTRY_TOKEN_SEARCH $ID"
|
||||
ENTRY_TOKEN_SEARCH="$ENTRY_TOKEN_SEARCH Default"
|
||||
else
|
||||
ENTRY_TOKEN_SEARCH="$ENTRY_TOKEN"
|
||||
fi
|
||||
|
||||
# NB: The $MACHINE_ID is guaranteed to be a valid machine ID, but
|
||||
# $ENTRY_TOKEN can be any string that fits into a VFAT filename, though
|
||||
# typically is just the machine ID.
|
||||
|
||||
[ -z "$BOOT_ROOT" ] && for suff in $ENTRY_TOKEN_SEARCH; do
|
||||
for pref in "/efi" "/boot" "/boot/efi"; do
|
||||
if [ -d "$pref/$suff" ]; then
|
||||
BOOT_ROOT="$pref"
|
||||
ENTRY_TOKEN="$suff"
|
||||
break 2
|
||||
fi
|
||||
done
|
||||
done
|
||||
|
||||
[ -z "$BOOT_ROOT" ] && for pref in "/efi" "/boot" "/boot/efi"; do
|
||||
if [ -d "$pref/loader/entries" ]; then
|
||||
BOOT_ROOT="$pref"
|
||||
break
|
||||
fi
|
||||
done
|
||||
|
||||
[ -z "$BOOT_ROOT" ] && for pref in "/efi" "/boot/efi"; do
|
||||
if mountpoint -q "$pref"; then
|
||||
BOOT_ROOT="$pref"
|
||||
break
|
||||
fi
|
||||
done
|
||||
|
||||
[ -z "$BOOT_ROOT" ] && BOOT_ROOT="/boot"
|
||||
|
||||
[ -z "$ENTRY_TOKEN" ] && ENTRY_TOKEN="$MACHINE_ID"
|
||||
|
||||
if [ -z "$layout" ]; then
|
||||
# Administrative decision: if not present, some scripts generate into /boot.
|
||||
if [ -d "$BOOT_ROOT/$MACHINE_ID" ]; then
|
||||
if [ -d "$BOOT_ROOT/$ENTRY_TOKEN" ]; then
|
||||
layout="bls"
|
||||
else
|
||||
layout="other"
|
||||
fi
|
||||
fi
|
||||
|
||||
ENTRY_DIR_ABS="$BOOT_ROOT/$MACHINE_ID/$KERNEL_VERSION"
|
||||
ENTRY_DIR_ABS="$BOOT_ROOT/$ENTRY_TOKEN/$KERNEL_VERSION"
|
||||
|
||||
# Provide a directory where to store generated initrds
|
||||
cleanup() {
|
||||
@@ -138,6 +174,7 @@ trap cleanup EXIT
|
||||
KERNEL_INSTALL_STAGING_AREA="$(mktemp -d -t -p /tmp kernel-install.staging.XXXXXXX)"
|
||||
|
||||
export KERNEL_INSTALL_MACHINE_ID="$MACHINE_ID"
|
||||
export KERNEL_INSTALL_ENTRY_TOKEN="$ENTRY_TOKEN"
|
||||
export KERNEL_INSTALL_BOOT_ROOT="$BOOT_ROOT"
|
||||
export KERNEL_INSTALL_LAYOUT="$layout"
|
||||
export KERNEL_INSTALL_INITRD_GENERATOR="$initrd_generator"
|
||||
@@ -170,7 +207,7 @@ case "$COMMAND" in
|
||||
fi
|
||||
|
||||
if [ "$MAKE_ENTRY_DIR_ABS" -eq 0 ]; then
|
||||
# Compatibility with earlier versions that used the presence of $BOOT_ROOT/$MACHINE_ID
|
||||
# Compatibility with earlier versions that used the presence of $BOOT_ROOT/$ENTRY_TOKEN
|
||||
# to signal to 00-entry-directory to create $ENTRY_DIR_ABS
|
||||
# to serve as the indication to use or to not use the BLS
|
||||
if [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ]; then
|
||||
@@ -205,6 +242,18 @@ case "$COMMAND" in
|
||||
fi
|
||||
;;
|
||||
|
||||
inspect)
|
||||
echo "KERNEL_INSTALL_MACHINE_ID: $KERNEL_INSTALL_MACHINE_ID"
|
||||
echo "KERNEL_INSTALL_ENTRY_TOKEN: $KERNEL_INSTALL_ENTRY_TOKEN"
|
||||
echo "KERNEL_INSTALL_BOOT_ROOT: $KERNEL_INSTALL_BOOT_ROOT"
|
||||
echo "KERNEL_INSTALL_LAYOUT: $KERNEL_INSTALL_LAYOUT"
|
||||
echo "KERNEL_INSTALL_INITRD_GENERATOR: $KERNEL_INSTALL_INITRD_GENERATOR"
|
||||
echo "ENTRY_DIR_ABS: $KERNEL_INSTALL_BOOT_ROOT/$ENTRY_TOKEN/\$KERNEL_VERSION"
|
||||
|
||||
# Assert that ENTRY_DIR_ABS actually matches what we are printing here
|
||||
[ "${ENTRY_DIR_ABS%/*}" = "$KERNEL_INSTALL_BOOT_ROOT/$ENTRY_TOKEN" ] || { echo "Assertion didn't pass." >&2; exit 1; }
|
||||
|
||||
;;
|
||||
*)
|
||||
echo "Error: unknown command '$COMMAND'" >&2
|
||||
exit 1
|
||||
|
||||
Reference in New Issue
Block a user