27

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
6
  • You want to mount the image without writing it to de SD card?
    – OrangeTux
    Apr 10, 2014 at 14:08
  • Yes, I want to have full access to the filesystem stored in the .img file so that I can copy/modify/delete/etc. files without having a SD card
    – MrD
    Apr 10, 2014 at 14:12
  • Maybe this question helps you.
    – OrangeTux
    Apr 10, 2014 at 14:22
  • Unfortunately this doesn't work, tried both attempts. The first one didn't create the /dev/loop0p1 device. The second one (I have calculated the starting point accordingly) throws a wrong fs type, bad option, bad superblock on /dev/loop0, missing codepage or helper program, or other error problem
    – MrD
    Apr 10, 2014 at 14:30
  • In addition, sudo fdisk -l /dev/loop0 claims: Disk /dev/loop0 doesn't contain a valid partition table.
    – MrD
    Apr 10, 2014 at 14:36

4 Answers 4

48

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/
1
  • I was not able to mount more than one partition. Would that be because we need to specify the size? (sizelimit=... along the offset=...) Or is the loop device not allowing me to mount more than one partition from a given file? Sep 27, 2020 at 23:04
27

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/
1
  • 1
    In ubuntu 18.04 and 20.04, partitions in images can be mounted by double-clicking the .img file in the file browser. They are mounted read-only, however, and cannot be remounted writable. When I need to modify the filesystem in an image, kpartx is still the best tool to achieve it.
    – T. Herzke
    Jun 12, 2020 at 8:58
2

if your goal is to explore or modify the content of a partition (file system), this command line will mount the file system of the sd card dump my_sdcard_dump.img into the directory mount_dir.

part_id=2; INFILE=my_sdcard_dump.img; MOUNTPT=mount_dir PARTITION=${part_id}; sudo mount "$INFILE" "$MOUNTPT" -o loop,offset=$[ `/sbin/sfdisk -d "$INFILE" | grep "start=" | head -n $PARTITION | tail -n1 | sed 's/.*start=[ ]*//' | sed 's/,.*//'` * 512 ]
1
  • Explanation to the subcommands (you can als add this to your answer):This subcommand reads and calculates the offset: /sbin/sfdisk -d "$INFILE" | grep "start=" | head -n $PARTITION | tail -n1 | sed 's/.*start=[ ]*//' | sed 's/,.*//'. sfdisk -d gives all partition information of your image user readable. grep "start=" selects all lines that contain a partition start. head -n $PARTITION | tail -n1 gives the n-th line, which corresponds to the respective selected partition number. I dont know sed, but sed 's/.*start=[ ]*//' | sed 's/,.*//' returns the offset. $[... * 512] finishes.
    – Cadoiz
    Jun 17, 2021 at 14:03
0

Dealing with an image of a whole disk with multiple partitions is quite tricky. Linux was not designed to read a partition table out of a regular file, even when attached to a loopback device, so you must carefully identify the offsets of the partitions and pass them in to the mount command.

The preferable way would be to create separate images of each partition:

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

Now you can easily treat these files as if they were individual partitions on a disk, mounting them as you normally would a real disk partition, by mapping them to a loop device. A loop device, or loopback device, is a virtual device that provides a translation layer for Linux to treat a file as a block device (like a disk or partition).

The loop devices are typically /dev/loop0 through /dev/loop8. Identify an unused loop device with the losetup command:

$ sudo losetup /dev/loop0
loop: can't get info on device /dev/loop0: No such device or address

This response indicates an unassigned loop device. Now we can assign the loop device to one of our image files:

$ sudo losetup /dev/loop0 /home/myusername/raspberry-backup-sdf1-2014-04-10.img

Absence of output from this command indicates success. Now /dev/loop0 is for most purposes functionally equivalent to /dev/sdf1 of your SD card, and you can mount it as you normally would:

sudo mount -t vfat /dev/loop0 /media/sdimage-1

Repeat the process using another loop device to mount the other partition. When you're done, unmount the filesystems and unassign the loop devices:

sudo umount /dev/loop0
sudo losetup -d /dev/loop0
1
  • I would like to avoid creating two image files, because - among other minor reasons - I would like to distribute the image to other people so that they can use it to initialize their SD-cards with. Therefore, the different partitions like /boot etc. have to be in one file.
    – MrD
    Apr 10, 2014 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.