[pve-devel] [PATCH 1/7] vm_deviceplug|unplug : implement pending change

Alexandre Derumier aderumier at odiso.com
Tue Nov 18 13:39:46 CET 2014


also cleanup intendation

Signed-off-by: Alexandre Derumier <aderumier at odiso.com>
---
 PVE/QemuServer.pm |   93 ++++++++++++++++++++++++++++-------------------------
 1 file changed, 49 insertions(+), 44 deletions(-)

diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index e43a228..015bb29 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -2994,18 +2994,18 @@ sub vm_devices_list {
 }
 
 sub vm_deviceplug {
-    my ($storecfg, $conf, $vmid, $deviceid, $device) = @_;
-
-    return 1 if !check_running($vmid);
+    my ($storecfg, $conf, $vmid, $deviceid, $device, $optvalue) = @_;
 
     my $q35 = machine_type_is_q35($conf);
 
     if ($deviceid eq 'tablet') {
-	qemu_deviceadd($vmid, print_tabletdevice_full($conf));
+
+	eval { qemu_deviceadd($vmid, print_tabletdevice_full($conf->{pending}))};
 	return 1;
     }
 
-    return 1 if !$conf->{hotplug};
+    return undef if !$conf->{hotplug};
+    return undef if ($deviceid =~ m/^(ide|sata)(\d+)$/);
 
     my $devices_list = vm_devices_list($vmid);
     return 1 if defined($devices_list->{$deviceid});
@@ -3013,44 +3013,43 @@ sub vm_deviceplug {
     qemu_bridgeadd($storecfg, $conf, $vmid, $deviceid); #add bridge if we need it for the device
 
     if ($deviceid =~ m/^(virtio)(\d+)$/) {
-        return undef if !qemu_driveadd($storecfg, $vmid, $device);
-        my $devicefull = print_drivedevice_full($storecfg, $conf, $vmid, $device);
-        qemu_deviceadd($vmid, $devicefull);
-        if(!qemu_deviceaddverify($vmid, $deviceid)) {
-           qemu_drivedel($vmid, $deviceid);
-           return undef;
-        }
+	return undef if !qemu_driveadd($storecfg, $vmid, $device);
+	my $devicefull = print_drivedevice_full($storecfg, $conf->{pending}, $vmid, $device);
+	qemu_deviceadd($vmid, $devicefull);
+	if(!qemu_deviceaddverify($vmid, $deviceid)) {
+	    qemu_drivedel($vmid, $deviceid);
+	    return undef;
+	}
     }
 
     if ($deviceid =~ m/^(scsihw)(\d+)$/) {
-        my $scsihw = defined($conf->{scsihw}) ? $conf->{scsihw} : "lsi";
-        my $pciaddr = print_pci_addr($deviceid);
-        my $devicefull = "$scsihw,id=$deviceid$pciaddr";
-        qemu_deviceadd($vmid, $devicefull);
-        return undef if(!qemu_deviceaddverify($vmid, $deviceid));
+	my $scsihw = defined($conf->{scsihw}) ? $conf->{scsihw} : "lsi";
+	my $pciaddr = print_pci_addr($deviceid);
+	my $devicefull = "$scsihw,id=$deviceid$pciaddr";
+	qemu_deviceadd($vmid, $devicefull);
+	return undef if(!qemu_deviceaddverify($vmid, $deviceid));
     }
 
     if ($deviceid =~ m/^(scsi)(\d+)$/) {
-        return undef if !qemu_findorcreatescsihw($storecfg,$conf, $vmid, $device);
-        return undef if !qemu_driveadd($storecfg, $vmid, $device);
-        my $devicefull = print_drivedevice_full($storecfg, $conf, $vmid, $device);
-        if(!qemu_deviceadd($vmid, $devicefull)) {
-           qemu_drivedel($vmid, $deviceid);
-           return undef;
-        }
+	return undef if !qemu_findorcreatescsihw($storecfg,$conf, $vmid, $device);
+	return undef if !qemu_driveadd($storecfg, $vmid, $device);
+	my $devicefull = print_drivedevice_full($storecfg, $conf->{pending}, $vmid, $device);
+	if(!qemu_deviceadd($vmid, $devicefull)) {
+	    qemu_drivedel($vmid, $deviceid);
+	    return undef;
+	}
     }
 
     if ($deviceid =~ m/^(net)(\d+)$/) {
-        return undef if !qemu_netdevadd($vmid, $conf, $device, $deviceid);
-        my $netdevicefull = print_netdevice_full($vmid, $conf, $device, $deviceid);
-        qemu_deviceadd($vmid, $netdevicefull);
-        if(!qemu_deviceaddverify($vmid, $deviceid)) {
-           qemu_netdevdel($vmid, $deviceid);
-           return undef;
-        }
+	return undef if !qemu_netdevadd($vmid, $conf->{pending}, $device, $deviceid);
+	my $netdevicefull = print_netdevice_full($vmid, $conf->{pending}, $device, $deviceid);
+	qemu_deviceadd($vmid, $netdevicefull);
+	if(!qemu_deviceaddverify($vmid, $deviceid)) {
+	    qemu_netdevdel($vmid, $deviceid);
+	    return undef;
+	}
     }
 
-
     if (!$q35 && $deviceid =~ m/^(pci\.)(\d+)$/) {
 	my $bridgeid = $2;
 	my $pciaddr = print_pci_addr($deviceid);
@@ -3063,16 +3062,16 @@ sub vm_deviceplug {
 }
 
 sub vm_deviceunplug {
-    my ($vmid, $conf, $deviceid) = @_;
+    my ($vmid, $conf, $deviceid, $optvalue) = @_;
 
-    return 1 if !check_running ($vmid);
 
     if ($deviceid eq 'tablet') {
-	qemu_devicedel($vmid, $deviceid);
+	eval { qemu_devicedel($vmid, $deviceid) };
 	return 1;
     }
 
-    return 1 if !$conf->{hotplug};
+    return undef if !$conf->{hotplug};
+    return undef if ($deviceid =~ m/^(ide|sata)(\d+)$/);
 
     my $devices_list = vm_devices_list($vmid);
     return 1 if !defined($devices_list->{$deviceid});
@@ -3080,9 +3079,12 @@ sub vm_deviceunplug {
     die "can't unplug bootdisk" if $conf->{bootdisk} && $conf->{bootdisk} eq $deviceid;
 
     if ($deviceid =~ m/^(virtio)(\d+)$/) {
-        qemu_devicedel($vmid, $deviceid);
-        return undef if !qemu_devicedelverify($vmid, $deviceid);
-        return undef if !qemu_drivedel($vmid, $deviceid);
+	qemu_devicedel($vmid, $deviceid);
+	return undef if !qemu_devicedelverify($vmid, $deviceid);
+	return undef if !qemu_drivedel($vmid, $deviceid);
+
+	my $drive = PVE::QemuServer::parse_drive($deviceid, $conf->{$deviceid});
+	PVE::QemuServer::add_unused_volume($conf, $drive->{file});
     }
 
     if ($deviceid =~ m/^(lsi)(\d+)$/) {
@@ -3090,14 +3092,17 @@ sub vm_deviceunplug {
     }
 
     if ($deviceid =~ m/^(scsi)(\d+)$/) {
-        return undef if !qemu_devicedel($vmid, $deviceid);
-        return undef if !qemu_drivedel($vmid, $deviceid);
+	return undef if !qemu_devicedel($vmid, $deviceid);
+	return undef if !qemu_drivedel($vmid, $deviceid);
+
+	my $drive = PVE::QemuServer::parse_drive($deviceid, $conf->{$deviceid});
+	PVE::QemuServer::add_unused_volume($conf, $drive->{file});
     }
 
     if ($deviceid =~ m/^(net)(\d+)$/) {
-        qemu_devicedel($vmid, $deviceid);
-        return undef if !qemu_devicedelverify($vmid, $deviceid);
-        return undef if !qemu_netdevdel($vmid, $deviceid);
+	qemu_devicedel($vmid, $deviceid);
+	return undef if !qemu_devicedelverify($vmid, $deviceid);
+	return undef if !qemu_netdevdel($vmid, $deviceid);
     }
 
     return 1;
-- 
1.7.10.4




More information about the pve-devel mailing list