2023-03-22 17:45:56 +00:00
|
|
|
// 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"
|
2023-03-22 17:45:56 +00:00
|
|
|
"math"
|
2023-05-13 10:47:47 +00:00
|
|
|
|
2023-03-18 21:30:07 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2019-08-16 05:05:26 +00:00
|
|
|
|
|
|
|
"code.dumpstack.io/tools/out-of-tree/config"
|
2023-05-13 10:14:45 +00:00
|
|
|
"code.dumpstack.io/tools/out-of-tree/container"
|
2023-05-13 10:47:47 +00:00
|
|
|
"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-04-05 16:24:45 +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"`
|
2023-04-05 16:26:26 +00:00
|
|
|
Retries int64 `help:"amount of tries for each kernel" default:"10"`
|
2023-05-10 08:09:53 +00:00
|
|
|
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-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 {
|
2023-03-22 17:45:56 +00:00
|
|
|
log.Debug().Err(err).Msg("read kernel config")
|
2023-01-31 07:13:33 +00:00
|
|
|
}
|
2019-08-12 22:58:34 +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.DistroType, k.DistroRelease, k.KernelRelease)
|
|
|
|
}
|
2023-01-31 07:13:33 +00:00
|
|
|
|
2018-11-17 20:24:09 +00:00
|
|
|
return
|
|
|
|
}
|
2018-11-28 22:41:17 +00:00
|
|
|
|
2023-05-10 07:52:44 +00:00
|
|
|
type KernelListRemoteCmd struct {
|
|
|
|
Distro string `required:"" help:"distribution"`
|
|
|
|
Ver string `required:"" help:"distro version"`
|
|
|
|
}
|
|
|
|
|
2023-05-10 08:09:53 +00:00
|
|
|
func (cmd *KernelListRemoteCmd) Run(kernelCmd *KernelCmd, g *Globals) (err error) {
|
2023-05-10 07:52:44 +00:00
|
|
|
distroType, err := config.NewDistroType(cmd.Distro)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
km := config.KernelMask{
|
|
|
|
DistroType: distroType,
|
|
|
|
DistroRelease: cmd.Ver,
|
|
|
|
ReleaseMask: ".*",
|
|
|
|
}
|
|
|
|
|
2023-05-13 10:47:47 +00:00
|
|
|
_, err = kernel.GenRootfsImage(container.Image{Name: km.DockerName()}, false)
|
2023-05-10 07:52:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-13 10:47:47 +00:00
|
|
|
err = kernel.GenerateBaseDockerImage(
|
2023-05-10 07:52:44 +00:00
|
|
|
g.Config.Docker.Registry,
|
|
|
|
g.Config.Docker.Commands,
|
|
|
|
km,
|
2023-05-10 08:09:53 +00:00
|
|
|
kernelCmd.Update,
|
2023-05-10 07:52:44 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-13 10:47:47 +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-04-05 16:24:45 +00:00
|
|
|
Max int64 `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-04-07 20:52:45 +00:00
|
|
|
shutdown := false
|
2023-05-13 10:47:47 +00:00
|
|
|
kernel.SetSigintHandler(&shutdown)
|
2023-04-07 20:52:45 +00:00
|
|
|
|
2023-01-31 07:13:33 +00:00
|
|
|
for _, sk := range ka.SupportedKernels {
|
|
|
|
if sk.DistroRelease == "" {
|
|
|
|
err = errors.New("Please set distro_release")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-13 10:47:47 +00:00
|
|
|
err = kernel.GenerateKernels(sk,
|
2023-03-29 17:15:27 +00:00
|
|
|
g.Config.Docker.Registry,
|
|
|
|
g.Config.Docker.Commands,
|
2023-04-05 16:24:45 +00:00
|
|
|
cmd.Max, kernelCmd.Retries,
|
2023-03-29 17:15:27 +00:00
|
|
|
!kernelCmd.NoDownload,
|
|
|
|
kernelCmd.Force,
|
|
|
|
!kernelCmd.NoHeaders,
|
2023-04-05 11:29:31 +00:00
|
|
|
kernelCmd.Shuffle,
|
2023-05-10 08:09:53 +00:00
|
|
|
kernelCmd.Update,
|
2023-04-07 20:52:45 +00:00
|
|
|
&shutdown,
|
2023-03-29 17:15:27 +00:00
|
|
|
)
|
2023-01-31 07:13:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2023-04-07 20:52:45 +00:00
|
|
|
if shutdown {
|
|
|
|
break
|
|
|
|
}
|
2023-01-31 07:13:33 +00:00
|
|
|
}
|
|
|
|
|
2023-05-13 10:47:47 +00:00
|
|
|
return kernel.UpdateKernelsCfg(kernelCmd.UseHost, !kernelCmd.NoDownload)
|
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 := config.NewDistroType(cmd.Distro)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-07 20:52:45 +00:00
|
|
|
shutdown := false
|
2023-05-13 10:47:47 +00:00
|
|
|
kernel.SetSigintHandler(&shutdown)
|
2023-04-07 20:52:45 +00:00
|
|
|
|
2023-01-31 07:13:33 +00:00
|
|
|
km := config.KernelMask{
|
|
|
|
DistroType: distroType,
|
|
|
|
DistroRelease: cmd.Ver,
|
|
|
|
ReleaseMask: ".*",
|
|
|
|
}
|
2023-05-13 10:47:47 +00:00
|
|
|
err = kernel.GenerateKernels(km,
|
2023-02-27 18:53:06 +00:00
|
|
|
g.Config.Docker.Registry,
|
|
|
|
g.Config.Docker.Commands,
|
2023-04-05 16:24:45 +00:00
|
|
|
math.MaxUint32, kernelCmd.Retries,
|
2023-03-22 18:21:21 +00:00
|
|
|
!kernelCmd.NoDownload,
|
|
|
|
kernelCmd.Force,
|
2023-03-29 17:15:27 +00:00
|
|
|
!kernelCmd.NoHeaders,
|
2023-04-05 11:29:31 +00:00
|
|
|
kernelCmd.Shuffle,
|
2023-05-10 08:09:53 +00:00
|
|
|
kernelCmd.Update,
|
2023-04-07 20:52:45 +00:00
|
|
|
&shutdown,
|
2023-03-22 18:21:21 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-13 10:47:47 +00:00
|
|
|
return kernel.UpdateKernelsCfg(kernelCmd.UseHost, !kernelCmd.NoDownload)
|
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 := config.NewDistroType(cmd.Distro)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-07 20:52:45 +00:00
|
|
|
shutdown := false
|
2023-05-13 10:47:47 +00:00
|
|
|
kernel.SetSigintHandler(&shutdown)
|
2023-04-07 20:52:45 +00:00
|
|
|
|
2023-03-22 18:21:21 +00:00
|
|
|
km := config.KernelMask{
|
|
|
|
DistroType: distroType,
|
|
|
|
DistroRelease: cmd.Ver,
|
|
|
|
ReleaseMask: cmd.Kernel,
|
|
|
|
}
|
2023-05-13 10:47:47 +00:00
|
|
|
err = kernel.GenerateKernels(km,
|
2023-03-22 18:21:21 +00:00
|
|
|
g.Config.Docker.Registry,
|
|
|
|
g.Config.Docker.Commands,
|
2023-04-05 16:24:45 +00:00
|
|
|
math.MaxUint32, kernelCmd.Retries,
|
2023-02-27 18:53:06 +00:00
|
|
|
!kernelCmd.NoDownload,
|
2023-03-22 18:20:45 +00:00
|
|
|
kernelCmd.Force,
|
2023-03-29 17:15:27 +00:00
|
|
|
!kernelCmd.NoHeaders,
|
2023-04-05 11:29:31 +00:00
|
|
|
kernelCmd.Shuffle,
|
2023-05-10 08:09:53 +00:00
|
|
|
kernelCmd.Update,
|
2023-04-07 20:52:45 +00:00
|
|
|
&shutdown,
|
2023-02-27 18:53:06 +00:00
|
|
|
)
|
2023-01-31 07:13:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-13 10:47:47 +00:00
|
|
|
return kernel.UpdateKernelsCfg(kernelCmd.UseHost, !kernelCmd.NoDownload)
|
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) {
|
2023-05-13 10:47:47 +00:00
|
|
|
return kernel.UpdateKernelsCfg(kernelCmd.UseHost, !kernelCmd.NoDownload)
|
2018-12-02 19:54:09 +00:00
|
|
|
}
|