[Libguestfs] [PATCH 2/4] New APIs: add-drive{, -ro}-with-if allows you to set QEMU block emulation.

Richard W.M. Jones rjones at redhat.com
Fri Feb 12 12:45:24 UTC 2010


-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
virt-p2v converts physical machines to virtual machines.  Boot with a
live CD or over the network (PXE) and turn machines into Xen guests.
http://et.redhat.com/~rjones/virt-p2v
-------------- next part --------------
>From 946dc06bb861a38cae959416362e4561c9e4829a Mon Sep 17 00:00:00 2001
From: Richard Jones <rjones at redhat.com>
Date: Fri, 12 Feb 2010 11:47:18 +0000
Subject: [PATCH 2/4] New APIs: add-drive{,-ro}-with-if allows you to set QEMU block emulation.

The default if=... comes from configure time (currently it
defaults to if=virtio).

This change allows you to set the QEMU block emulation.

We don't think this will be used very often, but virt-v2v
requires it in order to work around a subtle problem with
running 'mkinitrd' in an appliance attached to a guest.
---
 src/generator.ml |   25 +++++++++++++++++++++++++
 src/guestfs.c    |   24 +++++++++++++++++++-----
 2 files changed, 44 insertions(+), 5 deletions(-)

diff --git a/src/generator.ml b/src/generator.ml
index 7db58eb..607b6d1 100755
--- a/src/generator.ml
+++ b/src/generator.ml
@@ -489,9 +489,15 @@ image).
 
 This is equivalent to the qemu parameter
 C<-drive file=filename,cache=off,if=...>.
+
 C<cache=off> is omitted in cases where it is not supported by
 the underlying filesystem.
 
+C<if=...> is set at compile time by the configuration option
+C<./configure --with-drive-if=...>.  In the rare case where you
+might need to change this at run time, use C<guestfs_add_drive_with_if>
+or C<guestfs_add_drive_ro_with_if>.
+
 Note that this call checks for the existence of C<filename>.  This
 stops you from specifying other types of drive which are supported
 by qemu such as C<nbd:> and C<http:> URLs.  To specify those, use
@@ -540,6 +546,11 @@ changes to be committed, although qemu can support this.
 This is equivalent to the qemu parameter
 C<-drive file=filename,snapshot=on,if=...>.
 
+C<if=...> is set at compile time by the configuration option
+C<./configure --with-drive-if=...>.  In the rare case where you
+might need to change this at run time, use C<guestfs_add_drive_with_if>
+or C<guestfs_add_drive_ro_with_if>.
+
 Note that this call checks for the existence of C<filename>.  This
 stops you from specifying other types of drive which are supported
 by qemu such as C<nbd:> and C<http:> URLs.  To specify those, use
@@ -887,6 +898,20 @@ qemu, which is not very helpful.");
    "\
 Return the recovery process enabled flag.");
 
+  ("add_drive_with_if", (RErr, [String "filename"; String "iface"]), -1, [],
+   [],
+   "add a drive specifying the QEMU block emulation to use",
+   "\
+This is the same as C<guestfs_add_drive> but it allows you
+to specify the QEMU interface emulation to use at run time.");
+
+  ("add_drive_ro_with_if", (RErr, [String "filename"; String "iface"]), -1, [],
+   [],
+   "add a drive read-only specifying the QEMU block emulation to use",
+   "\
+This is the same as C<guestfs_add_drive_ro> but it allows you
+to specify the QEMU interface emulation to use at run time.");
+
 ]
 
 (* daemon_functions are any functions which cause some action
diff --git a/src/guestfs.c b/src/guestfs.c
index 9908e7f..3c00114 100644
--- a/src/guestfs.c
+++ b/src/guestfs.c
@@ -748,7 +748,8 @@ guestfs__config (guestfs_h *g,
 }
 
 int
-guestfs__add_drive (guestfs_h *g, const char *filename)
+guestfs__add_drive_with_if (guestfs_h *g, const char *filename,
+                            const char *drive_if)
 {
   size_t len = strlen (filename) + 64;
   char buf[len];
@@ -771,12 +772,12 @@ guestfs__add_drive (guestfs_h *g, const char *filename)
   int fd = open (filename, O_RDONLY|O_DIRECT);
   if (fd >= 0) {
     close (fd);
-    snprintf (buf, len, "file=%s,cache=off,if=" DRIVE_IF, filename);
+    snprintf (buf, len, "file=%s,cache=off,if=%s", filename, drive_if);
   } else {
     fd = open (filename, O_RDONLY);
     if (fd >= 0) {
       close (fd);
-      snprintf (buf, len, "file=%s,if=" DRIVE_IF, filename);
+      snprintf (buf, len, "file=%s,if=%s", filename, drive_if);
     } else {
       perrorf (g, "%s", filename);
       return -1;
@@ -787,7 +788,8 @@ guestfs__add_drive (guestfs_h *g, const char *filename)
 }
 
 int
-guestfs__add_drive_ro (guestfs_h *g, const char *filename)
+guestfs__add_drive_ro_with_if (guestfs_h *g, const char *filename,
+                               const char *drive_if)
 {
   size_t len = strlen (filename) + 64;
   char buf[len];
@@ -802,12 +804,24 @@ guestfs__add_drive_ro (guestfs_h *g, const char *filename)
     return -1;
   }
 
-  snprintf (buf, len, "file=%s,snapshot=on,if=%s", filename, DRIVE_IF);
+  snprintf (buf, len, "file=%s,snapshot=on,if=%s", filename, drive_if);
 
   return guestfs__config (g, "-drive", buf);
 }
 
 int
+guestfs__add_drive (guestfs_h *g, const char *filename)
+{
+  return guestfs__add_drive_with_if (g, filename, DRIVE_IF);
+}
+
+int
+guestfs__add_drive_ro (guestfs_h *g, const char *filename)
+{
+  return guestfs__add_drive_ro_with_if (g, filename, DRIVE_IF);
+}
+
+int
 guestfs__add_cdrom (guestfs_h *g, const char *filename)
 {
   if (strchr (filename, ',') != NULL) {
-- 
1.6.5.2



More information about the Libguestfs mailing list