Mounting raw images with multiple partitions is easy with kpartx. Type aptitude install kpartx as root to install kpartx under Debian. Kpartx is creating device-mappings for each partition. If the raw image looks like this. 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 under /dev/mapper which you can use to access the partition, for example to fsck it: fsck /dev/mapper/loop1p1. When you're done, you need to remove the devices: kpartx -d disk.img SEE ALSO. Active Oldest Votes. Partx asks the kernel to probe a given device and re-read the partition table. The kernel is doing the work here. Kpartx creates device mapper entries and so can be used by devices that the kernel does not natively support partitioning, such as multipath device mapper devices ('kpartx' is part of multipath-tools) or files. Show partitions. Fdisk -l myserver.root.img. List device mappings that would be created. Kpartx -l myserver.root.img. Create mappings / create dev for image. Kpartx -av myserver.root.img. Add map loop0p1 (252:1): 0 310378496 linear 7:0 2048. Add map loop0p2 (252:2): 0 6289408 linear 7:0 310380544.
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.
- Ubuntu: http://packages.ubuntu.com/de/hardy/kpartx
- Debian: http://packages.debian.org/search?keywords=kpartx
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 #
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 #
Its a full image of a complete hard disk
Comment by smary — June 29, 2009 #
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 #
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 #
Just use it on a device and not an image file. Create the device with losetup first.
Comment by partition mage — July 14, 2009 #
Just found ya…Great stuff.
I will be adding you to my links list and checking in…Comment by andy — March 2, 2010 #
[…] Kpartx: a tool for mounting partitions within an image file […]
Pingback by 通过kpartx使用LVM卷 - 阿泰的菜园 — June 28, 2010 #
[…] 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 #
[…] 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 #
[…] 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 #
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/loop0p5Comment by Ján Sáreník — April 5, 2016 #
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!
YasmineComment by Yasmine — April 15, 2016 #
Leave a comment
kpartx: Create device maps from partition tables.
Command to display kpartx
manual in Linux: $ man 8 kpartx
NAME
kpartx - Create device maps from partition tables.
SYNOPSIS
kpartx[-a|-d|-u|Kpartx Linux
-l][-r][Kpartx Tutorial
-p][-f][-g][-s|-n][-v]wholediskDESCRIPTION
This tool, derived from util-linux' partx, reads partition tables on specifieddevice and create device maps over partitions segments detected. It is calledfrom hotplug upon device maps creation and deletion.OPTIONS
- -g
- Force GUID partition table (GPT).
- -s
- Sync mode (Default). Don't return until the partitions are created.
- -n
- Nosync mode. Return before the partitions are created.
- -v
- Operate verbosely.
EXAMPLE
To mount all the partitions in a raw disk image:- add map loop1p1 (254:4): 0 409597 linear 7:1 3
Kpartx Source
The loop1p1 is the name of a device file under /dev/mapper which youcan use to access the partition, for example to fsck it:
When you're done, you need to remove the devices:
- Parts ExpressThis man page was assembled By Patrick Caulfield for the Debian project.
- kpasswdd (8) - Kerberos 5 password changing server
- kprop (8) - propagate a Kerberos V5 principal database to a slave server
- kpropd (8) - Kerberos V5 slave KDC update server
- kpropd_selinux (8) - Security Enhanced Linux Policy for the kpropd processes
- kproplog (8) - display the contents of the Kerberos principal update log
multipath-tools was developed by Christophe Varoqui <christophe.varoqui [at] opensvc.com>and others.