Linux 虚拟化-0001-安装 KVM

​目录

0、准备步骤

检查处理器 CPU 是否支持硬件虚拟化 hardware virtualization。

# grep -E (vmx|svm) /proc/cpuinfo flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch epb cat_l3 cdp_l3 invpcid_single intel_ppin intel_pt ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm cqm rdt_a rdseed adx smap xsaveopt cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts md_clear spec_ctrl intel_stibp flush_l1d

输出结果中要么有 vmx 要么有 svm。如果都没有那就是处理器 CPU 不支持虚拟化。

1、安装步骤

# 1.安装 KVM 及其关联包 # yum install -y qemu-kvm qumu-img virt-manager \ libvirt libvirt-python libvirt-client \ virt-install virt-viewer bridge-utils ... Downloading packages: (1/15): gnutls-dane-3.3.29-9.el7_6.x86_64.rpm|36 kB 00:00 (2/15): libvirt-daemon-config-nwfilter-4.5.0-36.el7_9.5.x8 | 211 kB 00:00 (3/15): libvirt-daemon-driver-lxc-4.5.0-36.el7_9.5.x86_64. | 336 kB 00:00 (4/15): gnutls-utils-3.3.29-9.el7_6.x86_64.rpm | 238 kB 00:00 (5/15): libvirt-python-4.5.0-1.el7.x86_64.rpm| 343 kB 00:00 (6/15): python-ipaddr-2.1.11-2.el7.noarch.rpm|35 kB 00:00 (7/15): python-requests-2.6.0-10.el7.noarch.rpm|95 kB 00:00 (8/15): libvirt-4.5.0-36.el7_9.5.x86_64.rpm| 204 kB 00:00 (9/15): python-urllib3-1.10.2-7.el7.noarch.rpm | 103 kB 00:00 (10/15): virt-install-1.5.0-7.el7.noarch.rpm |97 kB 00:00 (11/15): libvirt-client-4.5.0-36.el7_9.5.x86_64.rpm| 501 kB 00:00 (12/15): libvirt-bash-completion-4.5.0-36.el7_9.5.x86_64.r | 204 kB 00:00 (13/15): virt-viewer-5.0-18.el7.x86_64.rpm | 395 kB 00:00 (14/15): virt-manager-common-1.5.0-7.el7.noarch.rpm| 1.2 MB 00:00 (15/15): virt-manager-1.5.0-7.el7.noarch.rpm | 661 kB 00:00 -------------------------------------------------------------------------------- 总计 3.0 MB/s | 4.6 MB00:01 ...# 2.启动服务并设置服务开机自动启动 # systemctl start libvirtd # 查看服务状态 # systemctl status libvirtd ● libvirtd.service - Virtualization daemon Loaded: loaded (/usr/lib/systemd/system/libvirtd.service; enabled; vendor preset: enabled) Active: active (running) since 六 2021-10-23 13:16:37 CST; 3min 59s ago Docs: man:libvirtd(8) Main PID: 3981 (libvirtd) Tasks: 19 (limit: 32768) Memory: 11.3M CGroup: /system.slice/libvirtd.service ├─2512 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/defa... ├─2513 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/defa... └─3981 /usr/sbin/libvirtd 10月 23 13:16:37 home systemd[1]: Starting Virtualization daemon... 10月 23 13:16:37 home systemd[1]: Started Virtualization daemon. 10月 23 13:16:37 home dnsmasq[2512]: read /etc/hosts - 2 addresses 10月 23 13:16:37 home dnsmasq[2512]: read /var/lib/libvirt/dnsmasq/default...es 10月 23 13:16:37 home dnsmasq-dhcp[2512]: read /var/lib/libvirt/dnsmasq/def...e Hint: Some lines were ellipsized, use -l to show in full. # 设置服务开机自动启动 # systemctl enable libvirtd# 3.检查 KVM 模块是否加载 # lsmod | grep kvm kvm_intel 0 kvm 1 kvm_intel irqbypass135031 kvm# 4.启动 Virt Manager # virt-manager (virt-manager:4278): Gtk-WARNING **: 13:25:22.380: cannot open display: ^C# 安装 x-windows 包 # yum install -y "@X Window System" \ xorg-x11-xauth xorg-x11-fonts-* xorg-x11-utils ... Downloading packages: (1/15): xorg-x11-fonts-ISO8859-1-75dpi-7.5-9.el7.noarch.rp | 933 kB 00:01 (2/15): xorg-x11-fonts-ISO8859-1-100dpi-7.5-9.el7.noarch.r | 1.1 MB 00:01 (3/15): xorg-x11-fonts-ISO8859-14-75dpi-7.5-9.el7.noarch.r | 906 kB 00:00 (4/15): xorg-x11-fonts-ISO8859-14-100dpi-7.5-9.el7.noarch. | 1.0 MB 00:02 (5/15): xorg-x11-fonts-ISO8859-15-75dpi-7.5-9.el7.noarch.r | 932 kB 00:00 (6/15): xorg-x11-fonts-ISO8859-2-75dpi-7.5-9.el7.noarch.rp | 901 kB 00:01 (7/15): xorg-x11-fonts-ISO8859-2-100dpi-7.5-9.el7.noarch.r | 1.0 MB 00:02 (8/15): xorg-x11-fonts-ISO8859-9-100dpi-7.5-9.el7.noarch.r | 1.1 MB 00:00 (9/15): xorg-x11-fonts-cyrillic-7.5-9.el7.noarch.rpm | 397 kB 00:00 (10/15): xorg-x11-fonts-ethiopic-7.5-9.el7.noarch.rpm| 151 kB 00:00 (11/15): xorg-x11-fonts-ISO8859-9-75dpi-7.5-9.el7.noarch.r | 930 kB 00:02 (12/15): xorg-x11-fonts-misc-7.5-9.el7.noarch.rpm| 5.8 MB 00:02 (13/15): xorg-x11-fonts-75dpi-7.5-9.el7.noarch.rpm | 2.8 MB 00:08 (14/15): xorg-x11-fonts-100dpi-7.5-9.el7.noarch.rpm| 3.1 MB 00:09 (15/15): xorg-x11-fonts-ISO8859-15-100dpi-7.5-9.el7.noarch | 1.1 MB 00:20 -------------------------------------------------------------------------------- 总计 997 kB/s |22 MB00:22 ...# 4.启动 virt manager # ssh 远程 home 时加上 -Y 参数 (base) [email protected]:~ $ ssh -Y root@home 130 ↵ Warning: No xauth data; using fake authentication data for X11 forwarding. Last login: Sat Oct 23 14:20:53 2021 from 192.168.1.6 [root@home ~]# virt-manager [root@home ~]# libGL error: No matching fbConfigs or visuals found libGL error: failed to load driver: swrast# 在输入 virt-manager 后会在本机启动一个窗体,如下图所示本机启动的“虚拟系统管理器”,显示的是 home 主机上的 QEMU/KVM# 5.配置桥接网卡 [root@home ~]# cd /etc/sysconfig/network-scripts/ [root@home network-scripts]# ls ifcfg-eno1 ifdown-postifup-eth ifup-sit ifcfg-eno2 ifdown-ppp ifup-ibifup-Team ifcfg-lo ifdown-routesifup-ipppifup-TeamPort ifdown ifdown-sit ifup-ipv6ifup-tunnel ifdown-bnepifdown-Teamifup-isdnifup-wireless ifdown-eth ifdown-TeamPortifup-plipinit.ipv6-global ifdown-ibifdown-tunnelifup-plusb network-functions ifdown-ipppifup ifup-postnetwork-functions-ipv6 ifdown-ipv6ifup-aliases ifup-ppp ifdown-isdnifup-bnepifup-routes # 将第一块网卡的配置复制为 br0 [root@home network-scripts]# cp ifcfg-eno1 ifcfg-br0 # 编辑第一块网卡的配置 [root@home network-scripts]# nano ifcfg-eno1 [root@home network-scripts]# cat ifcfg-eno1 TYPE=Ethernet BOOTPROTO=static DEVICE=eno1 ONBOOT=yes BRIDGE=br0 [root@home network-scripts]#[root@home network-scripts]# nano ifcfg-br0 [root@home network-scripts]# systemctl restart NetworkManager [root@home network-scripts]# cat ifcfg-br0 TYPE=bridge BOOTPROTO=static DEVICE=br0 ONBOOT=yes IPADDR=192.168.1.100 NETMASK=255.255.255.0 GATEWAY=192.168.1.1 DNS1=192.168.1.1[root@home network-scripts]# ip addr show br0 7: br0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000 link/ether ca:d3:d0:aa:32:53 brd ff:ff:ff:ff:ff:ff inet 192.168.1.100/24 brd 192.168.1.255 scope global noprefixroute br0 valid_lft forever preferred_lft forever

2、创建虚机

参考文章“Linux 虚拟化-创建虚拟机”