Linux-KVM: Managing Disk Images
Disk images have long been the traditional form of storage for virtualized environments, they are essentially containers in the form of a file on the host’s file system. These files can be either fully allocated or sparsely allocated at time of disk creation, a fully allocated 20GB disk image will take up 20GB of storage on the host file system, a sparsely allocated 20GB disk image will only take up as much storage on the host file system as has actually been written inside of the disk, so if you have a newly created sparse file the file will be just a few KB in size.
Disk images can be very flexible, especially if they have been sparsely allocated. This allows you to move VM files between hosts as needed, if they have not been sparsely allocated it could potentially take too long to do any sort of move from one host to another. If you are using shared storage this can reduce some of the issues relating to large disk images.
When I use disk images I prefer to create them via the virt-install script, however if you’d like to manually create them you could use kvm-img (also known as qemu-img).
View Disk Image Information
# kvm-img info /kvm/images/disk/disk.img
Create a Raw Image (Sparse)
# kvm-img create -f raw /kvm/images/disk/disk.img 20G
Create a QCow2 Image (Sparse)
# kvm-img create -f qcow2 /kvm/images/disk/disk.img 20G
Create Snapshots (qcow2)
# kvm-img snapshot -c snapshot01 /kvm/images/disk/disk.img
List Snapshots (qcow2)
# kvm-img snapshot -l /kvm/images/disk/disk.img
Apply Snapshots (qcow2)
# kvm-img snapshot -a snapshot01 /kvm/images/disk/disk.img
Delete Snapshots (qcow2)
# kvm-img snapshot -d snapshot01 /kvm/images/disk/disk.img
Create Layered Images (qcow2)
# kvm-img create -f qcow2 /kvm/images/disk/base.img 20G # kvm-img create -f qcow2 -o backing_file=/kvm/images/disk/base.img /kvm/images/disk/disk.img # kvm-img info /kvm/images/disk/disk.img image: disk.img file format: qcow2 virtual size: 20G (21474836480 bytes) disk size: 136K cluster_size: 65536 backing file: /kvm/images/disk/base.img (actual path: /kvm/images/disk/base.img)
Commit Image Changes into Backing Image (qcow2)
# kvm-img commit -f qcow2 /kvm/images/disk/disk.img
One of the most important things to keep in mind is when you are moving sparse images you need to ensure that you move it in such a way that you honor the “holes” in the file. This ensures that after the copy you still have a sparse file.
Copy Sparse File with cp
# cp --sparse=always /kvm/images/disk/sparse.img /kvm/images/disk/newsparse.img
Copy Sparse File with rsync (Locally)
# rsync -S /kvm/images/disk/sparse.img /kvm/images/disk/newsparse.img
Copy Sparse File with rsync (Remotely)
# rsync -S /kvm/images/disk/sparse.img root@remotehost:/kvm/images/disk/newsparse.img
In addition to disk images there is a different (read: better) way of managing guest storage. LVM Logical Volumes are incredibly flexible, they can be moved from one disk to another, they can be expanded (up to the limitations of the hardware). The one inflexible portion of logical volumes through LVM is that there is no way to provide sparse storage. This is my preferred method, as it is fairly standard when it comes to administration between a physical and a virtual machine. In another article Linux LVM2: Flexible Local Storage Management I have detailed how you would go about expanding a LVM logical volume as well as how to perform a snapshot. In my environment all of my production VMs run off of LVs however I keep my base images in the form a base image file which can be pasted onto an LV when it is time to put them into production. In my article Linux-KVM: Converting Raw Disk Images to LVM Logical Volumes to see how you can implement this as well.
