Kpartx

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.

The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8) Syncing disks. Use kpartx to add partition. root@rhel6a # kpartx -av /dev.

  • Jun 12, 2020 EXAMPLE To mount all the partitions in a raw disk image: kpartx -av disk.img This will output lines such as: add map loop1p1 (254:4): 0 409597 linear 7:1 3 The loop1p1 is the name of a device file.
  • The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8) Syncing disks. Use kpartx to add partition. root@rhel6a # kpartx -av /dev.
  • Kpartx Fails With: 'Device-mapper: Create Ioctl On Failed: Device or Resource Busy, Create/Reload Failed On ' (Doc ID 2185219.1) Last updated on MAY 21, 2020. Applies to: Linux OS - Version Oracle Linux 5.0 and later Oracle Cloud Infrastructure - Version N/A and later Linux x86-64 Linux x86 Symptoms.
  • Kpartx -av disk.img. This will output lines such as: loop3p1: 0 20964762 /dev/loop3 63. The loop3p1 is the name of a device file under /dev/mapper which you can use to access the partition, for example to fsck it: fsck /dev/mapper/loop3p1. When you’re done, you need to remove the devices: kpartx -d disk.img.
  • Ubuntu: http://packages.ubuntu.com/de/hardy/kpartx
  • Debian: http://packages.debian.org/search?keywords=kpartx
  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:

    [[email protected] ~]# 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
    [[email protected] ~]# 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
    [[email protected] ~]# 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
    [[email protected] ~]#

    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

Kpartx

IIf you are using hot swappable hard disk and created a new partition using the fdisk, then you need to reboot Linux based system to get partition recognized. Without reboot, you will NOT be able to create a filesystem on your newly created or modified partitions with the mke2fs command.
The kernel still uses the old table. The new table will be utilized at the next reboot or after you run partprobe or kpartx command. Both of these programs informs the operating system kernel of partition table changes, by requesting that the operating system re-read the partition table.
You will seen an error that read as follows:

Re-reading the partition table failed.: Device or resource busy

A sample session:

After the fdisk command session (which makes changes to partition table) just type the following command:
# partprobe
OR
# partprobe /dev/sdX
Replace /dev/sdX or /dev/hdX with actual device name. Now you will able to create filesystem on new partition with the mke2fs command.

Kpartx

Inform the OS of partition table changes

/dev/mapper

Kpartx Linux

The partprobe command is part of GNU parted software. parted is a disk partitioning and partition resizing program. It allows you to create, destroy, resize, move and copy ext2, ext3, linux-swap, FAT, FAT32, and reiserfs partitions. It can create, resize and move Macintosh HFS partitions, as well as detect jfs, ntfs, ufs, and xfs partitions. It is useful for creating space for new operating systems, reorganising disk usage, and copying data to new hard disks.

Install parted

Kpartx Linux

To use partprobe command install parted. If you are using Debian / Ubuntu Linux, enter:
$ sudo apt-get install parted
OR if you are using RHEL version <= 4, enter:# up2date parted
OR if you are using CentOS / RHEL 5/6, enter:
# yum install parted
OR if you are Fedora, enter:
$ sudo dnf install parted
Now you can use the partprobe command.

Install kpartx

The kpartx tool, derived from util-linux’ partx, reads partition tables on specified device and create device maps over partitions segments detected. It is called from hotplug upon device maps creation and deletion. To use kpartx command install kpartx. If you are using Debian / Ubuntu Linux, enter:
$ sudo apt install kpartx
To use, type:
# kpartx -u /dev/sdd2