From 4eabb957bb55b2700eae2dff9cfcfece9db195e3 Mon Sep 17 00:00:00 2001 From: Christian Goll Date: Wed, 2 Feb 2022 11:40:57 +0100 Subject: [PATCH] added option for automatic kernel detection --- internal/app/wwctl/kernel/imprt/main.go | 32 ++++++++++++++++++++----- internal/app/wwctl/kernel/imprt/root.go | 12 ++++++---- internal/pkg/kernel/kernel.go | 27 +++++++++++++++++++++ 3 files changed, 60 insertions(+), 11 deletions(-) diff --git a/internal/app/wwctl/kernel/imprt/main.go b/internal/app/wwctl/kernel/imprt/main.go index f2412240..97e3e219 100644 --- a/internal/app/wwctl/kernel/imprt/main.go +++ b/internal/app/wwctl/kernel/imprt/main.go @@ -14,13 +14,15 @@ import ( ) func CobraRunE(cmd *cobra.Command, args []string) error { - - // Checking if container flag was set, then overwriting OptRoot - kernelVersion := args[0] - kernelName := kernelVersion - if len(args) > 1 { - kernelName = args[1] + if len(args) == 0 && !OptDetect { + wwlog.Printf(wwlog.ERROR, "the '--detect' flag is needed, if no kernel version is suppiled") + os.Exit(1) } + if OptDetect && (OptRoot == "" || OptContainer == "") { + wwlog.Printf(wwlog.ERROR, "the '--detect flag needs the '--container' or '--root' flag") + os.Exit(1) + } + // Checking if container flag was set, then overwriting OptRoot if OptContainer != "" { if container.ValidSource(OptContainer) { OptRoot = container.RootFsDir(OptContainer) @@ -29,6 +31,24 @@ func CobraRunE(cmd *cobra.Command, args []string) error { os.Exit(1) } } + + var kernelVersion string + var err error + if len(args) > 0 { + kernelVersion = args[0] + } else { + kernelVersion, err = kernel.FindKernelVersion(OptRoot) + if err != nil { + wwlog.Printf(wwlog.ERROR, "could not detect kernel under %s\n", OptRoot) + os.Exit(1) + } + } + kernelName := kernelVersion + if len(args) > 1 { + kernelName = args[1] + } else if OptDetect && (OptContainer != "") { + kernelName = OptContainer + } output, err := kernel.Build(kernelVersion, kernelName, OptRoot) if err != nil { wwlog.Printf(wwlog.ERROR, "Failed building kernel: %s\n", err) diff --git a/internal/app/wwctl/kernel/imprt/root.go b/internal/app/wwctl/kernel/imprt/root.go index 7d398264..c8521a7b 100644 --- a/internal/app/wwctl/kernel/imprt/root.go +++ b/internal/app/wwctl/kernel/imprt/root.go @@ -10,17 +10,18 @@ import ( var ( baseCmd = &cobra.Command{ DisableFlagsInUseLine: true, - Use: "import [OPTIONS] KERNEL", - Short: "Import Kernel version into Warewulf", - Long: "This will import a boot KERNEL version from the control node into Warewulf", - RunE: CobraRunE, - Args: cobra.MinimumNArgs(1), + Use: "import [OPTIONS] KERNEL", + Short: "Import Kernel version into Warewulf", + Long: "This will import a boot KERNEL version from the control node into Warewulf", + RunE: CobraRunE, + Args: cobra.MinimumNArgs(0), } BuildAll bool ByNode bool SetDefault bool OptRoot string OptContainer string + OptDetect bool ) func init() { @@ -36,6 +37,7 @@ func init() { if err != nil { log.Println(err) } + baseCmd.PersistentFlags().BoolVarP(&OptDetect, "detect", "D", false, "Try to detect the kernel version in an automated way, needs the -C or -r option") } // GetRootCommand returns the root cobra.Command for the application. diff --git a/internal/pkg/kernel/kernel.go b/internal/pkg/kernel/kernel.go index 94f3d5d1..0841d927 100644 --- a/internal/pkg/kernel/kernel.go +++ b/internal/pkg/kernel/kernel.go @@ -8,6 +8,8 @@ import ( "os" "os/exec" "path" + "path/filepath" + "regexp" "github.com/pkg/errors" @@ -228,3 +230,28 @@ func DeleteKernel(name string) error { wwlog.Printf(wwlog.VERBOSE, "Removing path: %s\n", fullPath) return os.RemoveAll(fullPath) } + +func FindKernelVersion(root string) (string, error) { + for _, searchPath := range kernelSearchPaths { + testPattern := fmt.Sprintf(path.Join(root, searchPath), `*`) + wwlog.Printf(wwlog.VERBOSE, "Looking for kernel version with pattern at: %s\n", testPattern) + potentialKernel, _ := filepath.Glob(testPattern) + if len(potentialKernel) == 0 { + continue + } + for _, foundKernel := range potentialKernel { + wwlog.Printf(wwlog.VERBOSE, "Parsing out kernel version for %s\n", foundKernel) + re := regexp.MustCompile(fmt.Sprintf(path.Join(root, searchPath), `([\w\d-\.]*)`)) + version := re.FindAllStringSubmatch(foundKernel, -1) + if version == nil { + return "", fmt.Errorf("could not parse kernel version") + } + wwlog.Printf(wwlog.VERBOSE, "found kernel version %s\n", version) + return version[0][1], nil + + } + + } + return "", fmt.Errorf("could not find kernel version") + +}