Skip to content

Commit 27fcedb

Browse files
committed
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 db8f971 commit 27fcedb

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,
@@ -822,7 +822,7 @@ pub(crate) fn setup_composefs_bls_boot(
822822
) -> Result<String> {
823823
let id_hex = id.to_hex();
824824

825-
let (esp_device, mut cmdline_refs, bootloader) = match setup_type {
825+
let (boot_partition, mut cmdline_refs, bootloader) = match setup_type {
826826
BootSetupType::Setup((root_setup, state, postfetch, allow_missing_fsverity)) => {
827827
// root_setup.kargs has [root=UUID=<UUID>, "rw"]
828828
let mut cmdline_options = Cmdline::new();
@@ -862,7 +862,7 @@ pub(crate) fn setup_composefs_bls_boot(
862862
let esp_part = root_setup.device_info.find_first_colocated_esp()?;
863863

864864
(
865-
esp_part.path(),
865+
BootPartition::for_install(esp_part.path().into(), postfetch.detected_bootloader)?,
866866
cmdline_options,
867867
postfetch.detected_bootloader.clone(),
868868
)
@@ -895,7 +895,7 @@ pub(crate) fn setup_composefs_bls_boot(
895895
)?;
896896

897897
(
898-
find_booted_from_esp(&storage.physical_root)?,
898+
storage.require_boot_partition()?.clone(),
899899
cmdline,
900900
bootloader,
901901
)
@@ -931,7 +931,8 @@ pub(crate) fn setup_composefs_bls_boot(
931931
}
932932

933933
BootloaderKind::BLSCompatible => {
934-
let efi_mount = mount_esp_writable(&esp_device).context("Mounting ESP")?;
934+
let efi_mount = mount_esp_writable(boot_partition.device().as_str())
935+
.context("Mounting boot partition")?;
935936

936937
let mounted_efi = Utf8PathBuf::from(efi_mount.dir.path().as_str()?);
937938
let efi_linux_dir = mounted_efi.join(EFI_LINUX);
@@ -1775,7 +1776,7 @@ fn write_grub_uki_menuentry(
17751776
setup_type: &BootSetupType,
17761777
boot_label: String,
17771778
id: &Sha512HashValue,
1778-
esp_device: &String,
1779+
esp_device: &str,
17791780
) -> Result<()> {
17801781
let boot_dir = setup_type.grub_boot_path()?;
17811782
create_dir_all(&boot_dir).context("Failed to create boot dir")?;
@@ -1826,7 +1827,7 @@ fn write_grub_uki_menuentry(
18261827
// Open grub2/efiuuid.cfg and write the EFI partition fs-UUID in there
18271828
// This will be sourced by grub2/user.cfg to be used for `--fs-uuid`
18281829
let esp_uuid = Task::new("blkid for ESP UUID", "blkid")
1829-
.args(["-s", "UUID", "-o", "value", &esp_device])
1830+
.args(["-s", "UUID", "-o", "value", esp_device])
18301831
.read()?;
18311832

18321833
grub_dir.atomic_write(
@@ -1930,7 +1931,7 @@ pub(crate) fn setup_composefs_uki_boot(
19301931
entries: Vec<ComposefsBootEntry<Sha512HashValue>>,
19311932
) -> Result<(String, Sha512HashValue)> {
19321933
let (
1933-
esp_device,
1934+
boot_partition,
19341935
bootloader,
19351936
missing_fsverity_allowed,
19361937
uki_addons_via_cli,
@@ -1968,7 +1969,7 @@ pub(crate) fn setup_composefs_uki_boot(
19681969
}
19691970

19701971
(
1971-
esp_part.path(),
1972+
BootPartition::for_install(esp_part.path().into(), postfetch.detected_bootloader)?,
19721973
postfetch.detected_bootloader.clone(),
19731974
allow_missing_fsverity,
19741975
addons,
@@ -2021,7 +2022,7 @@ pub(crate) fn setup_composefs_uki_boot(
20212022
}
20222023

20232024
(
2024-
find_booted_from_esp(&storage.physical_root)?,
2025+
storage.require_boot_partition()?.clone(),
20252026
bootloader,
20262027
booted_cfs.cmdline.allow_missing_fsverity,
20272028
addons_via_cli,
@@ -2065,7 +2066,8 @@ pub(crate) fn setup_composefs_uki_boot(
20652066
}
20662067
}
20672068

2068-
let esp_mount = mount_esp_writable(&esp_device).context("Mounting ESP")?;
2069+
let boot_mount =
2070+
mount_esp_writable(boot_partition.device().as_str()).context("Mounting boot partition")?;
20692071

20702072
let mut uki_info = UKIInfo {
20712073
boot_label: "".into(),
@@ -2176,20 +2178,23 @@ pub(crate) fn setup_composefs_uki_boot(
21762178

21772179
// Create a `ESP/loader/addons.staged` if we have Global UKI Addons to update
21782180
if is_upgrade {
2179-
stage_global_uki_addons(&esp_mount.fd)?;
2181+
stage_global_uki_addons(&boot_mount.fd)?;
21802182
}
21812183

21822184
for entry in entries_to_write {
2183-
write_pe_to_esp(repo, &entry, &deploy_id, esp_mount.dir.path(), is_upgrade)?;
2185+
write_pe_to_esp(repo, &entry, &deploy_id, boot_mount.dir.path(), is_upgrade)?;
21842186
}
21852187

21862188
match bootloader.kind()? {
2187-
BootloaderKind::GRUBClassic => {
2188-
write_grub_uki_menuentry(&setup_type, boot_label, &deploy_id, &esp_device)?
2189-
}
2189+
BootloaderKind::GRUBClassic => write_grub_uki_menuentry(
2190+
&setup_type,
2191+
boot_label,
2192+
&deploy_id,
2193+
boot_partition.device().as_str(),
2194+
)?,
21902195

21912196
BootloaderKind::BLSCompatible => write_systemd_uki_config(
2192-
&esp_mount.fd,
2197+
&boot_mount.fd,
21932198
&setup_type,
21942199
boot_label,
21952200
version,
@@ -2208,8 +2213,7 @@ pub(crate) fn setup_composefs_uki_boot(
22082213
/// The composefs image (a detached `fsmount(2)` fd with no VFS path) is
22092214
/// attached to a tmpdir via `move_mount(2)`, giving us a real filesystem path
22102215
/// that `mount(2)` and bootctl can use. The ESP is mounted at
2211-
/// `<tmpdir>/efi` (if that directory exists in the image) or `<tmpdir>/boot`,
2212-
/// per the Boot Loader Specification. A tmpfs is also mounted at
2216+
/// `<tmpdir>/boot`. A tmpfs is also mounted at
22132217
/// `<tmpdir>/tmp` to provide a writable scratch area for tools invoked with
22142218
/// `--root`.
22152219
///
@@ -2264,10 +2268,11 @@ impl MountedImageRoot {
22642268
let composefs = TempMount::mount_fd(composefs_mnt_fd)
22652269
.context("Attaching composefs image to temporary directory")?;
22662270

2267-
// TODO: support XBOOTLDR. Per BLS, the ESP should be mounted at /efi
2268-
// when a separate XBOOTLDR partition is present at /boot. bootc does
2269-
// not yet detect or use XBOOTLDR in the composefs install path, so
2270-
// unconditionally mount the ESP at /boot for now.
2271+
// The ESP goes on /boot even when there's an XBOOTLDR partition,
2272+
// where BLS would mount it on /efi: that would need an /efi
2273+
// directory in the image, and only the bootloader is installed
2274+
// through this mount, with the ESP's path passed explicitly. bootc
2275+
// writes the boot entries itself (see `BootPartition`).
22712276
let esp_subdir = "boot";
22722277

22732278
// 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)