LinuxCommandLibrary

kpartx

Map partitions within a disk image to devices

TLDR

Add partition mappings

$ kpartx -a [whole_disk.img]
copy

Delete partition mappings
$ kpartx -d [whole_disk.img]
copy

List partition mappings
$ kpartx -l [whole_disk.img]
copy

SYNOPSIS

kpartx [-a|-d|-l] [-p offset] image

PARAMETERS

-a
    Add partition mappings. This option causes kpartx to create device mapper entries for each partition found within the specified image.

-d
    Delete partition mappings. This option removes the device mapper entries associated with the partitions in the specified image. This reverses the action of the -a option.

-l
    List partition mappings. This option displays the partition information and the corresponding device mapper entries without actually creating or deleting any mappings.

-p offset
    Specifies an offset to add to the partition numbers when creating the device mapper entries. This can be useful when dealing with images that have conflicting partition numbers.

image
    The path to the disk image file or block device containing the partition table.

DESCRIPTION

The kpartx command creates device maps from partition tables. It reads partition tables from a specified device image (e.g., a disk image file or a physical device) and creates device mapper entries for each partition. These entries allow access to individual partitions as if they were separate block devices.

This is useful for mounting partitions within disk images, accessing partitions on removable media, or managing complex storage configurations. You can list, add, and delete partition mappings. kpartx simplifies working with partitions without needing to directly manipulate the device mapper. It is often used in scripting and automation for disk image management and virtualization.

CAVEATS

kpartx requires root privileges to operate, as it directly interacts with the device mapper. Incorrect usage can lead to data corruption or system instability. Ensure the image is not in use or mounted before deleting mappings. The tool manipulates device mapper entries, ensure no processes are using these entries before deleting them.

RETURN CODES

kpartx returns 0 on success, and a non-zero value on failure. Errors can occur due to invalid image files, insufficient permissions, or problems with the device mapper.

EXAMPLES

Adding partition mappings for disk image:
kpartx -a mydisk.img

Deleting partition mappings for disk image:
kpartx -d mydisk.img

Listing partition mappings for disk image:
kpartx -l mydisk.img

HISTORY

kpartx is part of the `multipath-tools` package. It was developed to simplify the process of managing partitions within disk images using the device mapper. It helps in accessing partition content easily in a controlled and scriptable manner. The tool helps automate the process of partitioning and working with different disk images.

SEE ALSO

losetup(8), dmsetup(8), mount(8)

Copied to clipboard