firecracker_sdk/api/startup/
mod.rs

1use std::{
2    collections::HashMap,
3    path::{Path, PathBuf},
4};
5
6use anyhow::Result;
7use serde::Serialize;
8
9use crate::{
10    domain::config::{BootSource, Drives, FirecrackerConfiguration},
11    infrastructure::{fs::FileManager, process::FirecrackerProcess, s3::S3Downloader},
12};
13
14/// A structure for configuring the launch of FirecrackerVM. Helps to preconfigure and start the virtual machine.
15///
16/// Note: Firecracker must be installed globally.
17///
18/// Exemple:
19/// ```no_compile
20/// let process = FirecrackerStartup::new()
21///     .set_api_socket("/tmp/some.socket")
22///     .download_rootfs(true)
23///     .download_kernel(true)
24///     .start().await.unwrap();
25/// ```
26#[derive(Serialize)]
27pub struct FirecrackerStartup {
28    api_socket: PathBuf,
29    download_kernel: bool,
30    download_rootfs: bool,
31}
32
33impl FirecrackerStartup {
34    /// Creates a new instance of FirecrackerStartup
35    pub fn new() -> Self {
36        Self {
37            api_socket: PathBuf::from("/tmp/firecracker.socket"),
38            download_kernel: false,
39            download_rootfs: false,
40        }
41    }
42
43    /// Set the --api-sock startup argument with the path to the unix socket
44    ///
45    /// Note: For the best documentation, please refer to [here](https://github.com/firecracker-microvm/firecracker/blob/main/docs/getting-started.md).
46    pub fn set_api_socket<P: AsRef<Path>>(mut self, path: P) -> Self {
47        self.api_socket = path.as_ref().to_path_buf();
48        self
49    }
50
51    /// Returns the --api-sock startup argument with the path to the unix socket
52    ///
53    /// Note: For the best documentation, please refer to [here](https://github.com/firecracker-microvm/firecracker/blob/main/docs/getting-started.md).
54    pub fn get_api_socket(&self) -> &PathBuf {
55        &self.api_socket
56    }
57
58    /// Flag to download the latest kernel version for microVM
59    pub fn download_kernel(mut self, flag: bool) -> Self {
60        self.download_kernel = flag;
61        self
62    }
63    /// Flag to download the ubuntu-22.04.ext4 for microVM
64    pub fn download_rootfs(mut self, flag: bool) -> Self {
65        self.download_rootfs = flag;
66        self
67    }
68
69    /// Starts a VM with specified parameters
70    /// Returns a structure for working with the Firecracker process
71    pub async fn start(self) -> Result<FirecrackerProcess> {
72        let fs = FileManager::default();
73        let s3 = S3Downloader::default();
74        let kernel_path = fs.resolve_kernel_path(self.download_kernel, &s3).await?;
75        let rootfs_path = fs.resolve_rootfs_path(self.download_rootfs, &s3).await?;
76
77        FirecrackerProcess::new(FirecrackerConfiguration {
78            startup_config: self,
79            boot_source: BootSource {
80                kernel_image_path: kernel_path,
81                boot_args: HashMap::new(),
82            },
83            drives: Drives {
84                drive_id: "rootfs".into(),
85                path_on_host: rootfs_path,
86                is_root_device: true,
87                is_read_only: false,
88            },
89        })
90        .await
91    }
92}
93
94impl Default for FirecrackerStartup {
95    fn default() -> Self {
96        Self::new()
97    }
98}