Physical Disk
This is about converting a QEMU image into a physical disk, like hard disk or USB drive. Make sure you backup your storage device data! This action will erase the entire device, making those data not recoverable.
Prepare Operating System Specific Procedures
Some operating system such as Windows have hardware system binding, making it impossible to image the physical disk. They are documented separately in the sub-sections. Use the navigation bar and find your target operating system. Execute them first before proceeding to next step.
Double Check Disk Partition ID
Use lsblk
command to check your storage device.
$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 931.5G 0 disk
├─sda1 8:1 0 243M 0 part
├─sda2 8:2 0 1K 0 part
└─sda5 8:5 0 931.3G 0 part
└─sda5_crypt 254:0 0 931.3G 0 crypt
├─<lvm-name>-root
│ 254:1 0 28G 0 lvm /
├─<lvm-name>-swap_1
│ 254:2 0 11.9G 0 lvm [SWAP]
└─<lvm-name>-home
254:3 0 891.4G 0 lvm /home
sdb 8:16 1 28.7G 0 disk
└─sdb1 8:17 1 250M 0 part /mnt/bootloader
FUCKING Important Note:
- DO NOT skip this step or be overconfident with it.
qemu-img
anddd
commands all have the ability to overwrite your existing operating system. One wrong designation means your entire machine is gone./dev/sdb
doesn't necessary means USB devices. Kernel allocate them based on port numbers so you must check them.- Had a personal bad experience with it. I assumed
/dev/sdb
was my external USB hard disk, but turned out to be my OS hard disk. I lost 1 long day just to recover my machine during peak hour.
Image to Physical Disk
To convert the image into physical disk, use the QEMU-img command.
$ sudo qemu-img convert -O raw <path to image> <main device>
Example, say we want to convert sample.qcow2
to the USB device /dev/sdb
, we do it:
$ sudo qemu-img convert -O raw sample.qcow2 /dev/sdb
TIP
For certain disk format, you can use gparted
to resize the partition after a successful conversion. You have to make sure those formats like FAT32, vFAT, ntfs, ext4, ext2 etc. Some are highly restricted and not allowed for reformatting, like LUKS encrypted partitions. Please do your research before proceeding.
Also, after resizing, remember to let any disk check software run fully during the first boot. Otherwise, unpredictable outcome may happen.
Physical Disk to Image
There are 2 styles depending on your needs.
Simple Conversion
To convert the physical disk back into QEMU image, use can use the QEMU-img command:
$ sudo qemu-img convert -c -O qcow2 <main device> <path to image>
For example, say we want to convert /dev/sdd
into the QEMU qcow2
image, we do:
$ sudo qemu-img convert -c -O qcow2 /dev/sdd image.qcow2
Backing Up Disk Image Simultaneously
However, if you need to backup the physical disk before converting to qcow2
image, you can do the intermediate conversion with DD then QEMU-img commands. Remember to perform sync for each steps to ensure good quality for data integrity. The commands are:
$ dd if=<main device> of=<path to raw.img image> bs=4M
$ sync
$ sudo qemu-img convert -c -O qcow2 <path to raw.img image> <path to qcow2 image>
$ sync
Here's the example commands from the above:
$ dd if=/dev/sdb of=disk.img bs=4M
$ sync
$ sudo qemu-img convert -c -O qcow2 disk.img image.qcow2
$ sync
Here, you get 2 files:
disk.img
for your original disk image.image.qcow2
for your QEMU deployment, same as above.
TIP
For certain disk format, you can use gparted
to resize the partition before the conversion. This can save time and reduce the image size on the output. You have to make sure those formats like FAT32, vFAT, ntfs, ext4, ext2 etc. Some are highly restricted and not allowed for reformatting, like LUKS encrypted partitions. Please do your research before trying.
Cleanup Operating System Specific Procedures
If you did execute the operating system specific preparation procedures just now, look up the same sub-section page again for clean up procedures. There are some clean up steps to do after performing a successful QEMU boot.
That's all about managing QEMU images with physical storage devices.