1#! /bin/bash
2
3dir=$(dirname "$0")
4. "$dir/xen-hotplug-common.sh"
5
6# Claim the lock protecting ${XEN_SCRIPT_DIR}/block.  This stops a race whereby
7# paths in the store would disappear underneath that script as it attempted to
8# read from the store checking for device sharing.
9# Any other scripts that do similar things will have to have their lock
10# claimed too.
11# This is pretty horrible, but there's not really a nicer way of solving this.
12claim_lock "block"
13
14# split backend/DEVCLASS/VMID/DEVID on slashes
15path_array=( ${XENBUS_PATH//\// } )
16# get /vm/UUID path
17vm=$(xenstore_read_default "/local/domain/${path_array[2]}/vm" "")
18# construct /vm/UUID/device/DEVCLASS/DEVID
19if [ "$vm" != "" ]; then
20  vm_dev="$vm/device/${path_array[1]}/${path_array[3]}"
21
22  # if the vm path does not exist and the device class is 'vbd' then we may have
23  # a tap2 device
24  $(xenstore-read "$vm_dev" 2>/dev/null) || \
25  {
26     if [ "${path_array[1]}" = "vbd" ]; then
27        vm_dev="$vm/device/tap2/${path_array[3]}"
28     fi
29  }
30else
31  vm_dev=
32fi
33
34# remove device frontend store entries
35xenstore-rm -t \
36  $(xenstore-read "$XENBUS_PATH/frontend" 2>/dev/null) 2>/dev/null || true
37
38# remove device backend store entries
39xenstore-rm -t "$XENBUS_PATH"        2>/dev/null || true
40xenstore-rm -t "error/$XENBUS_PATH"  2>/dev/null || true
41
42# remove device path from /vm/UUID
43[ "$vm_dev" != "" ] && xenstore-rm -t "$vm_dev" 2>/dev/null || true
44
45release_lock "block"
46