dd is an amazing tool. I use it to clone SDHC cards for small ARM-computers. In this post, I'll write down some tips/common scenarios.
Clone a disk
Couldn't be more simple.
sudo dd if=/dev/sda of=/tmp/sda.img. That's it. This will create a bit-by-bit copy of the whole disk, including partition tables, partitions and file systems.
Restore a cloned disk
sudo dd if=/tmp/sda.img of=/dev/sda
It is also possible to clone individual partitions by specifying them instead (
Mount whole disk image as a "real" device
This is my favorite feature. The disk image is mounted as a loopback device, and can then be managed exactly as a real disk (using gparted, for example).
- First, we mount the image as device loop0.
sudo losetup /dev/loop0 /tmp/sda.img
- If we now look at this partition in gparted (using
sudo gparted /dev/loop0) there will be exclamation marks next to the partitions on the device, although it will find the partitions (the partition table is available). To make it find the partitions, we have to tell Linux to reread the partition table for the loop device.
sudo partprobe /dev/loop0
- The disk (including partitions) can now be managed using
sudo gparted /dev/loop0, and the individual partitions will also appear as
Disconnect loop device
sudo losetup -d /dev/loop0
Mount individual partitions directly from hard drive image
To do this, we first need to know where the partition starts in the whole disk image. Find this using
sudo fdisk -l /tmp/sda.img. The block count will be printed, look for the Start block for the partition you want to mount. There will also be a note of how many bytes each block is (in my case 512 bytes). Multiply that with the Start block for the partition. The resulting number is the byte offset where the partition begins in the disk image. To mount the partition, use
sudo mount /tmp/sda.img /mnt/somefolder -o loop,offset=<bytenumberhere>.
There are several tools available for resizing partitions (
resize2fs), creating filesystems (
mkfs.ext4), checking filesystems (
fsck.ext4) - usually I just end up using gparted, plain and simple.
That's it for now. Will add more if I find more useful stuff.