Prepare, configure, and manage Firecracker microVMs in seconds!
virtualization linux microvm firecracker
at main 44 lines 1.4 kB view raw
1use crate::{command::run_command, constants::BRIDGE_IP}; 2use anyhow::Result; 3 4pub fn configure_guest_network(key_path: &str, guest_ip: &str, is_nixos: bool) -> Result<()> { 5 println!("[+] Configuring network in guest..."); 6 const MAX_RETRIES: u32 = 500; 7 let mut retries = 0; 8 loop { 9 if run_command( 10 "ssh", 11 &[ 12 "-i", 13 key_path, 14 "-o", 15 "StrictHostKeyChecking=no", 16 "-o", 17 "UserKnownHostsFile=/dev/null", 18 &format!("root@{}", guest_ip), 19 &match is_nixos { 20 true => "uname -a".into(), 21 false => format!("echo 'nameserver {}' > /etc/resolv.conf", BRIDGE_IP), 22 }, 23 ], 24 false, 25 ) 26 .is_ok() 27 || retries >= MAX_RETRIES 28 { 29 if retries >= MAX_RETRIES { 30 println!( 31 "[-] Max retries reached. Failed to configure network in guest. {}", 32 guest_ip 33 ); 34 } else { 35 println!("[+] Network configured in guest."); 36 } 37 break; 38 } 39 println!("[-] Waiting for ssh to be available..."); 40 std::thread::sleep(std::time::Duration::from_millis(100)); 41 retries += 1; 42 } 43 Ok(()) 44}