From 575bcd86ee0ce6d3082126415d371424fc7b5bdb Mon Sep 17 00:00:00 2001
From: Anthony PERARD <anthony.perard@citrix.com>
Date: Thu, 8 Mar 2018 18:16:41 +0000
Subject: [PATCH] libxl: Specify format of inserted cdrom

Without this extra parameter on the QMP command, QEMU will guess the
format of the new file.

This is XSA-258.

Signed-off-by: Anthony PERARD <anthony.perard@citrix.com>
Acked-by: Ian Jackson <ian.jackson@eu.citrix.com>
---
 tools/libxl/libxl_device.c   | 12 ++++++++++++
 tools/libxl/libxl_dm.c       | 20 ++++++--------------
 tools/libxl/libxl_internal.h |  1 +
 tools/libxl/libxl_qmp.c      |  2 ++
 4 files changed, 21 insertions(+), 14 deletions(-)

Index: xen-4.5.5-testing/tools/libxl/libxl_device.c
===================================================================
--- xen-4.5.5-testing.orig/tools/libxl/libxl_device.c
+++ xen-4.5.5-testing/tools/libxl/libxl_device.c
@@ -395,6 +395,18 @@ char *libxl__device_disk_string_of_backe
     }
 }
 
+const char *libxl__qemu_disk_format_string(libxl_disk_format format)
+{
+    switch (format) {
+    case LIBXL_DISK_FORMAT_QCOW: return "qcow";
+    case LIBXL_DISK_FORMAT_QCOW2: return "qcow2";
+    case LIBXL_DISK_FORMAT_VHD: return "vpc";
+    case LIBXL_DISK_FORMAT_RAW: return "raw";
+    case LIBXL_DISK_FORMAT_EMPTY: return NULL;
+    default: return NULL;
+    }
+}
+
 int libxl__device_physdisk_major_minor(const char *physpath, int *major, int *minor)
 {
     struct stat buf;
Index: xen-4.5.5-testing/tools/libxl/libxl_dm.c
===================================================================
--- xen-4.5.5-testing.orig/tools/libxl/libxl_dm.c
+++ xen-4.5.5-testing/tools/libxl/libxl_dm.c
@@ -374,18 +374,6 @@ static char ** libxl__build_device_model
     return (char **) flexarray_contents(dm_args);
 }
 
-static const char *qemu_disk_format_string(libxl_disk_format format)
-{
-    switch (format) {
-    case LIBXL_DISK_FORMAT_QCOW: return "qcow";
-    case LIBXL_DISK_FORMAT_QCOW2: return "qcow2";
-    case LIBXL_DISK_FORMAT_VHD: return "vpc";
-    case LIBXL_DISK_FORMAT_RAW: return "raw";
-    case LIBXL_DISK_FORMAT_EMPTY: return NULL;
-    default: return NULL;
-    }
-}
-
 static char *dm_spice_options(libxl__gc *gc,
                                     const libxl_spice_info *spice)
 {
@@ -815,7 +803,7 @@ static char ** libxl__build_device_model
             int disk, part;
             int dev_number =
                 libxl__device_disk_dev_number(disks[i].vdev, &disk, &part);
-            const char *format = qemu_disk_format_string(disks[i].format);
+            const char *format;
             char *drive;
             const char *pdev_path;
 
@@ -825,6 +813,11 @@ static char ** libxl__build_device_model
                 continue;
             }
 
+            if (disks[i].backend == LIBXL_DISK_BACKEND_QDISK)
+                format = libxl__qemu_disk_format_string(disks[i].format);
+            else
+                format = libxl__qemu_disk_format_string(LIBXL_DISK_FORMAT_RAW);
+
             if (disks[i].is_cdrom) {
                 if (disks[i].format == LIBXL_DISK_FORMAT_EMPTY)
                     drive = libxl__sprintf
@@ -848,7 +841,6 @@ static char ** libxl__build_device_model
                 }
 
                 if (disks[i].backend == LIBXL_DISK_BACKEND_TAP) {
-                    format = qemu_disk_format_string(LIBXL_DISK_FORMAT_RAW);
                     pdev_path = libxl__blktap_devpath(gc, disks[i].pdev_path,
                                                       disks[i].format);
                 } else {
Index: xen-4.5.5-testing/tools/libxl/libxl_internal.h
===================================================================
--- xen-4.5.5-testing.orig/tools/libxl/libxl_internal.h
+++ xen-4.5.5-testing/tools/libxl/libxl_internal.h
@@ -1048,6 +1048,7 @@ _hidden int libxl__domain_pvcontrol_writ
 /* from xl_device */
 _hidden char *libxl__device_disk_string_of_backend(libxl_disk_backend backend);
 _hidden char *libxl__device_disk_string_of_format(libxl_disk_format format);
+_hidden const char *libxl__qemu_disk_format_string(libxl_disk_format format);
 _hidden int libxl__device_disk_set_backend(libxl__gc*, libxl_device_disk*);
 
 _hidden int libxl__device_physdisk_major_minor(const char *physpath, int *major, int *minor);
Index: xen-4.5.5-testing/tools/libxl/libxl_qmp.c
===================================================================
--- xen-4.5.5-testing.orig/tools/libxl/libxl_qmp.c
+++ xen-4.5.5-testing/tools/libxl/libxl_qmp.c
@@ -940,6 +940,8 @@ int libxl__qmp_insert_cdrom(libxl__gc *g
         return qmp_run_command(gc, domid, "eject", args, NULL, NULL);
     } else {
         qmp_parameters_add_string(gc, &args, "target", disk->pdev_path);
+        qmp_parameters_add_string(gc, &args, "arg",
+            libxl__qemu_disk_format_string(disk->format));
         return qmp_run_command(gc, domid, "change", args, NULL, NULL);
     }
 }
