半虚拟化驱动virtio的工作原理
为了提高内存、硬盘、网络的性能,需要支持半虚拟化
virtio 是一种 I/O 半虚拟化解决方案,是一套通用 I/O 设备虚拟化的程序,是对半虚拟化 Hypervisor 中的一组通用 I/O 设备的抽象,提供了一套上层应用与各 Hypervisor 虚拟化设备(KVM,Xen,VMware等)之间的通信框架和编程接口,减少跨平台所带来的兼容性问题,大大提高驱动程序开发效率,Windows 系统需要单独安装virtio驱 动,Linux系统自带virtio驱动。
libvirt 架构
如果libvirtd服务意外关闭,将导致相关工具,如:virt-manager等无法和虚拟机连接,但虚拟机仍会正常运行
[root@ubuntu2204 ~]#virsh list --all
Id Name State
------------------------------------
2 centos7-mooreyxia running
- rocky8 shut off
[root@ubuntu2204 ~]#systemctl status libvirtd
● libvirtd.service - Virtualization daemon
Loaded: loaded (/lib/systemd/system/libvirtd.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2022-12-31 17:53:28 CST; 17h ago
TriggeredBy: ● libvirtd-ro.socket
● libvirtd.socket
● libvirtd-admin.socket
Docs: man:libvirtd(8)
https://libvirt.org
Main PID: 871 (libvirtd)
Tasks: 23 (limit: 32768)
Memory: 72.6M
CPU: 9.258s
CGroup: /system.slice/libvirtd.service
├─871 /usr/sbin/libvirtd
├─987 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/lib/libvirt/libvirt_leaseshelper
└─988 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/lib/libvirt/libvirt_leaseshelper
Dec 31 17:53:30 ubuntu2204.wang.org dnsmasq[987]: using nameserver 127.0.0.53#53
Dec 31 17:53:30 ubuntu2204.wang.org dnsmasq[987]: read /etc/hosts - 7 addresses
Dec 31 17:53:30 ubuntu2204.wang.org dnsmasq[987]: read /var/lib/libvirt/dnsmasq/default.addnhosts - 0 addresses
Dec 31 17:53:30 ubuntu2204.wang.org dnsmasq-dhcp[987]: read /var/lib/libvirt/dnsmasq/default.hostsfile
Jan 01 11:11:55 ubuntu2204.wang.org dnsmasq-dhcp[987]: DHCPDISCOVER(virbr0) 192.168.122.23 52:54:00:18:b7:d2
Jan 01 11:11:55 ubuntu2204.wang.org dnsmasq-dhcp[987]: DHCPOFFER(virbr0) 192.168.122.23 52:54:00:18:b7:d2
Jan 01 11:11:55 ubuntu2204.wang.org dnsmasq-dhcp[987]: DHCPREQUEST(virbr0) 192.168.122.23 52:54:00:18:b7:d2
Jan 01 11:11:55 ubuntu2204.wang.org dnsmasq-dhcp[987]: DHCPACK(virbr0) 192.168.122.23 52:54:00:18:b7:d2
Jan 01 11:18:35 ubuntu2204.wang.org dnsmasq-dhcp[987]: DHCPREQUEST(virbr0) 192.168.122.23 52:54:00:18:b7:d2
Jan 01 11:18:35 ubuntu2204.wang.org dnsmasq-dhcp[987]: DHCPACK(virbr0) 192.168.122.23 52:54:00:18:b7:d2
#停止libvirtd服务,socket还在运行,systemctl会自动激活socket,所以libvirtd会自动重启
[root@ubuntu2204 ~]#systemctl stop libvirtd
Warning: Stopping libvirtd.service, but it can still be activated by:
libvirtd-ro.socket
libvirtd.socket
libvirtd-admin.socket
[root@ubuntu2204 ~]#systemctl status libvirtd-ro.socket
● libvirtd-ro.socket - Libvirt local read-only socket
Loaded: loaded (/lib/systemd/system/libvirtd-ro.socket; enabled; vendor preset: enabled)
Active: active (listening) since Sat 2022-12-31 17:53:24 CST; 17h ago
Triggers: ● libvirtd.service
Listen: /run/libvirt/libvirt-sock-ro (Stream)
CGroup: /system.slice/libvirtd-ro.socket
Dec 31 17:53:24 ubuntu2204.wang.org systemd[1]: Listening on Libvirt local read-only socket.
[root@ubuntu2204 ~]#systemctl status libvirtd
○ libvirtd.service - Virtualization daemon
Loaded: loaded (/lib/systemd/system/libvirtd.service; enabled; vendor preset: enabled)
Active: inactive (dead) since Sun 2023-01-01 11:27:32 CST; 1min 32s ago
TriggeredBy: ● libvirtd-ro.socket
● libvirtd.socket
● libvirtd-admin.socket
Docs: man:libvirtd(8)
https://libvirt.org
Process: 871 ExecStart=/usr/sbin/libvirtd $LIBVIRTD_ARGS (code=exited, status=0/SUCCESS)
Main PID: 871 (code=exited, status=0/SUCCESS)
Tasks: 2 (limit: 32768)
Memory: 57.1M
CPU: 9.370s
CGroup: /system.slice/libvirtd.service
├─987 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/lib/libvirt/libvirt_leaseshelper
└─988 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/lib/libvirt/libvirt_leaseshelper
Jan 01 11:11:55 ubuntu2204.wang.org dnsmasq-dhcp[987]: DHCPREQUEST(virbr0) 192.168.122.23 52:54:00:18:b7:d2
Jan 01 11:11:55 ubuntu2204.wang.org dnsmasq-dhcp[987]: DHCPACK(virbr0) 192.168.122.23 52:54:00:18:b7:d2
Jan 01 11:18:35 ubuntu2204.wang.org dnsmasq-dhcp[987]: DHCPREQUEST(virbr0) 192.168.122.23 52:54:00:18:b7:d2
Jan 01 11:18:35 ubuntu2204.wang.org dnsmasq-dhcp[987]: DHCPACK(virbr0) 192.168.122.23 52:54:00:18:b7:d2
Jan 01 11:27:32 ubuntu2204.wang.org systemd[1]: Stopping Virtualization daemon...
Jan 01 11:27:32 ubuntu2204.wang.org systemd[1]: libvirtd.service: Deactivated successfully.
Jan 01 11:27:32 ubuntu2204.wang.org systemd[1]: libvirtd.service: Unit process 987 (dnsmasq) remains running after unit stopped.
Jan 01 11:27:32 ubuntu2204.wang.org systemd[1]: libvirtd.service: Unit process 988 (dnsmasq) remains running after unit stopped.
Jan 01 11:27:32 ubuntu2204.wang.org systemd[1]: Stopped Virtualization daemon.
Jan 01 11:27:32 ubuntu2204.wang.org systemd[1]: libvirtd.service: Consumed 9.370s CPU time.
#彻底停掉libvirt服务,发现虚拟机还在运行,只是连接虚拟机的窗口失效
[root@ubuntu2204 ~]#systemctl stop libvirtd libvirtd.socket libvirtdadmin.socket libvirtd-ro.socket
Failed to stop libvirtdadmin.socket: Unit libvirtdadmin.socket not loaded.
[root@ubuntu2204 ~]#virsh list --all
error: failed to connect to the hypervisor
error: Failed to connect socket to '/var/run/libvirt/libvirt-sock': Connection refused
[root@ubuntu2204 ~]#ps aux |grep kvm
root 1247 0.0 0.0 0 0 ? I 10:52 0:02 [kworker/5:0-kvm-irqfd-cleanup]
libvirt+ 1575 5.8 3.2 3853876 532360 ? Sl 11:17 0:55 /usr/bin/qemu-system-x86_64 -name guest=centos7-mooreyxia,debug-threads=on -S -object {"qom-type":"secret","id":"masterKey0","format":"raw","file":"/var/lib/libvirt/qemu/domain-2-centos7-mooreyxia/master-key.aes"} -machine pc-q35-6.2,usb=off,vmport=off,dump-guest-core=off,memory-backend=pc.ram -accel kvm -cpu host,migratable=on -m 2048 -object {"qom-type":"memory-backend-ram","id":"pc.ram","size":2147483648} -overcommit mem-lock=off -smp 2,sockets=2,cores=1,threads=1 -uuid 3492dccc-468d-4fe9-8ec8-b4a251dccf00 -no-user-config -nodefaults -chardev socket,id=charmonitor,fd=33,server=on,wait=off -mon chardev=charmonitor,id=monitor,mode=control -rtc base=utc,driftfix=slew -global kvm-pit.lost_tick_policy=delay -no-hpet -no-shutdown -global ICH9-LPC.disable_s3=1 -global ICH9-LPC.disable_s4=1 -boot strict=on -device pcie-root-port,port=16,chassis=1,id=pci.1,bus=pcie.0,multifunction=on,addr=0x2 -device pcie-root-port,port=17,chassis=2,id=pci.2,bus=pcie.0,addr=0x2.0x1 -device pcie-root-port,port=18,chassis=3,id=pci.3,bus=pcie.0,addr=0x2.0x2 -device pcie-root-port,port=19,chassis=4,id=pci.4,bus=pcie.0,addr=0x2.0x3 -device pcie-root-port,port=20,chassis=5,id=pci.5,bus=pcie.0,addr=0x2.0x4 -device pcie-root-port,port=21,chassis=6,id=pci.6,bus=pcie.0,addr=0x2.0x5 -device pcie-root-port,port=22,chassis=7,id=pci.7,bus=pcie.0,addr=0x2.0x6 -device pcie-root-port,port=23,chassis=8,id=pci.8,bus=pcie.0,addr=0x2.0x7 -device pcie-root-port,port=24,chassis=9,id=pci.9,bus=pcie.0,multifunction=on,addr=0x3 -device pcie-root-port,port=25,chassis=10,id=pci.10,bus=pcie.0,addr=0x3.0x1 -device pcie-root-port,port=26,chassis=11,id=pci.11,bus=pcie.0,addr=0x3.0x2 -device pcie-root-port,port=27,chassis=12,id=pci.12,bus=pcie.0,addr=0x3.0x3 -device pcie-root-port,port=28,chassis=13,id=pci.13,bus=pcie.0,addr=0x3.0x4 -device pcie-root-port,port=29,chassis=14,id=pci.14,bus=pcie.0,addr=0x3.0x5 -device qemu-xhci,p2=15,p3=15,id=usb,bus=pci.2,addr=0x0 -device virtio-serial-pci,id=virtio-serial0,bus=pci.3,addr=0x0 -blockdev {"driver":"file","filename":"/var/lib/libvirt/images/centos7-mooreyxia.qcow2","node-name":"libvirt-2-storage","auto-read-only":true,"discard":"unmap"} -blockdev {"node-name":"libvirt-2-format","read-only":false,"discard":"unmap","driver":"qcow2","file":"libvirt-2-storage","backing":null} -device virtio-blk-pci,bus=pci.4,addr=0x0,drive=libvirt-2-format,id=virtio-disk0,bootindex=1 -device ide-cd,bus=ide.0,id=sata0-0-0 -netdev tap,fd=34,id=hostnet0,vhost=on,vhostfd=36 -device virtio-net-pci,netdev=hostnet0,id=net0,mac=52:54:00:18:b7:d2,bus=pci.1,addr=0x0 -chardev pty,id=charserial0 -device isa-serial,chardev=charserial0,id=serial0 -chardev socket,id=charchannel0,fd=32,server=on,wait=off -device virtserialport,bus=virtio-serial0.0,nr=1,chardev=charchannel0,id=channel0,name=org.qemu.guest_agent.0 -chardev spicevmc,id=charchannel1,name=vdagent -device virtserialport,bus=virtio-serial0.0,nr=2,chardev=charchannel1,id=channel1,name=com.redhat.spice.0 -device usb-tablet,id=input0,bus=usb.0,port=1 -audiodev {"id":"audio1","driver":"spice"} -spice port=5900,addr=127.0.0.1,disable-ticketing=on,image-compression=off,seamless-migration=on -device qxl-vga,id=video0,ram_size=67108864,vram_size=67108864,vram64_size_mb=0,vgamem_mb=16,max_outputs=1,bus=pcie.0,addr=0x1 -device ich9-intel-hda,id=sound0,bus=pcie.0,addr=0x1b -device hda-duplex,id=sound0-codec0,bus=sound0.0,cad=0,audiodev=audio1 -chardev spicevmc,id=charredir0,name=usbredir -device usb-redir,chardev=charredir0,id=redir0,bus=usb.0,port=2 -chardev spicevmc,id=charredir1,name=usbredir -device usb-redir,chardev=charredir1,id=redir1,bus=usb.0,port=3 -device virtio-balloon-pci,id=balloon0,bus=pci.5,addr=0x0 -object {"qom-type":"rng-random","id":"objrng0","filename":"/dev/urandom"} -device virtio-rng-pci,rng=objrng0,id=rng0,bus=pci.6,addr=0x0 -sandbox on,obsolete=deny,elevateprivileges=deny,spawn=deny,resourcecontrol=deny -msg timestamp=on
root 1581 0.0 0.0 0 0 ? S 11:17 0:00 [kvm-nx-lpage-re]
root 1586 0.0 0.0 0 0 ? S 11:17 0:00 [kvm-pit/1575]
root 1781 0.0 0.0 9696 2404 pts/1 S+ 11:33 0:00 grep --color=auto kvm
#重启libvirtd服务,就可以继续连接kvm进行管理
[root@ubuntu2204 ~]#systemctl start libvirtd
[root@ubuntu2204 ~]#virsh list --all
Id Name State
------------------------------------
2 centos7-mooreyxia running
- rocky8 shut off
virt-manager 管理虚拟机
virt-manager是一个图形化工具,主要功能:
- 定义和创建虚拟机
- 硬件管理
- 性能监视
- 控制台
- 在线和离线迁移
- 虚拟机的保存和恢复、暂停和继续、关闭和启动
virsh 命令行工具
virsh是使用libvirt managementAPI构建的管理工具,相比virt-manager可以提高效率
#virsh 主要功能
[root@ubuntu2204 ~]#virsh --help
virsh [options]... [<command_string>]
virsh [options]... <command> [args...]
options:
-c | --connect=URI hypervisor connection URI
-d | --debug=NUM debug level [0-4]
-e | --escape <char> set escape sequence for console
-h | --help this help
-k | --keepalive-interval=NUM
keepalive interval in seconds, 0 for disable
-K | --keepalive-count=NUM
number of possible missed keepalive messages
-l | --log=FILE output logging to file
-q | --quiet quiet mode
-r | --readonly connect readonly
-t | --timing print timing information
-v short version
-V long version
--version[=TYPE] version, TYPE is short or long (default short)
....
Domain Management (help keyword 'domain'):
Domain Monitoring (help keyword 'monitor'):
Host and Hypervisor (help keyword 'host'):
Interface (help keyword 'interface'):
Network Filter (help keyword 'filter'):
Networking (help keyword 'network'):
Node Device (help keyword 'nodedev'):
Secret (help keyword 'secret'):
Snapshot (help keyword 'snapshot'):
Storage Pool (help keyword 'pool'):
Storage Volume (help keyword 'volume'):
Virsh itself (help keyword 'virsh'):
#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版本
#查看子命令 list 命令用法
[root@ubuntu2204 ~]#virsh help list
NAME
list - list domains
SYNOPSIS
list [--inactive] [--all] [--transient] [--persistent] [--with-snapshot] [--without-snapshot] [--with-checkpoint] [--without-checkpoint] [--state-running] [--state-paused] [--state-shutoff] [--state-other] [--autostart] [--no-autostart] [--with-managed-save] [--without-managed-save] [--uuid] [--name] [--id] [--table] [--managed-save] [--title]
DESCRIPTION
Returns list of domains.
OPTIONS
--inactive list inactive domains
--all list inactive & active domains
--transient list transient domains
--persistent list persistent domains
--with-snapshot list domains with existing snapshot
--without-snapshot list domains without a snapshot
--with-checkpoint list domains with existing checkpoint
--without-checkpoint list domains without a checkpoint
--state-running list domains in running state
--state-paused list domains in paused state
--state-shutoff list domains in shutoff state
--state-other list domains in other states
--autostart list domains with autostart enabled
--no-autostart list domains with autostart disabled
--with-managed-save list domains with managed save state
--without-managed-save list domains without managed save
--uuid list uuid's only
--name list domain names only
--id list domain IDs only
--table list table (default)
--managed-save mark inactive domains with managed save state
--title show domain title
例:
[root@ubuntu2204 ~]#virsh list --all
Id Name State
------------------------------------
2 centos7-mooreyxia running
- rocky8 shut off
[root@ubuntu2204 ~]#virsh start rocky8
Domain 'rocky8' started
[root@ubuntu2204 ~]#virsh list --all
Id Name State
-----------------------------------
2 centos7-mooreyxia running
3 rocky8 running
[root@ubuntu2204 ~]#virsh shutdown 3
Domain '3' is being shutdown
#列出开机状态虚拟机的UUID和名称
[root@ubuntu2204 ~]#virsh list --uuid --name
3492dccc-468d-4fe9-8ec8-b4a251dccf00 centos7-mooreyxia
3a984e57-70f4-428d-8f30-3127dd4e9be2 rocky8
案例:查看虚拟机UUID,通过UUID启动关闭虚拟机
[root@ubuntu2204 ~]#virsh list --all
Id Name State
-----------------------------------
2 centos7-mooreyxia running
3 rocky8 running
[root@ubuntu2204 ~]#virsh domuuid 3
3a984e57-70f4-428d-8f30-3127dd4e9be2
[root@ubuntu2204 ~]#virsh destroy 3a984e57-70f4-428d-8f30-3127dd4e9be2
Domain '3a984e57-70f4-428d-8f30-3127dd4e9be2' destroyed
[root@ubuntu2204 ~]#virsh list --all
Id Name State
------------------------------------
2 centos7-mooreyxia running
- rocky8 shut off
案例:暂停和恢复虚拟机
[root@ubuntu2204 ~]#virsh list
Id Name State
-----------------------------------
2 centos7-mooreyxia running
[root@ubuntu2204 ~]#virsh suspend centos7-mooreyxia
Domain 'centos7-mooreyxia' suspended
[root@ubuntu2204 ~]#virsh list
Id Name State
----------------------------------
2 centos7-mooreyxia paused
[root@ubuntu2204 ~]#ps aux|grep kvm
root 1247 0.0 0.0 0 0 ? I 10:52 0:02 [kworker/5:0-kvm-irqfd-cleanup]
libvirt+ 1575 2.3 3.3 3862072 550556 ? Sl 11:17 1:13 /usr/bin/qemu-system-x86_64 -name guest=centos7-mooreyxia,debug-threads=on -S -object {"qom-type":"secret","id":"masterKey0","format":"raw","file":"/var/lib/libvirt/qemu/domain-2-centos7-mooreyxia/master-key.aes"} -machine pc-q35-6.2,usb=off,vmport=off,dump-guest-core=off,memory-backend=pc.ram -accel kvm -cpu host,migratable=on -m 2048 -object {"qom-type":"memory-backend-ram","id":"pc.ram","size":2147483648} -overcommit mem-lock=off -smp 2,sockets=2,cores=1,threads=1 -uuid 3492dccc-468d-4fe9-8ec8-b4a251dccf00 -no-user-config -nodefaults -chardev socket,id=charmonitor,fd=33,server=on,wait=off -mon chardev=charmonitor,id=monitor,mode=control -rtc base=utc,driftfix=slew -global kvm-pit.lost_tick_policy=delay -no-hpet -no-shutdown -global ICH9-LPC.disable_s3=1 -global ICH9-LPC.disable_s4=1 -boot strict=on -device pcie-root-port,port=16,chassis=1,id=pci.1,bus=pcie.0,multifunction=on,addr=0x2 -device pcie-root-port,port=17,chassis=2,id=pci.2,bus=pcie.0,addr=0x2.0x1 -device pcie-root-port,port=18,chassis=3,id=pci.3,bus=pcie.0,addr=0x2.0x2 -device pcie-root-port,port=19,chassis=4,id=pci.4,bus=pcie.0,addr=0x2.0x3 -device pcie-root-port,port=20,chassis=5,id=pci.5,bus=pcie.0,addr=0x2.0x4 -device pcie-root-port,port=21,chassis=6,id=pci.6,bus=pcie.0,addr=0x2.0x5 -device pcie-root-port,port=22,chassis=7,id=pci.7,bus=pcie.0,addr=0x2.0x6 -device pcie-root-port,port=23,chassis=8,id=pci.8,bus=pcie.0,addr=0x2.0x7 -device pcie-root-port,port=24,chassis=9,id=pci.9,bus=pcie.0,multifunction=on,addr=0x3 -device pcie-root-port,port=25,chassis=10,id=pci.10,bus=pcie.0,addr=0x3.0x1 -device pcie-root-port,port=26,chassis=11,id=pci.11,bus=pcie.0,addr=0x3.0x2 -device pcie-root-port,port=27,chassis=12,id=pci.12,bus=pcie.0,addr=0x3.0x3 -device pcie-root-port,port=28,chassis=13,id=pci.13,bus=pcie.0,addr=0x3.0x4 -device pcie-root-port,port=29,chassis=14,id=pci.14,bus=pcie.0,addr=0x3.0x5 -device qemu-xhci,p2=15,p3=15,id=usb,bus=pci.2,addr=0x0 -device virtio-serial-pci,id=virtio-serial0,bus=pci.3,addr=0x0 -blockdev {"driver":"file","filename":"/var/lib/libvirt/images/centos7-mooreyxia.qcow2","node-name":"libvirt-2-storage","auto-read-only":true,"discard":"unmap"} -blockdev {"node-name":"libvirt-2-format","read-only":false,"discard":"unmap","driver":"qcow2","file":"libvirt-2-storage","backing":null} -device virtio-blk-pci,bus=pci.4,addr=0x0,drive=libvirt-2-format,id=virtio-disk0,bootindex=1 -device ide-cd,bus=ide.0,id=sata0-0-0 -netdev tap,fd=34,id=hostnet0,vhost=on,vhostfd=36 -device virtio-net-pci,netdev=hostnet0,id=net0,mac=52:54:00:18:b7:d2,bus=pci.1,addr=0x0 -chardev pty,id=charserial0 -device isa-serial,chardev=charserial0,id=serial0 -chardev socket,id=charchannel0,fd=32,server=on,wait=off -device virtserialport,bus=virtio-serial0.0,nr=1,chardev=charchannel0,id=channel0,name=org.qemu.guest_agent.0 -chardev spicevmc,id=charchannel1,name=vdagent -device virtserialport,bus=virtio-serial0.0,nr=2,chardev=charchannel1,id=channel1,name=com.redhat.spice.0 -device usb-tablet,id=input0,bus=usb.0,port=1 -audiodev {"id":"audio1","driver":"spice"} -spice port=5900,addr=127.0.0.1,disable-ticketing=on,image-compression=off,seamless-migration=on -device qxl-vga,id=video0,ram_size=67108864,vram_size=67108864,vram64_size_mb=0,vgamem_mb=16,max_outputs=1,bus=pcie.0,addr=0x1 -device ich9-intel-hda,id=sound0,bus=pcie.0,addr=0x1b -device hda-duplex,id=sound0-codec0,bus=sound0.0,cad=0,audiodev=audio1 -chardev spicevmc,id=charredir0,name=usbredir -device usb-redir,chardev=charredir0,id=redir0,bus=usb.0,port=2 -chardev spicevmc,id=charredir1,name=usbredir -device usb-redir,chardev=charredir1,id=redir1,bus=usb.0,port=3 -device virtio-balloon-pci,id=balloon0,bus=pci.5,addr=0x0 -object {"qom-type":"rng-random","id":"objrng0","filename":"/dev/urandom"} -device virtio-rng-pci,rng=objrng0,id=rng0,bus=pci.6,addr=0x0 -sandbox on,obsolete=deny,elevateprivileges=deny,spawn=deny,resourcecontrol=deny -msg timestamp=on
root 1581 0.0 0.0 0 0 ? S 11:17 0:00 [kvm-nx-lpage-re]
root 1586 0.0 0.0 0 0 ? S 11:17 0:00 [kvm-pit/1575]
root 2244 0.0 0.0 9696 2268 pts/1 S+ 12:09 0:00 grep --color=auto kvm
[root@ubuntu2204 ~]#virsh resume centos7-mooreyxia
Domain 'centos7-mooreyxia' resumed
[root@ubuntu2204 ~]#virsh list
Id Name State
-----------------------------------
2 centos7-mooreyxia running
案例:配置虚拟机开机自动启动
在virt-manager图形界面中可以设置宿主机启动时虚拟机自动开启
xml是这个虚拟机的所有配置文本
查看虚拟机配置
#未勾选开机启动时
[root@ubuntu2204 ~]#virsh list --all
Id Name State
------------------------------------
2 centos7-mooreyxia running
- rocky8 shut off
#每个虚拟机配置都存放在/etc/libvirt/qemu目录下的xml文件中
[root@ubuntu2204 ~]#ll /etc/libvirt/qemu/
total 28
drwxr-xr-x 3 root root 4096 Dec 31 00:18 ./
drwxr-xr-x 7 root root 4096 Dec 29 20:24 ../
-rw------- 1 root root 7673 Dec 29 21:36 centos7-mooreyxia.xml
drwxr-xr-x 3 root root 4096 Dec 29 19:10 networks/
-rw------- 1 root root 7078 Dec 30 22:44 rocky8.xml
#查看XML配置文件
[root@ubuntu2204 ~]#cat /etc/libvirt/qemu/centos7-mooreyxia.xml
<!--
WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE
OVERWRITTEN AND LOST. Changes to this xml configuration should be made using:
virsh edit centos7-mooreyxia
or other application using the libvirt API.
-->
<domain type='kvm'>
<name>centos7-mooreyxia</name>
<uuid>3492dccc-468d-4fe9-8ec8-b4a251dccf00</uuid>
<metadata>
<libosinfo:libosinfo xmlns:libosinfo="http://libosinfo.org/xmlns/libvirt/domain/1.0">
<libosinfo:os id="http://centos.org/centos/7.0"/>
</libosinfo:libosinfo>
</metadata>
<memory unit='KiB'>2097152</memory>
<currentMemory unit='KiB'>2097152</currentMemory>
<vcpu placement='static'>2</vcpu>
<os>
<type arch='x86_64' machine='pc-q35-6.2'>hvm</type>
<boot dev='hd'/>
</os>
<features>
<acpi/>
<apic/>
<vmport state='off'/>
</features>
<cpu mode='host-passthrough' check='none' migratable='on'/>
<clock offset='utc'>
<timer name='rtc' tickpolicy='catchup'/>
<timer name='pit' tickpolicy='delay'/>
<timer name='hpet' present='no'/>
</clock>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>destroy</on_crash>
<pm>
<suspend-to-mem enabled='no'/>
<suspend-to-disk enabled='no'/>
</pm>
<devices>
<emulator>/usr/bin/qemu-system-x86_64</emulator>
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2' discard='unmap'/>
<source file='/var/lib/libvirt/images/centos7-mooreyxia.qcow2'/>
<target dev='vda' bus='virtio'/>
<address type='pci' domain='0x0000' bus='0x04' slot='0x00' function='0x0'/>
</disk>
<disk type='file' device='cdrom'>
<driver name='qemu' type='raw'/>
...
勾选开机启动项看看会发生什么
#设置虚拟机随宿主机启动而自动启动,本质就是在下面目录生成软链接
-------------------------------------
#图形界面操作可以用命令代替
virsh autostart centos7-mooreyxia
----------------------------------------
#生成了autostart/centos7-mooreyxia.xml 软链接指向虚拟机配置文件
[root@ubuntu2204 ~]#ll /etc/libvirt/qemu/
total 32
drwxr-xr-x 4 root root 4096 Jan 1 12:24 ./
drwxr-xr-x 7 root root 4096 Dec 29 20:24 ../
drwxr-xr-x 2 root root 4096 Jan 1 12:24 autostart/
-rw------- 1 root root 7673 Jan 1 12:24 centos7-mooreyxia.xml
drwxr-xr-x 3 root root 4096 Dec 29 19:10 networks/
-rw------- 1 root root 7078 Dec 30 22:44 rocky8.xml
[root@ubuntu2204 ~]#ll /etc/libvirt/qemu/autostart/
total 8
drwxr-xr-x 2 root root 4096 Jan 1 12:24 ./
drwxr-xr-x 4 root root 4096 Jan 1 12:24 ../
lrwxrwxrwx 1 root root 39 Jan 1 12:24 centos7-mooreyxia.xml -> /etc/libvirt/qemu/centos7-mooreyxia.xml
案例:删除虚拟机配置
[root@centos8 ~]#virsh list --all
Id Name State
----------------------------------------------------
2 centos8 running
- centos7 shut off
- centos8-vm2 shut off
- centos8-vm3 shut off
- centos8-vm4 shut off
- Win_2008_r2-x86_64 shut off
#删除虚拟机配置,但不删除磁盘文件
[root@centos8 ~]#virsh undefine centos8-vm4
Domain centos8-vm4 has been undefined
[root@centos8 ~]#virsh list --all
Id Name State
----------------------------------------------------
2 centos8 running
- centos7 shut off
- centos8-vm2 shut off
- centos8-vm3 shut off
- Win_2008_r2-x86_64 shut off
#对应虚拟机xml的配置文件被删除
[root@centos8 ~]#ll /etc/libvirt/qemu/
total 28
drwxr-xr-x 2 root root 25 Sep 17 18:54 autostart
-rw------- 1 root root 3618 Sep 17 15:14 centos7.xml
-rw------- 1 root root 3673 Sep 13 22:43 centos8-vm2.xml
-rw------- 1 root root 3601 Sep 13 22:42 centos8-vm3.xml
-rw------- 1 root root 6024 Sep 17 15:47 centos8.xml
drwx------ 3 root root 42 Sep 13 19:03 networks
-rw------- 1 root root 5369 Sep 17 11:31 Win_2008_r2-x86_64.xml
#对应的磁盘文件并没有删除
[root@centos8 ~]#ls /var/lib/libvirt/images/
centos7.qcow2 centos8.qcow2 centos8-vm2.qcow2 centos8-vm3.qcow2 centos8-vm4.qcow2 Windows-2008_r2-x86_64.qcow2
--------------------------------------------------------
#删除虚拟机包括磁盘文件
[root@ubuntu2004 ~]#virsh undefine k8s-node-05 --remove-all-storage
Domain k8s-node-05 has been undefined
Volume 'vda'(/var/lib/libvirt/images/k8s-node-05.qcow2) removed.
案例:冷迁移虚拟机
将一个宿主机的虚拟机迁移到另一台宿主机,注意: 不支持Ubuntu和Rocky8宿主机之间迁移
对下面两个文件进行迁移即可
- /etc/libvirt/qemu/xxx.xml
- /var/lib/libvirt/images/xxx.qcow2
#在一台目标宿主机安装相关虚拟化软件
[root@ubuntu2004 ~]#apt update
[root@ubuntu2004 ~]#apt -y install qemu-kvm virt-manager libvirt-daemon-system
#在源宿主机查看虚拟机的相关文件
[root@ubuntu2004 ~]#virsh dumpxml --domain rocky8-template
......
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2'/>
<source file='/var/lib/libvirt/images/rocky8-template.qcow2'/>
.....
#或者#查看块设备
[root@ubuntu2004 ~]#virsh domblklist rocky8-template
Target Source
------------------------------------------------
vda /var/lib/libvirt/images/rocky8-template.qcow2
vdb -
#复制源宿主机上的虚拟机的两个文件到目标宿主机
[root@ubuntu2004 ~]#scp /etc/libvirt/qemu/rocky8-template.xml
10.0.0.101:/etc/libvirt/qemu/
[root@ubuntu2004 ~]#scp /var/lib/libvirt/images/rocky8-template.qcow2
10.0.0.101:/var/lib/libvirt/images
#在目标宿主机不重启服务无法看到新的虚拟机
[root@ubuntu2004 ~]#virsh list --all
[root@ubuntu2004 ~]#systemctl restart libvirtd
#在目标宿主机重启服务后看到新的虚拟机
[root@ubuntu2004 ~]#virsh list --all
Id Name State
----------------------------------
rocky8-template shut off
我是moore,新年快乐!!!