Using kpartx to Manage Partition Mappings (with examples)
- Linux
- November 5, 2023
Add partition mappings:
kpartx -a whole_disk.img
Motivation: Adding partition mappings allows us to access individual partitions within a disk image file without the need for manual calculations or mounting the entire disk image.
Explanation: The -a
option instructs kpartx
to add partition mappings for the specified disk image file. In this case, whole_disk.img
is the disk image file for which we want to create partition mappings.
Example Output: Suppose we have a whole_disk.img
file that contains partitions /dev/loop0p1
, /dev/loop0p2
, and /dev/loop0p3
. After running the kpartx -a whole_disk.img
command, kpartx
will create device mappings for each partition, making them accessible as /dev/mapper/loop0p1
, /dev/mapper/loop0p2
, and /dev/mapper/loop0p3
.
Delete partition mappings:
kpartx -d whole_disk.img
Motivation: Deleting partition mappings is useful when we no longer need to access individual partitions within a disk image file. This allows us to clean up and avoid potential conflicts with other disk operations.
Explanation: The -d
option tells kpartx
to delete partition mappings associated with the specified disk image file. In this example, we are deleting partition mappings for whole_disk.img
.
Example Output: If we have previously added partition mappings for whole_disk.img
using the kpartx -a
command, running kpartx -d whole_disk.img
will remove the device mappings associated with the partitions. The output will not contain any information if the command is successful.
List partition mappings:
kpartx -l whole_disk.img
Motivation: Listing partition mappings helps us to identify and confirm which partitions are associated with a particular disk image file. This is especially useful when working with complex disk setups or troubleshooting partition-related issues.
Explanation: The -l
option tells kpartx
to list partition mappings for the specified disk image file. Here, whole_disk.img
is the disk image file for which we want to display the partition mappings.
Example Output: Suppose we have previously added partition mappings for whole_disk.img
. Running kpartx -l whole_disk.img
will display a table of partition mappings associated with the disk image file. The output may look something like the following:
loop0p1 : 0 123456 /dev/loop0 2048
loop0p2 : 0 123456 /dev/loop0 123450368
loop0p3 : 0 123456 /dev/loop0 246900736
The output will show the partition name (loop0p1
, loop0p2
, etc.), the starting sector, the sector count, the device path (/dev/loop0
in this case), and the offset in sectors.