···11+use std::process;
22+13use anyhow::Error;
24use firecracker_process::stop;
55+use firecracker_state::repo;
36use firecracker_vm::types::VmOptions;
47use glob::glob;
58use owo_colors::OwoColorize;
99+1010+use crate::command::run_command;
611712pub async fn reset(options: VmOptions) -> Result<(), Error> {
88- println!(
99- "Are you sure you want to reset? This will remove all ext4 files. Type '{}' to confirm:",
1010- "yes".bright_green()
1111- );
1313+ let name = options
1414+ .api_socket
1515+ .trim_start_matches("/tmp/firecracker-")
1616+ .trim_end_matches(".sock")
1717+ .to_string();
1818+1919+ if options.api_socket.is_empty() {
2020+ println!(
2121+ "Are you sure you want to reset? This will remove all *.img files. Type '{}' to confirm:",
2222+ "yes".bright_green()
2323+ );
2424+ let mut input = String::new();
2525+ std::io::stdin()
2626+ .read_line(&mut input)
2727+ .map_err(|e| Error::msg(format!("Failed to read input: {}", e)))?;
2828+ let input = input.trim();
2929+3030+ if input != "yes" {
3131+ println!("Reset cancelled.");
3232+ return Ok(());
3333+ }
3434+3535+ stop(Some(name)).await?;
3636+3737+ let app_dir = crate::config::get_config_dir()?;
3838+ let img_file = glob(format!("{}/*.img", app_dir).as_str())
3939+ .map_err(|e| Error::msg(format!("Failed to find img file: {}", e)))?;
4040+4141+ for file in img_file {
4242+ if let Ok(path) = file {
4343+ run_command("rm", &[path.to_str().unwrap_or_default()], true)?;
4444+ }
4545+ }
4646+4747+ println!("[+] Reset complete. All *.img files have been removed.");
4848+ println!(
4949+ "[+] You can now run '{}' to start a new Firecracker MicroVM.",
5050+ "fireup".bright_green()
5151+ );
5252+ return Ok(());
5353+ }
5454+5555+ 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());
1256 let mut input = String::new();
1357 std::io::stdin()
1458 .read_line(&mut input)
1559 .map_err(|e| Error::msg(format!("Failed to read input: {}", e)))?;
1660 let input = input.trim();
1717-1861 if input != "yes" {
1962 println!("Reset cancelled.");
2063 return Ok(());
2164 }
22652323- let name = options
2424- .api_socket
2525- .trim_start_matches("/tmp/firecracker-")
2626- .trim_end_matches(".sock")
2727- .to_string();
6666+ let pool = firecracker_state::create_connection_pool().await?;
28672929- stop(Some(name)).await?;
6868+ let vm = repo::virtual_machine::find_by_api_socket(&pool, &options.api_socket).await?;
6969+ if vm.is_none() {
7070+ println!("[!] No virtual machine found with name: {}", name);
7171+ process::exit(1);
7272+ }
30733131- let app_dir = crate::config::get_config_dir()?;
3232- let ext4_file = glob(format!("{}/*.ext4", app_dir).as_str())
3333- .map_err(|e| Error::msg(format!("Failed to find ext4 file: {}", e)))?;
7474+ let vm = vm.unwrap();
7575+ stop(Some(vm.name.clone())).await?;
34763535- for file in ext4_file {
3636- if let Ok(path) = file {
3737- std::fs::remove_file(path)
3838- .map_err(|e| Error::msg(format!("Failed to remove file: {}", e)))?;
3939- }
7777+ if let Some(rootfs) = &vm.rootfs {
7878+ run_command("rm", &[rootfs], true)?;
4079 }
41804242- println!("[+] Reset complete. All ext4 files have been removed.");
8181+ println!("[+] Reset complete. Associated *.img files have been removed.");
4382 println!(
4483 "[+] You can now run '{}' to start a new Firecracker MicroVM.",
4584 "fireup".bright_green()