+remove_from_bridge()
+{
+ case $NETWORK_MODE in
+ bridge)
+ # Nothing to do
+ ;;
+ openvswitch)
+ $vsctl --timeout=30 -- del-port $dev
+ ;;
+ esac
+}
+
+call_hook_script() {
+ local domid=$1
+ local action=$2
+ # Call the VIF hotplug hook if present
+ if [ -x /etc/xapi.d/vif-hotplug ]; then
+ local vm=$(xenstore-read "/local/domain/$domid/vm" 2>/dev/null)
+ if [ $? -eq 0 -a -n "${vm}" ] ; then
+ local vm_uuid=$(xenstore-read "$vm/uuid" 2>/dev/null)
+ fi
+ if [ -n "${vm_uuid}" ] ; then
+ logger -t scripts-vif "VM UUID ${vm_uuid}"
+ fi
+
+ local vif_uuid=$(xenstore-read "${PRIVATE}/vif-uuid" 2>/dev/null)
+ if [ -n "${vif_uuid}" ] ; then
+ logger -t scripts-vif "VIF UUID ${vif_uuid}"
+ fi
+ if [ -n "${vif_uuid}" -a -n "${vm_uuid}" ] ; then
+ logger -t scripts-vif "Calling VIF hotplug hook for VM ${vm_uuid}, VIF ${vif_uuid}"
+ /etc/xapi.d/vif-hotplug -action "${action}" -vifuuid "${vif_uuid}" -vmuuid "${vm_uuid}"
+ fi
+ fi
+}
+
+NETWORK_MODE=$(cat /etc/xensource/network.conf)
+ACTION=$1
+
+# Older versions of XenServer do not pass in the type as an argument
+if [[ $# -lt 2 ]]; then
+ TYPE=vif
+else
+ TYPE=$2
+fi
+
+case $NETWORK_MODE in
+ bridge|openvswitch) ;;
+ vswitch) NETWORK_MODE=openvswitch ;;
+ *)
+ logger -t scripts-vif "Unknown network mode $NETWORK_MODE"
+ exit 1
+ ;;
+esac
+
+case ${TYPE} in
+ vif)
+ if [ -z ${XENBUS_PATH} ]; then
+ DOMID=$3
+ DEVID=$4
+ else
+ DOMID=`echo ${XENBUS_PATH} | cut -f 3 -d '/'`
+ DEVID=`echo ${XENBUS_PATH} | cut -f 4 -d '/'`
+ fi
+ dev=vif${DOMID}.${DEVID}
+ ;;
+ tap)
+ dev=$INTERFACE
+ DOMID=`echo ${dev#tap} | cut -f 1 -d '.'`
+ DEVID=`echo ${dev#tap} | cut -f 2 -d '.'`
+ ;;
+ *)
+ logger -t scripts-vif "unknown interface type ${TYPE}"
+ exit 1
+ ;;
+esac
+
+XAPI=/xapi/${DOMID}/hotplug/vif/${DEVID}
+HOTPLUG=/xapi/${DOMID}/hotplug/vif/${DEVID}
+PRIVATE=/xapi/${DOMID}/private/vif/${DEVID}
+
+logger -t scripts-vif "Called as \"$@\" domid:$DOMID devid:$DEVID mode:$NETWORK_MODE"
+case "${ACTION}" in