Skip to content

Commit a55a9c0

Browse files
authored
Merge pull request #116 from lae/fix/efi-kernel
Identify kernel information via /lib/modules instead of boot parameters
2 parents f3bcd26 + 5ae6177 commit a55a9c0

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
@@ -16,44 +16,31 @@ def main():
1616

1717
params = module.params
1818

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

22+
# Identify path to the latest kernel
3023
latest_kernel = ""
31-
re_prefix = re.compile("[^-]*-")
32-
re_attributes = re.compile("[._-](pre|rc|test|git|old|trunk)")
3324
for kernel in kernels:
34-
right = re.sub(re_attributes, "~\1", re.sub(re_prefix, '', latest_kernel, count=1))
35-
if not right:
25+
if not latest_kernel:
3626
latest_kernel = kernel
3727
continue
38-
left = re.sub(re_attributes, "~\1", re.sub(re_prefix, '', kernel, count=1))
28+
# These splits remove the path and get the base directory name, which
29+
# should be something like 5.4.78-1-pve, that we can compare
30+
right = latest_kernel.split("/")[-1]
31+
left = kernel.split("/")[-1]
3932
cmp_str = "gt"
40-
if left.endswith(".old") and not right.endswith(".old"):
41-
left = left[:-4]
42-
if right.endswith(".old") and not left.endswith(".old"):
43-
right = right[:-4]
44-
cmp_str = "ge"
4533
if subprocess.call(["dpkg", "--compare-versions", left, cmp_str, right]) == 0:
4634
latest_kernel = kernel
4735

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

5238
booted_kernel_package = ""
5339
old_kernel_packages = []
54-
5540
if params['lookup_packages']:
5641
for kernel in kernels:
42+
# Identify the currently booted kernel and unused old kernels by
43+
# querying which packages owns the directory in /lib/modules
5744
if kernel.split("/")[-1] == booted_kernel.split("/")[-1]:
5845
booted_kernel_package = to_text(subprocess.check_output(["dpkg-query", "-S", kernel])).split(":")[0]
5946
elif kernel != latest_kernel:

0 commit comments

Comments
 (0)