安装KVM并且在KVM中安装rhel7
KVM简单介绍
KVM是Kernel-based Virtual Machine的简称,是一个开源的系统虚拟化模块。KVM是一块开源软件,基于内核的虚拟化技术,实际是嵌入系统的一个虚拟化模块,通过优化内核来使用虚拟化技术,该内核模块使得linux变成了一个hypervisor,虚拟机使用linux自身的调度器进行管理。
KVM的内核模块为kvm.ko,只用于管理虚拟CPU和内存。IO的虚拟化是交给linux内核和qemu来实现的。
libvirt是KVM的管理工具。libvirt包含后台daemon程序libvirtd、API库和命令行工具virsh,其中libvirtd是运行在后台的服务程序,接收和处理API请求;API库使得其他人可以开发基于libvirt的高级工具,比如virt-manager这个图形化的kvm管理工具;virsh是经常要使用的kvm命令行工具。
接下来演示在redhat7主机上安装KVM。
在安装kvm之前需要查看主机是否支持虚拟化?
主机支持虚拟化必须具备三个条件:
1、CPU必须是64位
2、CPU具有虚拟化功能
3、在BIOS中要开启虚拟化
因为kvm是要安装在redhat主机上,因此要查看redhat上是否支持虚拟化,通过查看CPU信息的命令来查看。
从flags字段中可以看到CPU两个标志,lm表明CPU为64位,vmx表示此CPU支持虚拟化功能。
当然也可以使用命令直接查看
KVM是基于内核的虚拟机,因此还要查看redhat系统也必须加载kvm模块。
安装KVM
KVM的软件都在redhat安装镜像里面,所以需要将安装镜像作为软件源。redhat的镜像文件作为redhat里面的虚拟机的软件源,我这里通过搭建ftp服务器来创建软件源。
1、首先安装vsftpd软件包
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 40G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 4G 0 part [SWAP]
└─sda3 8:3 0 35G 0 part /
sr0 11:0 1 3.5G 0 rom
[root@localhost ~]# mount /dev/sr0 /mnt
mount: /dev/sr0 is write-protected, mounting read-only
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 35G 3.0G 33G 9% /
devtmpfs 3.8G 0 3.8G 0% /dev
tmpfs 3.9G 84K 3.9G 1% /dev/shm
tmpfs 3.9G 9.0M 3.9G 1% /run
tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
/dev/sda1 1014M 169M 846M 17% /boot
tmpfs 781M 16K 781M 1% /run/user/42
tmpfs 781M 0 781M 0% /run/user/0
/dev/sr0 3.6G 3.6G 0 100% /mnt
[root@localhost ~]# rpm -ivh /mnt/Packages/vsftpd-3.0.2-21.el7.x86_64.rpm
warning: /mnt/Packages/vsftpd-3.0.2-21.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:vsftpd-3.0.2-21.el7 ################################# [100%]
安装ftp服务后,创建软件仓库目录/var/ftp/dvd,将镜像文件永久挂载到此目录下。
[root@localhost ~]# mkdir /var/ftp/dvd
[root@localhost ~]# vi /etc/fstab
[root@localhost ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Wed Feb 24 15:34:07 2021
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=e260d75c-d83a-45fe-b084-a5016087f284 / xfs defaults 0 0
UUID=bbce0f47-6db0-4d4a-a37f-025ed12a45ca /boot xfs defaults 0 0
UUID=c299518f-7ce3-430b-872d-7d30e5ec3b98 swap swap defaults 0 0
/dev/sr0 /var/ftp/dvd iso9660 defaults 0 0
[root@localhost ~]# mount /dev/sr0 /var/ftp/dvd
重新启动vsftpd服务,并设置开机启动vsftpd服务,再创建软件仓库的配置文件dvd.repo,指向前面新建的/var/ftp/dvd中存放的软件仓库。
安装kvm,通常需要三个包:qemu-kvm,libvirt和virt-manager。qemu-kvm主要提供kvm模拟器,libvirt提供相关的库文件,virt-manager提供虚拟机管理器。
[root@localhost ~]# yum -y install qemu-kvm libvirt virt-manager
Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
redhat | 4.1 kB 00:00:00
(1/2): redhat/group_gz | 136 kB 00:00:00
(2/2): redhat/primary_db | 3.9 MB 00:00:01
Package 10:qemu-kvm-1.5.3-126.el7.x86_64 already installed and latest version
Resolving Dependencies
--> Running transaction check
---> Package libvirt.x86_64 0:2.0.0-10.el7 will be installed
--> Processing Dependency: libvirt-daemon-config-nwfilter = 2.0.0-10.el7 for package: libvirt-2.0.0-10.el7.x86_64
--> Processing Dependency: libvirt-daemon-driver-lxc = 2.0.0-10.el7 for package: libvirt-2.0.0-10.el7.x86_64
---> Package virt-manager.noarch 0:1.4.0-2.el7 will be installed
--> Processing Dependency: virt-manager-common = 1.4.0-2.el7 for package: virt-manager-1.4.0-2.el7.noarch
--> Running transaction check
---> Package libvirt-daemon-config-nwfilter.x86_64 0:2.0.0-10.el7 will be installed
---> Package libvirt-daemon-driver-lxc.x86_64 0:2.0.0-10.el7 will be installed
---> Package virt-manager-common.noarch 0:1.4.0-2.el7 will be installed
--> Processing Dependency: libvirt-python >= 0.7.0 for package: virt-manager-common-1.4.0-2.el7.noarch
--> Processing Dependency: python-ipaddr for package: virt-manager-common-1.4.0-2.el7.noarch
--> Running transaction check
---> Package libvirt-python.x86_64 0:2.0.0-2.el7 will be installed
---> Package python-ipaddr.noarch 0:2.1.9-5.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
============================================================================================
Package Arch Version Repository Size
============================================================================================
Installing:
libvirt x86_64 2.0.0-10.el7 redhat 136 k
virt-manager noarch 1.4.0-2.el7 redhat 629 k
Installing for dependencies:
libvirt-daemon-config-nwfilter x86_64 2.0.0-10.el7 redhat 140 k
libvirt-daemon-driver-lxc x86_64 2.0.0-10.el7 redhat 801 k
libvirt-python x86_64 2.0.0-2.el7 redhat 321 k
python-ipaddr noarch 2.1.9-5.el7 redhat 36 k
virt-manager-common noarch 1.4.0-2.el7 redhat 1.1 M
Transaction Summary
============================================================================================
Install 2 Packages (+5 Dependent packages)
Total download size: 3.1 M
Installed size: 13 M
Downloading packages:
(1/7): libvirt-2.0.0-10.el7.x86_64.rpm | 136 kB 00:00:00
(2/7): libvirt-daemon-config-nwfilter-2.0.0-10.el7.x86_64.rpm | 140 kB 00:00:00
(3/7): libvirt-daemon-driver-lxc-2.0.0-10.el7.x86_64.rpm | 801 kB 00:00:00
(4/7): libvirt-python-2.0.0-2.el7.x86_64.rpm | 321 kB 00:00:00
(5/7): python-ipaddr-2.1.9-5.el7.noarch.rpm | 36 kB 00:00:00
(6/7): virt-manager-1.4.0-2.el7.noarch.rpm | 629 kB 00:00:00
(7/7): virt-manager-common-1.4.0-2.el7.noarch.rpm | 1.1 MB 00:00:00
--------------------------------------------------------------------------------------------
Total 5.1 MB/s | 3.1 MB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
Installing : libvirt-daemon-driver-lxc-2.0.0-10.el7.x86_64 1/7
Installing : python-ipaddr-2.1.9-5.el7.noarch 2/7
Installing : libvirt-python-2.0.0-2.el7.x86_64 3/7
Installing : virt-manager-common-1.4.0-2.el7.noarch 4/7
Installing : libvirt-daemon-config-nwfilter-2.0.0-10.el7.x86_64 5/7
Installing : libvirt-2.0.0-10.el7.x86_64 6/7
Installing : virt-manager-1.4.0-2.el7.noarch 7/7
redhat/productid | 1.6 kB 00:00:00
Verifying : libvirt-daemon-config-nwfilter-2.0.0-10.el7.x86_64 1/7
Verifying : libvirt-python-2.0.0-2.el7.x86_64 2/7
Verifying : libvirt-2.0.0-10.el7.x86_64 3/7
Verifying : virt-manager-1.4.0-2.el7.noarch 4/7
Verifying : python-ipaddr-2.1.9-5.el7.noarch 5/7
Verifying : virt-manager-common-1.4.0-2.el7.noarch 6/7
Verifying : libvirt-daemon-driver-lxc-2.0.0-10.el7.x86_64 7/7
Installed:
libvirt.x86_64 0:2.0.0-10.el7 virt-manager.noarch 0:1.4.0-2.el7
Dependency Installed:
libvirt-daemon-config-nwfilter.x86_64 0:2.0.0-10.el7
libvirt-daemon-driver-lxc.x86_64 0:2.0.0-10.el7
libvirt-python.x86_64 0:2.0.0-2.el7
python-ipaddr.noarch 0:2.1.9-5.el7
virt-manager-common.noarch 0:1.4.0-2.el7
Complete!
kvm需要的三个包安装完成后,在终端输入virt-manager,就可以打开图形界面对kvm进行管理。
[root@localhost ~]# virt-manager
在kvm里面新建虚拟机
在kvm里面跑一个操作系统,也需要新建一个虚拟机。
在虚拟机管理器中,选择新建命令。
进入新建虚拟机向导后,选择安装操作系统的方式,此处选择网络安装。
进入配置页面,找到以下图片信息进行调整。
添加完成后,选择开始安装。
使用virt-install新建虚拟机
如果没有virt-install命令,则需要先进行安装。
[root@localhost ~]# yum install -y virt-install.noarch
Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-
: manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
redhat | 4.1 kB 00:00
Resolving Dependencies
--> Running transaction check
---> Package virt-install.noarch 0:1.4.0-2.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
virt-install noarch 1.4.0-2.el7 redhat 88 k
Transaction Summary
================================================================================
Install 1 Package
Total download size: 88 k
Installed size: 86 k
Downloading packages:
virt-install-1.4.0-2.el7.noarch.rpm | 88 kB 00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : virt-install-1.4.0-2.el7.noarch 1/1
Verifying : virt-install-1.4.0-2.el7.noarch 1/1
Installed:
virt-install.noarch 0:1.4.0-2.el7
Complete!
然后使用virt-install命令完成虚拟机新建。
virt-install --name=vm01 --file=/var/lib/libvirt/images/vm01.dsk --file-size=8 --nonsparse --graphics spice --vcpus=1 --ram=1024 --location=ftp://192.168.217.139/dvd --network bridge=virbr0 --os-type=linux --os-variant=rhel7.3
[root@localhost ~]# virt-install --name=vm01 --file=/var/lib/libvirt/images/vm01.dsk --file-size=8 --nonsparse --graphics spice --vcpus=1 --ram=1024 --location=ftp://192.168.217.139/dvd --network bridge=virbr0 --os-type=linux --os-variant=rhel7.3
WARNING Unable to connect to graphical console: virt-viewer not installed. Please install the 'virt-viewer' package.
WARNING No console to launch for the guest, defaulting to --wait -1
Starting install...
Retrieving file vmlinuz... | 5.1 MB 00:00
Retrieving file initrd.img... | 43 MB 00:00
Allocating 'vm01.dsk' | 8.0 GB 00:00
Creating domain... | 0 B 00:03
Domain installation still in progress. Waiting for installation to complete.
创建完成即可在virt-manager中查看到新建的虚拟机,双击打开虚拟机则进入安装界面。
在kvm虚拟机中安装rhel7
安装rhel7-vm01虚拟机中的操作系统。
发现安装后,登录时无法输入内容,回头看一下虚拟机配置。
之前设置的没有保存应用,现在重新设置后,没有问题了。
在宿主机中通过ssh远程登录到rhel7-vm01。
注意:kvm在安装虚拟机时,一定要确保当前宿主机的磁盘空间足够,同时宿主机的虚拟化功能开启。