Skip to content

Commit

Permalink
Move /dev mounting to loopback
Browse files Browse the repository at this point in the history
  • Loading branch information
bcrochet committed Mar 26, 2024
1 parent 94a45bd commit 94e5ce8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
13 changes: 13 additions & 0 deletions lib/src/blockdev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ use std::os::unix::io::AsRawFd;
use std::path::Path;
use std::process::Command;

/// The mount path and type for devtmpfs
#[cfg(feature = "install")]
const DEV_MOUNT_POINT: &str = "/dev";
const DEVTMPFS: &str = "devtmpfs";

#[derive(Debug, Deserialize)]
struct DevicesOutput {
blockdevices: Vec<Device>,
Expand Down Expand Up @@ -80,9 +85,17 @@ pub(crate) struct LoopbackDevice {
pub(crate) dev: Option<Utf8PathBuf>,
}

// Ensure that `/dev` directory exists and is mounted.
fn ensure_dev_mounted() -> Result<()> {
crate::mount::ensure_mount(DEVTMPFS, DEV_MOUNT_POINT, DEVTMPFS)?;
Ok(())
}

impl LoopbackDevice {
// Create a new loopback block device targeting the provided file path.
pub(crate) fn new(path: &Path) -> Result<Self> {
// Ensure /dev exists and is mounted
ensure_dev_mounted()?;
let dev = Task::new("losetup", "losetup")
.args(["--show", "--direct-io=on", "-P", "--find"])
.arg(path)
Expand Down
11 changes: 0 additions & 11 deletions lib/src/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@ const SELINUXFS: &str = "/sys/fs/selinux";
#[cfg(feature = "install")]
const EFIVARFS: &str = "/sys/firmware/efi/efivars";
pub(crate) const ARCH_USES_EFI: bool = cfg!(any(target_arch = "x86_64", target_arch = "aarch64"));
/// The mount path for devtmpfs
#[cfg(feature = "install")]
const DEVTMPFS: &str = "/dev";

/// Kernel argument used to specify we want the rootfs mounted read-write by default
const RW_KARG: &str = "rw";
Expand Down Expand Up @@ -1079,14 +1076,6 @@ async fn prepare_install(
super::cli::ensure_self_unshared_mount_namespace().await?;
}

// We need to ensure that /dev is mounted before we get to any losetup tasks
for (fstype, fspath) in [("devtmpfs", DEVTMPFS)] {
let _ = Task::new(format!("Mounting {fstype} {fspath}"), "mount")
.args(["-t", fstype, fstype, fspath])
.quiet()
.run();
}

setup_sys_mount("efivarfs", EFIVARFS)?;

// Now, deal with SELinux state.
Expand Down
16 changes: 16 additions & 0 deletions lib/src/mount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,19 @@ pub(crate) fn mount(dev: &str, target: &Utf8Path) -> Result<()> {
[dev, target.as_str()],
)
}

// Ensure the `/dev` directory exists.
fn ensure_mountpoint(target: &str) -> Result<()> {
std::fs::create_dir_all(target)?;
Ok(())
}

/// General mount that also checks that mount path exists
pub(crate) fn ensure_mount(dev: &str, target: &str, fstype: &str) -> Result<()> {
ensure_mountpoint(target)?;
Task::new(format!("Mounting {fstype} {target}"), "mount")
.args(["-t", fstype, dev, target])
.quiet()
.run()?;
Ok(())
}

0 comments on commit 94e5ce8

Please sign in to comment.