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
| systemctl enable libvirtd --now
|

1.4 命令行删除默认的虚拟化网桥
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| [root@KVM ~] Name State Autostart Persistent -------------------------------------------- default active yes yes
[root@KVM ~] Network default destroyed
[root@KVM ~] Name State Autostart Persistent ---------------------------------------------- default inactive yes yes
[root@KVM ~] Network default has been undefined
[root@KVM ~] Name State Autostart Persistent ----------------------------------------
|
1.5 通过nmcli 命令行创建linux bridge
1.6 关闭selinux与防火墙
1 2 3
| [root@KVM ~] [root@KVM ~] [root@KVM ~]
|
2、命令行管理
2.1 网桥的创建
2.2 使用cloud镜像
KVM虚拟化创建的虚拟机,虚拟磁盘的后缀名叫做qcow2,当创建了一个虚拟机后,将会生成一个qcow2的虚拟磁盘文件,里面就有你安装的操作系统。但是不能每次都安装系统,就出现了一个模板化布置虚拟机的方法。如何获取一个qcow2的文件作为模板?
主流的linux发行版本官方都会提供qcow2模板文件
centos:https://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 ~]
[root@KVM ~]
[root@KVM ~] --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 ~] Id Name State ---------------------------------- 1 centos-stream8-1 running
|
2.4 进入虚拟机的控制台
2.5 删除虚拟机
1 2 3 4 5
| [root@KVM ~] Domain 'centos-stream8-1' destroyed
[root@KVM ~] 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 ~]
[root@KVM ~] --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 ~] 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 ~] 8: vnet0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 master vm-bridge state forwarding priority 32 cost 100
[root@KVM ~] [root@KVM ~]
[root@KVM ~] Domain 'centos-stream8-1' destroyed
[root@KVM ~] Domain 'centos-stream8-1' started
[root@KVM ~] 9: vnet1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 master vm-bridge state listening priority 32 cost 100
[root@KVM ~] 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 ~] Domain 'centos-stream8-1' destroyed
[root@KVM ~] Domain 'centos-stream8-1' has been undefined
[root@KVM ~] Physical volume "/dev/sdd" successfully created. [root@KVM ~] Volume group "data" successfully created [root@KVM ~] 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
| [root@KVM ~] 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
[root@KVM ~] 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 -
[root@KVM ~]
[root@KVM tmp] /tmp/CentOS-Stream-GenericCloud-8-20220125.1.x86_64.qcow2 /tmp/os.qcow2
[root@KVM tmp] 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 ~] 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
|