Skip to main content

vm/
lib.rs

1#![deny(unsafe_code)]
2
3#[cfg(target_os = "linux")]
4mod clone;
5mod sandbox;
6
7#[cfg(target_os = "linux")]
8pub use clone::{clone_file, reflink_file};
9
10pub use sandbox::{command_line, MountConfig, PortForwardHandle, Sandbox, VmConfigBuilder};
11pub use vm_proto::{
12    frame, ExecRequest, ForwardRequest, ForwardResponse, MountRequest, MountResponse, PortMapping,
13    ReadFileRequest, WriteFileRequest, WriteFileResponse, VSOCK_PORT, VSOCK_PORT_FORWARD,
14};
15
16// The platform backend: Virtualization.framework on macOS, KVM on arm64
17// Linux, cloud-hypervisor on x86_64 Linux. All expose the same vocabulary.
18#[cfg(all(target_os = "linux", target_arch = "x86_64"))]
19pub(crate) use vm_ch as backend;
20#[cfg(target_os = "macos")]
21pub(crate) use vm_darwin as backend;
22#[cfg(all(target_os = "linux", target_arch = "aarch64"))]
23pub(crate) use vm_linux as backend;
24
25// Re-exports from the backend for advanced/escape-hatch use
26pub use backend::{VirtualMachine, VmState, VzError};
27
28/// Reject checkpoint names that could escape the checkpoints directory.
29pub fn validate_checkpoint_name(name: &str) -> Result<(), String> {
30    if name.is_empty() {
31        return Err("checkpoint name cannot be empty".into());
32    }
33    if name.contains('/') || name.contains('\\') || name.contains('\0') || name.contains("..") {
34        return Err(format!("invalid checkpoint name: '{}'", name));
35    }
36    Ok(())
37}
38
39/// `HANZO_VM_HOME`, else `~/.hanzo/vm`.
40pub fn default_data_dir() -> String {
41    if let Ok(dir) = std::env::var("HANZO_VM_HOME") {
42        return dir;
43    }
44    let home = std::env::var("HOME").unwrap_or_else(|_| "/tmp".to_string());
45    format!("{}/.hanzo/vm", home)
46}