Skip to contentSkip to conversation

libxl: support custom block hotplug scripts

PATCH Superseded Ian Campbell 2 participants 4 messages Aug 1, 2012 – Aug 2, 2012

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 libxl: support custom block hotplug scripts

# HG changeset patch
# User Ian Campbell <[email protected]> # Date 1343815041 -3600
# Node ID 84f0686ebcbfb0fa3a437def65513038be389736 # Parent 006588b1bbb609186df45770a3c16d3028b54778
libxl: support custom block hotplug scripts

These are provided using the "script=" syntax described in docs/misc/xl-disk-configuration.txt.

The existing hotplug scripts currently conflate two different concepts, namely that of making a datapath available in the backend domain (logging into iSCSI LUNs and the like) and that of actually connecting that datapath to a Xen backend path (e.g. writing "physical-device" node in xenstore to bring up blkback).

For this reason the script support implemented here is only supported in conjunction with backendtype=phy.

Eventually we hope to rework the hotplug scripts to separate the to concepts, but that is not 4.2 material.

In addition there are some other subtleties:

- Previously in the blktap case we would add "script = .../blktap" to the backend flex array, but then jumped to the PHY case which added "script = .../block" too. The block one takes precendence since it comes second.

This was, accidentally, correct. The blktap script is for blktap1 devices and not blktap2 devices. libxl completely manages the blktap2 side of things without resorting to hotplug scripts and creates a blkback device directly. Therefore the "block" script is always the correct one to call. Custom script are not supported in this context.

- libxl should not write the "physical-device" node. This is the responsibility of the block script. Writing the "physical-device" node in libxl basically completely short-cuts the standard block hotplug script which uses "physical-device" to know if it has run already or not.

In the case of more complex scripts libxl cannot know the right value to write here anyway, in particular the device may not exist until after the script is called.

This change has the side effect of re-enabling the checks for device sharing aspect of the default block script, which I have tested and which now cause libxl to properly abort now that libxl properly checks for hotplug script errors.

There is no sharing check for blktap2 since even if you reuse the same vhd the resulting tap device is different. I would have preferred to simply write the "physical-device" node for the blktap2 case but the hotplug script infrastructure is not currently setup to handle LIBXL__DEVICE_KIND_VBD devices without a hotplug script (backendtype phy and tap both end up as KIND_VBD). Changing this was more surgery than I was happy doing for 4.2 and therefore I have simply hardcoded to the block script for the LIBXL_DISK_BACKEND_TAP case.

- libxl__device_disk_set_backend running against a phy device with a script cannot stat the device to check its properties since it may not exist until the script is run. Therefore I have special cased this in disk_try_backend to simply assume that backend == phy is always ok if a script was configured. Similarly the other backend types are always rejected if a script was configured.

Note that the reason for implementing the default script behaviour in device_disk_add instead of libxl__device_disk_setdefault is because we need to be able to tell when the script was user-supplied rather than defaulted by libxl in order to correctly implement the above. The setdefault function must be idempotent so we cannot simply update disk->script.

I suspect that for 4.3 a script member should be added to libxl__device, this would also help in the case above of handling devices with no script in a consistent manner. This is not 4.2 material.

- When the block script falls through and shells out to a block-$type script it used to pass "$node" however the only place this was assigned was in the remove+phy case (in which case it contains the file:// derived /dev/loopN device), and in that case the script exits without falling through to the block-$type case.

Since libxl never creates a type other than phy this never happens in practice anyway and we now call the correct block-$type script directly. But fix it up anyway since it is confusing.

- The block-nbd and block-enbd scripts which we supply appear to be broken WRT the hotplug calling convention, in that they seem to expect a command line parameter (perhaps the $node described above) rather than reading the appropriate node from xenstore.

I rather suspect this was broken by 7774:e2e7f47e6f79 in November 2005. I think it is safe to say no one is using these scripts! I haven't fixed this here. It would be good to track down some working scripts and either incorproate them or defer to them in their existing home (e.g. if they live somewhere useful like the nbd tools package).

- Added a few block script related entries to check-xl-disk-parse from
http://backdrift.org/xen-block-iscsi-script-with-multipath-support and
http://lists.linbit.com/pipermail/drbd-user/2008-September/010221.html /
http://www.drbd.org/users-guide-emb/s-xen-configure-domu.html
(and snuck in another interesting empty CDROM case)

This highlighted two bugs in the libxlu disk parser handling of the deprecated "<script>:" prefix:

- It was failing to prefix with "block-" to construct the actual script name

- The regex for matching iscsi or drdb or e?nbd was incorrect

- Use libxl__abs_path for the nic script too. Just because the existing code nearly tricked me into repeating the mistake

I have tested with a custom block script which uses "lvchange -a" to dynamically add remove the referenced device (simulates iSCSI login/logout without requiring me to faff around setting up an iSCSI target). I also tested on a blktap2 system.

I haven't directly tested anything more complex like iscsi: or nbd: other than what check-xl-disk-parse exercises.

---
v2:
- observe that script= requires backendtype=phy and substantially rework to correctly reflect that.
- remove unintentional braces change in SAVESTRING macro

tools/hotplug/Linux/block +1 -1
diff -r 006588b1bbb6 -r 84f0686ebcbf tools/hotplug/Linux/block
@@ -342,4 +342,4 @@ esac
 
 # If we've reached here, $t is neither phy nor file, so fire a helper script.
 [ -x ${XEN_SCRIPT_DIR}/block-"$t" ] && \
-  ${XEN_SCRIPT_DIR}/block-"$t" "$command" $node
+  ${XEN_SCRIPT_DIR}/block-"$t" "$command"
tools/libxl/check-xl-disk-parse +39 -0
diff -r 006588b1bbb6 -r 84f0686ebcbf tools/libxl/check-xl-disk-parse
@@ -142,5 +142,44 @@ disk: {
 
 EOF
 one 0 vdev=hdc,access=r,devtype=cdrom,format=empty
+one 0 vdev=hdc,access=r,devtype=cdrom
+
+expected <<EOF
+disk: {
+    "backend_domid": 0,
+    "pdev_path": "iqn.2001-05.com.equallogic:0-8a0906-23fe93404-c82797962054a96d-examplehost",
+    "vdev": "xvda",
+    "backend": "unknown",
+    "format": "raw",
+    "script": "block-iscsi",
+    "removable": 0,
+    "readwrite": 1,
+    "is_cdrom": 0
+}
+
+EOF
+
+# http://backdrift.org/xen-block-iscsi-script-with-multipath-support
+one 0 iscsi:iqn.2001-05.com.equallogic:0-8a0906-23fe93404-c82797962054a96d-examplehost,xvda,w
+one 0 vdev=xvda,access=w,script=block-iscsi,target=iqn.2001-05.com.equallogic:0-8a0906-23fe93404-c82797962054a96d-examplehost
+
+expected <<EOF
+disk: {
+    "backend_domid": 0,
+    "pdev_path": "app01",
+    "vdev": "hda",
+    "backend": "unknown",
+    "format": "raw",
+    "script": "block-drbd",
+    "removable": 0,
+    "readwrite": 1,
+    "is_cdrom": 0
+}
+
+EOF
+
+# http://lists.linbit.com/pipermail/drbd-user/2008-September/010221.html
+# http://www.drbd.org/users-guide-emb/s-xen-configure-domu.html
+one 0 drbd:app01,hda,w
 
 complete
tools/libxl/libxl.c +18 -28

81 lines not on this page · raw message

tools/libxl/libxl_device.c +20 -4
diff -r 006588b1bbb6 -r 84f0686ebcbf tools/libxl/libxl_device.c
@@ -191,18 +191,26 @@ typedef struct {
 } disk_try_backend_args;
 
 static int disk_try_backend(disk_try_backend_args *a,
-                            libxl_disk_backend backend) {
+                            libxl_disk_backend backend)
+ {
+    libxl__gc *gc = a->gc;
     /* returns 0 (ie, DISK_BACKEND_UNKNOWN) on failure, or
      * backend on success */
-    libxl_ctx *ctx = libxl__gc_owner(a->gc);
+    libxl_ctx *ctx = libxl__gc_owner(gc);
+
     switch (backend) {
-
     case LIBXL_DISK_BACKEND_PHY:
         if (!(a->disk->format == LIBXL_DISK_FORMAT_RAW ||
               a->disk->format == LIBXL_DISK_FORMAT_EMPTY)) {
             goto bad_format;
         }
 
+        if (a->disk->script) {
+            LOG(DEBUG, "Disk vdev=%s, uses script=... assuming phy backend",
+                a->disk->vdev);
+            return backend;
+        }
+
         if (libxl__try_phy_backend(a->stab.st_mode))
             return backend;
 
@@ -212,6 +220,8 @@ static int disk_try_backend(disk_try_bac
         return 0;
 
     case LIBXL_DISK_BACKEND_TAP:
+        if (a->disk->script) goto bad_script;
+
         if (!libxl__blktap_enabled(a->gc)) {
             LIBXL__LOG(ctx, LIBXL__LOG_DEBUG, "Disk vdev=%s, backend tap"
                        " unsuitable because blktap not available",
@@ -225,6 +235,7 @@ static int disk_try_backend(disk_try_bac
         return backend;
 
     case LIBXL_DISK_BACKEND_QDISK:
+        if (a->disk->script) goto bad_script;
         return backend;
 
     default:
@@ -242,6 +253,11 @@ static int disk_try_backend(disk_try_bac
                libxl_disk_backend_to_string(backend),
                libxl_disk_format_to_string(a->disk->format));
     return 0;
+
+ bad_script:
+    LOG(DEBUG, "Disk vdev=%s, backend %s not compatible with script=...",
+        a->disk->vdev, libxl_disk_backend_to_string(backend));
+    return 0;
 }
 
 int libxl__device_disk_set_backend(libxl__gc *gc, libxl_device_disk *disk) {
@@ -264,7 +280,7 @@ int libxl__device_disk_set_backend(libxl
             return ERROR_INVAL;
         }
         memset(&a.stab, 0, sizeof(a.stab));
-    } else {
+    } else if (!disk->script) {
         if (stat(disk->pdev_path, &a.stab)) {
             LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "Disk vdev=%s "
                              "failed to stat: %s",
tools/libxl/libxlu_disk_l.c +283 -279

748 lines not on this page · raw message

tools/libxl/libxlu_disk_l.h +1 -1
diff -r 006588b1bbb6 -r 84f0686ebcbf tools/libxl/libxlu_disk_l.h
@@ -340,7 +340,7 @@ extern int xlu__disk_yylex (yyscan_t yys
 #undef YY_DECL
 #endif
 
-#line 244 "libxlu_disk_l.l"
+#line 255 "libxlu_disk_l.l"
 
 #line 346 "libxlu_disk_l.h"
 #undef xlu__disk_yyIN_HEADER
tools/libxl/libxlu_disk_l.l +15 -4
diff -r 006588b1bbb6 -r 84f0686ebcbf tools/libxl/libxlu_disk_l.l
@@ -188,11 +188,22 @@ target=.*	{ STRIP(','); SAVESTRING("targ
                     setformat(DPC, yytext);
                  }
 
-iscsi:|e?nbd:drbd:/.* {
-		    STRIP(':');
+(iscsi|e?nbd|drbd):/.* {
+                    STRIP(':');
                     DPC->had_depr_prefix=1; DEPRECATE("use `script=...'");
-		    SAVESTRING("script", script, yytext);
-		}
+                    if (DPC->disk->script) {
+                        if (*DPC->disk->script) {
+                            xlu__disk_err(DPC,yytext,"script respecified");
+                            return 0;
+                        }
+                        /* do not complain about overwriting empty strings */
+                        free(DPC->disk->script);
+                    }
+                    DPC->disk->script = malloc(strlen("block-")
+                                               +strlen(yytext) + 1);
+                    strcpy(DPC->disk->script, "block-");
+                    strcat(DPC->disk->script, yytext);
+                }
 
 tapdisk:/.*	{ DPC->had_depr_prefix=1; DEPRECATE(0); }
 tap2?:/.*	{ DPC->had_depr_prefix=1; DEPRECATE(0); }
#1 lore raw
Quoted 3 lines · Ian Campbell

I have tested with a custom block script which uses "lvchange -a" to dynamically add remove the referenced device (simulates iSCSI login/logout without requiring me to faff around setting up an iSCSI target).

Script below for reference. Configured with:
'access=w,vdev=xvde,script=block-lvm,target=VG=VG LV=trash'

Once you've done the initial "lvchange -a n /dev/VG/trash" then you should find that /dv/VG/trash exists only when the domain exists.

I deliberately chose a target with = in it to valid the "target= eats to end of line" use case. As expected it works, the params node ends up as "VG=VG LV=trash". This also exercised the behaviour of not stat()ing the device in libxl.

8<-------------------------------------------

#!/bin/bash

dir=$(dirname "$0")
. "$dir/block-common.sh"

p=$(xenstore_read "$XENBUS_PATH/params")
evalVariables $p

exec 1>>/tmp/block-lvm.log
DEV=/dev/$VG/$LV

echo block-lvm $command on date
echo VG=$VG LV=$LV DEV=$DEV

case "$command" in
add)
if [ -e $DEV ] ; then
fatal "$DEV already active, disable with \`lvchange -a n $DEV\'"
fi
lvchange -a y $DEV
write_dev $DEV
exit 0
;;
remove)
lvchange -a n $DEV
exit 0
;;
esac

#2 lore raw
In reply to Ian Campbell

Ian Campbell writes ("[PATCH V2] libxl: support custom block hotplug scripts"):

Quoted 1 line

libxl: support custom block hotplug scripts

Wow. Thanks. Everything looks good apart from this:

Quoted 5 lines
                     DPC->had_depr_prefix=1; DEPRECATE("use `script=...'");
tools/libxl/libxlu_disk_l.c +2 -2
-                   SAVESTRING("script", script, yytext);
-               }
+                    if (DPC->disk->script) {
+                        if (*DPC->disk->script) {

tools/libxl/libxlu_disk_l.c case 14: · line 1340

Quoted 10 lines
tools/libxl/libxlu_disk_l.c +10 -0
+                            xlu__disk_err(DPC,yytext,"script respecified");
+                            return 0;
+                        }
+                        /* do not complain about overwriting empty strings */
+                        free(DPC->disk->script);
+                    }
+                    DPC->disk->script = malloc(strlen("block-")
+                                               +strlen(yytext) + 1);
+                    strcpy(DPC->disk->script, "block-");
+                    strcat(DPC->disk->script, yytext);

Isn't this very like the contents of the savestring() function ? Ie you could do:

        char *newscript;
        asprintf(&newscript, ...);
        savestring(DPC, "script respecified", &DPC->disk->script, newscript);
        free(newscript);

Other places in xl use asprintf so you can use it here.

Quoted 1 line

+ }

Is this one-character indentation change intentional ?

Ian.

#3 lore raw
Quoted 23 lines · Ian Jackson

Ian Campbell writes ("[PATCH V2] libxl: support custom block hotplug scripts"):

Quoted 1 line

libxl: support custom block hotplug scripts

Wow. Thanks. Everything looks good apart from this:

Quoted 5 lines
                     DPC->had_depr_prefix=1; DEPRECATE("use `script=...'");
tools/libxl/libxlu_disk_l.c +2 -2
-                   SAVESTRING("script", script, yytext);
-               }
+                    if (DPC->disk->script) {
+                        if (*DPC->disk->script) {

tools/libxl/libxlu_disk_l.c case 14: · line 1340

Quoted 10 lines
tools/libxl/libxlu_disk_l.c +10 -0
+                            xlu__disk_err(DPC,yytext,"script respecified");
+                            return 0;
+                        }
+                        /* do not complain about overwriting empty strings */
+                        free(DPC->disk->script);
+                    }
+                    DPC->disk->script = malloc(strlen("block-")
+                                               +strlen(yytext) + 1);
+                    strcpy(DPC->disk->script, "block-");
+                    strcat(DPC->disk->script, yytext);

Isn't this very like the contents of the savestring() function ?

Quoted 7 lines

Ie you could do:

        char *newscript;
        asprintf(&newscript, ...);
        savestring(DPC, "script respecified", &DPC->disk->script, newscript);
        free(newscript);

Other places in xl use asprintf so you can use it here.

I hadn't realised asprintf was fair game. I'll rework along these lines.

Quoted 3 lines
Quoted 1 line

+ }

Is this one-character indentation change intentional ?

There's a few stray hard tabs in this file (which is predominantly using 8 space indendation). I think I fixed it in the blocks I was changing which will look like a 1 char reindent in an MUA.

Ian.