Libvirt API的C语言调用
| 編譯和安裝 ??1、(1)準備好libvirt-1.3.5安裝包,用MobaXterm工具將準備好的libvirt上傳到/usr/local/目錄下 ?? ?(2)將libvirt-1.3.5.tar解壓到/usr/local/目錄下 ???????? cd /usr/local/ ??? ?????tar -zxf libvirt-1.3.5.tar.gz ??2、配置libvirt ??? (1)配置libvirt時,需運行libvirt目錄下的configure腳本文件。 ??????? 進入libvirt解壓目錄下 ??????? cd libvirt-1.3.5 ??????? [root@localhost libvirt-1.3.5]#./configure ??????? ??3、編譯libvirt ??? (1)配置。/configure成功后,在libvirt安裝目錄下執行“make”命令編譯。 ????? ?cd libvirt-1.3.5 ?????? [root@localhost libvirt-1.3.5]#make ??4、安裝libvirt,libvirt 安裝會默認安裝libvirtd和virsh等可執行程序。 ???? (1)編譯完成后執行“make install”安裝libvirt [root@localhost libvirt-1.3.5]# make install ???? (2)查看libvirtd位置命令: [root@localhost libvirt-1.3.5]# which libvirtd ??????? ???? (3)查看libvirtd版本號:libvirtd -version ????? ????? (4) 查看virsh位置命令: ?????????? [root@localhost libvirt-1.3.5]# which virsh ????????? ?????? (5)查看virsh版本號: ???????? [root@localhost libvirt-1.3.5]# virsh -version ???????? ????? (6)查看libvirtd和virsh的頭文件和庫文件: ???????? [root@localhost libvirt-1.3.5]# ls /usr/local/include/libvirt ???????? ??????? [root@localhost libvirt-1.3.5]# ls /usr/local/lib/libvirt* ??????? ?? 5、查看已經安裝的libvirt ????? 查看libvirt是否啟動,實質是查看libvirt的libvirtd這個守護進程是否啟動。 ??? ??(1) [root@localhost libvirt-1.3.5]# ps -le |grep libvirtd ???? ????? (2)啟動libvirt [root@localhost libvirt-1.3.5]# service?libvirtd?start [root@localhost libvirt-1.3.5]# service?libvirtd?status
??? 1、libvirt文件配置 (1)libvirt的配置文件一般在/etc/libvirt/目錄下 ?????? [root@localhost ~]# cd /etc/libvirt ?????? [root@localhost libvirt]# ls ??? 2、libvirt中域的XML配置文件格式 ?<domain type='kvm'> ???? <!-- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! --> ???? <name>libvirt</name> ?? ??<uuid>32b95fab-6d78-4e28-beca-2d3988ff8c8e</uuid> ??? ?<memory unit='KiB'>1048576</memory> ???? <currentMemory unit='KiB'>1048576</currentMemory> ???? <vcpu placement='static'>1</vcpu> ???? <os> ????????? <type arch='x86_64' machine='pc-1.2'>hvm</type> ????????? <boot dev='hd'/> ???????? ?<boot dev='cdrom'/> ???? </os> ???? <features> ???????? ?<acpi/> ????????? <apic/> ????????? <pae/> ???? </features> ???? ?<clock offset='utc'/> ? ????<on_poweroff>destroy</on_poweroff> ???? ?<on_reboot>restart</on_reboot> ???? ?<on_crash>restart</on_crash> ???? <devices> ??? ??<!-- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! --> ??? ??<emulator>/usr/local/bin/qemu-system-x86_64</emulator> ??? ??<!-- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! --> ??? ??<disk type='file' device='disk'> ??? ????<driver name='qemu' type='qcow2'/> ????? ??<source file='/tmp/ubuntu.img'/> ????? ??<target dev='hda' bus='ide'/> ????? ??<address type='drive' controller='0' buss='0' target='0' unit='0'/> ???? ?</disk> ??? ??<!-- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! --> ???? ?<disk type='file' device='cdrom'> ??????? ?<driver name='qemu' type='raw'/> ???????? <source file='/tmp/ubuntu-18.04.5-desktop-amd64.iso'/> ??????? ?<target dev='hdc' bus='ide'/> ???????? <readonly/> ???? ?</disk> ? ????<!-- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! --> <controller type='usb' index='0'> ?? ??????<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/> ????? </controller> ??? ??<controller type='ide' index='0'> ? ???????<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/> ? ????</controller> ? ??????<interface type='bridge'> ???? ?????<mac address='52:54:00:78:f9:98'/> ???? ?????<source bridge='virbr0'/> ??? ??????<model type='virtio'/> ? ????????<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/> ???? ???</interface> ????????? <input type='mouse' bus='ps2'/> ????????? <graphics type='vnc' port='-1' autoprot='yes'/> ???????? ?<memballoon model='virtio'> ?????????? ?<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/> ????? ????</memballoon> ???? </devices> </domain> ??? 3、libvirt API使用示例 ????? (1)[root@localhost qemu]#virsh create /etc/libvirt/qemu/demo.xml ??????? ?[root@localhost qemu]#virsh -c qemu:///session ???? (2)使用API查詢某個域的信息,C語言示例如下: ??? ??#include<stdio.h> #include<stdlib.h> #include<libvirt/libvirt.h> virConnectPtr conn=NULL; virConnectPtr getConn() { ??????? ? conn=virConnectOpenReadOnly(NULL); ??????? ? if(conn==NULL) ??????? ? { ?????????? ? printf("error,cann't connect!"); ?????????? ? exit(1); ??????? ?? } ??????? ?? return conn; } int getInfo(int id) { ??????? ? virDomainPtr dom=NULL; ??????? ? virDomainInfo info; ??????? ??conn=getConn(); ??????? ? dom=virDomainLookupByID(conn,id); ??????? ? if(dom==NULL) ??????? ? { ?????????? ? printf("error,cann't find domain!'"); ?????????? ? virConnectClose(conn); ?????????? ? exit(1); ??????? ?? } ??????? ? if (virDomainGetInfo(dom,&info)<0) ??????? ? { ??????? ?? ???printf("error,cann't get info!"); ?????????? ?? virDomainFree(dom); ?????????? ?? exit(1); ??????? ?? } ??????? ? ?printf("the Domain state is:%c\n",info.state); ??????? ?? printf("the Domain allowed max memory is:%ld KB\n",info.maxMem); ??????? ?? printf("the Domain used memory is:%ld KB\n",info.memory); ??????? ?? printf("the Domain vCPU number is:%d\n",info.nrVirtCpu); ??????? ?? if(dom!=NULL) ??????? ?? { ?????????? ? virDomainFree(dom); ??????? ?? } ??????? ?? if(conn!=NULL) ??????? ? { ?????????? ? virConnectClose(conn); ??????? ?? } ??????? ? return 0; } int main() { ??????? ? getInfo(1); ??????? ? return 0; } ??? (3)編譯運行libvirt-conn.c并使用virsh查看當前節點情況 ???? ?[root@localhost ~]#virsh virsh#define /etc/libvirt/qemu/demo.xml virsh#start libvirt virsh#list ??? [root@localhost qemu]#ln -s /usr/local/var/run/libvirt /var/run/libvirt ??? virsh#domname1 virsh#dominfo 1 ?????? virsh#list ?????? [root@localhost ~]#vncviewer 172.0.0.1:0 |
| 問題解決: 一、問題: ?? 1、問題一:configure: error: You must install the libyajl library & headers to compile libvirt ???? ??解決方案:yum install yajl-devel 2、問題:You must install the pciaccess module to build with udev ?? ?解決方案:yum install libpciaccess-devel 3、問題:configure: error: You must install device-mapper-devel/libdevmapper >= 1.0.0 to ?compile libvirt ?? 解決方案:yum install device-mapper-devel 4、出現問題:checking libxml2 xml2-config >= 2.6.0 ... configure: error: Could not find libxml2 anywhere (see config.log for details). 解決方案:yum install libxml2-devel 5、問題:configure: error: libnl-devel >= 1.1 is required for macvtap support 6、問題:啟動Libvirt出現問題:libvirtd: error: Unable to obtain pidfile. Check /var/log/messages or run without --daemon for more 解決方案: (1)查找到libvirtd.pid文件,根據libvirt的安裝不同,位置可能不同: $ sudo find / -name libvirtd.pid [sudo] password for cloud: /usr/local/var/run/libvirtd.pid (2)刪除:$sudo rm /usr/local/var/run/libvirtd.pid (3)啟動:$sudo libvirtd -d |
總結
以上是生活随笔為你收集整理的Libvirt API的C语言调用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 对号动画实现
- 下一篇: Oracle创建directory