qemu-hook 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/bin/sh -e
  2. #
  3. # Libvirt qemu hook to magically connect VM bridge interfaces to desired untagged Vlan
  4. #
  5. # IF
  6. # * the VM interface if configured to be connected to a bridge,
  7. # * the bridge is configured to be vlan aware (vlan_filtering = 1)
  8. # * the VM interface name ends with _vXXXX with XXXX being a one to four
  9. # digit number
  10. # the interface will be configured as an untagged port connected to vlan XXXX.
  11. #
  12. # See <man bridge> for more details about vlan aware bridges.
  13. #
  14. # Maximilian Wilhelm <max@sdn.clinic>
  15. # -- Wed, 31 Aug 2016 20:48:02 +0200
  16. my_name="qemu-magic"
  17. # We only care for the "started" event.
  18. if [ "$2" != 'started' ]; then
  19. exit 0
  20. fi
  21. if ! which xmlstarlet >/dev/null 2>/dev/null; then
  22. logger -t "${my_name}" "ERROR: xmlstarlet not found. Dying of shame."
  23. echo "${my_name}: ERROR: xmlstarlet not found. Dying of shame." >&2
  24. exit 1
  25. fi
  26. # Save domain XML which was given to us on stdin, so we can poke around in it.
  27. # Yes this is ugly. But I don't see another way storing the MTU for an device
  28. # within the libvirt domain config when NOT using a libvirt network.
  29. # If you happen to now a better way than specifying this within custom domain
  30. # metadata PLEASE let me know.
  31. export domain_xml="$(cat)"
  32. echo "${domain_xml}" | xmlstarlet sel -t -m '//interface[@type="bridge"]' -v 'concat(target/@dev, " ", source/@bridge)' --nl | while read iface bridge; do
  33. if [ ! -d "/sys/class/net/${bridge}/bridge" ]; then
  34. logger -t "${my_name}" "Bridge \"${bridge}\" for iface \"${iface}\" doesn't exist or isn't a bridge."
  35. exit 2
  36. fi
  37. #
  38. # Check if this kernel supports vlan-aware bridges and if ${bridge} is one
  39. vlan_filtering=0
  40. if [ -f "/sys/class/net/${bridge}/bridge/vlan_filtering" ]; then
  41. vlan_filtering=$(cat "/sys/class/net/${bridge}/bridge/vlan_filtering")
  42. fi
  43. # If the interface is named *_vXXXX, with XXXX being a 1-4 digit number
  44. # we assume that this iface should be connected to Vlan XXXX with
  45. # an untagged port.
  46. vlan_id=$(echo ${iface} | grep -o '_v[0-9]\{1,4\}$' | cut -c3-)
  47. # If vlan filtering is activated and we found a vlan id, kindly do the needful.
  48. if [ "${vlan_filtering}" -a "${vlan_id}" ]; then
  49. # Remove association with vlan 1 and add association with
  50. # vlan $vlan_id with packages being sent out untagged and
  51. # untagged ingress packets get tagged accordingly.
  52. bridge vlan del vid 1 dev "${iface}"
  53. bridge vlan add vid "${vlan_id}" dev "${iface}" pvid untagged
  54. logger -t "${my_name}" "Configured untagged pvid ${vlan_id} for ${iface} in bridge ${bridge}."
  55. # If vlan filtering isn't activated or supported but we found a vlan id,
  56. # this probably is an error!
  57. elif [ ! "${vlan_filtering}" -a "${vlan_id}" ]; then
  58. logger -t "${my_name}" -p user.error "ERROR: Should configure untagged pvid ${vlan_id} for ${iface} in bridge ${bridge}, but bridge does not support vlan filtering!"
  59. fi
  60. # We dont' care about "no vlan filtering AND no vlan id" as well as "vlan filtering AND no vlan id"
  61. mtu=$(echo "${domain_xml}" | xmlstarlet sel -N ffho="https://ffho.net/libvirt/" -t -m "//ffho:net/interface[@name='${iface}']" -v 'mtu/@size' --nl || true)
  62. if [ "${mtu}" ]; then
  63. ip link set mtu "${mtu}" dev "${iface}"
  64. logger -t "${my_name}" "Setting MTU of ${iface} to ${mtu}."
  65. fi
  66. # If there is an configuration stanza in /etc/network/interfaces
  67. # for this interfaces, we try to get it up and running. Proceed
  68. # with fingers crossed.
  69. if grep -q "^iface\s\+${iface}" /etc/network/interfaces; then
  70. if ifup $iface; then
  71. logger -t "${my_name}" "ifup'ed ${iface}."
  72. else
  73. logger -t "${my_name}" -p user.error "ifup ${iface} FAILED."
  74. fi
  75. fi
  76. done