#!/bin/sh

# Writes to the store when this domain's frontend block device appears.
# Once this happens we should be safe opening the device.

# FIXME: The interface with xapi doesn't distinguish between the whole
# device and any partitions so we handle them identically.

DOMID=`xenstore-read domid`

if [ ! -z ${PHYSDEVPATH} ]; then
    # Extract the device type and ID from the PHYSDEVPATH eg
    # PHYSDEVPATH=/devices/xen/vbd-51728
    TYPE=`echo ${PHYSDEVPATH} | cut -f 4 -d '/' | cut -f 1 -d '-'`
    DEVID=`echo ${PHYSDEVPATH} | cut -f 2 -d '-'`
else
    # Extract the device type and ID from DEVPATH. e.g.,
    # DEVPATH=/devices/vbd-51728/block/xvda1
    id=$(echo ${DEVPATH} | cut -f 3 -d '/')
    TYPE=$(echo ${id} | cut -f 1 -d '-')
    DEVID=$(echo ${id} | cut -f 2 -d '-')
fi

XAPI=/xapi/${DOMID}/frontend/${TYPE}/${DEVID}

case "$1" in
add)
        xenstore-write "${XAPI}/hotplug" "online"
        ;;
remove)
        xenstore-rm "${XAPI}"
        ;;
esac
