Thursday, July 18, 2024

Make cloning hdd with dd command

Cloning a partition

From physical disk /dev/sda, partition 1, to physical disk /dev/sdb, partition 1.

dd if=/dev/sda1 of=/dev/sdb1 bs=4096 conv=notrunc,noerror,sync

If output file of (sdb1 in the example) does not exist, dd will start at the beginning of the disk and create it.
Cloning an entire hard disk

From physical disk /dev/sda to physical disk /dev/sdb

dd if=/dev/sda of=/dev/sdb bs=4096 conv=notrunc,noerror,sync

This will clone the entire drive, including MBR (and therefore bootloader), all partitions, UUIDs, and data.

-notrunc or ‘do not truncate’ maintains data integrity by instructing dd not to truncate any data.
-noerror instructs dd to continue operation, ignoring all read errors. Default behavior for dd is to halt at any error.
-sync writes zeroes for read errors, so data offsets stay in sync.
-bs=4096 sets the block size to 4k, an optimal size for hard disk read/write efficiency and therefore, cloning speed.

Backing up the MBR

The MBR is stored in the the first 512 bytes of the disk. It consist of 3 parts:

The first 446 bytes contain the boot loader.
The next 64 bytes contain the partition table (4 entries of 16 bytes each, one entry for each primary partition).
The last 2 bytes contain an identifier

To save the MBR into the file “mbr.img”:

# dd if=/dev/hda of=/mnt/sda1/mbr.img bs=512 count=1

To restore (be careful : this could destroy your existing partition table and with it access to all data on the disk):

# dd if=/mnt/sda1/mbr.img of=/dev/hda

If you only want to restore the boot loader, but not the primary partition table entries, just restore the first 446 bytes of the MBR:

# dd if=/mnt/sda1/mbr.img of=/dev/hda bs=446 count=1

To restore only the partition table, one must use

# dd if=/mnt/sda1/mbr.img of=/dev/hda bs=1 skip=446 count=64

You can also get the MBR from a full dd disk image.

#dd if=/path/to/disk.img of=/mnt/sda1/mbr.img bs=512 count=1

Source :

(Visited 29 times, 1 visits today)