LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Loop-mount image of harddrive (https://www.linuxquestions.org/questions/linux-general-1/loop-mount-image-of-harddrive-332730/)

addy86 06-12-2005 08:50 AM

Loop-mount image of harddrive
 
Hello!

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

Adrian

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

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

Code:

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?

Yves.

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.

@addy86:
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.

http://fabrice.bellard.free.fr/qemu/

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.

Code:

#!/bin/bash
die() {
        echo "$1" >&2
        exit 1
}
if [ "$UID" != 0 ]
then
        die "You have to be root."
fi

if [ -z "$1" ]
then
        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>

Example:
        $0 hd.img hd.img6 /mnt/tmp"
        exit
fi

img="$1"

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


theYinYeti 06-14-2005 03:30 AM

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

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

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

Quote:

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.

Code:

a=
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 01:33 PM.