Page 1 of 1

Creating a hard disk image

Posted: Thu Mar 20, 2014 12:07 pm
by MicroPenguin
Hi, how would I create a hard disk drive image for my operating system that currently boots from a floppy image? Are there any resources or tutorials that someone can suggest for me to learn from?

I've taken a look at IanOS which boots from a HDD, but I don't really understand how it works.

Re: Creating a hard disk image

Posted: Thu Mar 20, 2014 12:40 pm
by zhiayang
MicroPenguin wrote:Hi, how would I create a hard disk drive image for my operating system that currently boots from a floppy image? Are there any resources or tutorials that someone can suggest for me to learn from?

I've taken a look at IanOS which boots from a HDD, but I don't really understand how it works.

There are a couple of tutorials on the wiki for setting up a disk image with GRUB.

Re: Creating a hard disk image

Posted: Thu Mar 20, 2014 12:49 pm
by milliburn
There's really no single coherent source for this. On Linux, one possible workflow is:
  • Create a blank image with 'dd' (must be reasonably aligned, e.g. on cylinder boundary)
  • Generate a MBR on that image with 'fdisk' (you can automate the choices by 'echo "..." | fdisk ...')
  • Using 'losetup -o <offset>', mount the offset in the image where your partition would start AND the whole image as well (i.e. 2 loops)
  • Format the offset loop drive
  • grub-install onto the unoffset loop drive. It's cryptically documented, so

    Code: Select all

      $(grub-install --no-floppy --grub-mkdevicemap=$TMP_DEVMAP --boot-directory="$TMP"/boot --modules="ext2 part_msdos biosdisk configfile normal multiboot" $DISK_LOOP &> /dev/null)
    is the line from my build script. $TMP_DEVMAP is a file with "(hd0) <whole-drive-loop>" so that GRUB doesn't probe my configuration instead. $TMP is the mounted partition, $DISK_LOOP the whole drive.
  • and you're done. Insert into /boot/grub/grub.cfg your favourite GRUB configuration.
To update files on the partition, losetup the offset and mount as usual. Remember to unmount to assure that the writes are flushed and you don't get any weird conflicts with the simulator. Consult the man pages/tutorials for specifics on how to use the tools.

The downside is that losetup and mount want sudo every time you update the image. Possibly something could be hacked together with genext2fs in such a way that GRUB works every time.

Re: Creating a hard disk image

Posted: Thu Mar 20, 2014 2:01 pm
by no92
milliburn said almost everything you need to know. The only thing that misses is the reference to the wiki page.

Re: Creating a hard disk image

Posted: Thu Mar 20, 2014 5:24 pm
by MicroPenguin
milliburn wrote:There's really no single coherent source for this. On Linux, one possible workflow is:
  • Create a blank image with 'dd' (must be reasonably aligned, e.g. on cylinder boundary)
  • Generate a MBR on that image with 'fdisk' (you can automate the choices by 'echo "..." | fdisk ...')
  • Using 'losetup -o <offset>', mount the offset in the image where your partition would start AND the whole image as well (i.e. 2 loops)
  • Format the offset loop drive
  • grub-install onto the unoffset loop drive. It's cryptically documented, so

    Code: Select all

      $(grub-install --no-floppy --grub-mkdevicemap=$TMP_DEVMAP --boot-directory="$TMP"/boot --modules="ext2 part_msdos biosdisk configfile normal multiboot" $DISK_LOOP &> /dev/null)
    is the line from my build script. $TMP_DEVMAP is a file with "(hd0) <whole-drive-loop>" so that GRUB doesn't probe my configuration instead. $TMP is the mounted partition, $DISK_LOOP the whole drive.
  • and you're done. Insert into /boot/grub/grub.cfg your favourite GRUB configuration.
To update files on the partition, losetup the offset and mount as usual. Remember to unmount to assure that the writes are flushed and you don't get any weird conflicts with the simulator. Consult the man pages/tutorials for specifics on how to use the tools.

The downside is that losetup and mount want sudo every time you update the image. Possibly something could be hacked together with genext2fs in such a way that GRUB works every time.

Thank you very much! :)
I guess I'll have to do some more Google Fu'ing.

Re: Creating a hard disk image

Posted: Fri Mar 21, 2014 5:26 am
by Antti
I am glad if these advices helped you but please remember that using GRUB is not necessary. Like with floppies, you can compose the hard disk image by yourself although it is not so simple because a hard disk should contain partitions. At some point you might be interested in studying how a commonly used master boot record works. Reading these might help also: Master boot record and Volume boot record.

Re: Creating a hard disk image

Posted: Sun Mar 23, 2014 4:20 pm
by MicroPenguin
Antti wrote:I am glad if these advices helped you but please remember that using GRUB is not necessary. Like with floppies, you can compose the hard disk image by yourself although it is not so simple because a hard disk should contain partitions. At some point you might be interested in studying how a commonly used master boot record works. Reading these might help also: Master boot record and Volume boot record.

Yes, I would like to learn how to create my own hard disk image the same way I created my own floppy image.

Re: Creating a hard disk image

Posted: Mon Mar 24, 2014 3:24 am
by bwat
This simple kernel has a boot loader that loads from a harddisk. The binary to be loaded is written to the disk with dd. Take a look at the file build.x86/mbr.S and build.x86/Makefile for more details. In the file mbr.S, pay special attention to the use of this assembly routine:

Code: Select all

get_disk_geometry:	
# BIOS int 13h, function 08h, Get Drive Parameters.
# Input
#	AH = 08h
#	DL = disk (00h-7Fh floppy, 80h-FFh hard disk)
# Output
#	CF = 0 on success, 1 on failure with AH = failure code
#	BL = drive type (01=360K, 02=1.2MB, 03=720K, 04=1.44MB)
#	CH = miximum value of track/cylinder
#	CL = maximum value of sector
#	DH = maximum value of head
#	ES:DI = segment:offset of disk drive paramter table (on failure)
	movb	disk_number, %dl	
	movw	$0x0800, %ax
	int	$0x13			
	jc	disk_error
	movb	%ch, disk_cylinders
	movb	%dh, disk_heads
	ret
It does the other half of what dd does. You can get away with hard coded values for these parameters for floppies but for harddisks you need to query the hardware. I'm assuming you have a harddisk which is dedicated to your OS and you have the OS in binary form that you want to boot from disk sectors not a file system. This could be a secondary loader in a bigger system where the OS was on a filesystem possibly on the same disk.

Re: Creating a hard disk image

Posted: Mon Mar 24, 2014 9:28 am
by Bender
Hello and welcome to the forums 8).
As I see other people are assuming that you're using a *nix system,
you can also create a disk image using Windows. :P (Waits for flames to come in....)
-Start diskmgmt.msc from the Windows Run Command.
-The Virtual Disk Service should open up.
-Now on the strip that lies below the Window border, click "Action->Create VHD"
-You'll see a message asking you the type of disk and size.
-Choose Fixed (Recommended)
-Set the disk size.
-Set the path
-Now you should see a new disk being mounted, this is the disk that you'll be using.
-Right Click on Square beside the disk that says 'Disk 'N', Unknown, Uninitialized.'
-Click Initialize Disk and select MBR (since you want this, or GPT) on the dialog box and click ok.
-Once it's initialized right click on the white space that says 'XX MB Unallocated'
-Click 'New Simple Volume'
-A wizard should open up.
-Next
-Set your volume size.
-Next
-Assign a drive letter (recommended), or choose an NTFS folder to mount or do nothing :).
-Next
-Now choose your file system (NOTE: There are three options: FAT/FAT32/NTFS!) or leave it blank, there are tools to help you format
disk images with other FSes.
-Check Perform Quick format if you want to. :P
-Next and finish
-Windows Explorer should open a new instance of Disk Manager that'll tell you that there is a new volume inserted, this isn't really a
device it's a virtual disk (loopback device in Unix terms?)
-If you want to browse around the disk open My Computer browse it...... :)
-To use the disk after browsing etc, you must unmount it, from the Disk Manager right click the square block, and click Detach VHD.
-Done???
Limitations:
The only 2 major limitations are:
-Filesystems, Only FAT, FAT32 and NTFS are supported, you may need to use a 3rd party program to format the disk after mounting it.
-Format, Creates VHD files, which may (?) or may not (?) be supported by some emulators, maybe use QEMU's qemu-img.exe or Virtual Box's VboxManage.exe or even M$'s open source (true!) vhd tool to convert them to IMG or preferred format.
This worked fine for my OS which uses FAT16 as the root filesystem. :)
Btw you can also use WinImage (both free and paid) to browse the disk after unmounting it.

Re: Creating a hard disk image

Posted: Tue Mar 25, 2014 1:49 am
by iansjack
... or you could just use one of the many Windows versions of "dd".

Re: Creating a hard disk image

Posted: Fri Apr 04, 2014 12:46 am
by phillid
...or for those of you who don't want to piss into the pocket of adfly, nor download from a shady website, you can get the aforementioned software from its official source, without paying an undeserving person: http://www.ltr-data.se/opencode.html/#ImDisk.

As for creating an HDD image, it's fairly simple if you're running Linux. Basically:
  • `truncate --size=10G hdd-image`
  • `cfdisk hdd-image`
  • Muck around in cfdisk to create partitions
  • `partx -a hdd-image`
  • Mkfs and mkswap to taste
  • Mount /dev/loop0px to taste
Simple as that.

Cheers.