Grub on image without grub command.

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
evoex
Member
Member
Posts: 103
Joined: Tue Dec 13, 2011 4:11 pm

Grub on image without grub command.

Post by evoex »

Hello everyone,

I'm trying to build a bootable disk image using GRUB, for a pet OS project. While there's many tutorials on how to do so, they all use the interactive "grub" command line interface. And for some reason, the Linux distro I'm using (crunchbang) does not have it. I have installed grub, and grub-install works fine, but "grub" reports command not found.

I've tried a lot. I've even tried to install some linux distros (such as CentOS) to work from there, but bochs just stops working after a while.

Before that I obviously tried the "normal" method. For example, I tried the following:

Code: Select all

# dd if=/dev/zero of=disk.img bs=512 count=2056320
# fdisk disk.img
Inside fdisk I changed the following:
128 cylinders
255 heads
63 sectors per track
I made a new partition, made it bootable, such that:

Code: Select all

# fdisk -ul disk.img 
You must set cylinders.
You can do this from the extra functions menu.

Disk disk.img: 0 MB, 0 bytes
255 heads, 63 sectors/track, 0 cylinders, total 0 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x7201d21a

   Device Boot      Start         End      Blocks   Id  System
disk.img1   *          63     2056319     1028128+  83  Linux
And then:

Code: Select all

# losetup -o 32256 /dev/loop0 disk.img
# mkfs.ext2 /dev/loop0
And now I'm a bit at a loss what to do... I tried:

Code: Select all

# mount /dev/loop0 /mnt
# grub-install --root-directory=/mnt --modules=ext2 /dev/loop0 --force
/usr/local/sbin/grub-setup: warn: Attempting to install GRUB to a partitionless disk.  This is a BAD idea..
/usr/local/sbin/grub-setup: warn: Embedding is not possible.  GRUB can only be installed in this setup by using blocklists.  However, blocklists are UNRELIABLE and its use is discouraged..
Installation finished. No error reported.
I'm booting this file using the following bochsrc file:

Code: Select all

megs: 1024
romimage: file=/usr/share/bochs/BIOS-bochs-latest
vgaromimage: file=/usr/share/bochs/VGABIOS-lgpl-latest
ata0-master: type=disk, path="disk.img", mode=flat, cylinders=128, heads=255, spt=63
cpu: count=1, ips=15000000
mouse: enabled=0
log: out.bochs
boot: disk
It says it's booting from hard disk, but nothing happens after that.

So, how do I install grub on there?

EDIT:
This helped a bit...

Code: Select all

# losetup /dev/loop0 disk.img
# losetup -o 32256 /dev/loop1 disk.img
# mount /dev/loop1 /mnt
# grub-install --root-directory=/mnt --modules=ext2 /dev/loop0
Installation finished. No error reported.
And probably makes more sense. Now I get a grub rescue prompt, but it reports: "error: no such device: [GUID]".
At ls it only outputs "(hd0)".

Thanks in advance,
evoex
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Grub on image without grub command.

Post by Solar »

evoex wrote:I have installed grub, and grub-install works fine, but "grub" reports command not found.
Strange. I know the 'grub' utility is considered obsolete, but it should still be installed AFAIK. Have you checked /usr/sbin/grub?

Edit: Which version of GRUB are you trying to use? AFAIK, all tutorials assume GRUB "Legacy", i.e. 0.9x. The current "GRUB 2" version (1.98 or something) indeed doesn't have the "grub" command anymore...
I've tried a lot. I've even tried to install some linux distros (such as CentOS) to work from there, but bochs just stops working after a while.
Uh... what? You mean you're trying to install CentOS on Bochs?

Bad idea.

If all else fails, using a LiveCD (like Knoppix) is always a solution. Alternatively, you could re-do the installation from the GRUB rescue prompt you got, or use one of the ready-made Disk Images, boot from it, and use that GRUB prompt for a more "reproducable" approach.
Every good solution is obvious once you've found it.
egos
Member
Member
Posts: 612
Joined: Fri Nov 16, 2007 1:59 pm

Re: Grub on image without grub command.

Post by egos »

evoex, look at my last post here if you use stage1_5. If you don't use it you should store byte partition_number_where_grubdir_is_located at position 20Ah of stage2 image.

It's important what version of GRUB you are using because some GRUB structures are version-specific.
If you have seen bad English in my words, tell me what's wrong, please.
evoex
Member
Member
Posts: 103
Joined: Tue Dec 13, 2011 4:11 pm

Re: Grub on image without grub command.

Post by evoex »

Thanks for your answers, guys! I'm going to try the pre-installed images first. I tried that before, but all I could find were partitioned as isofs...
Solar wrote: Strange. I know the 'grub' utility is considered obsolete, but it should still be installed AFAIK. Have you checked /usr/sbin/grub?

Edit: Which version of GRUB are you trying to use? AFAIK, all tutorials assume GRUB "Legacy", i.e. 0.9x. The current "GRUB 2" version (1.98 or something) indeed doesn't have the "grub" command anymore...
I'm not sure what version of grub I'm using, I can't tell you now. It's not legacy (I tried to install that, but it wouldn't compile on my 64 bit pc, though I didn't try that hard). I believe it was grub 2. If the solutions don't work I'll get back to here with more information on the version and stuff.
Solar wrote:Uh... what? You mean you're trying to install CentOS on Bochs?

Bad idea.

If all else fails, using a LiveCD (like Knoppix) is always a solution. Alternatively, you could re-do the installation from the GRUB rescue prompt you got, or use one of the ready-made Disk Images, boot from it, and use that GRUB prompt for a more "reproducable" approach.
Yes, I figured it wasn't a very good idea. But my laptop wouldn't boot some other distros. I tried ubuntu first and it didn't work, that's the reason I had to use crunchbang. At least it ran on my pc without any issues ;-). It was kind of a last resort idea...
egos wrote: evoex, look at my last post here if you use stage1_5. If you don't use it you should store byte partition_number_where_grubdir_is_located at position 20Ah of stage2 image.

It's important what version of GRUB you are using because some GRUB structures are version-specific.
Very interesting, thanks! I'm going to try the ready made disk images first, though, as I'm a lazy person ;-).


Thanks, guys!
evoex
Member
Member
Posts: 103
Joined: Tue Dec 13, 2011 4:11 pm

Re: Grub on image without grub command.

Post by evoex »

Actually, I have a follow up question on this, making it even more complex...
I'm running a 64 bit computer with Linux. Which means I'll either have to set up a compiler for 32 bits (which I believe is quite a lot of work), or use grub to load a 64 bits kernel. Though I won't be able to use one of those pre-made images, then.
Though most resources on os development are 32 bits, I believe. And I am a beginner.

So what is advisable? Should I set up a cross compiler and start with a 32 bits OS, or jump straight to 64 bits?

Thanks
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Grub on image without grub command.

Post by Solar »

evoex wrote:I'm running a 64 bit computer with Linux. Which means I'll either have to set up a compiler for 32 bits (which I believe is quite a lot of work)...
"gcc -m32" is a lot of work?
...or use grub to load a 64 bits kernel. Though I won't be able to use one of those pre-made images, then.
You are, apparently, quite confused. ;-) What host you compiled GRUB on doesn't have anything to do with the kernel formats GRUB would be able to load.

(I didn't actually check this, but it would be insane.)

(Which doesn't mean it'd be completely impossible, of course.)
Should I set up a cross compiler and start with a 32 bits OS, or jump straight to 64 bits?
Setting up a cross-compiler is strongly recommended in any case, though not for the purpose of compiling GRUB.
Every good solution is obvious once you've found it.
evoex
Member
Member
Posts: 103
Joined: Tue Dec 13, 2011 4:11 pm

Re: Grub on image without grub command.

Post by evoex »

Solar wrote:
evoex wrote:I'm running a 64 bit computer with Linux. Which means I'll either have to set up a compiler for 32 bits (which I believe is quite a lot of work)...
"gcc -m32" is a lot of work?
Well, no, I managed to compile the files to 32 bits object files. However, when I try to link them the compiler complains about the formats being incompatible (the object files have proper formats, and I use --oformat to specify a 32 bits format, so I'm not sure what it's actually complaining about).
The error message is this:

Code: Select all

ld: i386 architecture of input file `kernel.o' is incompatible with i386:x86-64 output
EDIT: Never mind, fixed it... Needed to use the flag "-melf_i386" for ld.
Solar wrote:
...or use grub to load a 64 bits kernel. Though I won't be able to use one of those pre-made images, then.
You are, apparently, quite confused. ;-) What host you compiled GRUB on doesn't have anything to do with the kernel formats GRUB would be able to load.

(I didn't actually check this, but it would be insane.)

(Which doesn't mean it'd be completely impossible, of course.)
Can it? I thought grub legacy (which those images are using, I believe) was too old to load a kernel in 64 bits mode. I'm not sure about this though. Sure, I can make another loader that switches to long mode and loads the actual kernel, but can it switch to long mode directly? I might be wrong, though a quote on the wiki here makes me think I'm right:
Decide now on how to load your kernel - your own bootloader, GRUB (with separate loader executable), or GRUB2 (elf64 + 32-bit bootstrap code)
When I tried it, grub did not recognize the 64 bits elf file.
Solar wrote:
Should I set up a cross compiler and start with a 32 bits OS, or jump straight to 64 bits?
Setting up a cross-compiler is strongly recommended in any case, though not for the purpose of compiling GRUB.
No, I didn't mean for compiling grub. I meant for the kernel. Should I stick to 32 bits or should I jump straight to 64 bits, as a beginner? Of course, to stick to only 64 bits I would need a boot loader that supports it.


Thanks for your answer!
HyperAssembler
Member
Member
Posts: 36
Joined: Thu Sep 04, 2014 5:24 pm
Location: !SIGSEGV

Re: Grub on image without grub command.

Post by HyperAssembler »

evoex wrote:Hello everyone,

I'm trying to build a bootable disk image using GRUB, for a pet OS project. While there's many tutorials on how to do so, they all use the interactive "grub" command line interface. And for some reason, the Linux distro I'm using (crunchbang) does not have it. I have installed grub, and grub-install works fine, but "grub" reports command not found.

I've tried a lot. I've even tried to install some linux distros (such as CentOS) to work from there, but bochs just stops working after a while.

Before that I obviously tried the "normal" method. For example, I tried the following:

Code: Select all

# dd if=/dev/zero of=disk.img bs=512 count=2056320
# fdisk disk.img
Inside fdisk I changed the following:
128 cylinders
255 heads
63 sectors per track
I made a new partition, made it bootable, such that:

Code: Select all

# fdisk -ul disk.img 
You must set cylinders.
You can do this from the extra functions menu.

Disk disk.img: 0 MB, 0 bytes
255 heads, 63 sectors/track, 0 cylinders, total 0 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x7201d21a

   Device Boot      Start         End      Blocks   Id  System
disk.img1   *          63     2056319     1028128+  83  Linux
And then:

Code: Select all

# losetup -o 32256 /dev/loop0 disk.img
# mkfs.ext2 /dev/loop0
And now I'm a bit at a loss what to do... I tried:

Code: Select all

# mount /dev/loop0 /mnt
# grub-install --root-directory=/mnt --modules=ext2 /dev/loop0 --force
/usr/local/sbin/grub-setup: warn: Attempting to install GRUB to a partitionless disk.  This is a BAD idea..
/usr/local/sbin/grub-setup: warn: Embedding is not possible.  GRUB can only be installed in this setup by using blocklists.  However, blocklists are UNRELIABLE and its use is discouraged..
Installation finished. No error reported.
I'm booting this file using the following bochsrc file:

Code: Select all

megs: 1024
romimage: file=/usr/share/bochs/BIOS-bochs-latest
vgaromimage: file=/usr/share/bochs/VGABIOS-lgpl-latest
ata0-master: type=disk, path="disk.img", mode=flat, cylinders=128, heads=255, spt=63
cpu: count=1, ips=15000000
mouse: enabled=0
log: out.bochs
boot: disk
It says it's booting from hard disk, but nothing happens after that.

So, how do I install grub on there?

EDIT:
This helped a bit...

Code: Select all

# losetup /dev/loop0 disk.img
# losetup -o 32256 /dev/loop1 disk.img
# mount /dev/loop1 /mnt
# grub-install --root-directory=/mnt --modules=ext2 /dev/loop0
Installation finished. No error reported.
And probably makes more sense. Now I get a grub rescue prompt, but it reports: "error: no such device: [GUID]".
At ls it only outputs "(hd0)".

Thanks in advance,
evoex
I have the exact same problem as you do. How did you manage to solve it?(outputs hd0 and unknown filesystem)
Post Reply