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
- A CIM provider for the DMTF virtualization schema
- A QMF agent for the AMQP/QPid messaging system
libvirt supports:
- The KVM/QEMU Linux hypervisor
- The Xen hypervisor on Linux and Solaris hosts.
- The LXC Linux container system
- The OpenVZ Linux container system
- The User Mode Linux paravirtualized kernel
- The VirtualBox hypervisor
- The VMware ESX and GSX hypervisors
- The VMware Workstation and Player hypervisors
- The Microsoft Hyper-V hypervisor
- Virtual networks using bridging, NAT, VEPA and VN-LINK.
- Storage on IDE/SCSI/USB disks, FibreChannel, LVM, iSCSI, NFS and filesystems
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
ant build
libvirt也提供了Maven庫:
這麼多途徑,相信你總可以搞到一份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路徑,有兩種方式,一種是直接設置系統環境變量:
另一種是可在程序中動態指定,筆者選擇了後者,比較靈活簡單,編寫測試代碼如下:
- public void testGetXenVMs() {
- try {
- System.setProperty("jna.library.path", "D:/Git-Repo/git/libvirt-java/libvirt-java/src/test/java/kubi/coder/");
- Connect conn = new Connect("xen+tcp://10.4.55.203/");
- System.out.println(conn.nodeInfo().cores);
- for (String name : conn.listDefinedDomains()) {
- System.out.println(name);
- if (name != null) {
- Domain domain = conn.domainLookupByName(name);
- System.out.println(domain.getMaxMemory());
- System.out.println(domain.getUUIDString());
- System.out.println(domain.getInfo().maxMem);
- System.out.println(domain.getInfo().state);
- System.out.println(conn.listDomains().length);
- }
- }
- } catch (LibvirtException e) {
- e.printStackTrace();
- }
- }
是不是還是找不到dll?報異常?
- Exception in thread "main" java.lang.UnsatisfiedLinkError: Unable to load library 'virt'
原來他是搜索叫virt的dll文件。
查看源碼:
- Libvirt INSTANCE = (Libvirt) Native.loadLibrary("virt", Libvirt.class);
確實如此,將libvirt-0.dll改名爲virt.dll。結果終於出來了。
注:libvirt的Java API封裝的比較直觀,上手很容易,其入口就是Connect 這個連接類,獲取連接後,即可對虛擬機環境進行查看和管理操作。筆者後續會奉上Java API詳細使用介紹。