Skip to main content

vm_rs/setup/
mod.rs

1//! VM setup — image preparation, cloud-init seed ISO generation, SSH keys.
2//!
3//! The user specifies what OS image they want. vm-rs provides:
4//! - A catalog of known cloud images (Ubuntu, Alpine) with verified URLs
5//! - `prepare_image()` to download and cache images by spec
6//! - `create_seed_iso()` to generate per-VM cloud-init configuration
7//! - `generate_ssh_key()` to create SSH key pairs for VM access
8//!
9//! Users can also bring their own kernel + disk image — just point VmConfig at the files.
10
11mod image;
12mod seed;
13mod ssh;
14
15use std::path::PathBuf;
16
17pub use image::{prepare_image, Arch, ImageSpec, PreparedImage};
18pub use seed::{
19    create_seed_iso, HealthCheckConfig, NicConfig, ProcessConfig, SeedConfig, VolumeMountConfig,
20};
21pub use ssh::generate_ssh_key;
22
23/// Default data directory for vm-rs assets.
24///
25/// Reads `VMRS_DATA_DIR` env var, falls back to `~/.vm-rs/`.
26pub fn data_dir() -> Result<PathBuf, SetupError> {
27    if let Ok(dir) = std::env::var("VMRS_DATA_DIR") {
28        return Ok(PathBuf::from(dir));
29    }
30    let home = std::env::var("HOME").map_err(|_| {
31        SetupError::Config(
32            "HOME environment variable not set. Set VMRS_DATA_DIR explicitly.".into(),
33        )
34    })?;
35    Ok(PathBuf::from(home).join(".vm-rs"))
36}
37
38// ---------------------------------------------------------------------------
39// Error types
40// ---------------------------------------------------------------------------
41
42/// Setup errors.
43#[derive(Debug, thiserror::Error)]
44pub enum SetupError {
45    #[error("I/O error: {0}")]
46    Io(#[from] std::io::Error),
47
48    #[error("ISO creation failed: {0}")]
49    IsoCreation(String),
50
51    #[error("asset download failed: {0}")]
52    AssetDownload(String),
53
54    #[error("configuration error: {0}")]
55    Config(String),
56
57    #[error("unsupported image: {0}")]
58    UnsupportedImage(String),
59}