基于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 = "<domain type='kvm'>"+
  13.              "<name>tt2</name>"+
  14.              "<memory>524288</memory>"+
  15.              "<currentMemory>524288</currentMemory>"+
  16.              "<vcpu>1</vcpu>"+
  17.              "<os>"+
  18.              " <type arch='i686' machine='pc-0.11'>hvm</type>"+
  19.              " <boot dev='hd'/>"+
  20.              "</os>"+
  21.              "<features>"+
  22.              " <acpi/>"+
  23.              " <apic/>"+
  24.              " <pae/>"+
  25.              "</features>"+
  26.              "<clock offset='utc'/>"+
  27.              "<on_poweroff>destroy</on_poweroff>"+
  28.              "<on_reboot>restart</on_reboot>"+
  29.              "<on_crash>restart</on_crash>"+
  30.              "<devices>"+
  31.              " <emulator>/usr/bin/qemu-kvm</emulator>"+
  32.              " <disk type='file' device='disk'>"+
  33.              " <driver name='qemu' cache='none'/>"+
  34.              " <source file='/var/lib/libvirt/images/tt.img'/>"+
  35.              " <target dev='hda' bus='ide'/>"+
  36.              " </disk>"+
  37.              " <interface type='network'>"+
  38.              " <mac address='54:52:00:02:02:2c'/>"+
  39.              " <source network='default'/>"+
  40.              " </interface>"+
  41.              " <serial type='pty'>"+
  42.              " <target port='0'/>"+
  43.              " </serial>"+
  44.              " <console type='pty'>"+
  45.              " <target port='0'/>"+
  46.              " </console>"+
  47.              " <input type='mouse' bus='ps2'/>"+
  48.              " <graphics type='vnc' port='-1' autoport='yes' keymap='en-us'/>"+
  49.              "</devices>"+
  50.             "</domain>";
  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/
精彩科技工作室

猜你喜欢

转载自yaolinnan.iteye.com/blog/1750453