How-To: Create, Compress, or Convert a Disk Image
In everyday IT tasks, things often need to be quick and simple. This includes creating a virtual copy of a physical hard drive. There are several reasons for this—for
instance, when a backup copy of a hard drive is needed or when a physical PC needs to be converted into a virtual machine. From Clonezilla to paid software, the options
are endless. However, it can also be done easily using command-line tools like dd
and qemu-img
. This guide applies to macOS and Linux-based
systems (Ubuntu, etc.).
Step by Step:
Creating a Disk Image
Before getting started, the necessary tools must be installed, specifically the QEMU tools. Next, a 1:1 image of the hard drive will be created using dd. If an image is made of a 512GB hard drive, the resulting image will also require at least 512GB. Once the image is created, it can be compressed using the QEMU tools.
Tip: The external hard drive should not be connected through a slow USB hub. Ideally, use USB 3. For SATA hard drives, a USB 3 adapter is recommended, especially if used frequently.
The following steps are required:
- Install the QEMU tools
- Identify the hard drive number
- Start tools with the appropriate parameters
- Perform compression or conversion
# Install with Homebrew on macOS
brew install qemu
# Install on e.g. Ubuntu 24.04
apt install qemu-img
# Get a list of all drives on macOS
diskutil list
# Get a list of all drives on Linux
ls -la /dev/disk*
# Start the imaging process with dd
dd if=/dev/disk<number?> of=raw.img bs=4M
# macOS: Ctrl+T shows the current progress
# Konvert in QCow2 file format
qemu-img convert -f raw -O qcow2 raw.img drive.qcow2
# Second iteration for better compression
qemu-img convert -c -O qcow2 drive.qcow2 hdd-final.qcow2
What is the QCow2 Format?
The QCow2 format (QEMU Copy On Write version 2) is a disk image format primarily used in virtualization environments like QEMU and KVM. It is a flexible and efficient format that offers additional features compared to simple RAW formats, optimizing storage space and performance.
Features of QCow2:
-
Copy-On-Write: QCow2 uses copy-on-write techniques to utilize storage space more efficiently. Changes to the disk are not written directly to the existing data area but are stored in a separate area. This saves space and allows for easy creation of snapshots.
-
Snapshots: QCow2 allows for the creation of snapshots, enabling the state of a virtual drive to be saved at a specific point in time. This is especially useful for testing or rollbacks.
-
Compression: Data can be stored in a compressed format within QCow2, reducing the amount of physical storage space required.
-
Encryption: QCow2 includes built-in encryption mechanisms to protect sensitive data on virtual disks.
-
Dynamic Allocation: The format supports sparse files, meaning only the actual storage used by the disk is allocated on the host, rather than the full size of the virtual disk.
-
Backing File Support: QCow2 images can reference backing files, making it easier to share common data among multiple virtual machines and save storage space.
Converting to VMware Virtual Machine Disk (VMDK)
Depending on the use case, it might make sense to convert the disk into the VMware Virtual Machine Disk (.vmdk) format. This can be done easily with the QEMU tools.
# Convert e.g. in .vmdk file format
qemu-img convert -f qcow2 -O vmdk hdd-final.qcow2 hdd.vmdk