Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<QemuImg.PhysicalDiskFormat> 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);
Expand Down Expand Up @@ -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;
}
}

Expand All @@ -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<String, String> sorted = new TreeMap<>(params);
String paramString = Joiner.on(",").withKeyValueSeparator("=").join(sorted);
return new String[] {"--image-opts", paramString};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public static Collection<Object[]> 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)} }
});
Expand Down