Kpartx: a tool for mounting partitions within an image file

July 12, 2008

Kpartx can be used to set up device mappings for the partitions of any partitioned block device. It is part of the Linux multipath-tools. With kpartx -l imagefile you get an overview of the partitions in the image file and with kpartx -a imagefile the partitions will accessible via /dev/mapper/loop0pX (X is the number of the partition). You can mount it now with mount /dev/mapper/loop0pX /mnt/ -o loop,ro. After unmounting you can disconnect the mapper devices with kpartx -d imagefile.

There are packages for Debian and Ubuntu.

13 Comments »

RSS feed for comments on this post. TrackBack URI

  1. Hello,

    I’m trying to do this in my fedora system but doesnt work. I am not getting any output after issuing this command kpartx -a and kpartx -l and getting nothing in /dev/mapper/

    What should I check ?

    Comment by smary — June 26, 2009 #

  2. What is the nature of the image file you’re using? Is it a full image of a complete hard disk or only an image of one partition?

    Comment by robert — June 27, 2009 #

  3. Its a full image of a complete hard disk

    Comment by smary — June 29, 2009 #

  4. Actually I’m getting the following error now:

    [root@smary ~]# kpartx -l /tmp-mnt/BACKUP_final.img
    loop1p1 : 0 1701990410 /dev/loop1 218129509
    loop1p2 : 0 543974724 /dev/loop1 729050177
    loop1p4 : 0 51635 /dev/loop1 2692939776
    [root@smary ~]# kpartx -a -v /tmp-mnt/BACKUP_final.img
    device-mapper: reload ioctl failed: Invalid argument
    create/reload failed on loop1p1
    add map loop1p1 (0:0): 0 1701990410 linear /dev/loop1 218129509
    device-mapper: reload ioctl failed: Invalid argument
    create/reload failed on loop1p2
    add map loop1p2 (0:0): 0 543974724 linear /dev/loop1 729050177
    device-mapper: reload ioctl failed: Invalid argument
    create/reload failed on loop1p4
    add map loop1p4 (0:0): 0 51635 linear /dev/loop1 2692939776
    [root@smary ~]# ls -all /dev/mapper/
    total 0
    drwxr-xr-x 2 root root 60 2009-06-25 08:32 .
    drwxr-xr-x 16 root root 5060 2009-06-29 08:14 ..
    crw-rw—- 1 root root 10, 63 2009-06-25 08:32 control
    [root@smary ~]#

    Comment by smary — June 29, 2009 #

  5. This normally says that you’ve updated your kernel and it doesn’t fit anymore your device-mapper version. Update also the device-mapper package. also take a look at the syslog as it should provide additional info.

    Comment by robert — June 29, 2009 #

  6. Just use it on a device and not an image file. Create the device with losetup first.

    Comment by partition mage — July 14, 2009 #

  7. Just found ya…Great stuff.
    I will be adding you to my links list and checking in…

    Comment by andy — March 2, 2010 #

  8. […] Kpartx: a tool for mounting partitions within an image file […]

    Pingback by 通过kpartx使用LVM卷 - 阿泰的菜园 — June 28, 2010 #

  9. […] appears you can use the kpartx tools: http://robert.penz.name/73/kpartx-a-tool-for-mounting-partitions-within-an-image-file/ Kpartx can be used to set up device mappings for the partitions of any partitioned block device. […]

    Pingback by Linux - how to format multiple file systems within one file? — March 18, 2012 #

  10. […] So all partitions are in there! We will use kpartx to map this file to a block device which you can use just as your normal […]

    Pingback by Accessing an encrypted full disc image (LUKS;LVM) — January 8, 2013 #

  11. […] http://robert.penz.name/73/kpartx-a-tool-for-mounting-partitions-within-an-image-file/ […]

    Pingback by How to: How can I mount a partition from dd-created image of a block device (e.g. HDD) under Linux? #programming #solution #it | StackCopy — January 12, 2015 #

  12. Now even losetup can do it!

    # losetup -fP /home/jasan/ubuntu-15.04-snappy-amd64-generic.img
    # losetup
    NAME SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE
    /dev/loop0 0 0 0 0 /home/jasan/ubuntu-15.04-snappy-amd64-generic.img
    # ls -l /dev/loop0*
    brw-rw—- 1 root disk 7, 0 Apr 5 10:09 /dev/loop0
    brw-rw—- 1 root disk 259, 5 Apr 5 10:09 /dev/loop0p1
    brw-rw—- 1 root disk 259, 6 Apr 5 10:09 /dev/loop0p2
    brw-rw—- 1 root disk 259, 7 Apr 5 10:09 /dev/loop0p3
    brw-rw—- 1 root disk 259, 8 Apr 5 10:09 /dev/loop0p4
    brw-rw—- 1 root disk 259, 9 Apr 5 10:09 /dev/loop0p5

    Comment by Ján Sáreník — April 5, 2016 #

  13. Hello,

    I’m not familiar at all with Linux, please can you help me with this issue? My system required this update yesterday and consequently (or not?) there is no more sound from my computer neither from the loudspeaker nor from the earphone whereas the volume control shows there’s an activity. Same issue from any application: movie, soundtrack and Internet.
    Thank you for your support!
    Yasmine

    Comment by Yasmine — April 15, 2016 #

Leave a comment

XHTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Powered by WordPress
Entries and comments feeds. Valid XHTML and CSS. 37 queries. 0.066 seconds.