57

The image file has a partition table, and it contains multiple partitions.

loopback devices might be a possibility.

Related threads:

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Atlas1j
  • 2,442
  • 3
  • 27
  • 35

7 Answers7

92

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
#
Fritz
  • 1,293
  • 15
  • 27
Andrew Y
  • 5,107
  • 2
  • 28
  • 29
  • 10
    Should also mention -d for tearing the setup down. – Jeremy Visser Feb 18 '12 at 13:42
  • 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 09 '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
  • What about the block size? You cannot set the block size of the original device with this tool. If you run `fdisk` on the file it assumes 512. The partition table stores positions as block counts, so the block size does matter. In the answer by @jdehaan you can say `-o offset=$OFFSET,block=4096` if that is the case. – Notinlist Mar 03 '15 at 10:36
  • 1
    This is no longer the best alternative with losetup -P. – kiko Feb 13 '19 at 01:19
  • It woks fine on ubuntu 19.10 and a virtual box converted img file! Thanks! – wensiso Feb 19 '20 at 20:53
70

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
Jeremy Visser
  • 6,237
  • 1
  • 25
  • 30
Andrew Oakley
  • 801
  • 6
  • 2
  • 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 02: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 02:31
  • 2
    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
  • 1
    Finally, the real solution. Anyone having trouble with this is likely running an ancient distro and should fix that first! – Navin Jun 12 '17 at 14:22
  • I know this is 7 years old now, but it deserves more upvotes due to how long it took me to discover this solution! – C. Dunn May 01 '20 at 17:03
32

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

APerson
  • 8,140
  • 8
  • 35
  • 49
jdehaan
  • 19,700
  • 6
  • 57
  • 97
  • 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 09 '12 at 21:51
  • 2
    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 03:16
  • example application of above answer for Raspibian images: http://raspberrypi.stackexchange.com/a/13138/359 – Grzegorz Wierzowiecki Feb 04 '17 at 08:43
  • This is really no longer the best alternative, with losetup -P available. – kiko Feb 13 '19 at 01:18
11

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

$ sudo losetup -l
NAME       SIZELIMIT OFFSET AUTOCLEAR RO BACK-FILE                                                                                      DIO
/dev/loop1         0      0         0  0 /full/path/to/my.img

$ # 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
    if [ "$part" = "${dev}p*" ]; then
      part="${dev}"
    fi
    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
    if [ "$part" = "${dev}p*" ]; then
      part="${dev}"
    fi
    dst="/mnt/$(basename "$part")"
    sudo umount "$dst"
  done
  sudo losetup -d "$dev"
)
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
3

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
Pfiver
  • 249
  • 2
  • 6
0

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
Alexander Amelkin
  • 759
  • 1
  • 9
  • 15
0

Just an addition to Andrew Y's answer.

If you run into this error:

/dev/mapper/control: open failed: Permission denied
Failure to communicate with kernel device-mapper driver.

Then you just need to run the command again using sudo.

I know it's not much of an answer, but it might help those that are fairly new to linux.

Also as of right now, the solution still works just fine, and I'm running it on Kali linux 2023.1

KyleDev
  • 11
  • 1
  • 5