-   Linux - General (
-   -   Loop-mount image of harddrive (

addy86 06-12-2005 08:50 AM

Loop-mount image of harddrive

I can mount a regular file (containing a filesystem) using `-o loop'. Is there a way to mount a file representing a complete harddrive image (like the result of `dd if=/dev/hda of=my-file'), containing partition table and partitions? If yes, how can it be done?
I hope, this hasn't been answered already (I don't know what to search for...).

Thank you


dub.wav 06-13-2005 03:59 AM

This how to mount the first partition on a harddisk image:


losetup -o $((63 * 512)) /dev/loop0 hd.img
mount /dev/loop0 /mnt/loop -t vfat

(-o meaning offset...)
If you run fdisk -l on the image and play around with the offset maybe you can find out how to mount the other partitions as well.

theYinYeti 06-13-2005 06:36 AM

OOhhhh ! 8-)

That's what I was looking for a year ago (or so...). Thanks! It's a nice tip to know.

Could you give the explanation for the bash-computed 63*512? I guess it is 63 times the size of a sector, but then are we sure that sectors are always 512 bytes big, and why 63 times?


addy86 06-13-2005 07:43 AM

Thank you, you helped me a lot (this makes it much easier to play around with Bochs).

But I guess it's easier to use
fdisk -ul
because you get the partition sizes in sectors (partitions are not necessarily aligned to cylinders).

@theYinYeti: AFAIK a cylinder has typically 63 sectors on a large hard disk.
I don't know on which devices a sector has <> 512 bytes, but you can get the geometry and sector size
with fdisk -l

dub.wav 06-13-2005 08:13 AM

I found that recipe through googling a few months ago, don't really remember what it means, but I think the offset is to skip the MBR.

Have you tried Qemu? In my opinion it is a lot easier to use than bochs, and it is supposed to be faster as well, especially with the accelerator module.

EDIT: This is useful to me too, so I wrote a bash script to mount a partition on a image, thanks for the tips about fdisk -u.

EDIT2: Bugfix for the below script, would fail for partitions marked bootable.


die() {
        echo "$1" >&2
        exit 1
if [ "$UID" != 0 ]
        die "You have to be root."

if [ -z "$1" ]
        echo \
"To list partitions on image:
        $0 <hd image>
To mount partition on image:
        $0 <hd image> <partition> <mountpoint>
To mount partition on image with a specific type:
        $0 <hd image> <partition> <mountpoint> <filesystem>

        $0 hd.img hd.img6 /mnt/tmp"


if [ "$#" == 1 ]
        fdisk -l "$img"
elif [ "$#" == 3 ] || [ "$#" == 4 ]
        begin="$(fdisk -lu "$img" 2>/dev/null | grep "$part" | \
        (read a b c d; [ "$b" == "*" ] && echo "$c" || echo "$b"))"
        if [ -z "$begin" ]
                die "Error: Invalid partition given."
        if mount "$img" "$mnt" -o loop,offset="$((begin * 512))" -t "${4-auto}"
                echo "Partition mounted"
        die "Error: Invalid number of arguments given"

theYinYeti 06-14-2005 03:30 AM

Excellent! Thanks both! And thanks for this nice script.

(Small typing error: 7th line from the end: 4:- instead of 4-)

dub.wav 06-14-2005 06:06 AM


Originally posted by theYinYeti
(Small typing error: 7th line from the end: 4:- instead of 4-)
No, not really, both works in the context used in the script.


b=${a-40} # $b is now blank
b=${a:-40} # $b is now 40

- checks if the variable is defined, :- checks if the variable is non-zero.

All times are GMT -5. The time now is 11:40 AM.