Mounting partitions within image file

kpartx is a tool which creates device maps from partition tables. Assume that you have a .img disk image which contains various partitions and you want to mount one or more of the partitions. kpartx will map each partition to a loop device under /dev/mapper and will allow you to mount the loop device to a local directory and access the files.
Mapping an image:

# kpartx -a /foo/bar/disk.img

Checking which loop device was used to mount the partitions:

# kpartx -l /foo/bar/disk.img
loop1p1 : 0 8177022 /dev/loop1 63

The disk image has one partition and is mapped to /dev/loop1p1. You can now mount the partition:

# mount /dev/mapper/loop1p1 /mnt/img

Once finished, you can remove the mapper device:

# kpartx -d /foo/bar/disk.img
loop deleted : /dev/loop1

3 thoughts on “Mounting partitions within image file

  1. Pingback: mount qemu image

  2. Pingback: Firmware: Debian v0.3 for Flytouch SD bootable.

  3. Daniel

    Fedora 13

    If you have problems removing

    device-mapper: remove ioctl failed: Device or resource busy

    unmount the tmp_dir like this >

    sudo umount “/foo/bar/tmp_dir”

    Then remove like described above.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s