New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new facts for list of loaded kernel modules #48644

Open
wants to merge 1 commit into
base: devel
from

Conversation

Projects
None yet
5 participants
@ageis
Contributor

ageis commented Nov 14, 2018

New fact: ansible_loaded_kernel_modules, a list of dicts
Based upon the content of /proc/modules
Included in the default collectors and minimal_gather_subset
Each module has the following attributes:

  • name: name of the kernel module
    
  • size: memory size of the module in bytes
    
  • instances: number of instances of the module currently loaded
    
  • dependencies: other modules this depends upon in order to run
    
  • state: current state (either Live, Loading or Unloading)
    
SUMMARY

This is extremely useful to have as facts (enabling conditional behavior based on the system's modules, e.g.:

when: ansible_loaded_kernel_modules | selectattr('name','equalto','md_mod') | list | count > 0

I was surprised this was not already included.

ISSUE TYPE
  • Feature Pull Request
COMPONENT NAME

LsmodFactCollector, lsmod facts, ansible_loaded_kernel_modules

ADDITIONAL INFORMATION

I'm disappointed that the order of the OrderedDict is not preserved. =/
Let me know if you need any tests or documentation updates.

Example output (purposely truncated for length):

        "ansible_loaded_kernel_modules": [
            {
                "dependencies": "", 
                "instances": "2", 
                "name": "cifs", 
                "size": "909312", 
                "state": "Live"
            }, 
            {
                "dependencies": "", 
                "instances": "5", 
                "name": "fuse", 
                "size": "122880", 
                "state": "Live"
            }, 
            {
                "dependencies": "", 
                "instances": "0", 
                "name": "vboxpci", 
                "size": "28672", 
                "state": "Live"
            }, 
            {
                "dependencies": "", 
                "instances": "0", 
                "name": "vboxnetadp", 
                "size": "28672", 
                "state": "Live"
            }, 
            {
                "dependencies": "", 
                "instances": "0", 
                "name": "vboxnetflt", 
                "size": "32768", 
                "state": "Live"
            }, 
            {
                "dependencies": "vboxpci,vboxnetadp,vboxnetflt", 
                "instances": "3", 
                "name": "vboxdrv", 
                "size": "483328", 
                "state": "Live"
            }, 
            {
                "dependencies": "", 
                "instances": "16", 
                "name": "lz4", 
                "size": "16384", 
                "state": "Live"
            }, 
            {
                "dependencies": "lz4", 
                "instances": "1", 
                "name": "lz4_compress", 
                "size": "32768", 
                "state": "Live"
            }, 
            {
                "dependencies": "", 
                "instances": "9", 
                "name": "nf_conntrack_ipv4", 
                "size": "16384", 
                "state": "Live"
            }, 
            {
                "dependencies": "ipt_MASQUERADE,nf_conntrack_netlink,xt_conntrack,nft_ct,nf_conntrack_ipv6,nf_nat_ipv6,nf_conntrack_ipv4,nf_nat_ipv4,nf_nat", 
                "instances": "9", 
                "name": "nf_conntrack", 
                "size": "155648", 
                "state": "Live"
            }, 
            {
                "dependencies": "", 
                "instances": "0", 
                "name": "ip6_tables", 
                "size": "32768", 
                "state": "Live"
            }, 
            {
                "dependencies": "", 
                "instances": "1", 
                "name": "vfat", 
                "size": "24576", 
                "state": "Live"
            }, 
            {
                "dependencies": "", 
                "instances": "1", 
                "name": "snd_hda_codec_realtek", 
                "size": "110592", 
                "state": "Live"
            }, 
            {
                "dependencies": "snd_hda_codec_realtek", 
                "instances": "1", 
                "name": "snd_hda_codec_generic", 
                "size": "86016", 
                "state": "Live"
            }, 
            {
                "dependencies": "", 
                "instances": "12", 
                "name": "amdgpu", 
                "size": "3354624", 
                "state": "Live"
            }, 
            {
                "dependencies": "", 
                "instances": "0", 
                "name": "snd_usb_audio", 
                "size": "249856", 
                "state": "Live"
            }, 
            {
                "dependencies": "snd_hda_codec_realtek,snd_hda_codec_generic,snd_hda_codec_hdmi,snd_hda_intel", 
                "instances": "4", 
                "name": "snd_hda_codec", 
                "size": "151552", 
                "state": "Live"
            }, 
            {
                "dependencies": "snd_hda_codec_realtek,snd_hda_codec_generic,snd_hda_codec_hdmi,snd_hda_intel,snd_hda_codec", 
                "instances": "5", 
                "name": "snd_hda_core", 
                "size": "94208", 
                "state": "Live"
            }, 
            {
                "dependencies": "", 
                "instances": "0", 
                "name": "pcspkr", 
                "size": "16384", 
                "state": "Live"
            }, 
            {
                "dependencies": "snd_hda_codec_hdmi,snd_usb_audio,snd_hda_intel,snd_hda_codec,snd_hda_core,snd_pcm_oss", 
                "instances": "6", 
                "name": "snd_pcm", 
                "size": "118784", 
                "state": "Live"
            }, 
            {
                "dependencies": "efi_pstore", 
                "instances": "1", 
                "name": "efivars", 
                "size": "20480", 
                "state": "Live"
            }, 
            {
                "dependencies": "snd_hda_codec_realtek,snd_hda_codec_generic,snd_hda_codec_hdmi,snd_usb_audio,snd_hda_intel,snd_usbmidi_lib,snd_hda_codec,snd_rawmidi,snd_seq_device,snd_hwdep,snd_pcm_oss,snd_mixer_oss,snd_pcm,snd_timer", 
                "instances": "14", 
                "name": "snd", 
                "size": "94208", 
                "state": "Live"
            }, 
            {
                "dependencies": "snd", 
                "instances": "1", 
                "name": "soundcore", 
                "size": "16384", 
                "state": "Live"
            }, 
            {
                "dependencies": "", 
                "instances": "0", 
                "name": "video", 
                "size": "45056", 
                "state": "Live"
            }, 

            {
                "dependencies": "", 
                "instances": "1", 
                "name": "binfmt_misc", 
                "size": "20480", 
                "state": "Live"
            }, 
            {
                "dependencies": "", 
                "instances": "0", 
                "name": "bfq", 
                "size": "69632", 
                "state": "Live"
            }, 
            {
                "dependencies": "", 
                "instances": "0", 
                "name": "lp", 
                "size": "20480", 
                "state": "Live"
            }, 
            {
                "dependencies": "", 
                "instances": "0", 
                "name": "loop", 
                "size": "32768", 
                "state": "Live"
            }, 
            {
                "dependencies": "", 
                "instances": "1", 
                "name": "efivarfs", 
                "size": "16384", 
                "state": "Live"
            }, 
            {
                "dependencies": "", 
                "instances": "0", 
                "name": "ip_tables", 
                "size": "28672", 
                "state": "Live"
            }, 
            {
                "dependencies": "", 
                "instances": "5", 
                "name": "ext4", 
                "size": "741376", 
                "state": "Live"
            },
            {
                "dependencies": "", 
                "instances": "0", 
                "name": "crc32c_generic", 
                "size": "16384", 
                "state": "Live"
            }, 
            {
                "dependencies": "", 
                "instances": "0", 
                "name": "raid1", 
                "size": "45056", 
                "state": "Live"
            }, 
            {
                "dependencies": "", 
                "instances": "0", 
                "name": "raid0", 
                "size": "20480", 
                "state": "Live"
            }, 
            {
                "dependencies": "raid10,raid456,raid1,raid0,multipath,linear", 
                "instances": "6", 
                "name": "md_mod", 
                "size": "159744", 
                "state": "Live"
            }, 
            {
                "dependencies": "", 
                "instances": "0", 
                "name": "hid_generic", 
                "size": "16384", 
                "state": "Live"
            }, 
            {
                "dependencies": "", 
                "instances": "0", 
                "name": "usbhid", 
                "size": "57344", 
                "state": "Live"
            }, 
            {
                "dependencies": "hid_generic,usbhid", 
                "instances": "2", 
                "name": "hid", 
                "size": "135168", 
                "state": "Live"
            }, 
            {
                "dependencies": "", 
                "instances": "9", 
                "name": "crc32c_intel", 
                "size": "24576", 
                "state": "Live"
            }, 
            {
                "dependencies": "", 
                "instances": "4", 
                "name": "ahci", 
                "size": "40960", 
                "state": "Live"
            }, 
            {
                "dependencies": "", 
                "instances": "7", 
                "name": "aesni_intel", 
                "size": "200704", 
                "state": "Live"
            }, 
            {
                "dependencies": "", 
                "instances": "0", 
                "name": "igb", 
                "size": "249856", 
                "state": "Live"
            }, 
            {
                "dependencies": "", 
                "instances": "0", 
                "name": "xhci_pci", 
                "size": "16384", 
                "state": "Live"
            }, 
            {
                "dependencies": "aesni_intel", 
                "instances": "1", 
                "name": "aes_x86_64", 
                "size": "20480", 
                "state": "Live"
            }, 
            {
                "dependencies": "ahci", 
                "instances": "1", 
                "name": "libahci", 
                "size": "40960", 
                "state": "Live"
            }, 
            {
                "dependencies": "xhci_pci", 
                "instances": "1", 
                "name": "xhci_hcd", 
                "size": "270336", 
                "state": "Live"
            }, 
            {
                "dependencies": "ahci,libahci", 
                "instances": "2", 
                "name": "libata", 
                "size": "278528", 
                "state": "Live"
            }, 
            {
                "dependencies": "", 
                "instances": "0", 
                "name": "e1000e", 
                "size": "282624", 
                "state": "Live"
            }, 
            {
                "dependencies": "snd_usb_audio,snd_usbmidi_lib,usbhid,xhci_pci,xhci_hcd", 
                "instances": "5", 
                "name": "usbcore", 
                "size": "290816", 
                "state": "Live"
            }, 
            {
                "dependencies": "usbcore", 
                "instances": "1", 
                "name": "usb_common", 
                "size": "16384", 
                "state": "Live"
            }
        ], 

@ageis ageis force-pushed the ageis:add-lsmod-facts branch from 34d236e to 73a4e19 Nov 14, 2018

@ansibot

This comment has been minimized.

Contributor

ansibot commented Nov 14, 2018

Hi @ageis, thank you for submitting this pull-request!

click here for bot help

@ansibot

This comment has been minimized.

Contributor

ansibot commented Nov 14, 2018

The test ansible-test sanity --test ansible-doc --python 2.6 [explain] failed with the error:

Command "ansible-doc -t strategy debug free host_pinned linear" returned exit status 250.
>>> Standard Error
ERROR! Unexpected Exception, this is probably a bug: cannot import name OrderedDict

The test ansible-test sanity --test import --python 2.6 [explain] failed with 2 errors:

lib/ansible/module_utils/facts/system/lsmod.py:20:0: ImportError: cannot import name OrderedDict
test/runner/.tox/import/lib/ansible/module_utils/facts/system/lsmod.py:20:0: ImportError: cannot import name OrderedDict

click here for bot help

@ansibot ansibot added needs_revision and removed core_review labels Nov 14, 2018

__metaclass__ = type
import os
from collections import OrderedDict

This comment has been minimized.

@mattclay

mattclay Nov 16, 2018

Member

OrderedDict is not available in Python 2.6, which is why CI tests are failing.

This comment has been minimized.

@ageis

ageis Nov 16, 2018

Contributor

Ah, it doesn't work anyway, so I just rescoped and amended the commit. Thought you guys had already phased out py2.6 support.

This comment has been minimized.

@mattclay

mattclay Nov 16, 2018

Member

Python 2.6 is no longer supported on the controller. Modules (and therefore module_utils) still support Python 2.6.

Add new facts for list of loaded kernel modules
New fact: ansible_loaded_kernel_modules, a list of dicts
Based upon the content of /proc/modules
Included in the default collectors and minimal_gather_subset
Each module has the following attributes:
    name: name of the kernel module
    size: memory size of the module in bytes
    instances: number of instances of the module currently loaded
    dependencies: other modules this depends upon in order to run
    state: current state (either Live, Loading or Unloading)

@ageis ageis force-pushed the ageis:add-lsmod-facts branch from 73a4e19 to 4a7c8e2 Nov 16, 2018

@ansibot

This comment has been minimized.

Contributor

ansibot commented Nov 24, 2018

@ansibot ansibot added stale_ci and removed support:community labels Nov 24, 2018

@cognifloyd

This comment has been minimized.

Contributor

cognifloyd commented Nov 28, 2018

Would it also be helpful to provide a list of builtin modules?
This is how I look up builtins in my PR for the modprobe module:
https://github.com/ansible/ansible/pull/37150/files#diff-ed0bbe136ae5cdd039513df42b331ec0

@ageis

This comment has been minimized.

Contributor

ageis commented Nov 28, 2018

@cognifloyd: if I could call modprobe module with no state ['present', 'absent'] (just a module name and a registar var) and it would tell me the status of whether the module is present or loaded, this effort may not even be necessary. last I checked it defaulted to state: present even if you're just trying to do a dry-run lookup. thanks for the tip....

anyway, by builtin you mean e.g. kernel config variable value 'y' versus 'm', .ko files in /lib/modules etc.? if we start differentiating these, aren't there a lot of alternative locations modules can reside and ways the can be loaded? if i enabled something in my initrd does it count as "builtin"?

@cognifloyd

This comment has been minimized.

Contributor

cognifloyd commented Nov 30, 2018

if I could call modprobe module with no state ['present', 'absent'] ...

@ageis I guess it would be possible to return facts from the modprobe module, but it wouldn't make sense to run it without requesting present/absent for a particular kernel module. I think returning facts is best as a separate module like you've done here, or if you want something you can call on the fly, maybe modprobe_facts or even lsmod.

anyway, by builtin you mean e.g. kernel config variable value 'y' versus 'm' ...

Yes. I mean the things that are compiled into the kernel so that they cannot be rmmoded. Thus a builtin does not have a *.ko file anywhere as it's built into vmlinuz.

if i enabled something in my initrd does it count as "builtin"?

No. It is only builtin if it is compiled into the kernel binary, not as a separate loadable *.ko kernel module file (wherever that *.ko file might be).

aren't there a lot of alternative locations modules can reside

Sure. But that's only talking about modules that can be loaded and unloaded. Builtins cannot be unloaded without recompiling the kernel. Plus, you are adding facts about what is currently loaded in the kernel, not about which modules are not loaded but could be loaded in the kernel.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment