Skip to content
Draft
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
111 changes: 83 additions & 28 deletions crates/lib/src/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,7 @@ pub(crate) struct RootSetup {
pub(crate) physical_root_path: Utf8PathBuf,
/// Directory file descriptor for the above physical root.
pub(crate) physical_root: Dir,
pub(crate) target_root_path: Option<Utf8PathBuf>,
pub(crate) rootfs_uuid: Option<String>,
/// True if we should skip finalizing
skip_finalize: bool,
Expand Down Expand Up @@ -1521,7 +1522,10 @@ async fn install_with_sysroot(
Bootloader::Grub => {
crate::bootloader::install_via_bootupd(
&rootfs.device_info,
&rootfs.physical_root_path,
&rootfs
.target_root_path
.clone()
.unwrap_or(rootfs.physical_root_path.clone()),
&state.config_opts,
Some(&deployment_path.as_str()),
)?;
Expand Down Expand Up @@ -1811,6 +1815,42 @@ fn require_empty_rootdir(rootfs_fd: &Dir) -> Result<()> {
Ok(())
}

#[context("Removing boot directory content except loader and ostree dirs")]
fn remove_all_except_loader_dirs(bootdir: &Dir) -> Result<()> {
let entries = bootdir.entries().context("Reading boot directory entries")?;
let protected_dirs = ["loader", "ostree"];

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");
};

// Skip any entry that starts with a protected prefix,
// as we also need to protect loader.0 or loader.1
if protected_dirs.iter().any(|p| file_name.starts_with(p)) {
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(())
}

/// Remove all entries in a directory, but do not traverse across distinct devices.
/// If mount_err is true, then an error is returned if a mount point is found;
/// otherwise it is silently ignored.
Expand All @@ -1834,29 +1874,22 @@ fn remove_all_in_dir_no_xdev(d: &Dir, mount_err: bool) -> Result<()> {
}

#[context("Removing boot directory content")]
fn clean_boot_directories(rootfs: &Dir, is_ostree: bool) -> Result<()> {
fn clean_boot_directories(rootfs: &Dir) -> 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).context("Emptying /boot except protected dirs")?;

// 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")?;
}
}

Expand Down Expand Up @@ -1993,6 +2026,7 @@ pub(crate) async fn install_to_filesystem(
.context("Mounting host / to {ALONGSIDE_ROOT_MOUNT}")?;
}

let target_root_path = &fsopts.root_path.clone();
// Check that the target is a directory
{
let root_path = &fsopts.root_path;
Expand All @@ -2012,6 +2046,20 @@ pub(crate) async fn install_to_filesystem(
warn_on_host_root(&rootfs_fd)?;
}

// Get a file descriptor for the root path /target
// This is needed to find boot dev
let target_rootfs_fd = {
let rootfs_fd = Dir::open_ambient_dir(&target_root_path, cap_std::ambient_authority())
.with_context(|| format!("Opening target root directory {target_root_path}"))?;

tracing::debug!("Root filesystem: {target_root_path}");

if let Some(false) = rootfs_fd.is_mountpoint(".")? {
anyhow::bail!("Not a mountpoint: {target_root_path}");
}
rootfs_fd
};

// If we're installing to an ostree root, then find the physical root from
// the deployment root.
let possible_physical_root = fsopts.root_path.join("sysroot");
Expand All @@ -2025,9 +2073,9 @@ pub(crate) async fn install_to_filesystem(
};

// Get a file descriptor for the root path
let rootfs_fd = {
let rootfs_fd = if fsopts.root_path.ends_with("sysroot") {
let root_path = &fsopts.root_path;
let rootfs_fd = Dir::open_ambient_dir(&fsopts.root_path, cap_std::ambient_authority())
let rootfs_fd = Dir::open_ambient_dir(root_path, cap_std::ambient_authority())
.with_context(|| format!("Opening target root directory {root_path}"))?;

tracing::debug!("Root filesystem: {root_path}");
Expand All @@ -2036,6 +2084,8 @@ pub(crate) async fn install_to_filesystem(
anyhow::bail!("Not a mountpoint: {root_path}");
}
rootfs_fd
} else {
target_rootfs_fd.try_clone()?
};

match fsopts.replace {
Expand All @@ -2045,8 +2095,11 @@ pub(crate) async fn install_to_filesystem(
tokio::task::spawn_blocking(move || remove_all_in_dir_no_xdev(&rootfs_fd, true))
.await??;
}
Some(ReplaceMode::Alongside) => clean_boot_directories(&rootfs_fd, is_already_ostree)?,
None => require_empty_rootdir(&rootfs_fd)?,
// Find boot under /
Some(ReplaceMode::Alongside) => {
clean_boot_directories(&target_rootfs_fd)?
}
None => require_empty_rootdir(&target_rootfs_fd)?,
}

// Gather data about the root filesystem
Expand Down Expand Up @@ -2090,7 +2143,7 @@ pub(crate) async fn install_to_filesystem(

let boot_is_mount = {
let root_dev = rootfs_fd.dir_metadata()?.dev();
let boot_dev = rootfs_fd
let boot_dev = target_rootfs_fd
.symlink_metadata_optional(BOOT)?
.ok_or_else(|| {
anyhow!("No /{BOOT} directory found in root; this is is currently required")
Expand All @@ -2101,9 +2154,10 @@ pub(crate) async fn install_to_filesystem(
};
// Find the UUID of /boot because we need it for GRUB.
let boot_uuid = if boot_is_mount {
let boot_path = fsopts.root_path.join(BOOT);
let boot_path = target_root_path.join(BOOT);
tracing::debug!("boot_path={boot_path}");
let u = bootc_mount::inspect_filesystem(&boot_path)
.context("Inspecting /{BOOT}")?
.with_context(|| format!("Inspecting /{BOOT}"))?
.uuid
.ok_or_else(|| anyhow!("No UUID found for /{BOOT}"))?;
Some(u)
Expand Down Expand Up @@ -2184,6 +2238,7 @@ pub(crate) async fn install_to_filesystem(
device_info,
physical_root_path: fsopts.root_path,
physical_root: rootfs_fd,
target_root_path: Some(target_root_path.clone()),
rootfs_uuid: inspect.uuid.clone(),
boot,
kargs,
Expand Down
1 change: 1 addition & 0 deletions crates/lib/src/install/baseline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,7 @@ pub(crate) fn install_create_rootfs(
device_info,
physical_root_path,
physical_root,
target_root_path: None,
rootfs_uuid: Some(root_uuid.to_string()),
boot,
kargs,
Expand Down
Loading