1
0
mirror of https://github.com/containers/bootc.git synced 2026-02-05 15:45:53 +01:00

install: empty /boot & /boot/efi

Get pointer from Colin's comment
https://github.com/bootc-dev/bootc/pull/1752#issuecomment-3532953293
- Empty the complete ESP
- On ostree OS, empty `/boot` but preserve `/boot/loader`
- On none ostree OS, the loader is directory that needs to be
removed.

Signed-off-by: Huijing Hei <hhei@redhat.com>
This commit is contained in:
Huijing Hei
2025-11-25 15:27:02 +08:00
committed by Colin Walters
parent 5ec2c5ce69
commit 19534d148e

View File

@@ -1922,30 +1922,58 @@ fn remove_all_in_dir_no_xdev(d: &Dir, mount_err: bool) -> Result<()> {
anyhow::Ok(())
}
#[context("Removing boot directory content except loader dir on ostree")]
fn remove_all_except_loader_dirs(bootdir: &Dir, is_ostree: bool) -> Result<()> {
let entries = bootdir
.entries()
.context("Reading boot directory entries")?;
for entry in entries {
let entry = entry.context("Reading directory entry")?;
let file_name = entry.file_name();
let file_name = if let Some(n) = file_name.to_str() {
n
} else {
anyhow::bail!("Invalid non-UTF8 filename: {file_name:?} in /boot");
};
// Only preserve loader on ostree
if is_ostree && file_name.starts_with("loader") {
continue;
}
let etype = entry.file_type()?;
if etype == FileType::dir() {
// Open the directory and remove its contents
if let Some(subdir) = bootdir.open_dir_noxdev(&file_name)? {
remove_all_in_dir_no_xdev(&subdir, false)
.with_context(|| format!("Removing directory contents: {}", file_name))?;
}
} else {
bootdir
.remove_file_optional(&file_name)
.with_context(|| format!("Removing file: {}", file_name))?;
}
}
Ok(())
}
#[context("Removing boot directory content")]
fn clean_boot_directories(rootfs: &Dir, is_ostree: bool) -> Result<()> {
let bootdir =
crate::utils::open_dir_remount_rw(rootfs, BOOT.into()).context("Opening /boot")?;
if is_ostree {
// On ostree systems, the boot directory already has our desired format, we should only
// remove the bootupd-state.json file to avoid bootupctl complaining it already exists.
bootdir
.remove_file_optional("bootupd-state.json")
.context("removing bootupd-state.json")?;
} else {
// This should not remove /boot/efi note.
remove_all_in_dir_no_xdev(&bootdir, false).context("Emptying /boot")?;
// TODO: Discover the ESP the same way bootupd does it; we should also
// support not wiping the ESP.
if ARCH_USES_EFI {
if let Some(efidir) = bootdir
.open_dir_optional(crate::bootloader::EFI_DIR)
.context("Opening /boot/efi")?
{
remove_all_in_dir_no_xdev(&efidir, false)
.context("Emptying EFI system partition")?;
}
// This should not remove /boot/efi note.
remove_all_except_loader_dirs(&bootdir, is_ostree).context("Emptying /boot")?;
// TODO: Discover the ESP the same way bootupd does it; we should also
// support not wiping the ESP.
if ARCH_USES_EFI {
if let Some(efidir) = bootdir
.open_dir_optional(crate::bootloader::EFI_DIR)
.context("Opening /boot/efi")?
{
remove_all_in_dir_no_xdev(&efidir, false).context("Emptying EFI system partition")?;
}
}