有 python grub.cfg 解析器吗?
有谁知道 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道 grub.cfg 的 Python 解析器,但您不需要解析整个文件来获取该信息。这是您要查找的数据的格式:
因此,查找以
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:
So look for lines starting with
menuentry
, parse the name from that line, and scan until the next line with a}
forset root=
.我认为你可以尝试解析器 augeas libconfuse Devicetree
I think you can try parsers augeas libconfuse Devicetree