Mount single partition from image of entire disk (device)

I made an image of my entire disk with

dd if=/dev/sda of=/media/external_media/sda.img

Now the problem is I'd like to mount an ext4 filesystem that was on that disk but

mount -t ext4 -o loop /media/external_media/sda.img /media/sda_image

obviously gives a superblock error since the image contains the whole disk (MBR, other partitions) not just the partition I need. So I guess I should find a way to make the disk image show up in the /dev/ folder...

Does anyone know how to do that?

PS: I can always dd back the image to the original disk, but that would be very inconvenient (I updated the OS and I'd like to keep it as it is)


Get the partition layout of the image

$ sudo fdisk -lu sda.img
...
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
...
  Device Boot      Start         End      Blocks   Id  System
sda.img1   *          56     6400000     3199972+   c  W95 FAT32 (LBA)

Calculate the offset from the start of the image to the partition start

Sector size * Start = (in the case) 512 * 56 = 28672

Mount it on /dev/loop0 using the offset

sudo losetup -o 28672 /dev/loop0 sda.img

Now the partition resides on /dev/loop0. You can fsck it, mount it etc

sudo fsck -fv /dev/loop0
sudo mount /dev/loop0 /mnt

Unmount

sudo umount /mnt
sudo losetup -d /dev/loop0

Update for Ubuntu 16.04: With the new losetup this is now easier:

sudo losetup -Pf disk_image.raw

See the rest of the answer for older versions of Ubuntu.


An easy solution is using kpartx: it will figure out the partition layout and map each to a block devices. After that all you have to do is mount the one you want.

Open Terminal, locate the disk image, and enter this command:

$ sudo kpartx -av disk_image.raw 
add map loop0p1 (252:2): 0 3082240 linear /dev/loop0 2048
add map loop0p2 (252:3): 0 17887232 linear /dev/loop0 3084288

This created loop0p1 and loop0p2 under /dev/mapper. From the output you can see the sizes of the partitions which helps you identify them. You can mount the one you want with:

$ sudo mount /dev/mapper/loop0p2 /mnt

Alternatively, the block device is detected by Nautilus and you can mount it from the side bar:

enter image description here

When you are done, unmount what you mounted and remove the device mapping:

$ sudo umount /mnt
$ sudo kpartx -d disk_image.raw

Edit : works with util-linux >=2.21. At the time of writing ubuntu ships with version 2.20 only

From man losetup :

       -P, --partscan
          force kernel to scan partition table on newly created loop device

So just run

$ sudo losetup -f --show -P /path/to/image.img

to create device nodes for every partition of your disk image on the first unused loop device and print it to stdout. If using /dev/loop0 device it will create at least /dev/loop0p1 that you will be able to mount as usual.


Try gnome-disk-image-mounter:

gnome-disk-image-mounter sda.img

No sudo required. It will be mounted at /media/your_user_name/partition_name, just like USB drives.