use anyhow::Result;
#[cfg(target_os = "linux")]
pub fn mount_fs(
source: &str,
target: &str,
fstype: &str,
_options: &[String],
) -> Result<()> {
use nix::mount::{mount, MsFlags};
use std::path::Path;
tracing::info!("Mounting {} on {} (type: {})", source, target, fstype);
std::fs::create_dir_all(target)?;
let source: Option<&str> = Some(source);
let target = Path::new(target);
let fstype: Option<&str> = Some(fstype);
let flags = MsFlags::empty();
let data: Option<&str> = None;
mount(source, target, fstype, flags, data)?;
Ok(())
}
#[cfg(not(target_os = "linux"))]
pub fn mount_fs(
source: &str,
target: &str,
fstype: &str,
_options: &[String],
) -> Result<()> {
tracing::warn!(
"mount_fs is only supported on Linux (called with source={}, target={}, fstype={})",
source,
target,
fstype
);
anyhow::bail!("mount_fs is only supported on Linux")
}
#[cfg(target_os = "linux")]
pub fn unmount_fs(target: &str) -> Result<()> {
tracing::info!("Unmounting {}", target);
nix::mount::umount(target)?;
Ok(())
}
#[cfg(not(target_os = "linux"))]
pub fn unmount_fs(target: &str) -> Result<()> {
tracing::warn!("unmount_fs is only supported on Linux (target={})", target);
anyhow::bail!("unmount_fs is only supported on Linux")
}
pub fn mount_virtiofs(tag: &str, mountpoint: &str) -> Result<()> {
mount_fs(tag, mountpoint, "virtiofs", &[])
}