• Edit
  • Delete

mount disk image files / loop / kpartx

apt-get install kpartx

Show partitions

  • fdisk -l myserver.root.img

List device mappings that would be created

  • kpartx -l myserver.root.img

Create mappings / create dev for image

  • kpartx -av myserver.root.img
    • add map loop0p1 (252:1): 0 310378496 linear 7:0 2048
      add map loop0p2 (252:2): 0 6289408 linear 7:0 310380544
    • the device is
      • /dev/loop0
    • the partions are
      • /dev/mapper/loop0p1
        /dev/mapper/loop0p1

Mount a partition

  • mount -o loop /dev/mapper/loop0p1 /mnt/

Find out what image file a loop belongs to

  • losetup --list
    • /dev/loop0         0      0         0  0 /path/to/myserver.root.img
  • cat /sys/class/block/loop0/loop/backing_file
    • /path/to/myserver.root.img

If you want to get the exact loop device for a script you can do

  • losetup --list | grep "/path/to/myserver.root.img" | cut -d ' ' -f1
    • /dev/loop0

Remove mapping

  • kpartx -d myserver.root.img