From a6c793cacaa32ea566704c4cafe4f2e441486465 Mon Sep 17 00:00:00 2001 From: Gregory Kurtzer Date: Wed, 16 Feb 2022 04:48:17 +0000 Subject: [PATCH] Fixes for kernel identification within the container --- internal/app/wwctl/container/list/main.go | 7 +-- internal/pkg/container/kernel.go | 72 +++++++++++++++-------- 2 files changed, 52 insertions(+), 27 deletions(-) diff --git a/internal/app/wwctl/container/list/main.go b/internal/app/wwctl/container/list/main.go index f228a5f9..b9fce309 100644 --- a/internal/app/wwctl/container/list/main.go +++ b/internal/app/wwctl/container/list/main.go @@ -3,7 +3,6 @@ package list import ( "fmt" "os" - "path" "github.com/hpcng/warewulf/internal/pkg/container" "github.com/hpcng/warewulf/internal/pkg/node" @@ -27,14 +26,14 @@ func CobraRunE(cmd *cobra.Command, args []string) error { nodemap[n.ContainerName.Get()]++ } - fmt.Printf("%-25s %-6s %-6s\n", "CONTAINER NAME", "NODES", "KERNEL") + fmt.Printf("%-25s %-6s %-6s\n", "CONTAINER NAME", "NODES", "KERNEL VERSION") for _, source := range sources { if nodemap[source] == 0 { nodemap[source] = 0 } - kernel := container.KernelFind(source) - fmt.Printf("%-25s %-6d %s\n", source, nodemap[source], path.Base(kernel)) + kernelVersion := container.KernelVersion(source) + fmt.Printf("%-25s %-6d %s\n", source, nodemap[source], kernelVersion) } return nil diff --git a/internal/pkg/container/kernel.go b/internal/pkg/container/kernel.go index e1279e60..9a9cd8c2 100644 --- a/internal/pkg/container/kernel.go +++ b/internal/pkg/container/kernel.go @@ -1,18 +1,22 @@ 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-*", - "/lib/modules/*/vmlinuz*", + `/boot/vmlinuz-%s`, + `/boot/vmlinuz-%s.gz`, + `/lib/modules/%s/vmlinuz`, + `/lib/modules/%s/vmlinuz.gz`, } ) @@ -22,26 +26,48 @@ func KernelFind(container string) string { return "" } - for _, searchPath := range kernelSearchPaths { - - check_path := path.Join(container_path, searchPath) - - wwlog.Printf(wwlog.DEBUG, "Searching for kernel(s) at: %s\n", check_path) - kernels, err := filepath.Glob(check_path) - if err != nil { - return "" - } - - if len(kernels) > 1 { - sort.Slice(kernels, func(i, j int) bool { - return kernels[i] > kernels[j] - }) - wwlog.Printf(wwlog.VERBOSE, "Multiple kernels found in container: %s\n", container) - return kernels[0] - } else if len(kernels) == 1 { - return kernels[0] - } - + kernelVersion := KernelVersion(container) + if kernelVersion == "" { + return "" } - return "NOT_FOUND" + + 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) + } + } + + return "" +} + +func KernelVersion(container string) string { + container_path := RootFsDir(container) + if container_path == "" { + 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 "" }