···001use anyhow::Error;
2use firecracker_process::stop;
03use firecracker_vm::types::VmOptions;
4use glob::glob;
5use owo_colors::OwoColorize;
0067pub async fn reset(options: VmOptions) -> Result<(), Error> {
8- println!(
9- "Are you sure you want to reset? This will remove all ext4 files. Type '{}' to confirm:",
10- "yes".bright_green()
11- );
00000000000000000000000000000000000000012 let mut input = String::new();
13 std::io::stdin()
14 .read_line(&mut input)
15 .map_err(|e| Error::msg(format!("Failed to read input: {}", e)))?;
16 let input = input.trim();
17-18 if input != "yes" {
19 println!("Reset cancelled.");
20 return Ok(());
21 }
2223- let name = options
24- .api_socket
25- .trim_start_matches("/tmp/firecracker-")
26- .trim_end_matches(".sock")
27- .to_string();
2829- stop(Some(name)).await?;
00003031- let app_dir = crate::config::get_config_dir()?;
32- let ext4_file = glob(format!("{}/*.ext4", app_dir).as_str())
33- .map_err(|e| Error::msg(format!("Failed to find ext4 file: {}", e)))?;
3435- for file in ext4_file {
36- if let Ok(path) = file {
37- std::fs::remove_file(path)
38- .map_err(|e| Error::msg(format!("Failed to remove file: {}", e)))?;
39- }
40 }
4142- println!("[+] Reset complete. All ext4 files have been removed.");
43 println!(
44 "[+] You can now run '{}' to start a new Firecracker MicroVM.",
45 "fireup".bright_green()
···1+use std::process;
2+3use anyhow::Error;
4use firecracker_process::stop;
5+use firecracker_state::repo;
6use firecracker_vm::types::VmOptions;
7use glob::glob;
8use owo_colors::OwoColorize;
9+10+use crate::command::run_command;
1112pub async fn reset(options: VmOptions) -> Result<(), Error> {
13+ let name = options
14+ .api_socket
15+ .trim_start_matches("/tmp/firecracker-")
16+ .trim_end_matches(".sock")
17+ .to_string();
18+19+ if options.api_socket.is_empty() {
20+ println!(
21+ "Are you sure you want to reset? This will remove all *.img files. Type '{}' to confirm:",
22+ "yes".bright_green()
23+ );
24+ let mut input = String::new();
25+ std::io::stdin()
26+ .read_line(&mut input)
27+ .map_err(|e| Error::msg(format!("Failed to read input: {}", e)))?;
28+ let input = input.trim();
29+30+ if input != "yes" {
31+ println!("Reset cancelled.");
32+ return Ok(());
33+ }
34+35+ stop(Some(name)).await?;
36+37+ let app_dir = crate::config::get_config_dir()?;
38+ let img_file = glob(format!("{}/*.img", app_dir).as_str())
39+ .map_err(|e| Error::msg(format!("Failed to find img file: {}", e)))?;
40+41+ for file in img_file {
42+ if let Ok(path) = file {
43+ run_command("rm", &[path.to_str().unwrap_or_default()], true)?;
44+ }
45+ }
46+47+ println!("[+] Reset complete. All *.img files have been removed.");
48+ println!(
49+ "[+] You can now run '{}' to start a new Firecracker MicroVM.",
50+ "fireup".bright_green()
51+ );
52+ return Ok(());
53+ }
54+55+ println!("Are you sure you want to reset the VM {}? This will remove its associated {} file. Type '{}' to confirm:", name.cyan(), "*.img".cyan(), "yes".bright_green());
56 let mut input = String::new();
57 std::io::stdin()
58 .read_line(&mut input)
59 .map_err(|e| Error::msg(format!("Failed to read input: {}", e)))?;
60 let input = input.trim();
061 if input != "yes" {
62 println!("Reset cancelled.");
63 return Ok(());
64 }
6566+ let pool = firecracker_state::create_connection_pool().await?;
00006768+ let vm = repo::virtual_machine::find_by_api_socket(&pool, &options.api_socket).await?;
69+ if vm.is_none() {
70+ println!("[!] No virtual machine found with name: {}", name);
71+ process::exit(1);
72+ }
7374+ let vm = vm.unwrap();
75+ stop(Some(vm.name.clone())).await?;
07677+ if let Some(rootfs) = &vm.rootfs {
78+ run_command("rm", &[rootfs], true)?;
00079 }
8081+ println!("[+] Reset complete. Associated *.img files have been removed.");
82 println!(
83 "[+] You can now run '{}' to start a new Firecracker MicroVM.",
84 "fireup".bright_green()