kvm-磁盘,关联 disk, qcow2, libvirt, virtualization, 虚拟机

Overview

Images are virtual disks used to store the operating system and data of VM Guests. They can be created, maintained and queried with the qemu-img command.

image format

raw 和 qcow2 是性能最好的2个磁盘文件格式。

  • Raw Format
    • This format is simple and easily exportable to all other emulators/hypervisors.
    • It provides best performance (least I/O overhead).
    • It occupies all allocated space on the file system.
    • The raw format allows to copy a VM Guest image to a physical device (dd if=VMGUEST.RAW of=/dev/sda).
    • It is byte-for-byte the same as what the VM Guest sees, so this wastes a lot of space.
  • qcow2 Format

    • Use this to have smaller images (useful if your file system does not supports holes).
    • It has optional AES encryption (now deprecated).
    • Zlib-based compression option.
    • Support of multiple VM snapshots (internal, external).
    • Improved performance and stability.
    • Supports changing the backing file.
    • Supports consistency checks.
    • Less performance than raw format.

    • l2-cache-size
      和raw一样的性能需要借助cache。
      8G 容量,需要1M cache。 64G就需要 8M cache。
      设置cache的方法:-drive format=qcow2,l2-cache-size=8M

    • Cluster Size
      可以修改 Cluster Size,设置范围在 512 KB 和 2 MB 之间。
      小的簇大小,节省磁盘空间;大的簇大小,提高性能。

    • Preallocation
      An image with preallocated metadata is initially larger but can improve performance when the image needs to grow.

    • Lazy Refcounts
      Reference count updates are postponed with the goal of avoiding metadata I/O and improving performance. This is particularly beneficial with cache=writethrough. This option does not batch metadata updates, but if in case of host crash, the reference count tables must be rebuilt, this is done automatically at the next open with qemu-img check -r all. Note that this takes some time.
  • qed Format
    qcow2 太优秀,这个 qed 后继者被放弃了。

  • VMDK Format
    vmware 的格式,使用广泛。

thin provision & thick provision

qcow2 默认使用 thin provision

要使用 thick 模式,qcow2虚拟磁盘文件创建的时候,就完全分配磁盘空间,要使用 preallocation 参数:

1
qemu-img create -o preallocation=full -f qcow2 /var/lib/libvirt/images/urb-dat0.qcow2 10G

thick 占用更多磁盘空间,第一次创建文件时候,很耗时,后面磁盘使用性能会高些。

An existing thin-provisioned qcow2 file can be converted to a fully allocated one like this:

1
qemu-img convert -p -f qcow2 -O qcow2 -S 0 original-file.qcow2 new-file.qcow2

虚拟机磁盘信息查询

虚拟机有哪些磁盘:

1
2
3
virsh domblklist your-vm
# 或
virsh dumpxml your-vm | egrep 'disk type' -A 5

磁盘文件的信息

1
qemu-img info /var/lib/libvirt/images/your-vm.qcow2

snapshot / 快照

列出磁盘的 snapshot

1
virsh snapshot-list your-vm

删除快照

1
virsh snapshot-delete --domain rhel8 --snapshotname snapshot1

扩展磁盘文件

例如: 有个30G的redhat

1
2
3
4
5
6
7
8
9
10
11
qemu-img info /var/lib/libvirt/images/rhel8.qcow2
image: /var/lib/libvirt/images/rhel8.qcow2
file format: qcow2
virtual size: 30G (42949672960 bytes)
disk size: 2.0G
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: true
    refcount bits: 16
    corrupt: false

扩容:

1
2
3
qemu-img resize /var/lib/libvirt/images/rhel8.qcow2 +10G
virsh start rhel8
virsh blockresize rhel8 /var/lib/libvirt/images/rhel8.qcow2 40G

注意: qemu-img resize 对有个 snapshot 的 image 无效,要删掉所有 snapshot 才行。

硬盘格式转换

img 格式,转换为 qcow2

1
qemu-img convert -f raw -O qcow2 image.img image.qcow2

共享文件夹 / Share Folder

使用 virtio-fs 方式挂在共享文件夹到 VM,和其他基于网络存储的方式,更加接近本地文件系统的习惯。

使用了 virtio-fs VM就不再支持 migration了,migration/save/managed-save/snapshot 这些功能都不再支持。