From d51b84ffcc347ac7ab4e3418ec63d30cd421e968 Mon Sep 17 00:00:00 2001 From: Thomas Lamprecht Date: Sat, 25 Apr 2020 17:44:19 +0200 Subject: [PATCH] api/destroy: add force to allow destroying running CT Convenient and naturally opt-in Signed-off-by: Thomas Lamprecht --- src/PVE/API2/LXC.pm | 19 ++++++++++++++++--- src/PVE/API2/LXC/Status.pm | 3 --- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/PVE/API2/LXC.pm b/src/PVE/API2/LXC.pm index ac80eb2..148ba6a 100644 --- a/src/PVE/API2/LXC.pm +++ b/src/PVE/API2/LXC.pm @@ -619,6 +619,12 @@ __PACKAGE__->register_method({ properties => { node => get_standard_option('pve-node'), vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid_stopped }), + force => { + type => 'boolean', + description => "Force destroy, even if running.", + default => 0, + optional => 1, + }, purge => { type => 'boolean', description => "Remove container from all related configurations." @@ -656,15 +662,22 @@ __PACKAGE__->register_method({ } my $running_error_msg = "unable to destroy CT $vmid - container is running\n"; - - die $running_error_msg if PVE::LXC::check_running($vmid); # check early + die $running_error_msg if !$param->{force} && PVE::LXC::check_running($vmid); # check early my $code = sub { # reload config after lock $conf = PVE::LXC::Config->load_config($vmid); PVE::LXC::Config->check_lock($conf); - die $running_error_msg if PVE::LXC::check_running($vmid); + if (PVE::LXC::check_running($vmid)) { + die $running_error_msg if !$param->{force}; + warn "forced to stop CT $vmid before destroying!\n"; + if (!$ha_managed) { + PVE::LXC::vm_stop($vmid, 1); + } else { + run_command(['ha-manager', 'crm-command', 'stop', "ct:$vmid", '120']); + } + } PVE::LXC::destroy_lxc_container($storage_cfg, $vmid, $conf, { lock => 'destroyed' }); diff --git a/src/PVE/API2/LXC/Status.pm b/src/PVE/API2/LXC/Status.pm index 03d13a3..89186ae 100644 --- a/src/PVE/API2/LXC/Status.pm +++ b/src/PVE/API2/LXC/Status.pm @@ -224,11 +224,8 @@ __PACKAGE__->register_method({ my ($param) = @_; my $rpcenv = PVE::RPCEnvironment::get(); - my $authuser = $rpcenv->get_user(); - my $node = extract_param($param, 'node'); - my $vmid = extract_param($param, 'vmid'); my $skiplock = extract_param($param, 'skiplock'); -- 2.39.2