]> git.proxmox.com Git - qemu-server.git/commitdiff
create qemu_deviceadd, qemu_devicedel, qemu_driveadd, qemu_drivedel, qemu_deviceaddve...
authorDerumier Alexandre <aderumier@odiso.com>
Fri, 20 Jan 2012 10:42:05 +0000 (11:42 +0100)
committerDietmar Maurer <dietmar@proxmox.com>
Mon, 23 Jan 2012 08:09:15 +0000 (09:09 +0100)
and replace code in vm_deviceplug/vm_deviceunplug

Signed-off-by: Derumier Alexandre <aderumier@odiso.com>
PVE/QemuServer.pm

index 4e3da37d5ea2af464c55bdb0699730bdd53ffb80..eb077f10714a34c0824e0e64bc02546fc9887a86 100644 (file)
@@ -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 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 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 {