有 python grub.cfg 解析器吗?

发布于 2025-01-05 03:10:10 字数 1086 浏览 2 评论 0原文

有谁知道 grub2 的 grub.cfg 文件的 python 解析器?

我试图通过设备分区(根)获取“菜单项”,例如

hd0,msdos1: ['Ubuntu, with Linux 3.0.0-15-generic',
            'Ubuntu, with Linux 3.0.0-15-generic (recovery mode)',
            'Ubuntu, with Linux 3.0.0-14-generic']
hd2,msdos1: ["Windows 7 (on /dev/sdc1)"]

等。

解决方案:

re.findall("menuentry ['\"](.*?)['\"].*?set root='(.*?)'", x, re.S)

[('Ubuntu,使用 Linux 3.0.0-15-generic', '(hd0,msdos1)'), ('Ubuntu, 使用 Linux 3.0.0-15-generic(恢复模式)', '(hd0,msdos1)'), ('Ubuntu,使用 Linux 3.0.0-14-generic', '(hd0,msdos1)'), ('Ubuntu, 使用 Linux 3.0.0-14-generic(恢复模式)', '(hd0,msdos1)'), ('Ubuntu,使用 Linux 3.0.0-13-generic', '(hd0,msdos1)'), ('Ubuntu, 使用 Linux 3.0.0-13-generic(恢复模式)', '(hd0,msdos1)'), ('Ubuntu,使用 Linux 3.0.0-12-generic', '(hd0,msdos1)'), ('Ubuntu, 使用 Linux 3.0.0-12-generic(恢复模式)', '(hd0,msdos1)'), ('内存测试(memtest86+)', '(hd0,msdos1)'), ('内存测试 (memtest86+,串行控制台 115200)', '(hd0,msdos1)'), ('Windows 7 (在 /dev/sdc1 上)', '(hd2,msdos1)')]

Does anyone know of a python parser for grub2's grub.cfg file?

I'm trying to get the "menuentry" by device partition (root), e.g.

hd0,msdos1: ['Ubuntu, with Linux 3.0.0-15-generic',
            'Ubuntu, with Linux 3.0.0-15-generic (recovery mode)',
            'Ubuntu, with Linux 3.0.0-14-generic']
hd2,msdos1: ["Windows 7 (on /dev/sdc1)"]

etc.

Solution:

re.findall("menuentry ['\"](.*?)['\"].*?set root='(.*?)'", x, re.S)

[('Ubuntu, with Linux 3.0.0-15-generic', '(hd0,msdos1)'), ('Ubuntu,
with Linux 3.0.0-15-generic (recovery mode)', '(hd0,msdos1)'),
('Ubuntu, with Linux 3.0.0-14-generic', '(hd0,msdos1)'), ('Ubuntu,
with Linux 3.0.0-14-generic (recovery mode)', '(hd0,msdos1)'),
('Ubuntu, with Linux 3.0.0-13-generic', '(hd0,msdos1)'), ('Ubuntu,
with Linux 3.0.0-13-generic (recovery mode)', '(hd0,msdos1)'),
('Ubuntu, with Linux 3.0.0-12-generic', '(hd0,msdos1)'), ('Ubuntu,
with Linux 3.0.0-12-generic (recovery mode)', '(hd0,msdos1)'),
('Memory test (memtest86+)', '(hd0,msdos1)'), ('Memory test
(memtest86+, serial console 115200)', '(hd0,msdos1)'), ('Windows 7
(on /dev/sdc1)', '(hd2,msdos1)')]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦明 2025-01-12 03:10:10

我不知道 grub.cfg 的 Python 解析器,但您不需要解析整个文件来获取该信息。这是您要查找的数据的格式:

menuentry "<name>" [options] {
  ...
  set root='<root>'
  ...
}

因此,查找以 menuentry 开头的行,解析该行中的名称,然后扫描直到带有 } 对于设置root=

I'm not aware of a Python parser for grub.cfg, but you don't need to parse the whole file for that information. This is the format for the data you're looking for:

menuentry "<name>" [options] {
  ...
  set root='<root>'
  ...
}

So look for lines starting with menuentry, parse the name from that line, and scan until the next line with a } for set root=.

猫卆 2025-01-12 03:10:10

我认为你可以尝试解析器 augeas libconfuse Devicetree

I think you can try parsers augeas libconfuse Devicetree

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文