基于java的libvirt接口

准备:

1.确保你的linux平台搭建了kvm环境

 

2.下载libvirt-java-0.4.7.tar.gz  官方网站:http://libvirt.org/sources/java/

 

3.打开eclipse,新建工程,导入libvirt包

 

开发:

1.根据xml配置文件创建虚拟机

 

  1. public class Test {
  2.     /**
  3.      * @param args
  4.      */
  5.     public static void main(String[] args) {
  6.         Connect conn = null;
  7.         int flags = 0;
  8.         try {
  9.             
  10.             
  11.             conn = new Connect("qemu:///system", false);
  12.             String dumpxml = ""+
  13.              "tt2"+
  14.              "524288"+
  15.              "524288"+
  16.              "1"+
  17.              ""+
  18.              " hvm"+
  19.              " "+
  20.              ""+
  21.              ""+
  22.              " "+
  23.              " "+
  24.              " "+
  25.              ""+
  26.              ""+
  27.              "destroy"+
  28.              "restart"+
  29.              "restart"+
  30.              ""+
  31.              " /usr/bin/qemu-kvm"+
  32.              " "+
  33.              " "+
  34.              " "+
  35.              " "+
  36.              " "+
  37.              " "+
  38.              " "+
  39.              " "+
  40.              " "+
  41.              " "+
  42.              " "+
  43.              " "+
  44.              " "+
  45.              " "+
  46.              " "+
  47.              " "+
  48.              " "+
  49.              ""+
  50.             "";
  51.             
  52.             
  53.             Domain dm = null;
  54.             
  55.             Domain dm1 = conn.domainDefineXML(dumpxml);
  56.             //Domain dm1 = conn.domainDefineXML(dumpxml); //创建一个域,但不启动
  57.             //dm1.undefine(); //undefind一个域,如果活动,则不停止
  58.             /****
  59.             try{
  60.                 Domain dm1 =conn.domainLookupByName("tt2");
  61.             }
  62.             catch (LibvirtException e) {
  63.                 System.out.println("libvirt 错误" + e);
  64.                 // TODO: handle exception
  65.             }
  66.             if(dm!=null)
  67.             {
  68.                 
  69.                 dm.undefine();
  70.                 
  71.             }
  72.             ****/
  73.         
  74.             /********
  75.             Domain dm2 = conn.domainDefineXML(dumpxml);
  76.             dm2.undefine();
  77.             *******/
  78.                         
  79.         } catch (LibvirtException e) { //LibvirtException 返回错误详细信息
  80.             System.out.println("exception caught:" + e);
  81.             System.out.println("获取的错误;" + e.getError()); 
  82.             return;
  83.         }
  84.         
  85.         System.out.println("Clean exit");
  86.     }
  87. }
2 libvirt库函数说明
 
(1)
  1. conn = new Connect("qemu:///system",true)
  1. conn = new Connect("qemu:///system",false)

 

第一个是以只读的方式打开连接

第二个可以对domain进行写操作

 

(2)

  1. Domain dm1 = conn.domainDefineXML(dumpxml);
创建一个域, 但不启动
 
(3)
 
  1. dm1.undefine();
undefind一个域,如果活动,则不停止
 
3.官方API文档:http://libvirt.org/sources/java/javadoc/
精彩科技工作室

你可能感兴趣的:(基于java的libvirt接口)