Skip to content

Commit 8275daa

Browse files
krissedoutjeckersb
authored andcommitted
composefs: Use an XBOOTLDR partition with systemd-boot
The Boot Loader Specification lets systemd-boot read Type #1 entries and UKIs from an Extended Boot Loader Partition (XBOOTLDR) on the ESP's disk. The composefs backend always wrote them to the ESP, which doesn't work when the ESP is small. That's the usual case next to Windows: its ESP is often 100 MB, a UKI with a full initramfs can be bigger than that, and composefs keeps the booted, staged and rollback ones side by side. For bootloaders that read entries the way BLS describes, which for now is only systemd-boot, install the UKIs, BLS entries and their kernels and initramfs images to a VFAT XBOOTLDR partition next to the ESP when there is one. The bootloader and loader.conf, which systemd-boot only reads from the ESP, stay there. When booted, BootedStorage, which upgrades, rollback, finalize and GC go through, uses the XBOOTLDR partition next to the ESP named by LoaderDevicePartUUID only if it holds the booted deployment's entries, and the ESP otherwise. That way systems installed by an older bootc, or before an XBOOTLDR partition was added, keep their entries on the ESP. GRUB keeps its own /boot handling, and grub-cc is left alone since it's unclear whether it reads XBOOTLDR. The new tmt test splits the VM's ESP in half with an empty XBOOTLDR partition, and checks that a switch keeps the entries on the ESP. It then moves them to the XBOOTLDR partition, as an install there would have put them, and upgrades and rolls back. Sealed runs skip it, since the derived images are built in the VM without keys to sign them. Assisted-by: AI Signed-off-by: Kristof Imeri <kristof@lanth.me>
1 parent 12400f7 commit 8275daa

19 files changed

Lines changed: 622 additions & 92 deletions

‎crates/lib/src/bootc_composefs/backwards_compat/bcompat_boot.rs‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,11 @@ fn handle_bls_conf(
290290
create_staged_bls_entries(boot_dir, &new_bls_entries)?;
291291

292292
let binaries_dir = if is_uki {
293-
let esp = storage.require_esp()?;
294-
let uki_dir = esp.fd.open_dir(BOOTC_UKI_DIR).context("Opening UKI dir")?;
293+
let boot_mount = storage.require_boot_mount()?;
294+
let uki_dir = boot_mount
295+
.fd
296+
.open_dir(BOOTC_UKI_DIR)
297+
.context("Opening UKI dir")?;
295298

296299
uki_dir
297300
} else {
@@ -329,7 +332,7 @@ pub(crate) async fn prepend_custom_prefix(
329332

330333
BootType::Uki => match bootloader.kind()? {
331334
BootloaderKind::GRUBClassic => {
332-
let esp = storage.require_esp()?;
335+
let boot_mount = storage.require_boot_mount()?;
333336

334337
let mut buf = String::new();
335338
let menuentries = get_sorted_grub_uki_boot_entries(boot_dir, &mut buf)?;
@@ -345,7 +348,7 @@ pub(crate) async fn prepend_custom_prefix(
345348
continue;
346349
}
347350

348-
plan_efi_binary_renames(&esp.fd, &digest, &mut rename_transaction)?;
351+
plan_efi_binary_renames(&boot_mount.fd, &digest, &mut rename_transaction)?;
349352

350353
let new_path = Utf8PathBuf::from("/")
351354
.join(BOOTC_UKI_DIR)
@@ -376,8 +379,11 @@ pub(crate) async fn prepend_custom_prefix(
376379
})
377380
.with_context(|| format!("Writing to {USER_CFG_STAGED}"))?;
378381

379-
let esp = storage.require_esp()?;
380-
let uki_dir = esp.fd.open_dir(BOOTC_UKI_DIR).context("Opening UKI dir")?;
382+
let boot_mount = storage.require_boot_mount()?;
383+
let uki_dir = boot_mount
384+
.fd
385+
.open_dir(BOOTC_UKI_DIR)
386+
.context("Opening UKI dir")?;
381387

382388
// execute all EFI PE renames atomically before the final exchange
383389
rename_transaction

‎crates/lib/src/bootc_composefs/boot.rs‎

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ use rustix::{mount::MountFlags, path::Arg};
102102
use schemars::JsonSchema;
103103
use serde::{Deserialize, Serialize};
104104

105+
use crate::bootc_composefs::boot_partition::BootPartition;
105106
use crate::bootc_composefs::state::{get_booted_bls, write_composefs_state};
106107
use crate::bootc_composefs::status::build_composefs_karg;
107108
use crate::bootc_composefs::uki_addon::{UkiAddonType, UkiAddonsList, list_installed_uki_addons};
@@ -111,7 +112,6 @@ use crate::composefs_consts::{
111112
};
112113
use crate::parsers::bls_config::{BLSConfig, BLSConfigType, EFIKey};
113114
use crate::spec::BootloaderKind;
114-
use crate::store::find_booted_from_esp;
115115
use crate::task::Task;
116116
use crate::{
117117
bootc_composefs::repo::open_composefs_repo,
@@ -829,7 +829,7 @@ pub(crate) fn setup_composefs_bls_boot(
829829
) -> Result<String> {
830830
let id_hex = id.to_hex();
831831

832-
let (esp_device, mut cmdline_refs, bootloader) = match setup_type {
832+
let (boot_partition, mut cmdline_refs, bootloader) = match setup_type {
833833
BootSetupType::Setup((root_setup, state, postfetch, allow_missing_fsverity)) => {
834834
// root_setup.kargs has [root=UUID=<UUID>, "rw"]
835835
let mut cmdline_options = Cmdline::new();
@@ -869,7 +869,7 @@ pub(crate) fn setup_composefs_bls_boot(
869869
let esp_part = root_setup.device_info.find_first_colocated_esp()?;
870870

871871
(
872-
esp_part.path(),
872+
BootPartition::for_install(esp_part.path().into(), postfetch.detected_bootloader)?,
873873
cmdline_options,
874874
postfetch.detected_bootloader.clone(),
875875
)
@@ -902,7 +902,7 @@ pub(crate) fn setup_composefs_bls_boot(
902902
)?;
903903

904904
(
905-
find_booted_from_esp(&storage.physical_root)?,
905+
storage.require_boot_partition()?.clone(),
906906
cmdline,
907907
bootloader,
908908
)
@@ -938,7 +938,8 @@ pub(crate) fn setup_composefs_bls_boot(
938938
}
939939

940940
BootloaderKind::BLSCompatible => {
941-
let efi_mount = mount_esp_writable(&esp_device).context("Mounting ESP")?;
941+
let efi_mount = mount_esp_writable(boot_partition.device().as_str())
942+
.context("Mounting boot partition")?;
942943

943944
let mounted_efi = Utf8PathBuf::from(efi_mount.dir.path().as_str()?);
944945
let efi_linux_dir = mounted_efi.join(EFI_LINUX);
@@ -1782,7 +1783,7 @@ fn write_grub_uki_menuentry(
17821783
setup_type: &BootSetupType,
17831784
boot_label: String,
17841785
id: &Sha512HashValue,
1785-
esp_device: &String,
1786+
esp_device: &str,
17861787
) -> Result<()> {
17871788
let boot_dir = setup_type.grub_boot_path()?;
17881789
create_dir_all(&boot_dir).context("Failed to create boot dir")?;
@@ -1833,7 +1834,7 @@ fn write_grub_uki_menuentry(
18331834
// Open grub2/efiuuid.cfg and write the EFI partition fs-UUID in there
18341835
// This will be sourced by grub2/user.cfg to be used for `--fs-uuid`
18351836
let esp_uuid = Task::new("blkid for ESP UUID", "blkid")
1836-
.args(["-s", "UUID", "-o", "value", &esp_device])
1837+
.args(["-s", "UUID", "-o", "value", esp_device])
18371838
.read()?;
18381839

18391840
grub_dir.atomic_write(
@@ -1943,7 +1944,7 @@ pub(crate) fn setup_composefs_uki_boot(
19431944
entries: Vec<ComposefsBootEntry<Sha512HashValue>>,
19441945
) -> Result<(String, Sha512HashValue)> {
19451946
let (
1946-
esp_device,
1947+
boot_partition,
19471948
bootloader,
19481949
missing_fsverity_allowed,
19491950
uki_addons_via_cli,
@@ -1981,7 +1982,7 @@ pub(crate) fn setup_composefs_uki_boot(
19811982
}
19821983

19831984
(
1984-
esp_part.path(),
1985+
BootPartition::for_install(esp_part.path().into(), postfetch.detected_bootloader)?,
19851986
postfetch.detected_bootloader.clone(),
19861987
allow_missing_fsverity,
19871988
addons,
@@ -2034,7 +2035,7 @@ pub(crate) fn setup_composefs_uki_boot(
20342035
}
20352036

20362037
(
2037-
find_booted_from_esp(&storage.physical_root)?,
2038+
storage.require_boot_partition()?.clone(),
20382039
bootloader,
20392040
booted_cfs.cmdline.allow_missing_fsverity,
20402041
addons_via_cli,
@@ -2078,7 +2079,8 @@ pub(crate) fn setup_composefs_uki_boot(
20782079
}
20792080
}
20802081

2081-
let esp_mount = mount_esp_writable(&esp_device).context("Mounting ESP")?;
2082+
let boot_mount =
2083+
mount_esp_writable(boot_partition.device().as_str()).context("Mounting boot partition")?;
20822084

20832085
let mut uki_info = UKIInfo {
20842086
boot_label: "".into(),
@@ -2189,20 +2191,23 @@ pub(crate) fn setup_composefs_uki_boot(
21892191

21902192
// Create a `ESP/loader/addons.staged` if we have Global UKI Addons to update
21912193
if is_upgrade {
2192-
stage_global_uki_addons(&esp_mount.fd)?;
2194+
stage_global_uki_addons(&boot_mount.fd)?;
21932195
}
21942196

21952197
for entry in entries_to_write {
2196-
write_pe_to_esp(repo, &entry, &deploy_id, esp_mount.dir.path(), is_upgrade)?;
2198+
write_pe_to_esp(repo, &entry, &deploy_id, boot_mount.dir.path(), is_upgrade)?;
21972199
}
21982200

21992201
match bootloader.kind()? {
2200-
BootloaderKind::GRUBClassic => {
2201-
write_grub_uki_menuentry(&setup_type, boot_label, &deploy_id, &esp_device)?
2202-
}
2202+
BootloaderKind::GRUBClassic => write_grub_uki_menuentry(
2203+
&setup_type,
2204+
boot_label,
2205+
&deploy_id,
2206+
boot_partition.device().as_str(),
2207+
)?,
22032208

22042209
BootloaderKind::BLSCompatible => write_systemd_uki_config(
2205-
&esp_mount.fd,
2210+
&boot_mount.fd,
22062211
&setup_type,
22072212
boot_label,
22082213
version,
@@ -2221,8 +2226,7 @@ pub(crate) fn setup_composefs_uki_boot(
22212226
/// The composefs image (a detached `fsmount(2)` fd with no VFS path) is
22222227
/// attached to a tmpdir via `move_mount(2)`, giving us a real filesystem path
22232228
/// that `mount(2)` and bootctl can use. The ESP is mounted at
2224-
/// `<tmpdir>/efi` (if that directory exists in the image) or `<tmpdir>/boot`,
2225-
/// per the Boot Loader Specification. A tmpfs is also mounted at
2229+
/// `<tmpdir>/boot`. A tmpfs is also mounted at
22262230
/// `<tmpdir>/tmp` to provide a writable scratch area for tools invoked with
22272231
/// `--root`.
22282232
///
@@ -2277,10 +2281,11 @@ impl MountedImageRoot {
22772281
let composefs = TempMount::mount_fd(composefs_mnt_fd)
22782282
.context("Attaching composefs image to temporary directory")?;
22792283

2280-
// TODO: support XBOOTLDR. Per BLS, the ESP should be mounted at /efi
2281-
// when a separate XBOOTLDR partition is present at /boot. bootc does
2282-
// not yet detect or use XBOOTLDR in the composefs install path, so
2283-
// unconditionally mount the ESP at /boot for now.
2284+
// The ESP goes on /boot even when there's an XBOOTLDR partition,
2285+
// where BLS would mount it on /efi: that would need an /efi
2286+
// directory in the image, and only the bootloader is installed
2287+
// through this mount, with the ESP's path passed explicitly. bootc
2288+
// writes the boot entries itself (see `BootPartition`).
22842289
let esp_subdir = "boot";
22852290

22862291
// Mount a tmpfs over /tmp so that tools invoked with --root have a
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
//! Selecting the partition that holds the composefs boot entries.
2+
//!
3+
//! Per the Boot Loader Specification, boot entries go on the Extended Boot
4+
//! Loader Partition (XBOOTLDR) on the ESP's disk if there is one, and on the
5+
//! ESP otherwise. bootc follows that for bootloaders that read entries that
6+
//! way (see [`Bootloader::is_bls_conformant`]), with one exception: systems
7+
//! installed by a bootc that didn't use XBOOTLDR partitions keep their entries
8+
//! on the ESP, so a booted system keeps using the partition holding the booted
9+
//! deployment's entries.
10+
11+
use anyhow::Result;
12+
use bootc_mount::tempmount::TempMount;
13+
use camino::{Utf8Path, Utf8PathBuf};
14+
use cap_std_ext::cap_std::fs::Dir;
15+
use cap_std_ext::dirext::CapStdExtDirExt;
16+
17+
use crate::composefs_consts::{BLS_ENTRY_FILE_PREFIX, TYPE1_ENT_PATH, TYPE1_ENT_PATH_STAGED};
18+
use crate::parsers::bls_config::parse_bls_config;
19+
use crate::spec::Bootloader;
20+
21+
/// The partition holding UKIs, Type #1 entries and their kernels and
22+
/// initramfs images.
23+
#[derive(Debug, Clone)]
24+
pub(crate) enum BootPartition {
25+
/// The EFI System Partition
26+
Esp(Utf8PathBuf),
27+
/// An Extended Boot Loader Partition on the ESP's disk. It holds UKIs
28+
/// that don't fit on a small ESP, such as one shared with Windows.
29+
Xbootldr(Utf8PathBuf),
30+
}
31+
32+
impl BootPartition {
33+
/// The partition to install entries to, given the ESP at `esp`: the VFAT
34+
/// XBOOTLDR partition next to it if `bootloader` reads one and there is
35+
/// one, and the ESP otherwise.
36+
pub(crate) fn for_install(esp: Utf8PathBuf, bootloader: Bootloader) -> Result<Self> {
37+
Ok(match sibling_xbootldr(&esp, bootloader)? {
38+
Some(xbootldr) => Self::Xbootldr(xbootldr),
39+
None => Self::Esp(esp),
40+
})
41+
}
42+
43+
/// Find and mount, with `mount`, the partition holding the entries of
44+
/// the booted deployment `booted_verity`, given the ESP at `esp` it was
45+
/// booted from.
46+
pub(crate) fn mount_booted(
47+
esp: Utf8PathBuf,
48+
bootloader: Bootloader,
49+
booted_verity: &str,
50+
mount: impl Fn(&Utf8Path) -> Result<TempMount>,
51+
) -> Result<(Self, TempMount)> {
52+
if let Some(xbootldr) = sibling_xbootldr(&esp, bootloader)? {
53+
let mounted = mount(&xbootldr)?;
54+
if has_type1_entry_for(&mounted.fd, booted_verity)? {
55+
return Ok((Self::Xbootldr(xbootldr), mounted));
56+
}
57+
tracing::debug!(
58+
"The booted deployment has no entry on XBOOTLDR partition {xbootldr}, using {esp}"
59+
);
60+
}
61+
let mounted = mount(&esp)?;
62+
Ok((Self::Esp(esp), mounted))
63+
}
64+
65+
/// The partition's device path
66+
pub(crate) fn device(&self) -> &Utf8Path {
67+
match self {
68+
Self::Esp(device) | Self::Xbootldr(device) => device,
69+
}
70+
}
71+
}
72+
73+
fn sibling_xbootldr(esp: &Utf8Path, bootloader: Bootloader) -> Result<Option<Utf8PathBuf>> {
74+
if !bootloader.is_bls_conformant() {
75+
return Ok(None);
76+
}
77+
let xbootldr = bootc_blockdev::list_dev(esp)?.find_sibling_xbootldr()?;
78+
Ok(xbootldr.map(|xbootldr| {
79+
tracing::debug!("Found XBOOTLDR partition {} next to {esp}", xbootldr.path());
80+
Utf8PathBuf::from(xbootldr.path())
81+
}))
82+
}
83+
84+
/// Whether `boot_dir` has one of our Type #1 entries, staged or not, for the
85+
/// deployment `verity`.
86+
fn has_type1_entry_for(boot_dir: &Dir, verity: &str) -> Result<bool> {
87+
for path in [TYPE1_ENT_PATH, TYPE1_ENT_PATH_STAGED] {
88+
let Some(entries) = boot_dir.open_dir_optional(path)? else {
89+
continue;
90+
};
91+
for entry in entries.entries_utf8()? {
92+
let name = entry?.file_name()?;
93+
if !(name.starts_with(BLS_ENTRY_FILE_PREFIX) && name.ends_with(".conf")) {
94+
continue;
95+
}
96+
let entry = parse_bls_config(&entries.read_to_string(&name)?)?;
97+
if entry.get_verity().is_ok_and(|v| v == verity) {
98+
return Ok(true);
99+
}
100+
}
101+
}
102+
Ok(false)
103+
}
104+
105+
#[cfg(test)]
106+
mod tests {
107+
use super::*;
108+
use crate::testutils::{ChangeType, TestRoot, fake_digest_version};
109+
110+
#[test]
111+
fn test_has_type1_entry_for() -> Result<()> {
112+
let mut root = TestRoot::new()?;
113+
let installed = root.current().verity.clone();
114+
root.upgrade(1, ChangeType::Kernel)?;
115+
let upgraded = root.current().verity.clone();
116+
let boot_dir = root.boot_dir()?;
117+
118+
assert!(has_type1_entry_for(&boot_dir, &installed)?);
119+
assert!(has_type1_entry_for(&boot_dir, &upgraded)?);
120+
assert!(!has_type1_entry_for(&boot_dir, &fake_digest_version(2))?);
121+
Ok(())
122+
}
123+
124+
#[test]
125+
fn test_has_type1_entry_for_staged() -> Result<()> {
126+
let root = TestRoot::new()?;
127+
let verity = root.current().verity.clone();
128+
let boot_dir = root.boot_dir()?;
129+
boot_dir.rename(TYPE1_ENT_PATH, &boot_dir, TYPE1_ENT_PATH_STAGED)?;
130+
131+
assert!(has_type1_entry_for(&boot_dir, &verity)?);
132+
Ok(())
133+
}
134+
135+
#[test]
136+
fn test_has_type1_entry_for_other_os() -> Result<()> {
137+
let dir = cap_std_ext::cap_tempfile::tempdir(cap_std_ext::cap_std::ambient_authority())?;
138+
let verity = fake_digest_version(0);
139+
assert!(!has_type1_entry_for(&dir, &verity)?);
140+
141+
dir.create_dir_all(TYPE1_ENT_PATH)?;
142+
dir.atomic_write(
143+
format!("{TYPE1_ENT_PATH}/other-os.conf"),
144+
"title Other\nlinux /vmlinuz\noptions root=UUID=other rw\n",
145+
)?;
146+
assert!(!has_type1_entry_for(&dir, &verity)?);
147+
Ok(())
148+
}
149+
}

‎crates/lib/src/bootc_composefs/delete.rs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,10 @@ pub(crate) async fn delete_composefs_deployment(
272272
delete_depl_boot_entries(&depl_to_del, &storage, deleting_staged)?;
273273

274274
if deleting_staged {
275-
let esp = storage.require_esp()?;
275+
let boot_mount = storage.require_boot_mount()?;
276276

277-
esp.fd
277+
boot_mount
278+
.fd
278279
.remove_all_optional(STAGED_GLOBAL_UKI_ADDONS_DIR)
279280
.context("Removing staged Global UKI Addons")?;
280281
}

0 commit comments

Comments
 (0)