Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add -f / --force option to delete #44

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ https://threedots.ovh/blog/2022/06/quick-look-at-rosetta-on-linux/
if force_x86 {
_ = fs::create_dir(format!("{}/.rosetta", rootfs));
}
umount_container(cfg, &vmcfg).unwrap();
umount_container(cfg, &vmcfg, false).unwrap();

cfg.vmconfig_map.insert(name.clone(), vmcfg);
confy::store(APP_NAME, cfg).unwrap();
Expand Down
5 changes: 3 additions & 2 deletions src/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use super::utils::{remove_container, umount_container};

pub fn delete(cfg: &mut KrunvmConfig, matches: &ArgMatches) {
let name = matches.value_of("NAME").unwrap();
let force = matches.is_present("force");

let vmcfg = match cfg.vmconfig_map.remove(name) {
None => {
Expand All @@ -16,8 +17,8 @@ pub fn delete(cfg: &mut KrunvmConfig, matches: &ArgMatches) {
Some(vmcfg) => vmcfg,
};

umount_container(cfg, &vmcfg).unwrap();
remove_container(cfg, &vmcfg).unwrap();
umount_container(cfg, &vmcfg, force).unwrap();
remove_container(cfg, &vmcfg, force).unwrap();

confy::store(APP_NAME, &cfg).unwrap();
}
20 changes: 14 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,12 +247,20 @@ fn main() {
),
)
.subcommand(
App::new("delete").about("Delete an existing microVM").arg(
Arg::with_name("NAME")
.help("Name of the microVM to be deleted")
.required(true)
.index(1),
),
App::new("delete")
.about("Delete an existing microVM")
.arg(
Arg::with_name("NAME")
.help("Name of the microVM to be deleted")
.required(true)
.index(1),
)
.arg(
Arg::with_name("force")
.long("force")
.short("f")
.help("Force deletion even in case of buildah error"),
),
)
.subcommand(
App::new("list").about("List microVMs").arg(
Expand Down
4 changes: 2 additions & 2 deletions src/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ pub fn start(cfg: &KrunvmConfig, matches: &ArgMatches) {
Some(vmcfg) => vmcfg,
};

umount_container(cfg, vmcfg).expect("Error unmounting container");
umount_container(cfg, vmcfg, false).expect("Error unmounting container");
let rootfs = mount_container(cfg, vmcfg).expect("Error mounting container");

let args: Vec<CString> = if cmd.is_some() {
Expand All @@ -213,5 +213,5 @@ pub fn start(cfg: &KrunvmConfig, matches: &ArgMatches) {

unsafe { exec_vm(vmcfg, &rootfs, cmd, args) };

umount_container(cfg, vmcfg).expect("Error unmounting container");
umount_container(cfg, vmcfg, false).expect("Error unmounting container");
}
20 changes: 16 additions & 4 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,11 @@ pub fn mount_container(cfg: &KrunvmConfig, vmcfg: &VmConfig) -> Result<String, s
}

#[allow(unused_variables)]
pub fn umount_container(cfg: &KrunvmConfig, vmcfg: &VmConfig) -> Result<(), std::io::Error> {
pub fn umount_container(
cfg: &KrunvmConfig,
vmcfg: &VmConfig,
force: bool,
) -> Result<(), std::io::Error> {
let mut args = get_buildah_args(cfg, BuildahCommand::Unmount);
args.push(vmcfg.container.clone());

Expand All @@ -230,14 +234,20 @@ pub fn umount_container(cfg: &KrunvmConfig, vmcfg: &VmConfig) -> Result<(), std:
"buildah returned an error: {}",
std::str::from_utf8(&output.stdout).unwrap()
);
std::process::exit(-1);
if !force {
std::process::exit(-1);
}
}

Ok(())
}

#[allow(unused_variables)]
pub fn remove_container(cfg: &KrunvmConfig, vmcfg: &VmConfig) -> Result<(), std::io::Error> {
pub fn remove_container(
cfg: &KrunvmConfig,
vmcfg: &VmConfig,
force: bool,
) -> Result<(), std::io::Error> {
let mut args = get_buildah_args(cfg, BuildahCommand::Remove);
args.push(vmcfg.container.clone());

Expand All @@ -263,7 +273,9 @@ pub fn remove_container(cfg: &KrunvmConfig, vmcfg: &VmConfig) -> Result<(), std:
"buildah returned an error: {}",
std::str::from_utf8(&output.stdout).unwrap()
);
std::process::exit(-1);
if !force {
std::process::exit(-1);
}
}

Ok(())
Expand Down