[pve-devel] [PATCH 3/3] add qemu_netdevadd, qemu_netdevdel and add them in hotplug code

Derumier Alexandre aderumier at odiso.com
Sat Jan 28 11:02:29 CET 2012


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

diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index b106f9f..4d9033a 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -2302,7 +2302,7 @@ sub vm_devices_list {
 
 sub vm_deviceplug {
     my ($storecfg, $conf, $vmid, $deviceid, $device) = @_;
-    return 1 if !check_running($vmid) || !$conf->{hotplug} || $conf->{$deviceid};
+    return 1 if !check_running($vmid) || !$conf->{hotplug};
 
     if ($deviceid =~ m/^(virtio)(\d+)$/) {
         return undef if !qemu_driveadd($storecfg, $vmid, $device);
@@ -2331,6 +2331,16 @@ sub vm_deviceplug {
         }
     }
 
+    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 1;
 }
 
@@ -2356,6 +2366,12 @@ sub vm_deviceunplug {
         return undef if !qemu_drivedel($vmid, $deviceid);
     }
 
+    if ($deviceid =~ m/^(net)(\d+)$/) {
+        return undef if !qemu_netdevdel($vmid, $deviceid);
+        qemu_devicedel($vmid, $deviceid);
+        return undef if !qemu_devicedelverify($vmid, $deviceid);
+    }
+
     return 1;
 }
 
@@ -2449,6 +2465,30 @@ sub qemu_findorcreatelsi {
     return 1;
 }
 
+sub qemu_netdevadd {
+    my ($vmid, $conf, $device, $deviceid) = @_;
+
+    my $netdev = print_netdev_full($vmid, $conf, $device, $deviceid);
+    my $ret = vm_monitor_command($vmid, "netdev_add $netdev");
+    $ret =~ s/^\s+//;
+
+    #if the command succeeds, no output is sent. So any non-empty string shows an error 
+    return 1 if $ret eq "";
+    syslog("err", "adding netdev failed: $ret");
+    return undef;
+}
+
+sub qemu_netdevdel {
+    my ($vmid, $deviceid) = @_;
+
+    my $ret = vm_monitor_command($vmid, "netdev_del $deviceid");
+    $ret =~ s/^\s+//;
+    #if the command succeeds, no output is sent. So any non-empty string shows an error 
+    return 1 if $ret eq "";
+    syslog("err", "deleting netdev failed: $ret");
+    return undef;
+}
+
 sub vm_start {
     my ($storecfg, $vmid, $statefile, $skiplock) = @_;
 
-- 
1.7.2.5



More information about the pve-devel mailing list