@@ -21,44 +21,31 @@ def main():
21
21
22
22
params = module .params
23
23
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/*" )
34
26
27
+ # Identify path to the latest kernel
35
28
latest_kernel = ""
36
- re_prefix = re .compile ("[^-]*-" )
37
- re_attributes = re .compile ("[._-](pre|rc|test|git|old|trunk)" )
38
29
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 :
41
31
latest_kernel = kernel
42
32
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 ]
44
37
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"
50
38
if subprocess .call (["dpkg" , "--compare-versions" , left , cmp_str , right ]) == 0 :
51
39
latest_kernel = kernel
52
40
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 ))
56
42
57
43
booted_kernel_package = ""
58
44
old_kernel_packages = []
59
-
60
45
if params ['lookup_packages' ]:
61
46
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
62
49
if kernel .split ("/" )[- 1 ] == booted_kernel .split ("/" )[- 1 ]:
63
50
booted_kernel_package = to_text (subprocess .check_output (["dpkg-query" , "-S" , kernel ])).split (":" )[0 ]
64
51
elif kernel != latest_kernel :
0 commit comments