Centos 8 stream 部署 kvm

环境部署

网卡模式 NAT模式
ip 192.168.48.10
内存 4G
核心 4
硬盘 100G
功能 AMD-V

设置主机名

1
2
[root@localhost ~]# hostnamectl set-hostname KVM && bash
[root@KVM ~]#

设置网络

设置虚拟机网络(ens160是NAT模式)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
[root@KVM ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens160
BOOTPROTO=static
ONBOOT=yes
IPADDR=192.168.48.10
PREFIX=24
GATEWAY=192.168.48.2
DNS1=192.168.48.2
DOMAIN=114.114.114.114
[root@KVM ~]# nmcli c reload
[root@KVM ~]# nmcli c up ens160
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/4)
[root@KVM ~]# 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: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:ce:a9:48 brd ff:ff:ff:ff:ff:ff
altname enp3s0
inet 192.168.48.10/24 brd 192.168.48.255 scope global noprefixroute ens160
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fece:a948/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[root@KVM ~]# ping -c 4 jd.com
PING jd.com (111.13.149.108) 56(84) bytes of data.
64 bytes from 111.13.149.108 (111.13.149.108): icmp_seq=1 ttl=48 time=47.8 ms
64 bytes from 111.13.149.108 (111.13.149.108): icmp_seq=2 ttl=48 time=48.2 ms
64 bytes from 111.13.149.108 (111.13.149.108): icmp_seq=3 ttl=48 time=48.2 ms
64 bytes from 111.13.149.108 (111.13.149.108): icmp_seq=4 ttl=48 time=48.3 ms

--- jd.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 6077ms
rtt min/avg/max/mdev = 47.845/48.146/48.297/0.235 ms

检查虚拟机是否支持虚拟化

勾选cpu虚拟化(宿主机是amd芯片)

image-20230209225201606

查看虚拟化是否支持

1
2
3
[root@KVM ~]# lscpu
Virtualization: AMD-V
说明已支持虚拟化

设置阿里yum源

1
2
3
4
5
6
7
8
9
10
11
[root@KVM ~]# cp /etc/yum.repos.d/CentOS-Stream-BaseOS.repo /etc/yum.repos.d/CentOS-Stream-BaseOS.repo.bak
[root@KVM ~]# sed -i 's/mirrorlist/#mirrorlist/' /etc/yum.repos.d/CentOS-Stream-BaseOS.repo
[root@KVM ~]# sed -i 's/#baseurl=http:\/\/mirror.centos.org/baseurl=http:\/\/mirrors.aliyun.com/g' /etc/yum.repos.d/CentOS-Stream-BaseOS.repo
[root@KVM ~]# yum clean all && yum makecache
0 files removed
CentOS Stream 8 - AppStream 14 MB/s | 27 MB 00:01
CentOS Stream 8 - BaseOS 4.0 MB/s | 26 MB 00:06
CentOS Stream 8 - Extras 28 kB/s | 18 kB 00:00
CentOS Stream 8 - Extras common packages 8.2 kB/s | 5.2 kB 00:00
Metadata cache created.
[root@KVM ~]#

安装kvm

安装kvm及其工具

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@KVM ~]# yum install qemu-kvm qemu-img  virt-manager libvirt virt-manager libvirt-client virt-install virt-viewer -y
Waiting for process with pid 8674 to finish.
Last metadata expiration check: 0:00:05 ago on Sun 08 Jan 2023 06:03:34 AM EST.
Package qemu-kvm-15:6.2.0-20.module_el8.7.0+1218+f626c2ff.1.x86_64 is already installed.
Package qemu-img-15:6.2.0-20.module_el8.7.0+1218+f626c2ff.1.x86_64 is already installed.
Package virt-manager-3.2.0-4.el8.noarch is already installed.
Package libvirt-8.0.0-10.module_el8.7.0+1218+f626c2ff.x86_64 is already installed.
Package libvirt-client-8.0.0-10.module_el8.7.0+1218+f626c2ff.x86_64 is already installed.
Package virt-install-3.2.0-4.el8.noarch is already installed.
Package virt-viewer-9.0-11.el8.x86_64 is already installed.
Dependencies resolved.
Nothing to do.
Complete!

启动libvirtd服务

启动服务并设置韦开机自启动,查看状态

1
2
3
4
5
6
7
[root@KVM ~]# systemctl enable --now libvirtd
[root@KVM ~]# systemctl status libvirtd
● libvirtd.service - Virtualization daemon
Loaded: loaded (/usr/lib/systemd/system/libvirtd.service; enabled; vendor preset: enabled)
Active: active (running) since Sun 2023-01-08 06:05:12 EST; 12s ago
Docs: man:libvirtd(8)
https://libvirt.org

验证是否已加载kvm模块

1
2
3
4
5
[root@KVM ~]# lsmod | grep kvm
kvm_amd 143360 0
ccp 106496 1 kvm_amd
kvm 942080 1 kvm_amd
irqbypass 16384 1 kvm
1
2
3
4
[root@KVM ~]# virsh list
Id Name State
--------------------
说明kvm安装成功

设置宿主机网络(kvm这个机子)

查看网络情况

KVN软件安装后默认以NAT方式实现网络通信。为了让KVM虚拟机能够与宿主机、本地主机、互联网相互通信,需将宿主机(KVM这个机子)网络设置为bridge方式。

1
ip a

lo为回环接口,该接口不从外界接收和发送数据包,仅在操作系统内部接收和发送数据包

ens160是以太网接口,与网卡对应,每个硬件网卡对应一个以太网接口

virbr0为虚拟网络接口,由kvm创建,为连接其上的kvm虚拟机网络提供访问外部网络的功能

创建bridge

创建 bridge 时需使用nmcli命令创建 br0,并将其绑定到可以正常工作的网络接口上,同时让br0成为连接宿主机与互联网的接口。

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@KVM ~]# nmcli connection add type bridge con-name br0 ifname br0 autoconnect yes

Connection 'br0' (ac3429bc-907c-4ad1-bd54-bbf39d853a53) successfully added.
查看是否创建成功
[root@KVM ~]# nmcli c
NAME UUID TYPE DEVICE
br0 ac3429bc-907c-4ad1-bd54-bbf39d853a53 bridge br0
ens160 0d45e631-b256-4e08-b8d8-2c42b9481594 ethernet ens160
virbr0 7075d19e-a20c-43da-b161-e7c7519febdb bridge virbr0
网桥创建成功后会自动生成配置文件
[root@KVM ~]# ls -l /etc/sysconfig/network-scripts/
-rw-r--r--. 1 root root 312 Jan 8 06:20 ifcfg-br0
-rw-r--r--. 1 root root 365 Jan 8 05:40 ifcfg-ens160

设置br0和ens160网卡

将br0桥接到ens160

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@KVM ~]# vi /etc/sysconfig/network-scripts/ifcfg-br0
BOOTPROTO=static
IPADDR=192.168.48.10
GATEWAY=192.168.48.2
PREFIX=24
DNS=114.114.114.114
[root@KVM ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens160
BOOTPROTO=none
BRIDGE=br0
[root@KVM ~]# nmcli c reload
[root@KVM ~]# nmcli c drow br0r
此时如果你是用ssh工具连着的,你会断开,此时你要去vm那里输入
[root@KVM ~]# nmcli c up br0
[root@KVM ~]# nmcli c up ens160
然后等1分钟左右,就可以连上ssh工具了
1
2
3
4
5
6
7
8
9
10
测试网络连通性
[root@KVM ~]# ping -c 2 jd.com
PING jd.com (211.144.24.218) 56(84) bytes of data.
64 bytes from 211.144.24.218 (211.144.24.218): icmp_seq=1 ttl=128 time=50.7 ms
64 bytes from 211.144.24.218 (211.144.24.218): icmp_seq=2 ttl=128 time=51.9 ms

--- jd.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 50.683/51.286/51.890/0.644 ms
[root@KVM ~]#

创建kvm虚拟机

存储池 存放目录 内容规划
disk /opt/disk 存放KVM磁盘文件
存放iso /opt/iso 存放待安装的ISO文件

创建iso存储池

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
[root@KVM ~]# mkdir -p /opt/iso
[root@KVM ~]# chown root:root /opt/iso/
[root@KVM ~]# chmod 777 /opt/iso/
[root@KVM ~]# virsh pool-define-as iso --type dir --target /opt/iso/
Pool iso--type defined
#名称为iso的存储池定义成功
如果名字打错 用
virsh pool-destroy [名字]
virsh pool-undefine [名字] 删除
[root@KVM ~]# virsh pool-list --all
Name State Autostart
------------------------------
iso inactive no

[root@KVM ~]# virsh pool-build iso
Pool iso built
#创建名为iso的存储池
[root@KVM ~]# virsh pool-start iso
Pool iso started
#启动iso存储池
[root@KVM ~]# virsh pool-autostart iso
Pool iso marked as autostarted
#设置iso存储池自启动
查看iso信息
[root@KVM ~]# virsh pool-info iso
Name: iso
UUID: a966995e-2722-4a3f-a318-e158a642439e
State: running
Persistent: yes
Autostart: yes
Capacity: 63.84 GiB
Allocation: 2.75 GiB
Available: 61.09 GiB

创建disk存储池

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[root@KVM ~]# mkdir -p /opt/disk
[root@KVM ~]# chown root:root /opt/disk/
[root@KVM ~]# chmod 777 /opt/disk/
[root@KVM ~]# virsh pool-define-as disk --type dir --target /opt/disk/
Pool disk defined

[root@KVM ~]# virsh pool-build disk
Pool disk built

[root@KVM ~]# virsh pool-start disk
Pool disk started

[root@KVM ~]# virsh pool-autostart disk
Pool disk marked as autostarted

[root@KVM ~]# virsh pool-info disk
Name: disk
UUID: 30d784dc-386b-46e6-a22b-79a3b3447354
State: running
Persistent: yes
Autostart: yes
Capacity: 63.84 GiB
Allocation: 2.75 GiB
Available: 61.09 GiB

获取Centos7

下载Centos7最小化版本到ISO目录下(网络方式)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@KVM ~]# yum install -y wget
[root@KVM ~]# wget -O /opt/iso/Centos7.iso https://mirrors.aliyun.com/centos/7.9.2009/isos/x86_64/CentOS-7-x86_64-Minimal-2009.iso
--2023-01-08 08:09:00-- https://mirrors.aliyun.com/centos/7.9.2009/isos/x86_64/CentOS-7-x86_64-Minimal-2009.iso
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 120.241.238.243, 120.241.238.242, 120.241.238.241, ...
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|120.241.238.243|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1020264448 (973M) [application/x-cd-image]
Saving to: ‘/opt/iso/Centos7.iso’

/opt/iso/Centos7.iso 100%[================================>] 973.00M 1.53MB/s in 10m 54s

2023-01-08 08:19:54 (1.49 MB/s) - ‘/opt/iso/Centos7.iso’ saved [1020264448/1020264448]
[root@KVM ~]# ll /opt/iso
total 996352
-rw-r--r--. 1 root root 1020264448 Nov 3 2020 Centos7.iso

也可以本地下载好后,上传至iso目录(本地方式)

下载连接:https://mirrors.aliyun.com/centos/7.9.2009/isos/x86_64/CentOS-7-x86_64-Minimal-2009.iso

1
2
3
[root@KVM ~]# ll /opt/iso
total 996352
-rw-r--r--. 1 root root 1020264448 Nov 3 2020 Centos7.iso

安装Centos7

无界面安装Centos7

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
[root@KVM ~]# virt-install --virt-type=kvm --name=Centos7 --vcpus=1 --memory=2048 --network bridge=br0,model=virtio --os-type=linux --os-variant=rhel7.7 --location=/opt/iso/Centos7.iso --disk /opt/disk/Centos7.qcow2,format=qcow2,size=10 --console=pty,target_type=serial --graphics=none --extra-args="console=tty0 console=ttyS0"

Starting install...
Retrieving file vmlinuz... | 6.5 MB 00:00:00
Retrieving file initrd.img... | 53 MB 00:00:00
Allocating 'Centos7.qcow2' | 10 GB 00:00:00
Running text console command: virsh --connect qemu:///system console Centos7
Connected to domain 'Centos7'
......
进入安装界面

Connected to domain 'Centos7'
Please make your choice from above ['q' to quit | 'b' to begin installation |
'r' to refresh]: Please make your choice from above ['q' to quit | 'b' to begin installation |
================================================================================
================================================================================
Installation

1) [x] Language settings 2) [!] Time settings
(English (United States)) (Timezone is not set.)
3) [x] Installation source 4) [x] Software selection
(Local media) (Minimal Install)
5) [!] Installation Destination 6) [x] Kdump
(No disks selected) (Kdump is enabled)
7) [ ] Network configuration 8) [!] Root password
(Not connected) (Password is not set.)
9) [!] User creation
(No user will be created)
Please make your choice from above ['q' to quit | 'b' to begin installation |
'r' to refresh]: Please make your choice from above ['q' to quit | 'b' to begin installation |

输入2设置时区

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
================================================================================
================================================================================
Time settings

Timezone: not set

NTP servers:not configured

1) Set timezone
2) Configure NTP servers
Please make your choice from above ['q' to quit | 'c' to continue |
'r' to refresh]:
================================================================================
================================================================================
按1根据提示选择Asia/shanghai 然后按c

输入5设置磁盘(Installation Destination )

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
Probing storage...
Installation Destination

[x] 1) : 10 GiB (vda)

1 disk selected; 10 GiB capacity; 10 GiB free ...

Please make your choice from above ['q' to quit | 'c' to continue |
'r' to refresh]:
Autopartitioning Options
再按c
================================================================================
[ ] 1) Replace Existing Linux system(s)

[x] 2) Use All Space

[ ] 3) Use Free Space

Installation requires partitioning of your hard drive. Select what space to use
for the install target.

Please make your choice from above ['q' to quit | 'c' to continue |
再按c
================================================================================
Partition Scheme Options

[ ] 1) Standard Partition

[ ] 2) Btrfs

[x] 3) LVM

[ ] 4) LVM Thin Provisioning

Select a partition scheme configuration.

Please make your choice from above ['q' to quit | 'c' to continue |
'r' to refresh]: c
Generating updated storage configuration
Checking storage configuration...

按8设置root密码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  'r' to refresh]: 8
================================================================================
(Not connected)
9) [!] User creation
================================================================================
Please select new root password. You will have to type it twice.

Password:
Password (confirm):
================================================================================
================================================================================
Question

The password you have provided is weak: The password fails the dictionary check
- it is too simplistic/systematic.
Would you like to use it anyway?

Please respond 'yes' or 'no': yes

全部设置完成后按b确认

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  'r' to refresh]: b
================================================================================
================================================================================
Progress
Setting up the installation environment
.
Creating disklabel on /dev/vda
.
Creating xfs on /dev/vda1
.
Creating lvmpv on /dev/vda2
.
Creating swap on /dev/mapper/centos-swap
.
Creating xfs on /dev/mapper/centos-root
.
Running pre-installation scripts
.
Starting package installation process
就会开始安装

进入登入界面

1
2
3
4
5
CentOS Linux 7 (Core)
Kernel 3.10.0-1160.el7.x86_64 on an x86_64

localhost login:

管理kvm虚拟机

连接KVM虚拟机

1
2
3
4
5
6
7
8
9
10
11
12
[root@KVM ~]# virsh console Centos7
Connected to domain 'Centos7'
Escape character is ^] (Ctrl + ])
error: operation failed: Active console session exists for this domain
如果你之前连接过,没有退出,但是你与宿主机的ssh断了,就会出现这种问题,也就是你之前的连接控制台的连接还存在;
每次连接kvm之后要退出 ctrl+] 就可以退出
解决办法:
[root@KVM ~]# ps -ef |grep virsh
root 65402 61750 0 02:26 pts/0 00:00:00 virsh --connect qemu:///system console Centos7
root 68540 66569 0 02:35 pts/2 00:00:00 grep --color=auto virsh
[root@KVM ~]# kill -9 61750
[root@KVM ~]# kill -9 65402

查看KVM虚拟机状态

1
2
3
4
[root@KVM ~]# virsh list
Id Name State
-------------------------
2 Centos7 running

连接虚拟机

1
2
3
4
5
[root@KVM ~]# virsh console Centos7
Connected to domain 'Centos7'
Escape character is ^] (Ctrl + ])

[root@localhost ~]#

初始化KVM虚拟机

设置名字

1
2
[root@localhost ~]# hostnamectl set-hostname Centos7 && bash
[root@centos7 ~]#

设置网络

查看状态
1
2
3
4
5
6
7
8
9
[root@centos7 ~]# 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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 52:54:00:b2:b9:d1 brd ff:ff:ff:ff:ff:ff
编辑配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@centos7 ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static ---
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=eth0
UUID=b7053508-5183-4ee4-8af5-8d5241f57116
DEVICE=eth0
ONBOOT=yes ---
IPADDR=192.168.48.11 ---
PREFIX=24 ---
GATEWAY=192.168.48.2 ---
DNS1=114.114.114.114 ---
查看KVM网络信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@centos7 ~]# systemctl restart network
[root@centos7 ~]# 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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 52:54:00:b2:b9:d1 brd ff:ff:ff:ff:ff:ff
inet 192.168.48.11/24 brd 192.168.48.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet6 fe80::85d6:dde0:25ad:9c43/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[root@centos7 ~]#

测试一下ssh工具是不是也可以连(cmd、MobaXterm)

测试连通性
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
ping KVM机子
[root@centos7 ~]# ping 192.168.48.10 -c 2
PING 192.168.48.10 (192.168.48.10) 56(84) bytes of data.
64 bytes from 192.168.48.10: icmp_seq=1 ttl=64 time=0.481 ms
64 bytes from 192.168.48.10: icmp_seq=2 ttl=64 time=0.254 ms

--- 192.168.48.10 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.254/0.367/0.481/0.115 ms
ping 百度
[root@centos7 ~]# ping baidu.com -c 2
PING baidu.com (39.156.66.10) 56(84) bytes of data.
64 bytes from 39.156.66.10 (39.156.66.10): icmp_seq=1 ttl=128 time=43.5 ms
64 bytes from 39.156.66.10 (39.156.66.10): icmp_seq=2 ttl=128 time=44.9 ms

--- baidu.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 43.599/44.258/44.917/0.659 ms
[root@centos7 ~]#

设置Centos7虚拟机随KVM宿主机开机自启动

1
2
3
4
5
6
[root@KVM ~]# virsh autostart Centos7
Domain 'Centos7' marked as autostarted

开机自启动配置后,会在/etc/libvirt/qemu/autostart/目录中增加XML格式
[root@KVM ~]# ls /etc/libvirt/qemu/autostart/
Centos7.xml
1
2
3
virsh autostart --disable 关闭开机自启动
[root@KVM ~]# virsh autostart --disable Centos7
Domain 'Centos7' unmarked as autostarted

为KVM虚拟机增加CPU

查看Centos7配置信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@KVM ~]# virsh dominfo Centos7
Id: 1
Name: Centos7
UUID: 735f1ca1-c82a-4734-81ec-ea3e82e98bae
OS Type: hvm
State: running
CPU(s): 1
CPU time: 17.3s
Max memory: 2097152 KiB
Used memory: 2097152 KiB
Persistent: yes
Autostart: enable
Managed save: no
Security model: selinux
Security DOI: 0
Security label: system_u:system_r:svirt_t:s0:c202,c740 (enforcing)

关闭虚拟机

1
2
3
4
5
6
[root@KVM ~]# virsh shutdown Centos7
Domain 'Centos7' is being shutdown
[root@KVM ~]# virsh list --all
Id Name State
--------------------------
- Centos7 shut off

修改Centos7 CPU核心数量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@KVM ~]# vi /etc/libvirt/qemu/Centos7.xml
<domain type='kvm'>
<name>Centos7</name>
<uuid>735f1ca1-c82a-4734-81ec-ea3e82e98bae</uuid>
<metadata>
<libosinfo:libosinfo xmlns:libosinfo="http://libosinfo.org/xmlns/libvirt/domain/1.0">
<libosinfo:os id="http://redhat.com/rhel/7.7"/>
</libosinfo:libosinfo>
</metadata>
<memory unit='KiB'>2097152</memory>
<currentMemory unit='KiB'>2097152</currentMemory>
<vcpu placement='static'>2</vcpu> 修改为2
<os>
<type arch='x86_64' machine='pc-q35-rhel8.6.0'>hvm</type>

启动虚拟机并查看配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@KVM ~]# virsh start Centos7
Domain 'Centos7' started

[root@KVM ~]# virsh dominfo Centos7
Id: 1
Name: Centos7
UUID: 735f1ca1-c82a-4734-81ec-ea3e82e98bae
OS Type: hvm
State: running
CPU(s): 2
CPU time: 13.4s
Max memory: 2097152 KiB
Used memory: 2097152 KiB
Persistent: yes
Autostart: enable
Managed save: no
Security model: selinux
Security DOI: 0
Security label: system_u:system_r:svirt_t:s0:c414,c924 (enforcing)

[root@KVM ~]#

维护虚拟机

挂起/恢复虚拟机

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@KVM ~]# virsh suspend Centos7
Domain 'Centos7' suspended
[root@KVM ~]# virsh list --all
Id Name State
------------------------
1 Centos7 paused
[root@KVM ~]# virsh resume Centos7
Domain 'Centos7' resumed

[root@KVM ~]# virsh list --all
Id Name State
-------------------------
1 Centos7 running

克隆KVM虚拟机

关闭Centos7 并且克隆Centos7为C7

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@KVM ~]# virsh shutdown Centos7
Domain 'Centos7' is being shutdown

[root@KVM ~]# virsh list --all
Id Name State
--------------------------
- Centos7 shut off

[root@KVM ~]# virt-clone -o Centos7 -n C7 -f /opt/disk/C7.qcow2
Allocating 'C7.qcow2' | 10 GB 00:00:37

Clone 'C7' created successfully.
克隆成功
[root@KVM ~]# virsh list --all
Id Name State
--------------------------
- C7 shut off
- Centos7 shut off

开启C7,登入并且修改主机名为C7

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@KVM ~]# virsh start C7
Domain 'C7' started

[root@KVM ~]# virsh console C7
Connected to domain 'C7'
Escape character is ^] (Ctrl + ])
CentOS Linux 7 (Core)
Kernel 3.10.0-1160.el7.x86_64 on an x86_64

centos7 login: root
Password:
Last login: Mon Jan 9 15:56:16 from 192.168.48.250
[root@centos7 ~]# hostnamectl set-hostname C7 && bash
[root@c7 ~]#
克隆后ip回合Centos7一样,记得后期修改

设置Kvm虚拟机快照

登入C7 创建/opt/dev ,关闭虚拟机,创建快照

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@c7 ~]# mkdir /opt/dev
[root@c7 ~]# ls /opt
dev
按ctrl + ] 退回到KVM宿主机
[root@KVM ~]# virsh shutdown C7
Domain 'C7' is being shutdown
创建快照
[root@KVM ~]# virsh snapshot-create C7
Domain snapshot 1673252946 created
查看C7的快照列表
[root@KVM ~]# virsh snapshot-list C7
Name Creation Time State
---------------------------------------------------
1673252946 2023-01-09 03:29:06 -0500 shutoff

开启C7 删除/opt/dev ,恢复快照

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@KVM ~]# virsh start C7
Domain 'C7' started

[root@KVM ~]# virsh console C7
Connected to domain 'C7'

[root@c7 ~]# rm -rf /opt/dev
[root@c7 ~]# ll /opt
total 0
按ctrl + ] 退回到KVM宿主机
[root@KVM ~]# virsh snapshot-list C7
Name Creation Time State
---------------------------------------------------
1673252946 2023-01-09 03:29:06 -0500 shutoff

[root@KVM ~]# virsh snapshot-revert C7 1673252946
virsh snapshot-revert 虚拟机名字 快照名字

开启C7,查看是否恢复成功

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@KVM ~]# virsh start C7
Domain 'C7' started

[root@KVM ~]# virsh console C7
Connected to domain 'C7'
Escape character is ^] (Ctrl + ])

CentOS Linux 7 (Core)
Kernel 3.10.0-1160.el7.x86_64 on an x86_64

c7 login: root
Password:
Last login: Mon Jan 9 16:27:07 on ttyS0
[root@c7 ~]# ls /opt
dev

virsh 命令

1
2
3
4
5
6
virsh shutdown 关闭KVM虚拟机 
virsh destroy 强制关闭KVM虚拟机,不删除虚拟机磁盘,virsh list列表里面看不见,但是磁盘还在,还是可以启动,通过virsh list --all可以查看
virsh undefine 彻底删除虚拟机,包括虚拟机存储所在的位置
virsh start 开启KVM虚拟机
virsh suspend 虚拟机名称 #挂起
virsh resume 虚拟机名称 #恢复被挂起的
特别声明
千屹博客旗下的所有文章,是通过本人课堂学习和课外自学所精心整理的知识巨著
难免会有出错的地方
如果细心的你发现了小失误,可以在下方评论区告诉我,或者私信我!
非常感谢大家的热烈支持!