LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 06-12-2005, 08:50 AM   #1
addy86
Member
 
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332

Rep: Reputation: 31
Question 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
 
Old 06-13-2005, 03:59 AM   #2
dub.wav
Member
 
Registered: Aug 2003
Location: Norway
Distribution: FC4
Posts: 83

Rep: Reputation: 20
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.
 
Old 06-13-2005, 06:36 AM   #3
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 66
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.
 
Old 06-13-2005, 07:43 AM   #4
addy86
Member
 
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332

Original Poster
Rep: Reputation: 31
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
 
Old 06-13-2005, 08:13 AM   #5
dub.wav
Member
 
Registered: Aug 2003
Location: Norway
Distribution: FC4
Posts: 83

Rep: Reputation: 20
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

Last edited by dub.wav; 06-13-2005 at 03:43 PM.
 
Old 06-14-2005, 03:30 AM   #6
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 66
Excellent! Thanks both! And thanks for this nice script.

Yves.
(Small typing error: 7th line from the end: 4:- instead of 4-)
 
Old 06-14-2005, 06:06 AM   #7
dub.wav
Member
 
Registered: Aug 2003
Location: Norway
Distribution: FC4
Posts: 83

Rep: Reputation: 20
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP image-creation while loop taking forever benrose111488 Programming 9 07-09-2005 07:04 PM
Cannot mount more than 8 iso image files as loop device marsim Debian 4 11-09-2004 04:05 AM
HardDrive to HD image copy gerrymatbeacon Linux - Newbie 1 08-13-2004 03:18 PM
What does "loop-mount the disk image" mean ? karans Linux - Distributions 1 10-29-2003 08:21 AM
Problem mounting CD image (no loop device)... Slayer Linux - General 1 10-05-2003 09:30 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:29 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration