Windows下Libvirt Java API使用教程(一)- 開發環境部署

  Libvirt(http://libvirt.org/)是一個比較不錯的虛擬化環境管理的工具包。核心用c實現,不過提供了不同語言的調用API。官網的簡介如下:


 

libvirt is:

  • A toolkit to interact with the virtualization capabilities of recent versions of Linux (and other OSes), see our project goals for details.
  • Free software available under the GNU Lesser General Public License.
  • A long term stable C API
  • A set of bindings for common languages
  • CIM provider for the DMTF virtualization schema
  • QMF agent for the AMQP/QPid messaging system

libvirt supports:

libvirt provides:

  • Remote management using TLS encryption and x509 certificates
  • Remote management authenticating with Kerberos and SASL
  • Local access control using PolicyKit
  • Zero-conf discovery using Avahi multicast-DNS
  • Management of virtual machines, virtual networks and storage
  • Portable client API for Linux, Solaris and Windows

 

由於筆者我是一個簡單而純粹的Java程序員,所以自然只能依賴於libvirt的Java binding api。
作爲一個源碼控,我選擇下載源碼的方式驗證使用:
源碼的git地址如下:
 
git clone git://libvirt.org/libvirt-java.git 
 
筆者下載源碼後,直接構建了Eclipse的工程,當然你也可以用源碼編譯(ant)出一份jar來依賴:
 
cd libvirt-java 
ant build 
 
libvirt也提供了Maven庫:
 
沒有Maven?可以直接從Maven庫中下載Jar包:
 
 
這麼多途徑,相信你總可以搞到一份libvirt的源碼或者Jar了吧。
 
由於libvirt的核心都是c寫的,Java API只是幫助你封裝了對動態鏈接庫(dll)文件的本地調用,所以現在應該做的是安裝dll文件。
 
libvirt官網提供了自行編譯dll文件的腳本:

MSYS Build script

The easiest way is to use the msys_setup script, developed by Matthias Bolte. This is actively developed and kept current with libvirt releases:

https://github.com/photron/msys_setup 


不過筆者並沒有嘗試該種方式,因爲libvirt官網也提供了windows下的安裝包:


Experimental installation package

A windows installation package is in development. An experimental version is available here:

http://libvirt.org/sources/win32_experimental/Libvirt-0.8.8-0.exe

It is not production ready.(注:其並不是已經發布的產品)


該安裝包中不僅包含了需要的dll文件,還提供了一個方便好用的virsh-shell 命令行工具,通過命令可以調用libvirt的所有接口(查看,管理虛擬機等。),對於我們的開發調試是非常有幫助的。

安裝完成後,想讓Java API找到dll文件,還需要指定jna路徑,有兩種方式,一種是直接設置系統環境變量:

另一種是可在程序中動態指定,筆者選擇了後者,比較靈活簡單,編寫測試代碼如下:

  1. public void testGetXenVMs() { 
  2.         try { 
  3.             System.setProperty("jna.library.path""D:/Git-Repo/git/libvirt-java/libvirt-java/src/test/java/kubi/coder/"); 
  4.             Connect conn = new Connect("xen+tcp://10.4.55.203/"); 
  5.             System.out.println(conn.nodeInfo().cores); 
  6.             for (String name : conn.listDefinedDomains()) { 
  7.                 System.out.println(name); 
  8.                 if (name != null) { 
  9.                     Domain domain = conn.domainLookupByName(name); 
  10.                     System.out.println(domain.getMaxMemory()); 
  11.                     System.out.println(domain.getUUIDString()); 
  12.                     System.out.println(domain.getInfo().maxMem); 
  13.                     System.out.println(domain.getInfo().state); 
  14.                     System.out.println(conn.listDomains().length); 
  15.                 } 
  16.             } 
  17.         } catch (LibvirtException e) { 
  18.             e.printStackTrace(); 
  19.         } 
  20.     } 

是不是還是找不到dll報異常?

  1. Exception in thread "main" java.lang.UnsatisfiedLinkError: Unable to load library 'virt' 

原來他是搜索叫virt的dll文件。

查看源碼:

  1. Libvirt INSTANCE = (Libvirt) Native.loadLibrary("virt", Libvirt.class); 

確實如此,將libvirt-0.dll改名爲virt.dll。結果終於出來了。
 
:libvirt的Java API封裝的比較直觀,上手很容易,其入口就是Connect 這個連接類,獲取連接後,即可對虛擬機環境進行查看和管理操作。筆者後續會奉上Java API詳細使用介紹。

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章