Skip to content

Commit 2c9471e

Browse files
committed
kvm: use preallocation option for fat disk resize
Fixes #10589 Signed-off-by: Abhishek Kumar <[email protected]>
1 parent 671d8ad commit 2c9471e

File tree

3 files changed

+40
-36
lines changed

3 files changed

+40
-36
lines changed

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/LibvirtStorageAdaptor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1081,7 +1081,7 @@ private KVMPhysicalDisk createPhysicalDiskByQemuImg(String name, KVMStoragePool
10811081
destFile.setSize(size);
10821082
Map<String, String> options = new HashMap<String, String>();
10831083
if (List.of(StoragePoolType.NetworkFilesystem, StoragePoolType.Filesystem).contains(pool.getType())) {
1084-
options.put("preallocation", QemuImg.PreallocationType.getPreallocationType(provisioningType).toString());
1084+
options.put(QemuImg.PREALLOCATION, QemuImg.PreallocationType.getPreallocationType(provisioningType).toString());
10851085
}
10861086

10871087
try (KeyFile keyFile = new KeyFile(passphrase)) {

plugins/hypervisors/kvm/src/main/java/org/apache/cloudstack/utils/qemu/QemuImg.java

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.regex.Pattern;
2626

2727
import org.apache.cloudstack.storage.formatinspector.Qcow2Inspector;
28+
import org.apache.commons.collections.MapUtils;
2829
import org.apache.commons.lang.NotImplementedException;
2930
import org.apache.commons.lang3.StringUtils;
3031
import org.libvirt.LibvirtException;
@@ -51,6 +52,7 @@ public class QemuImg {
5152
public static final String ENCRYPT_FORMAT = "encrypt.format";
5253
public static final String ENCRYPT_KEY_SECRET = "encrypt.key-secret";
5354
public static final String TARGET_ZERO_FLAG = "--target-is-zero";
55+
public static final String PREALLOCATION = "preallocation";
5456
public static final long QEMU_2_10 = 2010000;
5557
public static final long QEMU_5_10 = 5010000;
5658

@@ -420,7 +422,6 @@ public void convert(final QemuImgFile srcFile, final QemuImgFile destFile,
420422
public void convert(final QemuImgFile srcFile, final QemuImgFile destFile,
421423
final Map<String, String> options, final List<QemuObject> qemuObjects, final QemuImageOptions srcImageOpts, final String snapshotName, final boolean forceSourceFormat,
422424
boolean keepBitmaps) throws QemuImgException {
423-
424425
Script script = new Script(_qemuImgPath, timeout);
425426
if (StringUtils.isNotBlank(snapshotName)) {
426427
String qemuPath = Script.runSimpleBashScript(getQemuImgPathScript);
@@ -484,7 +485,11 @@ public void convert(final QemuImgFile srcFile, final QemuImgFile destFile,
484485
}
485486

486487
if (srcFile.getSize() < destFile.getSize()) {
487-
this.resize(destFile, destFile.getSize());
488+
Map<String, String> resizeOpts = new HashMap<>();
489+
if (options.containsKey(PREALLOCATION)) {
490+
resizeOpts.put(PREALLOCATION, options.get(PREALLOCATION));
491+
}
492+
this.resize(destFile, destFile.getSize(), resizeOpts);
488493
}
489494
}
490495

@@ -692,16 +697,17 @@ public void deleteSnapshot(final QemuImageOptions srcImageOpts, final String sna
692697
}
693698

694699
private void addScriptOptionsFromMap(Map<String, String> options, Script s) {
695-
if (options != null && !options.isEmpty()) {
696-
s.add("-o");
697-
final StringBuffer optionsBuffer = new StringBuffer();
698-
for (final Map.Entry<String, String> option : options.entrySet()) {
699-
optionsBuffer.append(option.getKey()).append('=').append(option.getValue()).append(',');
700-
}
701-
String optionsStr = optionsBuffer.toString();
702-
optionsStr = optionsStr.replaceAll(",$", "");
703-
s.add(optionsStr);
700+
if (MapUtils.isEmpty(options)) {
701+
return;
704702
}
703+
s.add("-o");
704+
final StringBuffer optionsBuffer = new StringBuffer();
705+
for (final Map.Entry<String, String> option : options.entrySet()) {
706+
optionsBuffer.append(option.getKey()).append('=').append(option.getValue()).append(',');
707+
}
708+
String optionsStr = optionsBuffer.toString();
709+
optionsStr = optionsStr.replaceAll(",$", "");
710+
s.add(optionsStr);
705711
}
706712

707713
/**
@@ -747,19 +753,17 @@ public void rebase(final QemuImgFile file, final QemuImgFile backingFile, final
747753

748754
/**
749755
* Resizes an image.
750-
*
756+
* <p>
751757
* This method is a facade for 'qemu-img resize'.
752-
*
758+
* <p>
753759
* A negative size value will get prefixed with '-' and a positive with '+'. Sizes are in bytes and will be passed on that way.
754760
*
755-
* @param file
756-
* The file to be resized.
757-
* @param size
758-
* The new size.
759-
* @param delta
760-
* Flag to inform if the new size is a delta.
761+
* @param file The file to be resized.
762+
* @param size The new size.
763+
* @param delta Flag to inform if the new size is a delta.
764+
* @param options Script options for resizing. Takes a Map<String, String> with key value
761765
*/
762-
public void resize(final QemuImgFile file, final long size, final boolean delta) throws QemuImgException {
766+
public void resize(final QemuImgFile file, final long size, final boolean delta, Map<String, String> options) throws QemuImgException {
763767
String newSize = null;
764768

765769
if (size == 0) {
@@ -781,6 +785,7 @@ public void resize(final QemuImgFile file, final long size, final boolean delta)
781785

782786
final Script s = new Script(_qemuImgPath);
783787
s.add("resize");
788+
addScriptOptionsFromMap(options, s);
784789
s.add(file.getFileName());
785790
s.add(newSize);
786791
s.execute();
@@ -789,7 +794,7 @@ public void resize(final QemuImgFile file, final long size, final boolean delta)
789794
/**
790795
* Resizes an image.
791796
*
792-
* This method is a facade for {@link QemuImg#resize(QemuImgFile, long, boolean)}.
797+
* This method is a facade for {@link QemuImg#resize(QemuImgFile, long, boolean, Map)}.
793798
*
794799
* A negative size value will get prefixed with - and a positive with +. Sizes are in bytes and will be passed on that way.
795800
*
@@ -818,18 +823,17 @@ public void resize(final QemuImageOptions imageOptions, final List<QemuObject> q
818823

819824
/**
820825
* Resizes an image.
821-
*
822-
* This method is a facade for {@link QemuImg#resize(QemuImgFile, long, boolean)}.
823-
*
826+
* <p>
827+
* This method is a facade for {@link QemuImg#resize(QemuImgFile, long, boolean, Map)}.
828+
* <p>
824829
* A negative size value will get prefixed with - and a positive with +. Sizes are in bytes and will be passed on that way.
825830
*
826-
* @param file
827-
* The file to be resized.
828-
* @param size
829-
* The new size.
831+
* @param file The file to be resized.
832+
* @param size The new size.
833+
* @param options Script options for resizing. Takes a Map<String, String> with key value
830834
*/
831-
public void resize(final QemuImgFile file, final long size) throws QemuImgException {
832-
this.resize(file, size, false);
835+
public void resize(final QemuImgFile file, final long size, Map<String, String> options) throws QemuImgException {
836+
this.resize(file, size, false, options);
833837
}
834838

835839
/**

plugins/hypervisors/kvm/src/test/java/org/apache/cloudstack/utils/qemu/QemuImgTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public void testCreateAndResize() throws QemuImgException, LibvirtException {
162162
try {
163163
QemuImg qemu = new QemuImg(0);
164164
qemu.create(file);
165-
qemu.resize(file, endSize);
165+
qemu.resize(file, endSize, null);
166166
Map<String, String> info = qemu.info(file);
167167

168168
if (info == null) {
@@ -191,7 +191,7 @@ public void testCreateAndResizeDeltaPositive() throws QemuImgException, LibvirtE
191191
try {
192192
QemuImg qemu = new QemuImg(0);
193193
qemu.create(file);
194-
qemu.resize(file, increment, true);
194+
qemu.resize(file, increment, true, null);
195195
Map<String, String> info = qemu.info(file);
196196

197197
if (info == null) {
@@ -219,7 +219,7 @@ public void testCreateAndResizeDeltaNegative() throws QemuImgException, LibvirtE
219219
try {
220220
QemuImg qemu = new QemuImg(0);
221221
qemu.create(file);
222-
qemu.resize(file, increment, true);
222+
qemu.resize(file, increment, true, null);
223223
Map<String, String> info = qemu.info(file);
224224

225225
if (info == null) {
@@ -249,7 +249,7 @@ public void testCreateAndResizeFail() throws QemuImgException, LibvirtException
249249
QemuImg qemu = new QemuImg(0);
250250
try {
251251
qemu.create(file);
252-
qemu.resize(file, endSize);
252+
qemu.resize(file, endSize, null);
253253
} finally {
254254
File f = new File(filename);
255255
f.delete();
@@ -265,7 +265,7 @@ public void testCreateAndResizeZero() throws QemuImgException, LibvirtException
265265

266266
QemuImg qemu = new QemuImg(0);
267267
qemu.create(file);
268-
qemu.resize(file, 0);
268+
qemu.resize(file, 0, null);
269269

270270
File f = new File(filename);
271271
f.delete();

0 commit comments

Comments
 (0)