From 53b43e50a12f95743b613e3f767baf522c0fc1c0 Mon Sep 17 00:00:00 2001 From: Xinting Li Date: Mon, 25 Jul 2022 11:35:02 +0800 Subject: [PATCH 01/13] Feat: New grub2_editenv_list parser Signed-off-by: Xinting Li --- insights/parsers/grub2_editenv_list.py | 78 ++++++++++++++++++ .../tests/parsers/test_grub2_editenv_list.py | 82 +++++++++++++++++++ 2 files changed, 160 insertions(+) create mode 100644 insights/parsers/grub2_editenv_list.py create mode 100644 insights/tests/parsers/test_grub2_editenv_list.py diff --git a/insights/parsers/grub2_editenv_list.py b/insights/parsers/grub2_editenv_list.py new file mode 100644 index 0000000000..ad26f13acc --- /dev/null +++ b/insights/parsers/grub2_editenv_list.py @@ -0,0 +1,78 @@ +""" +Grub2EditenvList - command ``grub2-editenv list`` +================================================= + +This parser reads the output of command ``grub2-editenv list``, which list the +current variables in GRUB environment block file. The output without error message +is laid out in a key=value format similar to an ini file except it doesn't have any +laid headers. +""" + +from insights import get_active_lines, parser +from insights.parsers import SkipException +from insights.parsers.grubenv import GrubEnv +from insights.specs import Specs + + +@parser(Specs.grubenv) +class Grub2EditenvList(GrubEnv): + """ + Parses the ``grub2-editenv list`` command and returns a dict + of the grubenv variables. + + Sample output of the command:: + + saved_entry=295e1ba1696e4fad9e062f096f92d147-4.18.0-305.el8.x86_64 + kernelopts=root=/dev/mapper/root_vg-lv_root ro crashkernel=auto resume=/dev/mapper/root_vg-lv_swap rd.lvm.lv=root_vg/lv_root rd.lvm.lv=root_vg/lv_swap console=tty0 console=ttyS0,115200 + boot_success=0 + boot_indeterminate=2 + tuned_params=transparent_hugepages=never + tuned_initrd= + + Attributes: + has_kernelopts (bool): Returns True/False depending on if kernelopts key is in the dict. + kernelopts (bool): Returns the string of kernelopts from the dict. + has_tuned_params (str): Returns True/False depending of if the tuned_params key is in the dict. + tuned_params (str): Returns the string of tuned_params from the dict. + + Examples: + >>> type(grub2_editenv_list) + + >>> grub2_editenv_list.has_kernelopts + True + >>> grub2_editenv_list.kernelopts + 'root=/dev/mapper/root_vg-lv_root ro crashkernel=auto resume=/dev/mapper/root_vg-lv_swap rd.lvm.lv=root_vg/lv_root rd.lvm.lv=root_vg/lv_swap console=tty0 console=ttyS0,115200' + >>> grub2_editenv_list.has_tuned_params + True + >>> grub2_editenv_list.tuned_params + 'transparent_hugepages=never' + >>> grub2_editenv_list['saved_entry'] + '295e1ba1696e4fad9e062f096f92d147-4.18.0-305.el8.x86_64' + """ + + def __init__(self, context): + super().__init__(context) + + def parse_content(self, content): + if not content: + raise SkipException("Empty output.") + + self.error = [] + data = dict() + for line in get_active_lines(content): + if "=" not in line: + self.error.append(line) + continue + + key, value = line.split("=", 1) + + # Some keys can have empty values, so just skip them. + if not value: + continue + + data[key] = value + + if (not data) and (not self.error): + raise SkipException("No parsed data.") + + self.update(data) diff --git a/insights/tests/parsers/test_grub2_editenv_list.py b/insights/tests/parsers/test_grub2_editenv_list.py new file mode 100644 index 0000000000..64a1623d12 --- /dev/null +++ b/insights/tests/parsers/test_grub2_editenv_list.py @@ -0,0 +1,82 @@ +import doctest +from insights.parsers import grub2_editenv_list +from insights.tests.parsers import skip_exception_check +from insights.tests import context_wrap + + +GRUBENV_WITH_TUNED_PARAMS = """ +saved_entry=295e1ba1696e4fad9e062f096f92d147-4.18.0-305.el8.x86_64 +kernelopts=root=/dev/mapper/root_vg-lv_root ro crashkernel=auto resume=/dev/mapper/root_vg-lv_swap rd.lvm.lv=root_vg/lv_root rd.lvm.lv=root_vg/lv_swap console=tty0 console=ttyS0,115200 +boot_success=0 +boot_indeterminate=2 +tuned_params=transparent_hugepages=never +tuned_initrd= +""".strip() # noqa + +GRUBENV_WITHOUT_TUNED_PARAMS = """ +saved_entry=295e1ba1696e4fad9e062f096f92d147-4.18.0-305.el8.x86_64 +kernelopts=root=/dev/mapper/root_vg-lv_root ro crashkernel=auto resume=/dev/mapper/root_vg-lv_swap rd.lvm.lv=root_vg/lv_root rd.lvm.lv=root_vg/lv_swap console=tty0 console=ttyS0,115200 +boot_success=0 +boot_indeterminate=2 +""".strip() # noqa + +GRUBENV_RHEL7 = """ +saved_entry=Red Hat Enterprise Linux Server (3.10.0-1127.el7.x86_64) 7.8 (Maipo) +""".strip() # noqa + +GRUBENV_ERROR = """ +/usr/bin/grub2-editenv: error: invalid environment block. +""" + + +def test_doc_examples(): + env = { + 'grub2_editenv_list': grub2_editenv_list.Grub2EditenvList(context_wrap(GRUBENV_WITH_TUNED_PARAMS)) + } + failed, total = doctest.testmod(grub2_editenv_list, globs=env) + assert failed == 0 + + +def test_with_tuned_params(): + results = grub2_editenv_list.Grub2EditenvList(context_wrap(GRUBENV_WITH_TUNED_PARAMS)) + assert results is not None + assert results.has_kernelopts + assert results.has_tuned_params + assert results.kernelopts == "root=/dev/mapper/root_vg-lv_root ro crashkernel=auto resume=/dev/mapper/root_vg-lv_swap rd.lvm.lv=root_vg/lv_root rd.lvm.lv=root_vg/lv_swap console=tty0 console=ttyS0,115200" # noqa + assert results.tuned_params == "transparent_hugepages=never" + assert results['saved_entry'] == "295e1ba1696e4fad9e062f096f92d147-4.18.0-305.el8.x86_64" + assert results['boot_success'] == "0" + assert results['boot_indeterminate'] == "2" + + +def test_with_error(): + results = grub2_editenv_list.Grub2EditenvList(context_wrap(GRUBENV_ERROR)) + assert results is not None + assert results.error == ["/usr/bin/grub2-editenv: error: invalid environment block."] + + +def test_without_tuned_params(): + results = grub2_editenv_list.Grub2EditenvList(context_wrap(GRUBENV_WITHOUT_TUNED_PARAMS)) + assert results is not None + assert results.has_kernelopts + assert not results.has_tuned_params + assert results.kernelopts == "root=/dev/mapper/root_vg-lv_root ro crashkernel=auto resume=/dev/mapper/root_vg-lv_swap rd.lvm.lv=root_vg/lv_root rd.lvm.lv=root_vg/lv_swap console=tty0 console=ttyS0,115200" # noqa + assert results.tuned_params == "" + assert results['saved_entry'] == "295e1ba1696e4fad9e062f096f92d147-4.18.0-305.el8.x86_64" + assert results['boot_success'] == "0" + assert results['boot_indeterminate'] == "2" + + +def test_r7(): + results = grub2_editenv_list.Grub2EditenvList(context_wrap(GRUBENV_RHEL7)) + assert results is not None + assert not results.has_kernelopts + assert not results.has_tuned_params + assert results.kernelopts == "" + assert results.tuned_params == "" + assert results['saved_entry'] == "Red Hat Enterprise Linux Server (3.10.0-1127.el7.x86_64) 7.8 (Maipo)" + + +def test_skip(): + skip_exception_check(grub2_editenv_list.Grub2EditenvList, output_str="# test") + skip_exception_check(grub2_editenv_list.Grub2EditenvList) From ea90dd00105df71933c640cd45fc69bc108bcd84 Mon Sep 17 00:00:00 2001 From: Xinting Li Date: Mon, 25 Jul 2022 14:28:10 +0800 Subject: [PATCH 02/13] Update default and init Signed-off-by: Xinting Li --- insights/specs/__init__.py | 1 + insights/specs/default.py | 1 + 2 files changed, 2 insertions(+) diff --git a/insights/specs/__init__.py b/insights/specs/__init__.py index 27c7aad286..5a841ee4f4 100644 --- a/insights/specs/__init__.py +++ b/insights/specs/__init__.py @@ -211,6 +211,7 @@ class Specs(SpecSet): grub_efi_conf = RegistryPoint() grub1_config_perms = RegistryPoint() grub2_cfg = RegistryPoint() + grub2_editenv_list = RegistryPoint() grub2_efi_cfg = RegistryPoint() grubby_default_index = RegistryPoint() grubby_default_kernel = RegistryPoint() diff --git a/insights/specs/default.py b/insights/specs/default.py index fe8ae1097a..da38db24d9 100644 --- a/insights/specs/default.py +++ b/insights/specs/default.py @@ -220,6 +220,7 @@ class DefaultSpecs(Specs): grub_efi_conf = simple_file("/boot/efi/EFI/redhat/grub.conf") grub1_config_perms = simple_command("/bin/ls -lH /boot/grub/grub.conf") # RHEL6 grub2_cfg = simple_file("/boot/grub2/grub.cfg") + grub2_editenv_list = simple_command("/usr/bin/grub2-editenv list") grub2_efi_cfg = simple_file("boot/efi/EFI/redhat/grub.cfg") grubby_default_index = simple_command("/usr/sbin/grubby --default-index") # only RHEL7 and updwards grubby_default_kernel = simple_command("/sbin/grubby --default-kernel") From 92b180977acb843d07f467c99b6ebf2ab573aee8 Mon Sep 17 00:00:00 2001 From: Xinting Li Date: Mon, 25 Jul 2022 15:35:46 +0800 Subject: [PATCH 03/13] Update __init__ Signed-off-by: Xinting Li --- insights/parsers/grub2_editenv_list.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/insights/parsers/grub2_editenv_list.py b/insights/parsers/grub2_editenv_list.py index ad26f13acc..8c2cf1eb24 100644 --- a/insights/parsers/grub2_editenv_list.py +++ b/insights/parsers/grub2_editenv_list.py @@ -51,7 +51,7 @@ class Grub2EditenvList(GrubEnv): """ def __init__(self, context): - super().__init__(context) + super(Grub2EditenvList, self).__init__(context) def parse_content(self, content): if not content: From a0ab4adca2a74d61410e27753e17905f2125c956 Mon Sep 17 00:00:00 2001 From: Xinting Li Date: Mon, 25 Jul 2022 15:54:27 +0800 Subject: [PATCH 04/13] Correct docs-test error Signed-off-by: Xinting Li --- insights/parsers/grub2_editenv_list.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/insights/parsers/grub2_editenv_list.py b/insights/parsers/grub2_editenv_list.py index 8c2cf1eb24..5ca37c938a 100644 --- a/insights/parsers/grub2_editenv_list.py +++ b/insights/parsers/grub2_editenv_list.py @@ -29,13 +29,15 @@ class Grub2EditenvList(GrubEnv): tuned_params=transparent_hugepages=never tuned_initrd= - Attributes: + Attributes:: + has_kernelopts (bool): Returns True/False depending on if kernelopts key is in the dict. kernelopts (bool): Returns the string of kernelopts from the dict. has_tuned_params (str): Returns True/False depending of if the tuned_params key is in the dict. tuned_params (str): Returns the string of tuned_params from the dict. Examples: + >>> type(grub2_editenv_list) >>> grub2_editenv_list.has_kernelopts From 1c346de19f9aac29aa43a312c7b761fc91a7b406 Mon Sep 17 00:00:00 2001 From: Xinting Li Date: Mon, 25 Jul 2022 16:03:37 +0800 Subject: [PATCH 05/13] modify test content name Signed-off-by: Xinting Li --- .../tests/parsers/test_grub2_editenv_list.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/insights/tests/parsers/test_grub2_editenv_list.py b/insights/tests/parsers/test_grub2_editenv_list.py index 64a1623d12..fc257ea2c7 100644 --- a/insights/tests/parsers/test_grub2_editenv_list.py +++ b/insights/tests/parsers/test_grub2_editenv_list.py @@ -4,7 +4,7 @@ from insights.tests import context_wrap -GRUBENV_WITH_TUNED_PARAMS = """ +CONTENT_WITH_TUNED_PARAMS = """ saved_entry=295e1ba1696e4fad9e062f096f92d147-4.18.0-305.el8.x86_64 kernelopts=root=/dev/mapper/root_vg-lv_root ro crashkernel=auto resume=/dev/mapper/root_vg-lv_swap rd.lvm.lv=root_vg/lv_root rd.lvm.lv=root_vg/lv_swap console=tty0 console=ttyS0,115200 boot_success=0 @@ -13,32 +13,32 @@ tuned_initrd= """.strip() # noqa -GRUBENV_WITHOUT_TUNED_PARAMS = """ +CONTENT_WITHOUT_TUNED_PARAMS = """ saved_entry=295e1ba1696e4fad9e062f096f92d147-4.18.0-305.el8.x86_64 kernelopts=root=/dev/mapper/root_vg-lv_root ro crashkernel=auto resume=/dev/mapper/root_vg-lv_swap rd.lvm.lv=root_vg/lv_root rd.lvm.lv=root_vg/lv_swap console=tty0 console=ttyS0,115200 boot_success=0 boot_indeterminate=2 """.strip() # noqa -GRUBENV_RHEL7 = """ +CONTENT_RHEL7 = """ saved_entry=Red Hat Enterprise Linux Server (3.10.0-1127.el7.x86_64) 7.8 (Maipo) """.strip() # noqa -GRUBENV_ERROR = """ +CONTENT_ERROR = """ /usr/bin/grub2-editenv: error: invalid environment block. """ def test_doc_examples(): env = { - 'grub2_editenv_list': grub2_editenv_list.Grub2EditenvList(context_wrap(GRUBENV_WITH_TUNED_PARAMS)) + 'grub2_editenv_list': grub2_editenv_list.Grub2EditenvList(context_wrap(CONTENT_WITH_TUNED_PARAMS)) } failed, total = doctest.testmod(grub2_editenv_list, globs=env) assert failed == 0 def test_with_tuned_params(): - results = grub2_editenv_list.Grub2EditenvList(context_wrap(GRUBENV_WITH_TUNED_PARAMS)) + results = grub2_editenv_list.Grub2EditenvList(context_wrap(CONTENT_WITH_TUNED_PARAMS)) assert results is not None assert results.has_kernelopts assert results.has_tuned_params @@ -50,13 +50,13 @@ def test_with_tuned_params(): def test_with_error(): - results = grub2_editenv_list.Grub2EditenvList(context_wrap(GRUBENV_ERROR)) + results = grub2_editenv_list.Grub2EditenvList(context_wrap(CONTENT_ERROR)) assert results is not None assert results.error == ["/usr/bin/grub2-editenv: error: invalid environment block."] def test_without_tuned_params(): - results = grub2_editenv_list.Grub2EditenvList(context_wrap(GRUBENV_WITHOUT_TUNED_PARAMS)) + results = grub2_editenv_list.Grub2EditenvList(context_wrap(CONTENT_WITHOUT_TUNED_PARAMS)) assert results is not None assert results.has_kernelopts assert not results.has_tuned_params @@ -68,7 +68,7 @@ def test_without_tuned_params(): def test_r7(): - results = grub2_editenv_list.Grub2EditenvList(context_wrap(GRUBENV_RHEL7)) + results = grub2_editenv_list.Grub2EditenvList(context_wrap(CONTENT_RHEL7)) assert results is not None assert not results.has_kernelopts assert not results.has_tuned_params From 17c4e960568b4d732ea73256c58eac1c714501b7 Mon Sep 17 00:00:00 2001 From: Xinting Li Date: Mon, 25 Jul 2022 16:35:08 +0800 Subject: [PATCH 06/13] Add doc entry Signed-off-by: Xinting Li --- docs/shared_parsers_catalog/grub2_editenv_list.rst | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 docs/shared_parsers_catalog/grub2_editenv_list.rst diff --git a/docs/shared_parsers_catalog/grub2_editenv_list.rst b/docs/shared_parsers_catalog/grub2_editenv_list.rst new file mode 100644 index 0000000000..ce8a3e7453 --- /dev/null +++ b/docs/shared_parsers_catalog/grub2_editenv_list.rst @@ -0,0 +1,3 @@ +.. automodule:: insights.parsers.grub2_editenv_list + :members: + :show-inheritance: From 09d34b9e63ed40fdd91e7f9643be6665b280ed09 Mon Sep 17 00:00:00 2001 From: Xinting Li Date: Wed, 27 Jul 2022 10:42:18 +0800 Subject: [PATCH 07/13] Moving to grubenv Signed-off-by: Xinting Li --- .../grub2_editenv_list.rst | 3 - insights/parsers/grub2_editenv_list.py | 80 ------------------ insights/parsers/grubenv.py | 64 ++++++++++++++- .../tests/parsers/test_grub2_editenv_list.py | 82 ------------------- insights/tests/parsers/test_grubenv.py | 73 ++++++++++++++++- 5 files changed, 135 insertions(+), 167 deletions(-) delete mode 100644 docs/shared_parsers_catalog/grub2_editenv_list.rst delete mode 100644 insights/parsers/grub2_editenv_list.py delete mode 100644 insights/tests/parsers/test_grub2_editenv_list.py diff --git a/docs/shared_parsers_catalog/grub2_editenv_list.rst b/docs/shared_parsers_catalog/grub2_editenv_list.rst deleted file mode 100644 index ce8a3e7453..0000000000 --- a/docs/shared_parsers_catalog/grub2_editenv_list.rst +++ /dev/null @@ -1,3 +0,0 @@ -.. automodule:: insights.parsers.grub2_editenv_list - :members: - :show-inheritance: diff --git a/insights/parsers/grub2_editenv_list.py b/insights/parsers/grub2_editenv_list.py deleted file mode 100644 index 5ca37c938a..0000000000 --- a/insights/parsers/grub2_editenv_list.py +++ /dev/null @@ -1,80 +0,0 @@ -""" -Grub2EditenvList - command ``grub2-editenv list`` -================================================= - -This parser reads the output of command ``grub2-editenv list``, which list the -current variables in GRUB environment block file. The output without error message -is laid out in a key=value format similar to an ini file except it doesn't have any -laid headers. -""" - -from insights import get_active_lines, parser -from insights.parsers import SkipException -from insights.parsers.grubenv import GrubEnv -from insights.specs import Specs - - -@parser(Specs.grubenv) -class Grub2EditenvList(GrubEnv): - """ - Parses the ``grub2-editenv list`` command and returns a dict - of the grubenv variables. - - Sample output of the command:: - - saved_entry=295e1ba1696e4fad9e062f096f92d147-4.18.0-305.el8.x86_64 - kernelopts=root=/dev/mapper/root_vg-lv_root ro crashkernel=auto resume=/dev/mapper/root_vg-lv_swap rd.lvm.lv=root_vg/lv_root rd.lvm.lv=root_vg/lv_swap console=tty0 console=ttyS0,115200 - boot_success=0 - boot_indeterminate=2 - tuned_params=transparent_hugepages=never - tuned_initrd= - - Attributes:: - - has_kernelopts (bool): Returns True/False depending on if kernelopts key is in the dict. - kernelopts (bool): Returns the string of kernelopts from the dict. - has_tuned_params (str): Returns True/False depending of if the tuned_params key is in the dict. - tuned_params (str): Returns the string of tuned_params from the dict. - - Examples: - - >>> type(grub2_editenv_list) - - >>> grub2_editenv_list.has_kernelopts - True - >>> grub2_editenv_list.kernelopts - 'root=/dev/mapper/root_vg-lv_root ro crashkernel=auto resume=/dev/mapper/root_vg-lv_swap rd.lvm.lv=root_vg/lv_root rd.lvm.lv=root_vg/lv_swap console=tty0 console=ttyS0,115200' - >>> grub2_editenv_list.has_tuned_params - True - >>> grub2_editenv_list.tuned_params - 'transparent_hugepages=never' - >>> grub2_editenv_list['saved_entry'] - '295e1ba1696e4fad9e062f096f92d147-4.18.0-305.el8.x86_64' - """ - - def __init__(self, context): - super(Grub2EditenvList, self).__init__(context) - - def parse_content(self, content): - if not content: - raise SkipException("Empty output.") - - self.error = [] - data = dict() - for line in get_active_lines(content): - if "=" not in line: - self.error.append(line) - continue - - key, value = line.split("=", 1) - - # Some keys can have empty values, so just skip them. - if not value: - continue - - data[key] = value - - if (not data) and (not self.error): - raise SkipException("No parsed data.") - - self.update(data) diff --git a/insights/parsers/grubenv.py b/insights/parsers/grubenv.py index 72152634db..db7b002e31 100644 --- a/insights/parsers/grubenv.py +++ b/insights/parsers/grubenv.py @@ -1,7 +1,6 @@ """ GrubEnv - file ``/boot/grub2/grubenv`` ====================================== - This parser reads the GRUB environment block file. The file is laid out in a key=value format similar to an ini file except it doesn't have any headers. @@ -9,6 +8,11 @@ file is only used in Grub 2, but in RHEL8 with BLS being the default. There were several variables added that are referenced in the ``/boot/loader/entries/*.conf`` files. + +Grub2EditenvList - command ``grub2-editenv list`` +================================================= +This module parses the output of the command ``grub2-editenv list``, which +list the current variables in GRUB environment block file or error message. """ from insights import get_active_lines, parser, Parser from insights.parsers import SkipException @@ -90,3 +94,61 @@ def has_tuned_params(self): @property def tuned_params(self): return self.get("tuned_params", "") + + +@parser(Specs.grubenv) +class Grub2EditenvList(GrubEnv): + """ + Parses the ``grub2-editenv list`` command and returns a dict + of the grubenv variables. The command output without error + message is same as the content in ``/boot/grub2/grubenv``. + + Sample output of the command:: + + saved_entry=295e1ba1696e4fad9e062f096f92d147-4.18.0-305.el8.x86_64 + kernelopts=root=/dev/mapper/root_vg-lv_root ro crashkernel=auto resume=/dev/mapper/root_vg-lv_swap rd.lvm.lv=root_vg/lv_root rd.lvm.lv=root_vg/lv_swap console=tty0 console=ttyS0,115200 + boot_success=0 + boot_indeterminate=2 + tuned_params=transparent_hugepages=never + tuned_initrd= + + Examples: + + >>> type(grub2_editenv_list) + + >>> grub2_editenv_list.has_kernelopts + True + >>> grub2_editenv_list.kernelopts + 'root=/dev/mapper/root_vg-lv_root ro crashkernel=auto resume=/dev/mapper/root_vg-lv_swap rd.lvm.lv=root_vg/lv_root rd.lvm.lv=root_vg/lv_swap console=tty0 console=ttyS0,115200' + >>> grub2_editenv_list.has_tuned_params + True + >>> grub2_editenv_list.tuned_params + 'transparent_hugepages=never' + >>> grub2_editenv_list['saved_entry'] + '295e1ba1696e4fad9e062f096f92d147-4.18.0-305.el8.x86_64' + """ + + def __init__(self, context): + super(Grub2EditenvList, self).__init__(context) + + def parse_content(self, content): + if not content: + raise SkipException("Empty output.") + + self.error = [] + data = dict() + for line in get_active_lines(content): + if "=" not in line: + self.error.append(line) + continue + + key, value = line.split("=", 1) + if not value: + continue + + data[key] = value + + if (not data) and (not self.error): + raise SkipException("No parsed data.") + + self.update(data) diff --git a/insights/tests/parsers/test_grub2_editenv_list.py b/insights/tests/parsers/test_grub2_editenv_list.py deleted file mode 100644 index fc257ea2c7..0000000000 --- a/insights/tests/parsers/test_grub2_editenv_list.py +++ /dev/null @@ -1,82 +0,0 @@ -import doctest -from insights.parsers import grub2_editenv_list -from insights.tests.parsers import skip_exception_check -from insights.tests import context_wrap - - -CONTENT_WITH_TUNED_PARAMS = """ -saved_entry=295e1ba1696e4fad9e062f096f92d147-4.18.0-305.el8.x86_64 -kernelopts=root=/dev/mapper/root_vg-lv_root ro crashkernel=auto resume=/dev/mapper/root_vg-lv_swap rd.lvm.lv=root_vg/lv_root rd.lvm.lv=root_vg/lv_swap console=tty0 console=ttyS0,115200 -boot_success=0 -boot_indeterminate=2 -tuned_params=transparent_hugepages=never -tuned_initrd= -""".strip() # noqa - -CONTENT_WITHOUT_TUNED_PARAMS = """ -saved_entry=295e1ba1696e4fad9e062f096f92d147-4.18.0-305.el8.x86_64 -kernelopts=root=/dev/mapper/root_vg-lv_root ro crashkernel=auto resume=/dev/mapper/root_vg-lv_swap rd.lvm.lv=root_vg/lv_root rd.lvm.lv=root_vg/lv_swap console=tty0 console=ttyS0,115200 -boot_success=0 -boot_indeterminate=2 -""".strip() # noqa - -CONTENT_RHEL7 = """ -saved_entry=Red Hat Enterprise Linux Server (3.10.0-1127.el7.x86_64) 7.8 (Maipo) -""".strip() # noqa - -CONTENT_ERROR = """ -/usr/bin/grub2-editenv: error: invalid environment block. -""" - - -def test_doc_examples(): - env = { - 'grub2_editenv_list': grub2_editenv_list.Grub2EditenvList(context_wrap(CONTENT_WITH_TUNED_PARAMS)) - } - failed, total = doctest.testmod(grub2_editenv_list, globs=env) - assert failed == 0 - - -def test_with_tuned_params(): - results = grub2_editenv_list.Grub2EditenvList(context_wrap(CONTENT_WITH_TUNED_PARAMS)) - assert results is not None - assert results.has_kernelopts - assert results.has_tuned_params - assert results.kernelopts == "root=/dev/mapper/root_vg-lv_root ro crashkernel=auto resume=/dev/mapper/root_vg-lv_swap rd.lvm.lv=root_vg/lv_root rd.lvm.lv=root_vg/lv_swap console=tty0 console=ttyS0,115200" # noqa - assert results.tuned_params == "transparent_hugepages=never" - assert results['saved_entry'] == "295e1ba1696e4fad9e062f096f92d147-4.18.0-305.el8.x86_64" - assert results['boot_success'] == "0" - assert results['boot_indeterminate'] == "2" - - -def test_with_error(): - results = grub2_editenv_list.Grub2EditenvList(context_wrap(CONTENT_ERROR)) - assert results is not None - assert results.error == ["/usr/bin/grub2-editenv: error: invalid environment block."] - - -def test_without_tuned_params(): - results = grub2_editenv_list.Grub2EditenvList(context_wrap(CONTENT_WITHOUT_TUNED_PARAMS)) - assert results is not None - assert results.has_kernelopts - assert not results.has_tuned_params - assert results.kernelopts == "root=/dev/mapper/root_vg-lv_root ro crashkernel=auto resume=/dev/mapper/root_vg-lv_swap rd.lvm.lv=root_vg/lv_root rd.lvm.lv=root_vg/lv_swap console=tty0 console=ttyS0,115200" # noqa - assert results.tuned_params == "" - assert results['saved_entry'] == "295e1ba1696e4fad9e062f096f92d147-4.18.0-305.el8.x86_64" - assert results['boot_success'] == "0" - assert results['boot_indeterminate'] == "2" - - -def test_r7(): - results = grub2_editenv_list.Grub2EditenvList(context_wrap(CONTENT_RHEL7)) - assert results is not None - assert not results.has_kernelopts - assert not results.has_tuned_params - assert results.kernelopts == "" - assert results.tuned_params == "" - assert results['saved_entry'] == "Red Hat Enterprise Linux Server (3.10.0-1127.el7.x86_64) 7.8 (Maipo)" - - -def test_skip(): - skip_exception_check(grub2_editenv_list.Grub2EditenvList, output_str="# test") - skip_exception_check(grub2_editenv_list.Grub2EditenvList) diff --git a/insights/tests/parsers/test_grubenv.py b/insights/tests/parsers/test_grubenv.py index 21a1bef5e1..872ddc8a0e 100644 --- a/insights/tests/parsers/test_grubenv.py +++ b/insights/tests/parsers/test_grubenv.py @@ -32,9 +32,39 @@ """.strip() # noqa +GRUBEDITENVLIST_WITH_TUNED_PARAMS = """ +saved_entry=295e1ba1696e4fad9e062f096f92d147-4.18.0-305.el8.x86_64 +kernelopts=root=/dev/mapper/root_vg-lv_root ro crashkernel=auto resume=/dev/mapper/root_vg-lv_swap rd.lvm.lv=root_vg/lv_root rd.lvm.lv=root_vg/lv_swap console=tty0 console=ttyS0,115200 +boot_success=0 +boot_indeterminate=2 +tuned_params=transparent_hugepages=never +tuned_initrd= +""".strip() + +GRUBEDITENVLIST_WITHOUT_TUNED_PARAMS = """ +saved_entry=295e1ba1696e4fad9e062f096f92d147-4.18.0-305.el8.x86_64 +kernelopts=root=/dev/mapper/root_vg-lv_root ro crashkernel=auto resume=/dev/mapper/root_vg-lv_swap rd.lvm.lv=root_vg/lv_root rd.lvm.lv=root_vg/lv_swap console=tty0 console=ttyS0,115200 +boot_success=0 +boot_indeterminate=2 +""".strip() + +GRUBEDITENVLIST_RHEL7 = """ +saved_entry=Red Hat Enterprise Linux Server (3.10.0-1127.el7.x86_64) 7.8 (Maipo) +""".strip() + +GRUBEDITENVLIST_ERROR = """ +grub2-editenv: error: invalid environment block. +""".strip() + +GRUBEDITENVLIST_EMPTY = """ +""" + + + def test_doc_examples(): env = { - 'grubenv': grubenv.GrubEnv(context_wrap(GRUBENV_WITH_TUNED_PARAMS)) + 'grubenv': grubenv.GrubEnv(context_wrap(GRUBENV_WITH_TUNED_PARAMS)), + 'grub2_editenv_list': grubenv.Grub2EditenvList(context_wrap(GRUBEDITENVLIST_WITH_TUNED_PARAMS)) } failed, total = doctest.testmod(grubenv, globs=env) assert failed == 0 @@ -77,3 +107,44 @@ def test_r7(): def test_skip(): skip_exception_check(grubenv.GrubEnv, output_str="# test") skip_exception_check(grubenv.GrubEnv) + + +def test_grub2_editenv_list_with_tuned_params(): + results = grubenv.Grub2EditenvList(context_wrap(GRUBEDITENVLIST_WITH_TUNED_PARAMS)) + assert results is not None + assert results.has_kernelopts + assert results.has_tuned_params + assert results.kernelopts == "root=/dev/mapper/root_vg-lv_root ro crashkernel=auto resume=/dev/mapper/root_vg-lv_swap rd.lvm.lv=root_vg/lv_root rd.lvm.lv=root_vg/lv_swap console=tty0 console=ttyS0,115200" # noqa + assert results.tuned_params == "transparent_hugepages=never" + assert results['boot_success'] == "0" + + +def test_grub2_editenv_list_with_error(): + results = grubenv.Grub2EditenvList(context_wrap(GRUBEDITENVLIST_ERROR)) + assert results is not None + assert results.error == ["grub2-editenv: error: invalid environment block."] + + +def test_grub2_editenv_list_without_tuned_params(): + results = grubenv.Grub2EditenvList(context_wrap(GRUBEDITENVLIST_WITHOUT_TUNED_PARAMS)) + assert results is not None + assert results.has_kernelopts + assert not results.has_tuned_params + assert results.kernelopts == "root=/dev/mapper/root_vg-lv_root ro crashkernel=auto resume=/dev/mapper/root_vg-lv_swap rd.lvm.lv=root_vg/lv_root rd.lvm.lv=root_vg/lv_swap console=tty0 console=ttyS0,115200" # noqa + assert results.tuned_params == "" + assert results['boot_indeterminate'] == "2" + + +def test_grub2_editenv_list_rhel7(): + results = grubenv.Grub2EditenvList(context_wrap(GRUBEDITENVLIST_RHEL7)) + assert results is not None + assert not results.has_kernelopts + assert not results.has_tuned_params + assert results.kernelopts == "" + assert results.tuned_params == "" + assert results['saved_entry'] == "Red Hat Enterprise Linux Server (3.10.0-1127.el7.x86_64) 7.8 (Maipo)" + + +def test_grub2_editenv_list_skip(): + skip_exception_check(grubenv.Grub2EditenvList, output_str="# test") + skip_exception_check(grubenv.Grub2EditenvList) From 2746655f49a70701988dc58472dea727c8dc1fc0 Mon Sep 17 00:00:00 2001 From: Xinting Li Date: Wed, 27 Jul 2022 10:50:44 +0800 Subject: [PATCH 08/13] Fix flake8 error Signed-off-by: Xinting Li --- insights/tests/parsers/test_grubenv.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/insights/tests/parsers/test_grubenv.py b/insights/tests/parsers/test_grubenv.py index 872ddc8a0e..c2759deb69 100644 --- a/insights/tests/parsers/test_grubenv.py +++ b/insights/tests/parsers/test_grubenv.py @@ -34,7 +34,7 @@ GRUBEDITENVLIST_WITH_TUNED_PARAMS = """ saved_entry=295e1ba1696e4fad9e062f096f92d147-4.18.0-305.el8.x86_64 -kernelopts=root=/dev/mapper/root_vg-lv_root ro crashkernel=auto resume=/dev/mapper/root_vg-lv_swap rd.lvm.lv=root_vg/lv_root rd.lvm.lv=root_vg/lv_swap console=tty0 console=ttyS0,115200 +kernelopts=root=/dev/mapper/root_vg-lv_root ro crashkernel=auto resume=/dev/mapper/root_vg-lv_swap rd.lvm.lv=root_vg/lv_root rd.lvm.lv=root_vg/lv_swap console=tty0 console=ttyS0,115200 boot_success=0 boot_indeterminate=2 tuned_params=transparent_hugepages=never @@ -43,7 +43,7 @@ GRUBEDITENVLIST_WITHOUT_TUNED_PARAMS = """ saved_entry=295e1ba1696e4fad9e062f096f92d147-4.18.0-305.el8.x86_64 -kernelopts=root=/dev/mapper/root_vg-lv_root ro crashkernel=auto resume=/dev/mapper/root_vg-lv_swap rd.lvm.lv=root_vg/lv_root rd.lvm.lv=root_vg/lv_swap console=tty0 console=ttyS0,115200 +kernelopts=root=/dev/mapper/root_vg-lv_root ro crashkernel=auto resume=/dev/mapper/root_vg-lv_swap rd.lvm.lv=root_vg/lv_root rd.lvm.lv=root_vg/lv_swap console=tty0 console=ttyS0,115200 boot_success=0 boot_indeterminate=2 """.strip() @@ -60,7 +60,6 @@ """ - def test_doc_examples(): env = { 'grubenv': grubenv.GrubEnv(context_wrap(GRUBENV_WITH_TUNED_PARAMS)), @@ -114,7 +113,7 @@ def test_grub2_editenv_list_with_tuned_params(): assert results is not None assert results.has_kernelopts assert results.has_tuned_params - assert results.kernelopts == "root=/dev/mapper/root_vg-lv_root ro crashkernel=auto resume=/dev/mapper/root_vg-lv_swap rd.lvm.lv=root_vg/lv_root rd.lvm.lv=root_vg/lv_swap console=tty0 console=ttyS0,115200" # noqa + assert results.kernelopts == "root=/dev/mapper/root_vg-lv_root ro crashkernel=auto resume=/dev/mapper/root_vg-lv_swap rd.lvm.lv=root_vg/lv_root rd.lvm.lv=root_vg/lv_swap console=tty0 console=ttyS0,115200" assert results.tuned_params == "transparent_hugepages=never" assert results['boot_success'] == "0" @@ -130,7 +129,7 @@ def test_grub2_editenv_list_without_tuned_params(): assert results is not None assert results.has_kernelopts assert not results.has_tuned_params - assert results.kernelopts == "root=/dev/mapper/root_vg-lv_root ro crashkernel=auto resume=/dev/mapper/root_vg-lv_swap rd.lvm.lv=root_vg/lv_root rd.lvm.lv=root_vg/lv_swap console=tty0 console=ttyS0,115200" # noqa + assert results.kernelopts == "root=/dev/mapper/root_vg-lv_root ro crashkernel=auto resume=/dev/mapper/root_vg-lv_swap rd.lvm.lv=root_vg/lv_root rd.lvm.lv=root_vg/lv_swap console=tty0 console=ttyS0,115200" assert results.tuned_params == "" assert results['boot_indeterminate'] == "2" From 22aa8c830cc768ec84f605881e90d9a58f4db24c Mon Sep 17 00:00:00 2001 From: Xinting Li Date: Wed, 27 Jul 2022 15:27:09 +0800 Subject: [PATCH 09/13] Add base class Signed-off-by: Xinting Li --- insights/parsers/grubenv.py | 131 +++++++++++++++++------------------- 1 file changed, 62 insertions(+), 69 deletions(-) diff --git a/insights/parsers/grubenv.py b/insights/parsers/grubenv.py index db7b002e31..c6abfddd52 100644 --- a/insights/parsers/grubenv.py +++ b/insights/parsers/grubenv.py @@ -1,80 +1,60 @@ """ -GrubEnv - file ``/boot/grub2/grubenv`` -====================================== -This parser reads the GRUB environment block file. The file is laid out in a -key=value format similar to an ini file except it doesn't have any headers. +GRUB environment block +====================== +This module provides processing for the GRUB environment block. +Parsers included are: + -The parser stores the key/value pairs in itself which inherits a dict. This -file is only used in Grub 2, but in RHEL8 with BLS being the default. There -were several variables added that are referenced in the -``/boot/loader/entries/*.conf`` files. +GrubEnv - file ``/boot/grub2/grubenv`` +-------------------------------------- Grub2EditenvList - command ``grub2-editenv list`` -================================================= -This module parses the output of the command ``grub2-editenv list``, which -list the current variables in GRUB environment block file or error message. +------------------------------------------------- """ from insights import get_active_lines, parser, Parser from insights.parsers import SkipException from insights.specs import Specs -@parser(Specs.grubenv) -class GrubEnv(Parser, dict): +class GrubenvBase(Parser, dict): """ - Parses the /boot/grub2/grubenv file and returns a dict - of the grubenv variables. - - Sample output of the file:: + This module parses the content of GRUB environment block file - + ``/boot/grub2/grubenv``. The file is laid out in a key=value format similar + to an ini file except it doesn't have any headers. - saved_entry=295e1ba1696e4fad9e062f096f92d147-4.18.0-305.el8.x86_64 - kernelopts=root=/dev/mapper/root_vg-lv_root ro crashkernel=auto resume=/dev/mapper/root_vg-lv_swap rd.lvm.lv=root_vg/lv_root rd.lvm.lv=root_vg/lv_swap console=tty0 console=ttyS0,115200 - boot_success=0 - boot_indeterminate=2 - tuned_params=transparent_hugepages=never - tuned_initrd= + The module stores the key/value pairs in itself which inherits a dict. This + file is only used in Grub 2, but in RHEL8 with BLS being the default. There + were several variables added that are referenced in the + ``/boot/loader/entries/*.conf`` files. Attributes: has_kernelopts (bool): Returns True/False depending on if kernelopts key is in the dict. kernelopts (bool): Returns the string of kernelopts from the dict. has_tuned_params (str): Returns True/False depending of if the tuned_params key is in the dict. tuned_params (str): Returns the string of tuned_params from the dict. - - Examples: - >>> type(grubenv) - - >>> grubenv.has_kernelopts - True - >>> grubenv.kernelopts - 'root=/dev/mapper/root_vg-lv_root ro crashkernel=auto resume=/dev/mapper/root_vg-lv_swap rd.lvm.lv=root_vg/lv_root rd.lvm.lv=root_vg/lv_swap console=tty0 console=ttyS0,115200' - >>> grubenv.has_tuned_params - True - >>> grubenv.tuned_params - 'transparent_hugepages=never' - >>> grubenv['saved_entry'] - '295e1ba1696e4fad9e062f096f92d147-4.18.0-305.el8.x86_64' """ + def __init__(self, context): - super(GrubEnv, self).__init__(context) + super(GrubenvBase, self).__init__(context) def parse_content(self, content): if not content: raise SkipException("Empty output.") + self._error = [] data = dict() for line in get_active_lines(content): if "=" not in line: + self._error.append(line) continue key, value = line.split("=", 1) - - # Some keys can have empty values, so just skip them. if not value: continue data[key] = value - if not data: + if (not data) and (not self._error): raise SkipException("No parsed data.") self.update(data) @@ -97,11 +77,45 @@ def tuned_params(self): @parser(Specs.grubenv) -class Grub2EditenvList(GrubEnv): +class GrubEnv(GrubenvBase): + """ + Parses the ``/boot/grub2/grubenv`` file and returns a dict of the grubenv variables. + + Sample output of the file:: + + saved_entry=295e1ba1696e4fad9e062f096f92d147-4.18.0-305.el8.x86_64 + kernelopts=root=/dev/mapper/root_vg-lv_root ro crashkernel=auto resume=/dev/mapper/root_vg-lv_swap rd.lvm.lv=root_vg/lv_root rd.lvm.lv=root_vg/lv_swap console=tty0 console=ttyS0,115200 + boot_success=0 + boot_indeterminate=2 + tuned_params=transparent_hugepages=never + tuned_initrd= + + Examples: + >>> type(grubenv) + + >>> grubenv.has_kernelopts + True + >>> grubenv.kernelopts + 'root=/dev/mapper/root_vg-lv_root ro crashkernel=auto resume=/dev/mapper/root_vg-lv_swap rd.lvm.lv=root_vg/lv_root rd.lvm.lv=root_vg/lv_swap console=tty0 console=ttyS0,115200' + >>> grubenv.has_tuned_params + True + >>> grubenv.tuned_params + 'transparent_hugepages=never' + >>> grubenv['saved_entry'] + '295e1ba1696e4fad9e062f096f92d147-4.18.0-305.el8.x86_64' + """ + pass + + +@parser(Specs.grubenv) +class Grub2EditenvList(GrubenvBase): """ - Parses the ``grub2-editenv list`` command and returns a dict - of the grubenv variables. The command output without error - message is same as the content in ``/boot/grub2/grubenv``. + This parser parses the output of the command ``grub2-editenv list``, which + list the current variables in GRUB environment block file or error message. + + The parser processes the ``grub2-editenv list`` command and returns a dict + of the grubenv variables. The command output without error message is same + as the content in ``/boot/grub2/grubenv``. Sample output of the command:: @@ -128,27 +142,6 @@ class Grub2EditenvList(GrubEnv): '295e1ba1696e4fad9e062f096f92d147-4.18.0-305.el8.x86_64' """ - def __init__(self, context): - super(Grub2EditenvList, self).__init__(context) - - def parse_content(self, content): - if not content: - raise SkipException("Empty output.") - - self.error = [] - data = dict() - for line in get_active_lines(content): - if "=" not in line: - self.error.append(line) - continue - - key, value = line.split("=", 1) - if not value: - continue - - data[key] = value - - if (not data) and (not self.error): - raise SkipException("No parsed data.") - - self.update(data) + @property + def error(self): + return self._error From 1fcea5f2e04d70dd99577a113ef5b92153d5d70b Mon Sep 17 00:00:00 2001 From: Xinting Li Date: Thu, 28 Jul 2022 11:43:08 +0800 Subject: [PATCH 10/13] modify self._error to self._errors Signed-off-by: Xinting Li --- insights/parsers/grubenv.py | 13 ++++++++----- insights/tests/parsers/test_grubenv.py | 2 +- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/insights/parsers/grubenv.py b/insights/parsers/grubenv.py index c6abfddd52..e6e4bfa43e 100644 --- a/insights/parsers/grubenv.py +++ b/insights/parsers/grubenv.py @@ -41,11 +41,11 @@ def parse_content(self, content): if not content: raise SkipException("Empty output.") - self._error = [] + self._errors = [] data = dict() for line in get_active_lines(content): if "=" not in line: - self._error.append(line) + self._errors.append(line) continue key, value = line.split("=", 1) @@ -54,7 +54,7 @@ def parse_content(self, content): data[key] = value - if (not data) and (not self._error): + if (not data) and (not self._errors): raise SkipException("No parsed data.") self.update(data) @@ -142,6 +142,9 @@ class Grub2EditenvList(GrubenvBase): '295e1ba1696e4fad9e062f096f92d147-4.18.0-305.el8.x86_64' """ + # For 'GrubEnv' parser, it processes the content in '/boot/grub2/grubenv' file. + # The file saves environment variables for boot time, and will never contain error messages. + # The errors attribute only needed for Grub2EditenvList parser @property - def error(self): - return self._error + def errors(self): + return self._errors diff --git a/insights/tests/parsers/test_grubenv.py b/insights/tests/parsers/test_grubenv.py index c2759deb69..3873bfc967 100644 --- a/insights/tests/parsers/test_grubenv.py +++ b/insights/tests/parsers/test_grubenv.py @@ -121,7 +121,7 @@ def test_grub2_editenv_list_with_tuned_params(): def test_grub2_editenv_list_with_error(): results = grubenv.Grub2EditenvList(context_wrap(GRUBEDITENVLIST_ERROR)) assert results is not None - assert results.error == ["grub2-editenv: error: invalid environment block."] + assert results.errors == ["grub2-editenv: error: invalid environment block."] def test_grub2_editenv_list_without_tuned_params(): From bb8383fa09e86aa401cd12c2e68677ea360fa414 Mon Sep 17 00:00:00 2001 From: Xinting Li Date: Tue, 16 Aug 2022 17:22:43 +0800 Subject: [PATCH 11/13] Replace GrubEnv from handle ``/boot/grub2/grubenv`` file to ``grub2-editenv list`` command output Signed-off-by: Xinting Li --- insights/parsers/grubenv.py | 134 +++++++++---------------- insights/specs/__init__.py | 1 - insights/specs/default.py | 3 +- insights/specs/insights_archive.py | 1 + insights/specs/sos_archive.py | 1 + insights/tests/parsers/test_grubenv.py | 66 +----------- 6 files changed, 55 insertions(+), 151 deletions(-) diff --git a/insights/parsers/grubenv.py b/insights/parsers/grubenv.py index e6e4bfa43e..4435ddf4f8 100644 --- a/insights/parsers/grubenv.py +++ b/insights/parsers/grubenv.py @@ -5,37 +5,71 @@ Parsers included are: -GrubEnv - file ``/boot/grub2/grubenv`` --------------------------------------- - -Grub2EditenvList - command ``grub2-editenv list`` -------------------------------------------------- +GrubEnv - command ``grub2-editenv list`` +---------------------------------------- """ from insights import get_active_lines, parser, Parser from insights.parsers import SkipException from insights.specs import Specs -class GrubenvBase(Parser, dict): +@parser(Specs.grubenv) +class GrubEnv(Parser, dict): """ - This module parses the content of GRUB environment block file - - ``/boot/grub2/grubenv``. The file is laid out in a key=value format similar - to an ini file except it doesn't have any headers. + This parser parses the output of the command ``grub2-editenv list``, which + list the current variables in GRUB environment block file or error message. + The module stores the key/value pairs in itself which inherits a dict. + + This parser used to handle the contents of the ``/boot/grub2/grubenv`` file + before. Since the command output without error messages is the same as the + content in ``/boot/grub2/grubenv``, and the ``grub2-editenv list`` command + output will show the error messages if the file is corrupt or has serious + errors in content, the current parser processes the ``grub2-editenv list`` + command and returns a dict of the grubenv variables. - The module stores the key/value pairs in itself which inherits a dict. This - file is only used in Grub 2, but in RHEL8 with BLS being the default. There - were several variables added that are referenced in the - ``/boot/loader/entries/*.conf`` files. + Cause the sosreports only collect the grubenv file and don't collect the + ``grub2-editenv list`` command, the GrubEnv parser only handles the + ``/boot/grub2/grubenv`` content for sos_archive. + + ``/boot/grub2/grubenv`` file is only used in Grub 2, but in RHEL8 with BLS + being the default. There were several variables added that are referenced in + the ``/boot/loader/entries/*.conf`` files. + + Attributes:: - Attributes: has_kernelopts (bool): Returns True/False depending on if kernelopts key is in the dict. kernelopts (bool): Returns the string of kernelopts from the dict. has_tuned_params (str): Returns True/False depending of if the tuned_params key is in the dict. tuned_params (str): Returns the string of tuned_params from the dict. + errors (str): Returns the string of error message. + + Sample output of the command:: + + saved_entry=295e1ba1696e4fad9e062f096f92d147-4.18.0-305.el8.x86_64 + kernelopts=root=/dev/mapper/root_vg-lv_root ro crashkernel=auto resume=/dev/mapper/root_vg-lv_swap rd.lvm.lv=root_vg/lv_root rd.lvm.lv=root_vg/lv_swap console=tty0 console=ttyS0,115200 + boot_success=0 + boot_indeterminate=2 + tuned_params=transparent_hugepages=never + tuned_initrd= + + Examples:: + + >>> type(grubenv) + + >>> grubenv.has_kernelopts + True + >>> grubenv.kernelopts + 'root=/dev/mapper/root_vg-lv_root ro crashkernel=auto resume=/dev/mapper/root_vg-lv_swap rd.lvm.lv=root_vg/lv_root rd.lvm.lv=root_vg/lv_swap console=tty0 console=ttyS0,115200' + >>> grubenv.has_tuned_params + True + >>> grubenv.tuned_params + 'transparent_hugepages=never' + >>> grubenv['saved_entry'] + '295e1ba1696e4fad9e062f096f92d147-4.18.0-305.el8.x86_64' """ def __init__(self, context): - super(GrubenvBase, self).__init__(context) + super(GrubEnv, self).__init__(context) def parse_content(self, content): if not content: @@ -75,76 +109,6 @@ def has_tuned_params(self): def tuned_params(self): return self.get("tuned_params", "") - -@parser(Specs.grubenv) -class GrubEnv(GrubenvBase): - """ - Parses the ``/boot/grub2/grubenv`` file and returns a dict of the grubenv variables. - - Sample output of the file:: - - saved_entry=295e1ba1696e4fad9e062f096f92d147-4.18.0-305.el8.x86_64 - kernelopts=root=/dev/mapper/root_vg-lv_root ro crashkernel=auto resume=/dev/mapper/root_vg-lv_swap rd.lvm.lv=root_vg/lv_root rd.lvm.lv=root_vg/lv_swap console=tty0 console=ttyS0,115200 - boot_success=0 - boot_indeterminate=2 - tuned_params=transparent_hugepages=never - tuned_initrd= - - Examples: - >>> type(grubenv) - - >>> grubenv.has_kernelopts - True - >>> grubenv.kernelopts - 'root=/dev/mapper/root_vg-lv_root ro crashkernel=auto resume=/dev/mapper/root_vg-lv_swap rd.lvm.lv=root_vg/lv_root rd.lvm.lv=root_vg/lv_swap console=tty0 console=ttyS0,115200' - >>> grubenv.has_tuned_params - True - >>> grubenv.tuned_params - 'transparent_hugepages=never' - >>> grubenv['saved_entry'] - '295e1ba1696e4fad9e062f096f92d147-4.18.0-305.el8.x86_64' - """ - pass - - -@parser(Specs.grubenv) -class Grub2EditenvList(GrubenvBase): - """ - This parser parses the output of the command ``grub2-editenv list``, which - list the current variables in GRUB environment block file or error message. - - The parser processes the ``grub2-editenv list`` command and returns a dict - of the grubenv variables. The command output without error message is same - as the content in ``/boot/grub2/grubenv``. - - Sample output of the command:: - - saved_entry=295e1ba1696e4fad9e062f096f92d147-4.18.0-305.el8.x86_64 - kernelopts=root=/dev/mapper/root_vg-lv_root ro crashkernel=auto resume=/dev/mapper/root_vg-lv_swap rd.lvm.lv=root_vg/lv_root rd.lvm.lv=root_vg/lv_swap console=tty0 console=ttyS0,115200 - boot_success=0 - boot_indeterminate=2 - tuned_params=transparent_hugepages=never - tuned_initrd= - - Examples: - - >>> type(grub2_editenv_list) - - >>> grub2_editenv_list.has_kernelopts - True - >>> grub2_editenv_list.kernelopts - 'root=/dev/mapper/root_vg-lv_root ro crashkernel=auto resume=/dev/mapper/root_vg-lv_swap rd.lvm.lv=root_vg/lv_root rd.lvm.lv=root_vg/lv_swap console=tty0 console=ttyS0,115200' - >>> grub2_editenv_list.has_tuned_params - True - >>> grub2_editenv_list.tuned_params - 'transparent_hugepages=never' - >>> grub2_editenv_list['saved_entry'] - '295e1ba1696e4fad9e062f096f92d147-4.18.0-305.el8.x86_64' - """ - - # For 'GrubEnv' parser, it processes the content in '/boot/grub2/grubenv' file. - # The file saves environment variables for boot time, and will never contain error messages. - # The errors attribute only needed for Grub2EditenvList parser @property def errors(self): return self._errors diff --git a/insights/specs/__init__.py b/insights/specs/__init__.py index 5a841ee4f4..27c7aad286 100644 --- a/insights/specs/__init__.py +++ b/insights/specs/__init__.py @@ -211,7 +211,6 @@ class Specs(SpecSet): grub_efi_conf = RegistryPoint() grub1_config_perms = RegistryPoint() grub2_cfg = RegistryPoint() - grub2_editenv_list = RegistryPoint() grub2_efi_cfg = RegistryPoint() grubby_default_index = RegistryPoint() grubby_default_kernel = RegistryPoint() diff --git a/insights/specs/default.py b/insights/specs/default.py index da38db24d9..7ab2de12ed 100644 --- a/insights/specs/default.py +++ b/insights/specs/default.py @@ -214,13 +214,12 @@ class DefaultSpecs(Specs): gcp_license_codes = simple_command("/usr/bin/curl -s -H 'Metadata-Flavor: Google' http://metadata.google.internal/computeMetadata/v1/instance/licenses/?recursive=True --connect-timeout 5", deps=[IsGCP]) greenboot_status = simple_command("/usr/libexec/greenboot/greenboot-status") group_info = command_with_args("/usr/bin/getent group %s", user_group.group_filters) - grubenv = first_file(["/boot/grub2/grubenv", "/boot/efi/EFI/redhat/grubenv"]) + grubenv = simple_command("/usr/bin/grub2-editenv list") grub_conf = simple_file("/boot/grub/grub.conf") grub_config_perms = simple_command("/bin/ls -lH /boot/grub2/grub.cfg") # only RHEL7 and updwards grub_efi_conf = simple_file("/boot/efi/EFI/redhat/grub.conf") grub1_config_perms = simple_command("/bin/ls -lH /boot/grub/grub.conf") # RHEL6 grub2_cfg = simple_file("/boot/grub2/grub.cfg") - grub2_editenv_list = simple_command("/usr/bin/grub2-editenv list") grub2_efi_cfg = simple_file("boot/efi/EFI/redhat/grub.cfg") grubby_default_index = simple_command("/usr/sbin/grubby --default-index") # only RHEL7 and updwards grubby_default_kernel = simple_command("/sbin/grubby --default-kernel") diff --git a/insights/specs/insights_archive.py b/insights/specs/insights_archive.py index e62891e6e0..df4433907c 100644 --- a/insights/specs/insights_archive.py +++ b/insights/specs/insights_archive.py @@ -80,6 +80,7 @@ class InsightsArchiveSpecs(Specs): getconf_page_size = simple_file("insights_commands/getconf_PAGE_SIZE") getenforce = simple_file("insights_commands/getenforce") getsebool = simple_file("insights_commands/getsebool_-a") + grubenv = first_file(["/boot/grub2/grubenv", "/boot/efi/EFI/redhat/grubenv"]) grub1_config_perms = first_file(["insights_commands/ls_-lH_.boot.grub.grub.conf", "insights_commands/ls_-l_.boot.grub.grub.conf"]) grub_config_perms = first_file(["insights_commands/ls_-lH_.boot.grub2.grub.cfg", "insights_commands/ls_-l_.boot.grub2.grub.cfg"]) grubby_default_index = simple_file("insights_commands/grubby_--default-index") diff --git a/insights/specs/sos_archive.py b/insights/specs/sos_archive.py index 015f3ed060..a99427619a 100644 --- a/insights/specs/sos_archive.py +++ b/insights/specs/sos_archive.py @@ -102,6 +102,7 @@ class SosSpecs(Specs): gluster_v_info = simple_file("sos_commands/gluster/gluster_volume_info") gluster_v_status = simple_file("sos_commands/gluster/gluster_volume_status") gluster_peer_status = simple_file("sos_commands/gluster/gluster_peer_status") + grubenv = first_file(["/boot/grub2/grubenv", "/boot/efi/EFI/redhat/grubenv"]) heat_engine_log = first_file(["/var/log/containers/heat/heat-engine.log", "/var/log/heat/heat-engine.log"]) hostname = first_file(["sos_commands/general/hostname_-f", "sos_commands/host/hostname_-f"]) hostname_default = first_file(["sos_commands/general/hostname", "sos_commands/host/hostname", "/etc/hostname", "hostname"]) diff --git a/insights/tests/parsers/test_grubenv.py b/insights/tests/parsers/test_grubenv.py index 3873bfc967..6156b9ec5e 100644 --- a/insights/tests/parsers/test_grubenv.py +++ b/insights/tests/parsers/test_grubenv.py @@ -31,39 +31,14 @@ ###################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################### """.strip() # noqa - -GRUBEDITENVLIST_WITH_TUNED_PARAMS = """ -saved_entry=295e1ba1696e4fad9e062f096f92d147-4.18.0-305.el8.x86_64 -kernelopts=root=/dev/mapper/root_vg-lv_root ro crashkernel=auto resume=/dev/mapper/root_vg-lv_swap rd.lvm.lv=root_vg/lv_root rd.lvm.lv=root_vg/lv_swap console=tty0 console=ttyS0,115200 -boot_success=0 -boot_indeterminate=2 -tuned_params=transparent_hugepages=never -tuned_initrd= -""".strip() - -GRUBEDITENVLIST_WITHOUT_TUNED_PARAMS = """ -saved_entry=295e1ba1696e4fad9e062f096f92d147-4.18.0-305.el8.x86_64 -kernelopts=root=/dev/mapper/root_vg-lv_root ro crashkernel=auto resume=/dev/mapper/root_vg-lv_swap rd.lvm.lv=root_vg/lv_root rd.lvm.lv=root_vg/lv_swap console=tty0 console=ttyS0,115200 -boot_success=0 -boot_indeterminate=2 -""".strip() - -GRUBEDITENVLIST_RHEL7 = """ -saved_entry=Red Hat Enterprise Linux Server (3.10.0-1127.el7.x86_64) 7.8 (Maipo) -""".strip() - GRUBEDITENVLIST_ERROR = """ grub2-editenv: error: invalid environment block. """.strip() -GRUBEDITENVLIST_EMPTY = """ -""" - def test_doc_examples(): env = { - 'grubenv': grubenv.GrubEnv(context_wrap(GRUBENV_WITH_TUNED_PARAMS)), - 'grub2_editenv_list': grubenv.Grub2EditenvList(context_wrap(GRUBEDITENVLIST_WITH_TUNED_PARAMS)) + 'grubenv': grubenv.GrubEnv(context_wrap(GRUBENV_WITH_TUNED_PARAMS)) } failed, total = doctest.testmod(grubenv, globs=env) assert failed == 0 @@ -108,42 +83,7 @@ def test_skip(): skip_exception_check(grubenv.GrubEnv) -def test_grub2_editenv_list_with_tuned_params(): - results = grubenv.Grub2EditenvList(context_wrap(GRUBEDITENVLIST_WITH_TUNED_PARAMS)) - assert results is not None - assert results.has_kernelopts - assert results.has_tuned_params - assert results.kernelopts == "root=/dev/mapper/root_vg-lv_root ro crashkernel=auto resume=/dev/mapper/root_vg-lv_swap rd.lvm.lv=root_vg/lv_root rd.lvm.lv=root_vg/lv_swap console=tty0 console=ttyS0,115200" - assert results.tuned_params == "transparent_hugepages=never" - assert results['boot_success'] == "0" - - -def test_grub2_editenv_list_with_error(): - results = grubenv.Grub2EditenvList(context_wrap(GRUBEDITENVLIST_ERROR)) +def test_error(): + results = grubenv.GrubEnv(context_wrap(GRUBEDITENVLIST_ERROR)) assert results is not None assert results.errors == ["grub2-editenv: error: invalid environment block."] - - -def test_grub2_editenv_list_without_tuned_params(): - results = grubenv.Grub2EditenvList(context_wrap(GRUBEDITENVLIST_WITHOUT_TUNED_PARAMS)) - assert results is not None - assert results.has_kernelopts - assert not results.has_tuned_params - assert results.kernelopts == "root=/dev/mapper/root_vg-lv_root ro crashkernel=auto resume=/dev/mapper/root_vg-lv_swap rd.lvm.lv=root_vg/lv_root rd.lvm.lv=root_vg/lv_swap console=tty0 console=ttyS0,115200" - assert results.tuned_params == "" - assert results['boot_indeterminate'] == "2" - - -def test_grub2_editenv_list_rhel7(): - results = grubenv.Grub2EditenvList(context_wrap(GRUBEDITENVLIST_RHEL7)) - assert results is not None - assert not results.has_kernelopts - assert not results.has_tuned_params - assert results.kernelopts == "" - assert results.tuned_params == "" - assert results['saved_entry'] == "Red Hat Enterprise Linux Server (3.10.0-1127.el7.x86_64) 7.8 (Maipo)" - - -def test_grub2_editenv_list_skip(): - skip_exception_check(grubenv.Grub2EditenvList, output_str="# test") - skip_exception_check(grubenv.Grub2EditenvList) From 34c4fbd06d8da549acc30cdf9d5dba5c3984af43 Mon Sep 17 00:00:00 2001 From: Xinting Li Date: Thu, 18 Aug 2022 13:44:16 +0800 Subject: [PATCH 12/13] Update insights_archive Signed-off-by: Xinting Li --- insights/parsers/grubenv.py | 16 ++++++---------- insights/specs/insights_archive.py | 2 +- insights/tests/parsers/test_grubenv.py | 6 ------ 3 files changed, 7 insertions(+), 17 deletions(-) diff --git a/insights/parsers/grubenv.py b/insights/parsers/grubenv.py index 4435ddf4f8..dd3a22928c 100644 --- a/insights/parsers/grubenv.py +++ b/insights/parsers/grubenv.py @@ -35,14 +35,6 @@ class GrubEnv(Parser, dict): being the default. There were several variables added that are referenced in the ``/boot/loader/entries/*.conf`` files. - Attributes:: - - has_kernelopts (bool): Returns True/False depending on if kernelopts key is in the dict. - kernelopts (bool): Returns the string of kernelopts from the dict. - has_tuned_params (str): Returns True/False depending of if the tuned_params key is in the dict. - tuned_params (str): Returns the string of tuned_params from the dict. - errors (str): Returns the string of error message. - Sample output of the command:: saved_entry=295e1ba1696e4fad9e062f096f92d147-4.18.0-305.el8.x86_64 @@ -52,8 +44,7 @@ class GrubEnv(Parser, dict): tuned_params=transparent_hugepages=never tuned_initrd= - Examples:: - + Examples: >>> type(grubenv) >>> grubenv.has_kernelopts @@ -95,20 +86,25 @@ def parse_content(self, content): @property def has_kernelopts(self): + """ bool: Returns True/False depending on if kernelopts key is in the dict. """ return "kernelopts" in self @property def kernelopts(self): + """ bool: Returns the string of kernelopts from the dict. """ return self.get("kernelopts", "") @property def has_tuned_params(self): + """ bool: Returns True/False depending of if the tuned_params key is in the dict. """ return "tuned_params" in self @property def tuned_params(self): + """ str: Returns the string of tuned_params from the dict.""" return self.get("tuned_params", "") @property def errors(self): + """ list: Returns the list of error message. """ return self._errors diff --git a/insights/specs/insights_archive.py b/insights/specs/insights_archive.py index df4433907c..5b8e39e022 100644 --- a/insights/specs/insights_archive.py +++ b/insights/specs/insights_archive.py @@ -80,7 +80,7 @@ class InsightsArchiveSpecs(Specs): getconf_page_size = simple_file("insights_commands/getconf_PAGE_SIZE") getenforce = simple_file("insights_commands/getenforce") getsebool = simple_file("insights_commands/getsebool_-a") - grubenv = first_file(["/boot/grub2/grubenv", "/boot/efi/EFI/redhat/grubenv"]) + grubenv = first_file(["insights_commands/grub2-editenv_list", "/boot/grub2/grubenv", "/boot/efi/EFI/redhat/grubenv"]) grub1_config_perms = first_file(["insights_commands/ls_-lH_.boot.grub.grub.conf", "insights_commands/ls_-l_.boot.grub.grub.conf"]) grub_config_perms = first_file(["insights_commands/ls_-lH_.boot.grub2.grub.cfg", "insights_commands/ls_-l_.boot.grub2.grub.cfg"]) grubby_default_index = simple_file("insights_commands/grubby_--default-index") diff --git a/insights/tests/parsers/test_grubenv.py b/insights/tests/parsers/test_grubenv.py index 6156b9ec5e..8c42618769 100644 --- a/insights/tests/parsers/test_grubenv.py +++ b/insights/tests/parsers/test_grubenv.py @@ -6,29 +6,23 @@ GRUBENV_WITH_TUNED_PARAMS = """ -# GRUB Environment Block saved_entry=295e1ba1696e4fad9e062f096f92d147-4.18.0-305.el8.x86_64 kernelopts=root=/dev/mapper/root_vg-lv_root ro crashkernel=auto resume=/dev/mapper/root_vg-lv_swap rd.lvm.lv=root_vg/lv_root rd.lvm.lv=root_vg/lv_swap console=tty0 console=ttyS0,115200 boot_success=0 boot_indeterminate=2 tuned_params=transparent_hugepages=never tuned_initrd= -############################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################### """.strip() # noqa GRUBENV_WITHOUT_TUNED_PARAMS = """ -# GRUB Environment Block saved_entry=295e1ba1696e4fad9e062f096f92d147-4.18.0-305.el8.x86_64 kernelopts=root=/dev/mapper/root_vg-lv_root ro crashkernel=auto resume=/dev/mapper/root_vg-lv_swap rd.lvm.lv=root_vg/lv_root rd.lvm.lv=root_vg/lv_swap console=tty0 console=ttyS0,115200 boot_success=0 boot_indeterminate=2 -############################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################### """.strip() # noqa GRUBENV_RHEL7 = """ -# GRUB Environment Block saved_entry=Red Hat Enterprise Linux Server (3.10.0-1127.el7.x86_64) 7.8 (Maipo) -###################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################################### """.strip() # noqa GRUBEDITENVLIST_ERROR = """ From 7ad6b89215d3e2b5a7ba2a92917b99d3773adaa8 Mon Sep 17 00:00:00 2001 From: Xinting Li Date: Thu, 18 Aug 2022 13:52:42 +0800 Subject: [PATCH 13/13] Update doc string Signed-off-by: Xinting Li --- insights/parsers/grubenv.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/insights/parsers/grubenv.py b/insights/parsers/grubenv.py index dd3a22928c..79e9c82df5 100644 --- a/insights/parsers/grubenv.py +++ b/insights/parsers/grubenv.py @@ -91,7 +91,7 @@ def has_kernelopts(self): @property def kernelopts(self): - """ bool: Returns the string of kernelopts from the dict. """ + """ str: Returns the string of kernelopts from the dict. """ return self.get("kernelopts", "") @property