Creating a hard disk image

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
MicroPenguin
Posts: 3
Joined: Wed Feb 12, 2014 2:15 pm

Creating a hard disk image

Post 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.
User avatar
zhiayang
Member
Member
Posts: 368
Joined: Tue Dec 27, 2011 7:57 am
Libera.chat IRC: zhiayang

Re: Creating a hard disk image

Post 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.
milliburn
Posts: 5
Joined: Mon Jul 01, 2013 10:13 am

Re: Creating a hard disk image

Post 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.
no92
Member
Member
Posts: 307
Joined: Wed Oct 30, 2013 1:57 pm
Libera.chat IRC: no92
Location: Germany
Contact:

Re: Creating a hard disk image

Post by no92 »

milliburn said almost everything you need to know. The only thing that misses is the reference to the wiki page.
MicroPenguin
Posts: 3
Joined: Wed Feb 12, 2014 2:15 pm

Re: Creating a hard disk image

Post 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.
Antti
Member
Member
Posts: 923
Joined: Thu Jul 05, 2012 5:12 am
Location: Finland

Re: Creating a hard disk image

Post 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.
MicroPenguin
Posts: 3
Joined: Wed Feb 12, 2014 2:15 pm

Re: Creating a hard disk image

Post 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.
User avatar
bwat
Member
Member
Posts: 359
Joined: Fri Jul 03, 2009 6:21 am

Re: Creating a hard disk image

Post 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.
Every universe of discourse has its logical structure --- S. K. Langer.
User avatar
Bender
Member
Member
Posts: 449
Joined: Wed Aug 21, 2013 3:53 am
Libera.chat IRC: bender|
Location: Asia, Singapore

Re: Creating a hard disk image

Post 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.
"In a time of universal deceit - telling the truth is a revolutionary act." -- George Orwell
(R3X Runtime VM)(CHIP8 Interpreter OS)
User avatar
iansjack
Member
Member
Posts: 4711
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Creating a hard disk image

Post by iansjack »

... or you could just use one of the many Windows versions of "dd".
phillid
Member
Member
Posts: 58
Joined: Mon Jan 31, 2011 6:07 pm

Re: Creating a hard disk image

Post 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.
phillid - Newbie-ish operating system developer with a toy OS on the main burner
Post Reply