• 使用java连接Libvirtd


    基于springboot web

    一、依赖

    1. <dependency>
    2. <groupId>org.springframework.bootgroupId>
    3. <artifactId>spring-boot-starter-webartifactId>
    4. dependency>
    5. <dependency>
    6. <groupId>org.springframework.bootgroupId>
    7. <artifactId>spring-boot-starter-testartifactId>
    8. <scope>testscope>
    9. dependency>
    10. <dependency>
    11. <groupId>org.libvirtgroupId>
    12. <artifactId>libvirtartifactId>
    13. <version>0.5.2version>
    14. dependency>
    15. <dependency>
    16. <groupId>net.java.dev.jnagroupId>
    17. <artifactId>jnaartifactId>
    18. <version>5.13.0version>
    19. dependency>
    20. <dependency>
    21. <groupId>org.projectlombokgroupId>
    22. <artifactId>lombokartifactId>
    23. <version>1.18.24version>
    24. dependency>
    25. <dependency>
    26. <groupId>com.alibaba.fastjson2groupId>
    27. <artifactId>fastjson2artifactId>
    28. <version>2.0.37version>
    29. dependency>

    二、代码

    1. package com.libvirtddemo.controller;
    2. import com.alibaba.fastjson2.JSON; // 引入fastjson库,用于将对象转换为JSON字符串
    3. import lombok.extern.slf4j.Slf4j; // 引入Slf4j注解,方便日志记录
    4. import org.libvirt.Connect; // 引入libvirt库的Connect类
    5. import org.libvirt.Library; // 引入libvirt库的Library类
    6. import org.libvirt.LibvirtException; // 引入libvirt库的LibvirtException类
    7. import org.springframework.web.bind.annotation.GetMapping; // 引入Spring的GetMapping注解
    8. import org.springframework.web.bind.annotation.RestController; // 引入Spring的RestController注解
    9. import java.util.HashMap; // 引入HashMap类
    10. import java.util.Map; // 引入Map类
    11. import java.util.Objects; // 引入Objects类
    12. @RestController // 声明该类是一个RESTful API控制器
    13. @Slf4j // 添加Slf4j注解,方便日志记录
    14. public class LibvirtdController {
    15. static private Map s_connections = new HashMap(); // 定义一个静态Map对象,用于存储连接对象
    16. @GetMapping("/connection") // 处理GET请求的映射,路径为"/connection"
    17. public String connectLibvirt(String hypervisorURI) throws LibvirtException {
    18. log.info("hypervisorURI: " + hypervisorURI); // 记录日志,输出hypervisorURI的值
    19. if (hypervisorURI == null || Objects.equals(hypervisorURI, "")) { // 判断hypervisorURI是否为空或者为空字符串
    20. hypervisorURI = "qemu:///system"; // 如果为空,设置默认值为"qemu:///system"
    21. }
    22. log.info("Looking for libvirtd connection at: " + hypervisorURI); // 记录日志,输出连接的libvirtd的URI
    23. Connect conn = s_connections.get(hypervisorURI); // 从连接Map中获取指定URI的连接对象
    24. if (conn == null) { // 如果连接对象为空
    25. log.info("No existing libvirtd connection found. Opening a new one"); // 记录日志,表示没有已经存在的连接对象
    26. conn = new Connect(hypervisorURI, false); // 创建新的连接对象
    27. Library.initEventLoop(); // 初始化事件循环
    28. log.info("Successfully connected to libvirt at: " + hypervisorURI); // 记录日志,表示成功连接到libvirtd
    29. s_connections.put(hypervisorURI, conn); // 将连接对象加入到连接Map中
    30. } else { // 如果连接对象不为空
    31. try {
    32. long version = conn.getVersion(); // 获取libvirtd的版本号
    33. log.info("version:{}", version); // 记录日志,输出版本号
    34. } catch (LibvirtException e) {
    35. log.error("Connection with libvirtd is broken: " + e.getMessage()); // 记录错误日志,表示与libvirtd的连接中断
    36. log.info("Opening a new libvirtd connection to: " + hypervisorURI); // 记录日志,表示打开一个新的libvirtd连接
    37. conn = new Connect(hypervisorURI, false); // 创建新的连接对象
    38. s_connections.put(hypervisorURI, conn); // 将连接对象加入到连接Map中
    39. }
    40. }
    41. log.info("conn:{}", JSON.toJSONString(conn)); // 记录日志,输出连接对象的JSON字符串形式
    42. return JSON.toJSONString(conn); // 将连接对象转换为JSON字符串并返回
    43. }
    44. }

    URL说明

    1. qemu:///... QEMU and KVM URIs¶
    2. To use QEMU support in libvirt you must be running the libvirtd daemon (named libvirt_qemud in releases prior to 0.3.0). The purpose of this daemon is to manage qemu instances.
    3. The libvirtd daemon should be started by the init scripts when the machine boots. It should appear as a process libvirtd --daemon running as root in the background and will handle qemu instances on behalf of all users of the machine (among other things).
    4. So to connect to the daemon, one of two different URIs is used:
    5. qemu:///system connects to a system mode daemon.
    6. qemu:///session connects to a session mode daemon.
    7. (If you do libvirtd --help, the daemon will print out the paths of the Unix domain socket(s) that it listens on in the various different modes).
    8. KVM URIs are identical. You select between qemu, qemu accelerated and KVM guests in the guest XML as described here.

    三、测试

    1. curl http://localhost:8080/connection?hypervisorURI=qemu%3A%2F%2F%2Fsystem
    2. curl http://localhost:8080/connection

    四、官网案例

    https://java.libvirt.org/
    1. import org.libvirt.*;
    2. public class minitest {
    3. public static void main(String[] args) {
    4. Connect conn=null;
    5. try{
    6. conn = new Connect("test:///default", true);
    7. } catch (LibvirtException e) {
    8. System.out.println("exception caught:"+e);
    9. System.out.println(e.getError());
    10. }
    11. try{
    12. Domain testDomain=conn.domainLookupByName("test");
    13. System.out.println("Domain:" + testDomain.getName() + " id " +
    14. testDomain.getID() + " running " +
    15. testDomain.getOSType());
    16. } catch (LibvirtException e) {
    17. System.out.println("exception caught:"+e);
    18. System.out.println(e.getError());
    19. }
    20. }
    21. }

  • 相关阅读:
    2022谷粒商城学习笔记(十九)认证服务整合短信验证码和OAuth2第三方社交登录
    超实用的图片压缩大小教程,手把手教会你
    Streamsets Data Collector 3.12
    计算机视觉:使用opencv实现银行卡号识别
    应急响应-网站入侵篡改指南_Webshell内存马查杀_漏洞排查_时间分析
    【Luckfox pico入门记录(一)】开发环境与工具链
    山西电力市场日前价格预测【2023-11-20】
    【弃坑xdm】docker容器作为开发环境,更加灵活可靠
    权限模型 DAC ACL RBAC ABAC
    VScode配置Jupyter
  • 原文地址:https://blog.csdn.net/qq_29752857/article/details/132847084