How do I mount an `img` created with /bin/dd of a hard drive?

When you use dd on /dev/sdb instead of /dev/sdb1 or /dev/sdb2, you copy all the partitions from the said drive into one file.

You must mount each partition separately.


To mount a partition from a file, you must first find out where in the file that partition resides.

Using your output from file -s sdb.img we find the startsectors for each partition:

sdb.img: x86 boot sector; partition 1: ID=0x12, starthead 1, startsector 63, 10233342 sectors; partition 2: ID=0xc, active, starthead 0, startsector 10233405, 72517410 sectors; partition 3: ID=0xc, starthead 0, startsector 82750815, 73545570 sectors, code offset 0xc0

Partition     Startsector
1                   63
2                   10233405
3                   82750815

To mount a single partition, where X is the startsector of that partition, run:

mount ~/sdb.img /mnt/sdb -o offset=$((X*512))

So to mount the second partition, you will have to run:

mount ~/sdb.img /mnt/sdb2 -o offset=$((10233405*512))

sidenote: make sure that /mnt/sdb2 exists before you run this.

Have fun!


update: In the answer, I assumed that the sector size for you image was 512, please see this question on how to calculate that.


On Linux mount commands within the accepted answer will implicitly create a loop device. Assuming the the full disk image has a correct partition table (which your kernel is able to understand), you may prefer to use losetup(8) directly to save you the hassle of calculating offsets. Also there is no need for extra tools like kpartx as suggest in an other answer. It will do basically the same thing as the following:

losetup -Prf sdb.img will associate the first free loop device (-f) as readonly (-r) with the image file sdb.img. The -P option forces the kernel to scan the partition table of this loop device and will create loop devices for each partition.

So you may get something like:

$ lsblk
NAME      MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
loop0       7:0    0 167.7G  1 loop
├─loop0p1 259:0    0   500M  1 loop
└─loop0p2 259:1    0 167.2G  1 loop
...

You can now mount each partition, readonly of course, at a desired mountpoint.

$ mount -r /dev/loop0p1 /tmp/backup_sdb1

Keeping everything readonly is optional, but may be a good thing to do for a backup image.

update: -P option was added with util-linux-2.21 in 2012. So it was not available at the time the accepted answer was written. Also note this and the other answers are specific to linux. OpenBSD and NetBSD have vnode disks managed by vnconfig(8) and FreeBSD has memory disks managed by mdconfig(8).


Alternatively, you can use a nice tool called kpartx. From the man page:

kpartx - Create device maps from partition tables

This means that you can make a "pseudo device", with device partitions, right from the img file:

$ kpartx -av sdb.img
add map loop0p1 (254:2): 0 2048 linear /dev/loop0 0
add map loop0p2 (254:3): 0 31162 linear /dev/loop0 2048
$ lsblk
.
.
.
loop0                     7:0    0  16.2M  0 loop
├─loop0p1               254:2    0     1M  0 part
└─loop0p2               254:3    0  15.2M  0 part

Then you can mount /dev/mapper/loop0p2 for example, if you want the second partition on the image.

Tags:

Mount

Dd