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

LinuxFilesystemsMountLoopback

Linux Problem Overview


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

loopback devices might be a possibility.

Related threads:

Linux Solutions


Solution 1 - 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 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
#

Solution 2 - Linux

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

Solution 3 - Linux

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

Solution 4 - Linux

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"
)

Solution 5 - Linux

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

Solution 6 - Linux

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAtlas1jView Question on Stackoverflow
Solution 1 - LinuxAndrew YView Answer on Stackoverflow
Solution 2 - LinuxAndrew OakleyView Answer on Stackoverflow
Solution 3 - LinuxjdehaanView Answer on Stackoverflow
Solution 4 - LinuxCiro Santilli Путлер Капут 六四事View Answer on Stackoverflow
Solution 5 - LinuxPfiverView Answer on Stackoverflow
Solution 6 - LinuxAlexander AmelkinView Answer on Stackoverflow