kpartx
kpartx is a Linux command-line utility used to create device mappings for partitions in a disk image file or physical disk. It reads partition information from a disk (either physical or image) and creates corresponding devices in /dev/mapper/
, which can then be used as if they were individual partitions.
This tool is commonly used in situations where you need to mount partitions inside disk images (such as .iso
, .img
, or other formats), or in cases where you have a physical disk with multiple partitions, but they are not automatically recognized.
Purpose
Create Device Mappings for Partitions: It maps the partitions inside a disk image or physical device to
/dev/mapper/
devices, allowing users to access them separately.Access Partitions in Disk Images: Helps in accessing partitions within disk images (e.g.,
.iso
,.img
,.vmdk
), typically used when dealing with virtual machines or system recovery.Manage LVM and MD RAID Devices: Can be used in environments with LVM (Logical Volume Management) or RAID (Redundant Array of Independent Disks) to map and manage partitions.
Basic Syntax
<disk-image|device>
: Path to the disk image file (e.g.,.img
,.iso
) or a physical device (e.g.,/dev/sda
).
Common Options
-a
: Add partition mappings for the specified device or image.This will create mappings for all partitions on
/dev/sda
and add them under/dev/mapper/
.-d
: Delete the partition mappings for the specified device or image.This will remove all partition mappings created by
kpartx
for the device.-l
: List the partitions of the specified device or disk image.This will list all the partitions on
/dev/sda
, showing the names and details of the partitions.-v
: Display verbose output to show more details about what is being done (e.g., mapping devices or adding/removing partitions).
Example Usage
Listing Partitions in a Disk Image
To list the partitions in a disk image:
This command will list all partitions inside the disk image
disk-image.img
.Creating Partition Mappings
After identifying the partitions in a disk image or physical device, you can create mappings for them:
This will create device mappings for each partition inside the disk image, allowing access to them via
/dev/mapper/
.Removing Partition Mappings
To remove the mappings created for a disk image or device:
This command will delete the mappings for the partitions in the specified disk image.
Working with Physical Devices
You can also use
kpartx
on physical devices, such as/dev/sda
, to map the partitions:This will add mappings for all partitions on
/dev/sda
under/dev/mapper/
.
Example Output
Listing Partitions
This output shows two partitions on
/dev/sda
:/dev/sda1
and/dev/sda2
, with their respective partition sizes in sectors.Creating Mappings
This output shows that
kpartx
created two device mappings for the partitions/dev/sda1
and/dev/sda2
under/dev/mapper/
.
Best Practices
Use With Disk Images:
kpartx
is especially useful when dealing with disk images that contain multiple partitions. For example, when working with virtual machine disk images or recovering data from an image,kpartx
helps map the partitions into accessible devices.Backup Before Modifying: When working with physical devices, always ensure that you have backups of important data before creating or removing partition mappings.
Combine with Mounting: Once the partitions are mapped with
kpartx
, you can mount them like any other device usingmount
:
Conclusion
kpartx
is a useful tool for managing partition mappings in Linux, enabling users to work with multiple partitions in disk images or physical devices. It's ideal for environments where accessing individual partitions within disk images or creating mappings for RAID/LVM configurations is necessary. By using kpartx
, administrators can easily mount and manipulate partitions that are not directly available as block devices.
Last updated