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
ant build
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);
本文转自mushiqianmeng 51CTO博客,原文链接:http://blog.51cto.com/mushiqianmeng/863774,如需转载请自行联系原作者