Add a new QemuImgFile object to use The modification of LibvirtStorageAdapter shows how easy it becomes to use qemu-img
Project: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/commit/79482f50 Tree: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/tree/79482f50 Diff: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/diff/79482f50 Branch: refs/heads/qemu-img Commit: 79482f503c375741581e1efd676498924907e758 Parents: f11affe Author: Wido den Hollander <[email protected]> Authored: Wed Feb 6 21:14:56 2013 +0100 Committer: Wido den Hollander <[email protected]> Committed: Thu Feb 14 15:01:59 2013 +0100 ---------------------------------------------------------------------- .../kvm/storage/LibvirtStorageAdaptor.java | 9 +- .../org/apache/cloudstack/utils/qemu/QemuImg.java | 50 ++++++----- .../apache/cloudstack/utils/qemu/QemuImgFile.java | 72 +++++++++++++++ 3 files changed, 106 insertions(+), 25 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/79482f50/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/storage/LibvirtStorageAdaptor.java ---------------------------------------------------------------------- diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/storage/LibvirtStorageAdaptor.java b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/storage/LibvirtStorageAdaptor.java index 5aafc0c..b17633a 100644 --- a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/storage/LibvirtStorageAdaptor.java +++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/storage/LibvirtStorageAdaptor.java @@ -24,7 +24,9 @@ import java.util.List; import java.util.UUID; import org.apache.log4j.Logger; import org.apache.commons.codec.binary.Base64; +import org.apache.cloudstack.utils.qemu.QemuImg; import org.apache.cloudstack.utils.qemu.QemuImg.PhysicalDiskFormat; +import org.apache.cloudstack.utils.qemu.QemuImgFile; import org.libvirt.Connect; import org.libvirt.LibvirtException; import org.libvirt.Secret; @@ -594,9 +596,10 @@ public class LibvirtStorageAdaptor implements StorageAdaptor { + template.getFormat() + " -b " + template.getPath() + " " + disk.getPath()); } else if (format == PhysicalDiskFormat.RAW) { - Script.runSimpleBashScript("qemu-img convert -f " - + template.getFormat() + " -O raw " + template.getPath() - + " " + disk.getPath()); + QemuImgFile sourceFile = new QemuImgFile(template.getPath(), template.getFormat()); + QemuImgFile destFile = new QemuImgFile(disk.getPath(), PhysicalDiskFormat.RAW); + QemuImg qemu = new QemuImg(); + qemu.convert(sourceFile, destFile); } } else { disk = new KVMPhysicalDisk(destPool.getSourceDir() + "/" + newUuid, newUuid, destPool); http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/79482f50/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 a853939..c93d0ab 100644 --- a/utils/src/org/apache/cloudstack/utils/qemu/QemuImg.java +++ b/utils/src/org/apache/cloudstack/utils/qemu/QemuImg.java @@ -16,6 +16,8 @@ // under the License. package org.apache.cloudstack.utils.qemu; +import org.apache.cloudstack.utils.qemu.QemuImgFile; + import com.cloud.utils.script.Script; import java.util.List; @@ -24,7 +26,7 @@ import java.util.Map; public class QemuImg { /* The qemu-img binary. We expect this to be in $PATH */ - public static String _qemuImgPath = "qemu-img"; + public String _qemuImgPath = "qemu-img"; /* Shouldn't we have KVMPhysicalDisk and LibvirtVMDef read this? */ public static enum PhysicalDiskFormat { @@ -40,62 +42,66 @@ public class QemuImg { } } + public QemuImg() { + + } + + public QemuImg(String qemuImgPath) { + this._qemuImgPath = qemuImgPath; + } + /* These are all methods supported by the qemu-img tool */ /* Perform a consistency check on the disk image */ - public static void check() { + public void check(QemuImgFile file) { } /* Create a new disk image */ - public static void create(String filename, long size, PhysicalDiskFormat format, List<Map<String, String>> options) { + public void create(QemuImgFile file, List<Map<String, String>> options) { Script s = new Script(_qemuImgPath); s.add("create"); s.add("-f"); - s.add(format.toString()); - s.add(filename); - s.add(Long.toString(size)); + s.add(file.getFormat().toString()); + s.add(file.getFileName()); + s.add(Long.toString(file.getSize())); } - public static void create(String filename, long size, PhysicalDiskFormat format) { - QemuImg.create(filename, size, format, null); + public void create(QemuImgFile file) { + this.create(file, null); } /* Convert the disk image filename or a snapshot snapshot_name to disk image output_filename using format output_fmt. */ - public static void convert() { + public void convert(QemuImgFile srcFile, QemuImgFile destFile, List<Map<String, String>> options) { } - /* Commit the changes recorded in filename in its base image */ - public static void commit(String filename, PhysicalDiskFormat format) { - + public void convert(QemuImgFile srcFile, QemuImgFile destFile) { + this.convert(srcFile, destFile, null); } - public static void commit(String filename) { - QemuImg.commit(filename, null); + /* Commit the changes recorded in filename in its base image */ + public void commit(QemuImgFile file) { + } /* Give information about the disk image */ - public static void info(String filename, PhysicalDiskFormat format) { - - } + public void info(QemuImgFile file) { - public static void info(String filename) { - QemuImg.info(filename, null); } /* List, apply, create or delete snapshots in image */ - public static void snapshot() { + public void snapshot() { } /* Changes the backing file of an image */ - public static void rebase() { + public void rebase() { } /* Resize a disk image */ - public static void resize(String filename, long size) { + public void resize(String filename, long size) { String newSize = null; if (size > 0) { newSize = "+" + size; http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/79482f50/utils/src/org/apache/cloudstack/utils/qemu/QemuImgFile.java ---------------------------------------------------------------------- diff --git a/utils/src/org/apache/cloudstack/utils/qemu/QemuImgFile.java b/utils/src/org/apache/cloudstack/utils/qemu/QemuImgFile.java new file mode 100644 index 0000000..90d925d --- /dev/null +++ b/utils/src/org/apache/cloudstack/utils/qemu/QemuImgFile.java @@ -0,0 +1,72 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package org.apache.cloudstack.utils.qemu; + +import org.apache.cloudstack.utils.qemu.QemuImg.PhysicalDiskFormat; + +public class QemuImgFile { + + private long size = 0; + private String fileName; + private PhysicalDiskFormat format = PhysicalDiskFormat.RAW; + + public QemuImgFile(String fileName) { + this.fileName = fileName; + } + + public QemuImgFile(String fileName, long size) { + this.fileName = fileName; + this.size = size; + } + + public QemuImgFile(String fileName, long size, PhysicalDiskFormat format) { + this.fileName = fileName; + this.size = size; + this.format = format; + } + + public QemuImgFile(String fileName, PhysicalDiskFormat format) { + this.fileName = fileName; + this.size = size; + this.format = format; + } + + public void setFileName(String fileName) { + this.fileName = fileName; + } + + public void setSize(long size) { + this.size = size; + } + + public void setFormat(PhysicalDiskFormat format) { + this.format = format; + } + + public String getFileName() { + return this.fileName; + } + + public long getSize() { + return this.size; + } + + public PhysicalDiskFormat getFormat() { + return this.format; + } + +} \ No newline at end of file
