KVM storage volumes are virtual disk images that can be assigned to virtual machines. They are stored on the host system and presented to the guest as virtual hard drives. The most commonly used image format for KVM storage volumes is qcow2 (QEMU Copy-On-Write 2). qcow2 is a sparse format that only allocates disk space as needed, allowing multiple volumes to efficiently share the same base image using copy-on-write. This saves disk space compared to raw disk images. qcow2 also supports features like snapshots, encryption, and compression.
To manage storage volumes with virsh and qemu-img, you can use the following commands:
Creating volumes with virsh
Use virsh vol-create-as to create a new volume:
virsh vol-create-as <pool> <name> <capacity> --format <format>
For example, to create a 10 GB qcow2 volume named my-volume.qcow2 in the default pool:
virsh vol-create-as default my-volume.qcow2 10G --format qcow2
Alternatively, define the volume in an XML file and use virsh vol-create:
<volume>
<name>my-volume</name>
<capacity>20G</capacity>
<allocation>0</allocation>
<target>
<path>/var/lib/libvirt/images/my-volume.qcow2</path>
<format type='qcow2'/>
</target>
</volume>
virsh vol-create default my-volume.xml
Managing volumes with qemu-img
qemu-img is a utility for manipulating disk images.
Some common operations:
Create a new image:
qemu-img create -f qcow2 /path/to/image.qcow2 10G
Convert between formats:
qemu-img convert -f raw -O qcow2 /path/to/image.raw /path/to/image.qcow2
Resize an image:
qemu-img resize /path/to/image.qcow2 +10G
Get information about an image:
qemu-img info /path/to/image.qcow2
Using backing stores
Overlay images allow sharing a base image and only storing changed blocks, saving disk space. Create an overlay with virsh vol-create-as:
virsh vol-create-as default overlay.qcow2 10G --backing-vol base.qcow2 --backing-vol-format qcow2
The overlay will use base.qcow2 as its backing file. Changes are stored in overlay.qcow2.
In summary, virsh provides commands to manage storage pools and volumes, while qemu-img is used for low-level disk image operations like creating, converting and resizing. Overlay images allow efficiently sharing a base image across multiple VMs.
If you want to discuss the topic with other technology-minded people, join my Discord: https://discord.gg/YbSYGsQYES
Now we have an IRC channel as well: irc.libera.chat / #tomsitcafe