[pve-devel] [PATCH 8/8] add_unused_volume corrections.

Derumier Alexandre aderumier at odiso.com
Fri Jan 20 11:42:09 CET 2012


Currentle add_unused_volume not working when delete more than 1 device.

As it reading the vm conf file each time, it always assign the same id to each delete device.

So I add a new sub, load_unused_volume to load in memory the unused volume
and then not passing config anymore to add_unused_volume

Signed-off-by: Derumier Alexandre <aderumier at odiso.com>
---
 PVE/API2/Qemu.pm  |   11 ++++++++---
 PVE/QemuServer.pm |   28 ++++++++++++++++++++++++++--
 2 files changed, 34 insertions(+), 5 deletions(-)

diff --git a/PVE/API2/Qemu.pm b/PVE/API2/Qemu.pm
index 8b972a3..c519a32 100644
--- a/PVE/API2/Qemu.pm
+++ b/PVE/API2/Qemu.pm
@@ -501,6 +501,8 @@ __PACKAGE__->register_method({
 
 	    PVE::Cluster::log_msg('info', $user, "update VM $vmid: " . join (' ', @paramarr));
 
+	    PVE::QemuServer::load_unused_volume($conf, $param);
+
             my @newdelete = ();
             foreach my $opt (PVE::Tools::split_list($delete)) {
                if(PVE::QemuServer::vm_deviceunplug($vmid, $conf, $opt)) {
@@ -531,7 +533,7 @@ __PACKAGE__->register_method({
 		    my ($path, $owner);
 		    eval { ($path, $owner) = PVE::Storage::path($storecfg, $old_drive->{file}); };
 		    if ($owner && ($owner == $vmid)) {
-			PVE::QemuServer::add_unused_volume($conf, $param, $old_drive->{file});
+			PVE::QemuServer::add_unused_volume($param, $old_drive->{file});
 		    }
 		}
 	    }
@@ -558,7 +560,7 @@ __PACKAGE__->register_method({
 				if ($force) {
 				    push @$vollist, $volid;
 				} else {
-				    PVE::QemuServer::add_unused_volume($conf, $param, $volid);
+				    PVE::QemuServer::add_unused_volume($param, $volid);
 				}
 			    }
 			}
@@ -574,13 +576,16 @@ __PACKAGE__->register_method({
 
             #hotplug disks
             foreach my $opt (keys %$param) {
-                if($opt =~ m/^(scsi|virtio)(\d+)$/) {
+                if (PVE::QemuServer::valid_drivename($opt)) {
                    my $device = PVE::QemuServer::parse_drive($opt, $param->{$opt});
                    if(!PVE::QemuServer::vm_deviceplug($storecfg, $conf, $vmid, $opt, $device)) {
                        $unset->{$opt} = 1;
                        PVE::QemuServer::add_unused_volume($param, $device->{file});
                        push(@hotplugerr, $opt);
                    }
+                   else {
+                       PVE::QemuServer::del_unused_volume($param, $device->{file});
+                   }
                 }
             }
 
diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index f714cc8..865938b 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -1019,12 +1019,12 @@ sub add_random_macs {
 }
 
 sub add_unused_volume {
-    my ($config, $res, $volid) = @_;
+    my ($res, $volid) = @_;
 
     my $key;
     for (my $ind = $MAX_UNUSED_DISKS - 1; $ind >= 0; $ind--) {
 	my $test = "unused$ind";
-	if (my $vid = $config->{$test}) {
+	if (my $vid = $res->{$test}) {
 	    return if $vid eq $volid; # do not add duplicates
 	} else {
 	    $key = $test;
@@ -1036,6 +1036,30 @@ sub add_unused_volume {
     $res->{$key} = $volid;
 }
 
+sub del_unused_volume {
+    my ($res, $volid) = @_;
+
+    my $key;
+    for (my $ind = $MAX_UNUSED_DISKS - 1; $ind >= 0; $ind--) {
+	my $test = "unused$ind";
+	if (my $vid = $res->{$test}) {
+ 	    delete $res->{$test} if $vid eq $volid; 
+	} 
+    }
+}
+
+sub load_unused_volume {
+    my ($config, $res) = @_;
+ 
+    my $key;
+    for (my $ind = $MAX_UNUSED_DISKS - 1; $ind >= 0; $ind--) {
+        my $key = "unused$ind";
+        if (my $vid = $config->{$key}) {
+            $res->{$key} = $vid;
+        }
+    }
+}
+
 # fixme: remove all thos $noerr parameters?
 
 PVE::JSONSchema::register_format('pve-qm-bootdisk', \&verify_bootdisk);
-- 
1.7.2.5



More information about the pve-devel mailing list