From 99bdc8a35aaadcfda3689e191ec76709c5b301e9 Mon Sep 17 00:00:00 2001 From: Gregory Kurtzer Date: Thu, 24 Feb 2022 22:01:46 -0800 Subject: [PATCH] Fix issue with improper kernel version probe --- internal/pkg/container/kernel.go | 63 +++++++++++--------------------- 1 file changed, 21 insertions(+), 42 deletions(-) diff --git a/internal/pkg/container/kernel.go b/internal/pkg/container/kernel.go index 9a9cd8c2..b86aad03 100644 --- a/internal/pkg/container/kernel.go +++ b/internal/pkg/container/kernel.go @@ -1,41 +1,40 @@ package container import ( - "fmt" "path" "path/filepath" - "sort" "github.com/hpcng/warewulf/internal/pkg/util" "github.com/hpcng/warewulf/internal/pkg/wwlog" ) var ( - kernelSearchPaths = []string{ - // This is a printf format where the %s will be the kernel version - `/boot/vmlinuz-%s`, - `/boot/vmlinuz-%s.gz`, - `/lib/modules/%s/vmlinuz`, - `/lib/modules/%s/vmlinuz.gz`, + kernelNames = []string{ + `vmlinux`, + `vmlinuz`, + `vmlinuz.gz`, } + modulePath = "/lib/modules/" ) func KernelFind(container string) string { + wwlog.Printf(wwlog.DEBUG, "Finding kernel\n") container_path := RootFsDir(container) if container_path == "" { return "" } - kernelVersion := KernelVersion(container) - if kernelVersion == "" { - return "" - } - - for _, searchPath := range kernelSearchPaths { - testPath := fmt.Sprintf(searchPath, kernelVersion) - wwlog.Printf(wwlog.VERBOSE, "Looking for kernel at: '%s'\n", testPath) - if util.IsFile(path.Join(container_path, testPath)) { - return path.Join(container_path, testPath) + for _, kname := range kernelNames { + wwlog.Printf(wwlog.DEBUG, "Checking for kernel name within module path: %s\n", kname) + kernelPaths, err := filepath.Glob(path.Join(container_path, modulePath, "/*/", kname)) + if err != nil { + return "" + } + for _, kernelPath := range kernelPaths { + wwlog.Printf(wwlog.DEBUG, "Checking for kernel path: %s\n", kernelPath) + if util.IsFile(kernelPath) { + return kernelPath + } } } @@ -43,31 +42,11 @@ func KernelFind(container string) string { } func KernelVersion(container string) string { - container_path := RootFsDir(container) - if container_path == "" { + wwlog.Printf(wwlog.DEBUG, "Finding kernel version inside container: %s\n", container) + kernel := KernelFind(container) + if kernel == "" { return "" } - module_lib_path := path.Join(container_path, "/lib/modules/*") - wwlog.Printf(wwlog.DEBUG, "Searching for kernel modules at: %s\n", module_lib_path) - kernelversions, err := filepath.Glob(module_lib_path) - if err != nil { - return "" - } - - if len(kernelversions) > 1 { - sort.Slice(kernelversions, func(i, j int) bool { - return kernelversions[i] > kernelversions[j] - }) - wwlog.Printf(wwlog.VERBOSE, "Multiple kernels found in container: %s\n", container) - - wwlog.Printf(wwlog.DEBUG, "Found lib path: '%s'\n", kernelversions[0]) - return path.Base(kernelversions[0]) - } else if len(kernelversions) == 1 { - wwlog.Printf(wwlog.DEBUG, "Found lib path: '%s'\n", kernelversions[0]) - - return path.Base(kernelversions[0]) - } - - return "" + return path.Base(path.Dir(kernel)) }