Return-Path: X-Original-To: apmail-incubator-cloudstack-commits-archive@minotaur.apache.org Delivered-To: apmail-incubator-cloudstack-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 4517BE04A for ; Wed, 20 Feb 2013 21:52:59 +0000 (UTC) Received: (qmail 6865 invoked by uid 500); 20 Feb 2013 21:52:59 -0000 Delivered-To: apmail-incubator-cloudstack-commits-archive@incubator.apache.org Received: (qmail 6833 invoked by uid 500); 20 Feb 2013 21:52:58 -0000 Mailing-List: contact cloudstack-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: cloudstack-dev@incubator.apache.org Delivered-To: mailing list cloudstack-commits@incubator.apache.org Received: (qmail 6822 invoked by uid 99); 20 Feb 2013 21:52:58 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 20 Feb 2013 21:52:58 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id A569682D678; Wed, 20 Feb 2013 21:52:58 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: widodh@apache.org To: cloudstack-commits@incubator.apache.org X-Mailer: ASF-Git Admin Mailer Subject: [2/2] git commit: refs/heads/qemu-img - Added more tests and implemented adding optiomns to create and convert. Message-Id: <20130220215258.A569682D678@tyr.zones.apache.org> Date: Wed, 20 Feb 2013 21:52:58 +0000 (UTC) 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 Authored: Wed Feb 20 22:52:25 2013 +0100 Committer: Wido den Hollander 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> 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 with key value + * pairs which are passed on to qemu-img without validation. + * @return void + */ + public void create(QemuImgFile file, QemuImgFile backingFile, Map 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 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 with key value + * pairs which are passed on to qemu-img without validation. + * @return void + */ + public void create(QemuImgFile file, Map 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 with key value + * pairs which are passed on to qemu-img without validation. + * @return void + */ public void convert(QemuImgFile srcFile, QemuImgFile destFile, Map 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 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 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 options = new HashMap(); + + options.put("cluster_size", clusterSize); + + QemuImg qemu = new QemuImg(); + qemu.create(file, options); + Map 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";