Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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 an orrible 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)

share|improve this question
add comment

2 Answers

up vote 24 down vote accepted

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) 56 * 512 = 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
share|improve this answer
    
Thank you so much! Worked like a charm! You made my day (and saved an innocent Ocelot from being brutally deleted ;P) –  Nicola Feltrin Oct 21 '11 at 17:10
4  
At least on bash, the shell can even do the math for you: sudo losetup -o $((56*512)) /dev/loop0 sda.img –  Enno Gröper Jan 13 '13 at 11:55
    
This is not just helpful, this is absolutely awesome. –  andho Feb 18 '13 at 13:38
    
What if there's a .img1 and .img2? "win8.raw1 * 2048 718847 358400 7" and "win8.raw2 718848 52426751 25853952 7" –  Enkouyami Oct 4 '13 at 1:16
    
Why does this work even though we do not specify FS when mounting? –  Jernej Jerin Apr 3 at 11:39
add comment

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
share|improve this answer
add comment

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.