1
0
out-of-tree/kernel.go

327 lines
7.2 KiB
Go
Raw Normal View History

// Copyright 2023 Mikhail Klementev. All rights reserved.
2018-11-17 20:24:09 +00:00
// Use of this source code is governed by a AGPLv3 license
// (or later) that can be found in the LICENSE file.
package main
import (
"errors"
"fmt"
"math"
"os"
"strings"
"time"
"github.com/naoina/toml"
"github.com/remeh/sizedwaitgroup"
2023-03-18 21:30:07 +00:00
"github.com/rs/zerolog/log"
"code.dumpstack.io/tools/out-of-tree/config"
"code.dumpstack.io/tools/out-of-tree/container"
"code.dumpstack.io/tools/out-of-tree/distro"
"code.dumpstack.io/tools/out-of-tree/kernel"
2018-11-17 20:24:09 +00:00
)
2023-01-31 07:13:33 +00:00
type KernelCmd struct {
2023-05-21 21:43:18 +00:00
NoDownload bool `help:"do not download qemu image while kernel generation"`
UseHost bool `help:"also use host kernels"`
Force bool `help:"force reinstall kernel"`
NoHeaders bool `help:"do not install kernel headers"`
Shuffle bool `help:"randomize kernels installation order"`
Retries int `help:"amount of tries for each kernel" default:"10"`
Threads int `help:"threads for parallel installation" default:"1"`
Update bool `help:"update container"`
2023-01-31 07:13:33 +00:00
2023-03-24 04:20:28 +00:00
List KernelListCmd `cmd:"" help:"list kernels"`
2023-05-10 07:52:44 +00:00
ListRemote KernelListRemoteCmd `cmd:"" help:"list remote kernels"`
2023-03-24 04:20:28 +00:00
Autogen KernelAutogenCmd `cmd:"" help:"generate kernels based on the current config"`
Genall KernelGenallCmd `cmd:"" help:"generate all kernels for distro"`
Install KernelInstallCmd `cmd:"" help:"install specific kernel"`
ConfigRegen KernelConfigRegenCmd `cmd:"" help:"regenerate config"`
2023-05-24 16:13:12 +00:00
shutdown bool
kcfg config.KernelConfig
2023-01-31 07:13:33 +00:00
}
func (cmd KernelCmd) UpdateConfig() (err error) {
kcfg := config.KernelConfig{}
if cmd.UseHost {
// Get host kernels
kcfg.Kernels, err = kernel.GenHostKernels(!cmd.NoDownload)
if err != nil {
return
}
}
for _, dist := range distro.List() {
var kernels []distro.KernelInfo
kernels, err = dist.Kernels()
if err != nil {
return
}
kcfg.Kernels = append(kcfg.Kernels, kernels...)
}
buf, err := toml.Marshal(&kcfg)
if err != nil {
return
}
err = os.WriteFile(config.File("kernels.toml"), buf, os.ModePerm)
if err != nil {
return
}
log.Info().Msgf("kernels.toml successfully updated")
return
}
2023-05-24 16:13:12 +00:00
func (cmd *KernelCmd) GenKernel(km config.Target, pkg string, max *int) {
flog := log.With().
Str("kernel", pkg).
Str("distro", km.Distro.String()).
Logger()
reinstall := false
for _, kinfo := range cmd.kcfg.Kernels {
if strings.Contains(pkg, kinfo.KernelVersion) {
if !cmd.Force {
flog.Info().Msg("already installed")
return
}
reinstall = true
break
}
}
if reinstall {
flog.Info().Msg("reinstall")
} else {
flog.Info().Msg("install")
}
2023-05-24 16:13:12 +00:00
var attempt int
for {
attempt++
if cmd.shutdown {
return
}
err := km.Distro.Install(pkg, !cmd.NoHeaders)
if err == nil {
*max--
flog.Info().Msg("success")
2023-05-24 16:13:12 +00:00
break
} else if attempt >= cmd.Retries {
flog.Error().Err(err).Msg("install kernel")
flog.Debug().Msg("skip")
2023-05-24 16:13:12 +00:00
break
} else {
flog.Warn().Err(err).Msg("install kernel")
2023-05-24 16:13:12 +00:00
time.Sleep(time.Second)
flog.Info().Msg("retry")
2023-05-24 16:13:12 +00:00
}
}
}
func (cmd *KernelCmd) Generate(g *Globals, km config.Target, max int) (err error) {
2023-05-24 16:43:25 +00:00
if cmd.Update {
container.Update = true
}
cmd.kcfg, err = config.ReadKernelConfig(g.Config.Kernels)
if err != nil {
log.Debug().Err(err).Msg("read kernels config")
}
container.Commands = g.Config.Docker.Commands
container.Registry = g.Config.Docker.Registry
log.Info().Msgf("Generating for target %v", km)
_, err = kernel.GenRootfsImage(container.Image{Name: km.DockerName()},
!cmd.NoDownload)
2023-05-24 16:13:12 +00:00
if err != nil || cmd.shutdown {
return
}
pkgs, err := kernel.MatchPackages(km)
2023-05-24 16:13:12 +00:00
if err != nil || cmd.shutdown {
return
}
if cmd.Shuffle {
pkgs = kernel.ShuffleStrings(pkgs)
}
swg := sizedwaitgroup.New(cmd.Threads)
for i, pkg := range pkgs {
2023-05-24 16:13:12 +00:00
if cmd.shutdown {
err = nil
return
}
swg.Add()
if max <= 0 {
log.Print("Max is reached")
swg.Done()
break
}
log.Info().Msgf("%d/%d %s", i+1, len(pkgs), pkg)
go func(p string) {
defer swg.Done()
2023-05-24 16:13:12 +00:00
cmd.GenKernel(km, p, &max)
}(pkg)
}
swg.Wait()
return
}
2023-01-31 07:13:33 +00:00
type KernelListCmd struct{}
func (cmd *KernelListCmd) Run(g *Globals) (err error) {
kcfg, err := config.ReadKernelConfig(g.Config.Kernels)
if err != nil {
log.Debug().Err(err).Msg("read kernel config")
2023-01-31 07:13:33 +00:00
}
2018-11-17 20:24:09 +00:00
if len(kcfg.Kernels) == 0 {
return errors.New("No kernels found")
}
2023-01-31 07:13:33 +00:00
2018-11-17 20:24:09 +00:00
for _, k := range kcfg.Kernels {
fmt.Println(k.Distro.ID, k.Distro.Release, k.KernelRelease)
2018-11-17 20:24:09 +00:00
}
2023-01-31 07:13:33 +00:00
2018-11-17 20:24:09 +00:00
return
}
2023-05-10 07:52:44 +00:00
type KernelListRemoteCmd struct {
Distro string `required:"" help:"distribution"`
Ver string `required:"" help:"distro version"`
}
func (cmd *KernelListRemoteCmd) Run(kernelCmd *KernelCmd, g *Globals) (err error) {
distroType, err := distro.NewID(cmd.Distro)
2023-05-10 07:52:44 +00:00
if err != nil {
return
}
km := config.Target{
Distro: distro.Distro{ID: distroType, Release: cmd.Ver},
Kernel: config.Kernel{Regex: ".*"},
2023-05-10 07:52:44 +00:00
}
_, err = kernel.GenRootfsImage(container.Image{Name: km.DockerName()}, false)
2023-05-10 07:52:44 +00:00
if err != nil {
return
}
container.Registry = g.Config.Docker.Registry
container.Commands = g.Config.Docker.Commands
2023-05-10 07:52:44 +00:00
pkgs, err := kernel.MatchPackages(km)
// error check skipped on purpose
2023-05-10 07:52:44 +00:00
for _, k := range pkgs {
fmt.Println(k)
}
return
}
2023-01-31 07:13:33 +00:00
type KernelAutogenCmd struct {
2023-05-21 21:43:18 +00:00
Max int `help:"download kernels from set defined by regex in release_mask, but no more than X for each of release_mask" default:"100500"`
2023-01-31 07:13:33 +00:00
}
2023-01-31 09:05:43 +00:00
func (cmd KernelAutogenCmd) Run(kernelCmd *KernelCmd, g *Globals) (err error) {
2023-01-31 07:13:33 +00:00
ka, err := config.ReadArtifactConfig(g.WorkDir + "/.out-of-tree.toml")
if err != nil {
return
}
2023-05-24 16:13:12 +00:00
kernel.SetSigintHandler(&kernelCmd.shutdown)
2023-04-07 20:52:45 +00:00
for _, sk := range ka.Targets {
if sk.Distro.Release == "" {
2023-01-31 07:13:33 +00:00
err = errors.New("Please set distro_release")
return
}
2023-05-24 16:13:12 +00:00
err = kernelCmd.Generate(g, sk, cmd.Max)
2023-01-31 07:13:33 +00:00
if err != nil {
return
}
2023-05-24 16:13:12 +00:00
if kernelCmd.shutdown {
2023-04-07 20:52:45 +00:00
break
}
2023-01-31 07:13:33 +00:00
}
return kernelCmd.UpdateConfig()
2023-01-31 07:13:33 +00:00
}
type KernelGenallCmd struct {
Distro string `required:"" help:"distribution"`
Ver string `required:"" help:"distro version"`
}
func (cmd *KernelGenallCmd) Run(kernelCmd *KernelCmd, g *Globals) (err error) {
distroType, err := distro.NewID(cmd.Distro)
2023-01-31 07:13:33 +00:00
if err != nil {
return
}
2023-05-24 16:13:12 +00:00
kernel.SetSigintHandler(&kernelCmd.shutdown)
2023-04-07 20:52:45 +00:00
km := config.Target{
Distro: distro.Distro{ID: distroType, Release: cmd.Ver},
Kernel: config.Kernel{Regex: ".*"},
2023-01-31 07:13:33 +00:00
}
2023-05-24 16:13:12 +00:00
err = kernelCmd.Generate(g, km, math.MaxUint32)
2023-03-22 18:21:21 +00:00
if err != nil {
return
}
return kernelCmd.UpdateConfig()
2023-03-22 18:21:21 +00:00
}
type KernelInstallCmd struct {
Distro string `required:"" help:"distribution"`
Ver string `required:"" help:"distro version"`
Kernel string `required:"" help:"kernel release mask"`
}
func (cmd *KernelInstallCmd) Run(kernelCmd *KernelCmd, g *Globals) (err error) {
distroType, err := distro.NewID(cmd.Distro)
2023-03-22 18:21:21 +00:00
if err != nil {
return
}
2023-05-24 16:13:12 +00:00
kernel.SetSigintHandler(&kernelCmd.shutdown)
2023-04-07 20:52:45 +00:00
km := config.Target{
Distro: distro.Distro{ID: distroType, Release: cmd.Ver},
Kernel: config.Kernel{Regex: cmd.Kernel},
2023-03-22 18:21:21 +00:00
}
2023-05-24 16:13:12 +00:00
err = kernelCmd.Generate(g, km, math.MaxUint32)
2023-01-31 07:13:33 +00:00
if err != nil {
return
}
return kernelCmd.UpdateConfig()
2023-01-31 07:13:33 +00:00
}
2023-03-24 04:20:28 +00:00
type KernelConfigRegenCmd struct{}
func (cmd *KernelConfigRegenCmd) Run(kernelCmd *KernelCmd, g *Globals) (err error) {
return kernelCmd.UpdateConfig()
}