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
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/blockdev/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ bootc-mount = { package = "bootc-internal-mount", path = "../mount", version = "

# Workspace dependencies
anyhow = { workspace = true }
cfg-if = { workspace = true }
camino = { workspace = true, features = ["serde1"] }
cap-std-ext = { workspace = true, features = ["fs_utf8"] }
fn-error-context = { workspace = true }
Expand All @@ -26,6 +27,7 @@ tracing = { workspace = true }

[dev-dependencies]
indoc = { workspace = true }
regex = { workspace = true }

[lib]
path = "src/blockdev.rs"
57 changes: 51 additions & 6 deletions crates/blockdev/src/blockdev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ use serde::Deserialize;

use bootc_utils::CommandRunExt;

pub mod dps;
use dps::{BIOS_BOOT, ESP, XBOOTLDR};

/// Check whether the udev database is accessible (cached for the process lifetime).
///
/// When running inside a container or sandbox without `/run/udev`
Expand Down Expand Up @@ -76,12 +79,6 @@ fn blkid_probe(dev: &str) -> Result<HashMap<String, String>> {
/// Refer to <https://en.wikipedia.org/wiki/Partition_type>
pub const ESP_ID_MBR: &[u8] = &[0x06, 0xEF];

/// EFI System Partition (ESP) for UEFI boot on GPT
pub const ESP: &str = "c12a7328-f81f-11d2-ba4b-00a0c93ec93b";

/// BIOS boot partition type GUID for GPT
pub const BIOS_BOOT: &str = "21686148-6449-6e6f-744e-656564454649";

#[derive(Debug, Deserialize)]
struct DevicesOutput {
blockdevices: Vec<Device>,
Expand Down Expand Up @@ -240,6 +237,27 @@ impl Device {
})
}

/// Find a VFAT Extended Boot Loader Partition (XBOOTLDR) among children.
///
/// Other filesystems are skipped: an XBOOTLDR partition holding UKIs and
/// boot entries for systemd-boot must be readable by the firmware.
pub fn find_partition_of_xbootldr(&self) -> Option<&Device> {
self.find_partition_of_type(XBOOTLDR)
.filter(|part| part.fstype.as_deref() == Some("vfat"))
}

/// Find a VFAT Extended Boot Loader Partition (XBOOTLDR) on the same disk
/// as this partition, typically the ESP.
pub fn find_sibling_xbootldr(&self) -> Result<Option<Device>> {
for parent in self.list_parents()?.unwrap_or_default() {
let parent = list_dev(Utf8Path::new(&parent.path()))?;
if let Some(xbootldr) = parent.find_partition_of_xbootldr() {
return Ok(Some(xbootldr.clone()));
}
}
Ok(None)
}

/// Find the EFI System Partition (ESP) among children.
///
/// For GPT disks, this matches by the ESP partition type GUID.
Expand Down Expand Up @@ -1051,6 +1069,33 @@ mod test {
assert!(dev.find_partition_of_esp().is_err());
}

#[test]
fn test_find_partition_of_xbootldr() {
let cases = [
(XBOOTLDR, Some("vfat"), Some(2)),
(XBOOTLDR, Some("ext4"), None),
(XBOOTLDR, None, None),
(ESP, Some("vfat"), None),
];
for (parttype, fstype, expected) in cases {
let disk: Device = serde_json::from_value(serde_json::json!({
"name": "vda",
"size": 10737418240_u64,
"pttype": "gpt",
"children": [
{"name": "vda1", "size": 104857600, "partn": 1, "parttype": ESP, "fstype": "vfat"},
{"name": "vda2", "size": 2147483648_u64, "partn": 2, "parttype": parttype, "fstype": fstype},
]
}))
.unwrap();
assert_eq!(
disk.find_partition_of_xbootldr().and_then(|p| p.partn),
expected,
"{parttype} {fstype:?}"
);
}
}

#[test]
fn test_parse_partition_number_from_suffix() {
// Short alias like /dev/mapper/mpatha → /dev/mapper/mpatha2
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![allow(dead_code)]

//! Partition type GUIDs from the Discoverable Partitions Specification (DPS)
//!
//! This module contains constants for partition type GUIDs as defined by the
Expand Down Expand Up @@ -55,12 +53,10 @@
//! - **Verity signature partitions**: Signed verity root hashes
//! - **Special partitions**: ESP, XBOOTLDR, swap, home, var, etc.
//!
//! # Usage (internal)
//!
//! This is an internal module. Within bootc, it is used like:
//! # Usage
//!
//! ```ignore
//! use crate::discoverable_partition_specification::{this_arch_root, ESP};
//! use bootc_blockdev::dps::{this_arch_root, ESP};
//!
//! // Get the root partition type GUID for the current architecture
//! let root_type: &str = this_arch_root();
Expand Down
2 changes: 0 additions & 2 deletions crates/lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ anyhow = { workspace = true }
camino = { workspace = true, features = ["serde1"] }
canon-json = { workspace = true }
cap-std-ext = { workspace = true, features = ["fs_utf8"] }
cfg-if = { workspace = true }
chrono = { workspace = true, features = ["serde"] }
clap = { workspace = true, features = ["derive","cargo"] }
clap_complete = "4"
Expand All @@ -48,7 +47,6 @@ indoc = { workspace = true }
libc = { workspace = true }
openssl = { workspace = true }
percent-encoding = { workspace = true }
regex = { workspace = true }
rustix = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
Expand Down
18 changes: 12 additions & 6 deletions crates/lib/src/bootc_composefs/backwards_compat/bcompat_boot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,11 @@ fn handle_bls_conf(
create_staged_bls_entries(boot_dir, &new_bls_entries)?;

let binaries_dir = if is_uki {
let esp = storage.require_esp()?;
let uki_dir = esp.fd.open_dir(BOOTC_UKI_DIR).context("Opening UKI dir")?;
let boot_mount = storage.require_boot_mount()?;
let uki_dir = boot_mount
.fd
.open_dir(BOOTC_UKI_DIR)
.context("Opening UKI dir")?;

uki_dir
} else {
Expand Down Expand Up @@ -329,7 +332,7 @@ pub(crate) async fn prepend_custom_prefix(

BootType::Uki => match bootloader.kind()? {
BootloaderKind::GRUBClassic => {
let esp = storage.require_esp()?;
let boot_mount = storage.require_boot_mount()?;

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

plan_efi_binary_renames(&esp.fd, &digest, &mut rename_transaction)?;
plan_efi_binary_renames(&boot_mount.fd, &digest, &mut rename_transaction)?;

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

let esp = storage.require_esp()?;
let uki_dir = esp.fd.open_dir(BOOTC_UKI_DIR).context("Opening UKI dir")?;
let boot_mount = storage.require_boot_mount()?;
let uki_dir = boot_mount
.fd
.open_dir(BOOTC_UKI_DIR)
.context("Opening UKI dir")?;

// execute all EFI PE renames atomically before the final exchange
rename_transaction
Expand Down
51 changes: 28 additions & 23 deletions crates/lib/src/bootc_composefs/boot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ use rustix::{mount::MountFlags, path::Arg};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::bootc_composefs::boot_partition::BootPartition;
use crate::bootc_composefs::state::{get_booted_bls, write_composefs_state};
use crate::bootc_composefs::status::build_composefs_karg;
use crate::bootc_composefs::uki_addon::{UkiAddonType, UkiAddonsList, list_installed_uki_addons};
Expand All @@ -111,7 +112,6 @@ use crate::composefs_consts::{
};
use crate::parsers::bls_config::{BLSConfig, BLSConfigType, EFIKey};
use crate::spec::BootloaderKind;
use crate::store::find_booted_from_esp;
use crate::task::Task;
use crate::{
bootc_composefs::repo::open_composefs_repo,
Expand Down Expand Up @@ -822,7 +822,7 @@ pub(crate) fn setup_composefs_bls_boot(
) -> Result<String> {
let id_hex = id.to_hex();

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

(
esp_part.path(),
BootPartition::for_install(esp_part.path().into(), postfetch.detected_bootloader)?,
cmdline_options,
postfetch.detected_bootloader.clone(),
)
Expand Down Expand Up @@ -895,7 +895,7 @@ pub(crate) fn setup_composefs_bls_boot(
)?;

(
find_booted_from_esp(&storage.physical_root)?,
storage.require_boot_partition()?.clone(),
cmdline,
bootloader,
)
Expand Down Expand Up @@ -931,7 +931,8 @@ pub(crate) fn setup_composefs_bls_boot(
}

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

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

grub_dir.atomic_write(
Expand Down Expand Up @@ -1930,7 +1931,7 @@ pub(crate) fn setup_composefs_uki_boot(
entries: Vec<ComposefsBootEntry<Sha512HashValue>>,
) -> Result<(String, Sha512HashValue)> {
let (
esp_device,
boot_partition,
bootloader,
missing_fsverity_allowed,
uki_addons_via_cli,
Expand Down Expand Up @@ -1968,7 +1969,7 @@ pub(crate) fn setup_composefs_uki_boot(
}

(
esp_part.path(),
BootPartition::for_install(esp_part.path().into(), postfetch.detected_bootloader)?,
postfetch.detected_bootloader.clone(),
allow_missing_fsverity,
addons,
Expand Down Expand Up @@ -2021,7 +2022,7 @@ pub(crate) fn setup_composefs_uki_boot(
}

(
find_booted_from_esp(&storage.physical_root)?,
storage.require_boot_partition()?.clone(),
bootloader,
booted_cfs.cmdline.allow_missing_fsverity,
addons_via_cli,
Expand Down Expand Up @@ -2065,7 +2066,8 @@ pub(crate) fn setup_composefs_uki_boot(
}
}

let esp_mount = mount_esp_writable(&esp_device).context("Mounting ESP")?;
let boot_mount =
mount_esp_writable(boot_partition.device().as_str()).context("Mounting boot partition")?;

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

// Create a `ESP/loader/addons.staged` if we have Global UKI Addons to update
if is_upgrade {
stage_global_uki_addons(&esp_mount.fd)?;
stage_global_uki_addons(&boot_mount.fd)?;
}

for entry in entries_to_write {
write_pe_to_esp(repo, &entry, &deploy_id, esp_mount.dir.path(), is_upgrade)?;
write_pe_to_esp(repo, &entry, &deploy_id, boot_mount.dir.path(), is_upgrade)?;
}

match bootloader.kind()? {
BootloaderKind::GRUBClassic => {
write_grub_uki_menuentry(&setup_type, boot_label, &deploy_id, &esp_device)?
}
BootloaderKind::GRUBClassic => write_grub_uki_menuentry(
&setup_type,
boot_label,
&deploy_id,
boot_partition.device().as_str(),
)?,

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

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

// Mount a tmpfs over /tmp so that tools invoked with --root have a
Expand Down
Loading
Loading