[pve-devel] [PATCH] Add to config the nic flag link_down=[0|1] also enable nic link if the Flag is set in the config. we use to verify the result the qemu extension get_link_status from or pachtes.
Wolfgang Link
wolfgang at linksystems.org
Mon Dec 22 13:35:58 CET 2014
Signed-off-by: Wolfgang Link <wolfgang at linksystems.org>
---
PVE/API2/Qemu.pm | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++--
PVE/QemuServer.pm | 22 ++++++++++++++++++++-
2 files changed, 76 insertions(+), 3 deletions(-)
diff --git a/PVE/API2/Qemu.pm b/PVE/API2/Qemu.pm
index 066726d..f3fe3e6 100644
--- a/PVE/API2/Qemu.pm
+++ b/PVE/API2/Qemu.pm
@@ -849,9 +849,36 @@ my $vmconfig_update_net = sub {
if(($newnet->{bridge} ne $oldnet->{bridge}) || ($newnet->{tag} ne $oldnet->{tag}) || ($newnet->{firewall} ne $oldnet->{firewall})){
PVE::Network::tap_unplug($iface);
- PVE::Network::tap_plug($iface, $newnet->{bridge}, $newnet->{tag}, $newnet->{firewall});
+ PVE::Network::tap_plug($iface, $newnet->{bridge}, $newnet->{tag});
}
+ my $verify_link_status = sub {
+ my ($expected, $vmid) = @_;
+ my $nic_status;
+ eval {
+ my %param = (name => $opt);
+ $nic_status = PVE::QemuServer::vm_mon_cmd($vmid, "get_link_status",%param);
+ };
+ die $@ if $@;
+ die "chagning nic status dosn't work!" if $expected != $nic_status;
+ };
+
+ if($newnet->{link_down}){
+ eval {
+ my %param = (name => $opt, up => \0);
+ PVE::QemuServer::vm_mon_cmd($vmid, "set_link",%param);
+ };
+ die $@ if $@;
+ &$verify_link_status(0,$vmid);
+ }
+ if($oldnet->{link_down} && !$newnet->{link_down}){
+ eval {
+ my %param = (name => $opt, up => \1);
+ PVE::QemuServer::vm_mon_cmd($vmid, "set_link",%param);
+ };
+ die $@ if $@;
+ &$verify_link_status(1,$vmid);
+ }
}else{
#if bridge/nat mode change, we try to hot-unplug
die "error hot-unplug $opt for update" if !PVE::QemuServer::vm_deviceunplug($vmid, $conf, $opt);
@@ -982,7 +1009,7 @@ my $update_vm_api = sub {
my $running = PVE::QemuServer::check_running($vmid);
foreach my $opt (keys %$param) { # add/change
-
+
$conf = PVE::QemuServer::load_config($vmid); # update/reload
next if $conf->{$opt} && ($param->{$opt} eq $conf->{$opt}); # skip if nothing changed
@@ -1646,11 +1673,37 @@ __PACKAGE__->register_method({
my $realcmd = sub {
my $upid = shift;
+ my $conf = PVE::QemuServer::load_config($vmid);
+
syslog('info', "start VM $vmid: $upid\n");
PVE::QemuServer::vm_start($storecfg, $vmid, $stateuri, $skiplock, $migratedfrom, undef,
$machine, $spice_ticket);
+ foreach my $nic (keys %$conf){
+
+ if($nic =~ m/^net\d+$/){
+
+ my $nicconf= PVE::QemuServer::parse_net($conf->{$nic});
+
+ if($nicconf->{link_down}){
+
+ eval {
+ my %param = (name => $nic, up => \0);
+ PVE::QemuServer::vm_mon_cmd($vmid, "set_link",%param);
+ };
+ die $@ if $@;
+
+ my $nic_status;
+ eval {
+ my %param = (name => $nic);
+ $nic_status = PVE::QemuServer::vm_mon_cmd($vmid, "get_link_status",%param);
+ };
+ warn $@ if $@;
+ warn "Error Link from $nic is not Down" if $nic_status != 0;
+ }
+ }
+ }
return;
};
diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index 4840c73..7d8d863 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -510,7 +510,7 @@ my $nic_model_list_txt = join(' ', sort @$nic_model_list);
my $netdesc = {
optional => 1,
type => 'string', format => 'pve-qm-net',
- typetext => "MODEL=XX:XX:XX:XX:XX:XX [,bridge=<dev>][,queues=<nbqueues>][,rate=<mbps>][,tag=<vlanid>][,firewall=0|1]",
+ typetext => "MODEL=XX:XX:XX:XX:XX:XX [,bridge=<dev>][,queues=<nbqueues>][,rate=<mbps>][,tag=<vlanid>][,firewall=0|1][,link_down=0|1]",
description => <<EODESCR,
Specify network devices.
@@ -1378,6 +1378,8 @@ sub parse_net {
$res->{tag} = $1;
} elsif ($kvp =~ m/^firewall=(\d+)$/) {
$res->{firewall} = $1;
+ } elsif ($kvp =~ m/^link_down=(\d+)$/) {
+ $res->{link_down} = $1;
} else {
return undef;
}
@@ -1398,6 +1400,7 @@ sub print_net {
$res .= ",rate=$net->{rate}" if $net->{rate};
$res .= ",tag=$net->{tag}" if $net->{tag};
$res .= ",firewall=$net->{firewall}" if $net->{firewall};
+ $res .= ",link_down=$net->{link_down}" if $net->{link_down};
return $res;
}
@@ -3084,6 +3087,23 @@ sub vm_deviceplug {
qemu_netdevdel($vmid, $deviceid);
return undef;
}
+ my $netconf = parse_net($conf->{$deviceid});
+ if($netconf->{link_down}){
+
+ eval {
+ my %param = (name => $deviceid, up => \0);
+ PVE::QemuServer::vm_mon_cmd($vmid, "set_link",%param);
+ };
+ die $@ if $@;
+
+ my $nic_status;
+ eval {
+ my %param = (name => $deviceid);
+ $nic_status = PVE::QemuServer::vm_mon_cmd($vmid, "get_link_status",%param);
+ };
+ warn $@ if $@;
+ warn "Error Link from $deviceid is not Down" if $nic_status != 0;
+ }
}
--
1.7.10.4
More information about the pve-devel
mailing list