How can I mount a partition from dd-created image of a block device (e.g. HDD) under Linux?

34

20

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.

Deleted

Posted 2010-03-07T11:38:19.383

Reputation: 3 548

Answers

51

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

bhelm

Posted 2010-03-07T11:38:19.383

Reputation: 651

8On my Ubuntu 14.04 installation, losetup doesn't provide a --partscan option. – Cutter – 2014-10-05T23:11:05.530

1@Cutter it was added in util-linux 2.21, Ubuntu 16.04. :-) – Ciro Santilli 新疆改造中心法轮功六四事件 – 2016-09-24T10:57:22.763

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. :/ – Randy Syring – 2018-05-04T21:46:07.523

13

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.

GrGr

Posted 2010-03-07T11:38:19.383

Reputation: 231

6

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

baumgart

Posted 2010-03-07T11:38:19.383

Reputation: 1 176

4

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.

Richard June

Posted 2010-03-07T11:38:19.383

Reputation: 787

the offset looks wrong; that corresponds to a partition start of 63 (<i>63 * 512 = 32256</i>). the offset in baumgart's answer looks more correct for this situation. the link is a good reference, but it'd be a better answer if you took the time to summarize the steps or commands needed for this procedure. thanks! – quack quixote – 2010-03-07T15:57:18.913

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

Ciro Santilli 新疆改造中心法轮功六四事件

Posted 2010-03-07T11:38:19.383

Reputation: 5 621

0

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

Journeyman Geek

Posted 2010-03-07T11:38:19.383

Reputation: 119 122

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.

Kasun

Posted 2010-03-07T11:38:19.383

Reputation: 174