http://www.mega-tokyo.com/osfaq/Disk%20Images%20Under%20Linux
Linux uses loopback devices for images.
First, lets create an empty image.
dd if=/dev/zero of=floppy.img bs=512 count=2880
Now, lets set it up for mounting.
losetup /dev/loop0 floppy.img
Now lets make it MSDOS formatted.
mkdosfs /dev/loop0
Mount!
mount -t msdos /dev/loop0 /mnt/myfloppy
A hard disk image contains an MBR, then a number of partitions, but the 'mount' instruction in Linux works with disk partitions, not full disks. To mount a partition contained in our disk image, we need to make sure the 'mount' command only sees our partition, not the whole disk.
First, lets create an empty image.
dd if=/dev/zero of=/path/to/c.img bs=516096c count=#cylinders
Now, lets set it up for mounting.
losetup /dev/loop0 /path/to/c.img
Note: Under a normal Linux setup you will need to be root to use the losetup command (The same applies to most commands we'll be using).
Now to create the MBR and partition table on the disk image (Usually you need to be root).
fdisk -u -C#cylinders -S63 -H16 /dev/loop0
Explanation:
fdisk Linux DOS partition maintenance program. -u Display units in sectors not cylinders (We will need this). -C#cylinders Set the cylinders of disk to our value. -S63 Set the sectors/track to 63. -H16 Set the heads/track to 16. /dev/loop0 Thanks to losetup this device represents our raw 'disk'.
Within fdisk use the following commands:
o - Create a new empty DOS partition table. n - Create a new partition (For simplicity just make 1 primary partition covering the whole disk). a - Toggle the bootable flag (Optional). p - Print the partition table.
You should end up with a screen that looks something like this:
Disk /dev/loop0: 516 MB, 516096000 bytes 16 heads, 63 sectors/track, 1000 cylinders, total 1008000 sectors Units = sectors of 1 * 512 = 512 bytes
Device Boot Start End Blocks Id System /dev/loop0p1 * 63 1007999 503968+ 83 Linux
Obviously the cylinder count, partition end and blocks will be different depending on the size of your image.
Make a note of the start sector (63 here) and the block count (503968 here).
Note: If you are intending to format the partition to something other than ext2fs then change the partition id here using the t command. I should also point out that disk manufacturers and programmers don't agree on how many bytes are in a megabyte.
w - Write partition table to our 'disk' and exit.
Ignore any errors about rereading the partition table. Since it's not a physical device we really don't care.
We now have a partition table on our disk image.
Unfortunately this also means that from here on out we have to account for the fact that our partition does not start at byte 0 of the image.