[pve-devel] [PATCH v2 container] Add to unused volumes only if really unused

Fabian Grünbichler f.gruenbichler at proxmox.com
Wed Feb 17 14:30:36 CET 2016


- add unused volumes for changed mpX and rootfs, not only
for deleted mpX.
- add check before adding to unused volumes in order to
prevent duplicate or false entries (which could lead to
deletion of still used volumes!).
---
 src/PVE/LXC.pm | 37 +++++++++++++++++++++++++++++++++----
 1 file changed, 33 insertions(+), 4 deletions(-)

diff --git a/src/PVE/LXC.pm b/src/PVE/LXC.pm
index 42f6cc3..09de0df 100644
--- a/src/PVE/LXC.pm
+++ b/src/PVE/LXC.pm
@@ -1197,6 +1197,21 @@ sub verify_searchdomain_list {
     return join(' ', @list);
 }
 
+sub is_used_volume {
+    my ($config, $volid) = @_;
+    my $used = 0;
+
+    foreach_mountpoint($config, sub {
+	my ($ms, $mountpoint) = @_;
+	return if $used;
+	if ($mountpoint->{type} eq 'volume' && $mountpoint->{volume} eq $volid) {
+	    $used = 1;
+	}
+    });
+
+    return $used;
+}
+
 sub add_unused_volume {
     my ($config, $volid) = @_;
 
@@ -1273,11 +1288,11 @@ sub update_pct_config {
 	    } elsif ($opt =~ m/^mp(\d+)$/) {
 		next if $hotplug_error->($opt);
 		check_protection($conf, "can't remove CT $vmid drive '$opt'");
-		my $mountpoint = parse_ct_mountpoint($conf->{$opt});
-		if ($mountpoint->{type} eq 'volume') {
-		    add_unused_volume($conf, $mountpoint->{volume})
-		}
+		my $mp = parse_ct_mountpoint($conf->{$opt});
 		delete $conf->{$opt};
+		if ($mp->{type} eq 'volume' && !is_used_volume($conf, $mp->{volume})) {
+		    add_unused_volume($conf, $mp->{volume});
+		}
 	    } elsif ($opt eq 'unprivileged') {
 		die "unable to delete read-only option: '$opt'\n";
 	    } else {
@@ -1356,12 +1371,26 @@ sub update_pct_config {
         } elsif ($opt =~ m/^mp(\d+)$/) {
 	    next if $hotplug_error->($opt);
 	    check_protection($conf, "can't update CT $vmid drive '$opt'");
+	    my $old = $conf->{$opt};
 	    $conf->{$opt} = $value;
+	    if (defined($old)) {
+		my $mp = parse_ct_mountpoint($old);
+		if ($mp->{type} eq 'volume' && !is_used_volume($conf, $mp->{volume})) {
+		    add_unused_volume($conf, $mp->{volume});
+		}
+	    }
 	    $new_disks = 1;
         } elsif ($opt eq 'rootfs') {
 	    next if $hotplug_error->($opt);
 	    check_protection($conf, "can't update CT $vmid drive '$opt'");
+	    my $old = $conf->{$opt};
 	    $conf->{$opt} = $value;
+	    if (defined($old)) {
+		my $mp = parse_ct_rootfs($old);
+		if ($mp->{type} eq 'volume' && !is_used_volume($conf, $mp->{volume})) {
+		    add_unused_volume($conf, $mp->{volume});
+		}
+	    }
 	} elsif ($opt eq 'unprivileged') {
 	    die "unable to modify read-only option: '$opt'\n";
 	} else {
-- 
2.1.4





More information about the pve-devel mailing list