From 724e1de14c01c3fe87ba7f832e781155c3d92656 Mon Sep 17 00:00:00 2001 From: Gregory Kurtzer Date: Wed, 18 Nov 2020 21:52:03 -0800 Subject: [PATCH] Setup kernel support --- internal/app/wwctl/build/kernel/kernel.go | 1 - internal/app/wwctl/kernel/build/main.go | 55 ++++++++++++++++++++ internal/app/wwctl/kernel/build/root.go | 28 +++++++++++ internal/app/wwctl/kernel/export/main.go | 12 +++++ internal/app/wwctl/kernel/export/root.go | 22 ++++++++ internal/app/wwctl/kernel/imprt/main.go | 12 +++++ internal/app/wwctl/kernel/imprt/root.go | 22 ++++++++ internal/app/wwctl/kernel/list/main.go | 12 +++++ internal/app/wwctl/kernel/list/root.go | 21 ++++++++ internal/app/wwctl/kernel/root.go | 18 +++---- internal/app/wwctl/overlay/build/main.go | 2 - internal/pkg/kernel/kernel.go | 61 +++++++++++++++++++++++ 12 files changed, 253 insertions(+), 13 deletions(-) create mode 100644 internal/app/wwctl/kernel/build/main.go create mode 100644 internal/app/wwctl/kernel/build/root.go create mode 100644 internal/app/wwctl/kernel/export/main.go create mode 100644 internal/app/wwctl/kernel/export/root.go create mode 100644 internal/app/wwctl/kernel/imprt/main.go create mode 100644 internal/app/wwctl/kernel/imprt/root.go create mode 100644 internal/app/wwctl/kernel/list/main.go create mode 100644 internal/app/wwctl/kernel/list/root.go create mode 100644 internal/pkg/kernel/kernel.go diff --git a/internal/app/wwctl/build/kernel/kernel.go b/internal/app/wwctl/build/kernel/kernel.go index 0a68f278..08f3d850 100644 --- a/internal/app/wwctl/build/kernel/kernel.go +++ b/internal/app/wwctl/build/kernel/kernel.go @@ -65,7 +65,6 @@ func Build(nodeList []assets.NodeInfo, force bool) error { wwlog.Printf(wwlog.INFO, "%-35s: Done\n", "kmods-"+kernelVersion+".img") } } - } wwlog.SetIndent(0) diff --git a/internal/app/wwctl/kernel/build/main.go b/internal/app/wwctl/kernel/build/main.go new file mode 100644 index 00000000..cc11403a --- /dev/null +++ b/internal/app/wwctl/kernel/build/main.go @@ -0,0 +1,55 @@ +package build + +import ( + "github.com/hpcng/warewulf/internal/pkg/assets" + "github.com/hpcng/warewulf/internal/pkg/kernel" + "github.com/hpcng/warewulf/internal/pkg/wwlog" + "github.com/spf13/cobra" + "os" +) + +func CobraRunE(cmd *cobra.Command, args []string) error { + var nodes []assets.NodeInfo + set := make(map[string]int) + + if len(args) == 1 && ByNode == true { + var err error + nodes, err = assets.SearchByName(args[0]) + if err != nil { + wwlog.Printf(wwlog.ERROR, "Could not find nodes for search term: %s\n", args[0]) + os.Exit(1) + } + + for _, node := range nodes { + set[node.KernelVersion] ++ + } + + } else if BuildAll == true { + var err error + nodes, err = assets.FindAllNodes() + if err != nil { + wwlog.Printf(wwlog.ERROR, "Could not get list of nodes: %s\n", err) + os.Exit(1) + } + + for _, node := range nodes { + set[node.KernelVersion] ++ + } + + } else if len(args) == 1 { + set[args[0]] ++ + } else { + cmd.Usage() + os.Exit(1) + } + + for k := range set { + err := kernel.Build(k) + if err != nil { + wwlog.Printf(wwlog.ERROR, "%s\n", err) + os.Exit(1) + } + } + + return nil +} \ No newline at end of file diff --git a/internal/app/wwctl/kernel/build/root.go b/internal/app/wwctl/kernel/build/root.go new file mode 100644 index 00000000..bd504f76 --- /dev/null +++ b/internal/app/wwctl/kernel/build/root.go @@ -0,0 +1,28 @@ +package build + +import ( +"github.com/spf13/cobra" +) + +var ( + baseCmd = &cobra.Command{ + Use: "build (kernel version | node search pattern)", + Short: "Kernel Image Build", + Long: "Build kernel images", + RunE: CobraRunE, + Args: cobra.RangeArgs(0,1), + + } + BuildAll bool + ByNode bool +) + +func init() { + baseCmd.PersistentFlags().BoolVarP(&BuildAll, "all", "a", false, "Build all overlays (runtime and system)") + baseCmd.PersistentFlags().BoolVarP(&ByNode, "node", "n", false, "Build overlay for a particular node") +} + +// GetRootCommand returns the root cobra.Command for the application. +func GetCommand() *cobra.Command { + return baseCmd +} \ No newline at end of file diff --git a/internal/app/wwctl/kernel/export/main.go b/internal/app/wwctl/kernel/export/main.go new file mode 100644 index 00000000..dc10c347 --- /dev/null +++ b/internal/app/wwctl/kernel/export/main.go @@ -0,0 +1,12 @@ +package export + +import ( + "fmt" + "github.com/spf13/cobra" +) + +func CobraRunE(cmd *cobra.Command, args []string) error { + fmt.Printf("This command is coming soon...\n") + + return nil +} \ No newline at end of file diff --git a/internal/app/wwctl/kernel/export/root.go b/internal/app/wwctl/kernel/export/root.go new file mode 100644 index 00000000..f3491682 --- /dev/null +++ b/internal/app/wwctl/kernel/export/root.go @@ -0,0 +1,22 @@ +package export + +import "github.com/spf13/cobra" + +var ( + baseCmd = &cobra.Command{ + Use: "export", + Short: "Kernel Image Export", + Long: "Export kernel image", + RunE: CobraRunE, + Args: cobra.ExactArgs(1), + + } +) + +func init() { +} + +// GetRootCommand returns the root cobra.Command for the application. +func GetCommand() *cobra.Command { + return baseCmd +} \ No newline at end of file diff --git a/internal/app/wwctl/kernel/imprt/main.go b/internal/app/wwctl/kernel/imprt/main.go new file mode 100644 index 00000000..010b38af --- /dev/null +++ b/internal/app/wwctl/kernel/imprt/main.go @@ -0,0 +1,12 @@ +package imprt + +import ( + "fmt" + "github.com/spf13/cobra" +) + +func CobraRunE(cmd *cobra.Command, args []string) error { + fmt.Printf("This command is coming soon...\n") + + return nil +} \ No newline at end of file diff --git a/internal/app/wwctl/kernel/imprt/root.go b/internal/app/wwctl/kernel/imprt/root.go new file mode 100644 index 00000000..4642f655 --- /dev/null +++ b/internal/app/wwctl/kernel/imprt/root.go @@ -0,0 +1,22 @@ +package imprt + +import "github.com/spf13/cobra" + +var ( + baseCmd = &cobra.Command{ + Use: "import", + Short: "Kernel Image Import", + Long: "Import kernel image", + RunE: CobraRunE, + Args: cobra.ExactArgs(1), + + } +) + +func init() { +} + +// GetRootCommand returns the root cobra.Command for the application. +func GetCommand() *cobra.Command { + return baseCmd +} \ No newline at end of file diff --git a/internal/app/wwctl/kernel/list/main.go b/internal/app/wwctl/kernel/list/main.go new file mode 100644 index 00000000..65384b2a --- /dev/null +++ b/internal/app/wwctl/kernel/list/main.go @@ -0,0 +1,12 @@ +package list + +import ( + "fmt" + "github.com/spf13/cobra" +) + +func CobraRunE(cmd *cobra.Command, args []string) error { + fmt.Printf("This command is coming soon...\n") + + return nil +} \ No newline at end of file diff --git a/internal/app/wwctl/kernel/list/root.go b/internal/app/wwctl/kernel/list/root.go new file mode 100644 index 00000000..40b34c72 --- /dev/null +++ b/internal/app/wwctl/kernel/list/root.go @@ -0,0 +1,21 @@ +package list + +import "github.com/spf13/cobra" + +var ( + baseCmd = &cobra.Command{ + Use: "list", + Short: "List Installed Kernel Images", + Long: "List installed kernel images", + RunE: CobraRunE, + Args: cobra.ExactArgs(0), + } +) + +func init() { +} + +// GetRootCommand returns the root cobra.Command for the application. +func GetCommand() *cobra.Command { + return baseCmd +} \ No newline at end of file diff --git a/internal/app/wwctl/kernel/root.go b/internal/app/wwctl/kernel/root.go index 0a49f76e..5dd0bc7c 100644 --- a/internal/app/wwctl/kernel/root.go +++ b/internal/app/wwctl/kernel/root.go @@ -1,7 +1,10 @@ package kernel import ( - "fmt" + "github.com/hpcng/warewulf/internal/app/wwctl/kernel/build" + "github.com/hpcng/warewulf/internal/app/wwctl/kernel/export" + "github.com/hpcng/warewulf/internal/app/wwctl/kernel/imprt" + "github.com/hpcng/warewulf/internal/app/wwctl/kernel/list" "github.com/spf13/cobra" ) @@ -10,22 +13,17 @@ var ( Use: "kernel", Short: "Kernel Image Management", Long: "Management of Warewulf Kernels to be used for bootstrapping nodes", - RunE: CobraRunE, } - test bool ) func init() { - baseCmd.PersistentFlags().BoolVarP(&test, "test", "t", false, "Testing.") - + baseCmd.AddCommand(build.GetCommand()) + baseCmd.AddCommand(export.GetCommand()) + baseCmd.AddCommand(imprt.GetCommand()) + baseCmd.AddCommand(list.GetCommand()) } // GetRootCommand returns the root cobra.Command for the application. func GetCommand() *cobra.Command { return baseCmd -} - -func CobraRunE(cmd *cobra.Command, args []string) error { - fmt.Printf("Kernel: Hello World\n") - return nil } \ No newline at end of file diff --git a/internal/app/wwctl/overlay/build/main.go b/internal/app/wwctl/overlay/build/main.go index 9fd71277..7ca3d49b 100644 --- a/internal/app/wwctl/overlay/build/main.go +++ b/internal/app/wwctl/overlay/build/main.go @@ -9,8 +9,6 @@ import ( ) func CobraRunE(cmd *cobra.Command, args []string) error { - - var updateNodes []assets.NodeInfo if len(args) > 0 && BuildAll == false { diff --git a/internal/pkg/kernel/kernel.go b/internal/pkg/kernel/kernel.go new file mode 100644 index 00000000..b4c0c70b --- /dev/null +++ b/internal/pkg/kernel/kernel.go @@ -0,0 +1,61 @@ +package kernel + +import ( + "fmt" + "github.com/hpcng/warewulf/internal/pkg/config" + "github.com/hpcng/warewulf/internal/pkg/errors" + "github.com/hpcng/warewulf/internal/pkg/util" + "github.com/hpcng/warewulf/internal/pkg/wwlog" + "os" + "os/exec" + "path" +) + +func Build(kernelVersion string) error { + config := config.New() + + kernelImage := "/boot/vmlinuz-" + kernelVersion + kernelDrivers := "/lib/modules/" + kernelVersion + kernelDestination := config.KernelImage(kernelVersion) + driversDestination := config.KmodsImage(kernelVersion) + + // Create the destination paths just in case it doesn't exist + os.MkdirAll(path.Dir(kernelDestination), 0755) + os.MkdirAll(path.Dir(driversDestination), 0755) + + if util.IsFile(kernelImage) == false { + return errors.New("Could not locate kernel image: " + kernelImage) + } + + if util.IsDir(kernelDrivers) == false { + return errors.New("Could not locate kernel drivers: " + kernelDrivers) + } + + if _, err := os.Stat(kernelImage); err == nil { + if util.PathIsNewer(kernelImage, kernelDestination) { + wwlog.Printf(wwlog.INFO, "%-45s: Skipping, kernel is current\n", "vmlinuz-"+kernelVersion) + } else { + err := util.CopyFile(kernelImage, kernelDestination) + if err != nil { + return err + } + wwlog.Printf(wwlog.INFO, "%-45s: Done\n", "vmlinuz-"+kernelVersion) + } + } + + if _, err := os.Stat(kernelDrivers); err == nil { + if util.PathIsNewer(kernelDrivers, driversDestination) { + wwlog.Printf(wwlog.INFO, "%-45s: Skipping, drivers are current\n", "kmods-"+kernelVersion+".img") + + } else { + cmd := fmt.Sprintf("cd /; find .%s | cpio --quiet -o -H newc -F \"%s\"", kernelDrivers, driversDestination) + err := exec.Command("/bin/sh", "-c", cmd).Run() + if err != nil { + return err + } + wwlog.Printf(wwlog.INFO, "%-45s: Done\n", "kmods-"+kernelVersion+".img") + } + } + + return nil +}