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

share|improve this question

4 Answers 4

up vote 34 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
5  
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

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
    
Excellent answer, quick and easy, avoiding the multiplication step is nice –  Elder Geek May 13 at 19:11
    
sudo apt-get install kpartx - to get access to kpartx. The kpartx link in the beginning of the post is a 'trap' in my opinion. Links that imply installing software is a no-no. –  Hannu Jul 31 at 18:26
    
@Hannu, links that integrate with Software Center are a common practice on askubuntu, though in this particular case the typical icon could be added and the URL should be updated. –  Joni Aug 8 at 8: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.

share|improve this answer
1  
Where do I find this version of losetup ? The one I have in Ubuntu 14.04 has a manpage from July 2003 (util-linux) and no ` -P ` option (or anything else) for scanning partitions. –  Hannu Jul 31 at 18:26
    
Sorry, I made a mistake, I've been using this option on Arch Linux which comes with a more recent version of util-linux package. It seems that Ubuntu is sadly stuck on version 2.20.1 which has been released on january 2012 ! The --partscan option has been introduced in util-linux 2.21 –  zeludo Aug 4 at 15:09
    
Well, gnome-disks has an option to mount a disk image and it's partitions. (<- that is for installing and bash/Terminal use, Disksin the dash finds it too). –  Hannu Aug 4 at 15:35

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
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.