grub2 的搜索命令中的 --hint 选项有什么作用?

Jen*_*ens 14 grub2

我正在查看search命令的官方 GRUB2 文档,如http://www.gnu.org/software/grub/manual/grub.html#index-search

Command: search [--file|--label|--fs-uuid] [--set [var]] [--no-floppy] name

Search devices by file (-f, --file), filesystem label (-l, --label),
or filesystem UUID (-u, --fs-uuid).

If the --set option is used, the first device found is set as the
value of environment variable var. The default variable is ‘root’.

The --no-floppy option prevents searching floppy devices, which can be slow.

The ‘search.file’, ‘search.fs_label’, and ‘search.fs_uuid’ commands are aliases
for ‘search --file’, ‘search --label’, and ‘search --fs-uuid’ respectively.
Run Code Online (Sandbox Code Playgroud)

在 5.3 节中有很多例子

menuentry "FreeBSD" {
      insmod zfs
      search --set=root --label freepool --hint hd0,msdos7
      ...
}
Run Code Online (Sandbox Code Playgroud)

--hint除了作为示例之外,该选项似乎没有记录。它具体有什么作用?参数的确切格式是什么?

tot*_*tti 9

--hint用于在有多个匹配的分区时选择要选择的分区。默认情况下选择第一个匹配的。

假设有2个带有标签boot的存储设备 如下

hd0,msdos1
hd1,msdos7
Run Code Online (Sandbox Code Playgroud)

然后命令:

search --set=root --label freepool --hint hd1,msdos7
Run Code Online (Sandbox Code Playgroud)

将选择hd1,msdos7而不是 hd0,msdos1

  • 知道为什么存在单独的 --hint-efi、--hint-baremetal 等选项吗? (6认同)
  • 如果使用了“search --fs-uuid”,那么“--hint”有什么用呢? (2认同)
  • @SaadMalik,UUID 不必是唯一的。文件系统 UUID 的工作方式与标签相同,但 UUID 通常在 FS 创建时生成。 (2认同)

fro*_*utz 5

GRUB 手册中没有对此进行描述,但可以在 GRUB 本身(search --help在 GRUB shell 上)中找到文档:

--hint
    First try the device HINT.
    If HINT ends in comma, also try subpartitions

--hint-ieee1275
    First try the device HINT if currently running on IEEE1275.
    If HINT ends in comma, also try subpartitions

--hint-bios
    First try the device HINT if currently running on BIOS.
    If HINT ends in comma, also try subpartitions

--hint-baremetal
    First try the device HINT if direct hardware access is supported.
    If HINT ends in comma, also try subpartitions

--hint-efi
    First try the device HINT if currently running on EFI.
    If HINT ends in comma, also try subpartitions

--hint-arc
    First try the device HINT if currently running on ARC.
    If HINT ends in comma, also try subpartitions
Run Code Online (Sandbox Code Playgroud)

那么“首次尝试设备”有什么意义呢?

您必须了解这search可能是一个缓慢的操作。

也许您有 50 个驱动器,每个驱动器有 100 个分区,现在search必须经历所有这些......直到它最终在第 2356 次尝试中找到您正在寻找的 UUID。

或者,也许您的设备速度非常慢,并且检查其 UUID 会导致search卡住一段时间。我想这是--no-floppy为了避免最常见的情况 - 但其他设备也可能很慢。

使用--hint,您可以设置要首先检查的设备。如果提示正确,您就可以完全跳过可能很长的搜索操作。所以这是一个速度优化。(只有一个驱动器、三个分区时可能不会被注意到)

@totti 的回答中描述的效果是,当两个设备具有相同的 LABEL 或 UUID 时,优先考虑特定设备,这应该只是副作用。

当然,如果您首先检查一台设备,则不应在另一台设备上找到重复项。即便如此,一开始就不存在这样的重复项会更有意义。由于重复的 UUID(或标签)可以被视为配置错误,并且如果结果--hint是错误的,它仍然可能返回错误的设备。