[英]Is there a python grub.cfg parser?
Does anyone know of a python parser for grub2's grub.cfg file?有谁知道 grub2 的 grub.cfg 文件的 python 解析器?
I'm trying to get the "menuentry" by device partition (root), eg我正在尝试通过设备分区(root)获取“menuentry”,例如
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)')]
[('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)'), ('内存测试(memtest86+)', '(hd0,msdos1)'), ('内存测试(memtest86+,串口控制台115200)', '(hd0,msdos1)'), ( 'Windows 7(在 /dev/sdc1 上)','(hd2,msdos1)')]
I'm not aware of a Python parser for grub.cfg, but you don't need to parse the whole file for that information. 我不知道grub.cfg的Python解析器,但是您不需要解析整个文件以获取该信息。 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=
. 因此,查找以
menuentry
开头的行,从该行解析名称,然后扫描到下一行,其中带有}
for set root=
。
I think you can try parsers augeas libconfuse Devicetree我想你可以试试解析器augeas libconfuse Devicetree
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.