GRUB configuration files

This parser reads the configuration of the GRand Unified Bootloader, versions 1 or 2.

This is currently a fairly simple parsing process. Data read from the file is put into roughly three categories:

  • configs: lines read from the file that aren’t boot options (i.e. excluding lines that go in the title and menuentry sections). These are split into pairs on the first ‘=’ sign.

  • title: (GRUB v1 only) lines prefixed by the word ‘title’. All following lines up to the next title line are folded together.

  • menuentry: (GRUB v2 only) lines prefixed by the word ‘menuentry’. All following lines up to the line starting with ‘}’ are treated as part of one menu entry.

Each of these categories is (currently) stored as a simple list of tuples.

  • For the configs dict, the key-value pairs based on the line, split on the first ‘=’ character. If nothing is found after the ‘=’ character, then the value is ‘’.

  • For the title and menuentry list, dict of each boot entry is stored.

    • The items will be key-value pairs, e.g. load_video will be stored as {'load_video': ''} and set root='hd0,msdos1' will be stored as {'set': "root='hd0,msdos1'"}.

Note

For GRUB version 2, all lines between the if and fi will be ignored due to we cannot analyze the result of the bash conditions.

Parsers provided by this module are:

Grub1Config - file /boot/grub.conf

Grub1EFIConfig - file /boot/efi/EFI/redhat/grub.conf

Grub2Config - file /boot/grub/grub2.cfg

Grub2EFIConfig - file /boot/efi/EFI/redhat/grub.cfg

BootLoaderEntries - file /boot/loader/entries/*.conf

class insights.parsers.grub_conf.BootEntry(data={})[source]

Bases: dict

An object representing a boot entry in the Grub Configuration.

name

Name of the boot entry

Type

str

cmdline

Cmdline of the boot entry

Type

str

class insights.parsers.grub_conf.BootLoaderEntries(context)[source]

Bases: Parser, dict

Parses the /boot/loader/entries/*.conf files.

title

the name of the boot entry

Type

str

cmdline

the cmdline of the saved boot entry

Type

str

Raises

SkipException -- when input content is empty or no useful data.

parse_content(content)[source]

Parses the /boot/loader/entries/*.conf files.

class insights.parsers.grub_conf.Grub1Config(*args, **kwargs)[source]

Bases: GrubConfig

Parser for configuration for GRUB version 1.

Examples

>>> grub1_content = '''
... default=0
... timeout=0
... splashimage=(hd0,0)/grub/splash.xpm.gz
... hiddenmenu
... title Red Hat Enterprise Linux Server (2.6.32-431.17.1.el6.x86_64)
...     kernel /vmlinuz-2.6.32-431.17.1.el6.x86_64 crashkernel=128M rhgb quiet
... title Red Hat Enterprise Linux Server (2.6.32-431.11.2.el6.x86_64)
...     kernel /vmlinuz-2.6.32-431.11.2.el6.x86_64 crashkernel=128M rhgb quiet
... '''.strip()
>>> grub1_config.configs.get('default')
['0']
>>> grub1_config.configs.get('hiddenmenu')
['']
>>> grub1_config['title'][0]['kernel']
['/vmlinuz-2.6.32-431.17.1.el6.x86_64 crashkernel=128M rhgb quiet']
>>> grub1_config.entries[1]['title']
'Red Hat Enterprise Linux Server (2.6.32-431.11.2.el6.x86_64)'
>>> grub1_config.boot_entries[1].name
'Red Hat Enterprise Linux Server (2.6.32-431.11.2.el6.x86_64)'
>>> grub1_config.boot_entries[1].cmdline
'/vmlinuz-2.6.32-431.11.2.el6.x86_64 crashkernel=128M rhgb quiet'
>>> grub1_config.is_kdump_iommu_enabled
False
>>> grub1_config.kernel_initrds['grub_kernels']
['vmlinuz-2.6.32-431.17.1.el6.x86_64', 'vmlinuz-2.6.32-431.11.2.el6.x86_64']
get_current_title()[source]

Get the current default title from the default option in the main configuration. (GRUB v1 only)

Returns

A list of dict contains all settings of the default boot entry:
  • {title: name1, kernel: [val], …},

Return type

list

class insights.parsers.grub_conf.Grub1EFIConfig(*args, **kwargs)[source]

Bases: Grub1Config

Parses grub v1 configuration for EFI-based systems Content of grub-efi.conf is the same as grub.conf

class insights.parsers.grub_conf.Grub2Config(*args, **kwargs)[source]

Bases: GrubConfig

Parser for configuration for GRUB version 2.

Examples

>>> grub2_content = '''
... ### BEGIN /etc/grub.d/00_header ###
... set pager=1
... /
... if [ -s $prefix/grubenv ]; then
...   load_env
... fi
... #[...]
... if [ x"${feature_menuentry_id}" = xy ]; then
...   menuentry_id_option="--id"
... else
...   menuentry_id_option=""
... fi
... #[...]
... ### BEGIN /etc/grub.d/10_linux ###
... menuentry 'Red Hat Enterprise Linux Workstation (3.10.0-327.36.3.el7.x86_64) 7.2 (Maipo)' $menuentry_id_option 'gnulinux-3.10.0-123.13.2.el7.x86_64-advanced-fbff9f50-62c3-484e-bca5-d53f672cda7c' {
...     load_video
...     set gfxpayload=keep
...     insmod gzio
...     insmod part_msdos
...     insmod ext2
...     set root='hd0,msdos1'
...     if [ x$feature_platform_search_hint = xy ]; then
...       search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1 --hint='hd0,msdos1'  1184ab74-77b5-4cfa-81d3-fb87b0457577
...     else
...       search --no-floppy --fs-uuid --set=root 1184ab74-77b5-4cfa-81d3-fb87b0457577
...     fi
...     linux16 /vmlinuz-3.10.0-327.36.3.el7.x86_64 root=/dev/RHEL7CSB/Root ro rd.lvm.lv=RHEL7CSB/Root rd.luks.uuid=luks-96c66446-77fd-4431-9508-f6912bd84194 crashkernel=128M@16M rd.lvm.lv=RHEL7CSB/Swap vconsole.font=latarcyrheb-sun16 rhgb quiet LANG=en_GB.utf8
...     initrd16 /initramfs-3.10.0-327.36.3.el7.x86_64.img
... }
... '''.strip()
>>> grub2_config['configs']
{'set pager': ['1'], '/': ['']}
>>> grub2_config.entries[0]['menuentry']
"'Red Hat Enterprise Linux Workstation (3.10.0-327.36.3.el7.x86_64) 7.2 (Maipo)' $menuentry_id_option 'gnulinux-3.10.0-123.13.2.el7.x86_64-advanced-fbff9f50-62c3-484e-bca5-d53f672cda7c'"
>>> grub2_config['menuentry'][0]['insmod']
['gzio', 'part_msdos', 'ext2']
>>> grub2_config.boot_entries[0].name
"'Red Hat Enterprise Linux Workstation (3.10.0-327.36.3.el7.x86_64) 7.2 (Maipo)' $menuentry_id_option 'gnulinux-3.10.0-123.13.2.el7.x86_64-advanced-fbff9f50-62c3-484e-bca5-d53f672cda7c'"
>>> grub2_config.boot_entries[0].cmdline
'/vmlinuz-3.10.0-327.36.3.el7.x86_64 root=/dev/RHEL7CSB/Root ro rd.lvm.lv=RHEL7CSB/Root rd.luks.uuid=luks-96c66446-77fd-4431-9508-f6912bd84194 crashkernel=128M@16M rd.lvm.lv=RHEL7CSB/Swap vconsole.font=latarcyrheb-sun16 rhgb quiet LANG=en_GB.utf8'
>>> grub2_config.kernel_initrds['grub_kernels'][0]
'vmlinuz-3.10.0-327.36.3.el7.x86_64'
>>> grub2_config.is_kdump_iommu_enabled
False
class insights.parsers.grub_conf.Grub2EFIConfig(*args, **kwargs)[source]

Bases: Grub2Config

Parses grub2 configuration for EFI-based systems

class insights.parsers.grub_conf.GrubConfig(context)[source]

Bases: Parser, dict

Parser for configuration for both GRUB versions 1 and 2.

property boot_entries

Get all boot entries in GRUB configuration.

Returns

A list of insights.parsers.grub_conf.BootEntry

objects for each boot entry in below format: - ‘name’: “Red Hat Enterprise Linux Server” - ‘cmdline’: “kernel /vmlinuz-2.6.32-431.11.2.el6.x86_64 crashkernel=128M rhgb quiet”

Return type

(list)

property is_kdump_iommu_enabled

Does any kernel have ‘intel_iommu=on’ set?

Returns

True when ‘intel_iommu=on’ is set, otherwise returns False

Return type

(bool)

property kernel_initrds

Get the kernel and initrd files referenced in GRUB configuration files

Returns

Returns a dict of the kernel and initrd files referenced

in GRUB configuration files

Return type

(dict)

parse_content(content)[source]

Parse grub configuration file to create a dict with this structure:

{
    "configs": {
        name1: [val1, val2, ...]
        name2: [val],
        ...
    },
    "title": [
        {title: name1, kernel: [val], ...},
        {title: name2, module: [val1, val2], ...},
    ],
    "menuentry": [
        {menuentry: name1, insmod: [val1, val2], ...},
        {menuentry: name2, linux16: [val], ...},
    ],
}