KVM虚拟化相关

1、图形管理

1.1 KVM图形管理工具的安装

1
2
yum install virt-manager -y
yum install libvirt-client -y

1.2 上传ios镜像

1
scp CentOS-Stream-8-x86_64-latest-dvd1.iso root@192.168.100.19:~

1.3 打开KVM的图形管理界面

1
2
#保证libvirtd是开启的状态
systemctl enable libvirtd --now

image-20220804213736505

1.4 命令行删除默认的虚拟化网桥

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@KVM ~]# virsh net-list
Name State Autostart Persistent
--------------------------------------------
default active yes yes

[root@KVM ~]# virsh net-destroy default
Network default destroyed

[root@KVM ~]# virsh net-list --all
Name State Autostart Persistent
----------------------------------------------
default inactive yes yes

[root@KVM ~]# virsh net-undefine default
Network default has been undefined

#已经彻底删除了,此方法清理linux bridge属于是虚拟化层面上的清理,对于虚拟机来说可以使用libvirt提供的网桥,也可以使用NetworkManager蹄冻的网桥,看你更擅长哪一种方式,更推荐使用NetworkManager的网桥管理方式
[root@KVM ~]# virsh net-list --all
Name State Autostart Persistent
----------------------------------------

1.5 通过nmcli 命令行创建linux bridge

1
[root@KVM ~]# nmcli connection add con-name vm-bridge ifname vm-bridge type bridge ipv4.method disabled ipv6.method ignore

1.6 关闭selinux与防火墙

1
2
3
[root@KVM ~]# sed -i "s/^SELINUX=.*/SELINUX=disabled/" /etc/selinux/config
[root@KVM ~]# setenforce 0
[root@KVM ~]# systemctl disable firewalld --now

2、命令行管理

2.1 网桥的创建

1
[root@KVM ~]# nmcli connection add con-name vm-bridge ifname vm-bridge type bridge ipv4.method disabled ipv6.method ignore

2.2 使用cloud镜像

KVM虚拟化创建的虚拟机,虚拟磁盘的后缀名叫做qcow2,当创建了一个虚拟机后,将会生成一个qcow2的虚拟磁盘文件,里面就有你安装的操作系统。但是不能每次都安装系统,就出现了一个模板化布置虚拟机的方法。如何获取一个qcow2的文件作为模板?

主流的linux发行版本官方都会提供qcow2模板文件

centoshttps://cloud.centos.org/

1
wget https://cloud.centos.org/centos/8-stream/x86_64/images/CentOS-Stream-GenericCloud-8-20220125.1.x86_64.qcow2

2.3 创建虚拟机

通过cloud image使用命令行创建虚拟机,一般不建议将qcow2文件放到/tmp/下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@KVM ~]# mv CentOS-Stream-GenericCloud-8-20220125.1.x86_64.qcow2 /tmp/

[root@KVM ~]# yum install virt-install -y


[root@KVM ~]# virt-install --name centos-stream8-1 --memory 1024 --vcpus 1 --import \
--os-variant centos-stream8 \
--disk path=/tmp/CentOS-Stream-GenericCloud-8-20220125.1.x86_64.qcow2,bus=virtio \
--network bridge=vm-bridge,model=virtio --noautoconsole


[root@KVM ~]# virsh list
Id Name State
----------------------------------
1 centos-stream8-1 running

2.4 进入虚拟机的控制台

1
2
#使用Ctrl + ]可以退出控制台 
[root@KVM ~]# virsh console centos-stream8-1

2.5 删除虚拟机

1
2
3
4
5
[root@KVM ~]# virsh destroy centos-stream8-1 
Domain 'centos-stream8-1' destroyed

[root@KVM ~]# virsh undefine centos-stream8-1
Domain 'centos-stream8-1' has been undefined

2.6 自定义cloud image

qcow2文件是可以修改的,需要工具,这个工具的名字叫做libguestfs

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 ~]# yum install libguestfs-tools -y

[root@KVM ~]# virt-customize -a /tmp/CentOS-Stream-GenericCloud-8-20220125.1.x86_64.qcow2 \
--root-password password:1 --selinux-relabel
[ 0.0] Examining the guest ...
[ 7.8] Setting a random seed
[ 7.8] Setting passwords
[ 9.3] SELinux relabelling
[ 17.3] Finishing off


#再次创建虚拟机,进入虚拟机
[root@KVM ~]# virsh console centos-stream8-1
Connected to domain 'centos-stream8-1'
Escape character is ^] (Ctrl + ])

CentOS Stream 8
Kernel 4.18.0-358.el8.x86_64 on an x86_64

Activate the web console with: systemctl enable --now cockpit.socket

localhost login: root
Password: 1
Last login: Thu Aug 4 15:40:25 on ttyS0

2.7 配置虚拟机的ip地址

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
#虚拟机配置地址
nmcli con add con-name eth0-static ifname eth0 type ethernet ipv4.method manual ipv4.addresses \ 172.16.33.11/24


#物理机上
[root@KVM ~]# bridge link
8: vnet0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 master vm-bridge state forwarding priority 32 cost 100

#给vm-bridge配置地址
[root@KVM ~]# nmcli connection modify vm-bridge ipv4.addresses 172.16.33.100/24 ipv4.method manual
[root@KVM ~]# nmcli connection up vm-bridge

#当做完操作后会发现网桥找不到了,此时重启虚拟机可以解决(是由于网桥很脆弱,修改之后会断)

[root@KVM ~]# virsh destroy centos-stream8-1
Domain 'centos-stream8-1' destroyed

[root@KVM ~]# virsh start centos-stream8-1
Domain 'centos-stream8-1' started

[root@KVM ~]# bridge link
9: vnet1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 master vm-bridge state listening priority 32 cost 100


#可以进行ssh连接虚拟机了
[root@KVM ~]# ssh root@172.16.33.11
root@172.16.33.11's password:
Activate the web console with: systemctl enable --now cockpit.socket

Last login: Thu Aug 4 16:01:03 2022 from 172.16.33.100

2.8 重新拉起虚拟机配置额外的硬盘

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@KVM ~]# virsh destroy centos-stream8-1 
Domain 'centos-stream8-1' destroyed

[root@KVM ~]# virsh undefine centos-stream8-1
Domain 'centos-stream8-1' has been undefined


#物理机上创建卷组
[root@KVM ~]# pvcreate /dev/sdd
Physical volume "/dev/sdd" successfully created.
[root@KVM ~]# vgcreate data /dev/sdd
Volume group "data" successfully created
[root@KVM ~]# lvcreate -n data1 -L 120G data
Logical volume "data1" created.


#添加的硬盘可以是逻辑卷,也可以是裸盘
virt-install --name centos-stream8-1 --memory 1024 --vcpus 1 --import \
--os-variant centos-stream8 \
--disk path=/tmp/CentOS-Stream-GenericCloud-8-20220125.1.x86_64.qcow2,bus=virtio \
--disk path=/dev/data/data1,bus=virtio \
--disk path=/dev/sdb,bus=virtio \
--network bridge=vm-bridge,model=virtio --noautoconsole

2.9 让虚拟机中的”/“目录变大

因为虚拟机的根分区是标准分区,所以没有办法使用逻辑卷扩容的方法进行扩容,需要借助一个工具叫做virt-resize

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
41
42
43
44
45
46
47
48
49
50
51
#查看qcow2镜像信息
[root@KVM ~]# qemu-img info /tmp/CentOS-Stream-GenericCloud-8-20220125.1.x86_64.qcow2
image: /tmp/CentOS-Stream-GenericCloud-8-20220125.1.x86_64.qcow2
file format: qcow2
virtual size: 10 GiB (10737418240 bytes)
disk size: 1.48 GiB
cluster_size: 65536
Format specific information:
compat: 0.10
compression type: zlib
refcount bits: 16


#使用virt-filesystems命令查看qcow2文件的分区情况
[root@KVM ~]# virt-filesystems --long -h --all -a /tmp/CentOS-Stream-GenericCloud-8-20220125.1.x86_64.qcow2
Name Type VFS Label MBR Size Parent
/dev/sda1 filesystem xfs - - 7.8G -
/dev/sda1 partition - - 83 7.8G /dev/sda
/dev/sda device - - - 10G -


#创建一个100G的qcow2文件
[root@KVM ~]# qemu-img create -f qcow2 /tmp/os.qcow2 100G

[root@KVM tmp]# virt-resize --expand /dev/sda1 \
/tmp/CentOS-Stream-GenericCloud-8-20220125.1.x86_64.qcow2 /tmp/os.qcow2

[root@KVM tmp]# virt-filesystems --long -h --all -a /tmp/os.qcow2
Name Type VFS Label MBR Size Parent
/dev/sda1 filesystem xfs - - 100G -
/dev/sda1 partition - - 83 100G /dev/sda
/dev/sda device - - - 100G -


#再次创建虚拟机
virt-install --name centos-stream8-1 --memory 1024 --vcpus 1 --import \
--os-variant centos-stream8 \
--disk path=/tmp/os.qcow2,bus=virtio \
--disk path=/dev/data/data1,bus=virtio \
--disk path=/dev/sdb,bus=virtio \
--network bridge=vm-bridge,model=virtio --noautoconsole

#进入虚拟机查看磁盘的大小
[root@localhost ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 368M 0 368M 0% /dev
tmpfs tmpfs 403M 0 403M 0% /dev/shm
tmpfs tmpfs 403M 11M 392M 3% /run
tmpfs tmpfs 403M 0 403M 0% /sys/fs/cgroup
/dev/vda1 xfs 100G 2.1G 98G 3% /
tmpfs tmpfs 81M 0 81M 0% /run/user/0

3、eve-ng中虚拟机设置快照

1
2
3
4
5
6
/opt/unetlab/tmp/0/

qemu-img snapshot -c snapshot01 virtoia.qcow2 创建快照
qemu-img snapshot -l virtoia.qcow2 列出快照
qemu-img snapshot -a snapshot01 virtoia.qcow2 恢复快照
qemu-img snapshot -d tomcat01.snap hda.qcow2 删除快照
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#创建快照
for i in `ls | grep hd`
do
qemu-img snapshot -c $i.bak $i
done

#查看快照
for i in `ls | grep hd`
do
qemu-img snapshot -l $i
done


#还原快照
for i in `ls | grep hd`
do
qemu-img snapshot -a $i.bak $i
done

#删除快照
for i in `ls | grep hd`
do
qemu-img snapshot -d $i.bak $i
done

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!