Skip to content

Commit 54078f2

Browse files
ericcurtincgwalters
authored andcommitted
install: Only remove bootloader dirs from the ESP
to-existing-root emptied the whole ESP, which can hold content bootc cannot recreate. On Asahi this includes m1n1/boot.bin, vendorfw/ and ubootefi.var; wiping them leaves the machine unbootable. Only remove EFI/ and loader/, which we reinstall. Factor out remove_dir_no_xdev() to share the recursive removal. Generated-by: AI Signed-off-by: Eric Curtin <[email protected]>
1 parent 7d6df9a commit 54078f2

3 files changed

Lines changed: 90 additions & 19 deletions

File tree

‎crates/lib/src/install.rs‎

Lines changed: 59 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2448,19 +2448,27 @@ fn remove_all_in_dir_no_xdev(d: &Dir, mount_err: bool) -> Result<()> {
24482448
let name = entry.file_name();
24492449
let etype = entry.file_type()?;
24502450
if etype == FileType::dir() {
2451-
if let Some(subdir) = d.open_dir_noxdev(&name)? {
2452-
remove_all_in_dir_no_xdev(&subdir, mount_err)?;
2453-
d.remove_dir(&name)?;
2454-
} else if mount_err {
2455-
anyhow::bail!("Found unexpected mount point {name:?}");
2456-
}
2451+
remove_dir_no_xdev(d, &name, mount_err)?;
24572452
} else {
24582453
d.remove_file_optional(&name)?;
24592454
}
24602455
}
24612456
anyhow::Ok(())
24622457
}
24632458

2459+
/// Recursively remove the directory `name` in `d`, without crossing devices.
2460+
/// A mount point is left alone, or is an error if `mount_err` is true.
2461+
fn remove_dir_no_xdev(d: &Dir, name: impl AsRef<Path>, mount_err: bool) -> Result<()> {
2462+
let name = name.as_ref();
2463+
if let Some(subdir) = d.open_dir_noxdev(name)? {
2464+
remove_all_in_dir_no_xdev(&subdir, mount_err)?;
2465+
d.remove_dir(name)?;
2466+
} else if mount_err {
2467+
anyhow::bail!("Found unexpected mount point {name:?}");
2468+
}
2469+
Ok(())
2470+
}
2471+
24642472
#[context("Removing boot directory content except loader dir on ostree")]
24652473
fn remove_all_except_loader_dirs(bootdir: &Dir, is_ostree: bool) -> Result<()> {
24662474
let entries = bootdir
@@ -2485,12 +2493,8 @@ fn remove_all_except_loader_dirs(bootdir: &Dir, is_ostree: bool) -> Result<()> {
24852493

24862494
let etype = entry.file_type()?;
24872495
if etype == FileType::dir() {
2488-
// Open the directory and remove its contents
2489-
if let Some(subdir) = bootdir.open_dir_noxdev(&file_name)? {
2490-
remove_all_in_dir_no_xdev(&subdir, false)
2491-
.with_context(|| format!("Removing directory contents: {}", file_name))?;
2492-
bootdir.remove_dir(&file_name)?;
2493-
}
2496+
remove_dir_no_xdev(bootdir, file_name, false)
2497+
.with_context(|| format!("Removing directory: {file_name}"))?;
24942498
} else {
24952499
bootdir
24962500
.remove_file_optional(&file_name)
@@ -2500,6 +2504,22 @@ fn remove_all_except_loader_dirs(bootdir: &Dir, is_ostree: bool) -> Result<()> {
25002504
Ok(())
25012505
}
25022506

2507+
/// Remove the bootloader dirs (bootupd/grub, systemd-boot) from the ESP.
2508+
/// Other content, e.g. Asahi's `m1n1/` and `vendorfw/`, may be firmware
2509+
/// or earlier boot stages we cannot recreate, so it is preserved.
2510+
// TODO: be more selective, e.g. keep other OSes' `EFI/<vendor>` and
2511+
// non-bootc `loader/` entries, and drop old Type #1 kernels (see #2243).
2512+
#[context("Removing bootloader content from EFI system partition")]
2513+
fn clean_esp_bootloader_dirs(efidir: &Dir) -> Result<()> {
2514+
for name in ["EFI", "loader"] {
2515+
if efidir.try_exists(name)? {
2516+
remove_dir_no_xdev(efidir, name, false)
2517+
.with_context(|| format!("Removing directory: {name}"))?;
2518+
}
2519+
}
2520+
Ok(())
2521+
}
2522+
25032523
#[context("Removing boot directory content")]
25042524
fn clean_boot_directories(rootfs: &Dir, rootfs_path: &Utf8Path, is_ostree: bool) -> Result<()> {
25052525
let bootdir =
@@ -2514,13 +2534,12 @@ fn clean_boot_directories(rootfs: &Dir, rootfs_path: &Utf8Path, is_ostree: bool)
25142534
// This should not remove /boot/efi note.
25152535
remove_all_except_loader_dirs(&bootdir, is_ostree).context("Emptying /boot")?;
25162536

2517-
// TODO: we should also support not wiping the ESP.
25182537
if ARCH_USES_EFI {
25192538
if let Some(efidir) = bootdir
25202539
.open_dir_optional(crate::bootloader::EFI_DIR)
25212540
.context("Opening /boot/efi")?
25222541
{
2523-
remove_all_in_dir_no_xdev(&efidir, false).context("Emptying EFI system partition")?;
2542+
clean_esp_bootloader_dirs(&efidir)?;
25242543
}
25252544
}
25262545

@@ -3249,6 +3268,32 @@ mod tests {
32493268
Ok(())
32503269
}
32513270

3271+
#[test]
3272+
fn test_clean_esp_bootloader_dirs() -> Result<()> {
3273+
let td = cap_std_ext::cap_tempfile::TempDir::new(cap_std::ambient_authority())?;
3274+
3275+
td.create_dir_all("EFI/BOOT")?;
3276+
td.write("EFI/BOOT/BOOTAA64.EFI", b"shim")?;
3277+
td.create_dir_all("loader/entries")?;
3278+
td.write("loader/entries/foo.conf", b"entry")?;
3279+
// Asahi content which must survive
3280+
td.create_dir_all("m1n1")?;
3281+
td.write("m1n1/boot.bin", b"m1n1")?;
3282+
td.write("ubootefi.var", b"efivars")?;
3283+
3284+
clean_esp_bootloader_dirs(&td)?;
3285+
assert!(!td.exists("EFI"));
3286+
assert!(!td.exists("loader"));
3287+
assert_eq!(td.read("m1n1/boot.bin")?, b"m1n1");
3288+
assert_eq!(td.read("ubootefi.var")?, b"efivars");
3289+
3290+
// Idempotent
3291+
clean_esp_bootloader_dirs(&td)?;
3292+
assert_eq!(td.entries()?.count(), 2);
3293+
3294+
Ok(())
3295+
}
3296+
32523297
#[test]
32533298
fn test_read_boot_fstab_entry() -> Result<()> {
32543299
let td = cap_std_ext::cap_tempfile::TempDir::new(cap_std::ambient_authority())?;

‎docs/src/bootc-installation.7.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,9 @@ Set the environment variable `BOOTC_DIRECT_IO=on` to create the loopback device
233233

234234
This is a variant of `install to-filesystem`, which maximizes convenience for using
235235
an existing Linux system, converting it into the target container image. Note that
236-
the `/boot` (and `/boot/efi`) partitions *will be reinitialized* - so this is a
237-
somewhat destructive operation for the existing Linux installation.
236+
the `/boot` partition and the `EFI/` and `loader/` directories of the ESP
237+
*will be reinitialized* - so this is a somewhat destructive operation for the
238+
existing Linux installation. Other ESP content (e.g. Asahi's `m1n1/`) is preserved.
238239

239240
Also, because the filesystem is reused, it's required that the target system kernel
240241
support the root storage setup already initialized.

‎tmt/tests/booted/test-multi-device-esp.nu‎

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
# 3. Three devices, partial ESP: Three disks, ESP on disk1+disk3 only
1717
#
1818
# Reboot 2:
19-
# 4. Single device (no LVM): ESP + root partition on a single disk
19+
# 4. Single device (no LVM): ESP + root partition on a single disk;
20+
# also checks that non-bootloader ESP content is preserved
2021
# 5. No ESP anywhere: Two disks with no ESP; install should fail gracefully
2122
#
2223
# This validates the fix for https://github.com/bootc-dev/bootc/issues/481
@@ -157,6 +158,17 @@ def validate_esp [esp_partition: string] {
157158
}
158159
}
159160

161+
# Mount an ESP partition, run a closure on the mountpoint, and unmount it
162+
def with_esp [esp_partition: string, f: closure] {
163+
let esp_mount = "/var/mnt/esp_seed"
164+
mkdir $esp_mount
165+
mount $esp_partition $esp_mount
166+
let r = (do $f $esp_mount)
167+
umount $esp_mount
168+
rmdir $esp_mount
169+
$r
170+
}
171+
160172
# Run bootc install to-existing-root from within the container image under test
161173
def run_install [mountpoint: string] {
162174
(podman run
@@ -356,8 +368,15 @@ def test_single_device_no_lvm [] {
356368
mkdir $mountpoint
357369
mount $"($loop1)p2" $mountpoint
358370

359-
# Create boot directory
360-
mkdir $"($mountpoint)/boot"
371+
# Create /boot/efi so the ESP gets mounted and cleaned
372+
mkdir $"($mountpoint)/boot/efi"
373+
374+
# Seed non-bootloader content (as on Asahi) and a stale bootloader dir
375+
with_esp $"($loop1)p1" {|esp|
376+
mkdir $"($esp)/m1n1" $"($esp)/EFI/stale"
377+
"m1n1" | save $"($esp)/m1n1/boot.bin"
378+
"stale" | save $"($esp)/EFI/stale/x.efi"
379+
}
361380

362381
# Show block device hierarchy
363382
lsblk --pairs --paths --inverse --output NAME,TYPE $"($loop1)p2"
@@ -366,6 +385,12 @@ def test_single_device_no_lvm [] {
366385

367386
# Validate ESP was installed correctly
368387
validate_esp $"($loop1)p1"
388+
let r = (with_esp $"($loop1)p1" {|esp| {
389+
m1n1: ($"($esp)/m1n1/boot.bin" | path exists)
390+
stale: ($"($esp)/EFI/stale" | path exists)
391+
}})
392+
assert $r.m1n1 "m1n1/boot.bin was removed from the ESP"
393+
assert (not $r.stale) "EFI/stale was not removed from the ESP"
369394
} catch {|e|
370395
cleanup_simple $loop1 $mountpoint
371396
rm -f $disk1

0 commit comments

Comments
 (0)