Skip to content
Open
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
9 changes: 0 additions & 9 deletions plugins/hypervisors/kvm/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,6 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/QemuImg*.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1081,7 +1081,7 @@ private KVMPhysicalDisk createPhysicalDiskByQemuImg(String name, KVMStoragePool
destFile.setSize(size);
Map<String, String> options = new HashMap<String, String>();
if (List.of(StoragePoolType.NetworkFilesystem, StoragePoolType.Filesystem).contains(pool.getType())) {
options.put("preallocation", QemuImg.PreallocationType.getPreallocationType(provisioningType).toString());
options.put(QemuImg.PREALLOCATION, QemuImg.PreallocationType.getPreallocationType(provisioningType).toString());
}

try (KeyFile keyFile = new KeyFile(passphrase)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.regex.Pattern;

import org.apache.cloudstack.storage.formatinspector.Qcow2Inspector;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.NotImplementedException;
import org.apache.commons.lang3.StringUtils;
import org.libvirt.LibvirtException;
Expand All @@ -51,6 +52,7 @@ public class QemuImg {
public static final String ENCRYPT_FORMAT = "encrypt.format";
public static final String ENCRYPT_KEY_SECRET = "encrypt.key-secret";
public static final String TARGET_ZERO_FLAG = "--target-is-zero";
public static final String PREALLOCATION = "preallocation";
public static final long QEMU_2_10 = 2010000;
public static final long QEMU_5_10 = 5010000;

Expand Down Expand Up @@ -393,6 +395,17 @@ public void convert(final QemuImgFile srcFile, final QemuImgFile destFile,
convert(srcFile, destFile, options, qemuObjects, srcImageOpts, snapshotName, forceSourceFormat, false);
}

protected Map<String, String> getResizeOptionsFromConvertOptions(final Map<String, String> options) {
if (MapUtils.isEmpty(options)) {
return null;
}
Map<String, String> resizeOpts = new HashMap<>();
if (options.containsKey(PREALLOCATION)) {
resizeOpts.put(PREALLOCATION, options.get(PREALLOCATION));
}
return resizeOpts;
}

/**
* Converts an image from source to destination.
*
Expand Down Expand Up @@ -420,7 +433,6 @@ public void convert(final QemuImgFile srcFile, final QemuImgFile destFile,
public void convert(final QemuImgFile srcFile, final QemuImgFile destFile,
final Map<String, String> options, final List<QemuObject> qemuObjects, final QemuImageOptions srcImageOpts, final String snapshotName, final boolean forceSourceFormat,
boolean keepBitmaps) throws QemuImgException {

Script script = new Script(_qemuImgPath, timeout);
if (StringUtils.isNotBlank(snapshotName)) {
String qemuPath = Script.runSimpleBashScript(getQemuImgPathScript);
Expand Down Expand Up @@ -484,7 +496,7 @@ public void convert(final QemuImgFile srcFile, final QemuImgFile destFile,
}

if (srcFile.getSize() < destFile.getSize()) {
this.resize(destFile, destFile.getSize());
this.resize(destFile, destFile.getSize(), getResizeOptionsFromConvertOptions(options));
}
}

Expand Down Expand Up @@ -691,17 +703,18 @@ public void deleteSnapshot(final QemuImageOptions srcImageOpts, final String sna
}
}

private void addScriptOptionsFromMap(Map<String, String> options, Script s) {
if (options != null && !options.isEmpty()) {
s.add("-o");
final StringBuffer optionsBuffer = new StringBuffer();
for (final Map.Entry<String, String> option : options.entrySet()) {
optionsBuffer.append(option.getKey()).append('=').append(option.getValue()).append(',');
}
String optionsStr = optionsBuffer.toString();
optionsStr = optionsStr.replaceAll(",$", "");
s.add(optionsStr);
protected void addScriptOptionsFromMap(Map<String, String> options, Script s) {
if (MapUtils.isEmpty(options)) {
return;
}
s.add("-o");
final StringBuffer optionsBuffer = new StringBuffer();
for (final Map.Entry<String, String> option : options.entrySet()) {
optionsBuffer.append(option.getKey()).append('=').append(option.getValue()).append(',');
}
String optionsStr = optionsBuffer.toString();
optionsStr = optionsStr.replaceAll(",$", "");
s.add(optionsStr);
}

/**
Expand Down Expand Up @@ -747,19 +760,17 @@ public void rebase(final QemuImgFile file, final QemuImgFile backingFile, final

/**
* Resizes an image.
*
* <p>
* This method is a facade for 'qemu-img resize'.
*
* <p>
* A negative size value will get prefixed with '-' and a positive with '+'. Sizes are in bytes and will be passed on that way.
*
* @param file
* The file to be resized.
* @param size
* The new size.
* @param delta
* Flag to inform if the new size is a delta.
* @param file The file to be resized.
* @param size The new size.
* @param delta Flag to inform if the new size is a delta.
* @param options Script options for resizing. Takes a Map<String, String> with key value
*/
public void resize(final QemuImgFile file, final long size, final boolean delta) throws QemuImgException {
public void resize(final QemuImgFile file, final long size, final boolean delta, Map<String, String> options) throws QemuImgException {
String newSize = null;

if (size == 0) {
Expand All @@ -781,6 +792,7 @@ public void resize(final QemuImgFile file, final long size, final boolean delta)

final Script s = new Script(_qemuImgPath);
s.add("resize");
addScriptOptionsFromMap(options, s);
s.add(file.getFileName());
s.add(newSize);
s.execute();
Expand All @@ -789,7 +801,7 @@ public void resize(final QemuImgFile file, final long size, final boolean delta)
/**
* Resizes an image.
*
* This method is a facade for {@link QemuImg#resize(QemuImgFile, long, boolean)}.
* This method is a facade for {@link QemuImg#resize(QemuImgFile, long, boolean, Map)}.
*
* A negative size value will get prefixed with - and a positive with +. Sizes are in bytes and will be passed on that way.
*
Expand Down Expand Up @@ -818,18 +830,17 @@ public void resize(final QemuImageOptions imageOptions, final List<QemuObject> q

/**
* Resizes an image.
*
* This method is a facade for {@link QemuImg#resize(QemuImgFile, long, boolean)}.
*
* <p>
* This method is a facade for {@link QemuImg#resize(QemuImgFile, long, boolean, Map)}.
* <p>
* A negative size value will get prefixed with - and a positive with +. Sizes are in bytes and will be passed on that way.
*
* @param file
* The file to be resized.
* @param size
* The new size.
* @param file The file to be resized.
* @param size The new size.
* @param options Script options for resizing. Takes a Map<String, String> with key value
*/
public void resize(final QemuImgFile file, final long size) throws QemuImgException {
this.resize(file, size, false);
public void resize(final QemuImgFile file, final long size, Map<String, String> options) throws QemuImgException {
this.resize(file, size, false, options);
}

/**
Expand Down
Loading
Loading