How to mount one partition from an image file that contains multiple partitions on Linux? How to mount one partition from an image file that contains multiple partitions on Linux? linux linux

How to mount one partition from an image file that contains multiple partitions on Linux?


You might do it like this, without much hassle:

# kpartx -v -a logging-test.img add map loop0p1 (251:0): 0 497664 linear /dev/loop0 2048add map loop0p2 (251:1): 0 66605058 linear /dev/loop0 501758add map loop0p5 (251:2): 0 66605056 251:1 2# ls /dev/mapper/control  loop0p1  loop0p2  loop0p5# mount /dev/mapper/loop0p1 /mnt/test# mount  | grep test/dev/mapper/loop0p1 on /mnt/test type ext2 (rw)#

And to remove the loop device after you finished:

# kpartx -v -d logging-test.imgdel devmap : loop0p2del devmap : loop0p1loop deleted : /dev/loop0#


If you have util-linux v2.21 or higher, you can now do this with losetup. Use the -P (--partscan) option to read the partition table and create device nodes for each partition:

# losetup --show -f -P test.img/dev/loop0# ls /dev/loop0*/dev/loop0/dev/loop0p1/dev/loop0p2# mount /dev/loop0p1 /mnt/tmp


Let's say $IMAGE is set to the path to your image file.You could write a small script by using

fdisk -u sectors -l $IMAGE

to get a list of partitions inside the image. And then use a sequence of

mount -o ro,loop,offset=$OFFSET -t auto $IMAGE /media/$DEST

Where offset is calculated means the info from fdisk (start sector * size of a sector in bytes) and $DEST a unique name for each of the partitions.

That's not directly the solution but I hope a pretty good indication on how to realize it. If you make the job once, you've some small nice beginning for some forensic toolkit!​​​​​​​​​​​​​​​​​​​​​