ubuntu 11.10 安装与配置libvirt0.9.11.3
解压libvirt-0.9.11.3.tar.gz
cd libvirt-0.9.11.3
./configure
出现:
checking libxml2 xml2-config >= 2.6.0..
则:
sudo apt-get install libxml2 libxml2-dev
configrue: error: you must install the gnutls library in order to compile and run libvirt
sudo apt-get install gnutls-doc gnutls-bin
sudo apt-get install libneon27-gnutls libcurl4-gnutls-dev
又提示缺少device-mapper-devel
从网上下载device-mapper.1.02.28.tgz,然后解压缩tar -zxvf device-mapper.1.02.28.tgz,
进入解压后的文件夹中:cd device-mapper.1.02.28。安装device-mapper。
(1) ./configure
(2) sudo make
(3) sudo make install
然后退出该文件夹,重新安装libvirt
You must install python-devel to build Python bindings
sudo apt-get install python-dev
configure: error: libnl-devel >= 1.1 is required for macvtap support
sudo apt-get install libnl-dev
最后:
sudo make
sudo make install
启动libvirt后台进程
sudo libvirtd &
更新动态链接库缓存
sudo ldconfig
c语言库的使用
编辑好.c源码(例如myxm.cpp)后,在命令行下编译 gcc myxm.c -lvirt -o myxm
然后运行./myxm即可
例子:
#include <stdio.h>
#include <stdlib.h>
#include <libvirt/libvirt.h>
int main()
{
printf("Hello World!\n");
virConnectPtr conn=NULL;
conn=virConnectOpenReadOnly(NULL);
if(conn==NULL)
{
printf("conn is null!\n");
}
else
{
printf("connecting is successful!\n");
}
return 0;
}
使用过程出现的问题:
解决error while loading shared libraries: libvirt.so.0
第一种:
定义shell变量 LD_LIBRARY_PATH
$ LD_LIBRARY_PATH=/usr/local/lib
$ export LD_LIBRARY_PATH
$ ./example
第二种:
修改系统动态链接库配置文件
(the system dynamic linker configuration)
进入目录/etc/ld.so.conf.d
添加一个文件libvirt.conf,内容是:
# libvirt default configuration
/usr/local/lib
更新动态链接库缓存
sudo ldconfig
sudo apt-get install libxml2 libxml2-dev gnutls-doc gnutls-bin libneon27-gnutls libcurl4-gnutls-dev python-dev libnl-dev
出现的问题及解决办法
解决failed to connect socket to '/usr/local/var/run/libvirt/libvirt-sock':no such file or directory添加libvirt用户组sudo groupadd libvirt添加当前用户到libvirt组里sudo gpasswd yaxin libvirt记得要重启,才能有用 error: ebiptablesDriverInit:4129 : essential tools to support ip(6) tables firewalls could not be located编辑文件/usr/local/etc/libvirt/libvirtd.conf 取消一下几项的注释unix_sock_group = "libvirt"unix_sock_ro_perms = "0777"unix_sock_rw_perms = "0770"unix_sock_dir = "/var/run/libvirt"修改为unix_sock_dir = "/usr/local/var/run/libvirt"auto_unix_ro = "none"auth_unix_rw = "none" 如果要启动自动运行libvirtd编辑文件/etc/rc.local在exit 0之前添加sudo libvirtd &