Disk Images

From OSDev.wiki
Revision as of 03:48, 21 May 2010 by osdev>Mbrush
Jump to navigation Jump to search

The best way for hobbyist os developers to test their systems is to use programs like QEMU, Bochs, VMWare or VirtualPC.

All of these common tools rely on using disk images (either floppy or hard disk). A disk image is nothing more than a file whose content reflects the layout of a disk and that the tool will use as if it was a physical device. E.g. A floppy image typically is 1.44MB large and its 512 first bytes consist of the 'floppy's boot sector. The different sub-pages should tell you how to create such images from real disks and use them under the most common operating systems.


Tools

These programs can help you with the management of disk images

Linux Windows BSD Mac OS X File Systems Supported
BFI No Yes No No FAT
fat_imgen Yes Yes Yes Yes FAT12
File Disk No Yes No No Anything supported by Windows
hdiutil No No No Yes HFS HFS+ HFS+J HFSX FAT UFS
ImDisk No Yes No No Anything supported by Windows
Loopback Device Yes No Yes Yes Anything supported by the kernel
MagicISO No Yes No No ISO 9660, Rock Ridge, HFS/HFS+, Joilet, UDF, XBOX DVD FS
mkisofs Yes Yes Yes Yes ISO 9660
MTools Yes Yes Yes Yes FAT
Virtual Floppy Disk No Yes No No Anything supported by Windows
Win Image No Yes No No FAT ISO 9660

If you want to write/burn an image to media you can use one of the following tools:

Linux Windows BSD Mac OS X Notes
dd Yes Yes (Using Cygwin) Yes Yes
Rawwrite No Yes No No Floppy images only

Floppy disk images

You can use 'dd' to create a blank floppy image.

dd if=/dev/zero of=floppy.flp bs=512 count=2880

CD images

As of version 0.95, GNU GRUB comes with support for no-emulation El-Torito CD boot. Creating a CD image is much easier than working with floppy images and trying to stuff GRUB in them (and you get 650 meg more space too). Putting GRUB on a CD is now a simple matter of making a skeleton directory tree for the CD filesystem layout, copying the "stage2_eltorito" file in there, and running mkisofs with a specialized command line. See the GRUB 0.95 info node Installation > Making a GRUB bootable CD-ROM for details. Much more humane than those floppy games.

There is also a tutorial about creating a no-emulation El-Torito CD with GRUB.

Handling of Partition Tables in images.

At the time of writing, only Apple's hdiutil can handle the mounting of partitions inside of an image cleanly.

To create a disk image in Linux, use dd:

dd if=/dev/zero of=harddisk.img bs=1M count=10

This will create a 10MB image file, then you can mount the image to a loopback device as follows:

losetup /dev/loop0 harddisk.img

Now you can partition the image as if it were a regular hard disk, for example:

cfdisk /dev/loop0

Now if you want to format the new partition(s) you just need to find their offsets. The 'parted' utility is handy for this:

$ parted /dev/loop0
GNU Parted 2.2
Using /dev/loop0
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) unit                                                             
Unit?  [compact]? B                                                       
(parted) print                                                            
Model:  (file)
Disk /dev/loop0: 10485760B
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End       Size      Type     File system  Flags
 1      32256B  8225279B  8193024B  primary               boot

You can see that the first partition starts at 32256. To mount this partition to a loopback device, use losetup again, this time with the offset option:

losetup -o 32256 /dev/loop1 harddisk.img

Now you can format the partition, for example:

mkfs -t ext2 /dev/loop1

Now to mount the image to the filesystem, first unmount the loopback devices:

losetup -d /dev/loop0
losetup -d /dev/loop1

And the use the regular mount command with the loop and offset options, using the same offset as above:

mount -o loop,offset=32256 harddisk.img /mnt

You can now access /mnt just like any other mounted filesystem. When you're done just unmount the image:

umount /mnt

Images with preinstalled GRUB

If you are looking for a ready-made floppy image with GRUB already installed, such has been set up by MartinBaute, and is available from the following mirrors:

The image has GRUB stage1 / stage2 and an empty config file set up, so all you have to do is adding your binaries and editing the config file (see the GRUB manual for details).

Links

  • Related thread in the forum: Topic:10549 You gonna find a nifty trick Brendan uses to build his own iso images - with NASM. (download missing)