Skip to contentSkip to conversation

libxl: support custom block hotplug scripts

PATCH In review Ian Campbell 2 participants 3 messages Aug 3, 2012

lore
Share & tools
lore
#0 lore raw

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

# HG changeset patch
# User Ian Campbell <[email protected]> # Date 1343980890 -3600
# Node ID bfd5e107774c3ba5020719861288b9de4e4da68b # Parent 8e1090d822e51e484414856a43f297b34ecfeb2d
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.

---
v3:
- incorporate IanJ's improvements to docs/misc/xl-disk-configuration.txt

    from <[email protected]>

- wrap commit message to 70 columns
- use asprintf instead of open coding savestring. Using this requires libxl_osdep.h is included before stdio.h, which means moving it from libxlu_disk_i.h (included in the %{ } section of the .l file) into a new %top { } section so that it goes before the boiler plates own include of stdio.h. The other includer of libxlu_disk_i.h (libxlu_disk.c) already has its own libxl_osdep.h as the first include.

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

docs/misc/xl-disk-configuration.txt +5 -2
diff -r 8e1090d822e5 -r bfd5e107774c docs/misc/xl-disk-configuration.txt
@@ -160,7 +160,10 @@ script=<script>
 ---------------
 
 Specifies that <target> is not a normal host path, but rather
-information to be interpreted by /etc/xen/scripts/block-<script>.
+information to be interpreted by the executable program <script>,
+(looked for in /etc/xen/scripts, if it doesn't contain a slash).
+
+These scripts are normally called "block-<script>".
 
 
 
@@ -204,7 +207,7 @@ Supported values:      iscsi:  nbd:  enb
 In xend and old versions of libxl it was necessary to specify the
 "script" (see above) with a prefix.  For compatibility, these four
 prefixes are recognised as specifying the corresponding script.  They
-are equivalent to "script=<script>".
+are equivalent to "script=block-<script>".
 
 
 <deprecated-prefix>:
tools/hotplug/Linux/block +1 -1
diff -r 8e1090d822e5 -r bfd5e107774c 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 8e1090d822e5 -r bfd5e107774c 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 8e1090d822e5 -r bfd5e107774c 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_i.h +0 -2
diff -r 8e1090d822e5 -r bfd5e107774c tools/libxl/libxlu_disk_i.h
@@ -1,8 +1,6 @@
 #ifndef LIBXLU_DISK_I_H
 #define LIBXLU_DISK_I_H
 
-#include "libxl_osdeps.h" /* must come before any other headers */
-
 #include "libxlu_internal.h"
 
 
tools/libxl/libxlu_disk_l.c +301 -296

865 lines not on this page · raw message

tools/libxl/libxlu_disk_l.h +7 -3
diff -r 8e1090d822e5 -r bfd5e107774c tools/libxl/libxlu_disk_l.h
@@ -3,8 +3,12 @@
 #define xlu__disk_yyIN_HEADER 1
 
 #line 6 "libxlu_disk_l.h"
+#line 31 "libxlu_disk_l.l"
+#include "libxl_osdeps.h" /* must come before any other headers */
 
-#line 8 "libxlu_disk_l.h"
+
+
+#line 12 "libxlu_disk_l.h"
 
 #define  YY_INT_ALIGNED short int
 
@@ -340,8 +344,8 @@ 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"
+#line 350 "libxlu_disk_l.h"
 #undef xlu__disk_yyIN_HEADER
 #endif /* xlu__disk_yyHEADER_H */
tools/libxl/libxlu_disk_l.l +15 -4
diff -r 8e1090d822e5 -r bfd5e107774c tools/libxl/libxlu_disk_l.l
@@ -27,6 +27,10 @@
  * syntax's "target=" should be used.
  */
 
+%top{
+#include "libxl_osdeps.h" /* must come before any other headers */
+}
+
 %{
 #include "libxlu_disk_i.h"
 
@@ -188,11 +192,18 @@ target=.*	{ STRIP(','); SAVESTRING("targ
                     setformat(DPC, yytext);
                  }
 
-iscsi:|e?nbd:drbd:/.* {
-		    STRIP(':');
+(iscsi|e?nbd|drbd):/.* {
+                    char *newscript;
+                    STRIP(':');
                     DPC->had_depr_prefix=1; DEPRECATE("use `script=...'");
-		    SAVESTRING("script", script, yytext);
-		}
+                    if (asprintf(&newscript, "block-%s", yytext) < 0) {
+                            xlu__disk_err(DPC,yytext,"unable to format script");
+                            return 0;
+                    }
+                    savestring(DPC, "script respecified",
+                               &DPC->disk->script, newscript);
+                    free(newscript);
+                }
 
 tapdisk:/.*	{ DPC->had_depr_prefix=1; DEPRECATE(0); }
 tap2?:/.*	{ DPC->had_depr_prefix=1; DEPRECATE(0); }