Discussion:
[GTALUG] GRUB's DSL and looping
Giles Orr via talk
2018-07-25 16:54:39 UTC
Permalink
There's probably a GRUB-specific mailing list or forum, but I thought I
would try here first ...

GRUB has a DSL ( https://en.wikipedia.org/wiki/Domain-specific_language )
that looks a lot like shell scripting. Most people never see it as it's
used to generate the menus we use at boot-time and they see only the menus,
but it can do some interesting things - particularly when you're dealing
with multi-boot USB sticks. Here's a simple but slightly useful example:

function cpuinfo {
# only able to determine: 32/64 bit, and is it PAE
echo "GRUB's ability to analyse processors is limited, we can only tell
you:"
if cpuid -p; then
pae_assessment="PAE"
else
pae_assessment="NO PAE"
fi
if cpuid -l; then
echo "64-bit processor, $pae_assessment"
else
echo "32-bit processor, $pae_assessment"
fi
}

But it has some nasty limitations that are frustrating me:
- no pipes
- no command substitution
- no file globbing

That last in particular is really getting on my nerves. I want to source
all the files in one folder, but I can't say 'for file in folder/* ; do'
because the '*' is a literal character, no special meaning. Likewise, I
can't say 'for file in `ls folder/` ; do' because there's no command
substitution.

I was wondering if anyone knows of a solution to this problem. Or even if
you have ideas from long-gone old shells with similar limitations for which
someone worked out a nasty work-around ... What I've been doing is 'for
file in name1 name2 name3 ... ; do', but of course this only works if you
remember to add new filenames to the list.

I've googled quite a bit, but most people only modify GRUB through
/etc/default/grub or, if they're really adventurous, through
/etc/grub.d/40_custom or similar. What I'm looking for is (mostly) at a
lower level than that, and examples are surprisingly hard to come by. And
search results are occasionally polluted by GRUB4DOS results ... GRUB4DOS
_does_ seem to support file globbing. All very confusing. Thanks.
--
Giles
https://www.gilesorr.com/
***@gmail.com
Lennart Sorensen via talk
2018-07-25 17:45:05 UTC
Permalink
Post by Giles Orr via talk
There's probably a GRUB-specific mailing list or forum, but I thought I
would try here first ...
GRUB has a DSL ( https://en.wikipedia.org/wiki/Domain-specific_language )
that looks a lot like shell scripting. Most people never see it as it's
used to generate the menus we use at boot-time and they see only the menus,
but it can do some interesting things - particularly when you're dealing
function cpuinfo {
# only able to determine: 32/64 bit, and is it PAE
echo "GRUB's ability to analyse processors is limited, we can only tell
you:"
if cpuid -p; then
pae_assessment="PAE"
else
pae_assessment="NO PAE"
fi
if cpuid -l; then
echo "64-bit processor, $pae_assessment"
else
echo "32-bit processor, $pae_assessment"
fi
}
- no pipes
- no command substitution
- no file globbing
Actually it does file globbing if you load the regexp module.

Apparently this works:

insmod regexp
for i in /boot/*; do echo $i; done

Or:

grub> ls /boot/*
error: file `/boot/*' not found.
grub> insmod regexp
grub> ls /boot/*
unicode.pf2 i386-pc/ locale/ fonts/ grubenv grub.cfg
config-4.16.0-2-amd64
...
--
Len Sorensen
---
Talk Mailing List
***@gtalug.org
https://gtalu
Giles Orr via talk
2018-07-25 18:09:00 UTC
Permalink
Post by Giles Orr via talk
Post by Giles Orr via talk
There's probably a GRUB-specific mailing list or forum, but I thought I
would try here first ...
GRUB has a DSL ( https://en.wikipedia.org/wiki/Domain-specific_language
)
Post by Giles Orr via talk
that looks a lot like shell scripting. Most people never see it as it's
used to generate the menus we use at boot-time and they see only the
menus,
Post by Giles Orr via talk
but it can do some interesting things - particularly when you're dealing
function cpuinfo {
# only able to determine: 32/64 bit, and is it PAE
echo "GRUB's ability to analyse processors is limited, we can only
tell
Post by Giles Orr via talk
you:"
if cpuid -p; then
pae_assessment="PAE"
else
pae_assessment="NO PAE"
fi
if cpuid -l; then
echo "64-bit processor, $pae_assessment"
else
echo "32-bit processor, $pae_assessment"
fi
}
- no pipes
- no command substitution
- no file globbing
Actually it does file globbing if you load the regexp module.
insmod regexp
for i in /boot/*; do echo $i; done
grub> ls /boot/*
error: file `/boot/*' not found.
grub> insmod regexp
grub> ls /boot/*
unicode.pf2 i386-pc/ locale/ fonts/ grubenv grub.cfg
config-4.16.0-2-amd64
...
Thank you!

I don't think that's documented anywhere, and it doesn't strike me as being
in any way obvious.
--
Giles
https://www.gilesorr.com/
***@gmail.com
Lennart Sorensen via talk
2018-07-25 18:14:02 UTC
Permalink
Post by Giles Orr via talk
I don't think that's documented anywhere, and it doesn't strike me as being
in any way obvious.
Well it's vaguely documented in that the module has a description.
And it is totally not obvious that a module could change such behaviour
in the first place.
--
Len Sorensen
---
Talk Mailing List
***@gtalug.org
https://gtalug.org/mailm
Christopher Browne via talk
2018-07-25 20:07:03 UTC
Permalink
Post by Lennart Sorensen via talk
Post by Giles Orr via talk
I don't think that's documented anywhere, and it doesn't strike me as being
in any way obvious.
Well it's vaguely documented in that the module has a description.
And it is totally not obvious that a module could change such behaviour
in the first place.
Is there some place to push a documentation patch for that?

It's managed at Savannah, so there's a git repo, and it sure seems a
neat idea to augment it...

git clone git://git.savannah.gnu.org/grub.git
--
When confronted by a difficult problem, solve it by reducing it to the
question, "How would the Lone Ranger handle this?"
---
Talk Mailing List
***@gtalug.org
https://gtalu
Loading...