$linuxjunkies
>

kpartx(8)

Create device mappings for partitions on a loop device or disk image.

UbuntuDebianFedoraArch

Synopsis

kpartx [OPTION]... [-a|-d|-u] DEVICE

Description

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

FlagWhat it does
-aadd partition mappings (create /dev/mapper entries)
-ddelete partition mappings (remove /dev/mapper entries)
-uupdate existing partition mappings
-llist current partition mappings without adding or removing
-pspecify partition naming scheme (e.g., 'p' for p1, p2)
-gguess the partition table type automatically
-tspecify partition table type (dos, gpt, bsd, etc.)
-ssync partition mappings with partition table
-vverbose output

Examples

Attach a disk image to a loop device and create partition mappings

losetup /dev/loop0 disk.img && kpartx -a /dev/loop0

List all partitions on the loop device without creating mappings

kpartx -l /dev/loop0

Add partition mappings with 'p' prefix (creates loop0p1, loop0p2, etc.)

kpartx -a -p p /dev/loop0

Mount the first partition after kpartx has created the mappings

mount /dev/mapper/loop0p1 /mnt

Remove all partition mappings for the loop device

kpartx -d /dev/loop0

Force GPT partition table type and add mappings for a USB drive

kpartx -t gpt -a /dev/sdb

Create partition mappings for a network block device

kpartx -a /dev/nbd0

Related commands