How to mount sd-card image created with dd?

I have created an image of my Raspberry Pi SD-card using dd:

sudo dd if=/dev/sdf of=/home/myusername/raspberry-backup-2014-04-10.img

The SD-card includes two partitions (one vfat, one ext4) which are automatically mounted when I plug the card in.

My question: How can I mount these partitions from the .img file?


More details:

$ fdisk -l raspberry-backup-2014-04-10.img 

Disk raspberry-backup-2014-04-10.img: 3974 MB, 3974103040 bytes
255 heads, 63 sectors/track, 483 cylinders, total 7761920 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000981cb

                          Device Boot      Start         End      Blocks   Id  System
raspberry-backup-2014-04-10.img1            8192      122879       57344    c  W95 FAT32 (LBA)
raspberry-backup-2014-04-10.img2          122880     7761919     3819520   83  Linux

To avoid the need to create separate images for each partition or installing a utility like kpartx, you can mount each partition individually by specifying an offset in the mount command.

First examine the partitions in the image file and determine the offset by using fdisk:

$ fdisk -u -l rpi_image280914 

Disk rpi_image280914: 16.0 GB, 16012804096 bytes
255 heads, 63 sectors/track, 1946 cylinders, total 31275008 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000cdac7

           Device Boot      Start         End      Blocks   Id  System
rpi_image280914p1   *        2048      514047      256000    c  W95 FAT32 (LBA)
rpi_image280914p2          540672    31242239    15350784   83  Linux

Take the Start sector of the partition you want and multiply that value by the Units size. So if you want the second partition you'll get 540672 * 512 = 276824064.

Now create a folder and mount the partition:

mkdir rpi_partition2
sudo mount -o loop,offset=276824064 rpi_image280914 rpi_partition2/

Once you are done doing what you want with the partition data:

sudo umount rpi_partition2/
rm -r rpi_partition2/

After some additonal testing I found the solution myself: kpartx

sudo kpartx -a raspberry-backup-2014-04-10.img

This command created /dev/mapper/loop0p1 and /dev/mapper/loop0p2. Afterwards these partitions can be mounted straight forward:

sudo mount -o rw -t ext4 /dev/mapper/loop0p2 mount_target/