Bearbeiten von „VLAN for virtual machines“

Wechseln zu: Navigation, Suche

Warnung: Du bist nicht angemeldet. Deine IP-Adresse wird öffentlich sichtbar, falls du Bearbeitungen durchführst. Sofern du dich anmeldest oder ein Benutzerkonto erstellst, werden deine Bearbeitungen zusammen mit anderen Beiträgen deinem Benutzernamen zugeordnet.

Die Bearbeitung kann rückgängig gemacht werden. Bitte prüfe den Vergleich unten, um sicherzustellen, dass du dies tun möchtest, und speichere dann unten deine Änderungen, um die Bearbeitung rückgängig zu machen.
Aktuelle Version Dein Text
Zeile 6: Zeile 6:
 
and document it here.
 
and document it here.
  
In gerneral I will look at three methods:
+
In gerneral I will look at four methods:
 
# [[#oldstyle linux bridge as hub|oldstyle linux bridge as hub]]
 
# [[#oldstyle linux bridge as hub|oldstyle linux bridge as hub]]
 
# [[#linux bridge as hub|linux bridge as hub]]
 
# [[#linux bridge as hub|linux bridge as hub]]
 
# [[#linux bridge with libvirt hook scripts|linux bridge with libvirt hook scripts]]
 
# [[#linux bridge with libvirt hook scripts|linux bridge with libvirt hook scripts]]
 +
# [[#Open vSwitch|Open vSwitch]]
  
 
== Preparation ==
 
== Preparation ==
Zeile 68: Zeile 69:
 
modify to
 
modify to
 
  ExecStart=-/sbin/agetty --autologin ''yourloginname'' --keep-baud 115200,38400,9600 %I $TERM
 
  ExecStart=-/sbin/agetty --autologin ''yourloginname'' --keep-baud 115200,38400,9600 %I $TERM
<span id="bridge-settings">To list all settings of the bridge</span> you can use:
+
To list all settings of the bridge you can use:
 
  '''harley$''' find /sys/class/net/br0/bridge/ -type f -readable -printf '%f = ' -exec cat {} \; | sort
 
  '''harley$''' find /sys/class/net/br0/bridge/ -type f -readable -printf '%f = ' -exec cat {} \; | sort
  
Zeile 208: Zeile 209:
 
== linux bridge with libvirt hook scripts ==
 
== linux bridge with libvirt hook scripts ==
 
We setup a bridge with VLAN enabled:
 
We setup a bridge with VLAN enabled:
  '''harley$''' cat /etc/systemd/network/08-br0.netdev  
+
  '''harley$''' cat 08-br0.netdev  
 
  [NetDev]
 
  [NetDev]
 
  Name=br0
 
  Name=br0
Zeile 216: Zeile 217:
 
  VLANFiltering=true
 
  VLANFiltering=true
 
  STP=false
 
  STP=false
  '''harley$''' cat /etc/systemd/network/12-br0_add-enp1s0.network  
+
  '''harley$''' cat 12-br0_add-enp1s0.network  
 
  [Match]
 
  [Match]
 
  Name=enp1s0
 
  Name=enp1s0
Zeile 227: Zeile 228:
 
  [BridgeVLAN]
 
  [BridgeVLAN]
 
  VLAN=30
 
  VLAN=30
  '''harley$''' cat /etc/systemd/network/16-br0_up.network  
+
  '''harley$''' cat 16-br0_up.network  
 
  [Match]
 
  [Match]
 
  Name=br0
 
  Name=br0
Zeile 254: Zeile 255:
 
  vnet0  None
 
  vnet0  None
 
  '''harley$'''
 
  '''harley$'''
The virtual network interface vnet0 for deb9-test has no VLAN ID. Libvirt does not know something about this so we have to tell it. Libvirt provides [https://www.libvirt.org/hooks.html hook scripts] that we can use for this. We have to:
+
The virtual network interface vnet0 for deb9-test has no VLAN Id. Libvirt does not know something about this so we have to tell it. Libvirt provides [https://www.libvirt.org/hooks.html hook scripts] that we can use for this. We have to:
 
# [[#define VLAN-ID the virtual machine belongs to]]
 
# [[#define VLAN-ID the virtual machine belongs to]]
 
# [[#get information on startup from the runtime XML-config of the domain]]
 
# [[#get information on startup from the runtime XML-config of the domain]]
 
# [[#set VLAN-ID to the dynamic virtual network interface vnet*]]
 
# [[#set VLAN-ID to the dynamic virtual network interface vnet*]]
 
<span id="debug.sh">For debugging the hook-scripts</span> I've made a small script:
 
<span id="debug.sh">For debugging the hook-scripts</span> I've made a small script:
  '''harley$''' cat /etc/libvirt/hooks/debug.sh
+
  '''harley$''' cat debug.sh
 
  #!/bin/bash -e
 
  #!/bin/bash -e
 
  # https://www.libvirt.org/hooks.html
 
  # https://www.libvirt.org/hooks.html
Zeile 276: Zeile 277:
  
 
=== define VLAN-ID the virtual machine belongs to ===
 
=== define VLAN-ID the virtual machine belongs to ===
For this we have an extra [https://libvirt.org/formatdomain.html#elementsMetadata element <metadata> in Domain XML format] for custom metadata. We can simply add the information to the static configuration with <code>'''harley$''' virsh edit deb9-test</code> like this (look only at the <metadata> element):
+
For thist we have an extra [https://libvirt.org/formatdomain.html#elementsMetadata element <metadata> in Domain XML format] for custom metadata. We can simply add the information to the static configuration with <code>'''harley$''' virsh edit deb9-test</code> like this (look only at the <metadata> element):
 
  '''harley$''' virsh dumpxml deb9-test | head -n9
 
  '''harley$''' virsh dumpxml deb9-test | head -n9
 
  <domain type='kvm' id='1'>
 
  <domain type='kvm' id='1'>
Zeile 289: Zeile 290:
  
 
=== get information on startup from the runtime XML-config of the domain ===
 
=== get information on startup from the runtime XML-config of the domain ===
It seems a little bit difficult to get needed information out of the big XML-config but it's no problem with XSLT. I've made a XSL-stylesheet for this and use xmlstarlet. Start a virtual machine and then its runtime configuration is available with <code>'''harley$''' virsh dumpxml deb9-test | xmlstarlet tr qemu.xsl</code>. With this I can test my stylesheet. Here is it:
+
It seems a little bit difficult to get needed information out of the big XML-config but it's no problem with XSLT. I've made a XSL-stylesheet for this and use xmlstarlet. For developing I took a snapshot from runtime XML-config useing [[#debug.sh|debug.sh]] and prepaired it to a well formed xml-document by hand for hook-parameter $2=start. This is the result:
  '''harley$''' cat /etc/libvirt/hooks/qemu.xsl  
+
  '''harley$''' cat qemu.xsl  
 
  <?xml version="1.0" encoding="UTF-8"?>
 
  <?xml version="1.0" encoding="UTF-8"?>
  &lt;!-- This stylesheet extracts the VLAN-ID and the target device of the
+
  &lt;!-- This stylesheet extracts the VLAN-Id and the target device of the
 
       bridge from the domain-xml given to the libvirt hook-script "qemu".
 
       bridge from the domain-xml given to the libvirt hook-script "qemu".
 
       Example output: <meta><vlan>10</vlan><dev>vnet0</dev></meta>
 
       Example output: <meta><vlan>10</vlan><dev>vnet0</dev></meta>
Zeile 321: Zeile 322:
 
   </xsl:template>
 
   </xsl:template>
 
   
 
   
 +
&lt;!-- vim: set sts=2 sw=2: --&t;
 
  </xsl:stylesheet>
 
  </xsl:stylesheet>
 
  '''harley$'''
 
  '''harley$'''
  
  '''harley$''' virsh dumpxml deb9-test | xmlstarlet tr /etc/libvirt/hooks/qemu.xsl
+
  '''harley$''' xmlstarlet tr qemu.xsl /var/log/libvirt/hooks.xml
 
  <meta><vlan>10</vlan><dev>vnet0</dev></meta>'''harley$'''
 
  <meta><vlan>10</vlan><dev>vnet0</dev></meta>'''harley$'''
  
 
=== set VLAN-ID to the dynamic virtual network interface vnet* ===
 
=== set VLAN-ID to the dynamic virtual network interface vnet* ===
Putting it all together here is the executable hook-script:
+
Putting it all together here is the hook-script:
 
  '''harley$''' cat /etc/libvirt/hooks/qemu
 
  '''harley$''' cat /etc/libvirt/hooks/qemu
  #!/bin/bash
+
  #!/bin/bash -e
 
  #/etc/libvirt/hooks/qemu
 
  #/etc/libvirt/hooks/qemu
 
  # Docs: https://www.libvirt.org/hooks.html
 
  # Docs: https://www.libvirt.org/hooks.html
Zeile 336: Zeile 338:
 
   
 
   
 
  # On startup of the domain (guest) This script does:
 
  # On startup of the domain (guest) This script does:
  # Get Metadata VLAN-ID of the guest and target device of the bridge from
+
  # get Metadata VLAN-Id of the guest and target device of the bridge from
 
     # the domain-xml available on standard input. It is the runtime
 
     # the domain-xml available on standard input. It is the runtime
 
     # version from 'virsh dumpxml domainname'. For extracting the
 
     # version from 'virsh dumpxml domainname'. For extracting the
Zeile 375: Zeile 377:
 
     ;;
 
     ;;
 
  esac
 
  esac
'''harley$''' sudo chmod 744 /etc/libvirt/hooks/qemu
 
 
  '''harley$'''
 
  '''harley$'''
 +
 +
=== Workaround for setting DefaultPVID=none ===
  
 
=== References ===
 
=== References ===
 
* https://www.libvirt.org/hooks.html
 
* https://www.libvirt.org/hooks.html
 
* https://serverfault.com/questions/696011/libvirt-hook-qemu-suse12
 
* https://serverfault.com/questions/696011/libvirt-hook-qemu-suse12
 
== Workaround for setting DefaultPVID=none ==
 
<span style="color:red">We do not need it anymore. This bug is fixed in systemd 234.</span>
 
 
Setting [https://www.freedesktop.org/software/systemd/man/systemd.netdev.html#DefaultPVID= DefaultPVID] in a<code>systemd-networkd</code> configuration file to "none" does not work. Until this bug is fixed I've made a workaround. The kernel accepts setting <code>default_pvid</code> to 0 (means "none") only if <code>vlan_filtering=0</code>, so we have to do:
 
'''harley$''' sudo bash -c 'echo 0 >/sys/class/net/br0/bridge/vlan_filtering'
 
'''harley$''' sudo bash -c 'echo 0 >/sys/class/net/br0/bridge/default_pvid'
 
'''harley$''' sudo bash -c 'echo 1 >/sys/class/net/br0/bridge/vlan_filtering'
 
'''harley$'''
 
Check with listing of [[#bridge-settings|bridge-settings]].
 
Theese commands must run with <code>systemd-networkd</code> so we need a service for this. First I make a script and make it executable for root:
 
'''harley$''' cat /etc/systemd/network/DefaultPVID.sh
 
#!/bin/bash
 
#echo "entering DefaultPVID.sh" >>/tmp/debug.log
 
 
BRDIR="/sys/class/net/br0/bridge/"
 
 
if &#91;[ -f $BRDIR/vlan_filtering && -f $BRDIR/default_pvid ]&#93;; then
 
  #echo "setting DefaultPVID" >>/tmp/debug.log
 
  VLAN_FILTERING="$(cat "$BRDIR"/vlan_filtering)"
 
  echo 0 >"$BRDIR"/vlan_filtering
 
  echo 0 >"$BRDIR"/default_pvid
 
  echo "$VLAN_FILTERING" >"$BRDIR"/vlan_filtering
 
fi
 
exit 0
 
'''harley$''' sudo chmod 744 /etc/systemd/network/DefaultPVID.sh
 
'''harley$'''
 
Test with <code>'''harley$''' sudo /etc/systemd/network/DefaultPVID.sh</code>. Next I create a service to execute this script:
 
'''harley$''' cat /etc/systemd/system/DefaultPVID.service
 
[Unit]
 
Description=set DefaultPVID on a bridge as workaround
 
Wants=network.target
 
After=network.target
 
 
[Service]
 
Type=oneshot
 
ExecStart=/etc/systemd/network/DefaultPVID.sh
 
 
[Install]
 
WantedBy=multi-user.target
 
'''harley$'''
 
Test with <code>'''harley$''' sudo systemctl start DefaultPVID.service && systemctl status DefaultPVID.service</code>. After this I create a [https://www.freedesktop.org/software/systemd/man/systemd.unit.html#id-1.11.3 drop-in file for overriding vendor settings] so this service will be executed together with <code>systemd-networkd</code>:
 
'''harley$''' cat /etc/systemd/system/systemd-networkd.service.d/DefaultPVID.conf
 
[Unit]
 
# This is only a workaround. DefaultPVID cannot be set in
 
# /etc/systemd/network/br0.netdev. It seems buggy.
 
Wants=DefaultPVID.service
 
Before=DefaultPVID.service
 
'''harley$'''
 
Test with <code>'''harley$''' sudo systemctl restart systemd-networkd</code>.
 
  
 
[[Category:Virtualization]]
 
[[Category:Virtualization]]

Bitte beachte, dass alle Beiträge zu Ingos Wiki von anderen Mitwirkenden bearbeitet, geändert oder gelöscht werden können. Reiche hier keine Texte ein, falls du nicht willst, dass diese ohne Einschränkung geändert werden können.

Du bestätigst hiermit auch, dass du diese Texte selbst geschrieben hast oder diese von einer gemeinfreien Quelle kopiert hast (weitere Einzelheiten unter My wiki:Urheberrechte). ÜBERTRAGE OHNE GENEHMIGUNG KEINE URHEBERRECHTLICH GESCHÜTZTEN INHALTE!

Abbrechen | Bearbeitungshilfe (wird in einem neuen Fenster geöffnet)