Skip to content

Commit 5ae6177

Browse files
committed
Identify kernel information via /lib/modules instead of boot parameters
1 parent 511c789 commit 5ae6177

File tree

1 file changed

+11
-24
lines changed

1 file changed

+11
-24
lines changed

library/collect_kernel_info.py

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,44 +21,31 @@ def main():
2121

2222
params = module.params
2323

24-
# Much of the following is reimplemented from /usr/share/grub/grub-mkconfig_lib
25-
kernels = []
26-
# Collect a list of possible installed kernels
27-
for filename in glob.glob("/boot/vmlinuz-*") + glob.glob("/vmlinuz-*") + \
28-
glob.glob("/boot/kernel-*"):
29-
if ".dpkg-" in filename:
30-
continue
31-
if filename.endswith(".rpmsave") or filename.endswith(".rpmnew"):
32-
continue
33-
kernels.append(filename)
24+
# Collect a list of installed kernels
25+
kernels = glob.glob("/lib/modules/*")
3426

27+
# Identify path to the latest kernel
3528
latest_kernel = ""
36-
re_prefix = re.compile("[^-]*-")
37-
re_attributes = re.compile("[._-](pre|rc|test|git|old|trunk)")
3829
for kernel in kernels:
39-
right = re.sub(re_attributes, "~\1", re.sub(re_prefix, '', latest_kernel, count=1))
40-
if not right:
30+
if not latest_kernel:
4131
latest_kernel = kernel
4232
continue
43-
left = re.sub(re_attributes, "~\1", re.sub(re_prefix, '', kernel, count=1))
33+
# These splits remove the path and get the base directory name, which
34+
# should be something like 5.4.78-1-pve, that we can compare
35+
right = latest_kernel.split("/")[-1]
36+
left = kernel.split("/")[-1]
4437
cmp_str = "gt"
45-
if left.endswith(".old") and not right.endswith(".old"):
46-
left = left[:-4]
47-
if right.endswith(".old") and not left.endswith(".old"):
48-
right = right[:-4]
49-
cmp_str = "ge"
5038
if subprocess.call(["dpkg", "--compare-versions", left, cmp_str, right]) == 0:
5139
latest_kernel = kernel
5240

53-
# This will likely output a path that considers the boot partition as /
54-
# e.g. /vmlinuz-4.4.44-1-pve
55-
booted_kernel = to_text(subprocess.check_output(["grep", "-o", "-P", "(?<=BOOT_IMAGE=).*?(?= )", "/proc/cmdline"]).strip())
41+
booted_kernel = "/lib/modules/{}".format(to_text(subprocess.check_output(["uname", "-r"]).strip))
5642

5743
booted_kernel_package = ""
5844
old_kernel_packages = []
59-
6045
if params['lookup_packages']:
6146
for kernel in kernels:
47+
# Identify the currently booted kernel and unused old kernels by
48+
# querying which packages owns the directory in /lib/modules
6249
if kernel.split("/")[-1] == booted_kernel.split("/")[-1]:
6350
booted_kernel_package = to_text(subprocess.check_output(["dpkg-query", "-S", kernel])).split(":")[0]
6451
elif kernel != latest_kernel:

0 commit comments

Comments
 (0)