Skip to content
This repository was archived by the owner on Jan 15, 2020. It is now read-only.

Commit ea0daea

Browse files
widosudison
authored andcommitted
CLOUDSTACK-1158: Wrap qemu-img into it's own util
Signed-off-by: Edison Su <sudison@gmail.com>
1 parent aa79ccf commit ea0daea

15 files changed

Lines changed: 949 additions & 133 deletions

File tree

plugins/hypervisors/kvm/pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,16 @@
6767
</execution>
6868
</executions>
6969
</plugin>
70+
<plugin>
71+
<groupId>org.apache.maven.plugins</groupId>
72+
<artifactId>maven-surefire-plugin</artifactId>
73+
<version>2.14</version>
74+
<configuration>
75+
<excludes>
76+
<exclude>**/Qemu*.java</exclude>
77+
</excludes>
78+
</configuration>
79+
</plugin>
7080
</plugins>
7181
</build>
7282
</project>

plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@
5757
import javax.naming.ConfigurationException;
5858

5959
import org.apache.log4j.Logger;
60+
import org.apache.cloudstack.utils.qemu.QemuImg.PhysicalDiskFormat;
61+
import org.apache.cloudstack.utils.qemu.QemuImg;
62+
import org.apache.cloudstack.utils.qemu.QemuImgFile;
63+
import org.apache.cloudstack.utils.qemu.QemuImgException;
6064
import org.libvirt.Connect;
6165
import org.libvirt.Domain;
6266
import org.libvirt.DomainInfo;
@@ -193,7 +197,6 @@
193197
import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.VirtioSerialDef;
194198
import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.TermPolicy;
195199
import com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk;
196-
import com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk.PhysicalDiskFormat;
197200
import com.cloud.hypervisor.kvm.storage.KVMStoragePool;
198201
import com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager;
199202
import com.cloud.network.Networks.BroadcastDomainType;
@@ -1418,12 +1421,12 @@ private String getResizeScriptType (KVMStoragePool pool, KVMPhysicalDisk vol) {
14181421
StoragePoolType poolType = pool.getType();
14191422
PhysicalDiskFormat volFormat = vol.getFormat();
14201423

1421-
if(pool.getType() == StoragePoolType.CLVM && volFormat == KVMPhysicalDisk.PhysicalDiskFormat.RAW) {
1424+
if(pool.getType() == StoragePoolType.CLVM && volFormat == PhysicalDiskFormat.RAW) {
14221425
return "CLVM";
14231426
} else if ((poolType == StoragePoolType.NetworkFilesystem
14241427
|| poolType == StoragePoolType.SharedMountPoint
14251428
|| poolType == StoragePoolType.Filesystem)
1426-
&& volFormat == KVMPhysicalDisk.PhysicalDiskFormat.QCOW2 ) {
1429+
&& volFormat == PhysicalDiskFormat.QCOW2 ) {
14271430
return "QCOW2";
14281431
}
14291432
return null;
@@ -2230,14 +2233,25 @@ protected CreatePrivateTemplateAnswer execute(
22302233
}
22312234
} else {
22322235
s_logger.debug("Converting RBD disk " + disk.getPath() + " into template " + cmd.getUniqueName());
2233-
Script.runSimpleBashScript("qemu-img convert"
2234-
+ " -f raw -O qcow2 "
2235-
+ KVMPhysicalDisk.RBDStringBuilder(primary.getSourceHost(),
2236+
2237+
QemuImgFile srcFile = new QemuImgFile(KVMPhysicalDisk.RBDStringBuilder(primary.getSourceHost(),
22362238
primary.getSourcePort(),
22372239
primary.getAuthUserName(),
22382240
primary.getAuthSecret(),
2239-
disk.getPath())
2240-
+ " " + tmpltPath + "/" + cmd.getUniqueName() + ".qcow2");
2241+
disk.getPath()));
2242+
srcFile.setFormat(PhysicalDiskFormat.RAW);
2243+
2244+
QemuImgFile destFile = new QemuImgFile(tmpltPath + "/" + cmd.getUniqueName() + ".qcow2");
2245+
destFile.setFormat(PhysicalDiskFormat.QCOW2);
2246+
2247+
QemuImg q = new QemuImg();
2248+
try {
2249+
q.convert(srcFile, destFile);
2250+
} catch (QemuImgException e) {
2251+
s_logger.error("Failed to create new template while converting "
2252+
+ srcFile.getFileName() + " to " + destFile.getFileName() + " the error was: " + e.getMessage());
2253+
}
2254+
22412255
File templateProp = new File(tmpltPath + "/template.properties");
22422256
if (!templateProp.exists()) {
22432257
templateProp.createNewFile();

plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtDomainXMLParser.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import javax.xml.parsers.DocumentBuilderFactory;
2626
import javax.xml.parsers.ParserConfigurationException;
2727

28+
import org.apache.cloudstack.utils.qemu.QemuImg.PhysicalDiskFormat;
2829
import org.apache.log4j.Logger;
2930
import org.w3c.dom.Document;
3031
import org.w3c.dom.Element;

plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/storage/KVMPhysicalDisk.java

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,13 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717
package com.cloud.hypervisor.kvm.storage;
18+
import org.apache.cloudstack.utils.qemu.QemuImg.PhysicalDiskFormat;
1819

1920
public class KVMPhysicalDisk {
2021
private String path;
2122
private String name;
2223
private KVMStoragePool pool;
2324

24-
public static enum PhysicalDiskFormat {
25-
RAW("raw"), QCOW2("qcow2"), TAR("tar"), DIR("dir");
26-
String format;
27-
28-
private PhysicalDiskFormat(String format) {
29-
this.format = format;
30-
}
31-
32-
public String toString() {
33-
return this.format;
34-
}
35-
}
36-
3725
public static String RBDStringBuilder(String monHost, int monPort,
3826
String authUserName, String authSecret, String image) {
3927
String rbdOpts;

plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/storage/KVMStoragePool.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import java.util.List;
2020

21-
import com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk.PhysicalDiskFormat;
21+
import org.apache.cloudstack.utils.qemu.QemuImg.PhysicalDiskFormat;
2222
import com.cloud.storage.Storage.StoragePoolType;
2323

2424
public interface KVMStoragePool {

plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/storage/KVMStoragePoolManager.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@
2323
import java.util.HashMap;
2424
import java.util.UUID;
2525

26+
import org.apache.cloudstack.utils.qemu.QemuImg.PhysicalDiskFormat;
27+
2628
import com.cloud.hypervisor.kvm.resource.KVMHABase;
2729
import com.cloud.hypervisor.kvm.resource.KVMHABase.PoolType;
2830
import com.cloud.hypervisor.kvm.resource.KVMHAMonitor;
29-
import com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk.PhysicalDiskFormat;
3031
import com.cloud.storage.Storage.StoragePoolType;
3132
import com.cloud.storage.StorageLayer;
3233
import com.cloud.utils.exception.CloudRuntimeException;
@@ -134,18 +135,18 @@ public KVMPhysicalDisk createDiskFromTemplate(KVMPhysicalDisk template, String n
134135
// LibvirtStorageAdaptor-specific statement
135136
if (destPool.getType() == StoragePoolType.RBD) {
136137
return adaptor.createDiskFromTemplate(template, name,
137-
KVMPhysicalDisk.PhysicalDiskFormat.RAW, template.getSize(), destPool);
138+
PhysicalDiskFormat.RAW, template.getSize(), destPool);
138139
} else if (destPool.getType() == StoragePoolType.CLVM) {
139140
return adaptor.createDiskFromTemplate(template, name,
140-
KVMPhysicalDisk.PhysicalDiskFormat.RAW, template.getSize(),
141+
PhysicalDiskFormat.RAW, template.getSize(),
141142
destPool);
142-
} else if (template.getFormat() == KVMPhysicalDisk.PhysicalDiskFormat.DIR) {
143+
} else if (template.getFormat() == PhysicalDiskFormat.DIR) {
143144
return adaptor.createDiskFromTemplate(template, name,
144-
KVMPhysicalDisk.PhysicalDiskFormat.DIR,
145+
PhysicalDiskFormat.DIR,
145146
template.getSize(), destPool);
146147
} else {
147148
return adaptor.createDiskFromTemplate(template, name,
148-
KVMPhysicalDisk.PhysicalDiskFormat.QCOW2,
149+
PhysicalDiskFormat.QCOW2,
149150
template.getSize(), destPool);
150151
}
151152
}

0 commit comments

Comments
 (0)