Linux — dd Operations Notes

I recently was helping a friend with some computer trouble which resulting in me first creating a full disk Image backup using “dd”. I’ve done this before years ago, but I wanted to add some of the common dd backup/restore methods to my notepad. There are so many articles online on how to use dd to do a Full disk backup and restore it. I will be using similar methods.

dd Terms

  • if — input device (file,hardware,CD,etc).
  • of — output device (file,hardware,CD,etc).
  • bs — sets “dd” read and write size.
  • noerror — continues after read errors.
  • readom — CD to ISO utility.
  • mount — Linux command to mount file-systems
  • gz — gunzip is a compression utility which helps to reduce the size of images created with dd.
  • md5sum — a checksum utility to ensure integrity when moving large image files.

Direct Hard Drive to Hard Drive clone:

dd if=/dev/sda of=/dev/sdb

Hard Drive to Image

dd if=/dev/sda conv=sync,noerror bs=64K | gzip -c > image.gz

Partition to Image

dd if=/dev/sda1 conv=sync,noerror bs=64K | gzip -c > image.gz

NOTICE: This is the same command as the above example except it specifies the sda1 partition..

Get dd Status

watch -n 5 killall -USR1 dd

This will monitor the status of the running dd process every 5 seconds.

CD to ISO

umount /dev/sr0 
readom dev=/dev/sr0 f=/path/to/image.iso

NOTICE: Read this page as to why readom is better than using dd for reading CDs.

Zero a Drive

dd if=/dev/zero of=/dev/sda bs=4k conv=notrunc

NOTICE: This will write zeros to each block on the drive. Using /dev/random is safer to ensure data cannot be recovered ,however it is more CPU and time intensive.
**notrunc — Does not truncate the output file. This is important as you want the entire drive zero’d

Benchmarking

dd if=/dev/zero bs=1024 count=1000000 of=testfile 
dd if=testfile of=/dev/null bs=1024
  • First part tests the write performance of the drive by growing a file of “zeros” to a file size of 1GB. The bs=1024 will use a block size of 1024bytes. This is really a cool utility, I remember using it to perform a benchmark test of my home RAID server when I completed the RAID set up.
  • Second part tests the read performance of the drvie by re-reading the file we just created.


NOTICE: The differences in read/write is to be expected as performance benefits of RAID.

Mounting Single Partition Image File

mount -t vfat -o loop,ro,noexec image.file /mount/point

NOTICE: Replace vfat with the filesystem type.

Mounting Full Disk Image Partition

**If the image to be restored contains multiple partitions, you will need to offset the image file to single out a partition to mount.
First use fdisk to read the image file and get the starting sector of the partition you want to mount.

fdisk -l hd.img

**image missing, sorry 🙁 ****

mount -o loop,ro,offset=$((x*y)) hd.img /mount/point

x = sector size (usually 1024)
y = starting sector (from fdisk -l)

For my case:

mount -o loop,ro,offset=$((512*206848)) hd.img /tmp/temp/

Compression

Assuming image file name is hd.img, get md5sum of uncompressed Image. This will ensure integrity when compressing/decompressing.

md5sum hd.img

This may take a while depending on how large a file, and will generate a checksum string. I would save this string with the image file.

Compress the image file.

gzip -9 -c hd.img > hd.img.gz

NOTICE: This will compress the hd.img file using best compression. Time of compression will vary based on file size and bit complexity.

Sources: