handling k ernels by nme

version is also stored, kernel name defaults to its version
This commit is contained in:
Christian Goll
2021-09-20 12:22:36 +02:00
parent acf2990936
commit dfd6ce3735
3 changed files with 104 additions and 61 deletions

View File

@@ -14,58 +14,57 @@ import (
) )
func CobraRunE(cmd *cobra.Command, args []string) error { func CobraRunE(cmd *cobra.Command, args []string) error {
for _, arg := range args {
// Checking if container flag was set, then overwriting OptRoot // Checking if container flag was set, then overwriting OptRoot
if OptContainer != "" { kernelVersion := args[0]
if container.ValidSource(OptContainer) { kernelName := args[1]
OptRoot = container.RootFsDir(OptContainer) if kernelName == "" {
} else { kernelName = kernelVersion
wwlog.Printf(wwlog.ERROR, " %s is not a valid container", OptContainer) }
os.Exit(1) if OptContainer != "" {
} if container.ValidSource(OptContainer) {
} OptRoot = container.RootFsDir(OptContainer)
output, err := kernel.Build(arg, OptRoot)
if err != nil {
wwlog.Printf(wwlog.ERROR, "Failed building kernel: %s\n", err)
os.Exit(1)
} else { } else {
fmt.Printf("%s: %s\n", arg, output) wwlog.Printf(wwlog.ERROR, " %s is not a valid container", OptContainer)
os.Exit(1)
} }
} }
output, err := kernel.Build(kernelVersion, kernelName, OptRoot)
if err != nil {
wwlog.Printf(wwlog.ERROR, "Failed building kernel: %s\n", err)
os.Exit(1)
} else {
fmt.Printf("%s: %s\n", kernelName, output)
}
if SetDefault { if SetDefault {
if len(args) != 1 {
wwlog.Printf(wwlog.ERROR, "Can only set default for one kernel version\n")
} else {
nodeDB, err := node.New()
if err != nil {
wwlog.Printf(wwlog.ERROR, "Could not open node configuration: %s\n", err)
os.Exit(1)
}
//TODO: Don't loop through profiles, instead have a nodeDB function that goes directly to the map nodeDB, err := node.New()
profiles, _ := nodeDB.FindAllProfiles() if err != nil {
for _, profile := range profiles { wwlog.Printf(wwlog.ERROR, "Could not open node configuration: %s\n", err)
wwlog.Printf(wwlog.DEBUG, "Looking for profile default: %s\n", profile.Id.Get()) os.Exit(1)
if profile.Id.Get() == "default" { }
wwlog.Printf(wwlog.DEBUG, "Found profile default, setting kernel version to: %s\n", args[0]) //TODO: Don't loop through profiles, instead have a nodeDB function that goes directly to the map
profile.KernelVersion.Set(args[0]) profiles, _ := nodeDB.FindAllProfiles()
err := nodeDB.ProfileUpdate(profile) for _, profile := range profiles {
if err != nil { wwlog.Printf(wwlog.DEBUG, "Looking for profile default: %s\n", profile.Id.Get())
return errors.Wrap(err, "failed to update node profile") if profile.Id.Get() == "default" {
} wwlog.Printf(wwlog.DEBUG, "Found profile default, setting kernel version to: %s\n", args[0])
profile.KernelVersion.Set(args[0])
err := nodeDB.ProfileUpdate(profile)
if err != nil {
return errors.Wrap(err, "failed to update node profile")
} }
} }
err = nodeDB.Persist() }
if err != nil { err = nodeDB.Persist()
return errors.Wrap(err, "failed to persist nodedb") if err != nil {
} return errors.Wrap(err, "failed to persist nodedb")
fmt.Printf("Set default kernel version to: %s\n", args[0]) }
fmt.Printf("Set default kernel version to: %s\n", args[0])
err = warewulfd.DaemonReload() err = warewulfd.DaemonReload()
if err != nil { if err != nil {
return errors.Wrap(err, "failed to reload warewulf daemon") return errors.Wrap(err, "failed to reload warewulf daemon")
}
} }
} }

View File

@@ -26,9 +26,9 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
nodemap[n.KernelVersion.Get()]++ nodemap[n.KernelVersion.Get()]++
} }
fmt.Printf("%-35s %-6s\n", "VNFS NAME", "NODES") fmt.Printf("%-35s %-6s\n", "KERNEL NAME","KERNEL VERSION" "NODES")
for _, k := range kernels { for _, k := range kernels {
fmt.Printf("%-35s %6d\n", k, nodemap[k]) fmt.Printf("%-35s %-25s %6d\n", k, kernel.GetVersion(k), nodemap[k])
} }
return nil return nil

View File

@@ -2,6 +2,7 @@ package kernel
import ( import (
"fmt" "fmt"
"io"
"io/ioutil" "io/ioutil"
"os" "os"
"os/exec" "os/exec"
@@ -18,32 +19,58 @@ func ParentDir() string {
return path.Join(config.LocalStateDir, "provision/kernel") return path.Join(config.LocalStateDir, "provision/kernel")
} }
func KernelImage(kernelVersion string) string { func KernelImage(kernelName string) string {
if kernelVersion == "" { if kernelName == "" {
wwlog.Printf(wwlog.ERROR, "Kernel Version is not defined\n") wwlog.Printf(wwlog.ERROR, "Kernel Name is not defined\n")
return "" return ""
} }
if !util.ValidString(kernelVersion, "^[a-zA-Z0-9-._]+$") { if !util.ValidString(kernelName, "^[a-zA-Z0-9-._]+$") {
wwlog.Printf(wwlog.ERROR, "Runtime overlay name contains illegal characters: %s\n", kernelVersion) wwlog.Printf(wwlog.ERROR, "Runtime overlay name contains illegal characters: %s\n", kernelName)
return "" return ""
} }
return path.Join(ParentDir(), kernelVersion, "vmlinuz") return path.Join(ParentDir(), kernelName, "vmlinuz")
} }
func KmodsImage(kernelVersion string) string { func GetKernelVersion(kernelName string) string {
if kernelVersion == "" { if kernelName == "" {
wwlog.Printf(wwlog.ERROR, "Kernel Version is not defined\n") wwlog.Printf(wwlog.ERROR, "Kernel Name is not defined\n")
return ""
}
kernelVersion, err := ioutil.ReadFile(path.Join(ParentDir(), kernelName, "version"))
if err != nil {
return ""
}
return string(kernelVersion)
}
func KmodsImage(kernelName string) string {
if kernelName == "" {
wwlog.Printf(wwlog.ERROR, "Kernel Name is not defined\n")
return "" return ""
} }
if !util.ValidString(kernelVersion, "^[a-zA-Z0-9-._]+$") { if !util.ValidString(kernelName, "^[a-zA-Z0-9-._]+$") {
wwlog.Printf(wwlog.ERROR, "Runtime overlay name contains illegal characters: %s\n", kernelVersion) wwlog.Printf(wwlog.ERROR, "Runtime overlay name contains illegal characters: %s\n", kernelName)
return "" return ""
} }
return path.Join(ParentDir(), kernelVersion, "kmods.img") return path.Join(ParentDir(), kernelName, "kmods.img")
}
func KernelVersion(kernelName string) string {
if kernelName == "" {
wwlog.Printf(wwlog.ERROR, "Kernel Name is not defined\n")
return ""
}
if !util.ValidString(kernelName, "^[a-zA-Z0-9-._]+$") {
wwlog.Printf(wwlog.ERROR, "Runtime overlay name contains illegal characters: %s\n", kernelName)
return ""
}
return path.Join(ParentDir(), kernelName, "version")
} }
func ListKernels() ([]string, error) { func ListKernels() ([]string, error) {
@@ -71,11 +98,12 @@ func ListKernels() ([]string, error) {
return ret, nil return ret, nil
} }
func Build(kernelVersion string, root string) (string, error) { func Build(kernelVersion string, root string, kernelName string) (string, error) {
kernelImage := path.Join(root, "/boot/vmlinuz-"+kernelVersion) kernelImage := path.Join(root, "/boot/vmlinuz-"+kernelVersion)
kernelDrivers := path.Join(root, "/lib/modules/"+kernelVersion) kernelDrivers := path.Join(root, "/lib/modules/"+kernelVersion)
kernelDestination := KernelImage(kernelVersion) kernelDestination := KernelImage(kernelName)
driversDestination := KmodsImage(kernelVersion) driversDestination := KmodsImage(kernelName)
versionDestination := KernelVersion(kernelName)
// Create the destination paths just in case it doesn't exist // Create the destination paths just in case it doesn't exist
err := os.MkdirAll(path.Dir(kernelDestination), 0755) err := os.MkdirAll(path.Dir(kernelDestination), 0755)
@@ -88,6 +116,11 @@ func Build(kernelVersion string, root string) (string, error) {
return "", fmt.Errorf("failed to create driver dest: %s", err) return "", fmt.Errorf("failed to create driver dest: %s", err)
} }
err = os.MkdirAll(path.Dir(versionDestination), 0755)
if err != nil {
return "", fmt.Errorf("failed to create version dest: %s", err)
}
if !util.IsFile(kernelImage) { if !util.IsFile(kernelImage) {
return "", errors.New("Could not locate kernel image") return "", errors.New("Could not locate kernel image")
} }
@@ -123,6 +156,17 @@ func Build(kernelVersion string, root string) (string, error) {
} }
} }
wwlog.Printf(wwlog.VERBOSE, "Creating version file\n")
file, err := os.Create(versionDestination)
if err != nil {
return "", errors.Wrap(err, "Failed to create version file")
}
defer file.Close()
_, err = io.WriteString(file, kernelVersion)
if err != nil {
return "", errors.Wrap(err, "Could not write kernel version")
}
file.Sync()
return "Done", nil return "Done", nil
} }