62

I have an image of the entire disk created using dd. The disk structure follows:

kent@cow:~$ sudo fdisk -l

Disk /dev/sda: 750.1 GB, 750156374016 bytes
255 heads, 63 sectors/track, 91201 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x000b8508

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           5       90872   729929303+  83  Linux
/dev/sda2           90873       91201     2642692+   5  Extended
/dev/sda5           90873       91201     2642661   82  Linux swap / Solaris

The image was created using:

dd if=/dev/sda of=image750.img

How would I, if it is possible, mount /dev/sda1 from the image so that I'm able to read the contents?

It's not an option to clone the HDD again, I know how to do it if I had only cloned the single partition by itself. I hope it's still possible with the current image.

2

8 Answers 8

101

Nowadays, there is a better way, no need to use offsets or kpartx anymore:

losetup --partscan --find --show disk.img

mount /dev/loop0p1 /mnt

to free up loop0, use after umount:

losetup -d /dev/loop0
5
  • 8
    On my Ubuntu 14.04 installation, losetup doesn't provide a --partscan option.
    – Cutter
    Oct 5, 2014 at 23:11
  • 1
    @Cutter it was added in util-linux 2.21, Ubuntu 16.04. :-) Sep 24, 2016 at 10:57
  • Having used kpartx first, which mounts the partitions like /dev/mapper/loop3p1, I just want to point out that losetup creates the devices like /dev/loop0p1. The answer notes that, but I read over it probably 10 times. :/ May 4, 2018 at 21:46
  • losetup --partscan --find --show disk.img might give you /dev/loop1, in which case you'll need mount /dev/loop1p1 /mnt.
    – Iceberg
    Dec 13, 2021 at 15:52
  • Is there a way to remove the virtual block devices after you're finished at all? Sep 18 at 18:44
14

I ran into this problem today and wanted to update the answers just as a reminder for myself. Instead of calculating the offset on your own, you can use a tool that provides you with mountable devices from a dd image: kpartx

http://robert.penz.name/73/kpartx-a-tool-for-mounting-partitions-within-an-image-file/

http://linux.die.net/man/8/kpartx

In the given case, it would need something like

sudo kpartx -a image750.img
sudo mount /dev/mapper/loop1p1 /mount/point -o loop,ro

where loop1p1 stands for the first partition, loop1p2 for the second, etc.

13

You've got the first part: fdisk -l to find the start offset. Take that number, multiply by 512, and you'll get the offset option to mount. So, for sda1 in your case, 5 * 512 = 2560. Then run the mount:

mount -o loop,offset=2560 -t auto /path/to/image.dd /mount/point
6

I believe loopmounting is the answer -

sudo mkdir /path/to/dir/
mount -o loop example.img /path/to/dir/

The above should mount it under that directory.

This should unmount it:

umount /path/to/dir
5

Loopmounting is only part of the answer.

Look at http://wiki.edseek.com/guide:mount_loopback#accessing_specific_partitions_in_the_image for help on specifying the partition. I think mount -o loop,offset=32256 /path/to/image750.img /mnt will work for you. but you really should read the mentioned tutorial.

3
1

losetup -P automation

Method mentioned by https://superuser.com/a/684707/128124 (added in util-linux v2.21, added Ubuntu 16.04) , here are functions to automate it 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

Decent method before util-linux v2.21.

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: https://askubuntu.com/questions/51226/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

0

If you have User mode files system like fuse, then in desktop environments likes Gnome and have installed tool like gnome-disk-image-mounter, then it without even root by right click and open with it.

0
sudo kpartx -a image750.img
sudo mount /dev/mapper/loop1p1 /mount/point -o loop,ro

allowed me to mount an image of a damaged partition caused by multiple IO errors. there was no other way for me to mount. thanks so much for this answer @GrGr

You just helped me recover about a Tb of data that was level 10 crucial.

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .