1、创建虚拟机
1-1、rocky创建虚拟机
[root@rocky8 ~]#yum install -y qemu-kvm libvirt virt-manager virt-install virt-viewer #安装包
[root@rocky8 ~]#qemu-img create -f qcow2 /var/lib/libvirt/images/centos6.qcow2 20G #创建20G的虚拟磁盘文件
[root@rocky8 ~]#virt-install --virt-type kvm --name centos6-template --ram 1024 --vcpus 2 --cdrom=/data/isos/CentOS-6.10-x86_64-bin-DVD1.iso --disk path=/var/lib/libvirt/images/centos6.qcow2 --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole --os-variant=centos6.9 #创建centos6虚拟机,在/etc/libvirt/qemu/ 目录下回生成一个centos6-template.xml的xml文件
==================================================
[root@rocky8 ~]#qemu-img create -f qcow2 /var/lib/libvirt/images/centos6.qcow2 20G
[root@rocky8 ~]#osinfo-query os| grep rocky #查看是否支持rokcy系统
[root@rocky8 ~]#virt-install --virt-type kvm --name rokcy8 --ram 2048 --vcpus 2 --cdrom=/data/isos/Rocky-8.6-x86_64-dvd1.iso --disk path=/var/lib/libvirt/images/rocky8.qcow2 --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole --os-variant=rocky8.6 #创建rocky8虚拟机
[root@rocky8 ~]#virt-manager
[root@rocky8 ~]#Unable to init server: Could not connect: Connection refused
Unable to init server: Could not connect: Connection refused
Unable to init server: Could not connect: Connection refused
(virt-manager:7782): Gtk-WARNING **: 19:56:49.419: cannot open display:
[root@rocky8 ~]#export DISPLAY=10.0.0.1:0.0 #如果提示以上错误,增加此变量
[root@rocky8 ~]#virt-manager
1-2、ubuntu创建虚拟机
#ubuntu:
[root@ubuntu2004 ~]#apt install -y qemu-kvm virt-manager libvirt-daemon-system
[root@ubuntu2004 ~]#systemctl start libvirtd
[root@ubuntu2004 ~]#qemu-img create -f qcow2 /var/lib/libvirt/images/rokcy8-1.qcow2 20G
[root@ubuntu2004 ~]#virt-install --virt-type kvm --name rocky8-1 --ram 2048 --vcpus 2 --cdrom=/data/isos/Rocky-8.6-x86_64-dvd1.iso --disk path=/var/lib/libvirt/images/rokcy8-1.qcow2 --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole --os-variant=centos8
[root@ubuntu2004 ~]#localectl set-locale LANG=en_US.UTF-8
[root@ubuntu2004 ~]#export DISPLAY=10.0.0.1:0.0
[root@ubuntu2004 ~]#virt-manager
1-3、创建win2008虚拟机
[root@ubuntu2004 isos]#pwd
/data/isos
[root@ubuntu2004 isos]#ll
total 18141812
drwxr-xr-x 2 root root 4096 Oct 12 20:52 ./
drwxr-xr-x 4 root root 4096 Oct 12 19:40 ../
-rw-r--r-- 1 libvirt-qemu kvm 3368839168 Oct 12 20:28 cn_windows_server_2008_r2_standard_enterprise_datacenter_and_web_with_sp1_x64_dvd_617598.iso
-rw-r--r-- 1 libvirt-qemu kvm 2949120 Aug 22 2020 virtio-win-0.1.141_amd64.vfd
[root@ubuntu2004 isos]#osinfo-query os| grep win #查看支持的Windows版本
[root@ubuntu2004 ~]#qemu-img create -f qcow2 /var/lib/libvirt/images/win2008.qcow2 20G
[root@ubuntu2004 ~]#virt-install --virt-type kvm --name win2008r2 --memory 2048 --vcpus=2 --os-variant=win2k8r2 --cdrom=/data/isos/cn_windows_server_2008_r2_standard_enterprise_datacenter_and_web_with_sp1_x64_dvd_617598.iso --disk path=/var/lib/libvirt/images/win2008.qcow2,format=qcow2,bus=virtio --disk path=/data/isos/virtio-win-0.1.141_amd64.vfd,device=floppy --network bridge=virbr0,model=virtio --graphics vnc,listen=0.0.0.0 --noautoconsole --autostart
#说明:--disk path=/data/isos/virtio-win-0.1.141_amd64.vfd,device=floppy 这是windows的virtio驱动,驱动磁盘类型是软盘,此文件需要提前下载,下载网站:
# virtio下载地址:
https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/
# virtio 历史版本下载地址:
https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/archive-virtio/
https://rhn.redhat.com/rhn/software/packages/details/Overview.do?pid=868414
http://www.Linux-kvm.org/page/Downloads
https://docs.fedoraproject.org/en-US/quick-docs/creating-Windows-virtualmachines-using-virtio-drivers/index.html
2-1、验证 Windows virtio 驱动
2-2、安装PCI 内存管理驱动
挂载virtio-win的141的ISO光盘文件,进行安装驱动,注意:不要安装其它版本,比如189版本会蓝屏死机 ,185版本对某些server版本不兼容
2-3、生成Windows Server 2008 镜像模版
2-4、基于模版创建新的Windows的虚拟机
[root@ubuntu2004 isos]#cp /var/lib/libvirt/images/win2008.qcow2 win2008-2.qcow2
[root@ubuntu2004 ~]#virt-install --virt-type kvm --name win2008-2 --memory 2048 --vcpus 2 --os-variant=win2k8r2 --disk path=/var/lib/libvirt/images/win2008-2.qcow2,format=qcow2,bus=virtio --network bridge=virbr0,model=virtio --graphics vnc,listen=0.0.0.0 --noautoconsole --autostart --boot hd
#在manager管理,和新安装的一样。
2、基于现有虚拟机磁盘为模版创建新的虚拟机
[root@rocky8 ~]#cp /var/lib/libvirt/images/rocky8.qcow2 /var/lib/libvirt/images/rocky8-2.qcow2
[root@rocky8 ~]#virt-install --virt-type kvm --name rocky8-2 --ram 1048 --vcpus 2 --disk bus=virtio,path=/var/lib/libvirt/images/rocky8-2.qcow2 --network network=default,model=virtio --graphics vnc,listen=0.0.0.0 --noautoconsole --autostart --boot hd
#提示大概意思:打通ssh连接或者安装askpass
#方法1:在本机安装openssh-askpass包
[root@rocky8 ~]#yum -y install openssh-askpass
#方法2:实现基于本机到远程主机的key验证
[root@rocky8 ~]#ssh-keygen
[root@rocky8 ~]#ssh-copy-id 10.0.0.102
#再重新连接:已经可以看到102的虚拟机
3、virsh 命令行工具
3-1、virsh 子命令说明
help #打印基本帮助信息
attach-device #使用XML文件中的设备定义在虚拟机中添加设备
attach-disk #在虚拟机中附加新磁盘设备
attach-interface #在虚拟机中附加新网络接口
create #从 XML 配置文件生成虚拟机并启动新虚拟机
define #为虚拟机输出XML配置文件
destroy #强制虚拟机停止
detach-device #从虚拟机中分离设备,使用同样的XML 描述作为命令
attach-device
detach-disk #从虚拟机中分离磁盘设备
detach-interface #从虚拟机中分离网络接口
domblkstat #显示正在运行的虚拟机的块设备统计
domid #显示虚拟机ID
domifstat #显示正在运行的虚拟机的网络接口统计
dominfo #显示虚拟机信息
domname #显示虚拟机名称
domstate #显示虚以机状态
domuuid #显示虚拟机UUID
dumpxml #输出虚拟机 XML配置文件
list #列出所有虚拟机
migrate #将虚拟机迁移到另一台主机中
nodeinfo #有关管理程序的输出信息
quit #退出这个互动终端
reboot #重新启动虚拟机
restore #恢复以前保存在文件中的虚拟机
resume #恢复暂停的虚拟机
save #将虚拟机当前状态保存到某个文件中
setmaxmem #为管理程序设定内存上限
setmem #为虚拟机设定分配的内存
setvcpus #修改为虚拟机分配的虚拟CPU数目
shutdown #关闭某个虚拟机
start #启动未激活的虚拟机
suspend #暂停虚拟机
undefine #删除与虚拟机关联的所有文件
vepuinfo #显示虚以机的虚拟CPU信息
vcpupin #控制虚拟机的虚拟CPU亲和性
version #显示virsh版本
#查看子命令帮助
virsh help COMMAND
[root@rocky8 ~]#virsh help list
[root@rocky8 ~]#virsh list --all #列出所有虚拟机,不加all列出开机的虚拟机
Id Name State
-----------------------------------
13 centos6-template running
- rocky8-2 shut off
- rokcy8 shut off
[root@rocky8 ~]#virsh start rocky8-2 #启动
Domain 'rocky8-2' started
[root@rocky8 ~]#virsh list
Id Name State
----------------------------------
13 centos6-template running
14 rocky8-2 running
[root@rocky8 ~]#virsh destroy 14 #强制关机
Domain '14' destroyed
[root@rocky8 ~]#virsh shutdown 13 #正常关机
[root@rocky8 ~]#virsh list --uuid --name #列出启动的虚拟机的UUID和名称
43e849ad-ef9c-4ec5-9e71-644952e80f71 rocky8-
[root@rocky8 ~]#virsh list --uuid --all --name #列出所有的虚拟机的UUID和名称
43e849ad-ef9c-4ec5-9e71-644952e80f71 rocky8-2
50f347a7-8a96-4de0-ba30-8a02cd44fb36 centos6-template
eae4adb6-c499-47bb-99c3-d42e46cbd54d rokcy8
[root@rocky8 ~]#virsh domuuid --domain rokcy8 #查看指定虚拟机的uuid
eae4adb6-c499-47bb-99c3-d42e46cbd54d
[root@rocky8 ~]#virsh destroy 43e849ad-ef9c-4ec5-9e71-644952e80f71 #通过uuid关机
Domain '43e849ad-ef9c-4ec5-9e71-644952e80f71' destroyed
[root@rocky8 ~]#virsh start 43e849ad-ef9c-4ec5-9e71-644952e80f71 #通过uuid开机
Domain 'rocky8-2' started
[root@rocky8 ~]#virsh domblklist rocky8-2 ##查看块设备
Target Source
--------------------------------------------------
vda /var/lib/libvirt/images/rocky8-2.qcow
[root@rocky8 ~]#virsh suspend rocky8-2 #暂停虚拟机
Domain 'rocky8-2' suspended
[root@rocky8 ~]#virsh list
Id Name State
-------------------------
16 rocky8-2 paused
[root@rocky8 ~]#virsh resume rocky8-2 #恢复虚拟机
Domain 'rocky8-2' resumed
[root@rocky8 ~]#virsh autostart rokcy8 #配置虚拟机开机自动启动
Domain 'rokcy8' marked as autostarte
[root@rocky8 ~]#virsh autostart --domain rokcy8 --disable #关闭自动启动
Domain 'rokcy8' unmarked as autostarted
3-2、查看虚拟机配置
[root@rocky8 ~]#ll /etc/libvirt/qemu #每个虚拟机配置都存放在/etc/libvirt/qemu目录下的xml文件中
[root@rocky8 ~]#virsh dumpxml rocky8-2 #查看指定虚拟机的配置,此命令相当于查看/etc/libvirt/qemu/rocky8-2.xml
3-3、删除虚拟机配置
[root@rocky8 ~]#virsh list --all
Id Name State
-----------------------------------
16 rocky8-2 running
- centos6-template shut off
- rokcy8 shut off
[root@rocky8 ~]#virsh undefine rokcy8 #删除虚拟机配置,但不删除磁盘文件
Domain 'rokcy8' has been undefined
[root@rocky8 images]#virt-install --import --name=rocky8.qcow2 --vcpus=1 --ram=1024 --disk bus=scsi,path=/var/lib/libvirt/images/rocky8.qcow2 --network network=default --graphics vnc,listen=0.0.0.0 --os-variant=rocky8.6 --noautoconsole --boot hd #导入虚拟机
[root@rocky8 images]#virsh undefine rocky9 --remove-all-storage #删除虚拟机并且删除磁盘文件
Domain 'rocky9' has been undefined
Volume 'sda'(/var/lib/libvirt/images/Rocky9.img) removed.
4、存储管理
4-1、检查虚拟磁盘
[root@rocky8 ~]#qemu-img check /var/lib/libvirt/images/centos6.qcow2 #对于关机状态的虚拟机磁盘,可以检查文件错误
No errors were found on the image.
24391/327680 = 7.44% allocated, 5.01% fragmented, 0.00% compressed clusters
Image end offset: 1600258048
4-2、虚拟磁盘格式转换
[root@rocky8 images]#qemu-img info Rocky9.vmdk
image: Rocky9.vmdk
file format: vmdk
virtual size: 20 GiB (21474836480 bytes)
disk size: 1.56 GiB
cluster_size: 65536
Format specific information:
cid: 2614347852
parent cid: 4294967295
create type: monolithicSparse
extents:
[0]:
virtual size: 21474836480
filename: Rocky9.vmdk
cluster size: 65536
format:
[root@rocky8 images]#qemu-img convert Rocky9.vmdk Rocky9.img #默认转化为raw格式
[root@rocky8 images]#ll Rocky9.img Rocky9.vmdk -h #比较大小
-rw-r--r-- 1 root root 20G Oct 12 22:28 Rocky9.img
-rw-r--r-- 1 root root 1.6G Oct 12 22:23 Rocky9.vmdk
[root@rocky8 images]#du -h Rocky9.img Rocky9.vmdk
1.6G Rocky9.img
1.6G Rocky9.vmdk
[root@rocky8 images]#virt-install --import --name=rocky9 --vcpus=1 --ram=1024 --disk bus=scsi,path=/var/lib/libvirt/images/Rocky9.img --network network=default --graphics vnc,listen=0.0.0.0 --os-variant=rocky9.0 --noautoconsole --boot hd #导入虚拟机,但是vmdk转换的格式导入kvm后无法启动
4-3、调整虚拟磁盘大小
- 操作之前,一定要做好数据备份
- 增加文件大小后,需要在客户机中使用fdisk、parted等分区工具进行相应的操作才能真正让客户机使用到增加后的镜像空间。
- 缩小镜像之前,要在客户机中保证里面的文件系统有空余空间,否则会数据丢失。另外xfs文件系统不支持缩减
- qcow2不支持缩小镜像的操作
#扩展虚拟磁盘
[root@rocky8 images]#qemu-img info /var/lib/libvirt/images/rocky8.qcow2
image: /var/lib/libvirt/images/rocky8.qcow2
file format: qcow2
virtual size: 20 GiB (21474836480 bytes)
disk size: 1.86 GiB
cluster_size: 65536
Format specific information:
compat: 1.1
compression type: zlib
lazy refcounts: false
refcount bits: 16
corrupt: false
extended l2: false
[root@rocky8 images]#qemu-img resize /var/lib/libvirt/images/rocky8.qcow2 +10G
Image resized.
[root@rocky8 images]#qemu-img info /var/lib/libvirt/images/rocky8.qcow2
image: /var/lib/libvirt/images/rocky8.qcow2
file format: qcow2
virtual size: 30 GiB (32212254720 bytes)
#启动虚拟机后,还需要使用fdisk等工具进行空间的继续管理才能使用
4-4、磁盘快照管理
4-4-1、qemu-img管理快照
[root@rocky8 images]#qemu-img snapshot -l /var/lib/libvirt/images/rocky8-2.qcow2 #查看快照,如果没有快照,则无显示信息
[root@rocky8 images]#qemu-img snapshot -c rockysnap /var/lib/libvirt/images/rocky8-2.qcow2 #创建快照
[root@rocky8 images]#qemu-img snapshot -l /var/lib/libvirt/images/rocky8-2.qcow2 #查看快照
Snapshot list:
ID TAG VM SIZE DATE VM CLOCK ICOUNT
1 rockysnap 0 B 2022-10-12 22:44:15 00:00:00.000 0
[root@rocky8 images]#qemu-img info /var/lib/libvirt/images/rocky8-2.qcow2 #查看快照信息
image: /var/lib/libvirt/images/rocky8-2.qcow2
file format: qcow2
virtual size: 20 GiB (21474836480 bytes)
disk size: 1.87 GiB
cluster_size: 65536
Snapshot list:
ID TAG VM SIZE DATE VM CLOCK ICOUNT
1 rockysnap 0 B 2022-10-12 22:44:15 00:00:00.000 0
[root@rocky8 images]#qemu-img snapshot -a rockysnap /var/lib/libvirt/images/rocky8-2.qcow2 #关机后才能还原快照修复故障
[root@rocky8 images]#qemu-img snapshot -d rockysnap /var/lib/libvirt/images/rocky8-2.qcow2 #关机后才能删除快照
4-4-2、virsh管理快照
[root@rocky8 images]#virsh snapshot-list rocky8-2 #查看
Name Creation Time State
-------------------------------
# 第一种创建快照方法
[root@rocky8 images]#virsh snapshot-create-as --domain rocky8-2 --name rockysnap #为rocky8-2创建名为rockysnap的快照
Domain snapshot rockysnap created
#第二种创建快照方法
[root@rocky8 images]#virsh snapshot-create rocky8 #为rocky8创建快照,快照名称随机生成
Domain snapshot 1665586627 created
# 第一种快照还原方法
[root@rocky8 images]#virsh snapshot-revert rocky8-2 --snapshotname rockysnap --running
# 第二种快照还原方法
[root@rocky8 images]#virsh snapshot-revert rocky8 --snapshotname 1665586627 --running
5、网络管理
[root@rocky8 ~]#virsh domiflist rocky8-3 #查看虚拟机的网卡配置
Interface Type Source Model MAC
-------------------------------------------------------------
vnet2 network default virtio 52:54:00:9a:d9:ef
[root@rocky8 ~]#virsh domifaddr rocky8-3
Name MAC address Protocol Address
-------------------------------------------------------------------------------
vnet2 52:54:00:9a:d9:ef ipv4 192.168.122.247/24
[root@rocky8 ~]#virsh domifstat rocky8-3 --interface vnet2 #查看虚拟机的指定网卡的状态
vnet2 rx_bytes 13600
vnet2 rx_packets 239
vnet2 rx_errs 0
vnet2 rx_drop 0
vnet2 tx_bytes 2305
vnet2 tx_packets 22
vnet2 tx_errs 0
vnet2 tx_drop 0
[root@rocky8 ~]#virsh domif-getlink rocky8-3 --interface vnet2
vnet2 up
[root@rocky8 ~]#virsh domif-getlink rocky8-3 --config --interface 52\:54\:00\:9a\:d9\:ef
52:54:00:9a:d9:ef up
5-1、宿主机创建网桥
[root@rocky8 ~]#nmcli connection add type bridge ifname br0 con-name br0
[root@rocky8 ~]#nmcli connection modify br0 ipv4.addresses 10.0.0.199/24 ipv4.method manual gw4 10.0.0.2 ipv4.dns 10.0.0.2,223.5.5.5,180.76.76.76
[root@rocky8 ~]#nmcli connection up br0
[root@rocky8 ~]#nmcli connection add type bridge-slave con-name br0-port0 ifname eth0 master br0
[root@rocky8 ~]#nmcli connection add type bridge-slave con-name br0-port1 ifname eth1 master br0
[root@rocky8 ~]#nmcli connection up br0-port0 #启动此网卡后xshell会断开连接,需要连接br0的ip 10.0.0.199
[root@rocky8 ~]#nmcli connection up br0-port1
[root@rocky8 ~]#yum install -y bridge-utils
[root@rocky8 ~]#brctl show
bridge name bridge id STP enabled interfaces
br0 8000.000c295d78d1 yes eth0
eth1
virbr0 8000.52540053d73a yes vnet0
vnet1
vnet5
[root@rocky8 ~]#brctl show br0
bridge name bridge id STP enabled interfaces
br0 8000.000c295d78d1 yes eth0
eth1