]> git.proxmox.com Git - pve-container.git/commitdiff
api: destroy: add 'destroy-unreferenced-disks' parameter
authorThomas Lamprecht <t.lamprecht@proxmox.com>
Mon, 25 Jan 2021 19:20:13 +0000 (20:20 +0100)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Mon, 25 Jan 2021 19:20:15 +0000 (20:20 +0100)
Making the CT and VM API more stream lined. But, we do not use the
same dangerous default than the VM API does, as we only have it there
for backward compatibility.

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

index 9ecfb12d3ea0c6f85a61c8ba7e950c8ccb1a5522..cb62bf4231fda2f1d30ab4d3aac70c1a6e23c714 100644 (file)
@@ -633,6 +633,12 @@ __PACKAGE__->register_method({
                default => 0,
                optional => 1,
            },
+           'destroy-unreferenced-disks' => {
+               type => 'boolean',
+               description => "If set, destroy additionally all disks with the VMID from all"
+                   ." enabled storages which are not referenced in the config.",
+               optional => 1,
+           },
        },
     },
     returns => {
@@ -688,7 +694,13 @@ __PACKAGE__->register_method({
            }
 
            my $storage_cfg = cfs_read_file("storage.cfg");
-           PVE::LXC::destroy_lxc_container($storage_cfg, $vmid, $conf, { lock => 'destroyed' });
+           PVE::LXC::destroy_lxc_container(
+               $storage_cfg,
+               $vmid,
+               $conf,
+               { lock => 'destroyed' },
+               $param->{'destroy-unreferenced-disks'},
+           );
 
            PVE::AccessControl::remove_vm_access($vmid);
            PVE::Firewall::remove_vmfw_conf($vmid);
index 0b4ab069bf9f92a717fee255c61ad4fdebd5e39c..112bd0c7b0f537d8680979c24606ca1c7fd8984e 100644 (file)
@@ -829,13 +829,22 @@ sub delete_mountpoint_volume {
 }
 
 sub destroy_lxc_container {
-    my ($storage_cfg, $vmid, $conf, $replacement_conf) = @_;
+    my ($storage_cfg, $vmid, $conf, $replacement_conf, $purge_unreferenced) = @_;
 
     PVE::LXC::Config->foreach_volume_full($conf, {include_unused => 1}, sub {
        my ($ms, $mountpoint) = @_;
        delete_mountpoint_volume($storage_cfg, $vmid, $mountpoint->{volume});
     });
 
+    if ($purge_unreferenced) { # also remove unreferenced disk
+       my $vmdisks = PVE::Storage::vdisk_list($storage_cfg, undef, $vmid);
+       PVE::Storage::foreach_volid($vmdisks, sub {
+           my ($volid, $sid, $volname, $d) = @_;
+           eval { PVE::Storage::vdisk_free($storage_cfg, $volid) };
+           warn $@ if $@;
+       });
+    }
+
     rmdir "/var/lib/lxc/$vmid/rootfs";
     unlink "/var/lib/lxc/$vmid/config";
     rmdir "/var/lib/lxc/$vmid";