Help creating a bootable HARD DISK image with GRUB

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
Mikumiku747
Member
Member
Posts: 64
Joined: Thu Apr 16, 2015 7:37 am

Help creating a bootable HARD DISK image with GRUB

Post by Mikumiku747 »

Hello,

Once again, I'm asking for the forum's help (sorry, I wish I was good enough to solve more problems on my own). This time, I'm trying to create a bootable disk image to pass to bochs / qemu as a hard disk image, which has grub installed, a grub config file, my kernel image, and Extra Empty space (or maybe even eventually a few small files). I'm quite aware that grub-mkrescue can be used to make a bootable CDROM image from a directory (and that you can even burn the image to a hard disk image and load it as a hard disk), but I'd like to create something closer to installing my OS onto a hard disk image.

Currently, I'm just using grub-mkrescue on my sysroot folder, and then passing that image as a cdrom to my emulator. While this works, I'm about to begin some file system and disk interaction stuff, and so I'd like to have my build system create a disk image which I can use as a bootable hard drive, instead of a bootable CDROM image. That way, I can work with the file system on the disk, and not have to have both a boot CD and a hard disk to play around with.

I'm pretty bad at explaining things, so I'll try and summarize it as simply as possible: I would like a bootable disk image, like the one made by grub-mkrescue, but I want it to be a Hard disk image, not a CD image, and I want it to have some empty space left over in the filesystem, so I can interact with the filesystem.

So far, I've tried making a blank disk image (something along the lines of "dd if=/dev/zero of=hd.img bs=1M count=64" to make a disk image, formatted it with a single fat32 primary partition using gparted, msdos partition table), then tried to install grub on it in a variety of ways, including just "grub-install hd.img", "grub-install /dev/loop0" (with the disk image looped to /dev/loop0 using "losetup loop0 hd.img"), and even "grub-install --boot-directory=/mnt/loop0p1/boot /dev/loop0" (with the first partition of /dev/loop0 mounted at /mnt/loop0p1). However, grub always errors out on boot, providing some sort of image along the lines of "error: no such device: 5E35-6F94" (the numbers usually vary) and then a grub rescue prompt. So from what I understand about grub, it's loading the MBR fine, which is why grub loads at all, but then, somehow, grub can't load the rest of itself from the disk (either it can't find it or it's not on the disk to begin with).

I've tried searching the forums and google, but the forums seem empty, despite the fact that I would have thought wanting to make a hard drive installation is a common desire, and google turns up nothing but endless pages of ubuntu boot recovery tutorials, none of which seem to have any information at all. Like I mention above, I pretty much just want to make a rescue disk image, but have it be a HDD image and have it not be the minimum size to accommodate GRUB and my image.

I'm concerned this might become a problem later down the track when a user would want to install my OS to their hard drive instead of booting from a CD every time. Even if I were to port grub-install or any of the other grub tools to my OS, how would I use them to install my OS on a hard drive? It's a similar problem to what I'm facing now, the only difference is that right now I want to install the OS (and bootloader) to a hard disk at compile time VS the user trying to install the OS and bootloader to a hard disk at runtime.

I guess, in essence, my question is, how do I install grub on the hard disk and how do I get grub on said hard disk to load my kernel binary. The rescue image seems to have no trouble doing it, so I'm sure it's possible, but how? Maybe if somebody could explain more thoroughly how grub actually installs itself on a medium, and how it runs itself on boot, I could discover the problem as well, because right now, all I know is that grub-mkrescue is a magic black box program which makes a CDROM image, puts the grub bootloader in the MBR, puts the rest of grub on disk, and puts all the other files I ask it to onto the disk, and then spits that image out.

Any information relating is helpful, I know I'm terrible at explaining what I'm trying to do, so ask me some more questions if you think you can help but don't quite get what I'm trying to do...

- Mikumiku747
User avatar
iansjack
Member
Member
Posts: 4706
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Help creating a bootable HARD DISK image with GRUB

Post by iansjack »

https://www.gnu.org/software/grub/manua ... stall.html

You need to specify where you want the boot files to be located.
User avatar
Mikumiku747
Member
Member
Posts: 64
Joined: Thu Apr 16, 2015 7:37 am

Re: Help creating a bootable HARD DISK image with GRUB

Post by Mikumiku747 »

iansjack wrote:https://www.gnu.org/software/grub/manual/html_node/Installing-GRUB-using-grub_002dinstall.html

You need to specify where you want the boot files to be located.
As I mentioned above, I already tried the --boot-directory option, but it didn't seem to work. Any idea on what I could be doing wrong?
ashishkumar4
Member
Member
Posts: 73
Joined: Wed Dec 23, 2015 10:42 pm

Re: Help creating a bootable HARD DISK image with GRUB

Post by ashishkumar4 »

I too have the EXACT SAME Question!!! I am trying to search for a solution for sooo long :/ SOMEONE PLEASE HELP >_<
The best method for accelerating a computer is the one that boosts it by 9.8 m/s2.
My OS : https://github.com/AshishKumar4/Aqeous
linuxyne
Member
Member
Posts: 211
Joined: Sat Jul 02, 2016 7:02 am

Re: Help creating a bootable HARD DISK image with GRUB

Post by linuxyne »

I want to install the OS (and bootloader) to a hard disk at compile time
This article should get you started.
Once the disk is created, a script can be invoked from your makefile, to setup loopback devices, mount the volume and copy the newly built kernel image.



the user trying to install the OS and bootloader to a hard disk at runtime
archwiki's beginner's install guide shows how one can install Arch on a disk when booting from an install CD. It includes installing grub on the disk. The OS running from the CD will have to be almost a fully developed OS.

You can use an OS such as Linux to run from the CD, and have it install grub + your kernel + other modules on to the hard disk.

If the CD too should be running your own OS, then it must have some minimum support.
I can think of starting with the CD containing a compressed disk image of your installation of the OS. That compressed image contains the boot sector, etc.

The OS which boots from the CD should be able to get away with a minimum amount of functionality (ATA(PI) support + keyboard-display IO + decompression) required for decompressing the image in memory, then copying it to disk, and may be allowing the user some minimum interaction along with the display of the activity.

That is not very flexible, though -
* the target disk needs to be of a certain size,
* the target disk needs to be empty of any other installation,
* the target OS needs to be able to re-configure itself, because the environment in which the image was built is not the same environment where it will eventually run post-installation.
* the OS running from the CD may or may not have file system support (depending on how far along your OS is developed, if it can load extra modules.)

If the OS you are building has at least such support as mentioned above included in it, it can become self-installing in this, initially quite rigid, manner, and then go on to become more flexible as you add more support into your OS.

Edit0: some grammar fixes.
Last edited by linuxyne on Sun Jul 17, 2016 1:14 am, edited 1 time in total.
ashishkumar4
Member
Member
Posts: 73
Joined: Wed Dec 23, 2015 10:42 pm

Re: Help creating a bootable HARD DISK image with GRUB

Post by ashishkumar4 »

How to put MY Kernel and Config files in there? Directly putting them in /boot and /boot/grub respectively dosent WORK!!!
The best method for accelerating a computer is the one that boosts it by 9.8 m/s2.
My OS : https://github.com/AshishKumar4/Aqeous
linuxyne
Member
Member
Posts: 211
Joined: Sat Jul 02, 2016 7:02 am

Re: Help creating a bootable HARD DISK image with GRUB

Post by linuxyne »

I am not sure I understand the problem then.

The article works, except that the module 'biosdev' had to be removed from the grub-install command line, because my grub did not recognise that module.

Those steps leave the partition of the disk image mounted at /mnt.
- Copy the kernel binary into /mnt/boot
- Create /mnt/boot/grub/grub.cfg
- Fill in the grub.cfg with commands to boot your kernel.

Below is my cfg for a multiboot2-enabled, elf64 kernel.

Code: Select all

set timeout=15
set default=0

menuentry "osd" {
        multiboot2 /boot/osd.elf
        boot
}
Edit0: That same article also has a skeleton grub.cfg under the "Floppy Instructions". Unless I misunderstood your problem, the information to build a bootable hard disk image is all there on it.
User avatar
Mikumiku747
Member
Member
Posts: 64
Joined: Thu Apr 16, 2015 7:37 am

Re: Help creating a bootable HARD DISK image with GRUB

Post by Mikumiku747 »

Thanks for the replies, that article helps a lot, I had been on the GRUB page before, but didn't realise there was a guide there, with exactly what I wanted to do... oh well, at least in the end there's a forum thread now, so if somebody searches the forum they might have a better chance of finding the answer.

Now that I actually have something working, if you don't mind me asking for the sake of trying to learn, how come the steps on the wiki work (apart from the biosdev thing), but the things I tried didn't work? It seems the main differences between my own struggles and the correct way of doing it are:
  • I used losetup to mount the entire drive to /dev/loop0, and accessed the actual partition from /dev/loop0p1 (which was made automatically whenever I connected the disk image using losetup), as opposed to the tutorial mounting both the whole disk and the first partition by offsetting itself from the start of the disk.
  • The tutorial used the --no-floppy option (which isn't documented anywhere in the grub2 manpage or the grub2 manual, gee thanks). Note that I also had to change --root-directory= to --boot-directory= and change the path respectively (just add /boot).
In regards to the former, I have a hunch that it didn't affect anything at all, since grub didn't complain about not being able to write the files to the device when I was doing it my way. So it think it was likely the latter, specifically, the --no-floppy option. Since I can't find documentation on this, does anybody know what this option does? I can guess that it can't be used with floppies, but apart from that, I'm not really sure what it does or why not having it makes GRUB not find all the modules.

By the way, since that biosdev thing doesn't work any more, should I edit that out of the wiki. And also, is the --root-directory option the legacy grub equivalent of --boot-directory, and should I make a note on the wiki about that difference as well?
ashishkumar4
Member
Member
Posts: 73
Joined: Wed Dec 23, 2015 10:42 pm

Re: Help creating a bootable HARD DISK image with GRUB

Post by ashishkumar4 »

It wasnt working until now. I had been doing the same thing but still :/ At the end, Out of frustration I started over again after cleaning everything. I just did it the way the wiki said. Then I copied over my kernel and grub.cfg to the image and 'synced' everything :p It WORKED!!!
The Reason I think It worked is that This time, I had put my Kernel file as '.bin. file (my kernel file: "Aqeous.bin"). Earlier I had used nonsence extentions like Aqeous.aqeous (xD) and Aqeous (no extention :p ) and Aqeous.elf xD. I think Grub only reads this extention :p Because I was doing everything else just as I had been doing before. This worked only after I did this change. Thanks everyone!!!
The best method for accelerating a computer is the one that boosts it by 9.8 m/s2.
My OS : https://github.com/AshishKumar4/Aqeous
Post Reply