61

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.

Hennes
  • 64,768
  • 7
  • 111
  • 168
Deleted
  • 4,928
  • 9
  • 37
  • 33
  • http://unix.stackexchange.com/questions/9099/reading-a-filesystem-from-a-whole-disk-image || http://superuser.com/questions/117136/how-can-i-mount-a-partition-from-dd-created-image-of-a-block-device-e-g-hdd-u || http://askubuntu.com/questions/69363/mount-single-partition-from-image-of-entire-disk-device – Ciro Santilli OurBigBook.com Aug 29 '15 at 21:27
  • http://stackoverflow.com/questions/1419489/loopback-mounting-individual-partitions-from-within-a-file-that-contains-a-parti – Ciro Santilli OurBigBook.com Sep 11 '15 at 11:00

8 Answers8

97

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
CivMeierFan
  • 179
  • 1
  • 4
bhelm
  • 1,191
  • 8
  • 3
  • 8
    On my Ubuntu 14.04 installation, losetup doesn't provide a --partscan option. – Cutter Oct 05 '14 at 23:11
  • 1
    @Cutter it was added in util-linux 2.21, Ubuntu 16.04. :-) – Ciro Santilli OurBigBook.com Sep 24 '16 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. :/ – Randy Syring May 04 '18 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 '21 at 15:52
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.

GrGr
  • 241
  • 2
  • 3
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
baumgart
  • 1,416
  • 9
  • 14
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
Jonathon
  • 103
  • 4
Journeyman Geek
  • 127,463
  • 52
  • 260
  • 430
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.

Richard June
  • 947
  • 9
  • 8
  • the offset looks wrong; that corresponds to a partition start of 63 (63 * 512 = 32256). 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 Mar 07 '10 at 15:57
  • This is a good example of link rot. That page is gone. – Warren P Jul 29 '20 at 18:14
  • https://web.archive.org/web/20141213021001/http://wiki.edseek.com:80/guide:mount_loopback or this lists multiple variants to mount https://tinyapps.org/docs/mount_partitions_from_disk_images.html – mgutt Jan 22 '21 at 21:47
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
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.

Rohit Gupta
  • 2,721
  • 18
  • 27
  • 35
  • Please don't add "thank you" as an answer. Once you have sufficient [reputation](https://superuser.com/help/whats-reputation), you will be able to [vote up questions and answers](https://superuser.com/help/privileges/vote-up) that you found helpful. - [From Review](/review/late-answers/1207102) – Blind Spots Jul 28 '23 at 05:11
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
  • 174
  • 4