--- block-iscsi.orig 2016-05-09 15:16:35.447480532 +1000 +++ block-iscsi 2016-05-05 15:43:58.222159351 +1000 @@ -37,8 +37,7 @@ { # set multipath default value multipath="n" - for param in $(echo "$1" | tr "," "\n") - do + for param in $(echo "$1" | tr "," "\n"); do case $param in iqn=*) iqn=$(remove_label $param "iqn=") @@ -55,6 +54,15 @@ fatal "Multipath selected, but no multipath tools found" fi ;; + locktarget=*) + locktarget=$(remove_label $param "locktarget=") + if ! command -v sg_persist > /dev/null 2>&1; then + fatal "Locking requested but no sg_persist found" + fi + if ! command -v gethostip > /dev/null 2>&1; then + fatal "Locking requested but no gethostip found for key generation" + fi + ;; esac done if [ -z "$iqn" ] || [ -z "$portal" ]; then @@ -92,6 +100,31 @@ fi } + +lock_device() +{ + ## Lock the iSCSI target as Exclusive Access. + key=$(gethostip -x $(uname -n)) + if ! sg_persist -d ${dev} -o -G -S ${key} > /dev/null; then + unlock_device + iscsiadm -m node --targetname "$iqn" -p "$portal" --logout > /dev/null + fatal "iSCSI LOCK: Failed to register with target" + fi + if ! sg_persist -d ${dev} -o -R -K ${key} -T 6 > /dev/null; then + unlock_device + iscsiadm -m node --targetname "$iqn" -p "$portal" --logout > /dev/null + fatal "iSCSI LOCK: Failed to set persistent reservation" + fi +} + +unlock_device() +{ + ## Unlock the iSCSI target. + key=$(gethostip -x $(uname -n)) + sg_persist -d ${dev} -o -L -K ${key} -T 6 > /dev/null || true + sg_persist -d ${dev} -o -G -K ${key} -S 0 > /dev/null || true +} + command=$1 target=$(xenstore-read $XENBUS_PATH/params || true) if [ -z "$target" ]; then @@ -110,10 +143,17 @@ ## Login to the iSCSI target. do_or_die iscsiadm -m node --targetname "$iqn" -p "$portal" --login > /dev/null - + find_device + if [ "$locktarget" = "y" ]; then + lock_device + fi write_dev $dev ;; remove) + find_device + if [ "$locktarget" = "y" ]; then + unlock_device + fi do_or_die iscsiadm -m node --targetname "$iqn" -p "$portal" --logout > /dev/null ;; *)