ttcore/
lib.rs

1//!
2//! # TT 核心实现
3//!
4
5#![cfg(target_os = "linux")]
6#![warn(missing_docs, unused_import_braces, unused_extern_crates)]
7
8mod def;
9pub use def::*;
10
11#[cfg(not(feature = "testmock"))]
12mod linux;
13#[cfg(not(feature = "testmock"))]
14pub use linux::*;
15
16#[cfg(not(feature = "testmock"))]
17mod common {
18    use crate::Vm;
19    use futures::executor::{ThreadPool, ThreadPoolBuilder};
20    use lazy_static::lazy_static;
21    use myutil::{err::*, *};
22    use std::path::PathBuf;
23
24    pub(crate) const CLONE_MARK: &str = "clone_";
25
26    lazy_static! {
27        pub(crate) static ref POOL: ThreadPool =
28            pnk!(ThreadPoolBuilder::new().pool_size(1).create());
29    }
30
31    pub(crate) async fn asleep(sec: u64) {
32        futures_timer::Delay::new(std::time::Duration::from_secs(sec)).await;
33    }
34
35    #[cfg(feature = "zfs")]
36    lazy_static! {
37        pub(crate) static ref ZFS_ROOT: &'static str =
38            pnk!(imgroot_register(None));
39    }
40
41    #[cfg(feature = "zfs")]
42    pub(crate) fn imgroot_register(
43        imgpath: Option<&str>,
44    ) -> Option<&'static str> {
45        static mut ROOT: Option<String> = None;
46        if let Some(path) = imgpath {
47            unsafe {
48                ROOT.replace(
49                    path.trim_start_matches("/dev/zvol/")
50                        .trim_end_matches('/')
51                        .to_owned(),
52                );
53            }
54        }
55
56        unsafe { ROOT.as_deref() }
57    }
58
59    // VM image 命名格式为:
60    // - ${CLONE_MARK}_VmId
61    #[inline(always)]
62    pub(crate) fn vmimg_path(vm: &Vm) -> PathBuf {
63        let mut vmimg_path = vm.image_path.clone();
64        let vmimg_name = format!("{}{}", CLONE_MARK, vm.id);
65        vmimg_path.set_file_name(vmimg_name);
66        vmimg_path
67    }
68}
69
70#[cfg(not(feature = "testmock"))]
71pub(crate) use common::*;
72
73mod test;
74
75#[cfg(feature = "testmock")]
76mod mocker;
77#[cfg(feature = "testmock")]
78pub use mocker::*;