• 使用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. }

  • 相关阅读:
    安全基线核查
    Java 同步锁ReentrantLock与抽象同步队列AQS
    EPICS asyn诊断帮助
    冰蝎、蚁剑和哥斯拉
    期末复习总结【MySQL】五种约束类型, 主键和外键的使用方式(重点)
    怎么把amr格式转换成mp3?
    企业微信员工能私加客户吗?员工私自联系客户企业是否知道?
    常见大厂面试题(SQL)02
    【flex布局】解决:使用justify-content排列,一行四个,最后一行少于四个时,排列不会与上面的对齐
    Sql Server系列:子查询
  • 原文地址:https://blog.csdn.net/qq_29752857/article/details/132847084