183

I made an image of my entire disk with

dd if=/dev/sda of=/media/external_media/sda.img

Now the problem is I'd like to mount an ext4 filesystem that was on that disk but

mount -t ext4 -o loop /media/external_media/sda.img /media/sda_image

obviously gives a superblock error since the image contains the whole disk (MBR, other partitions) not just the partition I need. So I guess I should find a way to make the disk image show up in the /dev/ folder...

Does anyone know how to do that?

PS: I can always dd back the image to the original disk, but that would be very inconvenient (I updated the OS and I'd like to keep it as it is)

Zanna
  • 72,312

10 Answers10

211

Get the partition layout of the image

$ sudo fdisk -lu sda.img
...
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
...
  Device Boot      Start         End      Blocks   Id  System
sda.img1   *          56     6400000     3199972+   c  W95 FAT32 (LBA)

Calculate the offset from the start of the image to the partition start

Sector size * Start = (in the case) 512 * 56 = 28672

Mount it on /dev/loop0 using the offset

sudo losetup -o 28672 /dev/loop0 sda.img

Now the partition resides on /dev/loop0. You can fsck it, mount it etc

sudo fsck -fv /dev/loop0
sudo mount /dev/loop0 /mnt

Unmount

sudo umount /mnt
sudo losetup -d /dev/loop0
n611x007
  • 565
arrange
  • 15,219
162

Update for Ubuntu 16.04: With the new losetup this is now easier:

sudo losetup -Pf disk_image.raw

See the rest of the answer for older versions of Ubuntu.


An easy solution is using kpartx: it will figure out the partition layout and map each to a block devices. After that all you have to do is mount the one you want.

Open Terminal, locate the disk image, and enter this command:

$ sudo kpartx -av disk_image.raw 
add map loop0p1 (252:2): 0 3082240 linear /dev/loop0 2048
add map loop0p2 (252:3): 0 17887232 linear /dev/loop0 3084288

This created loop0p1 and loop0p2 under /dev/mapper. From the output you can see the sizes of the partitions which helps you identify them. You can mount the one you want with:

$ sudo mount /dev/mapper/loop0p2 /mnt

Alternatively, the block device is detected by Nautilus and you can mount it from the side bar:

enter image description here

When you are done, unmount what you mounted and remove the device mapping:

$ sudo umount /mnt
$ sudo kpartx -d disk_image.raw
Joni
  • 2,607
46

Edit : works with util-linux >=2.21. At the time of writing ubuntu ships with version 2.20 only

From man losetup :

       -P, --partscan
          force kernel to scan partition table on newly created loop device

So just run

$ sudo losetup -f --show -P /path/to/image.img

to create device nodes for every partition of your disk image on the first unused loop device and print it to stdout. If using /dev/loop0 device it will create at least /dev/loop0p1 that you will be able to mount as usual.

14

Try gnome-disk-image-mounter:

gnome-disk-image-mounter sda.img

No sudo required. It will be mounted at /media/your_user_name/partition_name, just like USB drives.

Zanna
  • 72,312
9

losetup -P automation

losetup -P is the best method starting in Ubuntu 16.04 as mentioned at https://askubuntu.com/a/496576/52975 , 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"
)

loop module max_part config

This is a decent method before 16.04.

loop is a kernel module, built into the kernel in Ubuntu 14.04.

If you configure it right, Linux automatically splits up the devices for you.

cat /sys/module/loop/parameters/max_part

says how many partitions loop devices can generate.

It is 0 by default on Ubuntu 14.04 which is why no auto-splitting happens.

To change it, we can either add:

options loop max_part=31

to a file in /etc/modprobe, or:

GRUB_CMDLINE_LINUX="loop.max_part=31"

to /etc/default/grub and then sudo update-grub.

How to set a module parameter is also covered at: How to add kernel module parameters?

After a reboot, when you do:

sudo losetup -f --show my.img

it mounts the image to a /dev/loopX device, and automatically mounts the partitions to /dev/loopXpY devices.

So this is the most convenient method if you are willing to reboot.

See also

9

The simplest way, in my opinion, is using mount -o loop,offset=... as mentioned in this answer on StackOverflow. The idea is as follows:

fdisk -l $IMAGE
# calculate the offset in bytes
mount -o loop,offset=$OFFSET $IMAGE $MOUNTPOINT

The method is best because it doesn't require deleting the loop device after you umount the mounted partition.

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 /media/external_media/sda.img 2 /media/sda_image

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

mountimg /media/external_media/sda.img 2 /media/sda_image -t vfat -o codepage=866,iocharset=utf-8

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

umount /media/sda_image
8

Use losetup to attach the whole disk image.

# sudo losetup /dev/loop2 sda.img

Then use mdadm to create an md device and block devices will be created for all of the partitions.

# sudo mdadm --build --level=0 --force --raid-devices=1 /dev/md2 /dev/loop2
mdadm: array /dev/md2 built and started.

Now you should see the partition devices.

nile-172-b0fef38-76:/mnt/sdb1 # ls -l /dev/md2*
brw-rw---- 1 root disk   9, 2 Oct 10 12:37 /dev/md2
brw-rw---- 1 root disk 259, 0 Oct 10 12:37 /dev/md2p1
1

Here's a user-friendly and interactive way that requires that the excellent fzf utility is installed, as well as sudo.

First add this function to your .bashrc or .zshrc:

pmount() { sudo mount -o loop,offset=$(($(sudo fdisk -lu "$1" | sed -n '/^Device/,/^$/p' | tail -n+2 | fzf | sed -E -e 's/[[:blank:]]+/ /g' | cut -d' ' -f2- | sed 's@^[^0-9]*\([0-9]\+\).*@\1@') * 512)) "$1" "$2"; }

Then reload your shell (or type $SHELL).

You can then mount .img files with this command:

pmount my.img /mnt

A list pops up where you can select which permision you wish to mount, using the arrow keys and then pressing return.

enter image description here

Unmount with sudo umount /mnt.

Alexander
  • 113
0

How do I open / mount my ISO file when normal methods throw this error? wrong fs type, bad option, bad superblock on /dev/loop0, missing codepage or helper program, or other error. I tried several methods but they all fail somehow.

I'm actually answering this problem (Unable to mount an ISO file), which problem I also have, and have just solved, but the original question has been closed, and refers to this question, even though they seem different.

I feel that curators often close questions for indecipherable reasons, however, I don't have any choice but to use the closure and reference given. If it's wrong, perhaps the previous question can be re-opened, and this answer put there, where I'd like it to be? Since this user edited the original question, I suspect he might have been the one to close the original: https://askubuntu.com/users/301745/wjandrea

Anyway, leaving the filing issues behind, here's an example of trying to mount the ISO:

$ sudo mount -o ro,loop -t iso9660 nero.iso /mnt
mount: /mnt: wrong fs type, bad option, bad superblock on /dev/loop0, missing codepage or helper program, or other error.

I called the ISO nero.iso because using the file command revealed the ISO is an artifact of the "Nero" CD Burning software, not that that helps any:

$ sudo file nero.iso
nero.iso: Nero CD image at 0x4B000 UDF filesystem data (version 1.5) 'nero'

After trying a few different things I came across a reference to the software iat which says it "converts many CD-ROM image formats to iso9660". Woah. It immediately, and effortlessly, works:

$ sudo apt install iat
$ man iat
$ iat nero.iso out.iso
Iso9660 Analyzer Tool v0.1.3 by Salvatore Santagati
Licensed under GPL v2 or later

Detect Signature ISO9660 START at 339968 Detect Signature ISO9660 END at 342016

Image offset start at 307200 Sector header 0 bit Sector ECC 0 bit Block 2048 Done

Really? Now the mount command silently completes:

$ sudo mount -o ro,loop out.iso /mnt

And, Behold, the ISO is mounted, and Handbrake will now happily rip the content.

Thank you, Salvatore Santagati. I've always liked coffee... and ice-cream.

I hope anyone else with this problem is able to follow the reference from the previous question and find this answer despite the obscure filing!

NeilG
  • 244
0

I ended up writing a simple FUSE application to do this without the need for root - source here: https://gist.github.com/tchatow/7751287f218a0cc39b2038a8e04c704d

Usage: main -i image.img mountpoint. Tested on GPT formatted image.

tchatow
  • 121
  • 3