You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by wi...@apache.org on 2013/02/20 22:52:58 UTC

[2/2] git commit: refs/heads/qemu-img - Added more tests and implemented adding optiomns to create and convert.

Updated Branches:
  refs/heads/qemu-img 5e6ff4b39 -> 593b367b8


Added more tests and implemented adding optiomns to create and convert.

Also more JavaDocs


Project: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/commit/593b367b
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/tree/593b367b
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/diff/593b367b

Branch: refs/heads/qemu-img
Commit: 593b367b88cfc7d2c254c19897bac28d6b9af3ef
Parents: af50747
Author: Wido den Hollander <wi...@42on.com>
Authored: Wed Feb 20 22:52:25 2013 +0100
Committer: Wido den Hollander <wi...@42on.com>
Committed: Wed Feb 20 22:52:25 2013 +0100

----------------------------------------------------------------------
 .../org/apache/cloudstack/utils/qemu/QemuImg.java  |  119 ++++++++++++++-
 .../apache/cloudstack/utils/qemu/QemuImgTest.java  |   32 ++++-
 2 files changed, 143 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/593b367b/utils/src/org/apache/cloudstack/utils/qemu/QemuImg.java
----------------------------------------------------------------------
diff --git a/utils/src/org/apache/cloudstack/utils/qemu/QemuImg.java b/utils/src/org/apache/cloudstack/utils/qemu/QemuImg.java
index dfaf275..b54c9b2 100644
--- a/utils/src/org/apache/cloudstack/utils/qemu/QemuImg.java
+++ b/utils/src/org/apache/cloudstack/utils/qemu/QemuImg.java
@@ -60,16 +60,38 @@ public class QemuImg {
 
     }
 
-    /* Create a new disk image */
-    public void create(QemuImgFile file, QemuImgFile backingFile, List<Map<String, String>> options) {
+    /**
+     * Create a new image
+     *
+     * This method calls 'qemu-img create'
+     * 
+     * @param file
+     *            The file to create
+     * @param backingFile
+     *            A backing file if used (for example with qcow2)
+     * @param options
+     *            Options for the create. Takes a Map<String, String> with key value
+     *            pairs which are passed on to qemu-img without validation.
+     * @return void
+     */
+    public void create(QemuImgFile file, QemuImgFile backingFile, Map<String, String> options) {
         Script s = new Script(_qemuImgPath);
         s.add("create");
-        s.add("-f");
+
+        if (options != null && !options.isEmpty()) {
+            s.add("-o");
+            String optionsStr = "";
+            for (Map.Entry<String, String> option : options.entrySet()) {
+                optionsStr += option.getKey() + "=" + option.getValue() + ",";
+            }
+            s.add(optionsStr);
+        }
 
         /*
             -b for a backing file does not show up in the docs, but it works.
             Shouldn't this be -o backing_file=filename instead?
         */
+        s.add("-f");
         if (backingFile != null) {
             s.add(backingFile.getFormat().toString());
             s.add("-b");
@@ -86,15 +108,66 @@ public class QemuImg {
         s.execute();
     }
 
+    /**
+     * Create a new image
+     *
+     * This method calls 'qemu-img create'
+     *
+     * @param file
+     *            The file to create
+     * @return void
+     */
     public void create(QemuImgFile file) {
         this.create(file, null, null);
     }
 
+    /**
+     * Create a new image
+     *
+     * This method calls 'qemu-img create'
+     *
+     * @param file
+     *            The file to create
+     * @param backingFile
+     *            A backing file if used (for example with qcow2)
+     * @return void
+     */
     public void create(QemuImgFile file, QemuImgFile backingFile) {
         this.create(file, backingFile, null);
     }
 
-    /* Convert the disk image filename or a snapshot snapshot_name to disk image output_filename using format output_fmt. */
+    /**
+     * Create a new image
+     *
+     * This method calls 'qemu-img create'
+     *
+     * @param file
+     *            The file to create
+     * @param options
+     *            Options for the create. Takes a Map<String, String> with key value
+     *            pairs which are passed on to qemu-img without validation.
+     * @return void
+     */
+    public void create(QemuImgFile file, Map<String, String> options) {
+        this.create(file, null, options);
+    }
+
+    /**
+     * Convert a image from source to destination
+     *
+     * This method calls 'qemu-img convert' and takes two objects
+     * as an argument.
+     * 
+     *
+     * @param srcFile
+     *            The source file
+     * @param destFile
+     *            The destination file
+     * @param options
+     *            Options for the convert. Takes a Map<String, String> with key value
+     *            pairs which are passed on to qemu-img without validation.
+     * @return void
+     */
     public void convert(QemuImgFile srcFile, QemuImgFile destFile, Map<String, String> options) {
         Script s = new Script(_qemuImgPath);
         s.add("convert");
@@ -102,11 +175,34 @@ public class QemuImg {
         s.add(srcFile.getFormat().toString());
         s.add("-O");
         s.add(destFile.getFormat().toString());
+
+        if (options != null && !options.isEmpty()) {
+            s.add("-o");
+            String optionsStr = "";
+            for (Map.Entry<String, String> option : options.entrySet()) {
+                optionsStr += option.getKey() + "=" + option.getValue() + ",";
+            }
+            s.add(optionsStr);
+        }
+
         s.add(srcFile.getFileName());
         s.add(destFile.getFileName());
         s.execute();
     }
 
+    /**
+     * Convert a image from source to destination
+     *
+     * This method calls 'qemu-img convert' and takes two objects
+     * as an argument.
+     *
+     *
+     * @param srcFile
+     *            The source file
+     * @param destFile
+     *            The destination file
+     * @return void
+     */
     public void convert(QemuImgFile srcFile, QemuImgFile destFile) {
         this.convert(srcFile, destFile, null);
     }
@@ -129,7 +225,6 @@ public class QemuImg {
      * @param file
      *            A QemuImgFile object containing the file to get the information from
      * @return A HashMap with String key-value information as returned by 'qemu-img info'
-     * @throws LibvirtException
      */
     public Map<String, String> info(QemuImgFile file) {
         Script s = new Script(_qemuImgPath);
@@ -168,7 +263,19 @@ public class QemuImg {
 
     }
 
-    /* Resize a disk image */
+    /**
+     * Resize an image
+     *
+     * This method simple calls 'qemu-img resize'.
+     * 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 resize
+     * @param
+     *            The new size
+     */
     public void resize(String filename, long size) {
         String newSize = null;
         if (size > 0) {

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/593b367b/utils/test/org/apache/cloudstack/utils/qemu/QemuImgTest.java
----------------------------------------------------------------------
diff --git a/utils/test/org/apache/cloudstack/utils/qemu/QemuImgTest.java b/utils/test/org/apache/cloudstack/utils/qemu/QemuImgTest.java
index b2464b9..4bc43d4 100644
--- a/utils/test/org/apache/cloudstack/utils/qemu/QemuImgTest.java
+++ b/utils/test/org/apache/cloudstack/utils/qemu/QemuImgTest.java
@@ -21,8 +21,6 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.fail;
 import org.apache.cloudstack.utils.qemu.QemuImgFile;
 import org.apache.cloudstack.utils.qemu.QemuImg.PhysicalDiskFormat;
-import java.util.ArrayList;
-import java.util.List;
 import java.util.Map;
 import java.util.HashMap;
 import java.io.File;
@@ -56,6 +54,36 @@ public class QemuImgTest {
     }
 
     @Test
+    public void testCreateAndInfoWithOptions() {
+        String filename = "/tmp/test-options-image.qcow2";
+
+        /* 10TB virtual_size */
+        long size = 10995116277760l;
+        QemuImgFile file = new QemuImgFile(filename, size, PhysicalDiskFormat.QCOW2);
+        String clusterSize = "131072";
+        Map<String, String> options = new HashMap<String, String>();
+
+        options.put("cluster_size", clusterSize);
+
+        QemuImg qemu = new QemuImg();
+        qemu.create(file, options);
+        Map<String, String> info = qemu.info(file);
+
+        Long infoSize = Long.parseLong(info.get(new String("virtual_size")));
+        assertEquals(Long.valueOf(size), Long.valueOf(infoSize));
+
+        String infoPath = info.get(new String("image"));
+        assertEquals(filename, infoPath);
+
+        String infoClusterSize = info.get(new String("cluster_size"));
+        assertEquals(clusterSize, infoClusterSize);
+
+        File f = new File(filename);
+        f.delete();
+
+    }
+
+    @Test
     public void testConvertBasic() {
         long srcSize = 20480;
         String srcFileName = "/tmp/test-src-image.qcow2";