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
3 changes: 3 additions & 0 deletions lib/src/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1177,6 +1177,9 @@ pub(crate) async fn install_to_disk(mut opts: InstallToDiskOpts) -> Result<()> {
block_opts.device
);
}
if !crate::mount::is_same_as_host(Utf8Path::new("/dev"))? {
anyhow::bail!("Loopback mounts (--via-loopback) require host devices (-v /dev:/dev)");
}
} else if !target_blockdev_meta.file_type().is_block_device() {
anyhow::bail!("Not a block device: {}", block_opts.device);
}
Expand Down
22 changes: 22 additions & 0 deletions lib/src/mount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,25 @@ pub(crate) fn mount(dev: &str, target: &Utf8Path) -> Result<()> {
[dev, target.as_str()],
)
}

/// If the fsid of the passed path matches the fsid of the same path rooted
/// at /proc/1/root, it is assumed that these are indeed the same mounted
/// filesystem between container and host.
/// Path should be absolute.
#[context("Comparing filesystems at {path} and /proc/1/root/{path}")]
pub(crate) fn is_same_as_host(path: &Utf8Path) -> Result<bool> {
// Add a leading '/' in case a relative path is passed
let path = Utf8Path::new("/").join(path);

// Using statvfs instead of fs, since rustix will translate the fsid field
// for us.
let devstat = rustix::fs::statvfs(path.as_std_path())?;
let hostpath = Utf8Path::new("/proc/1/root").join(path.strip_prefix("/")?);
let hostdevstat = rustix::fs::statvfs(hostpath.as_std_path())?;
tracing::trace!(
"base mount id {:?}, host mount id {:?}",
devstat.f_fsid,
hostdevstat.f_fsid
);
Ok(devstat.f_fsid == hostdevstat.f_fsid)
}