23
2024
05
17:42:14

Centos8下安装KVM虚拟机

0、准本环境

  1. 系统环境 产品:VMware® Workstation 16 Pro 版本:16.1.2 build-17966106

  2. 操作环境 CentOS Linux release 8.4.2105

  3. 连接软件 Xshell、FileZilla

  4. BIOS开启虚拟化技术

  5. 开启CPU虚拟化

1、准备一台裸机,配置网络和本地yum源

查看配置和ip地址

[root@localhost ~]# cat /etc/redhat-release 
CentOS Linux release 8.4.2105
[root@localhost ~]# uname -a
Linux localhost.localdomain 4.18.0-305.12.1.el8_4.x86_64 #1 SMP Wed Aug 11 01:59:55 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
[root@localhost ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:39:e7:ab brd ff:ff:ff:ff:ff:ff
    inet 192.168.116.129/24 brd 192.168.116.255 scope global dynamic noprefixroute ens33
       valid_lft 1148sec preferred_lft 1148sec
    inet6 fe80::13d0:b078:5c60:8824/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

可以看到192.168.116.129是我的虚拟机ip地址,使用Xshell连接即可方便地在Xshell上操作了

配置本地源

在虚拟机设置里使用Centos安装时的镜像,里面有软件,还有repodata/repomd.xml这个文件,这是建立本地镜像时需要读取的。我这里的是CentOS-8-x86_64-1905-dvd1.iso,读者可根据自己的系统镜像更改。

然后进入/etc/yum.repo.d/,先备份,再写新的源文件。

备份和删除:

mkidr /opt/yum.repos.d
cp -arp /etc/yum.repos.d/* /opt/yum.repos.d
rm -rf /etc/yum.repos.d/*

新建本地源:

[root@localhost ~]# df -h
文件系统             容量  已用  可用 已用% 挂载点
devtmpfs             3.8G     0  3.8G    0% /dev
tmpfs                3.8G     0  3.8G    0% /dev/shm
tmpfs                3.8G  9.8M  3.8G    1% /run
tmpfs                3.8G     0  3.8G    0% /sys/fs/cgroup
/dev/mapper/cl-root   35G  9.2G   26G   27% /
/dev/sda1            976M  262M  648M   29% /boot
tmpfs                775M   28K  775M    1% /run/user/1000
tmpfs                775M     0  775M    0% /run/user/0
/dev/sr0             6.7G  6.7G     0  100% /run/media/leon/CentOS-8-BaseOS-x86_64

先看ISO文件挂到那里了,这里能看到在/run/media/leon/CentOS-8-BaseOS-x86_64,但是这还不够,repodata这个文件其实是在/run/media/leon/CentOS-8-BaseOS-x86_64/BaseOS这个目录里的。

[root@localhost ~]# ll /run/media/leon/CentOS-8-BaseOS-x86_64/BaseOS
总用量 294
dr-xr-xr-x 2 leon leon 296960 8月  16 2019 Packages
dr-xr-xr-x 2 leon leon   4096 8月  16 2019 repodata

有了这个信息,就可以写Base.repo了

vim /etc/yum.repos.d/Base.repo

[Base]
name=Base
baseurl=file:///run/media/leon/CentOS-8-BaseOS-x86_64/BaseOS
gpgcheck=0
enabled=1

清理cache,建立新的cache

yum clean all
yum makecache

这样就可以用本地源了。

2、安装KVM相关软件包

[root@localhost ~]# yum install qemu-kvm qemu-img  virt-manager libvirt libvirt-python virt-manager  libvirt-client virt-install virt-viewer -y
上次元数据过期检查:0:02:19 前,执行于 2022年02月13日 星期日 16时28分48秒。
模块依赖问题:

 问题 1: conflicting requests

  - nothing provides module(perl:5.26) needed by module perl-DBD-SQLite:1.58:8010020191114033549:073fa5fe-0.x86_64
    问题 2: conflicting requests
  - nothing provides module(perl:5.26) needed by module perl-DBI:1.641:8010020191113222731:16b3ab4d-0.x86_64
    软件包 qemu-kvm-15:4.2.0-48.module_el8.4.0+885+5e18b468.3.x86_64 已安装。
    软件包 qemu-img-15:4.2.0-48.module_el8.4.0+885+5e18b468.3.x86_64 已安装。
    未找到匹配的参数: virt-manager
    未找到匹配的参数: libvirt
    未找到匹配的参数: libvirt-python
    未找到匹配的参数: libvirt-client
    未找到匹配的参数: virt-install
    未找到匹配的参数: virt-viewer
    错误:没有任何匹配: virt-manager libvirt libvirt-python libvirt-client virt-install virt-viewer

本地源不好用,很多软件找不到,换阿里源。

centos8(centos8官方源已下线,建议切换centos-vault源)

wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo

继续安装,发现还是报错

[root@localhost ~]# yum install virt-manager libvirt libvirt-python virt-manager  libvirt-client virt-install virt-viewer -y
上次元数据过期检查:0:01:04 前,执行于 2022年02月13日 星期日 17时01分03秒。
未找到匹配的参数: libvirt-python
错误:没有任何匹配: libvirt-python

去除报错的包,继续安装

`[root@localhost ~]# yum install virt-manager libvirt virt-manager  libvirt-client virt-install virt-viewer -y`
上次元数据过期检查:0:01:20 前,执行于 2022年02月13日 星期日 17时01分03秒。

依赖关系解决。

 软件包                                                                              架构                                            版本                                                                            仓库                                                大小

安装:
 libvirt                                                                             x86_64                                          6.0.0-37.module_el8.5.0+1002+36725df2                                           AppStream                                           61 k
 libvirt-client                                                                      x86_64                                          6.0.0-37.module_el8.5.0+1002+36725df2                                           AppStream                                          368 k
 virt-install                                                                        noarch                                          2.2.1-4.el8                                                                     AppStream                                          100 k
 virt-manager                                                                        noarch                                          2.2.1-4.el8                                                                     AppStream                                          369 k
 virt-viewer                                                                         x86_64                                          9.0-11.el8                                                                      AppStream                                          426 k
……

……

已安装:
  autogen-libopts-5.18.12-8.el8.x86_64                                   gnutls-dane-3.6.16-4.el8.x86_64                               gnutls-utils-3.6.16-4.el8.x86_64                                              libvirt-6.0.0-37.module_el8.5.0+1002+36725df2.x86_64  
  libvirt-bash-completion-6.0.0-37.module_el8.5.0+1002+36725df2.x86_64   libvirt-client-6.0.0-37.module_el8.5.0+1002+36725df2.x86_64   libvirt-daemon-config-nwfilter-6.0.0-37.module_el8.5.0+1002+36725df2.x86_64   python3-argcomplete-1.9.3-6.el8.noarch                
  python3-libvirt-6.0.0-1.module_el8.5.0+746+bbd5d70c.x86_64             virt-install-2.2.1-4.el8.noarch                               virt-manager-2.2.1-4.el8.noarch                                               virt-manager-common-2.2.1-4.el8.noarch                
  virt-viewer-9.0-11.el8.x86_64                                         

完毕!

再找方法安装libvirt-python

先继续安装kvm环境

yum install -y libvirt-devel

[root@localhost ~]# yum install -y libvirt-devel
上次元数据过期检查:0:03:39 前,执行于 2022年02月13日 星期日 17时01分03秒。

依赖关系解决。

 软件包                                                        架构                                                   版本                                                                                    仓库                                                       大小

安装:
 libvirt-devel                                                 x86_64                                                 6.0.0-37.module_el8.5.0+1002+36725df2                                                   AppStream                                                 234 k

事务概要

安装  1 软件包

总下载:234 k
安装大小:1.3 M
下载软件包:

libvirt-devel-6.0.0-37.module_el8.5.0+1002+36725df2.x86_64.rpm                                                                                                                                                                                594 kB/s | 234 kB     00:00    

总计                                                                                                                                                                                                                                          592 kB/s | 234 kB     00:00     
运行事务检查
事务检查成功。
运行事务测试
事务测试成功。
运行事务
  准备中  :                                                                                                                                                                                                                                                               1/1 
  安装    : libvirt-devel-6.0.0-37.module_el8.5.0+1002+36725df2.x86_64                                                                                                                                                                                                    1/1 
  运行脚本: libvirt-devel-6.0.0-37.module_el8.5.0+1002+36725df2.x86_64                                                                                                                                                                                                    1/1 
  验证    : libvirt-devel-6.0.0-37.module_el8.5.0+1002+36725df2.x86_64                                                                                                                                                                                                    1/1 
Installed products updated.

已安装:
  libvirt-devel-6.0.0-37.module_el8.5.0+1002+36725df2.x86_64                                                                                                                                                                                                                  

完毕!

安装python3

yum -y install python3

[root@localhost ~]# yum -y install python3
上次元数据过期检查:0:04:07 前,执行于 2022年02月13日 星期日 17时01分03秒。
软件包 python36-3.6.8-2.module_el8.4.0+790+083e3d81.x86_64 已安装。

依赖关系解决。

 软件包                                                     架构                                                    版本                                                                                     仓库                                                        大小

升级:
 chkconfig                                                  x86_64                                                  1.19.1-1.el8                                                                             base                                                       198 k
 python36                                                   x86_64                                                  3.6.8-38.module_el8.5.0+895+a459eca8                                                     AppStream                                                   19 k

事务概要

升级  2 软件包

总下载:218 k
下载软件包:
(1/2): python36-3.6.8-38.module_el8.5.0+895+a459eca8.x86_64.rpm                                                                                                                                                                                94 kB/s |  19 kB     00:00    

(2/2): chkconfig-1.19.1-1.el8.x86_64.rpm                                                                                                                                                                                                      431 kB/s | 198 kB     00:00    

总计                                                                                                                                                                                                                                          471 kB/s | 218 kB     00:00     
运行事务检查
事务检查成功。
运行事务测试
事务测试成功。
运行事务
  准备中  :                                                                                                                                                                                                                                                               1/1 
  升级    : chkconfig-1.19.1-1.el8.x86_64                                                                                                                                                                                                                                 1/4 
  升级    : python36-3.6.8-38.module_el8.5.0+895+a459eca8.x86_64                                                                                                                                                                                                          2/4 
  运行脚本: python36-3.6.8-38.module_el8.5.0+895+a459eca8.x86_64                                                                                                                                                                                                          2/4 
  清理    : python36-3.6.8-2.module_el8.4.0+790+083e3d81.x86_64                                                                                                                                                                                                           3/4 
  运行脚本: python36-3.6.8-2.module_el8.4.0+790+083e3d81.x86_64                                                                                                                                                                                                           3/4 
  清理    : chkconfig-1.13-2.el8.x86_64                                                                                                                                                                                                                                   4/4 
  运行脚本: chkconfig-1.13-2.el8.x86_64                                                                                                                                                                                                                                   4/4 
  验证    : chkconfig-1.19.1-1.el8.x86_64                                                                                                                                                                                                                                 1/4 
  验证    : chkconfig-1.13-2.el8.x86_64                                                                                                                                                                                                                                   2/4 
  验证    : python36-3.6.8-38.module_el8.5.0+895+a459eca8.x86_64                                                                                                                                                                                                          3/4 
  验证    : python36-3.6.8-2.module_el8.4.0+790+083e3d81.x86_64                                                                                                                                                                                                           4/4 
Installed products updated.

已升级:
  chkconfig-1.19.1-1.el8.x86_64                                                                                              python36-3.6.8-38.module_el8.5.0+895+a459eca8.x86_64                                                                                             

完毕!

装完我检查发现libvirt-python已经有了。

[root@localhost ~]# pip3 list |grep libvirt-python
libvirt-python (6.0.0)

这个libvirt-python装得有点稀里糊涂,不过好在是能用了。这应该就是装python3的时候自动装上去的,本来还想下这个包再手动用pip3 steup.py install的,目前看来用不着了。

安装后看其他教程发现直接用groupinstall更方便,就重新安装了,更新和替换了一些软件。也许直接用这个更好。

yum groupinstall "Virtualization Host"

3、安装虚拟机

以上软件包都安装完了的话,就可以在命令行中输入virt-manager调出KVM虚拟机的控制台了,然后就能在图形界面安装系统套娃了。

也可以通过命令行创建虚拟机,然后用VNC连接安装。这写可以参照网上资料进行。本次就先到这了。






推荐本站淘宝优惠价购买喜欢的宝贝:

image.png

本文链接:https://hqyman.cn/post/6300.html 非本站原创文章欢迎转载,原创文章需保留本站地址!

分享到:
打赏





休息一下~~


« 上一篇 下一篇 »

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

请先 登录 再评论,若不是会员请先 注册

您的IP地址是: