Make modprobe module check for builtins as well (#37150)

Without this modprobe always reports changed when modprobe-ing a builtin module.

With this, if a kernel module is a builtin, the modprobe module will:
- succeed (without incorrectly reporting changed) if ``state`` is ``present``;
- fail if ``state`` is ``absent``

The failure will have whatever error message modprobe returns when
attempting to remove a builtin module. For example:
``modprobe: ERROR: Module nfs is builtin.``
This commit is contained in:
Jacob Floyd 2018-12-20 15:30:29 -06:00 committed by Alicia Cozine
commit 069e0b8d57
3 changed files with 25 additions and 7 deletions

View file

@ -83,14 +83,22 @@ def main():
# Check if module is present
try:
modules = open('/proc/modules')
present = False
module_name = name.replace('-', '_') + ' '
for line in modules:
if line.startswith(module_name):
present = True
break
modules.close()
with open('/proc/modules') as modules:
module_name = name.replace('-', '_') + ' '
for line in modules:
if line.startswith(module_name):
present = True
break
if not present:
command = [module.get_bin_path('uname', True), '-r']
rc, uname_kernel_release, err = module.run_command(command)
module_file = '/' + name + '.ko'
with open('/lib/modules/' + uname_kernel_release + '/modules.builtin') as builtins:
for line in builtins:
if line.endswith(module_file):
present = True
break
except IOError as e:
module.fail_json(msg=to_native(e), exception=traceback.format_exc(), **result)