2.7. Cloning a Physical Disk

 

Everything in the UNIX system is a file.

 
 -- The UNIX Programming Environment - Chapter 2

I never fully grasped the everything's a file concept until I tried (expecting to fail) to use the qemu-img convert sub-command to create a virtual disk image of an actual hard drive. This is possible in part due to the philosophy laid down by Dennis Ritchie and Ken Thompson when they first created UNIX: everything's treated as a file. The synopsis of the convert sub-command is below.

qemu-img convert [-c] [-f fmt] [-O output_fmt] [-o options] {filename} [filename2...] {output_filename}

In this section we'll look at a standard 1GB USB thumb drive and then clone it into a disk image. Using parted, here's what that disk looks like to the host system:

Example 2.22. Thumb Drive Properties

# parted /dev/sdb print
Model: Generic Flash Disk (scsi)
Disk /dev/sdb: 1013MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size   Type     File system  Flags
 1      31.2kB  506MB   506MB  primary               boot
 2      506MB   1013MB  507MB  primary

To convert the thumb drive we're first going to unmount the drive, then use the qemu-img command to perform the actual conversion. While unmounting the drive I use the -l option which means to unmount lazily, i.e., to wait until there is no activity going on before attempting to unmount. [19]

Example 2.23. Conversion Steps

# umount -l /dev/sdb1
# time qemu-img convert -O raw /dev/sdb ./thumb_drive.raw

real    1m8.206s
user    0m0.161s
sys     0m2.593s