kpartx
Map partitions within a disk image to devices
TLDR
Add partition mappings and print created mappings
Delete partition mappings
List partition mappings
SYNOPSIS
kpartx [options] [device|file]
PARAMETERS
-a, --add
Add partition mappings to /dev/mapper/
-d, --delete
Delete partition mappings
-l, --list
List partition table entries without mapping
-u, --update
Update existing partition mappings
-p, --part-postfix prefix
Set partition name postfix (default: p)
-g, --gpt
Detect only GPT partition tables
-r, --dos
Detect only DOS/MBR tables (default)
-v, --verbose
Enable verbose output
DESCRIPTION
kpartx is a Linux utility for managing device-mapper mappings of partitions from block devices or disk image files. It reads partition tables (DOS/MBR or GPT) and creates corresponding device nodes in /dev/mapper/, such as /dev/mapper/loop0p1 for the first partition on /dev/loop0.
This is essential for accessing individual partitions in whole-disk images without manual loop device setup. Common workflows involve pairing with losetup: set up a loop device for an image, run kpartx -a to map partitions, mount them, then kpartx -d to clean up.
Key features include listing partitions without mapping (-l), updating existing maps (-u), and custom naming prefixes (-p). It supports verbose output for troubleshooting. Primarily used in forensics, virtualization, live CDs, and multipath storage environments where direct partition access is needed.
kpartx relies on the device-mapper kernel module and does not handle LVM, RAID, or encrypted volumes directly—use specialized tools like cryptsetup or mdadm for those. Operations are safe for read-only images but require caution with live devices to avoid data corruption.
CAVEATS
Requires root privileges and device-mapper kernel module. Does not unmount filesystems or handle LVM/RAID/encryption. Risk of naming conflicts on live devices; always umount before -d.
EXAMPLES
losetup -f image.img (e.g., /dev/loop0)
kpartx -a /dev/loop0 (maps partitions)
mount /dev/mapper/loop0p1 /mnt
kpartx -d /dev/loop0
losetup -d /dev/loop0
kpartx -l image.img (dry-run list)
HISTORY
Introduced around 2004-2005 in multipath-tools (device-mapper-multipath) for handling partitions on mapped devices; evolved for broad use with loop devices and images in distros like Debian/Ubuntu.


