|
|
@@ -39,18 +39,13 @@ public class Storage {
|
|
|
String[] inactivePoolNames = conn.listDefinedStoragePools();
|
|
|
for (String poolName : inactivePoolNames) {
|
|
|
StoragePool storagePool = conn.storagePoolLookupByName(poolName);
|
|
|
- storagePool.isPersistent();
|
|
|
- storagePool.isActive();
|
|
|
- storagePool.getAutostart();
|
|
|
- storagePool.getXMLDesc(0);
|
|
|
- storagePool.getUUIDString();
|
|
|
- list.add(new PoolInfo(poolName, storagePool.getInfo()));
|
|
|
+ list.add(new PoolInfo(storagePool));
|
|
|
}
|
|
|
|
|
|
String[] activePoolNames = conn.listStoragePools();
|
|
|
for (String poolName : activePoolNames) {
|
|
|
StoragePool storagePool = conn.storagePoolLookupByName(poolName);
|
|
|
- list.add(new PoolInfo(poolName, storagePool.getInfo()));
|
|
|
+ list.add(new PoolInfo(storagePool));
|
|
|
}
|
|
|
|
|
|
return list;
|
|
|
@@ -61,23 +56,26 @@ public class Storage {
|
|
|
StoragePool storagePool = conn.storagePoolLookupByName(poolName);
|
|
|
int res = storagePool.isActive();
|
|
|
if (res == 1) {
|
|
|
- //storagePool.destroy();
|
|
|
- //storagePool.undefine();
|
|
|
- // 释放所有与存储池关联的内存,存储池的状态不会改变
|
|
|
+ storagePool.destroy();
|
|
|
storagePool.free();
|
|
|
} else if (res == 0) {
|
|
|
- //
|
|
|
+ // 取消处于 inactive 状态的存储池的定义
|
|
|
storagePool.undefine();
|
|
|
+ storagePool.destroy();
|
|
|
+ // 释放所有与存储池关联的内存,存储池的状态不会改变
|
|
|
storagePool.free();
|
|
|
} else {
|
|
|
throw new Exception("存储池状态错误...");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public StorageVol createStorageVolume(StoragePool storagePool) throws Exception {
|
|
|
+ public void createStorageVolume(String poolName, int capacity) throws Exception {
|
|
|
+ Connect conn = new Connect(uri, false);
|
|
|
+ StoragePool storagePool = conn.storagePoolLookupByName(poolName);
|
|
|
+
|
|
|
String xml = "/home/reghao/docs/zzz/backend/kvm/xml/volume.xml";
|
|
|
Document doc = new SAXReader().read(new FileInputStream(xml));
|
|
|
- return storagePool.storageVolCreateXML(doc.asXML(), 0);
|
|
|
+ StorageVol storageVol = storagePool.storageVolCreateXML(doc.asXML(), 0);
|
|
|
}
|
|
|
|
|
|
public List<VolumeInfo> listStorageVolume(String poolName) throws Exception {
|
|
|
@@ -88,44 +86,14 @@ public class Storage {
|
|
|
String[] volumeNames = storagePool.listVolumes();
|
|
|
for (String volName : volumeNames) {
|
|
|
StorageVol storageVol = storagePool.storageVolLookupByName(volName);
|
|
|
- list.add(new VolumeInfo(volName, storageVol.getInfo()));
|
|
|
- //list.add(storagePool.storageVolLookupByName(vol));
|
|
|
- /*StorageVolInfo volInfo = storageVol.getInfo();
|
|
|
- log.info("存储卷的类型:{}", volInfo.type);
|
|
|
- log.info("存储卷的容量:{}GB", volInfo.capacity / 1024.00 / 1024.00 / 1024.00);
|
|
|
- log.info("存储卷的已用容量:{}GB", volInfo.allocation / 1024.00 / 1024.00 / 1024.00);*/
|
|
|
+ list.add(new VolumeInfo(storageVol));
|
|
|
}
|
|
|
return list;
|
|
|
}
|
|
|
|
|
|
- public static void main(String[] args) throws Exception {
|
|
|
- String uri = "qemu:///system";
|
|
|
- Storage storage = new Storage(uri);
|
|
|
-
|
|
|
- /*List<StoragePool> storagePools = storage.listStoragePool();
|
|
|
- if (storagePools.isEmpty()) {
|
|
|
- StoragePool storagePool = storage.createStoragePool();
|
|
|
- //StorageVol storageVol = storage.createStorageVolume(storagePool);
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- for (StoragePool storagePool: storagePools) {
|
|
|
- String poolName = storagePool.getName();
|
|
|
- log.info("存储池: {}", poolName);
|
|
|
-
|
|
|
- List<StorageVol> storageVols = storage.listStorageVolume(storagePool.getName());
|
|
|
- if (!storageVols.isEmpty()) {
|
|
|
- for (StorageVol storageVol : storageVols) {
|
|
|
- // 释放所有指向 volume 的指针(domain)
|
|
|
- //storageVol.free();
|
|
|
- // 物理删除
|
|
|
- storageVol.delete(0);
|
|
|
- log.info("存储卷:{}", storageVol.getName());
|
|
|
- }
|
|
|
- } else {
|
|
|
- storage.createStorageVolume(storagePool);
|
|
|
- }
|
|
|
- log.info("---------------------------------------------------------------------");
|
|
|
- }*/
|
|
|
+ public void deleteStorageVolume(String volKey) throws LibvirtException {
|
|
|
+ Connect conn = new Connect(uri, false);
|
|
|
+ StorageVol storageVol = conn.storageVolLookupByKey(volKey);
|
|
|
+ storageVol.delete(0);
|
|
|
}
|
|
|
}
|