-
Create a sparse (dynamically allocated) disk:
# Yields a 100 GiB (not GB) disk. # `-f qcow2` is required, as the default is raw (which preallocates all the space). qemu-img create -f qcow2 "$DISK_IMAGE" 100G
-
Create a diff disk:
qemu-img create -f qcow2 -b "$DISK_IMAGE" "$DISK_IMAGE.diff"
-
Create a compressed copy of a disk (can also be used to merge a diff image):
# [-p]rogress, [-O]utput format, [-c]ompressed qemu-img convert -p -O qcow2 -c "$DISK_IMAGE" "$DISK_IMAGE.merged"
-
Create a compressed copy without unallocated space (use
--tmp
, otherwise/tmp
is used!!):virt-sparsify --compress --tmp $(dirname "$DISK_IMAGE") "$DISK_IMAGE" "$DISK_IMAGE.sparse.compressed"
-
Import a directory in a disk:
virt-copy-in -a "$DISK_IMAGE" /tmp/pizza /
-
Mount a disk using
qemu-nbd
:PARTITION_NUMBER=4 # 1-based modprobe nbd max_part=8 qemu-nbd --connect=/dev/nbd0 "$DISK_IMAGE" # wait a second after this partprobe /dev/nbd0 mount "/dev/nbd0p"$PARTITION_NUMBER" /mnt
-
and unmount:
umount /mnt qemu-nbd --disconnect /dev/nbd0 rmmod nbd
-
Mounting a disk can also be used to shrink an image:
# mount the disk (5.) dd -status=progress if=/dev/zero of=/mnt/ZEROFILE rm /mnt/ZEROFILE # now umount the disk (6.) # now compress the image (2.)