I've been playing with fixing Ubuntu bug 1097570; trying to debug a script we use for the EFI images which detects where to boot from based on the location of a .disk/info file.
Turns out this mostly works, except for the fact that grub appears to always list devices in a specific order, which interferes with the detection if more than one device contains that file.
I think I can make this work appropriately provided we:
- Use hints for search --file;
- Have some value to pass to --hint= designating the boot drive.
Included is a patch that adds a "bootdrive" variable alongside cmdpath; which contains just the drive designation for the boot device, to be used as such:
search --set=root --hint=$bootdrive --file .disk/info
I'd much appreciate review on that patch :)
PATCH 1/1 Set a bootdrive environment variable.
--- grub-core/kern/main.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
grub-core/kern/main.c +15 -0
@@ -130,6 +130,8 @@ grub_set_prefix_and_root (void)if (fwdevice) {char *cmdpath; +char *bootdrive =NULL ; +char *cptr; cmdpath =grub_xasprintf ("(%s)%s" , fwdevice, fwpath ? :"" );if (cmdpath)
@@ -138,6 +140,19 @@ grub_set_prefix_and_root (void)grub_env_export ("cmdpath" );grub_free (cmdpath); } + + cptr =grub_strrchr (fwdevice,',' ); +if (cptr) + { + bootdrive =grub_strndup (fwdevice, cptr - fwdevice); + cptr++; + } +if (bootdrive) + { +grub_env_set ("bootdrive" , bootdrive); +grub_env_export ("bootdrive" ); +grub_free (bootdrive); + } }if (prefix)
Signature · 1 line
2.1.4
Re: Set a bootdrive environment variable.
In reply to Mathieu Trudel-LapierreВ Tue, 26 May 2015 12:46:07 -0400
Mathieu Trudel-Lapierre <[email protected]> пишет:
Quoted 4 lines
I've been playing with fixing Ubuntu bug 1097570; trying to debug a script we use for the EFI images which detects where to boot from based on the location of a .disk/info file.
Quoted 8 lines
Turns out this mostly works, except for the fact that grub appears to always list devices in a specific order, which interferes with the detection if more than one device contains that file.
I think I can make this work appropriately provided we:
- Use hints for search --file;
- Have some value to pass to --hint= designating the boot drive.
If you always want to use the same device GRUB was booted from why do you need to search for anything at all?
Quoted 7 lines
Included is a patch that adds a "bootdrive" variable alongside cmdpath; which contains just the drive designation for the boot device, to be used as such:
search --set=root --hint=$bootdrive --file .disk/info
I'd much appreciate review on that patch :)
You can extract it from $cmdpath using regexp already. In retrospect having separate variables for device and path would be more flexible; but $drive is not appropriate name as it can refer to network as well. Something like $grub_bootdev and $grub_bootpath.
Re: Set a bootdrive environment variable.
Quoted 15 lines · Andrei Borzenkov
В Tue, 26 May 2015 12:46:07 -0400
Mathieu Trudel-Lapierre <[email protected]> пишет:Quoted 1 line
I've been playing with fixing Ubuntu bug 1097570; trying to debug a
script we
Quoted 1 line
use for the EFI images which detects where to boot from based on the
location
Quoted 3 lines
of a .disk/info file.
Turns out this mostly works, except for the fact that grub appears to
always
Quoted 1 line
list devices in a specific order, which interferes with the detection if
more
Quoted 2 lines
than one device contains that file.
Quoted 8 lines
Quoted 4 lines
I think I can make this work appropriately provided we:
- Use hints for search --file;
- Have some value to pass to --hint= designating the boot drive.If you always want to use the same device GRUB was booted from why do you need to search for anything at all?
My understanding from why the search was used is that $root may not have been set for all types of devices (i.e. CDs), and the same built grub images (for EFI) were used on bootable USB and CDs since they are both built from a single ISO.
In retrospect, having a properly set $cmdpath (or $grub_bootdev or whatever), probably makes this unnecessary.
Quoted 7 lines
Quoted 1 line
Included is a patch that adds a "bootdrive" variable alongside cmdpath;
which
Quoted 1 line
contains just the drive designation for the boot device, to be used as
such:
Quoted 3 lines
search --set=root --hint=$bootdrive --file .disk/info
Quoted 8 lines
Quoted 2 lines
I'd much appreciate review on that patch :)
You can extract it from $cmdpath using regexp already. In retrospect having separate variables for device and path would be more flexible; but $drive is not appropriate name as it can refer to network as well. Something like $grub_bootdev and $grub_bootpath.
That was one of the reasons I wanted review on it, so thanks!
I'm happy to change it to $grub_bootdev or whatever else if we feel having separate variable for this in indeed a good idea (and I'll add what's needed to get $grub_bootpath).
Mathieu Trudel-Lapierre <[email protected]>
Freenode: cyphermox, Jabber: [email protected] 4096R/EE018C93 1967 8F7D 03A1 8F38 732E FF82 C126 33E1 EE01 8C93
Re: Set a bootdrive environment variable.
В Wed, 27 May 2015 08:55:09 -0400
Mathieu Trudel-Lapierre <[email protected]> пишет:
Quoted 23 lines
Quoted 20 lines · Andrei Borzenkov
В Tue, 26 May 2015 12:46:07 -0400
Mathieu Trudel-Lapierre <[email protected]> пишет:Quoted 1 line
I've been playing with fixing Ubuntu bug 1097570; trying to debug a
script we
Quoted 1 line
use for the EFI images which detects where to boot from based on the
location
Quoted 3 lines
of a .disk/info file.
Turns out this mostly works, except for the fact that grub appears to
always
Quoted 1 line
list devices in a specific order, which interferes with the detection if
more
Quoted 6 lines
than one device contains that file.
I think I can make this work appropriately provided we:
- Use hints for search --file;
- Have some value to pass to --hint= designating the boot drive.
Quoted 9 lines
Quoted 3 lines
If you always want to use the same device GRUB was booted from why do you need to search for anything at all?
My understanding from why the search was used is that $root may not have been set for all types of devices (i.e. CDs), and the same built grub images (for EFI) were used on bootable USB and CDs since they are both built from a single ISO.
That depends on your image. If image defines prefix and drive and/or partition is missing (e.g. (,msdos1)) missing parts are derived from whatever firmware provides (effectively what ends up in $cmdpath) and should be set for any boot media.
Although I just hit a system (Fujitsu Lifebook S761) which passes bogus device path to image that does not match any existing handle, so both $cmdpath and $root are not set.
Quoted 20 lines
In retrospect, having a properly set $cmdpath (or $grub_bootdev or whatever), probably makes this unnecessary.
Quoted 7 lines
Quoted 1 line
Included is a patch that adds a "bootdrive" variable alongside cmdpath;
which
Quoted 1 line
contains just the drive designation for the boot device, to be used as
such:
Quoted 3 lines
search --set=root --hint=$bootdrive --file .disk/info
Quoted 8 lines
Quoted 2 lines
I'd much appreciate review on that patch :)
You can extract it from $cmdpath using regexp already. In retrospect having separate variables for device and path would be more flexible; but $drive is not appropriate name as it can refer to network as well. Something like $grub_bootdev and $grub_bootpath.
Quoted 6 lines
That was one of the reasons I wanted review on it, so thanks!
I'm happy to change it to $grub_bootdev or whatever else if we feel having separate variable for this in indeed a good idea (and I'll add what's needed to get $grub_bootpath).
The problem is that $cmdpath had been around for long enough. If $cmdpath is used only after loading normal (that are cases I'm aware of) we can make it compatibility variable set by normal without causing more core bloat.
Quoted 4 lines
Mathieu Trudel-Lapierre <[email protected]>
Freenode: cyphermox, Jabber: [email protected] 4096R/EE018C93 1967 8F7D 03A1 8F38 732E FF82 C126 33E1 EE01 8C93
Re: Set a bootdrive environment variable.
What's the problem with extracting this from $prefix ? This seems to be a better match if what you want is location of GRUB.
Quoted 88 lines · Andrei Borzenkov
В Wed, 27 May 2015 08:55:09 -0400
Mathieu Trudel-Lapierre <[email protected]> пишет:Quoted 23 lines
Quoted 20 lines · Andrei Borzenkov
В Tue, 26 May 2015 12:46:07 -0400
Mathieu Trudel-Lapierre <[email protected]> пишет:Quoted 1 line
I've been playing with fixing Ubuntu bug 1097570; trying to debug a
script we
Quoted 1 line
use for the EFI images which detects where to boot from based on the
location
Quoted 3 lines
of a .disk/info file.
Turns out this mostly works, except for the fact that grub appears to
always
Quoted 1 line
list devices in a specific order, which interferes with the detection if
more
Quoted 6 lines
than one device contains that file.
I think I can make this work appropriately provided we:
- Use hints for search --file;
- Have some value to pass to --hint= designating the boot drive.Quoted 9 lines
Quoted 3 lines
If you always want to use the same device GRUB was booted from why do you need to search for anything at all?
My understanding from why the search was used is that $root may not have been set for all types of devices (i.e. CDs), and the same built grub images (for EFI) were used on bootable USB and CDs since they are both built from a single ISO.
That depends on your image. If image defines prefix and drive and/or partition is missing (e.g. (,msdos1)) missing parts are derived from whatever firmware provides (effectively what ends up in $cmdpath) and should be set for any boot media.
Although I just hit a system (Fujitsu Lifebook S761) which passes bogus device path to image that does not match any existing handle, so both $cmdpath and $root are not set.
Quoted 20 lines
In retrospect, having a properly set $cmdpath (or $grub_bootdev or whatever), probably makes this unnecessary.
Quoted 7 lines
Quoted 1 line
Included is a patch that adds a "bootdrive" variable alongside cmdpath;
which
Quoted 1 line
contains just the drive designation for the boot device, to be used as
such:
Quoted 3 lines
search --set=root --hint=$bootdrive --file .disk/info
Quoted 8 lines
Quoted 2 lines
I'd much appreciate review on that patch :)
You can extract it from $cmdpath using regexp already. In retrospect having separate variables for device and path would be more flexible; but $drive is not appropriate name as it can refer to network as well. Something like $grub_bootdev and $grub_bootpath.
Quoted 6 lines
That was one of the reasons I wanted review on it, so thanks!
I'm happy to change it to $grub_bootdev or whatever else if we feel having separate variable for this in indeed a good idea (and I'll add what's needed to get $grub_bootpath).
The problem is that $cmdpath had been around for long enough. If $cmdpath is used only after loading normal (that are cases I'm aware of) we can make it compatibility variable set by normal without causing more core bloat.
Quoted 4 lines
Mathieu Trudel-Lapierre <[email protected]>
Freenode: cyphermox, Jabber: [email protected] 4096R/EE018C93 1967 8F7D 03A1 8F38 732E FF82 C126 33E1 EE01 8C93_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Re: Set a bootdrive environment variable.
В Thu, 04 Jun 2015 17:40:01 +0200
Vladimir 'φ-coder/phcoder' Serbinenko <[email protected]> пишет:
Quoted 2 lines
What's the problem with extracting this from $prefix ? This seems to be a better match if what you want is location of GRUB.
The original problem was that this information was needed to set $prefix so it cannot be extracted from it :)
It can be extracted from $cmdpath but then it requires regexp in early config and feels backwards in general.
Quoted 91 lines · Andrei Borzenkov
Quoted 88 lines
В Wed, 27 May 2015 08:55:09 -0400
Mathieu Trudel-Lapierre <[email protected]> пишет:Quoted 23 lines
Quoted 20 lines · Andrei Borzenkov
В Tue, 26 May 2015 12:46:07 -0400
Mathieu Trudel-Lapierre <[email protected]> пишет:Quoted 1 line
I've been playing with fixing Ubuntu bug 1097570; trying to debug a
script we
Quoted 1 line
use for the EFI images which detects where to boot from based on the
location
Quoted 3 lines
of a .disk/info file.
Turns out this mostly works, except for the fact that grub appears to
always
Quoted 1 line
list devices in a specific order, which interferes with the detection if
more
Quoted 6 lines
than one device contains that file.
I think I can make this work appropriately provided we:
- Use hints for search --file;
- Have some value to pass to --hint= designating the boot drive.Quoted 9 lines
Quoted 3 lines
If you always want to use the same device GRUB was booted from why do you need to search for anything at all?
My understanding from why the search was used is that $root may not have been set for all types of devices (i.e. CDs), and the same built grub images (for EFI) were used on bootable USB and CDs since they are both built from a single ISO.
That depends on your image. If image defines prefix and drive and/or partition is missing (e.g. (,msdos1)) missing parts are derived from whatever firmware provides (effectively what ends up in $cmdpath) and should be set for any boot media.
Although I just hit a system (Fujitsu Lifebook S761) which passes bogus device path to image that does not match any existing handle, so both $cmdpath and $root are not set.
Quoted 20 lines
In retrospect, having a properly set $cmdpath (or $grub_bootdev or whatever), probably makes this unnecessary.
Quoted 7 lines
Quoted 1 line
Included is a patch that adds a "bootdrive" variable alongside cmdpath;
which
Quoted 1 line
contains just the drive designation for the boot device, to be used as
such:
Quoted 3 lines
search --set=root --hint=$bootdrive --file .disk/info
Quoted 8 lines
Quoted 2 lines
I'd much appreciate review on that patch :)
You can extract it from $cmdpath using regexp already. In retrospect having separate variables for device and path would be more flexible; but $drive is not appropriate name as it can refer to network as well. Something like $grub_bootdev and $grub_bootpath.
Quoted 6 lines
That was one of the reasons I wanted review on it, so thanks!
I'm happy to change it to $grub_bootdev or whatever else if we feel having separate variable for this in indeed a good idea (and I'll add what's needed to get $grub_bootpath).
The problem is that $cmdpath had been around for long enough. If $cmdpath is used only after loading normal (that are cases I'm aware of) we can make it compatibility variable set by normal without causing more core bloat.
Quoted 4 lines
Mathieu Trudel-Lapierre <[email protected]>
Freenode: cyphermox, Jabber: [email protected] 4096R/EE018C93 1967 8F7D 03A1 8F38 732E FF82 C126 33E1 EE01 8C93_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Re: Set a bootdrive environment variable.
In reply to Andrei BorzenkovQuoted 6 lines · Andrei Borzenkov
The problem is that $cmdpath had been around for long enough. If $cmdpath is used only after loading normal (that are cases I'm aware of) we can make it compatibility variable set by normal without causing more core bloat.
I'm not finding any uses of cmdpath in git, and when I went to dig in RH/Fedora packaging, it looks like there is already a patch that appears to rename it and change its meaning [1], along with two other related patches.
You mention you know of cases where it's used after loading normal, could you please point me to them?
I'd like to try to come up with a patch that will work for everyone. FWIW, my requirement right now is only to have some way (aside from regexp, since we're concerned running regexp in the EFI context could increase attack exposure) to get just the GRUB boot device (ie. cd0, hd0, etc. without partition information), so that ISO images could boot and load a generic grub.cfg that doesn't need to encode a filesystem UUID or some other identifier.
[1]
http://pkgs.fedoraproject.org/cgit/grub2.git/tree/0090-Add-fw_path-variable-revised.patch
Kind regards,
/ Matt
Re: Set a bootdrive environment variable.
В Mon, 6 Jul 2015 15:44:44 -0400
Mathieu Trudel-Lapierre <[email protected]> пишет:
Quoted 10 lines
Quoted 6 lines · Andrei Borzenkov
The problem is that $cmdpath had been around for long enough. If $cmdpath is used only after loading normal (that are cases I'm aware of) we can make it compatibility variable set by normal without causing more core bloat.
Quoted 4 lines
I'm not finding any uses of cmdpath in git, and when I went to dig in RH/Fedora packaging, it looks like there is already a patch that appears to rename it and change its meaning [1], along with two other related patches.
Well, it is up to RH. Moreover, the real problem as indicated in rhbz is
--><--
The issue comes from the fact that the original patch does not clear the grub_errno in case the grub.cfg is not found in the bootlocation. --><--
and it is unrelated to how variable is named. I have patch for it somewhere, I hit this issue as well. Another issue is missing initial '/' which is fixed in grub in the meantime as well.
Quoted 3 lines
You mention you know of cases where it's used after loading normal, could you please point me to them?
I can think of some use cases and we never know how creative people become in between.
Quoted 8 lines
I'd like to try to come up with a patch that will work for everyone. FWIW, my requirement right now is only to have some way (aside from regexp, since we're concerned running regexp in the EFI context could increase attack exposure) to get just the GRUB boot device (ie. cd0, hd0, etc. without partition information), so that ISO images could boot and load a generic grub.cfg that doesn't need to encode a filesystem UUID or some other identifier.
If you set initial prefix in grub.efi to '()', grub will strip partition part when setting $root; look at what grub-mkrescue does. Is it good enough for your purpose?
Quoted 6 lines
[1]
http://pkgs.fedoraproject.org/cgit/grub2.git/tree/0090-Add-fw_path-variable-revised.patchKind regards,
/ Matt