#syscalls #mount #linux #linux-api #system-api #umount

sys-mount

High level FFI binding around the sys mount & umount2 calls

13 stable releases

3.0.1 Feb 5, 2024
2.1.1 Jan 5, 2024
2.1.0 Aug 4, 2023
2.0.2 Jan 31, 2023
1.1.0 Nov 1, 2018

#35 in Filesystem

Download history 24868/week @ 2024-08-20 25097/week @ 2024-08-27 25312/week @ 2024-09-03 27650/week @ 2024-09-10 25570/week @ 2024-09-17 30333/week @ 2024-09-24 31269/week @ 2024-10-01 30674/week @ 2024-10-08 33377/week @ 2024-10-15 34637/week @ 2024-10-22 32671/week @ 2024-10-29 31074/week @ 2024-11-05 30372/week @ 2024-11-12 27732/week @ 2024-11-19 18322/week @ 2024-11-26 21462/week @ 2024-12-03

102,679 downloads per month
Used in 13 crates (12 directly)

MIT/Apache

35KB
534 lines

sys-mount

Rust Compatibility License

High level FFI bindings to the mount and umount2 system calls, for Rust.

Examples

Mount

This is how the mount command could be written with this API.

extern crate clap;
extern crate sys_mount;

use clap::{App, Arg};
use sys_mount::{Mount, MountFlags, SupportedFilesystems};
use std::process::exit;

fn main() {
    let matches = App::new("mount")
        .arg(Arg::with_name("source").required(true))
        .arg(Arg::with_name("directory").required(true))
        .get_matches();

    let src = matches.value_of("source").unwrap();
    let dir = matches.value_of("directory").unwrap();

    // Fetch a listed of supported file systems on this system. This will be used
    // as the fstype to `Mount::new`, as the `Auto` mount parameter.
    let supported = match SupportedFilesystems::new() {
        Ok(supported) => supported,
        Err(why) => {
            eprintln!("failed to get supported file systems: {}", why);
            exit(1);
        }
    };

    // The source block will be mounted to the target directory, and the fstype is likely
    // one of the supported file systems.
    let result = Mount::builder()
        .fstype(FilesystemType::from(&supported))
        .mount(src, dir);

    match result {
        Ok(mount) => {
            println!("mounted {} ({}) to {}", src, mount.get_fstype(), dir);
        }
        Err(why) => {
            eprintln!("failed to mount {} to {}: {}", src, dir, why);
            exit(1);
        }
    }
}

Umount

This is how the umount command could be implemented.

extern crate clap;
extern crate sys_mount;

use clap::{App, Arg};
use sys_mount::{unmount, UnmountFlags};
use std::process::exit;

fn main() {
    let matches = App::new("umount")
        .arg(Arg::with_name("lazy")
            .short("l")
            .long("lazy"))
        .arg(Arg::with_name("source").required(true))
        .get_matches();

    let src = matches.value_of("source").unwrap();

    let flags = if matches.is_present("lazy") {
        UnmountFlags::DETACH
    } else {
        UnmountFlags::empty()
    };

    match unmount(src, flags) {
        Ok(()) => (),
        Err(why) => {
            eprintln!("failed to unmount {}: {}", src, why);
            exit(1);
        }
    }
}

Dependencies

~0.6–7.5MB
~60K SLoC