Skip to content

Commit

Permalink
Feat: Add Parser LsinitrdKdumpImage (#3567)
Browse files Browse the repository at this point in the history
* New Parser for lsinitrd kdump initramfs image

Signed-off-by: Xinting Li <[email protected]>

* Update custom_datasources_index.rst
* Fix flake8 error
* Fix doc test error
* update multi_output in __init__.py
* Use listdir instead of datasource

Signed-off-by: Xinting Li <[email protected]>

* Feat: Update spec command with using -k option and change to using datasource

Signed-off-by: Xinting Li <[email protected]>

* Fix: Fix the title underline is too short in custom_datasources_index.rst
* fix: fix unexpected indentation in current_kernel_version
* Fix: fix the title underline is too short and rename datasource
* Fix: fix doc error
* update the order of datasource

Signed-off-by: Xinting Li <[email protected]>
(cherry picked from commit 4121e94)
  • Loading branch information
TZ3070 authored and xiangce committed Dec 8, 2022
1 parent 4ce979a commit bb2d1f9
Show file tree
Hide file tree
Showing 7 changed files with 227 additions and 5 deletions.
8 changes: 8 additions & 0 deletions docs/custom_datasources_index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ insights.specs.datasources.ipcs
:show-inheritance:
:undoc-members:

insights.specs.datasources.kernel
---------------------------------

.. automodule:: insights.specs.datasources.kernel
:members: current_version
:show-inheritance:
:undoc-members:

insights.specs.datasources.kernel_module_list
---------------------------------------------

Expand Down
56 changes: 53 additions & 3 deletions insights/parsers/lsinitrd.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
"""
Lsinitrd - command ``lsinitrd``
lsinitrd - command ``lsinitrd``
===============================
This parser parses the filtered output of command ``lsinitrd`` and provides the
info of listed files.
This module contains the following parsers:
Lsinitrd - command ``lsinitrd``
-------------------------------
LsinitrdKdumpImage - command ``lsinitrd initramfs-<kernel-version>kdump.img``
-----------------------------------------------------------------------------
"""

from insights import parser, CommandParser
Expand All @@ -15,6 +21,9 @@
@parser(Specs.lsinitrd)
class Lsinitrd(CommandParser):
"""
This parser parses the filtered output of command ``lsinitrd`` and provides
the info of listed files.
A parser for command "lsinitrd".
Attributes:
Expand Down Expand Up @@ -86,3 +95,44 @@ def search(self, **kwargs):
"""
return keyword_search(self.data.values(), **kwargs)


@parser(Specs.lsinitrd_kdump_image)
class LsinitrdKdumpImage(Lsinitrd):
"""
Parses output of ``lsinitrd initramfs-<kernel-version>kdump.img`` command.
Sample ``lsinitrd initramfs-<kernel-version>kdump.img`` output::
Image: initramfs-4.18.0-240.el8.x86_64kdump.img: 19M
========================================================================
Version: dracut-049-95.git20200804.el8
Arguments: --quiet --hostonly --hostonly-cmdline --hostonly-i18n --hostonly-mode 'strict' -o 'plymouth dash resume ifcfg earlykdump' --mount '/dev/mapper/rhel-root /sysroot xfs rw,relatime,seclabel,attr2,inode64,logbufs=8,logbsize=32k,noquota,nofail,x-systemd.before=initrd-fs.target' --no-hostonly-default-device -f
dracut modules:
bash
systemd
systemd-initrd
i18n
========================================================================
crw-r--r-- 1 root root 5, 1 Aug 4 2020 dev/console
crw-r--r-- 1 root root 1, 11 Aug 4 2020 dev/kmsg
crw-r--r-- 1 root root 1, 3 Aug 4 2020 dev/null
crw-r--r-- 1 root root 1, 8 Aug 4 2020 dev/random
crw-r--r-- 1 root root 1, 9 Aug 4 2020 dev/urandom
drwxr-xr-x 14 root root 0 Aug 4 2020 .
lrwxrwxrwx 1 root root 7 Aug 4 2020 bin -> usr/bin
drwxr-xr-x 2 root root 0 Aug 4 2020 dev
========================================================================
Examples:
>>> type(lsinitrd_kdump_image)
<class 'insights.parsers.lsinitrd_kdump_image.LsinitrdKdumpImage'>
>>> lsinitrd_kdump_image.search(name__contains='urandom')
>>> len(result_list)
1
>>> result_list[0].get('raw_entry')
'crw-r--r-- 1 root root 1, 9 Aug 4 2020 dev/urandom'
"""
pass
1 change: 1 addition & 0 deletions insights/specs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ class Specs(SpecSet):
lsblk_pairs = RegistryPoint()
lscpu = RegistryPoint()
lsinitrd = RegistryPoint(filterable=True)
lsinitrd_kdump_image = RegistryPoint(filterable=True)
lsinitrd_lvm_conf = RegistryPoint()
lsmod = RegistryPoint()
lsof = RegistryPoint(filterable=True)
Expand Down
26 changes: 26 additions & 0 deletions insights/specs/datasources/kernel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""
Custom datasource to get the current kernel version.
"""

from insights.core.context import HostContext
from insights.core.plugins import datasource
from insights.parsers.uname import Uname


@datasource(Uname, HostContext)
def current_version(broker):
"""
This datasource provides the current booting kernel version.
Sample data returned::
'4.18.0-240.el8.x86_64'
Returns:
String: The current kernel version.
Raises:
UnameError: When there is a problem occurs with uname data.
"""

return broker[Uname].kernel
3 changes: 2 additions & 1 deletion insights/specs/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from insights.specs import Specs
from insights.specs.datasources import (
aws, awx_manage, cloud_init, candlepin_broker, corosync as corosync_ds,
dir_list, ethernet, httpd, ipcs, kernel_module_list, lpstat, md5chk,
dir_list, ethernet, httpd, ipcs, kernel, kernel_module_list, lpstat, md5chk,
package_provides, ps as ps_datasource, sap, satellite_missed_queues,
semanage, ssl_certificate, sys_fs_cgroup_memory_tasks_number, rpm_pkgs, user_group, yum_updates, luks_devices)
from insights.specs.datasources.sap import sap_hana_sid, sap_hana_sid_SID_nr
Expand Down Expand Up @@ -342,6 +342,7 @@ class DefaultSpecs(Specs):
lsblk = simple_command("/bin/lsblk")
lsblk_pairs = simple_command("/bin/lsblk -P -o NAME,KNAME,MAJ:MIN,FSTYPE,MOUNTPOINT,LABEL,UUID,RA,RO,RM,MODEL,SIZE,STATE,OWNER,GROUP,MODE,ALIGNMENT,MIN-IO,OPT-IO,PHY-SEC,LOG-SEC,ROTA,SCHED,RQ-SIZE,TYPE,DISC-ALN,DISC-GRAN,DISC-MAX,DISC-ZERO")
lscpu = simple_command("/usr/bin/lscpu")
lsinitrd_kdump_image = command_with_args("/usr/bin/lsinitrd -k %skdump", kernel.current_version)
lsmod = simple_command("/sbin/lsmod")
lsof = first_of([
simple_command("/usr/bin/lsof"),
Expand Down
28 changes: 28 additions & 0 deletions insights/tests/datasources/test_kernel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import pytest
from insights.specs.datasources.kernel import current_version
from insights.parsers.uname import Uname
from insights.parsers import uname
from insights.tests import context_wrap

UNAME = """
Linux vm37-130.gsslab.pek2.redhat.com 5.14.0-160.el9.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Aug 25 20:41:37 EDT 2022 x86_64 x86_64 x86_64 GNU/Linux
"""

UNAME_ERROR_BLANK = ""


def test_current_kernel_version():
uname = Uname(context_wrap(UNAME))

broker = {
Uname: uname
}
result = current_version(broker)
assert result is not None
assert result == '5.14.0-160.el9.x86_64'


def test_current_kernel_version_without_uname():
with pytest.raises(uname.UnameError) as e_info:
current_version({Uname: uname.Uname(context_wrap(UNAME_ERROR_BLANK))})
assert 'Empty uname line' in str(e_info.value)
110 changes: 109 additions & 1 deletion insights/tests/parsers/test_lsinitrd.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import doctest
import pytest
from insights.parsers import lsinitrd
from insights.parsers.lsinitrd import LsinitrdKdumpImage
from insights.tests import context_wrap


Expand Down Expand Up @@ -77,6 +78,104 @@
c
""".strip()

LSINITRD_KDUMP_IMAGE_VALID = """
Image: initramfs-4.18.0-240.el8.x86_64kdump.img: 19M
========================================================================
Version: dracut-049-95.git20200804.el8
Arguments: --quiet --hostonly --hostonly-cmdline --hostonly-i18n --hostonly-mode 'strict' -o 'plymouth dash resume ifcfg earlykdump' --mount '/dev/mapper/rhel-root /sysroot xfs rw,relatime,seclabel,attr2,inode64,logbufs=8,logbsize=32k,noquota,nofail,x-systemd.before=initrd-fs.target' --no-hostonly-default-device -f
dracut modules:
bash
systemd
systemd-initrd
nss-softokn
rngd
i18n
drm
prefixdevname
dm
kernel-modules
kernel-modules-extra
lvm
qemu
fstab-sys
rootfs-block
terminfo
udev-rules
biosdevname
dracut-systemd
usrmount
base
fs-lib
kdumpbase
memstrack
microcode_ctl-fw_dir_override
shutdown
squash
========================================================================
crw-r--r-- 1 root root 5, 1 Aug 4 2020 dev/console
drwxr-xr-x 2 root root 0 Aug 4 2020 run
-rw-r--r-- 1 root root 306 Aug 4 2020 usr/lib/dracut/build-parameter.txt
-rw-r--r-- 1 root root 30 Aug 4 2020 usr/lib/dracut/dracut-049-95.git20200804.el8
drwxr-xr-x 2 root root 0 Aug 4 2020 usr/lib/dracut/hooks
-rw-r--r-- 1 root root 653 Aug 4 2020 usr/lib/dracut/hostonly-files
-rw-r--r-- 1 root root 780 Aug 4 2020 usr/lib/dracut/loaded-kernel-modules.txt
-rw-r--r-- 1 root root 273 Aug 4 2020 usr/lib/dracut/modules.txt
-rw-r--r-- 1 root root 0 Aug 4 2020 usr/lib/dracut/need-initqueue
-rw-r--r-- 1 root root 0 Aug 4 2020 usr/lib/dracut/no-switch-root
-rw-r--r-- 1 root root 221 Aug 4 2020 usr/lib/initrd-release
drwxr-xr-x 3 root root 0 Aug 4 2020 usr/lib/modules
drwxr-xr-x 3 root root 0 Aug 4 2020 usr/lib/modules/4.18.0-240.el8.x86_64
drwxr-xr-x 4 root root 0 Aug 4 2020 usr/lib/modules/4.18.0-240.el8.x86_64/kernel
drwxr-xr-x 3 root root 0 Aug 4 2020 usr/lib/modules/4.18.0-240.el8.x86_64/kernel/drivers
drwxr-xr-x 2 root root 0 Aug 4 2020 usr/lib/modules/4.18.0-240.el8.x86_64/kernel/drivers/block
-rw-r--r-- 1 root root 16868 Aug 4 2020 usr/lib/modules/4.18.0-240.el8.x86_64/kernel/drivers/block/loop.ko.xz
drwxr-xr-x 4 root root 0 Aug 4 2020 usr/lib/modules/4.18.0-240.el8.x86_64/kernel/fs
drwxr-xr-x 2 root root 0 Aug 4 2020 usr/lib/modules/4.18.0-240.el8.x86_64/kernel/fs/overlayfs
-rw-r--r-- 1 root root 53736 Aug 4 2020 usr/lib/modules/4.18.0-240.el8.x86_64/kernel/fs/overlayfs/overlay.ko.xz
drwxr-xr-x 2 root root 0 Aug 4 2020 usr/lib/modules/4.18.0-240.el8.x86_64/kernel/fs/squashfs
-rw-r--r-- 1 root root 24412 Aug 4 2020 usr/lib/modules/4.18.0-240.el8.x86_64/kernel/fs/squashfs/squashfs.ko.xz
-rw-r--r-- 1 root root 21985 Aug 4 2020 usr/lib/modules/4.18.0-240.el8.x86_64/modules.alias
-rw-r--r-- 1 root root 20181 Aug 4 2020 usr/lib/modules/4.18.0-240.el8.x86_64/modules.alias.bin
-rw-r--r-- 1 root root 7528 Aug 4 2020 usr/lib/modules/4.18.0-240.el8.x86_64/modules.builtin
-rw-r--r-- 1 root root 9683 Aug 4 2020 usr/lib/modules/4.18.0-240.el8.x86_64/modules.builtin.bin
-rw-r--r-- 1 root root 3632 Aug 4 2020 usr/lib/modules/4.18.0-240.el8.x86_64/modules.dep
-rw-r--r-- 1 root root 5304 Aug 4 2020 usr/lib/modules/4.18.0-240.el8.x86_64/modules.dep.bin
-rw-r--r-- 1 root root 126 Aug 4 2020 usr/lib/modules/4.18.0-240.el8.x86_64/modules.devname
-rw-r--r-- 1 root root 101785 Aug 4 2020 usr/lib/modules/4.18.0-240.el8.x86_64/modules.order
-rw-r--r-- 1 root root 85 Aug 4 2020 usr/lib/modules/4.18.0-240.el8.x86_64/modules.softdep
-rw-r--r-- 1 root root 50083 Aug 4 2020 usr/lib/modules/4.18.0-240.el8.x86_64/modules.symbols
-rw-r--r-- 1 root root 56186 Aug 4 2020 usr/lib/modules/4.18.0-240.el8.x86_64/modules.symbols.bin
drwxr-xr-x 2 root root 0 Aug 4 2020 usr/lib64
drwxr-xr-x 2 root root 0 Aug 4 2020 var/tmp
========================================================================
""".strip()

LSINITRD_KDUMP_IMAGE_VALID_EXAMPLE = """
Image: initramfs-4.18.0-240.el8.x86_64kdump.img: 19M
========================================================================
Version: dracut-049-95.git20200804.el8
Arguments: --quiet --hostonly --hostonly-cmdline --hostonly-i18n --hostonly-mode 'strict' -o 'plymouth dash resume ifcfg earlykdump' --mount '/dev/mapper/rhel-root /sysroot xfs rw,relatime,seclabel,attr2,inode64,logbufs=8,logbsize=32k,noquota,nofail,x-systemd.before=initrd-fs.target' --no-hostonly-default-device -f
dracut modules:
bash
systemd
systemd-initrd
i18n
========================================================================
crw-r--r-- 1 root root 5, 1 Aug 4 2020 dev/console
crw-r--r-- 1 root root 1, 11 Aug 4 2020 dev/kmsg
crw-r--r-- 1 root root 1, 3 Aug 4 2020 dev/null
crw-r--r-- 1 root root 1, 8 Aug 4 2020 dev/random
crw-r--r-- 1 root root 1, 9 Aug 4 2020 dev/urandom
drwxr-xr-x 14 root root 0 Aug 4 2020 .
lrwxrwxrwx 1 root root 7 Aug 4 2020 bin -> usr/bin
drwxr-xr-x 2 root root 0 Aug 4 2020 dev
========================================================================
""".strip()


def test_lsinitrd_empty():
d = lsinitrd.Lsinitrd(context_wrap(LSINITRD_EMPTY))
Expand Down Expand Up @@ -119,8 +218,17 @@ def test_lsinitrd_broken():
assert "list index out of range" in str(err)


def test_lsinitrd_kdump_image_valid():
parser_result = LsinitrdKdumpImage(context_wrap(LSINITRD_KDUMP_IMAGE_VALID))
assert parser_result is not None
result_list = parser_result.search(name__contains='devname')
assert len(result_list) == 1
assert result_list[0].get('raw_entry') == '-rw-r--r-- 1 root root 126 Aug 4 2020 usr/lib/modules/4.18.0-240.el8.x86_64/modules.devname'


def test_lsinitrd_docs():
failed_count, tests = doctest.testmod(
globs={'ls': lsinitrd.Lsinitrd(context_wrap(LSINITRD_FILTERED))}
globs={'ls': lsinitrd.Lsinitrd(context_wrap(LSINITRD_FILTERED)),
'lsinitrd_kdump_image': LsinitrdKdumpImage(context_wrap(LSINITRD_KDUMP_IMAGE_VALID_EXAMPLE))}
)
assert failed_count == 0

0 comments on commit bb2d1f9

Please sign in to comment.