diff --git a/plugins/hypervisors/kvm/src/main/java/org/apache/cloudstack/utils/qemu/QemuImageOptions.java b/plugins/hypervisors/kvm/src/main/java/org/apache/cloudstack/utils/qemu/QemuImageOptions.java index 4e2c1c4bc692..f9a2e4ba652e 100644 --- a/plugins/hypervisors/kvm/src/main/java/org/apache/cloudstack/utils/qemu/QemuImageOptions.java +++ b/plugins/hypervisors/kvm/src/main/java/org/apache/cloudstack/utils/qemu/QemuImageOptions.java @@ -18,7 +18,9 @@ import com.google.common.base.Joiner; +import java.util.Arrays; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.TreeMap; @@ -27,6 +29,10 @@ public class QemuImageOptions { private static final String FILENAME_PARAM_KEY = "file.filename"; private static final String LUKS_KEY_SECRET_PARAM_KEY = "key-secret"; private static final String QCOW2_KEY_SECRET_PARAM_KEY = "encrypt.key-secret"; + private static final String DRIVER = "driver"; + + private QemuImg.PhysicalDiskFormat format; + private static final List DISK_FORMATS_THAT_SUPPORT_OPTION_IMAGE_OPTS = Arrays.asList(QemuImg.PhysicalDiskFormat.QCOW2, QemuImg.PhysicalDiskFormat.LUKS); public QemuImageOptions(String filePath) { params.put(FILENAME_PARAM_KEY, filePath); @@ -55,14 +61,13 @@ public QemuImageOptions(QemuImg.PhysicalDiskFormat format, String filePath, Stri params.put(LUKS_KEY_SECRET_PARAM_KEY, secretName); } } - if (format != null) { - params.put("driver", format.toString()); - } + setFormat(format); } public void setFormat(QemuImg.PhysicalDiskFormat format) { if (format != null) { - params.put("driver", format.toString()); + params.put(DRIVER, format.toString()); + this.format = format; } } @@ -71,6 +76,9 @@ public void setFormat(QemuImg.PhysicalDiskFormat format) { * @return array of strings representing command flag and value (--image-opts) */ public String[] toCommandFlag() { + if (format == null || !DISK_FORMATS_THAT_SUPPORT_OPTION_IMAGE_OPTS.contains(format)) { + return new String[] { params.get(FILENAME_PARAM_KEY) }; + } Map sorted = new TreeMap<>(params); String paramString = Joiner.on(",").withKeyValueSeparator("=").join(sorted); return new String[] {"--image-opts", paramString}; diff --git a/plugins/hypervisors/kvm/src/test/java/org/apache/cloudstack/utils/qemu/QemuImageOptionsTest.java b/plugins/hypervisors/kvm/src/test/java/org/apache/cloudstack/utils/qemu/QemuImageOptionsTest.java index 2b56b69d1c5d..644c49543aa2 100644 --- a/plugins/hypervisors/kvm/src/test/java/org/apache/cloudstack/utils/qemu/QemuImageOptionsTest.java +++ b/plugins/hypervisors/kvm/src/test/java/org/apache/cloudstack/utils/qemu/QemuImageOptionsTest.java @@ -33,9 +33,9 @@ public static Collection data() { String imagePath = "/path/to/file"; String secretName = "secretname"; return Arrays.asList(new Object[][] { - { null, imagePath, null, new String[]{"--image-opts","file.filename=/path/to/file"} }, + { null, imagePath, null, new String[]{ imagePath } }, { QemuImg.PhysicalDiskFormat.QCOW2, imagePath, null, new String[]{"--image-opts",String.format("driver=qcow2,file.filename=%s", imagePath)} }, - { QemuImg.PhysicalDiskFormat.RAW, imagePath, secretName, new String[]{"--image-opts",String.format("driver=raw,file.filename=%s", imagePath)} }, + { QemuImg.PhysicalDiskFormat.RAW, imagePath, secretName, new String[]{ imagePath } }, { QemuImg.PhysicalDiskFormat.QCOW2, imagePath, secretName, new String[]{"--image-opts", String.format("driver=qcow2,encrypt.key-secret=%s,file.filename=%s", secretName, imagePath)} }, { QemuImg.PhysicalDiskFormat.LUKS, imagePath, secretName, new String[]{"--image-opts", String.format("driver=luks,file.filename=%s,key-secret=%s", imagePath, secretName)} } });