[pve-devel] [PATCH 4/8] create qemu_deviceadd, qemu_devicedel, qemu_driveadd, qemu_drivedel, qemu_deviceaddverify, qemu_devicedelverify

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


and replace code in vm_deviceplug/vm_deviceunplug

Signed-off-by: Derumier Alexandre <aderumier at odiso.com>
---
 PVE/QemuServer.pm |  125 +++++++++++++++++++++++++++++++++++-----------------
 1 files changed, 84 insertions(+), 41 deletions(-)

diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index 4e3da37..eb077f1 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -2284,66 +2284,109 @@ sub vm_devices_list {
 
 sub vm_deviceplug {
     my ($storecfg, $conf, $vmid, $deviceid, $device) = @_;
-    return if !check_running($vmid) || !$conf->{hotplug} || $conf->{$deviceid};
+    return 1 if !check_running($vmid) || !$conf->{hotplug} || $conf->{$deviceid};
     
-    if($deviceid =~ m/^(virtio)(\d+)$/) {
-
-        my $drive = print_drive_full($storecfg, $vmid, $device);
-        my $ret = vm_monitor_command($vmid, "drive_add auto $drive");
-        # If the command succeeds qemu prints: "OK"
-        if ($ret !~ m/OK/s) {
-           die "adding drive failed: $ret";
-        }
-       
+    if ($deviceid =~ m/^(virtio)(\d+)$/) {
+        return undef if !qemu_driveadd($storecfg, $vmid, $device);
         my $devicefull = print_drivedevice_full($storecfg, $vmid, $device);
-        $ret = vm_monitor_command($vmid, "device_add $devicefull");
-        $ret =~ s/^\s+//;
-        # Otherwise, if the command succeeds, no output is sent. So any non-empty string shows an error 
-        die 'error on hotplug device : $ret' if $ret ne "";
-    }
-
-    for (my $i = 0; $i <= 5; $i++) {
-        my $devices_list = vm_devices_list($vmid);
-        return if defined($devices_list->{$deviceid});   
-        sleep 1;
+        qemu_deviceadd($vmid, $devicefull);
+        if(!qemu_deviceaddverify($vmid, $deviceid)) {
+           qemu_drivedel($vmid, $deviceid);
+           return undef;
+        }
     }
-	
-    die "error on hotplug device $deviceid";
+    return 1;
 }
 
 sub vm_deviceunplug {
     my ($vmid, $conf, $deviceid) = @_;
 
-    return if !check_running ($vmid) || !$conf->{hotplug};
+    return 1 if !check_running ($vmid) || !$conf->{hotplug};
 
     die "can't unplug bootdisk" if $conf->{bootdisk} eq $deviceid;
 
-    if($deviceid =~ m/^(virtio)(\d+)$/){
+    if ($deviceid =~ m/^(virtio)(\d+)$/) {
+        return undef if !qemu_drivedel($vmid, $deviceid);
+        qemu_devicedel($vmid, $deviceid);
+        return undef if !qemu_devicedelverify($vmid, $deviceid);
+    }
+    return 1;
+}
+
+sub qemu_deviceadd {
+    my ($vmid, $devicefull) = @_;
 
-        my $ret = vm_monitor_command($vmid, "drive_del drive-$deviceid");
-        $ret =~ s/^\s+//;
-        if ($ret =~ m/Device \'.*?\' not found/s) {
-            # NB: device not found errors mean the drive was auto-deleted and we ignore the error 
-        }
-        elsif ($ret ne "") {
-            die "deleting drive $deviceid failed : $ret";
-        }
+    my $ret = vm_monitor_command($vmid, "device_add $devicefull");
+    $ret =~ s/^\s+//;
+    # Otherwise, if the command succeeds, no output is sent. So any non-empty string shows an error 
+    return 1 if $ret eq "";
+    syslog("err", "error on hotplug device : $ret");
+    return undef;
 
-        $ret = vm_monitor_command($vmid, "device_del $deviceid");
-        $ret =~ s/^\s+//;
-        die 'detaching device $deviceid failed : $ret' if $ret ne "";
+}
+ 
+sub qemu_devicedel {
+    my($vmid, $deviceid) = @_;
 
+    my $ret = vm_monitor_command($vmid, "device_del $deviceid");
+    $ret =~ s/^\s+//;
+    return 1 if $ret eq "";
+    syslog("err", "detaching device $deviceid failed : $ret");
+    return undef;
+}
+
+sub qemu_driveadd {
+    my($storecfg, $vmid, $device) = @_;
+
+    my $drive = print_drive_full($storecfg, $vmid, $device);
+    my $ret = vm_monitor_command($vmid, "drive_add auto $drive");
+    # If the command succeeds qemu prints: "OK"
+    if ($ret !~ m/OK/s) {
+        syslog("err", "adding drive failed: $ret");
+        return undef;
     }
+    return 1;
+}
+ 
+sub qemu_drivedel {
+    my($vmid, $deviceid) = @_;
 
-    #need to verify the device is correctly remove as device_del is async and empty return is not reliable
-    for (my $i = 0; $i <= 5; $i++) {
-        my $devices_list = vm_devices_list($vmid);
-        return if !defined($devices_list->{$deviceid});
-        sleep 1;
+    my $ret = vm_monitor_command($vmid, "drive_del drive-$deviceid");
+    $ret =~ s/^\s+//;
+    if ($ret =~ m/Device \'.*?\' not found/s) {
+        # NB: device not found errors mean the drive was auto-deleted and we ignore the error 
+    }
+    elsif ($ret ne "") {
+      syslog("err", "deleting drive $deviceid failed : $ret");
+      return undef;
     }
-    die "error on hot-plugging device $deviceid";
+    return 1;
+}
 
+sub qemu_deviceaddverify {
+    my ($vmid,$deviceid) = @_;
 
+    for (my $i = 0; $i <= 5; $i++) {
+         my $devices_list = vm_devices_list($vmid);
+         return 1 if defined($devices_list->{$deviceid});
+         sleep 1;
+    }  
+    syslog("err", "error on hotplug device $deviceid");
+    return undef;
+}
+ 
+
+sub qemu_devicedelverify {
+    my ($vmid,$deviceid) = @_;
+
+    #need to verify the device is correctly remove as device_del is async and empty return is not reliable
+    for (my $i = 0; $i <= 5; $i++) {
+         my $devices_list = vm_devices_list($vmid);
+         return 1 if !defined($devices_list->{$deviceid});
+         sleep 1;
+    }  
+    syslog("err", "error on hot-unplugging device $deviceid");
+    return undef;
 }
 
 sub vm_start {
-- 
1.7.2.5



More information about the pve-devel mailing list