Ubuntu mount partition of raw harddrive images via loopback

First we need the device mapper kernel Module.

modprobe dm_mod

Then you have to install kpartx. It creates device maps from partition tables

apt-get install kpartx

Next we setup the image file - here harddrive.img - for use with /dev/loop0

losetup /dev/loop0 harddrive.img

Here is where we use kpartx - look at the example output.

kpartx -a -v /dev/loop0

gpt: 0 slices
dos: 6 slices
add map loop0p1 (254:0): 0 1333332 linear /dev/loop0 63
add map loop0p2 (254:1): 0 11309760 linear /dev/loop0 1333395
add map loop0p5 (254:2): 0 11309697 254:1 63

Finally we can mount the single partitions of the whole drive image file.

mount /dev/mapper/loop0p1 /mountpoint/

Did it work for you. Just drop a comment on it.

11 comments:

Anonymous said...

Just what i was looking for sweet worked perfectly

orbitaldecay said...

Thanks man, this was very useful. I've been playing around with VirtualBox and I've been looking for an easy way to copy files from a virtual drive.

Using "VBoxManage internalcommands converttoraw" to convert the virtual drive to a raw disk image, then mounting the disk image using the method you've described has worked beautifully!

István said...

Works fine with SD card image.
Thanks.

Abhishek Gupta said...

It gives me following error:

mount: special device /dev/mapper/loop0p1 does not exist

on executing

mount /dev/mapper/loop0p1 /mnt

timcour said...

It gives me following error:

mount: special device /dev/mapper/loop0p1 does not exist

on executing

mount /dev/mapper/loop0p1 /mnt

---------
Does loop0 have a partition table?
fdisk -l /dev/loop0

This is one possible reason /dev/mapper/loop0pN doesn't exist.

Anonymous said...

Thanks for the recipe. Works for me on an SD card image.

Anonymous said...

thanks man!

Jack said...

Great tip. And once you are done, repeat the steps in the reverse order:

umount /mountpoint/
kpartx -d -v /dev/loop0
losetup -d /dev/loop0

Erick Jhonson said...

Thanks for your post, you have done a terrific job. Keep it up.
pst recovery

Anonymous said...

mount needs to know the filesystem type. what is it?

Anonymous said...

is there a way to do this with windows ?