43-KVM虚拟化-利用libvirt服务进行KVM虚拟机管理

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了43-KVM虚拟化-利用libvirt服务进行KVM虚拟机管理相关的知识,希望对你有一定的参考价值。

半虚拟化驱动virtio的工作原理

为了提高内存、硬盘、网络的性能,需要支持半虚拟化

43-KVM虚拟化-利用libvirt服务进行KVM虚拟机管理_libvirtd

virtio 是一种 I/O 半虚拟化解决方案,是一套通用 I/O 设备虚拟化的程序,是对半虚拟化 Hypervisor 中的一组通用 I/O 设备的抽象,提供了一套上层应用与各 Hypervisor 虚拟化设备(KVM,Xen,VMware等)之间的通信框架和编程接口,减少跨平台所带来的兼容性问题,大大提高驱动程序开发效率,Windows 系统需要单独安装virtio驱 动,Linux系统自带virtio驱动


libvirt 架构

43-KVM虚拟化-利用libvirt服务进行KVM虚拟机管理_虚拟化_02

如果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是一个图形化工具,主要功能:

  • 定义和创建虚拟机
  • 硬件管理
  • 性能监视
  • 控制台
  • 在线和离线迁移
  • 虚拟机的保存和恢复、暂停和继续、关闭和启动

43-KVM虚拟化-利用libvirt服务进行KVM虚拟机管理_libvirtd_03

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 uuids 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以上是关于43-KVM虚拟化-利用libvirt服务进行KVM虚拟机管理的主要内容,如果未能解决你的问题,请参考以下文章

手动编译安装Libvirt之后利用systemctl管理libvirtd服务

虚拟化技术—基础

kvm虚拟机管理

kvm虚拟机管理

KVM虚拟机快照研究

通过python获取kvm虚拟机的监控信息(基于libvirt API)