Libvirt Java API操作QEMU虚拟机(重启,强制关机,挂起,恢复,详情,关机,注销,快照备份等 )(CentOS)

需求背景

有个产品需求,需要在一台linux上装多个虚拟机,然后每个虚拟机单独部署一个产品,然后需要虚拟机的一个产品去控制宿主机中安装虚拟机的状态

注意:1, 如果虚拟机中装的产品去连宿主机的Libvirt服务,那么虚拟机一定要装 libvirt 安装命令: yum install -y libvirt
2,写的Java Demo一定要在linux服务器运行,我在windows上运行提示 virt-0.dll缺失,这个问题比较难以解决,linux运行命令如下:
nohup java -jar libvirt-demo-1.0-SNAPSHOT.jar >temp.txt 2>&1 &

Libvirt官网

1,虚拟机安装

博客
在这里插入图片描述
virsh 版本 4.5.0

2,宿主机Libvirt配置

2.1 libvirtd文件配置

开启 这两项配置
LIBVIRTD_CONFIG=/etc/libvirt/libvirtd.conf
LIBVIRTD_ARGS=“–listen”

vi /etc/sysconfig/libvirtd

Libvirt Java API操作QEMU虚拟机(重启,强制关机,挂起,恢复,详情,关机,注销,快照备份等 )(CentOS)_第1张图片

2.2 配置 libvirtd.conf文件

vi  /etc/libvirt/libvirtd.conf

修改内容如下

listen_tls = 0
listen_tcp = 1
tcp_port = "16509"
listen_addr = "0.0.0.0"
auth_tcp = "none"

2.3 重启libvirtd服务

systemctl restart libvirtd

3,Java代码

3.1 pom引入jar包

     
            org.libvirt
            libvirt
            0.5.1
        
        
        
            net.java.dev.jna
            jna
            5.12.1
        

3.2 代码

虚拟机对象

import lombok.Data;
import org.libvirt.DomainInfo;

@Data
public class HostDo {

    private int id;

    private String name;

    private DomainInfo.DomainState status;

    private String type;



    private long memTotal;
}

获取虚拟机列表,重启,强制关机,挂起,恢复,详情,关机,注销等虚拟机操作

 @GetMapping("/test")
    @ApiOperation("/test")
    public String test(@RequestParam String url) throws Exception {

        try {
            Connect connect = new Connect(url, true);
            System.out.println(connect.getHostName());
            System.out.println("dddd" + connect.getFreeMemory());
        } catch (LibvirtException e) {
            System.out.println("exception caught:" + e);
            System.out.println(e.getError());
            return e.getError().getMessage();
        }

        return "";
    }

    @GetMapping("/domains")
    @ApiOperation("/domains")
    private List listDomain(@RequestParam String url) throws LibvirtException {

        Map map = new HashMap();
        Connect connect = new Connect(url, true);

        int[] idsOfDomain = connect.listDomains();
        map.put("listDomains", idsOfDomain);
        List lists = new ArrayList();
        for (int id : idsOfDomain) {
            HostDo hostDo = new HostDo();
            Domain domain = connect.domainLookupByID(id);
            hostDo.setId(id);
            hostDo.setName(domain.getName());
            hostDo.setStatus(domain.getInfo().state);
            hostDo.setType(domain.getOSType());
            hostDo.setMemTotal(domain.getJobInfo().getMemTotal());
            lists.add(hostDo);
        }


        String[] namesOfDefinedDomain = connect.listDefinedDomains();

        for (String name : namesOfDefinedDomain) {
//            Domain domain = connect.domainLookupByName(name);
            HostDo hostDo = new HostDo();
//            hostDo.setId(id);
            hostDo.setName(name);
            hostDo.setStatus(DomainInfo.DomainState.VIR_DOMAIN_SHUTDOWN);
//            hostDo.setType(domain.getOSType());
//            hostDo.setMemTotal(domain.getJobInfo().getMemTotal());
            lists.add(hostDo);

        }

        return lists;
    }

    @GetMapping("/listDemo")
    @ApiOperation("/listDemo")
    private List listDemo(@RequestParam String url) throws LibvirtException {

        Connect connect = new Connect(url, true);
        List lists = new ArrayList();


        String[] namesOfDefinedDomain = connect.listDefinedDomains();

        for (String name : namesOfDefinedDomain) {
            Domain domain = connect.domainLookupByName(name);
            HostDo hostDo = new HostDo();
            hostDo.setId(domain.getID());
            hostDo.setName(name);
            hostDo.setStatus(DomainInfo.DomainState.VIR_DOMAIN_SHUTDOWN);
            hostDo.setType(domain.getOSType());
            hostDo.setMemTotal(domain.getJobInfo().getMemTotal());
            lists.add(hostDo);

        }

        return lists;
    }


    @GetMapping("/detail")
    @ApiOperation("详情")
    private Domain detail(@RequestParam String url, @RequestParam String name) throws LibvirtException {

        Connect connect = new Connect(url, false);
        return connect.domainLookupByName(name);
    }

    @GetMapping("/shutdown")
    @ApiOperation("正常关机")
    private Domain shutdown(@RequestParam String url, @RequestParam String name) throws LibvirtException {

        Connect connect = new Connect(url, false);
        Domain domain = connect.domainLookupByName(name);
        domain.shutdown();
        domain = connect.domainLookupByName(name);
        return domain;
    }

    @GetMapping("/reboot")
    @ApiOperation("重启")
    private Domain reboot(@RequestParam String url, @RequestParam String name) throws LibvirtException {

        Connect connect = new Connect(url, false);
        Domain domain = connect.domainLookupByName(name);
        domain.reboot(0);
        domain = connect.domainLookupByName(name);
        return domain;
    }


    @GetMapping("/delete")
    @ApiOperation("注销")
    private Domain delete(@RequestParam String url, @RequestParam String name) throws LibvirtException {

        Connect connect = new Connect(url, false);
        Domain domain = connect.domainLookupByName(name);
        domain.destroy();
        domain.undefine();
        return domain;
    }

    @GetMapping("/forceShutDown")
    @ApiOperation("强制关机")
    private Domain forceShutDown(@RequestParam String url, @RequestParam String name) throws LibvirtException {

        Connect connect = new Connect(url, false);
        Domain domain = connect.domainLookupByName(name);
        domain.destroy();
        System.out.println(domain.isActive());
        domain = connect.domainLookupByName(name);
        return domain;
    }

    @GetMapping("/start")
    @ApiOperation("启动")
    private Domain start(@RequestParam String url, @RequestParam String name) throws LibvirtException {

        Connect connect = new Connect(url, false);
        Domain domain = connect.domainLookupByName(name);
        domain.create();
        System.out.println(domain.isActive());
        return domain;
    }

    @GetMapping("/resume")
    @ApiOperation("恢复")
    private Domain resume(@RequestParam String url, @RequestParam String name) throws LibvirtException {

        Connect connect = new Connect(url, false);
        Domain domain = connect.domainLookupByName(name);
        domain.resume();
        System.out.println(domain.isActive());
        return domain;
    }

    @GetMapping("/suspend")
    @ApiOperation("挂起")
    private Domain suspend(@RequestParam String url, @RequestParam String name) throws LibvirtException {

        Connect connect = new Connect(url, false);
        Domain domain = connect.domainLookupByName(name);
        domain.suspend();
        System.out.println(domain.isActive());
        return domain;
    }

快照备份

  @GetMapping("/snapshotCurrent")
    @ApiOperation("生成当前快照")
    private Domain snapshotCurrent(@RequestParam String url, @RequestParam String name,@RequestParam String snap) throws LibvirtException {

        Connect connect = new Connect(url, false);
        Domain domain = connect.domainLookupByName(name);
        String snapshotXML = snap;
        domain.snapshotCreateXML(snapshotXML);
        System.out.println(domain.isActive());
        return domain;
    }

    @GetMapping("/snapshot-list")
    @ApiOperation("快照列表")
    private String[] snapshotListNames(@RequestParam String url, @RequestParam String name) throws LibvirtException {
        Connect connect = new Connect(url, false);
        Domain domain = connect.domainLookupByName(name);
        String[] snapArray = domain.snapshotListNames();
        return snapArray;
    }

    @GetMapping("/snapshotLookupByName")
    @ApiOperation("快照详情")
    private DomainSnapshot snapshotLookupByName(@RequestParam String url, @RequestParam String name, @RequestParam String snapName) throws LibvirtException {
        Connect connect = new Connect(url, false);
        Domain domain = connect.domainLookupByName(name);
        DomainSnapshot domainSnapshot = domain.snapshotLookupByName(snapName);
        return domainSnapshot;
    }

    @GetMapping("/snapshotNum")
    @ApiOperation("快照数量")
    private int snapshotNum(@RequestParam String url, @RequestParam String name, @RequestParam String snapName) throws LibvirtException {
        Connect connect = new Connect(url, false);
        Domain domain = connect.domainLookupByName(name);
        int i = domain.hasCurrentSnapshot();
        return i;
    }

    @GetMapping("/revertToSnapshot")
    @ApiOperation("恢复快照")
    private int revertToSnapshot(@RequestParam String url, @RequestParam String name, @RequestParam String snapName) throws LibvirtException {
        Connect connect = new Connect(url, false);
        Domain domain = connect.domainLookupByName(name);
        DomainSnapshot domainSnapshot = domain.snapshotLookupByName(snapName);
        int i = domain.revertToSnapshot(domainSnapshot);
        return i;
    }

    @GetMapping("/snapshot-delete")
    @ApiOperation("删除快照")
    private int snapDelete(@RequestParam String url, @RequestParam String name, @RequestParam String snapName) throws LibvirtException {
        Connect connect = new Connect(url, false);
        Domain domain = connect.domainLookupByName(name);
        DomainSnapshot domainSnapshot = domain.snapshotLookupByName(snapName);
        int delete = domainSnapshot.delete(0);
        return delete;
    }

3.3,调试

qemu+tcp://10.50.1.60:16511/system

Libvirt Java API操作QEMU虚拟机(重启,强制关机,挂起,恢复,详情,关机,注销,快照备份等 )(CentOS)_第2张图片
未完待续。。。后面会补充克隆等虚拟机操作

你可能感兴趣的:(虚拟化,自动化,服务器,linux)