Manually Mapping Partition Tables to Devices

The kpartx utility maps to device files the partitions of any block device or file that contains a partition table. The command reads the partition table, creates device files for the partitions, and stores the device files in /dev/mapper. Each device file represents a disk volume or a disk partition on a device or within an image file.

For more information, see the kpartx(8) manual page.

Creating Device Mappings by Using kpartx

The -a option creates the device mappings. The following example uses the disk partitions that were created in Creating Partitions as basis for creating the mapping. The example begins by showing the partition table:

  1. Display the partition table.

    sudo fdisk -l /dev/sdb
    ...
    Device     Boot   Start      End  Sectors  Size Id Type
    /dev/sdb1          2048  3907583  3905536  1.9G 83 Linux
    /dev/sdb2       3907584 32767999 28860416 13.8G 83 Linux
  2. Map the partitions.

    sudo kpartx -av /dev/sdb
    add map sdb1 (253:2): 0 3905536 linear 8:16 2048
    add map sdb2 (253:3): 0 28860416 linear 8:16 3907584
  3. Display /dev/mapper contents.

    ls /dev/mapper
    control  sdb1  sdb2  vg_main-lv_root  vg_main-lv_swap

Listing Partition Mappings For a Device by Using kpartx

To list the partitions in the device, use the -l option.

In the following example, the first column of the output identifies the device files in /dev/mapper.

sudo kpartx -l /dev/sdb
sdb1 : 0 3905536 /dev/sdb 2048
sdb2 : 0 28860416 /dev/sdb 3907584

The kpartx command also works with image files such as an installation image. For example, for an image file system.img, you can do the following:

sudo kpartx -a system.img
sudo kpartx -l system.img
loop0p1 : 0 204800 /dev/loop0 2048
loop0p2 : 0 12288000 /dev/loop0 206848
loop0p3 : 0 4096000 /dev/loop0 212494848
loop0p4 : 0 2 /dev/loop0 16590848

The output of the previous command shows that the drive image contains four partitions.

Removing Partition Mappings by Using kpartx

If a partition isn't in use you can remove the device mapping for the partition by using the -d option:

sudo kpartx -d system.img
ls /dev/mapper
control