Mounting a loopback hard disk

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.
Post Reply
User avatar
salil_bhagurkar
Member
Member
Posts: 261
Joined: Mon Feb 19, 2007 10:40 am
Location: India

Mounting a loopback hard disk

Post 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.
User avatar
karloathian
Posts: 22
Joined: Fri Mar 28, 2008 12:09 am

Re: Mounting a loopback hard disk

Post 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.
User avatar
salil_bhagurkar
Member
Member
Posts: 261
Joined: Mon Feb 19, 2007 10:40 am
Location: India

Re: Mounting a loopback hard disk

Post 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...
User avatar
salil_bhagurkar
Member
Member
Posts: 261
Joined: Mon Feb 19, 2007 10:40 am
Location: India

Re: Mounting a loopback hard disk

Post 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?
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Re: Mounting a loopback hard disk

Post 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.
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
Post Reply