diff options
author | Richard W.M. Jones <rjones@redhat.com> | 2012-07-23 12:54:08 +0100 |
---|---|---|
committer | Richard W.M. Jones <rjones@redhat.com> | 2012-07-23 13:02:25 +0100 |
commit | 67f7a30cc995be2aa5474422f708a2df093f5d52 (patch) | |
tree | f8532dc9e4988fa77aaee182c31b69164891f29f | |
parent | f2d7305091f538644a5ce6ce21ced320db60344d (diff) | |
download | libguestfs-67f7a30cc995be2aa5474422f708a2df093f5d52.tar.gz libguestfs-67f7a30cc995be2aa5474422f708a2df093f5d52.tar.xz libguestfs-67f7a30cc995be2aa5474422f708a2df093f5d52.zip |
launch: libvirt: Change disk XML according to whether host path is device or file (thanks Dan Berrange).
-rw-r--r-- | src/launch-libvirt.c | 49 |
1 files changed, 39 insertions, 10 deletions
diff --git a/src/launch-libvirt.c b/src/launch-libvirt.c index 1b667d11..12b0cd5c 100644 --- a/src/launch-libvirt.c +++ b/src/launch-libvirt.c @@ -44,6 +44,7 @@ #include <limits.h> #include <grp.h> #include <assert.h> +#include <sys/types.h> #include <sys/stat.h> #ifdef HAVE_LIBVIRT @@ -745,30 +746,58 @@ construct_libvirt_xml_disk (guestfs_h *g, xmlTextWriterPtr xo, char drive_name[64] = "sd"; char scsi_target[64]; char *path = NULL; + struct stat statbuf; + int is_host_device; guestfs___drive_name (drv_index, &drive_name[2]); snprintf (scsi_target, sizeof scsi_target, "%zu", drv_index); - /* Drive path must be absolute for libvirt. */ + /* Change the libvirt XML according to whether the host path is + * a device or a file. For devices, use: + * <disk type=block device=disk> + * <source dev=[path]> + * For files, use: + * <disk type=file device=disk> + * <source file=[path]> + */ + if (stat (drv->path, &statbuf) == -1) { + perrorf (g, "stat: %s", drv->path); + goto err; + } + is_host_device = S_ISBLK (statbuf.st_mode); + + /* Path must be absolute for libvirt. */ path = realpath (drv->path, NULL); if (path == NULL) { - perrorf (g, "realpath: could not convert '%s' to absolute path", drv->path); + perrorf (g, _("realpath: could not convert '%s' to absolute path"), + drv->path); goto err; } XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "disk")); XMLERROR (-1, - xmlTextWriterWriteAttribute (xo, BAD_CAST "type", - BAD_CAST "file")); - XMLERROR (-1, xmlTextWriterWriteAttribute (xo, BAD_CAST "device", BAD_CAST "disk")); + if (!is_host_device) { + XMLERROR (-1, + xmlTextWriterWriteAttribute (xo, BAD_CAST "type", + BAD_CAST "file")); - XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "source")); - XMLERROR (-1, - xmlTextWriterWriteAttribute (xo, BAD_CAST "file", - BAD_CAST path)); - XMLERROR (-1, xmlTextWriterEndElement (xo)); + XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "source")); + XMLERROR (-1, + xmlTextWriterWriteAttribute (xo, BAD_CAST "file", BAD_CAST path)); + XMLERROR (-1, xmlTextWriterEndElement (xo)); + } + else { + XMLERROR (-1, + xmlTextWriterWriteAttribute (xo, BAD_CAST "type", + BAD_CAST "block")); + + XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "source")); + XMLERROR (-1, + xmlTextWriterWriteAttribute (xo, BAD_CAST "dev", BAD_CAST path)); + XMLERROR (-1, xmlTextWriterEndElement (xo)); + } XMLERROR (-1, xmlTextWriterStartElement (xo, BAD_CAST "target")); XMLERROR (-1, |