需求背景
有个产品需求,需要在一台linux上装多个虚拟机,然后每个虚拟机单独部署一个产品,然后需要虚拟机的一个产品去控制宿主机中安装虚拟机的状态
注意: 如果虚拟机中装的产品去连宿主机的Libvirt服务,那么虚拟机一定要装 libvirt 安装命令: yum install -y libvirt
Libvirt官网
1,虚拟机安装
博客
virsh 版本 4.5.0
2,宿主机Libvirt配置
2.1 libvirtd文件配置
开启 这两项配置
LIBVIRTD_CONFIG=/etc/libvirt/libvirtd.conf
LIBVIRTD_ARGS=“–listen”
vi /etc/sysconfig/libvirtd
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包
<dependency>
<groupId>org.libvirt</groupId>
<artifactId>libvirt</artifactId>
<version>0.5.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/net.java.dev.jna/jna -->
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>5.12.1</version>
</dependency>
3.2 代码
获取虚拟机列表,重启,强制关机,挂起,恢复,详情,关机,注销等虚拟机操作
@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<HostDo> listDomain(@RequestParam String url) throws LibvirtException {
Map<String, Object> map = new HashMap<String, Object>();
Connect connect = new Connect(url, true);
int[] idsOfDomain = connect.listDomains();
map.put("listDomains", idsOfDomain);
List<HostDo> lists = new ArrayList<HostDo>();
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<HostDo> listDemo(@RequestParam String url) throws LibvirtException {
Connect connect = new Connect(url, true);
List<HostDo> lists = new ArrayList<HostDo>();
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,调试
qemu+tcp://10.50.1.60:16511/system
未完待续。。。后面会补充克隆等虚拟机操作