Kpartx

From Notes_Wiki

Home > CentOS > CentOS 6.x > Filesystem or partition tools > Kpartx

Mounting partitions from full disk image with multiple partitions

In order to mount partitions from full disk images (created probably with dd) one can use following method:

  1. First print partitions present in image using 'parted <filename> print' or 'fdisk -ul <filename>'
  2. Then use 'kpartx -va <filename>;' to mount entire file as device on '/dev/loop{n}' and its partitions in '/dev/mapper/loop{n}p{q}'.
  3. Then we can mount devices in '/dev/mapper' using normal mount command.
  4. After work is over one can use 'kpartx -vd <filename>' to delete the '/dev/mapper' devices and freeing the '/dev/loop{n}' device.


Using -oloop,offset={n} syntax to mount partitions

We can also use '-o loop,offset={n}' syntax of mount command to mount partitions from full disk image. For that we have to first print the partition offset and size using 'fdisk -ul <filename>' syntax. Then we can multiply the sector number with sector size (usually 512) and get offset in bytes for each partition. Then using above syntax we can mount the partition from full disk image. On some old systems the syntax may not work if offset cannot be specified in 32 bits, that is if offset is greater then 4 GB. With today's disks it means that we can mount only first partition.

To get around this limit one can use 'dd if=<full_disk_image> of=<partition> bs=<sector_size> skip=<start> count=<blocks>' syntax to extract partition image from full disk and then work on it.

To put the partition image back into full disk image we cant use just simple 'dd' command. Hence we would have to extract part of disk before starting of partition in file, say 'before_partition' and part of disk after partition in file, say 'after_partition'. Then we can use 'cat before_partition partition after_partition > new_disk_image' to get the entire disk back. Hence, if possible the kpartx method is much more faster, efficient and elegant.


Creating more loopback devices

One can create more loopback devices using 'mknod' command. Use syntax similar to below command to create additional loop devices

mknod /dev/loop8 b 7 8

It is recommended to do 'ls -l /dev/loop*' before running above command to see how many loop devices exist, what is their major number and what is their minor number. You can also verify that they are indeed block devices and not character or any other kind of devices.


Troubleshooting

SELinux error

If SELinux is in enforcing mode, kpartx can give errors like

read error, sector 0
read error, sector 1
read error, sector 29

which can be mis-interpreted as filesystem read errors. This can be solved by disabling SELinux temporarily using 'setenforce 0'


Home > CentOS > CentOS 6.x > Filesystem or partition tools > Kpartx