Quellcode durchsuchen

kvm 管理接口

reghao vor 5 Jahren
Ursprung
Commit
467300c4e1

+ 57 - 0
dmaster/src/main/java/cn/reghao/autodop/dmaster/vm/controller/GuestController.java

@@ -0,0 +1,57 @@
+package cn.reghao.autodop.dmaster.vm.controller;
+
+import cn.reghao.autodop.common.result.WebResult;
+import cn.reghao.autodop.dmaster.vm.kvm.Guest;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import org.springframework.web.bind.annotation.*;
+
+/**
+ * @author reghao
+ * @date 2020-10-31 16:04:58
+ */
+@Api(tags = "虚拟机管理接口")
+@RestController
+@RequestMapping("/api/vm/guest")
+public class GuestController {
+    private String uri = "qemu:///system";
+    private Guest guest = new Guest(uri);
+
+    @ApiOperation(value = "")
+    @PostMapping
+    public String addGuest(@RequestBody String data) throws Exception {
+        guest.createDomain();
+        return WebResult.success("ok");
+    }
+
+    @ApiOperation(value = "")
+    @GetMapping
+    public String getGuest() throws Exception {
+        return WebResult.success(guest.listDomain());
+    }
+
+    @ApiOperation(value = "")
+    @DeleteMapping
+    public String deleteGuest(@PathVariable("guestName") String guestName) throws Exception {
+        guest.deleteDomain(guestName);
+        return WebResult.success("ok");
+    }
+
+    @ApiOperation(value = "")
+    @PostMapping("/start")
+    public String start(@RequestParam("guestName") String guestName) throws Exception {
+        return WebResult.success("ok");
+    }
+
+    @ApiOperation(value = "")
+    @PostMapping("/stop")
+    public String stop(@RequestParam("guestName") String guestName) throws Exception {
+        return WebResult.success("ok");
+    }
+
+    @ApiOperation(value = "")
+    @PostMapping("/restart")
+    public String restart(@RequestParam("guestName") String guestName) throws Exception {
+        return WebResult.success("ok");
+    }
+}

+ 3 - 3
dmaster/src/main/java/cn/reghao/autodop/dmaster/vm/controller/PhysicalHostController.java → dmaster/src/main/java/cn/reghao/autodop/dmaster/vm/controller/HostController.java

@@ -12,10 +12,10 @@ import org.springframework.web.bind.annotation.RestController;
  * @author reghao
  * @date 2020-10-31 16:04:58
  */
-@Api(tags = "物理主机管理接口")
+@Api(tags = "宿主机管理接口")
 @RestController
-@RequestMapping("/api/vm/phost")
-public class PhysicalHostController {
+@RequestMapping("/api/vm/host")
+public class HostController {
     @PostMapping
     public String addPhysicalHost() {
         return WebResult.success("ok");

+ 13 - 5
dmaster/src/main/java/cn/reghao/autodop/dmaster/vm/controller/VirtualDiskController.java → dmaster/src/main/java/cn/reghao/autodop/dmaster/vm/controller/StorageController.java

@@ -12,8 +12,8 @@ import org.springframework.web.bind.annotation.*;
  */
 @Api(tags = "虚拟磁盘管理接口")
 @RestController
-@RequestMapping("/api/vm/vdisk")
-public class VirtualDiskController {
+@RequestMapping("/api/vm/storage")
+public class StorageController {
     private String uri = "qemu:///system";
     private Storage storage = new Storage(uri);
 
@@ -38,9 +38,10 @@ public class VirtualDiskController {
     }
 
     @ApiOperation(value = "")
-    @PostMapping("/volume/{poolName}")
-    public String addVolume(@PathVariable("poolName") String poolName) throws Exception {
-        return WebResult.success(storage.listStorageVolume(poolName));
+    @PostMapping("/volume")
+    public String addVolume(String poolName, int capacity) throws Exception {
+        storage.createStorageVolume(poolName, capacity);
+        return WebResult.success("ok");
     }
 
     @ApiOperation(value = "")
@@ -48,4 +49,11 @@ public class VirtualDiskController {
     public String getVolume(@PathVariable("poolName") String poolName) throws Exception {
         return WebResult.success(storage.listStorageVolume(poolName));
     }
+
+    @ApiOperation(value = "")
+    @DeleteMapping("/volume")
+    public String deleteVolume(String volKey) throws Exception {
+        storage.deleteStorageVolume(volKey);
+        return WebResult.success("ok");
+    }
 }

+ 0 - 15
dmaster/src/main/java/cn/reghao/autodop/dmaster/vm/controller/VirtualHostController.java

@@ -1,15 +0,0 @@
-package cn.reghao.autodop.dmaster.vm.controller;
-
-import io.swagger.annotations.Api;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
-
-/**
- * @author reghao
- * @date 2020-10-31 16:04:58
- */
-@Api(tags = "虚拟主机管理接口")
-@RestController
-@RequestMapping("/api/vm/vhost")
-public class VirtualHostController {
-}

+ 77 - 0
dmaster/src/main/java/cn/reghao/autodop/dmaster/vm/kvm/Guest.java

@@ -0,0 +1,77 @@
+package cn.reghao.autodop.dmaster.vm.kvm;
+
+import cn.reghao.autodop.dmaster.vm.pojo.GuestInfo;
+import org.dom4j.Document;
+import org.dom4j.io.SAXReader;
+import org.libvirt.Connect;
+import org.libvirt.Domain;
+import org.libvirt.LibvirtException;
+
+import java.io.FileInputStream;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * 虚拟主机
+ *
+ * @author reghao
+ * @date 2020-10-28 18:48:52
+ */
+public class Guest {
+    private String uri;
+
+    public Guest(String uri) {
+        this.uri = uri;
+    }
+
+    public void createDomain() throws Exception {
+        Connect conn = new Connect(uri, false);
+        String xml = "/home/reghao/docs/zzz/backend/kvm/xml/vhost.xml";
+        Document doc = new SAXReader().read(new FileInputStream(xml));
+        Domain domain = conn.domainDefineXML(doc.asXML());
+    }
+
+    public List<GuestInfo> listDomain() throws Exception {
+        Connect conn = new Connect(uri, false);
+        List<GuestInfo> list = new ArrayList<>();
+        String[] domainNames = conn.listDefinedDomains();
+        for (String domainName : domainNames) {
+            list.add(new GuestInfo(conn.domainLookupByName(domainName)));
+        }
+
+        int[] active = conn.listDomains();
+        for (int id : active) {
+            list.add(new GuestInfo(conn.domainLookupByID(id)));
+        }
+        return list;
+    }
+
+    public void deleteDomain(String guestName) throws LibvirtException {
+        Connect conn = new Connect(uri, false);
+        Domain domain = conn.domainLookupByName(guestName);
+
+        domain.undefine();
+        domain.free();
+        /*String xmlDesc = domain.getXMLDesc(0);
+        domain.detachDevice(xmlDesc);
+        domain.destroy();*/
+    }
+
+    public void start(String guestName) throws Exception {
+        Connect conn = new Connect(uri, false);
+        Domain domain = conn.domainLookupByName(guestName);
+        domain.create();
+    }
+
+    public void stop(String guestName) throws Exception {
+        Connect conn = new Connect(uri, false);
+        Domain domain = conn.domainLookupByName(guestName);
+        domain.shutdown();
+    }
+
+    public void restart(String guestName) throws Exception {
+        Connect conn = new Connect(uri, false);
+        Domain domain = conn.domainLookupByName(guestName);
+        domain.reboot(0);
+    }
+}

+ 16 - 48
dmaster/src/main/java/cn/reghao/autodop/dmaster/vm/kvm/Storage.java

@@ -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);
     }
 }

+ 0 - 78
dmaster/src/main/java/cn/reghao/autodop/dmaster/vm/kvm/VirtualHost.java

@@ -1,78 +0,0 @@
-package cn.reghao.autodop.dmaster.vm.kvm;
-
-import org.dom4j.Document;
-import org.dom4j.io.SAXReader;
-import org.libvirt.Connect;
-import org.libvirt.Domain;
-
-import java.io.FileInputStream;
-import java.util.ArrayList;
-import java.util.List;
-
-/**
- * 虚拟主机
- *
- * @author reghao
- * @date 2020-10-28 18:48:52
- */
-public class VirtualHost {
-    private String uri;
-
-    public VirtualHost(String uri) {
-        this.uri = uri;
-    }
-
-    public void start(Domain domain) throws Exception {
-        domain.create();
-    }
-
-    public void restart(Domain domain) throws Exception {
-        domain.reboot(0);
-    }
-
-    public void stop(Domain domain) throws Exception {
-        domain.shutdown();
-    }
-
-    public Domain createDomain() throws Exception {
-        Connect conn = new Connect(uri, false);
-        String xml = "/home/reghao/docs/zzz/backend/kvm/xml/vhost.xml";
-        Document doc = new SAXReader().read(new FileInputStream(xml));
-        return conn.domainDefineXML(doc.asXML());
-    }
-
-    public List<Domain> listDomain() throws Exception {
-        Connect conn = new Connect(uri, false);
-        List<Domain> list = new ArrayList<>();
-        String[] domainNames = conn.listDefinedDomains();
-        for (String domainName : domainNames) {
-            list.add(conn.domainLookupByName(domainName));
-        }
-
-        int[] active = conn.listDomains();
-        for (int id : active) {
-            list.add(conn.domainLookupByID(id));
-        }
-        return list;
-    }
-
-    public static void main(String[] args) throws Exception {
-        String uri = "qemu:///system";
-
-        VirtualHost vhost = new VirtualHost(uri);
-        Domain domain1 = vhost.createDomain();
-
-        List<Domain> domains = vhost.listDomain();
-        for (Domain domain : domains) {
-            String domainName = domain.getName();
-            domain.undefine();
-            domain.free();
-
-            /*String xmlDesc = domain.getXMLDesc(0);
-            domain.detachDevice(xmlDesc);
-            domain.destroy();*/
-        }
-
-        System.out.println();
-    }
-}

+ 44 - 0
dmaster/src/main/java/cn/reghao/autodop/dmaster/vm/pojo/GuestInfo.java

@@ -0,0 +1,44 @@
+package cn.reghao.autodop.dmaster.vm.pojo;
+
+import lombok.Data;
+import org.libvirt.Domain;
+import org.libvirt.DomainInfo;
+import org.libvirt.LibvirtException;
+
+/**
+ * @author reghao
+ * @date 2020-11-01 01:43:57
+ */
+@Data
+public class GuestInfo {
+    private String name;
+    private int id;
+    private String uuid;
+    private String osType;
+    private String isActive;
+    private String isAutoStart;
+    private String xmlDesc;
+    private String state;
+    private int vcpu;
+    private long vcpuTime;
+    private long vmem;
+    private long maxVmem;
+
+
+    public GuestInfo(Domain domain) throws LibvirtException {
+        this.name = domain.getName();
+        this.id = domain.getID();
+        this.uuid = domain.getUUIDString();
+        this.osType = domain.getOSType();
+        this.isActive = domain.isActive() == 1 ? "YES" : "NO";
+        this.isAutoStart = domain.getAutostart() ? "YES" : "NO";
+        this.xmlDesc = domain.getXMLDesc(0);
+
+        DomainInfo domainInfo = domain.getInfo();
+        this.state = domainInfo.state.name();
+        this.vcpu = domainInfo.nrVirtCpu;
+        this.vcpuTime = domainInfo.cpuTime;
+        this.vmem = domainInfo.memory;
+        this.maxVmem = domainInfo.maxMem;
+    }
+}

+ 16 - 2
dmaster/src/main/java/cn/reghao/autodop/dmaster/vm/pojo/PoolInfo.java

@@ -1,6 +1,8 @@
 package cn.reghao.autodop.dmaster.vm.pojo;
 
 import lombok.Data;
+import org.libvirt.LibvirtException;
+import org.libvirt.StoragePool;
 import org.libvirt.StoragePoolInfo;
 
 /**
@@ -10,13 +12,25 @@ import org.libvirt.StoragePoolInfo;
 @Data
 public class PoolInfo {
     private String name;
+    private String uuid;
     private String state;
     private long capacity;
     private long allocation;
     private long available;
+    private String isPersistent;
+    private String isActive;
+    private String isAutoStart;
+    private String xmlDesc;
 
-    public PoolInfo(String name, StoragePoolInfo storagePoolInfo) {
-        this.name = name;
+    public PoolInfo(StoragePool storagePool) throws LibvirtException {
+        this.name = storagePool.getName();
+        this.uuid = storagePool.getUUIDString();
+        this.isPersistent = storagePool.isPersistent() == 1 ? "YES" : "NO";
+        this.isActive = storagePool.isActive() == 1 ? "YES" : "NO";
+        this.isAutoStart = storagePool.getAutostart() ? "YES" : "NO";
+        this.xmlDesc = storagePool.getXMLDesc(0);
+
+        StoragePoolInfo storagePoolInfo = storagePool.getInfo();
         this.state = storagePoolInfo.state.name();
         this.capacity = storagePoolInfo.capacity;
         this.allocation = storagePoolInfo.allocation;

+ 13 - 2
dmaster/src/main/java/cn/reghao/autodop/dmaster/vm/pojo/VolumeInfo.java

@@ -1,6 +1,8 @@
 package cn.reghao.autodop.dmaster.vm.pojo;
 
 import lombok.Data;
+import org.libvirt.LibvirtException;
+import org.libvirt.StorageVol;
 import org.libvirt.StorageVolInfo;
 
 /**
@@ -10,14 +12,23 @@ import org.libvirt.StorageVolInfo;
 @Data
 public class VolumeInfo {
     private String name;
+    private String key;
     private String type;
     private long capacity;
     private long allocation;
+    private String path;
+    private String xmlDesc;
 
-    public VolumeInfo(String name, StorageVolInfo storageVolInfo) {
-        this.name = name;
+    public VolumeInfo(StorageVol storageVol) throws LibvirtException {
+        this.name = storageVol.getName();
+        this.key = storageVol.getKey();
+
+        StorageVolInfo storageVolInfo = storageVol.getInfo();
         this.type = storageVolInfo.type.name();
         this.capacity = storageVolInfo.capacity;
         this.allocation = storageVolInfo.allocation;
+
+        this.path = storageVol.getPath();
+        this.xmlDesc = storageVol.getXMLDesc(0);
     }
 }

+ 1 - 1
dmaster/src/main/java/cn/reghao/autodop/dmaster/vm/service/VirtualDiskService.java → dmaster/src/main/java/cn/reghao/autodop/dmaster/vm/service/GuestService.java

@@ -7,5 +7,5 @@ import org.springframework.stereotype.Service;
  * @date 2020-10-31 16:04:58
  */
 @Service
-public class VirtualDiskService {
+public class GuestService {
 }

+ 1 - 1
dmaster/src/main/java/cn/reghao/autodop/dmaster/vm/service/VirtualHostService.java → dmaster/src/main/java/cn/reghao/autodop/dmaster/vm/service/StorageService.java

@@ -7,5 +7,5 @@ import org.springframework.stereotype.Service;
  * @date 2020-10-31 16:04:58
  */
 @Service
-public class VirtualHostService {
+public class StorageService {
 }