Loading

virsh domxxx命令

1. 摘要信息类

1.1 dominfo 获取配置等摘要信息

[root@hyperhost ~]# virsh dominfo --domain centos7.6
Id:             1
Name:           centos7.6
UUID:           11377dd6-bb49-4b9d-8e36-773787858871
OS Type:        hvm        # 表示硬件辅助虚拟化
State:          running
CPU(s):         2
CPU time:       503.1s
Max memory:     4194304 KiB
Used memory:    4194304 KiB
Persistent:     yes        # 是否持久化
Autostart:      disable    # 是否自动启动
Managed save:   no         # 是否处于保存状态保存
Security model: none
Security DOI:   0

# 当虚拟机处于save状态时,State=shut off, Managed save=yes

1.2 domstate 获取虚拟机状态信息及原因

option:
--domain <string>  	虚拟机name、id或者uuid
--reason            输出处于此状态的原因

# 不加--reason:
[root@hyperhost-1 ~]# virsh domstate --domain rhel7
running

# 正常启动的虚拟机
[root@hyperhost ~]# virsh domstate --domain centos7.6 --reason
running (booted)

# 关机的虚拟机
[root@hyperhost ~]# virsh domstate --domain small --reason
shut off (unknown)

# 从save状态恢复的虚拟机
[root@hyperhost ~]# virsh domstate --domain 2 --reason 
running (restored)

关于对虚拟机状态机的描述,在libvirt官方文档中可以找到定义和参数取值,此处举例如下:

virDomainState包括以下:

state码 state 描述
0 no state 无状态
1 running 运行中
2 the domain is blocked on resource 阻塞
3 paused by user 被用户挂起
4 the domain is being shut down 正在关闭
5 the domain is shut off 已经关机
6 the domain is crashed 崩溃
7 suspended by guest power management 被电源管理挂起

如果state.state=5,则state.reason的取值由virDomainShutoffReason定义:

reason码 reason 描述
0 unknown 未知原因
1 normal shutdown 正常关机
2 forced poweroff 强制关机
3 crashed 奔溃
4 migrated to another host 迁移至其他主机
5 saved to a file 被保存
6 domain failed to start 启动失败
7 restored from a snapshot which was taken while domain was shutoff 从一个已经关闭的域的快照恢复的虚拟机

1.3 domuuid\ domid\ domname查找虚拟机的uuid\id\name

# 通过虚拟机的id或者name查找虚拟机的uuid
[root@hyperhost ~]# virsh domuuid 1
11377dd6-bb49-4b9d-8e36-773787858871
[root@hyperhost ~]# virsh domuuid centos7.6
11377dd6-bb49-4b9d-8e36-773787858871

# 通过虚拟机的uuid或者name查找虚拟机的id
[root@hyperhost ~]# virsh domid centos7.6
1
[root@hyperhost ~]# virsh domid 11377dd6-bb49-4b9d-8e36-773787858871
1

# 通过虚拟机的id或者uuid查找虚拟机的name
[root@hyperhost ~]# virsh domname 1
centos7.6
[root@hyperhost ~]# virsh domname 11377dd6-bb49-4b9d-8e36-773787858871
centos7.6

1.4 domdisplay 获得虚拟机vnc\spice\rdb地址

--domain <string>  	实例的name, id 或者 uuid
--include-password  	在返回的url中包含密码
--type <string>  		可选的类型有:spice\vnc\rdp
--all            		显示所有可能得display地址

[root@hyperhost ~]# virsh domdisplay centos7.6
spice://127.0.0.1:5900
# spice虚拟桌面传输协议,需要给vm配置display-spice设备。
# 配置完成后,可以使用virt-viewer访问,具体如下:
# 在桌面环境下安装:yum install virt-viewer -y
# 使用命令访问:virt-viewer --id 1

--include-password参数:
# 如果为spice设置了密码,可以使用--include-password参数将密码带出来:
[root@hyperhost-1 ~]# virsh domdisplay --include-password centos7.6
spice://127.0.0.1:5900?password=12345

1.5 domstats 输出主机上一个/多个/全部虚拟机统计信息(CPU、内存、网络接口、块设备)

domstats参数很多,回显的内容与其他命令很类似。domstats最大的好处是可以批量显示一个主机上所有实例的某些统计信息,便于快速定位。例如:
场景一:排查主机上是否存在异常流量的虚拟机:
virsh domstats --list-active --interface
场景二:排查主机上占用物理CPU高的虚拟机:
virsh domstat --cpu-total

使用方法:

virsh domstate       --> 返回该主机上全部虚拟机的全部统计信息
virsh domstate --vcpu  --> 返回该主机上全部虚拟机的vcpu的统计信息
virsh domstate <ID>  --> 返回指定ID的虚拟机的全部统计信息
virsh domstate <ID> --vcpu  --> 返回指定ID的虚拟机的vcpu的统计信息

domstats参数众多,可以分为两类:

  • 指定要列出的统计指标:
无参数            报告包括以下全部统计信息
--state          报告虚拟机运行状态及原因,类似于virsh domstate guest
--cpu-total      报告虚拟机占用主机cpu的运行时间
--vcpu           报告虚拟机的vcpu个数、状态、cpu运行时间
--balloon        报告虚拟机内存(内存气球)统计信息
--interface      报告虚拟机所有网卡的统计信息
--block          报告虚拟机所有块设备的统计信息
--perf           报告虚拟机性能事件统计信息(perf event)
  • 指定要列出何种状态的虚拟机:
无参数              报告主机上全部虚拟机的统计信息
<domain>           指定domain ID\name\UUID
--list-active      列出活跃状态的虚拟机统计信息
--list-inactive    列出非活跃状态的虚拟机统计信息
--list-persistent  列出持久化虚拟机统计信息
--list-transient   列出临时虚拟机统计信息
--list-running     列出正在运行的虚拟机统计信息
--list-paused      列出挂起的虚拟机统计信息
--list-shutoff     列出关机的虚拟机统计信息
--list-other       列出处于other状态的虚拟机统计信息

举例如下:

# --state参数,返回虚拟机状态及处于该状态的原因
# 这里的状态码和原因码参考上文的状态机介绍
[root@hyperhost ~]# virsh domstats --state 1
Domain: 'small'
  state.state=1
  state.reason=1

# --cpu-total参数,等价于 virsh cpu-stats --total 1,单位是纳秒,10^-9s
[root@hyperhost-1 ~]# virsh domstats --cpu-total 1
Domain: 'small'
  cpu.time=20304889731
  cpu.user=2830000000
  cpu.system=8900000000

# --vcpu参数,报告vcpu信息,注意与上例区分。
# 与virsh vcpuinfo --pretty 1 非常类似。
[root@hyperhost-1 ~]# virsh domstats --vcpu 1
Domain: 'small'
  vcpu.current=1
  vcpu.maximum=1
  vcpu.0.state=1
  vcpu.0.time=35520000000    # 单位纳秒
  vcpu.0.wait=0

# --balloon参数,报告内存信息,如果balloon.maximum > balloon.current,说明允许动态调整内存,也就是内存气球技术。
# 全部统计信息值释义参看man virsh.
[root@hyperhost-1 ~]# virsh domstats 1 --balloon
Domain: 'small'
  balloon.current=1048576
  balloon.maximum=1048576
  balloon.swap_in=0
  balloon.last-update=1694597008
  balloon.rss=374188

# --interface参数,报告网络接口信息
# 与 virsh domifstat --domain 1 --interface vnet0 非常相似
[root@hyperhost-1 ~]# virsh domstats 1 --interface
Domain: 'small'
  net.count=1
  net.0.name=vnet0
  net.0.rx.bytes=101243
  net.0.rx.pkts=1785
  net.0.rx.errs=0
  net.0.rx.drop=0
  net.0.tx.bytes=28547
  net.0.tx.pkts=370
  net.0.tx.errs=0
  net.0.tx.drop=0

# --block参数,报告虚拟机每个块设备的统计信息,包括容量和读写情况
# 该命令与virsh domblkinfo rhel7 --device vda 和 命令virsh domblkstat rhel7 --human输出的结果非常相似:
[root@hyperhost-1 ~]# virsh domstats --block rhel7 
Domain: 'rhel7'
  block.count=2
  block.0.name=vda
  block.0.path=/var/lib/libvirt/images/small.img
  block.0.rd.reqs=134
  block.0.rd.bytes=3448832
  block.0.rd.times=133278283
  block.0.wr.reqs=176
  block.0.wr.bytes=1058816
  block.0.wr.times=320043459
  block.0.fl.reqs=56                # flush_operations
  block.0.fl.times=71601324         # flush_total_times
  block.0.allocation=375127552
  block.0.capacity=10737418240
  block.0.physical=375128064
  block.1.name=hda
  block.1.rd.reqs=0
  block.1.rd.bytes=0
  block.1.rd.times=0
  block.1.wr.reqs=0
  block.1.wr.bytes=0
  block.1.wr.times=0
  block.1.fl.reqs=0
  block.1.fl.times=0
  block.1.allocation=0
  block.1.physical=0

通过上例可以看出,domstats命令输出的内容,其他命令都可以输出,并且可能更加全面,更具有可读性。但是domstats最佳使用的场景是什么参数都不加,一次性得到主机上所有虚拟机的全部统计信息,便于管理员使用分析工具快速提取信息:

[root@hyperhost-1 ~]# virsh domstats 1
Domain: 'small'
  state.state=1
  state.reason=1
  cpu.time=72084113330
  ……
  balloon.current=1048576
  ……
  vcpu.current=1
  ……
  net.count=1
  ……
  block.count=2
  ……

1.6 domrename 为处于不活跃状态的虚拟机重命名

  • 如何定义活跃/非活跃:
    • 活跃状态:running、paused
    • 非活跃状态:shutdown、saved

有一个快速区分的方法:

# 使用virsh list显示的都是活跃的虚拟机:
# 另外一个分辨方法,如果id被保留,就是active,如果id显示-,就是inactive:
[root@hyperhost-1 ~]# virsh list
 Id    Name                           State
----------------------------------------------------
 2     small                          paused
 3     rhel7                          running

# 使用virsh list --inactive显示的都是非活跃的虚拟机
[root@hyperhost-1 ~]# virsh list --inactive
 Id    Name                           State
----------------------------------------------------
 -     small                          shut off  --> 关机
 -     rhel7                          shut off  --> saved

参数和使用方法:

--domain <string>  		虚拟机的name或者uuid,处于关机的虚拟机没有id
--new-name <string>  	新名称

# 如果为正在运行的虚拟机重命名则报错:
[root@hyperhost-1 ~]# virsh domrename --new-name rhel7 2
error: Requested operation is not valid: cannot rename active domain

# 为关机的虚拟机重命名:
[root@hyperhost-1 ~]# virsh shutdown small
Domain small is being shutdown

[root@hyperhost-1 ~]# virsh domrename --domain small --new-name rhel7
Domain successfully renamed

[root@hyperhost-1 ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 -     rhel7                          shut off

1.7 domtime 获取/设置虚拟机时间

前置条件:获取/设置时间需要虚拟机安装QEMU guest agent,否则报错:

[root@hyperhost-1 ~]# virsh domtime rhel7
error: Guest agent is not responding: QEMU guest agent is not connected

这里获取/设置的都是虚拟机的UTC时间,此方法无法设置guest OS的时区。区分UTC/CST/RTC时间使用命令timedatectl:

[root@hyperhost-1 ~]# timedatectl
      Local time: Thu 2023-09-14 18:47:47 CST      # 世界协调时间
  Universal time: Thu 2023-09-14 10:47:47 UTC      # 各个时区的地方时间
        RTC time: Thu 2023-09-14 10:47:45          # 硬件时间
       Time zone: Asia/Shanghai (CST, +0800)

参数和使用方法:

--domain <string>  		虚拟机的name\uuid\id

# 获取时间:
无参数         		    UTC时间的Unix时间戳
--pretty         		以友好格式输出虚拟机时间

# 设置时间:
--time <number>  	设置虚拟机UTC时间内为指定的时间,用Unix时间戳表示
--now            	设置虚拟机UTC时间为执行此命令时,主机的UTC时间
--sync           	当虚拟机UTC时间和RTC时间不一致时,将虚拟机UTC时间置为RTC时间

# 获取虚拟机UTC时间:
# 无参数时,获取虚拟机的Unix时间戳:
[root@hyperhost ~]# virsh domtime centos7.6
Time: 1655865135

# 获取虚拟机UTC时间,使用--pretty以友好可读格式:
[root@hyperhost ~]# virsh domtime centos7.6 --pretty
Time: 2022-06-22 02:32:41

# 设置虚拟机UTC时间:
# 设置虚拟机UTC时间内为指定的时间,用Unix时间戳表示:
[root@hyperhost ~]# virsh domtime centos7.6 --time "1655865135"

# 设置虚拟机UTC时间为执行此命令时,主机的UTC时间:
[root@hyperhost ~]# virsh domtime centos7.6 --now

# 当虚拟机的UTC和RTC不一致时,将虚拟机UTC时间置为RTC时间
[root@hyperhost ~]# virsh domtime centos7.6 --sync

# 对于 --sync 参数:
# 例如,在虚拟机中,错误的设置了一个时间,但是硬件时间RTC是正确的:
[root@guest ~]# date -s "2001-1-1"
[root@guest ~]# timedatectl
      Local time: Thu 2001-01-01 00:00:25 CST
  Universal time: Thu 2000-12-31 16:00:25 UTC
        RTC time: Thu 2023-09-14 11:25:45
       Time zone: Asia/Shanghai (CST, +0800)
# 使用 --sync参数,可以将UTC时间和RTC时间同步:
[root@hyperhost ~]# virsh domtime centos7.6 --sync
# 虚拟机内:
[root@guest ~]# timedatectl
      Local time: Thu 2023-09-14 19:29:35 CST
  Universal time: Thu 2023-09-14 11:29:35 UTC
        RTC time: Thu 2023-09-14 11:29:35

2. 网卡信息类

2.1 domifaddr 查看虚拟机接口,ip地址,mac地址等信息

相关参数:

option:
--domain					虚拟机name, id, uuid
--interface <string>  		指定网络接口名称,缺省列出全部接口
--full           			总是显示name和mac,仅在agent模式下有用
--source <string>  			数据来源: 'lease', 'arp', or 'agent'

# 关于--source可选的三个源:
lease        缺省的,lease表示DHCP的租约。从libvirt网络维护的dhcp-lease信息读取。
arp          读取主机的arp表。
agent        使用qga,从虚拟机上读取网络信息。

第一种方式:读取dhcp-lease租约

[root@hyperhost-1 ~]# virsh domifaddr --domain rhel7 --source lease
 Name       MAC address          Protocol     Address
-------------------------------------------------------------------------------
 vnet0      52:54:00:ea:8b:cd    ipv4         192.168.122.84/24

# 从这里可以看到libvirt默认网桥维护的dhcp租约:
[root@hyperhost-1 ~]# virsh net-dhcp-leases default
 Expiry Time          MAC address        Protocol  IP address                Hostname        Client ID or DUID
-------------------------------------------------------------------------------------------------------------------
 2023-09-21 11:05:24  52:54:00:ea:8b:cd  ipv4      192.168.122.84/24         localhost       -

# 文件位置:
[root@hyperhost-1 ~]# cat /var/lib/libvirt/dnsmasq/virbr0.status
[
  {
    "ip-address": "192.168.122.84",
    "mac-address": "52:54:00:ea:8b:cd",
    "hostname": "localhost",
    "expiry-time": 1695265524
  }
]

第二种方式:读取主机arp表

[root@hyperhost-1 ~]# virsh domifaddr --domain rhel7 --source arp
 Name       MAC address          Protocol     Address
-------------------------------------------------------------------------------
 vnet0      52:54:00:ea:8b:cd    ipv4         192.168.122.84/0

[root@hyperhost-1 ~]# arp
Address                  HWtype  HWaddress           Flags Mask            Iface
192.168.122.84           ether   52:54:00:ea:8b:cd   C                     virbr0

第三种方式:通过qemu guest agent直接读取虚拟机网络信息

# 如果没有安装agent则报错:
[root@hyperhost-1 ~]# virsh domifaddr --domain rhel7 --source agent
error: Failed to query for interfaces addresses
error: Guest agent is not responding: QEMU guest agent is not connected

# 如果安装了agent:
[root@hyperhost ~]# virsh domifaddr --domain centos7.6 --source agent
 Name       MAC address          Protocol     Address
-------------------------------------------------------------------------------
 lo         00:00:00:00:00:00    ipv4         127.0.0.1/8
 -          -                    ipv6         ::1/128
 eth0       52:54:00:74:4d:5d    ipv4         192.168.122.25/24
 -          -                    ipv6         fe80::fd08:7618:40da:33b3/64

# --full参数的作用:列出完整信息,及时是重复的mac地址、接口信息
[root@hyperhost ~]# virsh domifaddr --domain centos7.6 --source agent --full
 Name       MAC address          Protocol     Address
-------------------------------------------------------------------------------
 lo         00:00:00:00:00:00    ipv4         127.0.0.1/8
 lo         00:00:00:00:00:00    ipv6         ::1/128
 eth0       52:54:00:74:4d:5d    ipv4         192.168.122.25/24
 eth0       52:54:00:74:4d:5d    ipv6         fe80::fd08:7618:40da:33b3/64

三种方式的区别:

1、lease只能应用于虚拟机使用libvirt维护的网络的方式,且地址为自动获取。OpenStack环境则无法获取;
2、arp方式,在OpenStack环境下,如果是provide vlan网络则无法回显地址信息;
3、agent方式,只要虚拟机装了qga,无论是OpenStack还是libvirt,都可以获取地址信息,并且可以获取主机上明确的网卡名称。

option:
--domain						指定guest name, id, uuid
--interface <string>  		指定网络接口名称,或者mac地址
--config         				Get persistent interface state

# 应该与domifaddr联合使用,interface指定的名称应为在宿主机上显示的名称:
[root@hyperhost ~]# virsh domif-getlink --domain centos7.6 --interface vnet0
vnet0 up

[root@hyperhost ~]# virsh domif-getlink --domain centos7.6 --interface 52:54:00:74:4d:5d
52:54:00:74:4d:5d up

# 使用--config,则接口应是mac,使用vent0报错,原因未知?
[root@hyperhost ~]# virsh domif-getlink centos7.6 52:54:00:74:4d:5d --config
52:54:00:74:4d:5d up
option:
--domain						指定guest name, id, uuid
--interface <string>  		指定网络接口名称,或者mac地址
--state <string>  			up或者是down
--config         				下次重启生效,缺省为立即生效

# 将虚拟机接口down掉,立即生效:
[root@hyperhost ~]# virsh domif-setlink --domain centos7.6 --interface vnet0 --state down
Device updated successfully

# 此时,xml文件显示,该网络接口设备被置为down的状态:
    <interface type='network'>
      <mac address='52:54:00:74:4d:5d'/>
      <source network='default' bridge='virbr0'/>
      <target dev='vnet0'/>
      <model type='virtio'/>
      <link state='down'/>				# 状态为down
      <alias name='net0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
    </interface>

# 此时,如果再虚拟机中启动了qemu-guest-agent,那么虚拟机网卡会被down掉;
# 如果没有启动qemu-guest-agent,那ip a看到网卡配置还在,状态为down

# 启动网卡:
[root@hyperhost ~]# virsh domif-setlink --domain centos7.6 --interface vnet0 --state up
Device updated successfully

2.4 domiflist 获取虚拟机网卡使用模式,网络来源,mac地址

option:
--domain						指定guest name, id, uuid
--inactive       				获取未在激活状态的网卡信息,而非正在运行的

[root@hyperhost ~]# virsh domiflist --domain centos7.6
Interface  Type       Source     Model       MAC
-------------------------------------------------------
vnet0      network    default    virtio      52:54:00:74:4d:5d


[root@hyperhost ~]# virsh domiflist --domain centos7.6 --inactive
Interface  Type       Source     Model       MAC
-------------------------------------------------------
-          network    default    virtio      52:54:00:74:4d:5d

2.5 domifstat 获取虚拟机网卡收发包统计信息

option:
--domain						指定guest name, id, uuid
--interface <string>  		指定网络接口名称,或者mac地址


# 指定接口名称 vnet0:
[root@hyperhost ~]# virsh domifstat centos7.6 --interface vnet0
vnet0 rx_bytes 379410
vnet0 rx_packets 7281
vnet0 rx_errs 0
vnet0 rx_drop 0
vnet0 tx_bytes 2590
vnet0 tx_packets 33
vnet0 tx_errs 0
vnet0 tx_drop 0


# 如果使用ifconfig 查看vnet0设备的收发情况,发现domifstat输出的结果与ifconfig相反:
[root@hyperhost ~]# ifconfig vnet0
vnet0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether fe:54:00:74:4d:5d  txqueuelen 1000  (Ethernet)
        RX packets 33  bytes 2590 (2.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 7279  bytes 379306 (370.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0


# 其实,系统的vnet看到的是接在bridge一端的tap设备,mac地址为fe:54:00:74:4d:5d
# 虚拟机内部的网卡接口mac地址是52:54:00:74:4d:5d,与vnet0组成一对tap设备;
# 系统中vnet0发出的流量流入虚拟机网卡接口eth0,自然二者流量统计是相反的;
# 只不过domifstat使用了vent0作为虚拟机接口名称,因为tap设备本来就是成对出现的,与系统中看到的 vnet0 接口是“一条链路的两端”

# 或者:
[root@hyperhost ~]# virsh domifstat --domain centos7.6 --interface 52:54:00:74:4d:5d
52:54:00:74:4d:5d rx_bytes 849036
...

2.6 domiftune 虚拟机 QoS流量整型

option:
--domain						指定guest name, id, uuid
--interface <string>			指定网络接口名称,或者mac地址	
--inbound <string>  			控制流入流量,可以传入最多四个位置参数,分别是:average,peak,burst,floor
--outbound <string>  		控制流出流量,可以最多传入三个位置参数,分别是:average,peak,burst
--config         				affect next boot
--live           				affect running domain
--current        				affect current domain

# 查看当前QoS情况:
[root@hyperhost ~]# virsh domiftune --domain centos7.6 --interface 52:54:00:74:4d:5d
inbound.average: 0
inbound.peak   : 0
inbound.burst  : 0
inbound.floor  : 0
outbound.average: 0
outbound.peak  : 0
outbound.burst : 0

# 设置:
[root@hyperhost ~]# virsh domiftune --domain centos7.6 --interface 52:54:00:74:4d:5d --inbound 500,700,600 --live

# 查看xml文件:
[root@hyperhost ~]# virsh dumpxml centos7.6
···
    <interface type='network'>
      <mac address='52:54:00:74:4d:5d'/>
      <source network='default' bridge='virbr0'/>
      <bandwidth>
        <inbound average='500' peak='700' burst='600'/>
      </bandwidth>
      <target dev='vnet0'/>
      <model type='virtio'/>
      <alias name='net0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
    </interface>
···

# 默认的default网络不支持floor属性:
[root@hyperhost ~]# virsh domiftune --domain centos7.6 --interface 52:54:00:74:4d:5d 0,0,0,100 --live
error: Unable to set interface parameters
error: Operation not supported: Invalid use of 'floor' on interface with MAC address 52:54:00:74:4d:5d - network 'default' has no inbound QoS set

# 使用iperf3 粗略测试一下:
# 虚拟设置为服务端:
[root@guest ~]# iperf3 -s
# 宿主机设置为客户端:
[root@hyperhost ~]# iperf3 -c 192.168.122.25
···
[ ID] Interval           Transfer     Bandwidth       Retr
[  4]   0.00-10.00  sec  5.43 MBytes  4.55 Mbits/sec    0             sender
[  4]   0.00-10.00  sec  5.12 MBytes  4.30 Mbits/sec                  receiver
···

3. 块设备信息类

3.1 domblklist 列出虚拟机挂载的块设备

option:
--domain <string>  		guest的name、id、uuid
--inactive       			get inactive rather than running configuration
--details        			打印设备类型、磁盘类型

[root@hyperhost ~]# virsh domblklist --domain centos7.6
Target     Source
------------------------------------------------
vda        /var/lib/libvirt/images/centos7.6.qcow2
vdb        /var/lib/libvirt/images/centos7.6-1.qcow2
hda        /var/lib/libvirt/images/CentOS-7-x86_64-Minimal-1810.iso

[root@hyperhost ~]# virsh domblklist --domain centos7.6 --details
Type       Device     Target     Source
------------------------------------------------
file       disk       vda        /var/lib/libvirt/images/centos7.6.qcow2
file       disk       vdb        /var/lib/libvirt/images/centos7.6-1.qcow2
file       cdrom      hda        /var/lib/libvirt/images/CentOS-7-x86_64-Minimal-1810.iso

3.2 domblkinfo 列出设备信息

option:
--domain <string>  		虚拟机的name,id,uuid
--device <string>  		指定设备名称,或者使用--all显示全部
--human          			友好可读
--all            			全部设备

[root@hyperhost ~]# virsh domblkinfo --domain centos7.6  --human  --all
Target     Capacity        Allocation      Physical
-----------------------------------------------------
vda        20.000 GiB      20.003 GiB      20.003 GiB
vdb        1.000 GiB       1.000 GiB       1.000 GiB
hda        918.000 MiB     918.000 MiB     918.000 MiB


[root@hyperhost ~]# virsh domblkinfo --domain centos7.6  --device vda
Capacity:       21474836480				# 单位byte,是虚拟机内部看到的大小
Allocation:     21478379520
Physical:       21478375424				# 占用宿主机的大小

[root@hyperhost ~]# ll /var/lib/libvirt/images/centos7.6.qcow2
-rw------- 1 qemu qemu 21478375424 Jun 22 12:10 /var/lib/libvirt/images/centos7.6.qcow2

[root@hyperhost ~]# du -b /var/lib/libvirt/images/centos7.6.qcow2
21478375424	/var/lib/libvirt/images/centos7.6.qcow2

3.3 domblkerror 显示块设备的error

官方描述:
Show errors on block devices. This command usually comes handy when domstate command says that a domain was paused due to I/O error. The domblkerror command lists all block devices in error state and the error seen on each of them.

如果使用domstate发现虚拟机因为IO问题处于挂起状态,可以使用该命令进一步排查。

[root@hyperhost ~]# virsh domblkerror --domain centos7.6
No errors found

3.4 domblkstat 显示虚拟机设备读写统计信息

option
--domain] <string>	虚拟机name id uuid
--device <string>		指定设备名称
--human				友好可读

[root@hyperhost ~]# virsh domblkstat --device vda centos7.6
vda rd_req 5645
vda rd_bytes 127312896
vda wr_req 614
vda wr_bytes 10207232
vda flush_operations 303
vda rd_total_times 4176908705
vda wr_total_times 1588961304
vda flush_total_times 819531072

[root@hyperhost ~]# virsh domblkstat --device vda centos7.6 --human
Device: vda
 number of read operations:      5645					# 读操作次数
 number of bytes read:           127312896				# 读操作字节数
 number of write operations:     614						# 写操作次数
 number of bytes written:        10207232				# 写操作字节数
 number of flush operations:     303						# flush次数
 total duration of reads (ns):   4176908705				# 持续时间,读
 total duration of writes (ns):  1588961304				# 持续时间,写
 total duration of flushes (ns): 819531072				# 持续时间,刷入

3.5 domblkthreshold 设置阈值

option:
--domain <string>  		domain name, id or uuid
--dev <string>  			device to set threshold for
--threshold <number>  	threshold as a scaled number (by default bytes)

4. 文件系统类

4.1 domfsinfo 获取虚拟机文件系统挂载情况,需要虚拟机启动qemu-ga

OPTIONS
--domain <string>  domain name, id or uuid

[root@hyperhost ~]# virsh domfsinfo --domain centos7.6
Mountpoint                           Name     Type     Target
-------------------------------------------------------------------
/                                    dm-0     xfs      vda
/data                                vdb1     xfs      vdb
/boot                                vda1     xfs      vda

4.2 domfsfreeze 冻结文件系统

option
--domain <string>  		虚拟机 name, id or uuid
--mountpoint <string>  	要冻结的文件系统挂载点

[root@hyperhost ~]# virsh domfsfreeze --domain centos7.6 --mountpoint /data
error: Unable to freeze filesystems
error: internal error: unable to execute QEMU agent command 'guest-fsfreeze-freeze-list': The command guest-fsfreeze-freeze-list has been disabled for this instance

# 无法执行qemu agent command 'guest-fsfreeze-freeze-list'

4.3 domfsthaw 解冻文件系统

option
--domain <string>			虚拟机 name, id or uuid
--mountpoint <string>	要解冻的文件系统挂载点

4.4 domfstrim 唤起对指定挂载点的文件系统的trim

option
--domain <string>			虚拟机 name, id or uuid
--minimum <number>  		Just a hint to ignore contiguous free ranges smaller than this (Bytes)
--mountpoint <string>  	which mount point to trim

[root@hyperhost ~]# virsh domfstrim --domain centos7.6 --mountpoint /
error: Unable to invoke fstrim
error: argument unsupported: Specifying mount point is not supported for now			指定的挂载点不支持,可能是虚拟机将其识别为机械盘而非ssd

[root@guest ~]# cat /sys/block/vdb/queue/rotational
1				# 虚拟机中,vdb是一块“可旋转”的磁盘,是机械盘而非ssd盘,所以无法做trim
posted @ 2023-09-11 18:29  wang272  阅读(349)  评论(0编辑  收藏  举报