最後更新: 2024-09-27
目錄
- virsh 的 qemu-monitor-command
- runtime change vnc password
- Replace Disk(hotplug)
指令詳見: QEMU Monitor CLI
virsh 的 qemu-monitor-command
Syntax
qemu-monitor-command <domain> [--hmp] [--pretty] [--return-value] {[--cmd] <string>}...
- --hmp # command is in human monitor protocol
- --pretty # pretty-print any qemu monitor protocol output
Help
# 查看 QEMU Monitor 有的指令
qemu-monitor-command Domain_Name --hmp help
acl_add aclname match allow|deny [index] -- add a match rule to the access control list acl_policy aclname allow|deny -- set default access control list policy acl_remove aclname match -- remove a match rule from the access control list acl_reset aclname -- reset the access control list acl_show aclname -- list rules in the access control list balloon target -- request VM to change its memory allocation (in MB) block_passwd block_passwd device password -- set the password of encrypted block devices block_resize device size -- resize a block image ...
qemu-monitor-command VM-NAME --hmp help info
info
# 查看 info 支援什麼 cmd
qemu-monitor-command Domain_Name --hmp info
info balloon -- show balloon information info block -- show the block devices info blockstats -- show block device statistics info capture -- show capture information info chardev -- show the character devices info cpus -- show infos for each CPU ...
Balloon
virsh # qemu-monitor-command c6a --hmp info balloon
balloon: actual=512
# Request VM to change its memory allocation to value (in MB)
virsh # qemu-monitor-command c6a --hmp balloon 1024
查看 verson
virsh # qemu-monitor-command <domain> --hmp info version
3.1.0Debian 1:3.1+dfsg-8+deb10u8
查看 VNC info
virsh # qemu-monitor-command <domain> --hmp info vnc
Server: address: 0.0.0.0:5965 auth: none Client: address: 192.168.123.21:2698 x509_dname: none username: none
runtime change vnc password
如何 change
行 ps aux | grep kvm 會見到
/usr/bin/kvm ... -vnc 192.168.123.10:31,password ...
那 change 的方法就是
virsh # qemu-monitor-command <domain> --hmp change vnc :65
virsh # qemu-monitor-command <domain> --hmp change vnc password your_new_pw
Replace Disk(hotplug)
Verifying Guest Suppor
grep CONFIG_HOTPLUG /boot/config-*
CONFIG_HOTPLUG_PCI=y CONFIG_HOTPLUG_PCI_ACPI=y
Guest(VM) 的準備
# un-plug 前, 將 disk offline
echo offline > /sys/block/sda/device/state
echo 1 > /sys/block/sda/device/delete
# 當 plug 了新碟後, 將其 online
echo "- - -" > /sys/class/scsi_host/host0/scan
Remark
ls -l /sys/class/scsi_host
lrwxrwxrwx. 1 root root 0 Jan 6 17:22 host0 -> ../../devices/pci0000:00/0000:00:06.0/virtio3/host0/scsi_host/host0 lrwxrwxrwx. 1 root root 0 Jan 6 17:22 host1 -> ../../devices/pci0000:00/0000:00:01.1/ata1/host1/scsi_host/host1
查看要 replace 的目標
virsh # qemu-monitor-command rocky8 --hmp info block
drive-scsi0-0-0-2 (#block787): nbd://127.0.0.1:10810 (raw) Attached to: scsi0-0-0-2 Cache mode: writeback
un-Plug
drive_del vs device_del
Diagram
Host (Drive)--(Device) Guest
drive_del
Close out the host-side resource and prevent any further IO to the device
The result is that guest generated IO is no longer submitted against the host device underlying the disk.
drive_del should be used in the case that a device_del doesn't complete successfully.
device_del
中斷 Guset 與 Device 的連接
e.g.
virsh qemu-monitor-command rocky8 --hmp info block
... drive-scsi0-0-0-2 (#block1759): /mnt/raid/rockylinux/data2.raw (raw) Attached to: scsi0-0-0-2 Cache mode: writeback
Remark: 這裡的 "drive-scsi0-0-0-2" 是 CLI 時用到的 id, 只要對應就 ok
virsh qemu-monitor-command rocky8 --hmp device_del scsi0-0-0-2
virsh qemu-monitor-command rocky8 --hmp info block
# scsi0-0-0-2 完全不見了
P.S.
如果使用 drive_del 先
virsh qemu-monitor-command rocky8 --hmp drive_del drive-scsi0-0-0-2
virsh qemu-monitor-command rocky8 --hmp info block
scsi0-0-0-2: [not inserted]
Attached to: scsi0-0-0-2
Plug
Check Device Current Parameter
ps aux | grep qemu-system-x86_64
... -drive file=nbd:127.0.0.1:10810,format=raw,if=none,id=drive-scsi0-0-0-2 \ -device scsi-hd,bus=scsi0.0,channel=0,scsi-id=0,lun=2,drive=drive-scsi0-0-0-2,id=scsi0-0-0-2,logical_block_size=4096,physical_block_size=4096
P.S.
'id' parameters is required when adding devices and drive objects so that they can be referenced when deleting them.
"if=none" # which allows qemu to use this drive with any guest visible device.
drive_add
# drive_add # define a block driver in qemu like -device on the command line
drive_add slot ... # add drive to PCI storage controller, 一般 slot 都係 0
virsh qemu-monitor-command rocky8 --hmp drive_add 0 file=nbd:127.0.0.1:10810,format=raw,if=none,id=drive-scsi0-0-0-2
virsh qemu-monitor-command rocky8 --hmp info block
drive-scsi0-0-0-2 (#block1101): nbd://127.0.0.1:10810 (raw)
Removable device: not locked, tray closed
Cache mode: writeback
device_add
Once you've defined an a new 'drive' object, to make it visible to the guest it needs to be connected to a device.
device 會是 virtio-blk-pci / scsi-disk
Help: virsh qemu-monitor-command rocky8 --hmp device_add ?
virsh qemu-monitor-command rocky8 --hmp device_add scsi-hd,?
virsh # qemu-monitor-command rocky8 --hmp device_add scsi-hd,bus=scsi0.0,channel=0,scsi-id=0,lun=2,drive=drive-scsi0-0-0-2,id=scsi0-0-0-2,logical_block_size=4096,physical_block_size=4096
這裡的 "id" 用於 "Attached to: scsi0-0-0-2"(--hmp info block) device_del 時要用到
scsi-hd
- bus= # 非必要
- channel= # 非必要
- scsi-id= # 非必要
- lun= # 非必要
- drive=<str> # 必要, Node name or ID of a block device to use as a backend
virsh qemu-monitor-command rocky8 --hmp info block
- Controller/Bridge/Hub devices
- USB devices
- Storage devices (scsi-hd = "virtual SCSI disk")
- Network devices
- Input devices
- CPU devices
- ...
drive-scsi0-0-0-2 (#block1149): nbd://127.0.0.1:10810 (raw) Attached to: scsi0-0-0-2 Cache mode: writeback
Remark: file
id="drive-scsi0-0-0-2"
virsh qemu-monitor-command rocky8 --hmp drive_add 0 \
file=/mnt/raid/rockylinux/data2.raw,format=raw,if=none,id=$id
block_size="logical_block_size=4096,physical_block_size=4096"
# virsh qemu-monitor-command rocky8 --hmp device_add scsi-hd,drive=${id},id=scsi0-0-0-2,${block_size}
virsh qemu-monitor-command rocky8 --hmp device_add \
scsi-hd,bus=scsi0.0,channel=0,scsi-id=0,lun=2,drive=${id},id=scsi0-0-0-2,${block_size}
virsh qemu-monitor-command rocky8 --hmp info block