Dezember 2009 Archives

Fr 11. Dez 18:22:55 CET 2009

Access partitions in loopback devices under linux

Ever needed to access a partition inside a block device not supporting partitions like /dev/loop or software raid? There exists multiple solutions like specifying the offset via losetup or patching the kernel. But this is error-prone and not very user friendly.

An easy and more user friendly way of supporting partition tables in any block device is kpartx. Besides the name, is has nothing to do with kde but is a tool from multipath tools to map partition tables to the device mapper.

After you installed kpartx you can easily create mappings from a block device with kpartx -a and delete them via kpartx -d. For example:

# echo p | fdisk /dev/loop0 
Command (m for help): 
Disk /dev/loop0: 102 MB, 102400000 bytes
255 heads, 63 sectors/track, 12 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x00000000

      Device Boot      Start         End      Blocks   Id  System
      /dev/loop0p1               1           5       40131   83  Linux
      /dev/loop0p2               6          12       56227+  83  Linux

# kpartx -a /dev/loop0
# ls /dev/mapper/loop0p*
/dev/mapper/loop0p1  /dev/mapper/loop0p2
# kpartx -d /dev/loop0
# ls /dev/mapper/loop0*
ls: cannot access /dev/mapper/loop0*: No such file or directory

To make a long story short: kpartx is a easy and handy solution for using partitions from any block device under linux making management of images for virtual machines quite simple.

Posted by Ulrich Dangel | Permanent Link | Categories: Shell stuff