當前位置:首頁 » 編程軟體 » libvirt編譯與安裝

libvirt編譯與安裝

發布時間: 2022-04-27 12:35:50

1. 如何在linux上安裝libvirt虛擬化工具

libvirt是一套免費、開源的支持Linux下主流虛擬化工具libvirt 比較和用例模型的C函數庫,其旨在為包括Xen在內的各種虛擬化工具提供一套方便、可靠的編程介面,支持與C,C++,Ruby,python等多種主流開發語言的綁定。當前主流Linux平台上默認的虛擬化管理工具virt-manager(圖形化),virt-install(命令行模式)等均基於libvirt開發而成。

Linux下虛擬化工具llibvirt安裝步驟:
by TANG Jia
本測試通過的版本號為:ubuntu12.04、libvirt1.1.2
安裝libvirt:
1,安裝virtinst
apt get installvirtinst
安裝libvirt之前首先安裝virtinst目的是為了使後面編譯安裝libvirt時,覆蓋安裝virtinst時自帶的libvirt的版本,這點很重要。之後才能編譯安裝libvirt.
2,下載libvirt1.1.2,解壓縮,切換至libvirt1.1.2目錄
下載libvirt壓縮包網址:ftp://libvirt.org/libvirt/
3,如果你直接執行./configure,會提示你缺少很多包,所以先安裝包:
apt-get install libxml++2.6-2 libxml++2.6-dev
apt-get install libdevmapper-dev
apt-get install libpciaccess-dev
apt-get install python-dev
apt-get install libnl-dev
4,安裝完依賴包後,在當前目錄下執行 ./configure--prefix=/usr
(其中--prefix選項為指定安裝路徑)。
執行成功,部分輸出如下:
configure:Readline:no
configure:Python:yes
configure:DTrace:no
configure:numad:no
configure:XML Catalog:/etc/xml/catalog
configure:Init script:check
configure:Char device locks:/var/lock
configure:
configure:Privileges
configure:
configure:QEMU:root:root
configure:
5,編譯:make
安裝:makeinstall
至此libvirtd安裝成功!
6,啟動libvirt服務
service libvirt-bin start
萬萬要注意啦,libvirt-daemon是由libvirt-bin來啟動的,而不是libvirtd,libvirt-bin是一個符號鏈接/etc/init.d/libvirt-bin-> /lib/init/upstart-job,後者是一個shell腳本
同樣,停止libvirt服務就是service libvrit-bin stop;
以上操作的正確性僅限於在ubuntu上安裝libvirt,重點是第1步和第6步,這和網上很多博文所講的libvirt安裝有些差異,比如很多沒有第1步,第6步啟動libvirt為service libvirtd
start,這會報錯為libvirtd:unrecognizedservice.
第2,3,4,5步的下載、編譯、安裝在任何版本的Linux上都一致。
libvirt 提供一種虛擬機監控程序不可知的 API 來安全管理運行於主機上的來賓操作系統。希望以上對llibvirt安裝流程介紹對大家有所幫助。關注系統部落,鎖定電腦系統下載!

2. 如何在python文件中導入libvirt

方法/步驟 1 使用 python -V 查詢本機python系統。 2 再安裝新版之前安裝 先安裝bz2、zlib,執行下列代碼進行安裝 # yum install -y zlib-devel bzip2-devel xz-libs wget 3 採用wget下載python,執行: # wget http://www.python.org/ftp/pytho...

3. 安裝kvm虛擬機為什麼還要安裝libvirt

kvm本身是做內核虛擬化的,是一種hypervisor,libvirt是一套對接hypervisor的庫函數介面,他可以對接多種hyperviser,在libvirt上面可以有cli和gui

4. centos 6.0 安裝libvirt-python 的具體步驟是什麼樣的

centos6.0的yum源中是有libvirt-python的,你只需要如下操作即可:

yuminstalllibvirt-python-y

5. 如何安裝XEN

對於Fedora系統,它的程序包管理器是yum,將用它從預編譯好的程序包來安裝Xen。實際上,使用yum安裝Xen是一件相當輕松的工作,具體過程如下所示:
1、通過下列命令來安裝Xen 內核和有關軟體:
yum install kernel-xen xen
這一命令將下載並安裝以下程序包及其依賴包:
l kernel-xen:該程序包存放的是主機操作系統和客戶操作系統以及系統管理程序所用的Xen內核。
l xen:該程序包存放的是跟Xen系統管理程序交互的用戶空間工具。
l bridge-utils :該程序包存放的是用於配置乙太網橋的實用程序,乙太網橋的作用是將多個乙太網設備連在一起。
l libvirt :該程序包存放的是一個C庫,它提供了使用Xen框架所需的應用編程介面API 。
l libvirt-python :該程序包存放的是一個Python 模塊,有了它就可以讓Python 應用程序使用libvirt提供的Xen框架應用編程介面。
l Python-virtinst :該程序包存放的是一個Python 模塊,該模塊使用libvirt在虛擬機內安裝Fedora /Red Hat Enterprise Linux 。
l Xen-libs :該程序包存放的是Xen的管理程序所需的程序庫。

6. 在ubuntu系統下編譯c程序,提示缺少libvirt.h,我已經安裝了libvirt,這個頭文件要怎麼擁有呢

你安裝的libvirt.h路徑可能不在gcc默認的$INCLUDE環境變數裡面。

可以將它添加到INCLUDE裡面,也可以編譯的時候加-I選項手動指定,gcc -I /path/to/libvirt

7. ibvirterror:將插槽連接到 /usr/local/var/run/libvirt/libvirt-sock'失敗:沒

你安裝的libvirt.h路徑可能不在gcc默認的$INCLUDE環境變數裡面。可以將它添加到INCLUDE裡面,也可以編譯的時候加-I選項手動指定,gcc-I/path/to/libvirt

8. 如何在Debian或Ubuntu上編譯virt-manager

眼下有幾種不同的方法可以管理在KVM虛擬機管理程序上運行的虛擬機。比如說,virt-manager就是一種廣受歡迎的基於圖形用戶界面(GUI)的前端工具,可用來管理虛擬機。不過,如果你想在無外設伺服器上使用KVM,那麼基於GUI的解決方案並非理想方案。這時候,virsh就派得上用場。virsh是一種命令行工具,可用於管理來賓虛擬機(guest

VM)。就其底層而言,virsh依賴libvirtd服務,該服務可以控制幾種不同的虛擬機管理程序,其中包括KVM、Xen、QEMU、LXC和OpenVZ。

如果你想讓虛擬機的配置和管理實現自動化,virsh等命令行管理界面也大有用處。此外,virsh支持多種虛擬機管理程序,這就意味著你可以通過同一個virsh界面,管理不同的虛擬機管理程序。

我在本教程中將演示如何在Debian或Ubuntu上使用virsh,從命令行運行KVM。

第一步:證實主機支持硬體虛擬化

作為第一步,證實主機的處理器配備硬體虛擬化擴展機制(比如英特爾VT或AMD-V),KVM需要這樣的機制。下面這個命令就能證實一點。

$
egrep '(vmx|svm)' --color /proc/cpuinfo flags : fpu vme de
pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts
mmx fxsr sse sse2
ss syscall nx rdtscp lm constant_tsc up arch_perfmon pebs bts nopl
xtopology tsc_reliable nonstop_tsc aperfmperf pni pclmulqdq vmx ssse3
cx16 pcid sse4_1 sse4_2 x2apic popcnt aes xsave avx f16c rdrand
hypervisor lahf_lm ida arat epb xsaveopt pln pts dtherm tpr_shadow vnmi
ept vpid fsgsbase smep

如果輸出結果並不含有vmx或svm標記,這意味著主機的處理器沒有支持硬體虛擬化的功能。因而,你就無法在主機上使用KVM。證實了主機處理器隨帶vmx或svm後,接下來繼續安裝KVM。

第二步:安裝KVM

使用apt-get,安裝KVM及相關的用戶空間工具。

$ sudo apt-get install qemu-kvm libvirt-bin

下一步,將你的用戶ID添加到libvirt群組,那樣你就能以非root普通用戶的身份,管理虛擬機了。作為上面安裝KVM的一部分,必須創建libvirt群組。

$ sudo adser [youruserID] libvirt

重新裝入更新後的群組成員信息,如下所示。看到要求輸入密碼的提示後,輸入你的登錄密碼。

$ exec su -l $USER

這時候,你應該能夠以普通用戶的身份運行virsh了。為了測試一下,不妨試一試下面這個命令,它會列出可用的虛擬機(目前沒有一個虛擬機)。要是你沒有遇到許可權錯誤,這意味著到目前為止,一切正常。

$ virsh --connect qemu:///system list Id Name State

9. ubuntu14.10能支持xen到多少版本,ubuntu12.04

Ubuntu12.04安裝Xen4

linux3.0內核已經支持xen了,所以不用再編譯內核了(對於那些內核支持的系統都不用重新編譯內核)。

第一步:安裝需要的軟體包

安裝編譯 Xen 和 Linux xen kernel 所需要的軟體包:

$ sudo apt-get install gcc g++ make patch libssl-dev bzip2 gettext \

zlib1g-dev python libncurses5-dev libjpeg62-dev libx11-dev \

libgcrypt11-dev pkg-config bridge-utils bcc bin86 libpci-dev \

libsdl-dev python-dev texinfo libc6-dev uuid-dev bison flex fakeroot \

build-essential crash kexec-tools makempfile libncurses5 \

libncurses5-dev iasl gawk

第二步:安裝xen vmm

32位和64位系統的區別也很重要。64位按下面步驟很簡單,32位就不同,因為xen需要開啟pae,默認是沒有開啟的,所以32位ubuntu首先得執行下面這個命令:

sudo apt-get install linux-image-server

sudo apt-get install xen-hypervisor-4.1-amd64 xen-utils-4.1 xenwatch xen-tools xen-utils-common xenstore-utils

這里xen-hypervisor-4.1要分32位、64位,64位系統用xen-hypervisor-4.1-amd64,32位用xen-hypervisor-4.1-i386.

第三步:安裝libvirt和Virtual Manager

sudo apt-get install virtinst python-libvirt virt-viewer virt-manager

第四步:重啟機器,選擇有xen的內核啟動,進入系統之後,再檢查一下 Xen 是否已經成功安裝:

$ sudo xm list

Name ID Mem VCPUs State Time(s)

Domain-0 0 1879 2 r----- 6.6

出現上面的信息,則代表安裝成功。

第五步:修改配置文件

sudo gedit /etc/xen/xend-config.sxp

將/etc/xen/xend-config.sxp文件中的「#(xend-unix-server no)」注釋去掉 即把「#」去掉,並把「no」改成「yes」,保存即可。 然後重啟xend服務

sudo gedit ~/.bashrc ,在打開的.bashrc文件中添加下面這句話

export VIRSH_DEFAULT_CONNECT_URI="xen:///"

第六步:打開Virtual Manage時,但是創建時因為相關資源路徑問題可能會報以下錯誤

6.1 ******/usr/lib/xen/bin/qemu-dm:****
路徑中xen應該是xen-4.1,解決辦法如下

mkdir /usr/lib/xen -p
cp /usr/lib/xen-4.1/* -r /usr/lib/xen/

6.2 ****** libvirtError: POST操作失敗: xend_post:來自 xen 守護進程的錯誤:****
查看日誌 /var/log/xen/xend.log 和 /var/log/xen/qemu-dm-demo.log
日誌顯示/usr/share/qemu/keymaps/en-us could not be found, the keymaps ***
路徑中qemu應該是qemu-linaro,解決辦法如下

cp -r /usr/share/qemu-linaro/ /usr/share/qemu

這樣創建過程基本沒有問題了.

第七步:通過libvirt查看xen的版本信息,確認libvirt和xen都已經安裝成功

# virsh version

Compiled against library: libvir 0.9.8

Using library: libvir 0.9.8

Using API: Xen 0.9.8

Running hypervisor: Xen 4.1

10. libvirt-java怎麼獲得虛擬機內存使用率

最近在用Nagios監控Xen PV虛擬機的時候出現了問題,在被監控的伺服器上是採用nrpe來採集數據的。但是在進程里無法看到PV虛擬機的進程,雖然可以通過xm top vpsname的方式來獲取名為vpsname虛擬機的cpu使用率情況,但是不便於採集數據,通過xm list可以採集到cpu時間,根據CPU時間的差值,可以計算CPU使用率,可是該命令只能root執行,因為該命令可以進行關閉,重啟虛擬機等重要操作,所以如果把許可權給了nrpe,將可能造成嚴重的安全問題。
幸好livirt提供了API,所以我打算嘗試用API寫一個Nagios的插件來滿足的我的需求,我的想法就是分別2次獲得虛擬機的CPU時間,並分別記錄2次取得數據時的系統時間,然後根據差值來計算,在理論上是存在一些誤差的。
1.要使用API,首先需要安裝libvirt-devel
[root@aikaiyuan ~]# yum -y install libvirt-devel

2.我的代碼如下,文件名為vCpu.c
/**
* Program Name: vCpu.c
* Author: steptodream
* Description:A simple plugin to get vps cpu usage
* for nagios(nrpe) by libvirt api
* Compile:gcc -o vCpu vCpu.c -lvirt
*/
#include <stdlib.h>
#include <stdio.h>
#include <libvirt/libvirt.h>
/* define the exit status for nagios */
#define OK 0
#define WARNING 1
#define CRITICAL 2
#define UNKNOWN 3
/* get cpu time of the given name */
double getCpuTime(char *vpsName,virConnectPtr conn) {
virDomainInfo info;
virDomainPtr domain = NULL;
int ret;
/* Find the domain of the given name */
domain = virDomainLookupByName(conn, vpsName);
if (domain == NULL) {
printf("Failed to find the vps called %sn", vpsName);
exit(OK);
}
/* Get the information of the domain */
ret = virDomainGetInfo(domain, &info);
virDomainFree(domain);
if (ret < 0) {
printf("Failed to get information for %sn", vpsName);
exit(OK);
}
return info.cpuTime;
}
int main(int argc,char * argv[])
{
char *vpsName; /* vps name */
int interval = 1; /* check interval */
double warning; /* warning value */
double critical; /* critical value */
double cpuUsage; /* cpu usage of the vps */
struct timeval startTime; /* time of the first time to get cpu time */
struct timeval endTime; /* time of the second time to get cpu time */
int realTime; /* real interval between two times */
long long startCpuTime; /* cpu time of the first time */
long long endCpuTime; /* cpu time of the second time */
int cpuTime; /* value of startCpuTime - endCpuTime */
char *output; /* output data for nagios */
int ret; /* exit status for nagios */
virConnectPtr conn; /* connection pointer */
switch (argc){
case 5:
interval = atoi(argv[4]);
case 4:
vpsName = argv[1];
warning = atof(argv[2]);
critical = atof(argv[3]);
break;
default:
printf("Usage:vCpu <vName> <warning> <critical> [interval]nn");
return OK;
}
/* connect to local Xen Host */
conn = virConnectOpenReadOnly(NULL);
if (conn == NULL) {
printf("Failed to connect to local Xen Hostn");
return OK;
}
/* get cpu time the first time */
startCpuTime = getCpuTime(vpsName, conn);
/* get start time */
if (gettimeofday(&startTime, NULL) == -1) {
printf("Failed to get start timen");
return OK;
}
/* wait for some seconds */
sleep(interval);
/* get cpu time the second time */
endCpuTime = getCpuTime(vpsName, conn);
/* get end time */
if (gettimeofday(&endTime, NULL) == -1) {
printf("Failed to get end timen");
return OK;
}
/* colose connection */
virConnectClose(conn);
/* calculate the usage of cpu */
cpuTime = (startCpuTime - endCpuTime) / 1000;
realTime = 1000000 * (startTime.tv_sec - endTime.tv_sec) +
(startTime.tv_usec - endTime.tv_usec);
cpuUsage = cpuTime / (double)(realTime);
/* display cpuUsage by percentage */
cpuUsage *= 100;

/* make output data and exit status for nagios*/
if (cpuUsage > critical) {
output = "CRITICAL";
ret = CRITICAL;
} else if (cpuUsage > warning){
output = "WARNING";
ret = WARNING;
} else {
output = "OK";
ret = OK;
}
printf("%s CPU:%.2f%|CPU=%.2f",output,cpuUsage,cpuUsage);
return ret;
}

3.編譯測試,根據我的需求,我設置了3個必須參數和1個可選參數,分別是虛擬機名稱vpsName,警告值warning(百分比值),危急值critical(百分比值)和檢查間隔interval(秒)
[root@aikaiyuan ~]# gcc -o vCpu vCpu.c -lvirt
[root@aikaiyuan ~]# ./vCpu vmtest 1 2
OK CPU:0.20%|CPU=0.20

當然了,你同時可以再打開一個終端,用xm top vmtest來獲取vmtest的cpu使用率,然後對比一下取值是否接近一致。我們再來看看返回值是否正常,因為Nagios是靠這個來判斷服務狀態的。
[root@aikaiyuan ~]# echo $?
0

注意,我的具體要求是檢測指定名稱的虛擬機的CPU使用率,如果超過了規定的warning或者critical值,就給使用者發郵件,所以在沒有得到數據或者程序出錯的情況,我都是以正常狀態退出程序的。
另外,本人開發經驗薄弱,所以代碼里難免存在錯誤和不合理的地方以及不完善的地方(比如參數的合法性檢驗),僅供參考。
最後,關於代碼中時間,我是以微秒(us)為統一單位來計算的,得到的cpu的時間是納秒(ns),這個在結構體virDomainInfo定義中可以看到:
struct virDomainInfo{
unsigned char state : the running state, one of virDomainState
unsigned long maxMem : the maximum memory in KBytes allowed
unsigned long memory : the memory in KBytes used by the domain
unsigned short nrVirtCpu : the number of virtual CPUs for the domain
unsigned long long cpuTime : the CPU time used in nanoseconds
}

而通過gettimeofday取得存取到timeval結構體里的時間,包含tv_sec(s秒)和tv_usec(us微秒)這2種單位,可以從timeval結構體的定義中看到:
struct timeval {
time_t tv_sec; /* seconds */
suseconds_t tv_usec; /* microseconds */
};

熱點內容
真我手機如何解除手機密碼 發布:2024-05-04 18:24:44 瀏覽:708
資料庫嵌套 發布:2024-05-04 18:24:29 瀏覽:146
豌豆莢源碼 發布:2024-05-04 18:10:54 瀏覽:117
蘋果消息的聲音安卓怎麼弄 發布:2024-05-04 18:06:23 瀏覽:555
減配配置有哪些 發布:2024-05-04 18:04:58 瀏覽:963
查詢密碼單是什麼 發布:2024-05-04 17:54:03 瀏覽:41
安卓系統不支持網路怎麼辦 發布:2024-05-04 17:49:31 瀏覽:128
oraclesqlserver 發布:2024-05-04 17:49:16 瀏覽:47
關愛腳本 發布:2024-05-04 17:43:47 瀏覽:422
linuxshellif 發布:2024-05-04 17:09:47 瀏覽:17