123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- my_name="qemu-magic"
- if [ "$2" != 'started' ]; then
- exit 0
- fi
- if ! which xmlstarlet >/dev/null 2>/dev/null; then
- logger -t "${my_name}" "ERROR: xmlstarlet not found. Dying of shame."
- echo "${my_name}: ERROR: xmlstarlet not found. Dying of shame." >&2
- exit 1
- fi
- export domain_xml="$(cat)"
- echo "${domain_xml}" | xmlstarlet sel -t -m '//interface[@type="bridge"]' -v 'concat(target/@dev, " ", source/@bridge)' --nl | while read iface bridge; do
- if [ ! -d "/sys/class/net/${bridge}/bridge" ]; then
- logger -t "${my_name}" "Bridge \"${bridge}\" for iface \"${iface}\" doesn't exist or isn't a bridge."
- exit 2
- fi
- #
- # Check if this kernel supports vlan-aware bridges and if ${bridge} is one
- vlan_filtering=0
- if [ -f "/sys/class/net/${bridge}/bridge/vlan_filtering" ]; then
- vlan_filtering=$(cat "/sys/class/net/${bridge}/bridge/vlan_filtering")
- fi
- # If the interface is named *_vXXXX, with XXXX being a 1-4 digit number
- # we assume that this iface should be connected to Vlan XXXX with
- # an untagged port.
- vlan_id=$(echo ${iface} | grep -o '_v[0-9]\{1,4\}$' | cut -c3-)
- # If vlan filtering is activated and we found a vlan id, kindly do the needful.
- if [ "${vlan_filtering}" = 1 -a "${vlan_id}" ]; then
- # Remove association with vlan 1 and add association with
- # vlan $vlan_id with packages being sent out untagged and
- # untagged ingress packets get tagged accordingly.
- bridge vlan del vid 1 dev "${iface}"
- bridge vlan add vid "${vlan_id}" dev "${iface}" pvid untagged
- logger -t "${my_name}" "Configured untagged VLAN ${vlan_id} for ${iface} in bridge ${bridge}."
- # If vlan filtering isn't activated or supported but we found a vlan id,
- # this probably is an error!
- elif [ "${vlan_filtering}" = 0 -a "${vlan_id}" ]; then
- 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!"
- fi
- # We dont' care about "no vlan filtering AND no vlan id" as well as "vlan filtering AND no vlan id"
- mtu=$(echo "${domain_xml}" | xmlstarlet sel -N ffho="https://ffho.net/libvirt/" -t -m "//ffho:net/interface[@name='${iface}']" -v 'mtu/@size' --nl || true)
- if [ "${mtu}" ]; then
- ip link set mtu "${mtu}" dev "${iface}"
- logger -t "${my_name}" "Setting MTU of ${iface} to ${mtu}."
- fi
- # If there is an configuration stanza in /etc/network/interfaces
- # for this interfaces, we try to get it up and running. Proceed
- # with fingers crossed.
- if grep -q "^iface\s\+${iface}" /etc/network/interfaces; then
- vids=$(/etc/libvirt/hooks/get-bridge-vids "${iface}")
- if [ "${vids}" ]; then
- bridge vlan del vid 1 dev "${iface}"
- for vid in ${vids}; do
- bridge vlan add vid ${vid} dev "${iface}"
- done
- logger -t "${my_name}" "Configured tagged VLANs ${vids} for ${iface} in bridge ${bridge}."
- fi
- fi
- done
|