xen/block-npiv

108 lines
2.0 KiB
Bash

#!/bin/bash
# Usage: block-npiv [add npiv | remove dev]
dir=$(dirname "$0")
. "$dir/block-common.sh"
#set -x
#command=$1
# Find first alive non-VHOST
find_qla()
{
unset qla
for qla in /proc/scsi/qla2xxx/*; do
if grep -q "<DEAD>" $qla; then
continue
fi
if grep -q "VHOST index" $qla; then
continue
fi
return
done
}
# Find dev for NPIV
find_sdev()
{
unset dev
for host in /proc/scsi/qla2xxx/*; do
if grep -q $1 $host; then
h=${host##*/}
dev=`readlink /sys/class/scsi_device/$h*:0/device/block*`
dev=${dev##*/}
return
fi
done
}
# Find host for NPIV
find_host()
{
unset host
for host in /proc/scsi/qla2xxx/*; do
if grep -q $1 $host; then
host=${host##*/}
return
fi
done
}
# Find NPIV for dev
find_sdev_rev()
{
unset npiv
for host in /proc/scsi/qla2xxx/*; do
h=${host##*/}
if test ! -L /sys/class/scsi_device/$h*:0/device/block*; then
continue
fi
dev=`readlink /sys/class/scsi_device/$h*:0/device/block*`
dev=${dev##*/}
if test $dev = $1; then
npiv=`grep adapter-port= $host`
npiv=${npiv##*=}
npiv=${npiv%;}
return
fi
done
}
find_qla
test -z "$qla" && exit 2
case "$command" in
add)
par=`xenstore-read $XENBUS_PATH/params` || true
#par=$2
NPIV=$par
find_sdev $NPIV
if test -z "$dev"; then
echo "scsi-qlavportc=$NPIV" > $qla
sleep 8
find_host $NPIV
# echo "/sys/class/scsi_host/host$host/scan"
echo "- - -" > /sys/class/scsi_host/host$host/scan
sleep 1
find_sdev $NPIV
xenstore-write $XENBUS_PATH/node /dev/$dev
write_dev /dev/$dev
exit 0
fi
exit 1
;;
remove)
node=`xenstore-read $XENBUS_PATH/node` || true
#node=$2
dev=$node; dev=${dev#/dev/}
find_sdev_rev $dev
if test -x /sbin/blockdev -a -n "$node"; then blockdev --flushbufs $node; fi
test -z "$npiv" && exit 2
# echo "scsi-qlavportd" > $qla
echo "scsi-qlavportd=$npiv" > $qla
sleep 4
exit 0
;;
esac