How to inspect disk image

Solution 1:

Since it is a file containing a copy of the entire disk, you can simply treat it like any other block device and run fdisk on it. Given that the disk image is called disk.img, the following command will suffice:

fdisk -l disk.img

This will give output similar to the following:

Disk /home/yjwong/disk.img: 250.1 GB, 250058268160 bytes
255 heads, 63 sectors/track, 30401 cylinders, total 488395055 sectors
Units = sectors of 1 * 512 = 512 bytes

   Device              Boot      Start         End      Blocks   Id  System
/home/yjwong/disk.img1            2048     3905535     1951744   82  Linux swap / Solaris
/home/yjwong/disk.img2   *     3905536   488394751   242244608   83  Linux

To mount the partitions, the Linux kernel allows you to specify an offset (in bytes) to the disk image in the mount command. You need to loop-mount the partitions within the disk image. However, since fdisk specifies the start offset in sectors, you will need to multiply the given offset by the sector size (typically 512).

To mount the second partition the example above, the offset is 3905536 * 512 = 1999634432.

The following command will do the trick, assuming the partition type is ext4, and /mnt is the intended mount point:

sudo mount -t ext4 /home/yjwong/disk.img /mnt -o loop,offset=1999634432

If you want to view the content in a read-only manner, you can add ro to the mount options:

sudo mount -t ext4 /home/yjwong/disk.img /mnt -o loop,offset=1999634432,ro