firecracker_sdk/api/startup/
mod.rs1use 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#[derive(Serialize)]
27pub struct FirecrackerStartup {
28 api_socket: PathBuf,
29 download_kernel: bool,
30 download_rootfs: bool,
31}
32
33impl FirecrackerStartup {
34 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 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 pub fn get_api_socket(&self) -> &PathBuf {
55 &self.api_socket
56 }
57
58 pub fn download_kernel(mut self, flag: bool) -> Self {
60 self.download_kernel = flag;
61 self
62 }
63 pub fn download_rootfs(mut self, flag: bool) -> Self {
65 self.download_rootfs = flag;
66 self
67 }
68
69 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}