libvirt
libvirt
virt-manger¶
图形化界面
virsh¶
命令行工具
导出虚拟机配置,并定义新虚拟机¶
qemu-img¶
复制 image(复制稀疏文件)¶
Via cp - best speed
cp --sparse=always srcFile dstFile
Performance Notes:
Copied 200GB max/16GB actual VM as 200GB max/26GB actual, bloat: 10GB
Copy time: 1:02 (mm:ss)
Via dd - best overall performer
dd if=srcFile of=dstFile iflag=direct oflag=direct bs=4M conv=sparse
Performance Notes:
Copied 200GB max/16GB actual VM as 200GB max/21GB actual, bloat: 5GB
Copy time: 2:02 (mm:ss)
Via virt-sparsify - best size
virt-sparsify srcFile dstFile
Copied 200GB max/16GB actual VM as 200GB max/16GB actual, bloat: 0
Copy time: 17:37 (mm:ss)
cp¶
cp --sparse=always
By default, sparse SOURCE files are detected by a crude heuristic and the corresponding DEST file is made sparse as well. That is the behavior selected by --sparse=auto. Specify --sparse=always to create a sparse DEST file whenever the SOURCE file contains a long enough sequence of zero bytes. Use --sparse=never to inhibit creation of sparse files.
virt-sparsify¶
virt-sparsify (libguestfs.org)
- 支持多种文件系统。Virt-sparsify can locate and sparsify free space in most filesystems (eg. ext2/3/4, btrfs, NTFS, etc.), and also in LVM physical volumes.
- 可以将 raw 格式转换为 qcow2 格式
- 发现一个优点同时也是缺点。virt-sparsify 复制后会清除现有文件的 0 元素,使得文件变小,但同时也会导致 snapshot 记录消失
使用注意:
- 使用前关闭虚拟机
- 在删除源镜像前检查
- 需要 2x 的空间(1 tmp + 1 destination)(最差情况,实际远小于该值)
- virt-sparsify ≥ 1.26 支持 in-place 参数,避免了大量空间的使用。
- 指定 tmp 目录:
--tmp my_tmp_folder
。不支持--in-place
- 显示更多信息:
-v
复制远程 sparese 文件¶
scp’ is not sparse aware and if you use scp to copy a file that is spare it will take up “more” room on the destination host. Instead if you use rsync with -S option, spare files will be maintained as sparse.
增大 image 大小¶
然后在 windows 中分配空间
减小 image 大小¶
要复杂一些 参考:Resizing KVM qcow2 images - mauBlog (maunium.net)
overlay¶
合并 overlay¶
参考:Qemu: Merge snapshot and backing file into standalone disk | by Praveen Kumar | Medium
如果 B 的 base 是 A,合并是将 B 合并到基镜像中。因此最好备份基镜像。
变基¶
convert¶
时间巨长。40G 花了 30 分钟。并且期间 zfs 的系统无妨使用。
QEMU agent¶
用于控制 windows 虚拟机关机,休眠 (domsuspend)
参考:
- Qemu Guest Agent 方式实现 libvirt 与虚机交互_HelloWuyp 的博客-CSDN 博客_blacklist_rpc
- 10.3. Running the QEMU Guest Agent on a Windows Guest Red Hat Enterprise Linux 6 | Red Hat Customer Portal
参考的都太复杂了,实际操作如下(还没验证效果):
- 验证了效果,虚拟机关不掉,一直在 running,只能通过 virsh destory 才能强行关掉。
windows 虚拟机内下载并安装 https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/archive-virtio/ https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/archive-qemu-ga/
virt-manger 中添加 channel,选择 qemu-agent,其余会自动配置
热插拔 usb¶
udev: udev - ArchWiki (archlinux.org)
ID_VENDOR_ID=0a12
ID_MODEL_ID=0001
/devices/pci0000:00/0000:00:08.1/0000:0a:00.3/usb3/3-3/3-3.4 (usb)
SUBSYSTEM=="usb",DEVPATH=="/devices/pci0000:00/0000:00:1a.0/usb1/1-1/1-1.2",RUN+="/opt/usb-libvirt-hotplug/usb-libvirt-hotplug.sh testvm-01"
SUBSYSTEM=="usb",ID_VENDOR_ID=="0a12",ID_MODEL_ID="0001",RUN+="/opt/usb-libvirt-hotplug/usb-libvirt-hotplug.sh win10"
KERNEL=="video[0-9]*", SUBSYSTEM=="video4linux", SUBSYSTEMS=="usb", ATTRS{idVendor}=="1de1", ATTRS{idProduct}=="f105", RUN+="/opt/usb-libvirt-hotplug/usb-libvirt-hotplug.sh win10"
# bluetooth
SUBSYSTEMS=="usb", ATTRS{idVendor}=="0a12", ATTRS{idProduct}=="0001", RUN+="/opt/usb-libvirt-hotplug/usb-libvirt-hotplug.sh win10"
# mouse
SUBSYSTEMS=="usb", ATTRS{idVendor}=="30fa", ATTRS{idProduct}=="0400", RUN+="/opt/usb-libvirt-hotplug/usb-libvirt-hotplug.sh win10"
# keyboard
SUBSYSTEMS=="usb", ATTRS{idVendor}=="1a81", ATTRS{idProduct}=="2039", RUN+="/opt/usb-libvirt-hotplug/usb-libvirt-hotplug.sh win10"
- 监听 usb 设备
- 拔掉会出现 unbind, remove 事件
- 插入会出现 add, change, bind 事件
UDEV [262962.917716] unbind /devices/pci0000:00/0000:00:01.2/0000:02:00.0/usb1/1-3 (usb)
ACTION=unbind
DEVPATH=/devices/pci0000:00/0000:00:01.2/0000:02:00.0/usb1/1-3
SUBSYSTEM=usb
DEVNAME=/dev/bus/usb/001/009
DEVTYPE=usb_device
PRODUCT=1de1/f105/409
TYPE=239/2/1
BUSNUM=001
DEVNUM=009
SEQNUM=7429
USEC_INITIALIZED=261113847055
ID_PATH=pci-0000:02:00.0-usb-0:3
ID_PATH_TAG=pci-0000_02_00_0-usb-0_3
MAJOR=189
MINOR=8
UDEV [262890.680932] bind /devices/pci0000:00/0000:00:08.1/0000:0a:00.3/usb3/3-3/3-3.4 (usb)
ACTION=bind
DEVPATH=/devices/pci0000:00/0000:00:08.1/0000:0a:00.3/usb3/3-3/3-3.4
SUBSYSTEM=usb
DEVNAME=/dev/bus/usb/003/023
DEVTYPE=usb_device
DRIVER=usb
PRODUCT=a12/1/8891
TYPE=224/1/1
BUSNUM=003
DEVNUM=023
SEQNUM=7414
USEC_INITIALIZED=262890673093
ID_VENDOR=0a12
ID_VENDOR_ENC=0a12
ID_VENDOR_ID=0a12
ID_MODEL=CSR8510_A10
ID_MODEL_ENC=CSR8510\x20A10
ID_MODEL_ID=0001
ID_REVISION=8891
ID_SERIAL=0a12_CSR8510_A10
ID_BUS=usb
ID_USB_INTERFACES=:e00101:
ID_VENDOR_FROM_DATABASE=Cambridge Silicon Radio, Ltd
ID_MODEL_FROM_DATABASE=Bluetooth Dongle (HCI mode)
ID_PATH=pci-0000:0a:00.3-usb-0:3.4
ID_PATH_TAG=pci-0000_0a_00_3-usb-0_3_4
MAJOR=189
MINOR=278
休眠¶
OVMF 不支持 S4(sleep to disk, hibernation) https://listman.redhat.com/archives/vfio-users/2017-September/msg00011.html
PS C:\Windows\system32> powercfg /A
此系统上没有以下睡眠状态:
待机 (S1)
系统固件不支持此待机状态。
虚拟机监控程序不支持此待机状态。
待机 (S2)
系统固件不支持此待机状态。
虚拟机监控程序不支持此待机状态。
待机 (S3)
虚拟机监控程序不支持此待机状态。
休眠
该虚拟机监控程序不支持休眠。
待机(S0 低电量待机)
系统固件不支持此待机状态。
混合睡眠
待机(S3)不可用。
休眠不可用。
虚拟机监控程序不支持此待机状态。
快速启动
休眠不可用。
https://www.linux-kvm.org/page/PowerManagement
libvirt 开启休眠选项:
These elements enable ('yes') or disable ('no') BIOS support for S3 (suspend-to-mem) and S4 (suspend-to-disk) ACPI sleep states. If nothing is specified, then the hypervisor will be left with its default value. Note: This setting cannot prevent the guest OS from performing a suspend as the guest OS itself can choose to circumvent the unavailability of the sleep states (e.g. S4 by turning off completely).
➜ VM-Pool sudo virsh domtime win10-base error: argument unsupported: QEMU guest agent is not configured