mod compression;
mod container;
mod error;
mod manager;
mod state;
mod util;
mod vm;
#[cfg(test)]
mod tests;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::PathBuf;
use arcbox_hypervisor::{DeviceSnapshot, VcpuSnapshot, VmSnapshot};
pub use error::SnapshotError;
type MemoryReaderFn = Box<dyn FnOnce(&mut [u8]) -> Result<(), SnapshotError> + Send>;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SnapshotInfo {
pub id: String,
pub name: String,
pub target_id: String,
pub target_type: SnapshotTargetType,
pub created: DateTime<Utc>,
pub size: u64,
pub parent: Option<String>,
pub description: Option<String>,
pub labels: HashMap<String, String>,
pub state: SnapshotState,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum SnapshotTargetType {
Vm,
Container,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum SnapshotState {
Creating,
Ready,
Invalid,
}
#[derive(Debug, Clone, Default)]
pub struct SnapshotCreateOptions {
pub name: Option<String>,
pub description: Option<String>,
pub labels: HashMap<String, String>,
pub parent: Option<String>,
pub pause_vm: bool,
pub compress: bool,
}
pub struct VmSnapshotContext {
pub vcpu_snapshots: Vec<VcpuSnapshot>,
pub device_snapshots: Vec<DeviceSnapshot>,
pub memory_size: u64,
pub memory_reader: MemoryReaderFn,
}
pub struct VmRestoreData {
pub vm_snapshot: VmSnapshot,
pub memory: Vec<u8>,
}
impl VmRestoreData {
#[must_use]
pub fn vcpu_snapshots(&self) -> &[VcpuSnapshot] {
&self.vm_snapshot.vcpus
}
#[must_use]
pub fn device_snapshots(&self) -> &[arcbox_hypervisor::DeviceSnapshot] {
&self.vm_snapshot.devices
}
#[must_use]
pub const fn memory_size(&self) -> u64 {
self.vm_snapshot.total_memory
}
#[must_use]
pub fn memory(&self) -> &[u8] {
&self.memory
}
#[must_use]
pub const fn was_compressed(&self) -> bool {
self.vm_snapshot.compressed
}
}
pub struct SnapshotManager {
base_dir: PathBuf,
snapshots: std::sync::RwLock<HashMap<String, SnapshotInfo>>,
restore_cache: std::sync::RwLock<HashMap<String, VmRestoreData>>,
}
#[cfg(all(target_os = "linux", feature = "criu"))]
#[derive(Debug, Clone, Default)]
pub struct CriuCheckpointOptions {
pub leave_running: bool,
pub file_locks: bool,
pub tcp_established: bool,
}