2525import java .util .regex .Pattern ;
2626
2727import org .apache .cloudstack .storage .formatinspector .Qcow2Inspector ;
28+ import org .apache .commons .collections .MapUtils ;
2829import org .apache .commons .lang .NotImplementedException ;
2930import org .apache .commons .lang3 .StringUtils ;
3031import 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 /**
0 commit comments