Skip to content

Commit 1cb0cba

Browse files
authored
Merge pull request #190 from lae/fix/disowned-kernels
Ignore module dirs that have no associated package
2 parents de29987 + 6dc58c6 commit 1cb0cba

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

library/collect_kernel_info.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#!/usr/bin/python
22
import glob
3-
import re
43
import subprocess
54

65
from ansible.module_utils.basic import AnsibleModule
76
from ansible.module_utils._text import to_text
87

8+
99
def main():
1010
module = AnsibleModule(
1111
argument_spec = dict(
@@ -33,22 +33,37 @@ def main():
3333
if subprocess.call(["dpkg", "--compare-versions", left, cmp_str, right]) == 0:
3434
latest_kernel = kernel
3535

36-
booted_kernel = "/lib/modules/{}".format(to_text(subprocess.check_output(["uname", "-r"]).strip))
36+
booted_kernel = "/lib/modules/{}".format(to_text(
37+
subprocess.run(["uname", "-r"], capture_output=True).stdout.strip))
3738

3839
booted_kernel_package = ""
3940
old_kernel_packages = []
4041
if params['lookup_packages']:
4142
for kernel in kernels:
4243
# Identify the currently booted kernel and unused old kernels by
43-
# querying which packages owns the directory in /lib/modules
44+
# querying which packages own directories in /lib/modules
45+
try:
46+
sp = subprocess.run(["dpkg-query", "-S", kernel],
47+
check=True, capture_output=True)
48+
except subprocess.CalledProcessError as e:
49+
# Ignore errors about directories not associated with a package
50+
if e.stderr.startswith(b"dpkg-query: no path found matching"):
51+
continue
52+
raise e
4453
if kernel.split("/")[-1] == booted_kernel.split("/")[-1]:
45-
booted_kernel_package = to_text(subprocess.check_output(["dpkg-query", "-S", kernel])).split(":")[0]
54+
booted_kernel_package = to_text(sp.stdout).split(":")[0]
4655
elif kernel != latest_kernel:
47-
old_kernel_packages.append(to_text(subprocess.check_output(["dpkg-query", "-S", kernel])).split(":")[0])
56+
old_kernel_packages.append(to_text(sp.stdout).split(":")[0])
4857

4958
# returns True if we're not booted into the latest kernel
5059
new_kernel_exists = booted_kernel.split("/")[-1] != latest_kernel.split("/")[-1]
51-
module.exit_json(changed=False, new_kernel_exists=new_kernel_exists, old_packages=old_kernel_packages, booted_package=booted_kernel_package)
60+
module.exit_json(
61+
changed=False,
62+
new_kernel_exists=new_kernel_exists,
63+
old_packages=old_kernel_packages,
64+
booted_package=booted_kernel_package
65+
)
66+
5267

5368
if __name__ == '__main__':
5469
main()

0 commit comments

Comments
 (0)