pub struct Nmount<'a> { /* private fields */ }
mount
only.Expand description
Mount a FreeBSD file system.
The nmount(2)
system call works similarly to the mount(8)
program; it
takes its options as a series of name-value pairs. Most of the values are
strings, as are all of the names. The Nmount
structure builds up an
argument list and then executes the syscall.
Examples
To mount target
onto mountpoint
with nullfs
:
use nix::mount::{MntFlags, Nmount, unmount};
use std::ffi::CString;
use tempfile::tempdir;
let mountpoint = tempdir().unwrap();
let target = tempdir().unwrap();
let fstype = CString::new("fstype").unwrap();
let nullfs = CString::new("nullfs").unwrap();
Nmount::new()
.str_opt(&fstype, &nullfs)
.str_opt_owned("fspath", mountpoint.path().to_str().unwrap())
.str_opt_owned("target", target.path().to_str().unwrap())
.nmount(MntFlags::empty()).unwrap();
unmount(mountpoint.path(), MntFlags::empty()).unwrap();
See Also
Implementations§
source§impl<'a> Nmount<'a>
impl<'a> Nmount<'a>
sourcepub unsafe fn mut_ptr_opt(
&mut self,
name: &'a CStr,
val: *mut c_void,
len: usize
) -> &mut Self
pub unsafe fn mut_ptr_opt(
&mut self,
name: &'a CStr,
val: *mut c_void,
len: usize
) -> &mut Self
Add an opaque mount option.
Some file systems take binary-valued mount options. They can be set with this method.
Safety
Unsafe because it will cause Nmount::nmount
to dereference a raw
pointer. The user is responsible for ensuring that val
is valid and
its lifetime outlives self
! An easy way to do that is to give the
value a larger scope than name
Examples
use libc::c_void;
use nix::mount::Nmount;
use std::ffi::CString;
use std::mem;
// Note that flags outlives name
let mut flags: u32 = 0xdeadbeef;
let name = CString::new("flags").unwrap();
let p = &mut flags as *mut u32 as *mut c_void;
let len = mem::size_of_val(&flags);
let mut nmount = Nmount::new();
unsafe { nmount.mut_ptr_opt(&name, p, len) };
sourcepub fn null_opt(&mut self, name: &'a CStr) -> &mut Self
pub fn null_opt(&mut self, name: &'a CStr) -> &mut Self
Add a mount option that does not take a value.
Examples
use nix::mount::Nmount;
use std::ffi::CString;
let read_only = CString::new("ro").unwrap();
Nmount::new()
.null_opt(&read_only);
sourcepub fn null_opt_owned<P: ?Sized + NixPath>(&mut self, name: &P) -> &mut Self
pub fn null_opt_owned<P: ?Sized + NixPath>(&mut self, name: &P) -> &mut Self
Add a mount option that does not take a value, but whose name must be owned.
This has higher runtime cost than Nmount::null_opt
, but is useful
when the name’s lifetime doesn’t outlive the Nmount
, or it’s a
different string type than CStr
.
Examples
use nix::mount::Nmount;
let read_only = "ro";
let mut nmount: Nmount<'static> = Nmount::new();
nmount.null_opt_owned(read_only);
sourcepub fn str_opt_owned<P1, P2>(&mut self, name: &P1, val: &P2) -> &mut Selfwhere
P1: ?Sized + NixPath,
P2: ?Sized + NixPath,
pub fn str_opt_owned<P1, P2>(&mut self, name: &P1, val: &P2) -> &mut Selfwhere
P1: ?Sized + NixPath,
P2: ?Sized + NixPath,
Add a mount option as an owned string.
This has higher runtime cost than Nmount::str_opt
, but is useful
when the value’s lifetime doesn’t outlive the Nmount
, or it’s a
different string type than CStr
.
Examples
use nix::mount::Nmount;
use std::path::Path;
let mountpoint = Path::new("/mnt");
Nmount::new()
.str_opt_owned("fspath", mountpoint.to_str().unwrap());
sourcepub fn nmount(&mut self, flags: MntFlags) -> NmountResult
pub fn nmount(&mut self, flags: MntFlags) -> NmountResult
Actually mount the file system.