Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions crates/lib/src/bootc_composefs/boot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,13 @@ pub(crate) fn secondary_sort_key(os_id: &str) -> String {
format!("bootc-{os_id}-{SORTKEY_PRIORITY_SECONDARY}")
}

/// Returns the os_id in a sort key from [`primary_sort_key`] or
/// [`secondary_sort_key`], or `None` if it isn't in their format.
pub(crate) fn os_id_from_sort_key(sort_key: &str) -> Option<&str> {
let (os_id, _priority) = sort_key.strip_prefix("bootc-")?.rsplit_once('-')?;
Some(os_id)
}

/// Returns the name of the directory where we store Type1 boot entries
pub(crate) fn get_type1_dir_name(depl_verity: &str) -> String {
format!("{TYPE1_BOOT_DIR_PREFIX}{depl_verity}")
Expand Down Expand Up @@ -3333,6 +3340,21 @@ mod tests {
assert_eq!(parts[2], "bootc_fedora_coreos"); // name
}

#[test]
fn test_os_id_from_sort_key() {
for (sort_key, expected) in [
(primary_sort_key("fedora"), Some("fedora")),
(secondary_sort_key("fedora"), Some("fedora")),
(primary_sort_key("fedora-coreos"), Some("fedora-coreos")),
(secondary_sort_key("bootc"), Some("bootc")),
("1".to_owned(), None),
("bootc".to_owned(), None),
("other-fedora-0".to_owned(), None),
] {
assert_eq!(os_id_from_sort_key(&sort_key), expected, "{sort_key}");
}
}

#[test]
fn test_sort_keys() {
// Test sort-key generation for systemd-boot
Expand Down
14 changes: 10 additions & 4 deletions crates/lib/src/bootc_composefs/rollback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use rustix::fs::{AtFlags, RenameFlags, fsync, renameat_with};
use crate::bootc_composefs::aboot;
use crate::bootc_composefs::boot::{
BootType, FILENAME_PRIORITY_PRIMARY, FILENAME_PRIORITY_SECONDARY, STAGED_GLOBAL_UKI_ADDONS_DIR,
primary_sort_key, secondary_sort_key, type1_entry_conf_file_name,
os_id_from_sort_key, primary_sort_key, secondary_sort_key, type1_entry_conf_file_name,
};
use crate::bootc_composefs::status::{get_composefs_status, get_sorted_type1_boot_entries};
use crate::composefs_consts::{
Expand Down Expand Up @@ -136,10 +136,16 @@ fn rollback_composefs_entries(host: &Host, boot_dir: &Dir, bootloader: Bootloade
// TODO(Johan-Liebert): Currently assuming there are only two deployments
assert!(all_configs.len() == 2);

// Keep the os_id the entries were written with, so rolling back only swaps them.
// Use "bootc" as default os_id for entries with a sort-key we didn't write.
let os_id = all_configs
.iter()
.find_map(|cfg| os_id_from_sort_key(cfg.sort_key.as_deref()?))
.unwrap_or("bootc")
.to_owned();
let os_id = os_id.as_str();

// For rollback: previous gets primary sort-key, booted gets secondary sort-key
// Use "bootc" as default os_id for rollback scenarios
// TODO: Extract actual os_id from deployment
let os_id = "bootc";

// This is the currently booted deployment - it should become secondary
// OR if rollback was queued, it would become primary
Expand Down
26 changes: 26 additions & 0 deletions tmt/tests/booted/test-rollback.nu
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ journalctl --list-boots

let st = bootc status --json | from json
let booted = $st.status.booted.image
let is_composefs = (tap is_composefs)

def imgsrc [] {
$env.BOOTC_upgrade_image? | default "localhost/bootc-derived-local"
Expand All @@ -32,6 +33,29 @@ def assert_rollback_queued [expected: bool] {
assert equal $queued $expected
}

# The name each boot entry starts with and its sort-key, which a rollback keeps
# while swapping the versions they belong to. Only systemd-boot orders entries
# by sort-key.
def boot_entries [] {
if not $is_composefs or (($st.status.booted.composefs.bootloader | str downcase) != "systemd") {
return []
}

if not ("/var/tmp/efi/loader" | path exists) {
mkdir /var/tmp/efi
mount /dev/disk/by-partlabel/EFI-SYSTEM /var/tmp/efi
}

glob /var/tmp/efi/loader/entries/*.conf
| each { |f|
{
name: ($f | path basename | split row "-" | first),
sort_key: (open $f | lines | parse "sort-key {key}" | get 0.key),
}
}
| sort-by sort_key
}

# Run on the first boot - capture initial state and switch to new image
def initial_switch [] {
tap begin "bootc rollback test"
Expand Down Expand Up @@ -81,7 +105,9 @@ def second_boot_rollback [] {
assert_rollback_queued false

print "Performing bootc rollback..."
let entries = (boot_entries)
bootc rollback
assert equal (boot_entries) $entries

# ...and a real queued rollback must be reported as one
assert_rollback_queued true
Expand Down
Loading