up vote 25 down vote accepted

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!​​​​​​​​​​​​​​​​​​​​​

share|improve this answer
1  
While this answer works, the the more recent kpartx answer is really better; it gives you a single command that makes all the partitions available. – mkj Apr 9 '12 at 21:51
1  
While kpartx is a nice tool, it is not available on every system. Several embedded systems I work on don't have it natively and I need to mount partitions of disk images or even sub partitions of a partition. – gnac Oct 18 '16 at 3:16
    
example application of above answer for Raspibian images: raspberrypi.stackexchange.com/a/13138/359 – Grzegorz Wierzowiecki Feb 4 '17 at 8:43

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 2048
add map loop0p2 (251:1): 0 66605058 linear /dev/loop0 501758
add 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.img
del devmap : loop0p2
del devmap : loop0p1
loop deleted : /dev/loop0
#
share|improve this answer
10  
Should also mention -d for tearing the setup down. – Jeremy Visser Feb 18 '12 at 13:42
1  
Good point, thanks! Don't have the full-disk image now handy to play though, so those in need might need to man kpartx :-) – Andrew Y Feb 20 '12 at 14:20
4  
To tear it down, you merely replace "-a" with "-d"; in the example here that would be kpartx -v -d logging-test.img – mkj Apr 9 '12 at 21:48
3  
+1 This should be the accepted answer, because this works even if modprobe loop misses the max_part=63 (or similar) parameter. – Tino May 29 '14 at 12:49
    
fantastic, thanks. – Spongman Dec 19 '14 at 6:50

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
share|improve this answer
1  
does not work for me. using util-linux-ng 2.22.2, I get only /dev/loop0 ... – Fabian Henze Apr 20 '13 at 20:34
2  
I used losetup from util-linux 2.22.2 successfully as described above (Mageia 3 package util-linux-2.22.2-5.mga3.src.rpm). – kbulgrien Aug 31 '14 at 2:21
    
One thing I noticed, though, is that using root, instead of sudo, made a difference whether it worked or not. – kbulgrien Aug 31 '14 at 2:31
    
For me, I see the different partitions under loop0 when I run fdisk -l /dev/loop0, such as /dev/loop0p1, but none of the partitions can be mounted. – zymhan Jan 14 '16 at 22:29
1  
Works for me on Fedora 24, util-linux 2.28.2 – LiuYan 刘研 Sep 27 '16 at 16:50

losetup -P automation

losetup -P is the best method starting in Ubuntu 16.04 as mentioned at https://stackoverflow.com/a/15200862/895245 , here are functions to automate if further. Usage:

$ los my.img
/dev/loop0
/mnt/loop0p1
/mnt/loop0p2

$ ls /mnt/loop0p1
/whatever
/files
/youhave
/there

$ # Cleanup.
$ losd 0
$ ls /mnt/loop0p1
$ ls /dev | grep loop0
loop0

Source:

los() (
  img="$1"
  dev="$(sudo losetup --show -f -P "$img")"
  echo "$dev"
  for part in "$dev"?*; do
    dst="/mnt/$(basename "$part")"
    echo "$dst"
    sudo mkdir -p "$dst"
    sudo mount "$part" "$dst"
  done
)
losd() (
  dev="/dev/loop$1"
  for part in "$dev"?*; do
    dst="/mnt/$(basename "$part")"
    sudo umount "$dst"
  done
  sudo losetup -d "$dev"
)
share|improve this answer

Ok, this question is aeons old, but just for the sako of completeness: This here seems a lot easier to me.

Quote:

rmmod loop
modprobe loop max_part=63
losetup -f /path/to/your/disk/image.raw
mount /dev/loop0p1 /mnt/path
share|improve this answer
1  
This fails on Ubuntu 14.04 because the loop module is built-in so rmmod does not work. How to fix that: askubuntu.com/a/673257/52975 – Ciro Santilli 华涌低端人口 六四事件 法轮功 Sep 12 '15 at 11:05

Some more automation to the previous answers that were great.

To further simplify the task (which is needed if you do it often), you may use my script mountimg to do everything for you. Just get it from https://github.com/AlexanderAmelkin/mountimg and use like this:

mountimg disk_image.img $PARTNO /mnt/mountpoint

You may as well specify filesystem type and any other additional mount options if you like:

mountimg disk_image.img $PARTNO /mnt/mountpoint -t vfat -o codepage=866,iocharset=utf-8

When you're done with the partition, simply umount it:

umount /mnt/mountpoint
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.