kpartx(8)
Create device mappings for partitions on a loop device or disk image.
Synopsis
kpartx [OPTION]... [-a|-d|-u] DEVICEDescription
kpartx is used to create or remove device mappings for partitions found on a block device or disk image file. It reads a partition table and creates device mapper entries for each partition, allowing you to mount individual partitions from a disk image without needing to mount the entire image first.
This tool is commonly used with loop devices to work with partition tables in ISO images, disk dumps, and virtual machine images. The device mapper creates virtual block devices under /dev/mapper/ that correspond to each partition.
Common options
| Flag | What it does |
|---|---|
-a | add partition mappings (create /dev/mapper entries) |
-d | delete partition mappings (remove /dev/mapper entries) |
-u | update existing partition mappings |
-l | list current partition mappings without adding or removing |
-p | specify partition naming scheme (e.g., 'p' for p1, p2) |
-g | guess the partition table type automatically |
-t | specify partition table type (dos, gpt, bsd, etc.) |
-s | sync partition mappings with partition table |
-v | verbose output |
Examples
Attach a disk image to a loop device and create partition mappings
losetup /dev/loop0 disk.img && kpartx -a /dev/loop0List all partitions on the loop device without creating mappings
kpartx -l /dev/loop0Add partition mappings with 'p' prefix (creates loop0p1, loop0p2, etc.)
kpartx -a -p p /dev/loop0Mount the first partition after kpartx has created the mappings
mount /dev/mapper/loop0p1 /mntRemove all partition mappings for the loop device
kpartx -d /dev/loop0Force GPT partition table type and add mappings for a USB drive
kpartx -t gpt -a /dev/sdbCreate partition mappings for a network block device
kpartx -a /dev/nbd0