kernel-install: configurable entry name format (#43290)

For some of us building image based operating systems with
`systemd-sysupdate` it's useful to have UKI filenames that encode
`IMAGE_VERSION` (and architecture). For example
`SimonOS_45.20260805.0_x86-64.efi`.

Currently to do this I have to manually post-process around the `.efi`
produced by `60-ukify` because it is named
`$ENTRY_TOKEN-$KERNEL_VERSION.efi`. It'd be very useful if I could skip
that step and instead have the image built directly without this
post-processing step; then extract the UKI and /usr partitions and plop
them onto my update server. It makes the build much more "install RPMs
into a tree and you're done" (barring other things `60-ukify` currently
can't do but I have follow-ups for those in mind).

This PR adds a knob to `install.conf` to let users specify a custom
format for the output filename for both UKI's and BLS entries produced
by `kernel-install`.

It accepts the following specifiers:

- `%e`: entry token
- `%v`: kernel version being installed
- Several common specifiers
    
The default is %e-%v, which is the current behavior. With
`entry_name_format=%M_%A_%a`, an OS with `IMAGE_ID=SimonOS` and
`IMAGE_VERSION=1.0` would get moved to `SimonOS_1.0_x86-64.efi`.
    
The resolved name is exported as `$KERNEL_INSTALL_ENTRY_NAME` for shell
plugins. Both `90-uki-copy.install` and `90-loaderentry.install` are
updated to use it, with a fallback to `$ENTRY_TOKEN-$KERNEL_VERSION` for
compatibility with older kernel-install versions.
This commit is contained in:
Yu Watanabe
2026-08-08 04:09:10 +09:00
committed by GitHub
8 changed files with 293 additions and 29 deletions
+2 -1
View File
@@ -338,7 +338,8 @@ static int load_kernel_install_layout(InstallContext *c) {
/* ret_boot_root= */ NULL,
&layout,
/* ret_initrd_generator= */ NULL,
/* ret_uki_generator= */ NULL);
/* ret_uki_generator= */ NULL,
/* ret_entry_name_format= */ NULL);
if (r <= 0)
return r;
+2 -1
View File
@@ -35,6 +35,7 @@ fi
MACHINE_ID="${KERNEL_INSTALL_MACHINE_ID:?}"
ENTRY_TOKEN="${KERNEL_INSTALL_ENTRY_TOKEN:?}"
BOOT_ROOT="${KERNEL_INSTALL_BOOT_ROOT:?}"
ENTRY_NAME="${KERNEL_INSTALL_ENTRY_NAME:-$ENTRY_TOKEN-$KERNEL_VERSION}"
[ -n "$BOOT_MNT" ] || BOOT_MNT="$(stat -c %m "$BOOT_ROOT")"
if [ "$BOOT_MNT" = '/' ]; then
@@ -45,7 +46,7 @@ fi
KERNEL_DEST="$ENTRY_DIR_ABS/linux"
KERNEL_ENTRY="$ENTRY_DIR/linux"
LOADER_ENTRY="$BOOT_ROOT/loader/entries/$ENTRY_TOKEN-$KERNEL_VERSION.conf"
LOADER_ENTRY="$BOOT_ROOT/loader/entries/$ENTRY_NAME.conf"
case "$COMMAND" in
remove)
+13 -12
View File
@@ -28,6 +28,7 @@ KERNEL_IMAGE="$4"
ENTRY_TOKEN="$KERNEL_INSTALL_ENTRY_TOKEN"
BOOT_ROOT="$KERNEL_INSTALL_BOOT_ROOT"
ENTRY_NAME="${KERNEL_INSTALL_ENTRY_NAME:-$ENTRY_TOKEN-$KERNEL_VERSION}"
UKI_DIR="$BOOT_ROOT/EFI/Linux"
@@ -39,11 +40,11 @@ case "$COMMAND" in
exit 0
fi
[ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && \
echo "Removing $UKI_DIR/$ENTRY_TOKEN-$KERNEL_VERSION*.efi and extras"
echo "Removing $UKI_DIR/$ENTRY_NAME*.efi and extras"
exec rm -rf \
"$UKI_DIR/$ENTRY_TOKEN-$KERNEL_VERSION.efi" \
"$UKI_DIR/$ENTRY_TOKEN-$KERNEL_VERSION.efi.extra.d/" \
"$UKI_DIR/$ENTRY_TOKEN-$KERNEL_VERSION+"*".efi"
"$UKI_DIR/$ENTRY_NAME.efi" \
"$UKI_DIR/$ENTRY_NAME.efi.extra.d/" \
"$UKI_DIR/$ENTRY_NAME+"*".efi"
;;
add)
;;
@@ -67,24 +68,24 @@ if [ -f "$TRIES_FILE" ]; then
echo "$TRIES_FILE does not contain an integer." >&2
exit 1
fi
if [ -f "$UKI_DIR/$ENTRY_TOKEN-$KERNEL_VERSION.efi" ]; then
if [ -f "$UKI_DIR/$ENTRY_NAME.efi" ]; then
[ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && \
echo "Removing previous UKI '$UKI_DIR/$ENTRY_TOKEN-$KERNEL_VERSION.efi' without boot counting." >&2
rm -f "$UKI_DIR/$ENTRY_TOKEN-$KERNEL_VERSION.efi"
echo "Removing previous UKI '$UKI_DIR/$ENTRY_NAME.efi' without boot counting." >&2
rm -f "$UKI_DIR/$ENTRY_NAME.efi"
fi
for uki in "$UKI_DIR/$ENTRY_TOKEN-$KERNEL_VERSION+"*.efi; do
[ "$uki" = "$UKI_DIR/$ENTRY_TOKEN-$KERNEL_VERSION+*.efi" ] && break
for uki in "$UKI_DIR/$ENTRY_NAME+"*.efi; do
[ "$uki" = "$UKI_DIR/$ENTRY_NAME+*.efi" ] && break
[ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && \
echo "Removing previous UKI '$uki' that has not yet booted successfully." >&2
rm -f "$uki"
done
UKI_FILE="$UKI_DIR/$ENTRY_TOKEN-$KERNEL_VERSION+$TRIES.efi"
UKI_FILE="$UKI_DIR/$ENTRY_NAME+$TRIES.efi"
else
UKI_FILE="$UKI_DIR/$ENTRY_TOKEN-$KERNEL_VERSION.efi"
UKI_FILE="$UKI_DIR/$ENTRY_NAME.efi"
fi
EXTRA_DIR="$UKI_DIR/$ENTRY_TOKEN-$KERNEL_VERSION.efi.extra.d"
EXTRA_DIR="$UKI_DIR/$ENTRY_NAME.efi.extra.d"
# If there is a UKI named uki.efi on the staging area use that, if not use what
# was passed in as $KERNEL_IMAGE but insist it has a .efi extension
+86 -3
View File
@@ -35,6 +35,7 @@
#include "path-util.h"
#include "recurse-dir.h"
#include "rm-rf.h"
#include "specifier.h"
#include "stat-util.h"
#include "string-table.h"
#include "string-util.h"
@@ -108,12 +109,19 @@ typedef struct Context {
char **initrds;
char *initrd_generator;
char *uki_generator;
char *entry_name_format;
char *entry_name;
char *staging_area;
char **plugins;
char **argv;
char **envp;
} Context;
/* Longest filename suffix plugins append to the entry name (from 90-uki-copy.install) */
#define ENTRY_NAME_SUFFIX_MAX STRLEN(".efi.extra.d")
#define DEFAULT_ENTRY_NAME_FORMAT "%e-%v"
#define CONTEXT_NULL \
(Context) { \
.rfd = XAT_FDROOT, \
@@ -137,6 +145,8 @@ static void context_done(Context *c) {
strv_free(c->initrds);
free(c->initrd_generator);
free(c->uki_generator);
free(c->entry_name_format);
free(c->entry_name);
if (c->action == ACTION_INSPECT)
free(c->staging_area);
else
@@ -199,6 +209,9 @@ static int context_copy(const Context *source, Context *ret) {
if (r < 0)
return r;
r = strdup_to(&copy.uki_generator, source->uki_generator);
if (r < 0)
return r;
r = strdup_to(&copy.entry_name_format, source->entry_name_format);
if (r < 0)
return r;
r = strdup_to(&copy.staging_area, source->staging_area);
@@ -327,6 +340,63 @@ static int context_set_uki_generator(Context *c, const char *s, const char *sour
return context_set_string(s, source, "UKI_GENERATOR", &c->uki_generator);
}
static int context_set_entry_name_format(Context *c, const char *s, const char *source) {
assert(c);
return context_set_string(s, source, "ENTRY_NAME_FORMAT", &c->entry_name_format);
}
static int context_resolve_entry_name(Context *c) {
assert(c);
if (c->entry_name)
return 0;
const Specifier table[] = {
{ 'e', specifier_string, c->entry_token },
{ 'm', specifier_id128, &c->machine_id },
{ 'v', specifier_string, c->version ?: "KERNEL_VERSION" },
{ 'a', specifier_architecture, NULL },
{ 'A', specifier_os_image_version, NULL },
{ 'B', specifier_os_build_id, NULL },
{ 'H', specifier_hostname, NULL },
{ 'l', specifier_short_hostname, NULL },
{ 'q', specifier_pretty_hostname, NULL },
{ 'M', specifier_os_image_id, NULL },
{ 'o', specifier_os_id, NULL },
{ 'w', specifier_os_version_id, NULL },
{ 'W', specifier_os_variant_id, NULL },
{}
};
_cleanup_free_ char *resolved = NULL;
int r;
r = specifier_printf(
c->entry_name_format ?: DEFAULT_ENTRY_NAME_FORMAT,
NAME_MAX - ENTRY_NAME_SUFFIX_MAX,
table,
arg_root,
/* userdata= */ c,
&resolved);
if (r < 0)
return log_error_errno(r, "Failed to expand entry name format '%s': %m",
c->entry_name_format ?: DEFAULT_ENTRY_NAME_FORMAT);
if (isempty(resolved))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Entry name format '%s' resolved to empty string.",
c->entry_name_format ?: DEFAULT_ENTRY_NAME_FORMAT);
if (!filename_is_valid(resolved))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Entry name format '%s' resolved to invalid filename: %s",
c->entry_name_format ?: DEFAULT_ENTRY_NAME_FORMAT, resolved);
c->entry_name = TAKE_PTR(resolved);
log_debug("Using entry name: %s", c->entry_name);
return 0;
}
static int context_set_version(Context *c, const char *s) {
assert(c);
@@ -449,12 +519,14 @@ static int context_load_environment(Context *c) {
(void) context_set_boot_root(c, getenv("BOOT_ROOT"), "environment");
(void) context_set_conf_root(c, getenv("KERNEL_INSTALL_CONF_ROOT"), "environment");
(void) context_set_plugins(c, getenv("KERNEL_INSTALL_PLUGINS"), "environment");
(void) context_set_entry_name_format(c, getenv("KERNEL_INSTALL_ENTRY_NAME_FORMAT"), "environment");
return 0;
}
static int context_load_install_conf(Context *c) {
_cleanup_free_ char *machine_id = NULL, *boot_root = NULL, *layout = NULL,
*initrd_generator = NULL, *uki_generator = NULL;
*initrd_generator = NULL, *uki_generator = NULL,
*entry_name_format = NULL;
int r;
assert(c);
@@ -467,7 +539,8 @@ static int context_load_install_conf(Context *c) {
&boot_root,
&layout,
&initrd_generator,
&uki_generator);
&uki_generator,
&entry_name_format);
if (r <= 0)
return r;
@@ -476,6 +549,7 @@ static int context_load_install_conf(Context *c) {
(void) context_set_layout(c, layout, "config");
(void) context_set_initrd_generator(c, initrd_generator, "config");
(void) context_set_uki_generator(c, uki_generator, "config");
(void) context_set_entry_name_format(c, entry_name_format, "config");
log_debug("Loaded config.");
return 0;
@@ -1032,6 +1106,10 @@ static int context_build_environment(Context *c) {
if (c->envp)
return 0;
r = context_resolve_entry_name(c);
if (r < 0)
return r;
r = strv_env_assign_many(&e,
"LC_COLLATE", SYSTEMD_DEFAULT_LOCALE,
"KERNEL_INSTALL_VERBOSE", one_zero(arg_verbose),
@@ -1043,7 +1121,8 @@ static int context_build_environment(Context *c) {
"KERNEL_INSTALL_INITRD_GENERATOR", strempty(c->initrd_generator),
"KERNEL_INSTALL_UKI_GENERATOR", strempty(c->uki_generator),
"KERNEL_INSTALL_BOOT_ENTRY_TYPE", boot_entry_type_to_string(c->entry_type),
"KERNEL_INSTALL_STAGING_AREA", c->staging_area);
"KERNEL_INSTALL_STAGING_AREA", c->staging_area,
"KERNEL_INSTALL_ENTRY_NAME", c->entry_name);
if (r < 0)
return log_error_errno(r, "Failed to build environment variables for plugins: %m");
@@ -1413,6 +1492,10 @@ static int verb_inspect(int argc, char *argv[], uintptr_t _data, void *userdata)
TABLE_STRING, boot_entry_token_type_to_string(c.entry_token_type),
TABLE_FIELD, "Entry Token",
TABLE_STRING, c.entry_token,
TABLE_FIELD, "Entry Name Format",
TABLE_STRING, c.entry_name_format ?: DEFAULT_ENTRY_NAME_FORMAT,
TABLE_FIELD, "Entry Name",
TABLE_STRING, c.entry_name,
TABLE_FIELD, "Entry Directory",
TABLE_STRING, c.entry_dir,
TABLE_FIELD, "Kernel Version",
+97 -1
View File
@@ -294,6 +294,99 @@ test ! -e "$BOOT_ROOT/hoge/1.1.1"
test -d "$BOOT_ROOT/hoge"
rmdir "$BOOT_ROOT/hoge"
###########################################
# tests for entry_name_format=
###########################################
# Test inspect with custom entry_name_format
cat >>"$D/sources/install.conf" <<EOF
entry_name_format=%e_%v
EOF
output="$("$kernel_install" -v --json=pretty inspect 1.1.1 "$D/sources/linux")"
echo "$output" | grep -E '"EntryNameFormat" : "%e_%v"' >/dev/null
echo "$output" | grep -E '"EntryName" : "the-token_1.1.1"' >/dev/null
# Test type#1 BLS entry with custom entry_name_format
rm -f "$D/sources/tries"
cat >"$D/sources/install.conf" <<EOF
layout=bls
initrd_generator=none
entry_name_format=%e_%v
EOF
KERNEL_INSTALL_PLUGINS="'${loaderentry_install}' '${uki_copy_install}'" \
"$kernel_install" -v add 1.1.4 "$D/sources/linux" "$D/sources/initrd"
entry="$BOOT_ROOT/loader/entries/the-token_1.1.4.conf"
test -f "$entry"
grep -qE '^version +1.1.4' "$entry"
KERNEL_INSTALL_PLUGINS="'${loaderentry_install}' '${uki_copy_install}'" \
"$kernel_install" -v remove 1.1.4
test ! -e "$entry"
rm -rf "$BOOT_ROOT/the-token"
# Test UKI with custom entry_name_format
if [ -f "$ukify" ]; then
cat >"$D/sources/install.conf" <<EOF
layout=uki
uki_generator=ukify
initrd_generator=none
entry_name_format=%e_%v
EOF
rm -f "$D/sources/tries"
KERNEL_INSTALL_PLUGINS="'${ukify_install}' '${loaderentry_install}' '${uki_copy_install}'" \
"$kernel_install" -v add 1.1.5 "$D/sources/linux" "$D/sources/initrd"
uki="${BOOT_ROOT}/EFI/Linux/the-token_1.1.5.efi"
test -f "$uki"
KERNEL_INSTALL_PLUGINS="'${ukify_install}' '${loaderentry_install}' '${uki_copy_install}'" \
"$kernel_install" -v remove 1.1.5
test ! -e "$uki"
# Test boot counting with custom entry_name_format
echo '56' >"$D/sources/tries"
KERNEL_INSTALL_PLUGINS="'${ukify_install}' '${loaderentry_install}' '${uki_copy_install}'" \
"$kernel_install" -v add 1.1.5 "$D/sources/linux" "$D/sources/initrd"
uki="${BOOT_ROOT}/EFI/Linux/the-token_1.1.5+56.efi"
test -f "$uki"
KERNEL_INSTALL_PLUGINS="'${ukify_install}' '${loaderentry_install}' '${uki_copy_install}'" \
"$kernel_install" -v remove 1.1.5
test ! -e "$uki"
rm -f "$D/sources/tries"
fi
# Test add-all with custom entry_name_format (exercises context_copy)
KVER="$(uname -r)"
if test -f "/usr/lib/modules/$KVER/vmlinuz"; then
rm -rf "$BOOT_ROOT"
mkdir -p "$BOOT_ROOT"
cat >"$D/sources/install.conf" <<EOF
layout=bls
initrd_generator=none
entry_name_format=%e_%v
EOF
KERNEL_INSTALL_PLUGINS="'${loaderentry_install}' '${uki_copy_install}'" \
"$kernel_install" -v add-all
entry="$BOOT_ROOT/loader/entries/the-token_${KVER}.conf"
test -f "$entry"
grep -qE "^version +${KVER}" "$entry"
KERNEL_INSTALL_PLUGINS="'${loaderentry_install}' '${uki_copy_install}'" \
"$kernel_install" -v remove "$KVER"
test ! -e "$entry"
rm -rf "$BOOT_ROOT/the-token"
fi
# Restore install.conf and plugins
cat >"$D/sources/install.conf" <<EOF
initrd_generator=none
# those are overridden by envvars
BOOT_ROOT="$D/badboot"
MACHINE_ID=badbadbadbadbadbad6abadbadbadbad
EOF
export KERNEL_INSTALL_PLUGINS="$D/00-skip.install"
###########################################
# tests for --json=
###########################################
@@ -307,6 +400,8 @@ diff -u <(echo "$output") - >&2 <<EOF
"BootRoot" : "$BOOT_ROOT",
"EntryTokenType" : "literal",
"EntryToken" : "the-token",
"EntryNameFormat" : "%e-%v",
"EntryName" : "the-token-1.1.1",
"EntryDirectory" : "$BOOT_ROOT/the-token/1.1.1",
"KernelVersion" : "1.1.1",
"Kernel" : "$D/sources/linux",
@@ -326,7 +421,8 @@ diff -u <(echo "$output") - >&2 <<EOF
"KERNEL_INSTALL_LAYOUT=other",
"KERNEL_INSTALL_INITRD_GENERATOR=none",
"KERNEL_INSTALL_UKI_GENERATOR=",
"KERNEL_INSTALL_STAGING_AREA=${TMPDIR:-/var/tmp}/kernel-install.staging.XXXXXX"
"KERNEL_INSTALL_STAGING_AREA=${TMPDIR:-/var/tmp}/kernel-install.staging.XXXXXX",
"KERNEL_INSTALL_ENTRY_NAME=the-token-1.1.1"
]
}
EOF
+12 -7
View File
@@ -14,16 +14,19 @@ int load_kernel_install_conf_at(
char **ret_boot_root,
char **ret_layout,
char **ret_initrd_generator,
char **ret_uki_generator) {
char **ret_uki_generator,
char **ret_entry_name_format) {
_cleanup_free_ char *machine_id = NULL, *boot_root = NULL, *layout = NULL,
*initrd_generator = NULL, *uki_generator = NULL;
*initrd_generator = NULL, *uki_generator = NULL,
*entry_name_format = NULL;
const ConfigTableItem items[] = {
{ NULL, "MACHINE_ID", config_parse_string, 0, &machine_id },
{ NULL, "BOOT_ROOT", config_parse_string, 0, &boot_root },
{ NULL, "layout", config_parse_string, 0, &layout },
{ NULL, "initrd_generator", config_parse_string, 0, &initrd_generator },
{ NULL, "uki_generator", config_parse_string, 0, &uki_generator },
{ NULL, "MACHINE_ID", config_parse_string, 0, &machine_id },
{ NULL, "BOOT_ROOT", config_parse_string, 0, &boot_root },
{ NULL, "layout", config_parse_string, 0, &layout },
{ NULL, "initrd_generator", config_parse_string, 0, &initrd_generator },
{ NULL, "uki_generator", config_parse_string, 0, &uki_generator },
{ NULL, "entry_name_format", config_parse_string, 0, &entry_name_format },
{}
};
int r;
@@ -71,5 +74,7 @@ int load_kernel_install_conf_at(
*ret_initrd_generator = TAKE_PTR(initrd_generator);
if (ret_uki_generator)
*ret_uki_generator = TAKE_PTR(uki_generator);
if (ret_entry_name_format)
*ret_entry_name_format = TAKE_PTR(entry_name_format);
return r >= 0; /* Return 0 if we got -ENOENT above, 1 otherwise. */
}
+5 -3
View File
@@ -12,7 +12,8 @@ int load_kernel_install_conf_at(
char **ret_boot_root,
char **ret_layout,
char **ret_initrd_generator,
char **ret_uki_generator);
char **ret_uki_generator,
char **ret_entry_name_format);
static inline int load_kernel_install_conf(
const char *root,
@@ -21,7 +22,8 @@ static inline int load_kernel_install_conf(
char **ret_boot_root,
char **ret_layout,
char **ret_initrd_generator,
char **ret_uki_generator) {
char **ret_uki_generator,
char **ret_entry_name_format) {
return load_kernel_install_conf_at(root, XAT_FDROOT, conf_root, ret_machine_id, ret_boot_root, ret_layout, ret_initrd_generator, ret_uki_generator);
return load_kernel_install_conf_at(root, XAT_FDROOT, conf_root, ret_machine_id, ret_boot_root, ret_layout, ret_initrd_generator, ret_uki_generator, ret_entry_name_format);
}