Prepare, configure, and manage Firecracker microVMs in seconds!
virtualization linux microvm firecracker
8
fork

Configure Feed

Select the types of activity you want to include in your feed.

add --vmlinux, --rootfs and --boot-args options

+92 -57
+3 -29
crates/firecracker-up/src/cmd/up.rs
··· 1 1 use std::thread; 2 2 3 3 use anyhow::Error; 4 - use firecracker_prepare::Distro; 4 + use firecracker_vm::types::VmOptions; 5 5 use owo_colors::OwoColorize; 6 6 7 7 use crate::command::run_command; 8 8 9 - #[derive(Default, Clone)] 10 - pub struct UpOptions { 11 - pub debian: Option<bool>, 12 - pub alpine: Option<bool>, 13 - pub ubuntu: Option<bool>, 14 - pub nixos: Option<bool>, 15 - pub vcpu: u16, 16 - pub memory: u16, 17 - } 18 - 19 - impl Into<Distro> for UpOptions { 20 - fn into(self) -> Distro { 21 - if self.debian.unwrap_or(false) { 22 - Distro::Debian 23 - } else if self.alpine.unwrap_or(false) { 24 - Distro::Alpine 25 - } else if self.nixos.unwrap_or(false) { 26 - Distro::NixOS 27 - } else if self.ubuntu.unwrap_or(true) { 28 - Distro::Ubuntu 29 - } else { 30 - panic!("No valid distribution option provided."); 31 - } 32 - } 33 - } 34 - 35 - pub fn up(options: UpOptions) -> Result<(), Error> { 9 + pub fn up(options: VmOptions) -> Result<(), Error> { 36 10 check_kvm_support()?; 37 11 38 12 firecracker_process::start()?; ··· 46 20 } 47 21 48 22 firecracker_prepare::prepare(options.clone().into())?; 49 - firecracker_vm::setup(options.clone().into(), options.vcpu, options.memory)?; 23 + firecracker_vm::setup(&options)?; 50 24 Ok(()) 51 25 } 52 26
+41 -19
crates/firecracker-up/src/main.rs
··· 1 1 use anyhow::Result; 2 - use clap::{arg, Command}; 2 + use clap::{arg, Arg, Command}; 3 + use firecracker_vm::types::VmOptions; 3 4 use owo_colors::OwoColorize; 4 5 5 - use crate::cmd::{ 6 - down::down, 7 - logs::logs, 8 - reset::reset, 9 - ssh::ssh, 10 - status::status, 11 - up::{up, UpOptions}, 12 - }; 6 + use crate::cmd::{down::down, logs::logs, reset::reset, ssh::ssh, status::status, up::up}; 13 7 14 8 pub mod cmd; 15 9 pub mod command; ··· 34 28 .about(&banner) 35 29 .subcommand( 36 30 Command::new("up") 37 - .arg(arg!(--debian "Prepare Debian rootfs").default_value("false")) 38 - .arg(arg!(--alpine "Prepare Alpine rootfs").default_value("false")) 39 - .arg(arg!(--nixos "Prepare NixOS rootfs").default_value("false")) 40 - .arg(arg!(--ubuntu "Prepare Ubuntu rootfs").default_value("true")) 31 + .arg(arg!(--debian "Prepare Debian MicroVM").default_value("false")) 32 + .arg(arg!(--alpine "Prepare Alpine MicroVM").default_value("false")) 33 + .arg(arg!(--nixos "Prepare NixOS MicroVM").default_value("false")) 34 + .arg(arg!(--ubuntu "Prepare Ubuntu MicroVM").default_value("true")) 41 35 .arg(arg!(--vcpu <n> "Number of vCPUs")) 42 36 .arg(arg!(--memory <m> "Memory size in MiB")) 37 + .arg(arg!(--vmlinux <path> "Path to the kernel image")) 38 + .arg(arg!(--rootfs <path> "Path to the root filesystem image")) 39 + .arg( 40 + Arg::new("boot-args") 41 + .long("boot-args") 42 + .value_name("ARGS") 43 + .help("Override boot arguments"), 44 + ) 43 45 .about("Start Firecracker MicroVM"), 44 46 ) 45 47 .subcommand(Command::new("down").about("Stop Firecracker MicroVM")) ··· 56 58 ) 57 59 .subcommand(Command::new("ssh").about("SSH into the Firecracker MicroVM")) 58 60 .subcommand(Command::new("reset").about("Reset the Firecracker MicroVM")) 59 - .arg(arg!(--debian "Prepare Debian rootfs").default_value("false")) 60 - .arg(arg!(--alpine "Prepare Alpine rootfs").default_value("false")) 61 - .arg(arg!(--nixos "Prepare NixOS rootfs").default_value("false")) 62 - .arg(arg!(--ubuntu "Prepare Ubuntu rootfs").default_value("true")) 61 + .arg(arg!(--debian "Prepare Debian MicroVM").default_value("false")) 62 + .arg(arg!(--alpine "Prepare Alpine MicroVM").default_value("false")) 63 + .arg(arg!(--nixos "Prepare NixOS MicroVM").default_value("false")) 64 + .arg(arg!(--ubuntu "Prepare Ubuntu MicroVM").default_value("true")) 63 65 .arg(arg!(--vcpu <n> "Number of vCPUs")) 64 66 .arg(arg!(--memory <m> "Memory size in MiB")) 67 + .arg(arg!(--vmlinux <path> "Path to the kernel image")) 68 + .arg(arg!(--rootfs <path> "Path to the root filesystem image")) 69 + .arg( 70 + Arg::new("boot-args") 71 + .long("boot-args") 72 + .value_name("ARGS") 73 + .help("Override boot arguments"), 74 + ) 65 75 } 66 76 67 77 fn main() -> Result<()> { ··· 77 87 .get_one::<String>("memory") 78 88 .map(|s| s.parse::<u16>().unwrap()) 79 89 .unwrap_or(512); 80 - let options = UpOptions { 90 + let vmlinux = matches.get_one::<String>("vmlinux").cloned(); 91 + let rootfs = matches.get_one::<String>("rootfs").cloned(); 92 + let bootargs = matches.get_one::<String>("boot-args").cloned(); 93 + let options = VmOptions { 81 94 debian: args.get_one::<bool>("debian").copied(), 82 95 alpine: args.get_one::<bool>("alpine").copied(), 83 96 ubuntu: args.get_one::<bool>("ubuntu").copied(), 84 97 nixos: args.get_one::<bool>("nixos").copied(), 85 98 vcpu, 86 99 memory, 100 + vmlinux, 101 + rootfs, 102 + bootargs, 87 103 }; 88 104 up(options)? 89 105 } ··· 108 124 .get_one::<String>("memory") 109 125 .map(|s| s.parse::<u16>().unwrap()) 110 126 .unwrap_or(if nixos { 2048 } else { 512 }); 127 + let vmlinux = matches.get_one::<String>("vmlinux").cloned(); 128 + let rootfs = matches.get_one::<String>("rootfs").cloned(); 129 + let bootargs = matches.get_one::<String>("boot-args").cloned(); 111 130 112 - let options = UpOptions { 131 + let options = VmOptions { 113 132 debian: Some(debian), 114 133 alpine: Some(alpine), 115 134 ubuntu: Some(ubuntu), 116 135 nixos: Some(nixos), 117 136 vcpu, 118 137 memory, 138 + vmlinux, 139 + rootfs, 140 + bootargs, 119 141 }; 120 142 up(options)? 121 143 }
+13 -6
crates/firecracker-vm/src/firecracker.rs
··· 1 1 use crate::constants::{API_SOCKET, FC_MAC, TAP_DEV}; 2 + use crate::types::VmOptions; 2 3 use anyhow::Result; 3 4 use firecracker_prepare::Distro; 4 5 use serde_json::json; ··· 14 15 kernel: &str, 15 16 rootfs: &str, 16 17 arch: &str, 17 - vcpu: u16, 18 - memory: u16, 18 + options: &VmOptions, 19 19 distro: Distro, 20 20 ) -> Result<()> { 21 21 configure_logger(logfile)?; 22 - setup_boot_source(kernel, arch, distro == Distro::NixOS)?; 22 + setup_boot_source(kernel, arch, distro == Distro::NixOS, &options)?; 23 23 setup_rootfs(rootfs)?; 24 24 setup_network_interface()?; 25 - setup_vcpu_and_memory(vcpu, memory)?; 25 + setup_vcpu_and_memory(options.vcpu, options.memory)?; 26 26 27 27 // Wait before starting instance 28 28 sleep(Duration::from_millis(15)); ··· 59 59 Ok(()) 60 60 } 61 61 62 - fn setup_boot_source(kernel: &str, arch: &str, is_nixos: bool) -> Result<()> { 62 + fn setup_boot_source(kernel: &str, arch: &str, is_nixos: bool, options: &VmOptions) -> Result<()> { 63 63 println!("[+] Setting boot source..."); 64 64 let mut boot_args = "console=ttyS0 reboot=k panic=1 pci=off".to_string(); 65 65 if arch == "aarch64" { ··· 70 70 boot_args = NIXOS_BOOT_ARGS.into(); 71 71 } 72 72 73 + if let Some(args) = &options.bootargs { 74 + boot_args = args.clone(); 75 + } 76 + 73 77 let payload = json!({ 74 - "kernel_image_path": kernel, 78 + "kernel_image_path": match &options.vmlinux { 79 + Some(path) => path.clone(), 80 + None => kernel.into(), 81 + }, 75 82 "boot_args": boot_args 76 83 }); 77 84 println!("{}", payload.to_string());
+5 -3
crates/firecracker-vm/src/lib.rs
··· 3 3 use owo_colors::OwoColorize; 4 4 use std::fs; 5 5 6 - use crate::config::get_config_dir; 6 + use crate::{config::get_config_dir, types::VmOptions}; 7 7 8 8 mod command; 9 9 mod config; ··· 11 11 mod firecracker; 12 12 mod guest; 13 13 mod network; 14 + pub mod types; 14 15 15 - pub fn setup(distro: Distro, vcpu: u16, memory: u16) -> Result<()> { 16 + pub fn setup(options: &VmOptions) -> Result<()> { 17 + let distro: Distro = options.clone().into(); 16 18 let app_dir = get_config_dir().with_context(|| "Failed to get configuration directory")?; 17 19 18 20 let logfile = format!("{}/firecracker.log", app_dir); ··· 73 75 let arch = command::run_command("uname", &["-m"], false)?.stdout; 74 76 let arch = String::from_utf8_lossy(&arch).trim().to_string(); 75 77 network::setup_network()?; 76 - firecracker::configure(&logfile, &kernel, &rootfs, &arch, vcpu, memory, distro)?; 78 + firecracker::configure(&logfile, &kernel, &rootfs, &arch, &options, distro)?; 77 79 78 80 if distro != Distro::NixOS { 79 81 guest::configure_guest_network(&key_name)?;
+30
crates/firecracker-vm/src/types.rs
··· 1 + use firecracker_prepare::Distro; 2 + 3 + #[derive(Default, Clone)] 4 + pub struct VmOptions { 5 + pub debian: Option<bool>, 6 + pub alpine: Option<bool>, 7 + pub ubuntu: Option<bool>, 8 + pub nixos: Option<bool>, 9 + pub vcpu: u16, 10 + pub memory: u16, 11 + pub vmlinux: Option<String>, 12 + pub rootfs: Option<String>, 13 + pub bootargs: Option<String>, 14 + } 15 + 16 + impl Into<Distro> for VmOptions { 17 + fn into(self) -> Distro { 18 + if self.debian.unwrap_or(false) { 19 + Distro::Debian 20 + } else if self.alpine.unwrap_or(false) { 21 + Distro::Alpine 22 + } else if self.nixos.unwrap_or(false) { 23 + Distro::NixOS 24 + } else if self.ubuntu.unwrap_or(true) { 25 + Distro::Ubuntu 26 + } else { 27 + panic!("No valid distribution option provided."); 28 + } 29 + } 30 + }