A question about GRUB

Programming, for all ages and all languages.
Post Reply
User avatar
teeuwen
Posts: 13
Joined: Thu May 04, 2017 2:03 am

A question about GRUB

Post by teeuwen »

Not specifically OS dev. related so I thought I'd just post it here.

After compilation of my kernel has finished, the linked binary is copied to a root directory which in turn is written to an ISO image by genisoimage, pretty normal stuff.
Instead of using grub-mkrescue however (which is recommended by the Wiki if I remember correctly and many of you probably also do it like this), I'm using grub-mkimage so that I can select which GRUB modules I want and the image doesn't immediately become 20 megabytes.

The modules I have included are

Code: Select all

i386-pc biosdisk boot iso9660 multiboot normal
Works like a charm for booting from cdrom

The thing is that comments in my grub.cfg file are interpreted instead of ignored, which results 3 (the amount of comments in the file) ugly warnings during boot:

Code: Select all

Unknown command '#'.
Unknown command '#'.
Unknown command '#'.
I've looked through a bunch of docs (and even tried to find it in the enormous GRUB2 source which is pretty much impossible) but I just can't seem to find what I'm missing. If anyone knows about a module or switch that resolves this, I'd love to hear it!
"Writing a portable OS is not much harder than a nonportable one, and all systems should be written with portability in mind these days." — Andrew S. Tanenbaum

Elarix
Source: https://github.com/teeuwen/elarix
User avatar
dozniak
Member
Member
Posts: 723
Joined: Thu Jul 12, 2012 7:29 am
Location: Tallinn, Estonia

Re: A question about GRUB

Post by dozniak »

Do these comments start on the start of the line? (Column 1) or somewhere after a few whitespaces?

The grub2 docs mention that starting a line with # makes it a comment, but it warns if a command has been broken in the middle then parser will be upset.
Learn to read.
User avatar
teeuwen
Posts: 13
Joined: Thu May 04, 2017 2:03 am

Re: A question about GRUB

Post by teeuwen »

dozniak wrote:Do these comments start on the start of the line? (Column 1) or somewhere after a few whitespaces?

The grub2 docs mention that starting a line with # makes it a comment, but it warns if a command has been broken in the middle then parser will be upset.
Nope, it contains exactly this:

Code: Select all

#
# /boot/grub/grub.cfg: configuration file for GRUB
#

multiboot /boot/kernel root=/sys/dev/hda0 rootfs=iso9660 console=/sys/dev/con0
boot
Unix line endings, nothing out of the ordinary. And I should mention that this works just fine if I'm using grub-mkrescue, so it has nothing to do with syntax errors. Thanks for your reply though!
"Writing a portable OS is not much harder than a nonportable one, and all systems should be written with portability in mind these days." — Andrew S. Tanenbaum

Elarix
Source: https://github.com/teeuwen/elarix
User avatar
dozniak
Member
Member
Posts: 723
Joined: Thu Jul 12, 2012 7:29 am
Location: Tallinn, Estonia

Re: A question about GRUB

Post by dozniak »

teeuwen wrote: Unix line endings, nothing out of the ordinary. And I should mention that this works just fine if I'm using grub-mkrescue, so it has nothing to do with syntax errors. Thanks for your reply though!
Does mkrescue probably strip the comments from the cfg file before writing it to the image?

I haven't looked into the grub code itself, but it may be worth doing just to see where exactly the `#` parsing and stripping takes place.
Learn to read.
User avatar
teeuwen
Posts: 13
Joined: Thu May 04, 2017 2:03 am

Re: A question about GRUB

Post by teeuwen »

dozniak wrote:
teeuwen wrote: Unix line endings, nothing out of the ordinary. And I should mention that this works just fine if I'm using grub-mkrescue, so it has nothing to do with syntax errors. Thanks for your reply though!
Does mkrescue probably strip the comments from the cfg file before writing it to the image?

I haven't looked into the grub code itself, but it may be worth doing just to see where exactly the `#` parsing and stripping takes place.
I have, like I mentioned before, and here's my findings:

So after downloading the source I did a grep for '#', which gives about 2 dozen results. Note that all the relevant results are in the grub-core/ directory and utilities like grub-mkrescue and grub-mkimage are in utils/.
This (probably) rules out the possibility that mkrescue strips out comments, because this is all handled by the "normal" module (which I did include) and I couldn't find any comment stripping code in the mkrescue source.
The exact location where the comments are stripped is: grub-core/normal/main.c > grub_cmd_normal > grub_enter_normal_mode > grub_normal_executable > read_config_file > read_config_file_getline (on line 96 in the GRUB git repo). No arguments to any of the functions to ignore comments or anything like that. This just seems like a dead end to me. It may even be a bug.
"Writing a portable OS is not much harder than a nonportable one, and all systems should be written with portability in mind these days." — Andrew S. Tanenbaum

Elarix
Source: https://github.com/teeuwen/elarix
User avatar
eryjus
Member
Member
Posts: 286
Joined: Fri Oct 21, 2011 9:47 pm
Libera.chat IRC: eryjus
Location: Tustin, CA USA

Re: A question about GRUB

Post by eryjus »

It was a simple test for me... grub2-mkrescue did not strip comments.

Just curious, is this grub or grub2?
Adam

The name is fitting: Century Hobby OS -- At this rate, it's gonna take me that long!
Read about my mistakes and missteps with this iteration: Journal

"Sometimes things just don't make sense until you figure them out." -- Phil Stahlheber
User avatar
teeuwen
Posts: 13
Joined: Thu May 04, 2017 2:03 am

Re: A question about GRUB

Post by teeuwen »

eryjus wrote:It was a simple test for me... grub2-mkrescue did not strip comments.

Just curious, is this grub or grub2?
GRUB2, 2.02~rc2 to be specific.

But what do mean when you say that grub-mkrescue does NOT strip comments for you? Because it sure does for me.
"Writing a portable OS is not much harder than a nonportable one, and all systems should be written with portability in mind these days." — Andrew S. Tanenbaum

Elarix
Source: https://github.com/teeuwen/elarix
User avatar
eryjus
Member
Member
Posts: 286
Joined: Fri Oct 21, 2011 9:47 pm
Libera.chat IRC: eryjus
Location: Tustin, CA USA

Re: A question about GRUB

Post by eryjus »

teeuwen wrote:But what do mean when you say that grub-mkrescue does NOT strip comments for you?
I added a comment into my grub.cfg and ran grub2-mkrescue. In fact:

Code: Select all

grub2-mkrescue -o $@ $(I686-KERNEL-SYSROOT) 2> /dev/null
the command line is rather simple. I opened the resulting .iso with ark and navigated to the grub.cfg therein and opened it. The comments were still there as I wrote them. I also checked that the image was bootable and I was able to boot both my MB and MB2 header from the resulting .iso.

For the record:

Code: Select all

[adam@os-dev century]$ grub2-mkrescue --version
grub2-mkrescue (GRUB) 2.02~beta3
and I am running on FC25 and I am patch-current as of 7 days ago.
Adam

The name is fitting: Century Hobby OS -- At this rate, it's gonna take me that long!
Read about my mistakes and missteps with this iteration: Journal

"Sometimes things just don't make sense until you figure them out." -- Phil Stahlheber
User avatar
teeuwen
Posts: 13
Joined: Thu May 04, 2017 2:03 am

Re: A question about GRUB

Post by teeuwen »

eryjus wrote:
teeuwen wrote:But what do mean when you say that grub-mkrescue does NOT strip comments for you?
I added a comment into my grub.cfg and ran grub2-mkrescue. In fact:

Code: Select all

grub2-mkrescue -o $@ $(I686-KERNEL-SYSROOT) 2> /dev/null
the command line is rather simple. I opened the resulting .iso with ark and navigated to the grub.cfg therein and opened it. The comments were still there as I wrote them. I also checked that the image was bootable and I was able to boot both my MB and MB2 header from the resulting .iso.

For the record:

Code: Select all

[adam@os-dev century]$ grub2-mkrescue --version
grub2-mkrescue (GRUB) 2.02~beta3
and I am running on FC25 and I am patch-current as of 7 days ago.
I think you're missing the point here I'm afraid.
2017-05-06-235603_3200x1080_scrot.png
2017-05-06-235603_3200x1080_scrot.png (7.25 KiB) Viewed 6028 times
And grub-mkrescue is the one that is working just fine. The problem is with grub-mkimage.
"Writing a portable OS is not much harder than a nonportable one, and all systems should be written with portability in mind these days." — Andrew S. Tanenbaum

Elarix
Source: https://github.com/teeuwen/elarix
User avatar
eryjus
Member
Member
Posts: 286
Joined: Fri Oct 21, 2011 9:47 pm
Libera.chat IRC: eryjus
Location: Tustin, CA USA

Re: A question about GRUB

Post by eryjus »

teeuwen wrote:I think you're missing the point here I'm afraid.
Perhaps I did. I thought one comment was that mkrescue was stripping comments in grub.cfg. I was going after that comment and I was not clear.

Anyway, I'm more than happy to run a test for you here if you feel that would help. Just send me your command line.
Adam

The name is fitting: Century Hobby OS -- At this rate, it's gonna take me that long!
Read about my mistakes and missteps with this iteration: Journal

"Sometimes things just don't make sense until you figure them out." -- Phil Stahlheber
User avatar
teeuwen
Posts: 13
Joined: Thu May 04, 2017 2:03 am

Re: A question about GRUB

Post by teeuwen »

I've filed a bug report: https://savannah.gnu.org/bugs/?50963

We'll see what happens now.

EDIT: Got a reply:
Embedded config is interpreted by rescue parser, not by normal.mod. It is intended for limited use cases and not for general purpose scripting.
For some reason I don't quite believe the guy (even though he's a maintainer) as I've looked though the normal.mod code myself but I guess I'm gonna have to use grub-mkrescue or simply remove the comment after all.
"Writing a portable OS is not much harder than a nonportable one, and all systems should be written with portability in mind these days." — Andrew S. Tanenbaum

Elarix
Source: https://github.com/teeuwen/elarix
Post Reply