diff mbox

v3 - Add exclusive locking option to block-iscsi

Message ID f854a325cfc48af3957fc4eae3018b5e@crc.id.au (mailing list archive)
State New, archived
Headers show

Commit Message

Steven Haigh May 9, 2016, 4:22 a.m. UTC
On 2016-05-05 15:52, Steven Haigh wrote:
> On 2016-05-05 12:32, Steven Haigh wrote:
>> Overview
>> 
>> If you're using iSCSI, you can mount a target by multiple Dom0
>> machines on the same target. For non-cluster aware filesystems, this
>> can lead to disk corruption and general bad times by all. The iSCSI
>> protocol allows the use of persistent reservations as per the SCSI
>> disk spec. Low level SCSI commands for locking are handled by the
>> sg_persist program (bundled with sg3_utils package in EL).
>> 
>> The aim of this patch is to create a 'locktarget=y' option specified
>> within the disk 'target' command for iSCSI to lock the target in
>> exclusive mode on VM start with a key generated from the local systems
>> IP, and release this lock on the shutdown of the DomU.
>> 
>> Example Config:
>> disk            =
>> ['script=block-iscsi,vdev=xvda,target=iqn=iqn.1986-03.com.sun:02:mytarget,portal=iscsi.example.com,locktarget=y']
>> 
>> In writing this, I have also re-factored parts of the script to put
>> some things in what I believe to be a better place to make expansion
>> easier. This is mainly in removing functions that purely call other
>> functions with no actual code execution.
>> 
>> Signed-off-by: Steven Haigh <netwiz@crc.id.au>
>> 
>> (on a side note, first time I've submitted a patch to the list and I'm
>> currently stuck on a webmail client, so apologies in advance if this
>> all goes wrong ;)
> 
> Changes in v2:
> Bugfix: Call find_device to locate the /dev/sdX component of the iSCSI
> target before trying to run unlock_device().
> 
> Apologies for this oversight.
> 

Changes in v3:
* Split the block-iscsi cleanup into a seperate patch 
(block-iscsi-locking-v3_01_simplify_block-iscsi.patch).
* Add locking in second patch file 
(block-iscsi-locking-v3_02_add_locking.patch)

Comments

Wei Liu May 12, 2016, 11:02 a.m. UTC | #1
Hi Steven

On Mon, May 09, 2016 at 02:22:48PM +1000, Steven Haigh wrote:
> On 2016-05-05 15:52, Steven Haigh wrote:
> >On 2016-05-05 12:32, Steven Haigh wrote:
> >>Overview
> >>
> >>If you're using iSCSI, you can mount a target by multiple Dom0
> >>machines on the same target. For non-cluster aware filesystems, this
> >>can lead to disk corruption and general bad times by all. The iSCSI
> >>protocol allows the use of persistent reservations as per the SCSI
> >>disk spec. Low level SCSI commands for locking are handled by the
> >>sg_persist program (bundled with sg3_utils package in EL).
> >>
> >>The aim of this patch is to create a 'locktarget=y' option specified
> >>within the disk 'target' command for iSCSI to lock the target in
> >>exclusive mode on VM start with a key generated from the local systems
> >>IP, and release this lock on the shutdown of the DomU.
> >>
> >>Example Config:
> >>disk            =
> >>['script=block-iscsi,vdev=xvda,target=iqn=iqn.1986-03.com.sun:02:mytarget,portal=iscsi.example.com,locktarget=y']

You seem to suggest an extension (locktarget) to disk spec as well but
you patch doesn't contain modification to
docs/txt/misc/xl-disk-configuration.txt.

Wei.
Steven Haigh May 16, 2016, 12:42 a.m. UTC | #2
On 2016-05-12 21:02, Wei Liu wrote:
> Hi Steven
> 
> On Mon, May 09, 2016 at 02:22:48PM +1000, Steven Haigh wrote:
>> On 2016-05-05 15:52, Steven Haigh wrote:
>> >On 2016-05-05 12:32, Steven Haigh wrote:
>> >>Overview
>> >>
>> >>If you're using iSCSI, you can mount a target by multiple Dom0
>> >>machines on the same target. For non-cluster aware filesystems, this
>> >>can lead to disk corruption and general bad times by all. The iSCSI
>> >>protocol allows the use of persistent reservations as per the SCSI
>> >>disk spec. Low level SCSI commands for locking are handled by the
>> >>sg_persist program (bundled with sg3_utils package in EL).
>> >>
>> >>The aim of this patch is to create a 'locktarget=y' option specified
>> >>within the disk 'target' command for iSCSI to lock the target in
>> >>exclusive mode on VM start with a key generated from the local systems
>> >>IP, and release this lock on the shutdown of the DomU.
>> >>
>> >>Example Config:
>> >>disk            =
>> >>['script=block-iscsi,vdev=xvda,target=iqn=iqn.1986-03.com.sun:02:mytarget,portal=iscsi.example.com,locktarget=y']
> 
> You seem to suggest an extension (locktarget) to disk spec as well but
> you patch doesn't contain modification to
> docs/txt/misc/xl-disk-configuration.txt.

Correct. There is no documentation for the existing block-iscsi script 
within xl-disk-configuration.txt.

In fact, there is no mention at all regarding block-iscsi in any of the 
documentation that I can see.
diff mbox

Patch

--- block-iscsi 2016-05-09 15:16:35.447480532 +1000
+++ block-iscsi-lock    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
     ;;
 *)