Skip to content

Conversation

@Johan-Liebert1
Copy link
Collaborator

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

@bootc-bot bootc-bot bot requested a review from jmarrero November 18, 2025 08:34
@Johan-Liebert1
Copy link
Collaborator Author

I'm also working on adding tests for these cases

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request fixes the boot order for sd-boot during an update by setting the correct sort-key for BLS entries. The change correctly handles the different sorting behaviors of Grub (descending) and systemd-boot (ascending). The logic is sound. I've added one suggestion to refactor duplicated logic to improve code maintainability.

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: bootc-dev#1777

Signed-off-by: Pragyan Poudyal <pragyanpoudyal41999@gmail.com>
.context("Computing boot digest")?;

let default_sort_key = "1";
let default_sort_key = bootloader.default_sort_key();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I think it'd be cleaner if computed all the BLS entries without writing them, and then walk over all of them and reverse the order if we detect grub or so?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is kind of what we're doing, but in a bit of a weird way. This might be good for a cleanup. I'll pick it up

pub(crate) const GRUB_SECONDARY_SORT_KEY: &str = "0";

/// Sort key for the primary BLS entry
pub(crate) fn default_sort_key(&self) -> &str {
Copy link
Collaborator

@cgwalters cgwalters Nov 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about

sorting_keys(&self) -> impl Iterator<Item=&'static str> {
  let r = ["0", "1"]
  match self {
    Systemd => r
    Grub => r.rev()
  }

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use the secondary keys when upgrading/switching, so this doesn't quite work for that case

@cgwalters
Copy link
Collaborator

cgwalters commented Nov 18, 2025

OK first when talking about grub, there's a giant mess there because it has a wildly varying patch set per distro/OS. In particular https://src.fedoraproject.org/rpms/grub2 now has 390 (!) patches.

Anyways I think the real problem here is:

~/src/src.fedoraproject.org/rpms/grub2/grub2-2.12-build/grub-2.12> grep -r sort-key .
./docs/grub.texi:sort-key     fedora
~/src/src.fedoraproject.org/rpms/grub2/grub2-2.12-build/grub-2.12>

i.e. at least Fedora's grub2 isn't implementing sort-key at all AFAICS. I suspect we're getting sorting by filenames instead.

We should make this at least build or runtime configurable probably in the future, but for now until we gather more data on grub on other OSes it's likely simplest to just not emit sort-key at all with grub and ensure that we always do filename ordering.

Copy link
Collaborator

@cgwalters cgwalters left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above

@TGNThump
Copy link
Contributor

TGNThump commented Nov 18, 2025

From what I can see, this patch doesn't change the current behaviour for Grub, but fixes the currently broken behaviour for systemd-boot. Is it worth getting this in first and then dealing with Grub as a separate PR if more info is needed before figuring out the best solution for Grub?

@Johan-Liebert1
Copy link
Collaborator Author

I suspect we're getting sorting by filenames instead

That's what's happening, at least in fedora based images. The sort-key is present in the BLS Config even for grub is because we only have one write function for the BLSConfig struct which writes every field in the struct regardless of which bootloader is used

@cgwalters cgwalters added this to the 1.11.0 milestone Nov 19, 2025
@cgwalters
Copy link
Collaborator

From what I can see, this patch doesn't change the current behaviour for Grub, but fixes the currently broken behaviour for systemd-boot. Is it worth getting this in first and then dealing with Grub as a separate PR if more info is needed before figuring out the best solution for Grub?

Yes I think that's true, however I think it's well worth some further cleanups here. We'll get this fixed soon.

@cgwalters cgwalters self-assigned this Nov 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Missing default systemd-boot load entry (wrong boot order)

3 participants