How to create, fsck, mount and restore a raw partition or disk image taken with dd

  • 7009629
  • 25-Oct-2011
  • 27-Apr-2012

Environment

SUSE Linux

Situation

  • Cloning a partition or physical disk with dd
  • How to mount a partition or disk image created with dd
  • How to fsck a partition or disk image created with dd

Resolution

Partition
  • Create an image of /dev/sdb1 called sdb1.img
    • # dd if=/dev/sdb1 of=sdb1.img

  • Fix the partition image
    • Use the appropriate version of fsck and switches
    • e.g.  # fsck.reiserfs --fix-fixable sdb1.img

  • Mount the partition image (optional)
    • # mount -o loop sdb1.img /mnt

  • Restore the partition image to a new physical drive/partition (ensure it is unused and not mounted)
    • Unmount the partition, if mounted
      • # umount /mnt
    • Restore the fsck'd partition image
      • # dd if=sdb1.img of=/dev/sdb1
If the above procedure is unsuccessful (the Partition boundaries are damaged, for example) a similar process can be carried out at the disk level as follows:

Disk

  • Create an image of /dev/sdb called sdb.img
    • # dd if=/dev/sdb of=sdb.img
  • Mount the dd disk image
    • Establish sector size
      • # fdisk -l
Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x00053f5c

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1               1         191     1534176   82  Linux swap / Solaris
/dev/sda2   *         192        2610    19430617+  83  Linux

Disk /dev/sdb: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x000b3ab3

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        1304    10474348+  83  Linux
    • Establish where the partition starts
      • # file sdb.img
sdb.img: x86 boot sector; partition 1: ID=0x83, starthead 1, startsector 63, 20948697 sectors
    • Calculate the offset
      • startsector*sectorsize
      • 63*512=32256

    • Mount the disk image
      • # losetup --offset 32256 /dev/loop0 sdb.img
  • Fix the partition in the disk image
    • Use the appropriate version of fsck and switches
    • e.g.  # fsck.reiserfs --fix-fixable /dev/loop0
  • Mount the partition in the disk image (optional)
    • # mount /dev/loop0 /mnt

  • Restore the disk image to a new physical drive (ensure it is unused and not mounted)
    • Unmount the partition, if mounted
      •  # umount /mnt

    • Unmount the disk image
      • # losetup -d /dev/loop0

    • Restore the fsck'd disk image
      • # dd if=sdb.img of=/dev/sdb

Additional Information

dd can be used to take an image of a failing drive.  This image may contain errors.  The procedure above allows the image to be fixed and restored to a new disk device or data to be salvaged from the image.

Great care should be taken with this procedure to ensure that the correct physical device and image is being used.

The methods described here can also be used to transfer partitions and disks from one physical device to another.  If doing this omit the fsck steps as appropriate.

Feedback service temporarily unavailable. For content questions or problems, please contact Support.