]> git.proxmox.com Git - pve-common.git/blobdiff - data/PVE/Network.pm
tap_unplug: auto-detect bridge, improve error messages
[pve-common.git] / data / PVE / Network.pm
index 73984c2ad9c1b142be604cd13355fd3036a3f993..885be0ef1083450e11af6294956a3384312b0a5e 100644 (file)
@@ -88,16 +88,19 @@ sub tap_plug {
 }
 
 sub tap_unplug {
-    my ($iface, $bridge, $tag) = @_;
+    my ($iface) = @_;
 
-    if (-d "/sys/class/net/$bridge/bridge") {
-       $bridge .= "v$tag" if $tag;
+    my $path= "/sys/class/net/$iface/brport/bridge";
+    if (-l $path) {
+       my $bridge = basename(readlink($path));
+       #avoid insecure dependency;
+       ($bridge) = $bridge =~ /(\S+)/;
 
        system("/sbin/brctl delif $bridge $iface") == 0 ||
-           die "can't del interface from bridge\n";
+           die "can't del interface '$iface' from bridge '$bridge'\n";
     } else {
        system ("/usr/bin/ovs-vsctl del-port $iface") == 0 ||
-           die "can't del interface from bridge\n";
+           die "can't del ovs port '$iface'\n";
     }
 }
 
@@ -141,6 +144,9 @@ sub activate_bridge_vlan_slave {
         my $tbridge = basename(readlink($path));
        if ($tbridge ne $bridgevlan) {
            die "interface $ifacevlan already exist in bridge $tbridge\n";
+       } else {
+            # Port already attached to bridge: do nothing.
+            return;
        }
     }
 
@@ -162,6 +168,14 @@ sub activate_bridge_vlan {
 
     my $bridgevlan = "${bridge}v$tag";
 
+    my @ifaces = ();
+    my $dir = "/sys/class/net/$bridge/brif";
+    PVE::Tools::dir_glob_foreach($dir, '((eth|bond)\d+)', sub {
+        push @ifaces, $_[0];
+    });
+
+    die "no physical interface on bridge '$bridge'\n" if scalar(@ifaces) == 0;
+
     # add bridgevlan if it doesn't already exist
     if (! -d "/sys/class/net/$bridgevlan") {
         system("/sbin/brctl addbr $bridgevlan") == 0 ||
@@ -169,15 +183,9 @@ sub activate_bridge_vlan {
     }
 
     # for each physical interface (eth or bridge) bind them to bridge vlan
-    my $ifcount = 0;
-    my $dir = "/sys/class/net/$bridge/brif";
-    PVE::Tools::dir_glob_foreach($dir, '((eth|bond)\d+)', sub {
-        my ($slave) = @_;
-        activate_bridge_vlan_slave($bridgevlan, $slave, $tag);
-        $ifcount++;
-    });
-
-    die "no physical interface on bridge '$bridge'\n" if $ifcount == 0;
+    foreach my $iface (@ifaces) {
+        activate_bridge_vlan_slave($bridgevlan, $iface, $tag);
+    }
 
     #fixme: set other bridge flags