目錄
- 1.libvirt介紹
- 2.解除安裝系統自帶的libvirt
- 2.1.檢視目前安裝的libvirt相關包
- 2.2.全部解除安裝掉
- 3.使用tar包編譯安裝
- 3.1.解壓縮
- 3.2.生成Makefile檔案
- 3.3.錯誤處理
- 檢視libvirt網絡配置
- 4.使用src.rpm包安裝
- 4.1.首先安裝rpm包
- 4.2.進入rpmbuild目錄執行spec檔案
- 4.3.逐一修複依賴關系
- 4.4.錯誤處理
- 錯誤1.無法啟動虛拟機
@
1.libvirt介紹
Libvirt是用于管理虛拟化平台的開源的API,背景程式和管理工具。它可以用于管理KVM、Xen、VMware ESX,QEMU和其他虛拟化技術。網上有很多文章做介紹,這裡就不詳細介紹了。libvirt本身安裝過程沒有什麼難度,主要在于原生的centos會自帶一個版本的libvirt,同時,如果自行編譯的版本在安裝之前,沒有把發行版自帶的libvirt解除安裝幹淨,就會出現各種各樣的錯誤。筆者工作較忙,時間比較倉促,排版可能不太好看,請見諒。
2.解除安裝系統自帶的libvirt
2.1.檢視目前安裝的libvirt相關包
[root@localhost libvirt-2.0.0]# rpm -qa |grep libvirt
2.2.全部解除安裝掉
由于目前筆者的環境已經是解除安裝幹淨的,是以,此處不做示範了
[root@localhost libvirt-2.0.0]# yum remove `rpm -qa |grep libvirt`
3.使用tar包編譯安裝
3.1.解壓縮
[root@localhost home]# tar xvf libvirt-2.0.0.tar.xz
3.2.生成Makefile檔案
注意:如果./configure不帶任何參數,各種檔案會被安裝到諸如/usr/local,/var/local下面,這樣和原生安裝路徑不一緻,通常不要
這樣安裝1
./configure --prefix=/usr --localstatedir=/var --sysconfdir=/etc --enable-debug=yes $ make $ sudo make install
3.3.錯誤處理
錯誤1:需要安裝依賴關系:
configure: error: You must install the pciaccess module to build with udev
[root@localhost libvirt-2.0.0]# yum install libpciaccess-devel
configure: error: You must install the libyajl library & headers to compile libvirt
[root@localhost libvirt-2.0.0]# yum install yajl-devel
configure: error: You must install device-mapper-devel/libdevmapper >= 1.0.0 to compile libvirt
[root@localhost libvirt-2.0.0]# yum install device-mapper-devel
configure: error: xml2-config not found. Please check your libxml2 installation.
yum -y install libxml2-devel
configure: error: libnl-devel >= 1.1 is required for macvtap support
configure: error: You must install the pciaccess module to build with udev
[root@localhost libvirt-2.0.0]# yum install libpciaccess-devel
configure: error: You must install the libyajl library & headers to compile libvirt
[root@localhost libvirt-2.0.0]# yum install yajl-devel
configure: error: You must install device-mapper-devel/libdevmapper >= 1.0.0 to compile libvirt
[root@localhost libvirt-2.0.0]# yum install device-mapper-devel
configure: error: xml2-config not found. Please check your libxml2 installation.
yum -y install libxml2-devel
錯誤2
沒有解除安裝系統自帶的,直接安裝的新的,但是版本一樣
#檢視服務狀态
[root@192 libvirt-2.0.0]# systemctl status libvirtd
● libvirtd.service - Virtualization daemon
Loaded: loaded (/usr/lib/systemd/system/libvirtd.service; enabled; vendor preset: enabled)
Active: inactive (dead) since Sun 2019-04-21 16:48:37 CST; 25s ago
Docs: man:libvirtd(8)
http://libvirt.org
Process: 79857 ExecStart=/usr/sbin/libvirtd $LIBVIRTD_ARGS (code=exited, status=0/SUCCESS)
Main PID: 79857 (code=exited, status=0/SUCCESS)
CGroup: /system.slice/libvirtd.service
├─2705 /sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/libexec/libvirt_leaseshelper
└─2706 /sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/libexec/libvirt_leaseshelper
Apr 21 16:48:36 localhost.localdomain systemd[1]: Started Virtualization daemon.
Apr 21 16:48:36 localhost.localdomain libvirtd[79857]: libvirt version: 2.0.0, package: 10.el7 (CentOS BuildSystem <http://bugs.centos.org>, 2016-11-12-02:15:12, c...entos.org)
Apr 21 16:48:36 localhost.localdomain libvirtd[79857]: hostname: localhost.localdomain
Apr 21 16:48:36 localhost.localdomain libvirtd[79857]: operation failed: network 'default' already exists with uuid 0c2459eb-2797-4a07-93af-6fb5857a6831
Apr 21 16:48:37 localhost.localdomain dnsmasq[2705]: read /etc/hosts - 2 addresses
Apr 21 16:48:37 localhost.localdomain dnsmasq[2705]: read /var/lib/libvirt/dnsmasq/default.addnhosts - 0 addresses
Apr 21 16:48:37 localhost.localdomain dnsmasq-dhcp[2705]: read /var/lib/libvirt/dnsmasq/default.hostsfile
Apr 21 16:48:37 localhost.localdomain libvirtd[79857]: internal error: failed to initialize netcf
Apr 21 16:48:37 localhost.localdomain libvirtd[79857]: Initialization of netcf state driver failed: internal error: failed to initialize netcf
Apr 21 16:48:37 localhost.localdomain libvirtd[79857]: Driver state initialization failed
Hint: Some lines were ellipsized, use -l to show in full.
處理方式,殺死殘餘程序
[root@192 libvirt-2.0.0]# ps -ef|grep libvirt
nobody 2705 1 0 12:20 ? 00:00:00 /sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/libexec/libvirt_leaseshelper
root 2706 2705 0 12:20 ? 00:00:00 /sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/libexec/libvirt_leaseshelper
root 79999 3730 0 16:52 pts/0 00:00:00 grep --color=auto libvirt
[root@192 libvirt-2.0.0]# kill -9 2705
[root@192 libvirt-2.0.0]# ps -ef|grep libvirt
root 80016 3730 0 16:53 pts/0 00:00:00 grep --color=auto libvirt
libvirt可以正常工作,但是依然有報錯
Apr 21 16:53:45 localhost.localdomain libvirtd[80026]: operation failed: network 'default' already exists with uuid 0c2459eb-2797-4a07-93af-6fb5857a6831
檢視libvirt網絡配置
[root@192 libvirt-2.0.0]# virsh net-list --all
Name State Autostart Persistent
---------------------------------------------------
default active no no
#檢視網絡的xml
[root@192 libvirt-2.0.0]# virsh net-dumpxml default
<network>
<name>default</name>
<uuid>0c2459eb-2797-4a07-93af-6fb5857a6831</uuid>
<forward mode='nat'>
<nat>
<port start='1024' end='65535'/>
</nat>
</forward>
<bridge name='virbr0' stp='on' delay='0'/>
<mac address='52:54:00:38:f8:e0'/>
<ip address='192.168.122.1' netmask='255.255.255.0'>
<dhcp>
<range start='192.168.122.2' end='192.168.122.254'/>
</dhcp>
</ip>
</network>
#删除該net之後重新開機libvirt程序遇到新問題
Apr 21 19:18:52 localhost.localdomain libvirtd[82476]: Initialization of netcf state driver failed: internal error: failed to initialize netcf
#關閉selinux之後,正常
[root@localhost ~]# setenforce 0
#網絡也重新生成了2
[root@localhost ~]# virsh net-list
Name State Autostart Persistent
----------------------------------------------------------
default active yes yes
錯誤3
virsh: error while loading shared libraries: libvirt-lxc.so.0: cannot open shared object file: No such file or directory
這是因為動态庫更新了,沒有重新整理,需要重新整理一下
bash-4.2# ldconfig
bash-4.2# virsh --version
2.0.0
錯誤4
[root@localhost libvirt-2.0.0]# virsh list
error: failed to connect to the hypervisor
error: no valid connection
error: Failed to connect socket to '/var/run/libvirt/libvirt-sock': No such file or directory
這是因為程序沒有啟動
[root@localhost libvirt-2.0.0]# systemctl start libvirtd
[root@localhost libvirt-2.0.0]# systemctl status libvirtd
● libvirtd.service - Virtualization daemon
Loaded: loaded (/usr/lib/systemd/system/libvirtd.service; disabled; vendor preset: enabled)
Active: active (running) since Wed 2019-05-15 10:28:31 EDT; 5s ago
Docs: man:libvirtd(8)
http://libvirt.org
Main PID: 81685 (libvirtd)
CGroup: /system.slice/libvirtd.service
├─81685 /usr/sbin/libvirtd
├─81825 /sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/libexec/libvirt_leaseshe...
└─81826 /sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/libexec/libvirt_leaseshe...
May 15 10:28:31 localhost.localdomain systemd[1]: Started Virtualization daemon.
May 15 10:28:33 localhost.localdomain dnsmasq[81825]: started, version 2.76 cachesize 150
May 15 10:28:33 localhost.localdomain dnsmasq[81825]: compile time options: IPv6 GNU-getopt DBus no-i18n IDN DHCP DHCPv6 no-Lua TFTP no...notify
May 15 10:28:33 localhost.localdomain dnsmasq-dhcp[81825]: DHCP, IP range 192.168.122.2 -- 192.168.122.254, lease time 1h
May 15 10:28:33 localhost.localdomain dnsmasq-dhcp[81825]: DHCP, sockets bound exclusively to interface virbr0
May 15 10:28:33 localhost.localdomain dnsmasq[81825]: reading /etc/resolv.conf
May 15 10:28:33 localhost.localdomain dnsmasq[81825]: using nameserver 192.168.233.2#53
May 15 10:28:33 localhost.localdomain dnsmasq[81825]: read /etc/hosts - 2 addresses
May 15 10:28:33 localhost.localdomain dnsmasq[81825]: read /var/lib/libvirt/dnsmasq/default.addnhosts - 0 addresses
May 15 10:28:33 localhost.localdomain dnsmasq-dhcp[81825]: read /var/lib/libvirt/dnsmasq/default.hostsfile
Hint: Some lines were ellipsized, use -l to show in full.
4.使用src.rpm包安裝
由于筆者實驗環境不小心被破壞,為了以後再出問題可以更快的恢複編譯環境,選擇使用在chroot的jail裡面搭建一個編譯環境,友善備份和還原,但是使用這種方式, make完了之後,無法在宿主環境中make install,缺少很多東西,但是又不想在jail之外的環境安裝過多的依賴包,否則就違背了初衷,是以,決定嘗試用src.rpm包來編譯安裝(此處都是基于chroot環境在操作,普通環境操作方式一樣,除了yum install的路徑不同)
4.1.首先安裝rpm包
bash-4.2# rpm -i libvirt-2.0.0-1.fc23.src.rpm
warning: libvirt-2.0.0-1.fc23.src.rpm: Header V4 DSA/SHA1 Signature, key ID de95bc1f: NOKEY
warning: user veillard does not exist - using root
warning: group veillard does not exist - using root
warning: user veillard does not exist - using root
warning: group veillard does not exist - using root
4.2.進入rpmbuild目錄執行spec檔案
bash-4.2# cd /root/rpmbuild/
bash-4.2# ll
total 8
drwxr-xr-x. 2 root root 4096 Aug 11 06:52 SOURCES
drwxr-xr-x. 2 root root 4096 Aug 11 06:52 SPECS
bash-4.2# rpmbuild -bp S
SOURCES/ SPECS/
bash-4.2# rpmbuild -bp SPECS/libvirt.spec
error: Failed build dependencies:
xhtml1-dtds is needed by libvirt-2.0.0-1.el7.x86_64
libxslt is needed by libvirt-2.0.0-1.el7.x86_64
readline-devel is needed by libvirt-2.0.0-1.el7.x86_64
ncurses-devel is needed by libvirt-2.0.0-1.el7.x86_64
libtasn1-devel is needed by libvirt-2.0.0-1.el7.x86_64
gnutls-devel is needed by libvirt-2.0.0-1.el7.x86_64
libattr-devel is needed by libvirt-2.0.0-1.el7.x86_64
libblkid-devel >= 2.17 is needed by libvirt-2.0.0-1.el7.x86_64
augeas is needed by libvirt-2.0.0-1.el7.x86_64
sanlock-devel >= 2.4 is needed by libvirt-2.0.0-1.el7.x86_64
libpcap-devel is needed by libvirt-2.0.0-1.el7.x86_64
libnl3-devel is needed by libvirt-2.0.0-1.el7.x86_64
avahi-devel is needed by libvirt-2.0.0-1.el7.x86_64
dnsmasq >= 2.41 is needed by libvirt-2.0.0-1.el7.x86_64
radvd is needed by libvirt-2.0.0-1.el7.x86_64
ebtables is needed by libvirt-2.0.0-1.el7.x86_64
cyrus-sasl-devel is needed by libvirt-2.0.0-1.el7.x86_64
polkit-devel >= 0.112 is needed by libvirt-2.0.0-1.el7.x86_64
/usr/bin/qemu-img is needed by libvirt-2.0.0-1.el7.x86_64
iscsi-initiator-utils is needed by libvirt-2.0.0-1.el7.x86_64
parted-devel is needed by libvirt-2.0.0-1.el7.x86_64
librados2-devel is needed by libvirt-2.0.0-1.el7.x86_64
librbd1-devel is needed by libvirt-2.0.0-1.el7.x86_64
glusterfs-api-devel >= 3.4.1 is needed by libvirt-2.0.0-1.el7.x86_64
glusterfs-devel >= 3.4.1 is needed by libvirt-2.0.0-1.el7.x86_64
libcap-ng-devel >= 0.5.0 is needed by libvirt-2.0.0-1.el7.x86_64
fuse-devel >= 2.8.6 is needed by libvirt-2.0.0-1.el7.x86_64
netcf-devel >= 0.2.2 is needed by libvirt-2.0.0-1.el7.x86_64
libcurl-devel is needed by libvirt-2.0.0-1.el7.x86_64
audit-libs-devel is needed by libvirt-2.0.0-1.el7.x86_64
systemtap-sdt-devel is needed by libvirt-2.0.0-1.el7.x86_64
nfs-utils is needed by libvirt-2.0.0-1.el7.x86_64
dbus-devel is needed by libvirt-2.0.0-1.el7.x86_64
scrub is needed by libvirt-2.0.0-1.el7.x86_64
numad is needed by libvirt-2.0.0-1.el7.x86_64
4.3.逐一修複依賴關系
yum --installroot=/home/user3/jail install -y xhtml1-dtds
yum --installroot=/home/user3/jail install -y libxslt
yum --installroot=/home/user3/jail install -y readline-devel
yum --installroot=/home/user3/jail install -y libtasn1-devel
yum --installroot=/home/user3/jail install -y gnutls-devel
yum --installroot=/home/user3/jail install -y libattr-devel
yum --installroot=/home/user3/jail install -y libblkid-devel
yum --installroot=/home/user3/jail install -y augeas sanlock-devel sanlock
yum --installroot=/home/user3/jail install -y libpcap-devel
...
最後再次嘗試,
bash-4.2# rpmbuild -bp SPECS/libvirt.spec
error: Failed build dependencies:
/usr/bin/qemu-img is needed by libvirt-2.0.0-1.el7.x86_64
隻剩下這一個,安裝了該檔案也無法解決,原因未知,是以查閱資料3,得知,這個不重要,是以可以修改spec檔案,注釋掉這個。
# From QEMU RPMs
# BuildRequires: /usr/bin/qemu-img
根據
./configure --prefix=/usr --localstatedir=/var --sysconfdir=/etc --enable-debug=yes
在spec檔案configure處增加--enable-debug
執行編譯
bash-4.2# rpmbuild -bb SPECS/libvirt.spec
如果成功,則會生成
bash-4.2# ll RPMS/x86_64/
total 21284
-rw-r--r--. 1 root root 2716 Aug 11 07:47 libvirt-2.0.0-1.el7.x86_64.rpm
-rw-r--r--. 1 root root 4380632 Aug 11 07:47 libvirt-client-2.0.0-1.el7.x86_64.rpm
-rw-r--r--. 1 root root 588380 Aug 11 07:47 libvirt-daemon-2.0.0-1.el7.x86_64.rpm
-rw-r--r--. 1 root root 3868 Aug 11 07:47 libvirt-daemon-config-network-2.0.0-1.el7.x86_64.rpm
-rw-r--r--. 1 root root 6356 Aug 11 07:47 libvirt-daemon-config-nwfilter-2.0.0-1.el7.x86_64.rpm
-rw-r--r--. 1 root root 47564 Aug 11 07:47 libvirt-daemon-driver-interface-2.0.0-1.el7.x86_64.rpm
-rw-r--r--. 1 root root 669320 Aug 11 07:47 libvirt-daemon-driver-lxc-2.0.0-1.el7.x86_64.rpm
-rw-r--r--. 1 root root 202876 Aug 11 07:47 libvirt-daemon-driver-network-2.0.0-1.el7.x86_64.rpm
-rw-r--r--. 1 root root 46672 Aug 11 07:47 libvirt-daemon-driver-nodedev-2.0.0-1.el7.x86_64.rpm
-rw-r--r--. 1 root root 71392 Aug 11 07:47 libvirt-daemon-driver-nwfilter-2.0.0-1.el7.x86_64.rpm
-rw-r--r--. 1 root root 478708 Aug 11 07:47 libvirt-daemon-driver-qemu-2.0.0-1.el7.x86_64.rpm
-rw-r--r--. 1 root root 36804 Aug 11 07:47 libvirt-daemon-driver-secret-2.0.0-1.el7.x86_64.rpm
-rw-r--r--. 1 root root 233904 Aug 11 07:47 libvirt-daemon-driver-storage-2.0.0-1.el7.x86_64.rpm
-rw-r--r--. 1 root root 1984 Aug 11 07:47 libvirt-daemon-kvm-2.0.0-1.el7.x86_64.rpm
-rw-r--r--. 1 root root 1956 Aug 11 07:47 libvirt-daemon-lxc-2.0.0-1.el7.x86_64.rpm
-rw-r--r--. 1 root root 10537260 Aug 11 07:47 libvirt-debuginfo-2.0.0-1.el7.x86_64.rpm
-rw-r--r--. 1 root root 152084 Aug 11 07:47 libvirt-devel-2.0.0-1.el7.x86_64.rpm
-rw-r--r--. 1 root root 3781352 Aug 11 07:47 libvirt-docs-2.0.0-1.el7.x86_64.rpm
-rw-r--r--. 1 root root 52048 Aug 11 07:47 libvirt-lock-sanlock-2.0.0-1.el7.x86_64.rpm
-rw-r--r--. 1 root root 334884 Aug 11 07:47 libvirt-login-shell-2.0.0-1.el7.x86_64.rpm
-rw-r--r--. 1 root root 123980 Aug 11 07:47 libvirt-nss-2.0.0-1.el7.x86_64.rpm
然後安裝所有的rpm包
[root@localhost home]# rpm -i /home/user3/jail/root/rpmbuild/RPMS/x86_64/*
可能會提示缺包,yum安裝一下就行了
[root@localhost home]# yum install -y augeas sanlock-devel sanlock
啟動libvirtd程序
[root@localhost home]# systemctl start libvirtd
[root@localhost home]# virsh list
Id Name State
----------------------------------------------------
[root@localhost home]# virsh -v
2.0.0
安裝成功,檢視libvirtd的路徑
[root@localhost home]# ps -ef|grep libvirtd
root 16193 1 0 16:04 ? 00:00:00 /usr/sbin/libvirtd
root 16407 21296 0 16:07 pts/0 00:00:00 grep --color=auto libvirtd
經過測試,可以正常啟動虛拟機
4.4.錯誤處理
錯誤1.無法啟動虛拟機
[root@localhost user3]# virsh start vm_vbras_64
error: Failed to start domain vm_vbras_64
error: Failed to connect socket to '/var/run/libvirt/virtlogd-sock': Connection refused
可能是因為重裝之前有相關程序沒殺掉,這個東西具體做什麼的還沒有研究
[root@localhost user3]# ll /var/run/libvirt/virtlogd-sock
srw-rw-rw-. 1 root root 0 Aug 11 11:57 /var/run/libvirt/virtlogd-sock
[root@localhost user3]# systemctl stop libvirtd
[root@localhost user3]# rm /var/run/libvirt/virtlogd-sock
rm: remove socket ‘/var/run/libvirt/virtlogd-sock’? y
[root@localhost ~]# /usr/sbin/virtlogd --daemon
[root@localhost user3]# systemctl start libvirtd
再重新執行即可正常啟動虛拟機
更新日期:20190911,長期更新維護...
- Linux下編譯安裝qemu和libvirt↩
- Networking↩
- Re: [PATCH 20/27] libvirt.spec.in: remove most storage conditionals↩