]> git.sesse.net Git - bcachefs-tools-debian/blob - rust-src/mount/src/main.rs
rust: support fstab style mount
[bcachefs-tools-debian] / rust-src / mount / src / main.rs
1
2 use clap::Parser;
3
4 fn main() {
5         // convert existing log statements to tracing events
6         // tracing_log::LogTracer::init().expect("logtracer init failed!");
7         // format tracing log data to env_logger like stdout
8         tracing_subscriber::fmt::init();
9
10         if let Err(e) = crate::main_inner() {
11                 tracing::error!(fatal_error = ?e);
12         }
13 }
14
15
16 #[tracing_attributes::instrument("main")]
17 pub fn main_inner() -> anyhow::Result<()> {
18         use bcachefs_mount::{Cli, filesystem, key};
19         unsafe {
20                 libc::setvbuf(
21                         filesystem::stdout,
22                         std::ptr::null_mut(),
23                         libc::_IONBF,
24                         0,
25                 );
26                 // libc::fflush(filesystem::stdout);
27         }
28
29         let opt = Cli::parse();
30
31         tracing::trace!(?opt);
32
33         let fss = filesystem::probe_filesystems()?;
34         let fs = fss
35                 .get(&opt.uuid)
36                 .ok_or_else(|| anyhow::anyhow!("filesystem was not found"))?;
37
38         tracing::info!(msg="found filesystem", %fs);
39         if fs.encrypted() {
40                 let key = opt
41                         .key_location
42                         .0
43                         .ok_or_else(|| anyhow::anyhow!("no keyoption specified for locked filesystem"))?;
44
45                 key::prepare_key(&fs, key)?;
46         }
47
48         let mountpoint = opt
49                 .mountpoint
50                 .ok_or_else(|| anyhow::anyhow!("mountpoint option was not specified"))?;
51
52         fs.mount(&mountpoint, &opt.options)?;
53
54         Ok(())
55 }
56
57 #[cfg(test)]
58 mod test {
59         // use insta::assert_debug_snapshot;
60         // #[test]
61         // fn snapshot_testing() {
62         //      insta::assert_debug_snapshot!();
63         // }
64 }