Page 1 of 1

Mounting a loopback hard disk

Posted: Wed Aug 20, 2008 10:34 am
by salil_bhagurkar
Hey all!.. I am currently working on the file system and volume architecture of my os. So in order to test my interface, i need to make a hard disk image which is around 100MB or so which i can use with Bochs. I want partitions and a filesystem in this image.

So i used the losetup to create /dev/loop0 and then created a partition table on it using fdisk. But how do i create a filesystem on the partitions? The partitions do not appear as /dev/hdc1 /dev/hdc2 etc as for a normal hard disk. Besides who exactly creates those /dev/hdc1,/dev/hdc2.... from /dev/hdc when linux creates devices...?

I could run a linux image in Bochs and make it operate directly on the hard disk image. But this seems like a problem i feel i should solve.

Re: Mounting a loopback hard disk

Posted: Wed Aug 20, 2008 12:02 pm
by karloathian
Using the

Code: Select all

fdisk -ul MyDisk.img 
should sputter out something like this:

Code: Select all

128 heads, 63 sectors/track, 0 cylinders, total 0 sectors
Units = sectors of 1 * 512 = 512 bytes

   Device Boot      Start         End      Blocks   Id  System
MyDisk.img1   *          63     3515903     1757920+  83  Linux
MyDisk.img2         3515904     4193279      338688   82  Linux swap / Solaris
You can then run the losetup command but offset to a specific partition of the disk as follows:

Code: Select all

losetup -o 32256 /dev/loop1 MyDisk.img
I used an offset of 32256 here because the partition started at block 63 and each block is 512 bytes in length. ( 63 * 512 = 32256 ). With that in hand you can now use a mkfs command on that new loopback device.

I hope that was what you were looking for.

Re: Mounting a loopback hard disk

Posted: Wed Aug 20, 2008 12:15 pm
by salil_bhagurkar
Oh great... I was trying to find that 'offset' kind of thing in mkfs... But never thought of finding it in losetup... Thanx a lot...

Re: Mounting a loopback hard disk

Posted: Thu Aug 21, 2008 11:09 pm
by salil_bhagurkar
And yes.. like I said before, who creates the /dev/hdc1, /dev/hdc2.... from the single /dev/hdc in linux during the boot time?

Re: Mounting a loopback hard disk

Posted: Fri Aug 22, 2008 8:38 pm
by Brynet-Inc
salil_bhagurkar wrote:And yes.. like I said before, who creates the /dev/hdc1, /dev/hdc2.... from the single /dev/hdc in linux during the boot time?
The kernel does, on Linux/FreeBSD anyway... See Devfs on Wikipedia.

On traditional Unix systems, device nodes are created by a shell script... /dev/MAKEDEV.