From ad86fc8b3482e1e430f2f13d4d219f578740114e Mon Sep 17 00:00:00 2001 From: Pragyan Poudyal Date: Tue, 18 Nov 2025 11:55:20 +0530 Subject: [PATCH] composefs/boot: Fix sd-boot order on update Grub sorts its BLS config in descending order, while sd-boot sorts the configs in ascending order. While upgrading we were always setting the new sort key to be `1` which would work for Grub but not for sd-boot. Fixes: #1777 Signed-off-by: Pragyan Poudyal --- crates/lib/src/bootc_composefs/boot.rs | 7 ++++--- crates/lib/src/spec.rs | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/crates/lib/src/bootc_composefs/boot.rs b/crates/lib/src/bootc_composefs/boot.rs index fd4c71981..b0a101675 100644 --- a/crates/lib/src/bootc_composefs/boot.rs +++ b/crates/lib/src/bootc_composefs/boot.rs @@ -474,7 +474,8 @@ pub(crate) fn setup_composefs_bls_boot( let boot_digest = compute_boot_digest(usr_lib_modules_vmlinuz, &repo) .context("Computing boot digest")?; - let default_sort_key = "1"; + let default_sort_key = bootloader.default_sort_key(); + let default_title_version = (id.to_hex(), default_sort_key.to_string()); let osrel_res = osrel_title_and_version(fs, &repo)?; @@ -540,7 +541,7 @@ pub(crate) fn setup_composefs_bls_boot( let boot_dir = Dir::open_ambient_dir(&entry_paths.config_path, ambient_authority())?; let mut booted_bls = get_booted_bls(&boot_dir)?; - booted_bls.sort_key = Some("0".into()); // entries are sorted by their filename in reverse order + booted_bls.sort_key = Some(bootloader.secondary_sort_key().into()); // This will be atomically renamed to 'loader/entries' on shutdown/reboot ( @@ -788,7 +789,7 @@ fn write_systemd_uki_config( boot_label: UKILabels, id: &Sha512HashValue, ) -> Result<()> { - let default_sort_key = "0"; + let default_sort_key = Bootloader::SYSTEMD_PRIMARY_SORT_KEY; let mut bls_conf = BLSConfig::default(); bls_conf diff --git a/crates/lib/src/spec.rs b/crates/lib/src/spec.rs index 83fd0b3ab..43752bd16 100644 --- a/crates/lib/src/spec.rs +++ b/crates/lib/src/spec.rs @@ -197,6 +197,31 @@ impl FromStr for Bootloader { } } +impl Bootloader { + pub(crate) const SYSTEMD_PRIMARY_SORT_KEY: &str = "0"; + // Grub entries are sorted by their filename in reverse order + pub(crate) const GRUB_PRIMARY_SORT_KEY: &str = "1"; + + pub(crate) const SYSTEMD_SECONDARY_SORT_KEY: &str = "1"; + pub(crate) const GRUB_SECONDARY_SORT_KEY: &str = "0"; + + /// Sort key for the primary BLS entry + pub(crate) fn default_sort_key(&self) -> &str { + match self { + Bootloader::Grub => Self::GRUB_PRIMARY_SORT_KEY, + Bootloader::Systemd => Self::SYSTEMD_PRIMARY_SORT_KEY, + } + } + + /// Sort key for the secondary BLS entry + pub(crate) fn secondary_sort_key(&self) -> &str { + match self { + Bootloader::Grub => Self::GRUB_SECONDARY_SORT_KEY, + Bootloader::Systemd => Self::SYSTEMD_SECONDARY_SORT_KEY, + } + } +} + /// A bootable entry #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, JsonSchema)] #[serde(rename_all = "camelCase")]