BOCHS

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:BOCHS

Post by Candy »

Tim Robinson wrote: I'm no Linux expert, but I'm guessing the problem with mounting it on the loopback device is that you've got an image of an entire hard disk, not just one partition.
IANALE either, but you can mount it through loopback with an offset. That would put you at the start of the partition, essentially what you want. IOW, man mount.
Since I'm not a Linux expert, I can't recommend a solution. If you were using Windows, I'll suggest that you used WinImage or Mtools to edit the image.
No, don't edit the image. Bad idea.
Curufir

Re:BOCHS

Post by Curufir »

Ok, the disk image (I'll call it c.img since that's what you're using apparently) is an image of a hard disk, including the MBR.

Mounting using the loop flag is the equivalent of doing:
losetup /dev/loop0 /path/to/c.img
mount -twhatever /dev/loop0 /mnt/lb

It should be obvious that this isn't going to work.

Mount is used to mount partitions, not complete disks.

What you need to do is correct for the MBR and patition size during mounting, ie just mount the parition, not the whole disk.

In Linux, I'll run through from scratch (Single partition because playing with multiple partitions is a bit more complicated).

Figure out roughly how big you want your disk to be.

Geometry you'll use is #cylinders, 16 heads, 63 sectors/track, 512 bytes/sector. This gives you 516096 bytes per cylinder (16*63*512). Choose an even number of cylinders sufficient to give you the disk space you want, try to keep it an even number.

First make the empty image file.

dd if=/dev/zero of=/path/to/c.img bs=516096c count=#cylinders

(This gives you an empty file where #cylinders is the number of cylinders you want to use)

losetup /dev/loop0 /path/to/c.img

(This sets up /dev/loop0 to point to c.img as though it were a physical hard disk...kinda :))

fdisk /dev/loop0

(Build the 'disk' MBR using the following instructions)

o - Create new empty DOS parition table
x - Switch to expert mode
h - Set number of heads to 16
s - Set sectors/track to 63
c - Set cylinders to #cylinders
r - Return to normal mode
n - Create one primary partition that fills the whole 'disk'
p - Dump the parition table to the screen.

At this point check a couple of things. Make sure cylinders, heads, sectors/track are correct. Make sure that your brand new partition starts at cylinder 1 (It will do if you haven't screwed around with the defaults when making the partion). If you're using FAT then you may want to change the disk id now, but since AFAIK GRUB ignores the partition table id it's not hugely important.

w - Write partition table to 'disk'

(That's fdisk done with)

losetup -d /dev/loop0

(Take the image off the loopback device)

Ok, now the choice is up to you how you go from here. I don't have mtools lying around atm so I can't format into fat under Linux, perhaps you can. I'll show you how it's done for ext2fs.

losetup -o32256 /dev/loop0 /path/to/c.img

(Ok, notice the 32256 here. This says setup the loopback device starting 32256 bytes into the file. The reason it is 32256 bytes is because that is precisely 1 cylinder into the file (63*512 bytes) which we know is the start of our partition.)

mke2fs /dev/loop0

(Formats the 'partition')

mount -text2 /dev/loop0 /mnt/wherever

If you check /mnt/wherever you'll now have a perfectly functioning ext2 partition that you can pretty much treat as though it were a normal physical partition (You'll know this because there'll be a lost+found directory on it).

From here on out use:

umount /mnt/wherever
losetup -d /dev/loop0

When unmounting the partition, and:

losetup -o32256 /dev/loop0 /path/to/c.img
mount /dev/loop0 /mnt/wherever

When mounting it.

Example Bochs line for this image would be:

ata0-master: type=disk, path=/path/to/c.img, cylinders=#cylinders,heads=16,spt=63

Have a nice day 8).
Tim

Re:BOCHS

Post by Tim »

Curufir, thanks for the detailed explanation. I'll put that on the Wiki if that's OK with you.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:BOCHS

Post by Candy »

Curufir wrote: This gives you 516096 bytes per cylinder (16*63*512).
The reason it is 32256 bytes is because that is precisely 1 cylinder into the file (63*512 bytes) which we know is the start of our partition.)
You mix them up. 516096 bytes is the count per cylinder, 32256 is the count per track (track is unique combination of cylinder + head).

[edit] put it in the wiki too [/edit]
kris

Re:BOCHS

Post by kris »

I could kick myself for the stupidity.

I was so close, just didn't see the -o for some reason :)

Thanks a lot, I really appreciate all the help I got eventhough I didn't see the obvious.

Well works now, the good thing is that I had some time to fill my notebook with some code while frustrated from fiddling with the image. Now the fun part actually starts :)
Curufir

Re:BOCHS

Post by Curufir »

Tim Robinson wrote: Curufir, thanks for the detailed explanation. I'll put that on the Wiki if that's OK with you.
No problem. I was in a bit of a rush so there are a couple of explanation errors in there (Candy already picked up on one ;D), the actual commands are fine, just the explanation of them.

If it's ok I'll sit down later on and write up a more detailed, error-free, version that is aimed squarely at a Linux/disk image newbie. I think that might be best if it's to be read by people looking at the wiki.
Tim

Re:BOCHS

Post by Tim »

Sure. When you come to it, it's under Disk Images Under Linux.
Curufir

Re:BOCHS

Post by Curufir »

Updated it, but this is my first time with Wiki stuff so I might have made some markup errors.

Could one of the Linux guys give it a quick read and make sure there are no errors in it. URL is the same as the one given by Tim.
Post Reply