From 75e8da1c5ca1b61a9a97fcc385eb5c6b9f83130f Mon Sep 17 00:00:00 2001
From: Ulrich Hecht <uli@suse.de>
Date: Tue, 14 Apr 2009 16:37:42 +0200
Subject: [PATCH 13/33] qemu-img-vmdk-scsi

Support creation of SCSI VMDK images in qemu-img.

Signed-off-by: Ulrich Hecht <uli@suse.de>
---
 block/vmdk.c     |    5 +++--
 block_int.h      |    1 +
 qemu-img-cmds.hx |    8 ++++----
 qemu-img.c       |   15 +++++++++++++--
 4 files changed, 21 insertions(+), 8 deletions(-)

Index: qemu-kvm-0.12.3/block/vmdk.c
===================================================================
--- qemu-kvm-0.12.3.orig/block/vmdk.c
+++ qemu-kvm-0.12.3/block/vmdk.c
@@ -710,7 +710,7 @@ static int vmdk_create(const char *filen
         "ddb.geometry.cylinders = \"%" PRId64 "\"\n"
         "ddb.geometry.heads = \"16\"\n"
         "ddb.geometry.sectors = \"63\"\n"
-        "ddb.adapterType = \"ide\"\n";
+        "ddb.adapterType = \"%s\"\n";
     char desc[1024];
     const char *real_filename, *temp_str;
     int64_t total_size = 0;
@@ -725,6 +725,8 @@ static int vmdk_create(const char *filen
             backing_file = options->value.s;
         } else if (!strcmp(options->name, BLOCK_OPT_COMPAT6)) {
             flags |= options->value.n ? BLOCK_FLAG_COMPAT6: 0;
+        } else if (!strcmp(options->name, BLOCK_OPT_SCSI)) {
+            flags |= options->value.n ? BLOCK_FLAG_SCSI: 0;
         }
         options++;
     }
@@ -800,7 +802,8 @@ static int vmdk_create(const char *filen
     snprintf(desc, sizeof(desc), desc_template, (unsigned int)time(NULL),
              total_size, real_filename,
              (flags & BLOCK_FLAG_COMPAT6 ? 6 : 4),
-             total_size / (int64_t)(63 * 16));
+             total_size / (int64_t)(63 * 16),
+             flags & BLOCK_FLAG_SCSI ? "lsilogic" : "ide");
 
     /* write the descriptor */
     lseek(fd, le64_to_cpu(header.desc_offset) << 9, SEEK_SET);
@@ -844,6 +847,11 @@ static QEMUOptionParameter vmdk_create_o
         .type = OPT_FLAG,
         .help = "VMDK version 6 image"
     },
+    {
+        .name = BLOCK_OPT_SCSI,
+        .type = OPT_FLAG,
+        .help = "SCSI image"
+    },
     { NULL }
 };
 
Index: qemu-kvm-0.12.3/block_int.h
===================================================================
--- qemu-kvm-0.12.3.orig/block_int.h
+++ qemu-kvm-0.12.3/block_int.h
@@ -30,10 +30,12 @@
 #define BLOCK_FLAG_ENCRYPT	1
 #define BLOCK_FLAG_COMPRESS	2
 #define BLOCK_FLAG_COMPAT6	4
+#define BLOCK_FLAG_SCSI	8
 
 #define BLOCK_OPT_SIZE          "size"
 #define BLOCK_OPT_ENCRYPT       "encryption"
 #define BLOCK_OPT_COMPAT6       "compat6"
+#define BLOCK_OPT_SCSI          "scsi"
 #define BLOCK_OPT_BACKING_FILE  "backing_file"
 #define BLOCK_OPT_BACKING_FMT   "backing_fmt"
 #define BLOCK_OPT_CLUSTER_SIZE  "cluster_size"
Index: qemu-kvm-0.12.3/qemu-img-cmds.hx
===================================================================
--- qemu-kvm-0.12.3.orig/qemu-img-cmds.hx
+++ qemu-kvm-0.12.3/qemu-img-cmds.hx
@@ -16,9 +16,9 @@ STEXI
 ETEXI
 
 DEF("create", img_create,
-    "create [-f fmt] [-o options] filename [size]")
+    "create [-s] [-f fmt] [-o options] filename [size]")
 STEXI
-@item create [-f @var{fmt}] [-o @var{options}] @var{filename} [@var{size}]
+@item create [-s] [-f @var{fmt}] [-o @var{options}] @var{filename} [@var{size}]
 ETEXI
 
 DEF("commit", img_commit,
@@ -28,9 +28,9 @@ STEXI
 ETEXI
 
 DEF("convert", img_convert,
-    "convert [-c] [-f fmt] [-O output_fmt] [-o options] filename [filename2 [...]] output_filename")
+    "convert [-c] [-s] [-f fmt] [-O output_fmt] [-o options] filename [filename2 [...]] output_filename")
 STEXI
-@item convert [-c] [-f @var{fmt}] [-O @var{output_fmt}] [-o @var{options}] @var{filename} [@var{filename2} [...]] @var{output_filename}
+@item convert [-c] [-s] [-f @var{fmt}] [-O @var{output_fmt}] [-o @var{options}] @var{filename} [@var{filename2} [...]] @var{output_filename}
 ETEXI
 
 DEF("info", img_info,
Index: qemu-kvm-0.12.3/qemu-img.c
===================================================================
--- qemu-kvm-0.12.3.orig/qemu-img.c
+++ qemu-kvm-0.12.3/qemu-img.c
@@ -227,6 +227,11 @@ static void add_old_style_options(const
             error("VMDK version 6 not supported for file format '%s'", fmt);
         }
     }
+    if (flags & BLOCK_FLAG_SCSI) {
+        if (set_option_parameter(list, BLOCK_OPT_SCSI, "on")) {
+            error("SCSI not supported for file format '%s'", fmt);
+        }
+    }
 
     if (base_filename) {
         if (set_option_parameter(list, BLOCK_OPT_BACKING_FILE, base_filename)) {
@@ -253,7 +258,7 @@ static int img_create(int argc, char **a
 
     flags = 0;
     for(;;) {
-        c = getopt(argc, argv, "F:b:f:he6o:");
+        c = getopt(argc, argv, "F:b:f:hes6o:");
         if (c == -1)
             break;
         switch(c) {
@@ -272,6 +277,9 @@ static int img_create(int argc, char **a
         case 'e':
             flags |= BLOCK_FLAG_ENCRYPT;
             break;
+        case 's':
+            flags |= BLOCK_FLAG_SCSI;
+            break;
         case '6':
             flags |= BLOCK_FLAG_COMPAT6;
             break;
@@ -354,6 +362,8 @@ static int img_create(int argc, char **a
 
     printf("Formatting '%s', fmt=%s ", filename, fmt);
     print_option_parameters(param);
+    if (flags & BLOCK_FLAG_SCSI)
+        printf(", SCSI");
     puts("");
 
     ret = bdrv_create(drv, filename, param);
@@ -548,7 +558,7 @@ static int img_convert(int argc, char **
     out_baseimg = NULL;
     flags = 0;
     for(;;) {
-        c = getopt(argc, argv, "f:O:B:hce6o:");
+        c = getopt(argc, argv, "f:O:B:hces6o:");
         if (c == -1)
             break;
         switch(c) {
@@ -570,6 +580,9 @@ static int img_convert(int argc, char **
         case 'e':
             flags |= BLOCK_FLAG_ENCRYPT;
             break;
+        case 's':
+            flags |= BLOCK_FLAG_SCSI;
+            break;
         case '6':
             flags |= BLOCK_FLAG_COMPAT6;
             break;
@@ -637,6 +650,9 @@ static int img_convert(int argc, char **
         }
     }
 
+    if (flags & BLOCK_FLAG_SCSI && strcmp(drv->format_name, "vmdk"))
+        error("SCSI devices not supported for this file format");
+
     /* Create the new image */
     ret = bdrv_create(drv, out_filename, param);
     free_option_parameters(param);
