|
|
@@ -1,99 +0,0 @@
|
|
|
-package cn.reghao.autodop.dmaster.vm.service.kvm;
|
|
|
-
|
|
|
-import cn.reghao.autodop.dmaster.vm.entity.PoolInfo;
|
|
|
-import cn.reghao.autodop.dmaster.vm.entity.VolumeInfo;
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.dom4j.Document;
|
|
|
-import org.dom4j.io.SAXReader;
|
|
|
-import org.libvirt.*;
|
|
|
-
|
|
|
-import java.io.FileInputStream;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.List;
|
|
|
-
|
|
|
-/**
|
|
|
- * 存储
|
|
|
- *
|
|
|
- * @author reghao
|
|
|
- * @date 2020-10-28 21:44:01
|
|
|
- */
|
|
|
-@Slf4j
|
|
|
-public class Storage {
|
|
|
- private String uri;
|
|
|
-
|
|
|
- public Storage(String uri) {
|
|
|
- this.uri = uri;
|
|
|
- }
|
|
|
-
|
|
|
- public void createStoragePool() throws Exception {
|
|
|
- Connect conn = new Connect(uri, false);
|
|
|
- String xml = "/home/reghao/docs/zzz/backend/kvm/xml/pool.xml";
|
|
|
- Document doc = new SAXReader().read(new FileInputStream(xml));
|
|
|
- StoragePool storagePool = conn.storagePoolCreateXML(doc.asXML(), 0);
|
|
|
- }
|
|
|
-
|
|
|
- public List<PoolInfo> listStoragePool() throws Exception {
|
|
|
- Connect conn = new Connect(uri, false);
|
|
|
-
|
|
|
- List<PoolInfo> list = new ArrayList<>();
|
|
|
- String[] inactivePoolNames = conn.listDefinedStoragePools();
|
|
|
- for (String poolName : inactivePoolNames) {
|
|
|
- StoragePool storagePool = conn.storagePoolLookupByName(poolName);
|
|
|
- list.add(new PoolInfo(storagePool));
|
|
|
- }
|
|
|
-
|
|
|
- String[] activePoolNames = conn.listStoragePools();
|
|
|
- for (String poolName : activePoolNames) {
|
|
|
- StoragePool storagePool = conn.storagePoolLookupByName(poolName);
|
|
|
- list.add(new PoolInfo(storagePool));
|
|
|
- }
|
|
|
-
|
|
|
- return list;
|
|
|
- }
|
|
|
-
|
|
|
- public void deleteStoragePool(String poolName) throws Exception {
|
|
|
- Connect conn = new Connect(uri, false);
|
|
|
- StoragePool storagePool = conn.storagePoolLookupByName(poolName);
|
|
|
- int res = storagePool.isActive();
|
|
|
- if (res == 1) {
|
|
|
- storagePool.destroy();
|
|
|
- storagePool.free();
|
|
|
- } else if (res == 0) {
|
|
|
- // 取消处于 inactive 状态的存储池的定义
|
|
|
- storagePool.undefine();
|
|
|
- storagePool.destroy();
|
|
|
- // 释放所有与存储池关联的内存,存储池的状态不会改变
|
|
|
- storagePool.free();
|
|
|
- } else {
|
|
|
- throw new 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));
|
|
|
- StorageVol storageVol = storagePool.storageVolCreateXML(doc.asXML(), 0);
|
|
|
- }
|
|
|
-
|
|
|
- public List<VolumeInfo> listStorageVolume(String poolName) throws Exception {
|
|
|
- Connect conn = new Connect(uri, false);
|
|
|
- StoragePool storagePool = conn.storagePoolLookupByName(poolName);
|
|
|
-
|
|
|
- List<VolumeInfo> list = new ArrayList<>();
|
|
|
- String[] volumeNames = storagePool.listVolumes();
|
|
|
- for (String volName : volumeNames) {
|
|
|
- StorageVol storageVol = storagePool.storageVolLookupByName(volName);
|
|
|
- list.add(new VolumeInfo(storageVol));
|
|
|
- }
|
|
|
- return list;
|
|
|
- }
|
|
|
-
|
|
|
- public void deleteStorageVolume(String volKey) throws LibvirtException {
|
|
|
- Connect conn = new Connect(uri, false);
|
|
|
- StorageVol storageVol = conn.storageVolLookupByKey(volKey);
|
|
|
- storageVol.delete(0);
|
|
|
- }
|
|
|
-}
|