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