From f2c832257aa3670f7fb3af4d61b74a7f81aa4a72 Mon Sep 17 00:00:00 2001
From: Rafael Monnerat <rafael@nexedi.com>
Date: Wed, 29 Aug 2018 16:38:36 +0200
Subject: [PATCH] kvm: allow to set main disk format

  it  clean up....
---
 .../kvm/instance-kvm-cluster-input-schema.json   |  7 +++++++
 software/kvm/instance-kvm-cluster.cfg.jinja2.in  |  1 +
 software/kvm/instance-kvm-input-schema.json      |  8 ++++++++
 software/kvm/instance-kvm.cfg.jinja2             |  4 ++--
 software/kvm/software.cfg                        |  6 +++---
 software/kvm/template/template-kvm-run.in        | 16 ++++++++++------
 6 files changed, 31 insertions(+), 11 deletions(-)

diff --git a/software/kvm/instance-kvm-cluster-input-schema.json b/software/kvm/instance-kvm-cluster-input-schema.json
index 9ca468b4e..76bc1631a 100644
--- a/software/kvm/instance-kvm-cluster-input-schema.json
+++ b/software/kvm/instance-kvm-cluster-input-schema.json
@@ -232,6 +232,13 @@
               "default": 10,
               "minimum": 1
             },
+            "disk-format": {
+              "title": "Type of external disk drive to create by QEMU.",
+              "description": "Type of QEMU disk drive, to create.",
+              "type": "string",
+              "default": "qcow2",
+              "enum": ["qcow2", "raw", "vdi", "vmdk", "cloop", "qed"]
+            },
             "disk-type": {
               "title": "Disk type",
               "description": "Type of QEMU disk drive.",
diff --git a/software/kvm/instance-kvm-cluster.cfg.jinja2.in b/software/kvm/instance-kvm-cluster.cfg.jinja2.in
index 5405eb2b5..7f5949b92 100644
--- a/software/kvm/instance-kvm-cluster.cfg.jinja2.in
+++ b/software/kvm/instance-kvm-cluster.cfg.jinja2.in
@@ -56,6 +56,7 @@ config-enable-device-hotplug = {{ dumps(kvm_parameter_dict.get('enable-device-ho
 config-ram-hotplug-slot-size = {{ dumps(kvm_parameter_dict.get('ram-hotplug-slot-size', 512)) }}
 config-disk-size = {{ dumps(kvm_parameter_dict.get('disk-size', 10)) }}
 config-disk-type = {{ dumps(kvm_parameter_dict.get('disk-type', 'virtio')) }}
+config-disk-format = {{ dumps(kvm_parameter_dict.get('disk-format', 'qcow2')) }}
 config-cpu-count = {{ dumps(kvm_parameter_dict.get('cpu-count', 1)) }}
 config-cpu-max-count = {{ dumps(kvm_parameter_dict.get('cpu-max-count', 24)) }}
 {{ setconfig('numa', kvm_parameter_dict.get('numa', '')) }}
diff --git a/software/kvm/instance-kvm-input-schema.json b/software/kvm/instance-kvm-input-schema.json
index e2e5edcb0..9aa6bbc56 100644
--- a/software/kvm/instance-kvm-input-schema.json
+++ b/software/kvm/instance-kvm-input-schema.json
@@ -55,6 +55,14 @@
       "default": "virtio",
       "enum": ["ide", "scsi", "sd", "mtd", "floppy", "pflash", "virtio"]
     },
+    "disk-format": {
+      "title": "Type of disk drive to create by QEMU.",
+      "description": "Type of QEMU disk drive, to create.",
+      "type": "string",
+      "default": "qcow2",
+      "enum": ["qcow2", "raw", "vdi", "vmdk", "cloop", "qed"]
+    },
+
     "disk-cache": {
       "title": "Cache option to use with Disk.",
       "description": "Disk cache controls how the host cache is used to access block data.",
diff --git a/software/kvm/instance-kvm.cfg.jinja2 b/software/kvm/instance-kvm.cfg.jinja2
index cb63c1a66..3c1844bb1 100644
--- a/software/kvm/instance-kvm.cfg.jinja2
+++ b/software/kvm/instance-kvm.cfg.jinja2
@@ -88,10 +88,10 @@ nbd2-port = ${slap-parameter:nbd2-port}
 
 tap-interface = ${slap-network-information:network-interface}
 
-disk-path = ${directory:srv}/virtual.qcow2
 disk-size = ${slap-parameter:disk-size}
 disk-type = ${slap-parameter:disk-type}
-
+disk-format = ${slap-parameter:disk-format}
+disk-path = ${directory:srv}/virtual.${slap-parameter:disk-format}
 
 pid-file-path = ${kvm-controller-parameter-dict:pid-file}
 socket-path = ${kvm-controller-parameter-dict:socket-path}
diff --git a/software/kvm/software.cfg b/software/kvm/software.cfg
index b5d9ccbab..5d50ee708 100644
--- a/software/kvm/software.cfg
+++ b/software/kvm/software.cfg
@@ -99,7 +99,7 @@ recipe = hexagonit.recipe.download
 ignore-existing = true
 url = ${:_profile_base_location_}/instance-kvm.cfg.jinja2
 mode = 644
-md5sum = e59ea29533d7f989ec676e14b0f29839
+md5sum = 92e0723a2b474d9cc686ef7f0a91e36c
 download-only = true
 on-update = true
 
@@ -108,7 +108,7 @@ recipe = hexagonit.recipe.download
 ignore-existing = true
 url = ${:_profile_base_location_}/instance-kvm-cluster.cfg.jinja2.in
 mode = 644
-md5sum = 6d165aec7d236ea3944765236d11940f
+md5sum = cccabafc001c6df35da95af6bf2ae28e 
 download-only = true
 on-update = true
 
@@ -186,7 +186,7 @@ ignore-existing = true
 url = ${:_profile_base_location_}/template/template-kvm-run.in
 mode = 644
 filename = template-kvm-run.in
-md5sum = bc09717dd86063a558b125f43905b4a4
+md5sum = 807e03a9ada561499adf447f79e9d2ec
 download-only = true
 on-update = true
 
diff --git a/software/kvm/template/template-kvm-run.in b/software/kvm/template/template-kvm-run.in
index b93115805..4e9975171 100644
--- a/software/kvm/template/template-kvm-run.in
+++ b/software/kvm/template/template-kvm-run.in
@@ -20,6 +20,10 @@ qemu_img_path = '{{ parameter_dict.get("qemu-img-path") }}'
 qemu_path = '{{ parameter_dict.get("qemu-path") }}'
 disk_size = '{{ parameter_dict.get("disk-size") }}'
 disk_type = '{{ parameter_dict.get("disk-type") }}'
+disk_format = '{{ parameter_dict.get("disk-format", "qcow2")}}'
+disk_format = disk_format \
+              if disk_format in ['qcow2', 'raw', 'vdi', 'vmdk', 'cloop', 'qed'] else 'qcow2'
+
 socket_path = '{{ parameter_dict.get("socket-path") }}'
 nbd_list = (('{{ parameter_dict.get("nbd-host") }}',
               {{ parameter_dict.get("nbd-port") }}),
@@ -46,7 +50,9 @@ init_ram_size = {{ parameter_dict.get("init-ram-size") }}
 pid_file_path = '{{ parameter_dict.get("pid-file-path") }}'
 external_disk_number = {{ parameter_dict.get("external-disk-number") }}
 external_disk_size = {{ parameter_dict.get("external-disk-size") }}
-external_disk_format = '{{ parameter_dict.get("external-disk-format") }}'
+external_disk_format = '{{ parameter_dict.get("external-disk-format", "qcow2") }}'
+external_disk_format = external_disk_format \
+              if external_disk_format in ['qcow2', 'raw', 'vdi', 'vmdk', 'cloop', 'qed'] else 'qcow2'
 disk_storage_dict = {}
 disk_storage_list = """{{ parameter_dict.get("disk-storage-list") }}""".split('\n')
 map_storage_list = []
@@ -185,7 +191,7 @@ if not os.path.exists(disk_path) and virtual_hard_drive_url != '':
 # XXX: move to Buildout profile
 if not os.path.exists(disk_path):
   print('Creating virtual hard drive...')
-  subprocess.check_call([qemu_img_path, 'create' ,'-f', 'qcow2',
+  subprocess.check_call([qemu_img_path, 'create' ,'-f', disk_format,
       disk_path, '%sG' % disk_size])
   print('Done.')
 
@@ -196,9 +202,6 @@ for storage in disk_storage_list:
     key, val = storage.split(' ')
     disk_storage_dict[key.strip()] = val.strip()
 
-if not external_disk_format in ['qcow2', 'raw', 'vdi', 'vmdk', 'cloop', 'qed']:
-  external_disk_format = 'qcow2'
-
 map_storage_list, external_disk_number = getMapStorageList(disk_storage_dict,
                                                       int(external_disk_number))
 
@@ -214,7 +217,7 @@ if disk_storage_dict:
         disk_list = glob.glob('%s.*' % os.path.join(path, 'kvm_virtual_disk'))
         if disk_list == []:
           print('Creating one additional virtual hard drive...')
-          process = subprocess.check_call([qemu_img_path, 'create' ,'-f', '%s' % external_disk_format,
+          process = subprocess.check_call([qemu_img_path, 'create' ,'-f', external_disk_format,
               disk_filepath, '%sG' % external_disk_size])
         else:
           # Cannot change or recreate if disk is exists
@@ -264,6 +267,7 @@ if enable_device_hotplug != 'true':
 else:
   smp = '1,maxcpus=%s' % smp_max_count
   ram = '%sM,slots=128,maxmem=%sM' % (init_ram_size, ram_max_size)
+
 kvm_argument_list = [qemu_path,
   '-enable-kvm', '-smp', smp, '-name', vm_name, '-m', ram, '-vga', 'std',
   '-drive', 'file=%s,if=%s,cache=%s,aio=%s' % (disk_path, disk_type, disk_cache, disk_aio),
-- 
2.30.9