libvirt2.0安装

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,长期更新维护...


  1. Linux下编译安装qemu和libvirt

  2. Networking

  3. Re: [PATCH 20/27] libvirt.spec.in: remove most storage conditionals

猜你喜欢

转载自www.cnblogs.com/daxiatou/p/11335761.html