]> git.proxmox.com Git - pve-container.git/commitdiff
api/destroy: add force to allow destroying running CT
authorThomas Lamprecht <t.lamprecht@proxmox.com>
Sat, 25 Apr 2020 15:44:19 +0000 (17:44 +0200)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Sat, 25 Apr 2020 15:44:21 +0000 (17:44 +0200)
Convenient and naturally opt-in

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
src/PVE/API2/LXC.pm
src/PVE/API2/LXC/Status.pm

index ac80eb285fdbe717bc1e8cb4beecdf17c456efce..148ba6aa5f2ec137578549b4131e6baeaf15e963 100644 (file)
@@ -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' });
 
index 03d13a3db6839c8ee6e1d952469fc128416fc514..89186ae54610070687ac881d84655c659ab351e2 100644 (file)
@@ -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');