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"
|
2018-11-28 22:41:17 +00:00
|
|
|
|
"io/ioutil"
|
2023-03-22 17:45:56 +00:00
|
|
|
|
"math"
|
2019-08-12 22:58:34 +00:00
|
|
|
|
"math/rand"
|
2018-11-28 22:41:17 +00:00
|
|
|
|
"os"
|
|
|
|
|
"os/exec"
|
2023-04-07 20:52:45 +00:00
|
|
|
|
"os/signal"
|
2018-11-28 22:41:17 +00:00
|
|
|
|
"os/user"
|
|
|
|
|
"regexp"
|
2019-12-25 14:36:32 +00:00
|
|
|
|
"runtime"
|
2018-11-28 22:41:17 +00:00
|
|
|
|
"strings"
|
2018-12-08 02:53:29 +00:00
|
|
|
|
"time"
|
2018-11-17 20:24:09 +00:00
|
|
|
|
|
2018-11-28 23:53:47 +00:00
|
|
|
|
"github.com/naoina/toml"
|
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"
|
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-01-31 07:13:33 +00:00
|
|
|
|
|
2023-03-24 04:20:28 +00:00
|
|
|
|
List KernelListCmd `cmd:"" help:"list kernels"`
|
|
|
|
|
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-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
|
|
|
|
|
setSigintHandler(&shutdown)
|
|
|
|
|
|
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-03-29 17:15:27 +00:00
|
|
|
|
err = generateKernels(sk,
|
|
|
|
|
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-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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return updateKernelsCfg(kernelCmd.UseHost, !kernelCmd.NoDownload)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
setSigintHandler(&shutdown)
|
|
|
|
|
|
2023-01-31 07:13:33 +00:00
|
|
|
|
km := config.KernelMask{
|
|
|
|
|
DistroType: distroType,
|
|
|
|
|
DistroRelease: cmd.Ver,
|
|
|
|
|
ReleaseMask: ".*",
|
|
|
|
|
}
|
2023-02-27 18:53:06 +00:00
|
|
|
|
err = generateKernels(km,
|
|
|
|
|
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-04-07 20:52:45 +00:00
|
|
|
|
&shutdown,
|
2023-03-22 18:21:21 +00:00
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return updateKernelsCfg(kernelCmd.UseHost, !kernelCmd.NoDownload)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
setSigintHandler(&shutdown)
|
|
|
|
|
|
2023-03-22 18:21:21 +00:00
|
|
|
|
km := config.KernelMask{
|
|
|
|
|
DistroType: distroType,
|
|
|
|
|
DistroRelease: cmd.Ver,
|
|
|
|
|
ReleaseMask: cmd.Kernel,
|
|
|
|
|
}
|
|
|
|
|
err = generateKernels(km,
|
|
|
|
|
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-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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return updateKernelsCfg(kernelCmd.UseHost, !kernelCmd.NoDownload)
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 04:20:28 +00:00
|
|
|
|
type KernelConfigRegenCmd struct{}
|
|
|
|
|
|
|
|
|
|
func (cmd *KernelConfigRegenCmd) Run(kernelCmd *KernelCmd, g *Globals) (err error) {
|
|
|
|
|
return updateKernelsCfg(kernelCmd.UseHost, !kernelCmd.NoDownload)
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-05 12:31:46 +00:00
|
|
|
|
func matchDebImagePkg(container, mask string) (pkgs []string, err error) {
|
2018-11-28 22:41:17 +00:00
|
|
|
|
|
2023-04-05 12:31:46 +00:00
|
|
|
|
cmd := "apt-cache search --names-only '^linux-image-[0-9\\.\\-]*-generic' | awk '{ print $1 }'"
|
2023-03-22 17:45:56 +00:00
|
|
|
|
|
2023-03-24 04:15:08 +00:00
|
|
|
|
// FIXME timeout should be in global out-of-tree config
|
|
|
|
|
c, err := NewContainer(container, time.Hour)
|
2023-03-22 17:45:56 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-07 16:43:56 +00:00
|
|
|
|
output, err := c.Run(tempDirBase, cmd)
|
2018-11-28 22:41:17 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-29 17:15:27 +00:00
|
|
|
|
r, err := regexp.Compile("linux-image-" + mask)
|
2018-11-28 22:41:17 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-05 18:01:04 +00:00
|
|
|
|
for _, pkg := range strings.Fields(output) {
|
|
|
|
|
if r.MatchString(pkg) || strings.Contains(pkg, mask) {
|
|
|
|
|
pkgs = append(pkgs, pkg)
|
|
|
|
|
}
|
2018-11-28 22:41:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-20 18:57:45 +00:00
|
|
|
|
func matchCentOSDevelPkg(container, mask string, generic bool) (
|
|
|
|
|
pkgs []string, err error) {
|
|
|
|
|
|
2020-05-30 12:48:24 +00:00
|
|
|
|
cmd := "yum search kernel-devel --showduplicates | " +
|
2019-08-20 18:57:45 +00:00
|
|
|
|
"grep '^kernel-devel' | cut -d ' ' -f 1"
|
2023-03-22 17:45:56 +00:00
|
|
|
|
|
2023-03-24 04:15:08 +00:00
|
|
|
|
// FIXME timeout should be in global out-of-tree config
|
|
|
|
|
c, err := NewContainer(container, time.Hour)
|
2023-03-22 17:45:56 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-07 16:43:56 +00:00
|
|
|
|
output, err := c.Run(tempDirBase, cmd)
|
2019-08-20 18:57:45 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r, err := regexp.Compile("kernel-devel-" + mask)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-05 18:04:22 +00:00
|
|
|
|
for _, pkg := range strings.Fields(output) {
|
|
|
|
|
if r.MatchString(pkg) || strings.Contains(pkg, mask) {
|
|
|
|
|
pkgs = append(pkgs, pkg)
|
|
|
|
|
}
|
2019-08-20 18:57:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-09 14:40:06 +00:00
|
|
|
|
func matchOracleLinuxPkg(container, mask string) (
|
|
|
|
|
pkgs []string, err error) {
|
|
|
|
|
|
|
|
|
|
cmd := "yum search kernel --showduplicates " +
|
|
|
|
|
"| grep '^kernel-[0-9]\\|^kernel-uek-[0-9]' " +
|
|
|
|
|
"| grep -v src " +
|
|
|
|
|
"| cut -d ' ' -f 1"
|
|
|
|
|
|
|
|
|
|
// FIXME timeout should be in global out-of-tree config
|
|
|
|
|
c, err := NewContainer(container, time.Hour)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
output, err := c.Run(tempDirBase, cmd)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r, err := regexp.Compile("kernel-" + mask)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, pkg := range strings.Fields(output) {
|
|
|
|
|
if r.MatchString(pkg) || strings.Contains(pkg, mask) {
|
|
|
|
|
log.Trace().Msg(pkg)
|
|
|
|
|
pkgs = append(pkgs, pkg)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-09 16:31:34 +00:00
|
|
|
|
if len(pkgs) == 0 {
|
|
|
|
|
log.Warn().Msg("no packages matched")
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-09 14:40:06 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-28 22:41:17 +00:00
|
|
|
|
func dockerImagePath(sk config.KernelMask) (path string, err error) {
|
|
|
|
|
usr, err := user.Current()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-22 17:45:56 +00:00
|
|
|
|
path = usr.HomeDir + "/.out-of-tree/containers/"
|
2018-11-28 22:41:17 +00:00
|
|
|
|
path += sk.DistroType.String() + "/" + sk.DistroRelease
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-20 18:57:45 +00:00
|
|
|
|
func vsyscallAvailable() (available bool, err error) {
|
2019-12-25 14:36:32 +00:00
|
|
|
|
if runtime.GOOS != "linux" {
|
|
|
|
|
// Docker for non-Linux systems is not using the host
|
|
|
|
|
// kernel but uses kernel inside a virtual machine, so
|
|
|
|
|
// it builds by the Docker team with vsyscall support.
|
|
|
|
|
available = true
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-20 18:57:45 +00:00
|
|
|
|
buf, err := ioutil.ReadFile("/proc/self/maps")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
available = strings.Contains(string(buf), "[vsyscall]")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-30 00:05:50 +00:00
|
|
|
|
func generateBaseDockerImage(registry string, commands []config.DockerCommand,
|
|
|
|
|
sk config.KernelMask) (err error) {
|
|
|
|
|
|
2018-11-28 22:41:17 +00:00
|
|
|
|
imagePath, err := dockerImagePath(sk)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
dockerPath := imagePath + "/Dockerfile"
|
|
|
|
|
|
|
|
|
|
d := "# BASE\n"
|
|
|
|
|
|
2023-04-07 18:57:18 +00:00
|
|
|
|
// TODO move as function to container.go
|
|
|
|
|
cmd := exec.Command(containerRuntime, "images", "-q", sk.DockerName())
|
2023-03-22 18:05:13 +00:00
|
|
|
|
log.Debug().Msgf("run %v", cmd)
|
2023-03-19 13:14:14 +00:00
|
|
|
|
|
2023-01-30 11:15:12 +00:00
|
|
|
|
rawOutput, err := cmd.CombinedOutput()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if exists(dockerPath) && string(rawOutput) != "" {
|
2023-03-22 17:45:56 +00:00
|
|
|
|
log.Info().Msgf("Base image for %s:%s found",
|
2018-11-28 22:41:17 +00:00
|
|
|
|
sk.DistroType.String(), sk.DistroRelease)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-22 17:45:56 +00:00
|
|
|
|
log.Info().Msgf("Base image for %s:%s not found, start generating",
|
2018-12-10 02:39:55 +00:00
|
|
|
|
sk.DistroType.String(), sk.DistroRelease)
|
|
|
|
|
os.MkdirAll(imagePath, os.ModePerm)
|
|
|
|
|
|
2019-08-29 22:49:59 +00:00
|
|
|
|
d += "FROM "
|
|
|
|
|
if registry != "" {
|
|
|
|
|
d += registry + "/"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
d += fmt.Sprintf("%s:%s\n",
|
2018-11-28 22:41:17 +00:00
|
|
|
|
strings.ToLower(sk.DistroType.String()),
|
|
|
|
|
sk.DistroRelease,
|
|
|
|
|
)
|
|
|
|
|
|
2019-08-20 18:57:45 +00:00
|
|
|
|
vsyscall, err := vsyscallAvailable()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-30 00:05:50 +00:00
|
|
|
|
for _, c := range commands {
|
2023-04-05 19:32:31 +00:00
|
|
|
|
d += "RUN " + c.Command + "\n"
|
2019-08-30 00:05:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-28 22:41:17 +00:00
|
|
|
|
switch sk.DistroType {
|
|
|
|
|
case config.Ubuntu:
|
|
|
|
|
d += "ENV DEBIAN_FRONTEND=noninteractive\n"
|
|
|
|
|
d += "RUN apt-get update\n"
|
|
|
|
|
d += "RUN apt-get install -y build-essential libelf-dev\n"
|
2019-08-20 07:06:08 +00:00
|
|
|
|
d += "RUN apt-get install -y wget git\n"
|
2023-04-06 20:53:59 +00:00
|
|
|
|
// Install a single kernel and headers to ensure all dependencies are cached
|
2023-04-06 21:08:51 +00:00
|
|
|
|
d += "RUN export PKGNAME=$(apt-cache search --names-only '^linux-headers-[0-9\\.\\-]*-generic' | awk '{ print $1 }' | head -n 1); " +
|
2023-04-07 16:27:56 +00:00
|
|
|
|
"apt-get install -y $PKGNAME $(echo $PKGNAME | sed 's/headers/image/'); " +
|
|
|
|
|
"apt-get remove -y $PKGNAME $(echo $PKGNAME | sed 's/headers/image/')\n"
|
2019-08-20 07:06:08 +00:00
|
|
|
|
if sk.DistroRelease >= "14.04" {
|
|
|
|
|
d += "RUN apt-get install -y libseccomp-dev\n"
|
|
|
|
|
}
|
2019-08-23 06:46:37 +00:00
|
|
|
|
d += "RUN mkdir -p /lib/modules\n"
|
2019-08-20 18:57:45 +00:00
|
|
|
|
case config.CentOS:
|
|
|
|
|
if sk.DistroRelease < "7" && !vsyscall {
|
2023-03-18 21:30:07 +00:00
|
|
|
|
log.Print("Old CentOS requires `vsyscall=emulate` " +
|
2019-08-20 18:57:45 +00:00
|
|
|
|
"on the latest kernels")
|
2023-03-18 21:30:07 +00:00
|
|
|
|
log.Print("Check out `A note about vsyscall` " +
|
2019-08-20 18:57:45 +00:00
|
|
|
|
"at https://hub.docker.com/_/centos")
|
2023-03-18 21:30:07 +00:00
|
|
|
|
log.Print("See also https://lwn.net/Articles/446528/")
|
2019-08-20 18:57:45 +00:00
|
|
|
|
err = fmt.Errorf("vsyscall is not available")
|
|
|
|
|
return
|
2023-04-06 13:38:33 +00:00
|
|
|
|
} else if sk.DistroRelease == "8" {
|
|
|
|
|
// CentOS 8 doesn't have Vault repos by default
|
|
|
|
|
for _, repover := range []string{
|
|
|
|
|
"8.0.1905", "8.1.1911", "8.2.2004", "8.3.2011", "8.4.2105", "8.5.2111",
|
|
|
|
|
} {
|
|
|
|
|
repo := fmt.Sprintf("[%s]\\nbaseurl=http://vault.centos.org/%s/BaseOS/$basearch/os/\\ngpgcheck=0", repover, repover)
|
|
|
|
|
d += fmt.Sprintf("RUN echo -e '%s' >> /etc/yum.repos.d/CentOS-Vault.repo\n", repo)
|
|
|
|
|
}
|
|
|
|
|
d += "RUN sed -i 's/enabled=1/enabled=0/' /etc/yum.repos.d/*\n"
|
2019-08-20 18:57:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// enable rpms from old minor releases
|
|
|
|
|
d += "RUN sed -i 's/enabled=0/enabled=1/' /etc/yum.repos.d/CentOS-Vault.repo\n"
|
|
|
|
|
// do not remove old kernels
|
|
|
|
|
d += "RUN sed -i 's;installonly_limit=;installonly_limit=100500;' /etc/yum.conf\n"
|
|
|
|
|
d += "RUN yum -y update\n"
|
2020-05-30 14:13:03 +00:00
|
|
|
|
|
2019-08-20 18:57:45 +00:00
|
|
|
|
d += "RUN yum -y groupinstall 'Development Tools'\n"
|
2020-05-30 12:40:12 +00:00
|
|
|
|
|
|
|
|
|
if sk.DistroRelease < "8" {
|
|
|
|
|
d += "RUN yum -y install deltarpm\n"
|
|
|
|
|
} else {
|
2023-04-06 13:38:33 +00:00
|
|
|
|
d += "RUN yum -y install grub2-tools-minimal " +
|
2020-05-30 13:42:04 +00:00
|
|
|
|
"elfutils-libelf-devel\n"
|
2020-05-30 12:40:12 +00:00
|
|
|
|
}
|
2023-04-06 21:56:22 +00:00
|
|
|
|
|
2023-04-06 22:37:15 +00:00
|
|
|
|
var flags string
|
|
|
|
|
if sk.DistroRelease >= "8" {
|
|
|
|
|
flags = "--noautoremove"
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-06 21:56:22 +00:00
|
|
|
|
// Cache kernel package dependencies
|
2023-04-06 21:57:47 +00:00
|
|
|
|
d += "RUN export PKGNAME=$(yum search kernel-devel --showduplicates | grep '^kernel-devel' | cut -d ' ' -f 1 | head -n 1); " +
|
2023-04-06 21:56:22 +00:00
|
|
|
|
"yum -y install $PKGNAME $(echo $PKGNAME | sed 's/-devel//'); " +
|
2023-04-09 17:13:54 +00:00
|
|
|
|
fmt.Sprintf("yum -y remove $PKGNAME "+
|
|
|
|
|
"$(echo $PKGNAME | sed 's/-devel//') "+
|
|
|
|
|
"$(echo $PKGNAME | sed 's/-devel/-modules/') "+
|
|
|
|
|
"$(echo $PKGNAME | sed 's/-devel/-core/') %s\n", flags)
|
2023-05-09 14:40:06 +00:00
|
|
|
|
case config.OracleLinux:
|
|
|
|
|
if sk.DistroRelease < "6" {
|
|
|
|
|
err = fmt.Errorf("no support for pre-EL6")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
d += "RUN sed -i 's/enabled=0/enabled=1/' /etc/yum.repos.d/*\n"
|
|
|
|
|
d += "RUN sed -i 's;installonly_limit=;installonly_limit=100500;' /etc/yum.conf /etc/dnf/dnf.conf || true\n"
|
|
|
|
|
d += "RUN yum -y update\n"
|
|
|
|
|
d += "RUN yum -y groupinstall 'Development Tools'\n"
|
|
|
|
|
d += "RUN yum -y install linux-firmware grubby\n"
|
2023-05-09 20:16:28 +00:00
|
|
|
|
if sk.DistroRelease <= "7" {
|
|
|
|
|
d += "RUN yum -y install libdtrace-ctf\n"
|
|
|
|
|
}
|
2018-11-28 22:41:17 +00:00
|
|
|
|
default:
|
2018-12-10 02:45:17 +00:00
|
|
|
|
err = fmt.Errorf("%s not yet supported", sk.DistroType.String())
|
2018-11-28 22:41:17 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
d += "# END BASE\n\n"
|
|
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(dockerPath, []byte(d), 0644)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-22 17:45:56 +00:00
|
|
|
|
c, err := NewContainer(sk.DockerName(), time.Hour)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-03-19 13:14:14 +00:00
|
|
|
|
|
2023-03-22 17:45:56 +00:00
|
|
|
|
output, err := c.Build(imagePath)
|
2018-11-28 22:41:17 +00:00
|
|
|
|
if err != nil {
|
2023-03-22 17:45:56 +00:00
|
|
|
|
log.Error().Err(err).Msgf("Base image for %s:%s generating error",
|
2018-11-28 22:41:17 +00:00
|
|
|
|
sk.DistroType.String(), sk.DistroRelease)
|
2023-03-22 17:45:56 +00:00
|
|
|
|
log.Fatal().Msg(output)
|
2018-11-28 22:41:17 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-22 17:45:56 +00:00
|
|
|
|
log.Info().Msgf("Base image for %s:%s generating success",
|
2018-11-28 22:41:17 +00:00
|
|
|
|
sk.DistroType.String(), sk.DistroRelease)
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-29 17:15:27 +00:00
|
|
|
|
func installKernel(sk config.KernelMask, pkgname string, force, headers bool) (err error) {
|
2023-03-22 18:05:13 +00:00
|
|
|
|
slog := log.With().
|
|
|
|
|
Str("distro_type", sk.DistroType.String()).
|
|
|
|
|
Str("distro_release", sk.DistroRelease).
|
|
|
|
|
Str("pkg", pkgname).
|
|
|
|
|
Logger()
|
|
|
|
|
|
2023-03-22 17:45:56 +00:00
|
|
|
|
c, err := NewContainer(sk.DockerName(), time.Hour) // TODO conf
|
2018-11-28 22:41:17 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-22 18:05:13 +00:00
|
|
|
|
moddirs, err := ioutil.ReadDir(c.Volumes.LibModules)
|
|
|
|
|
if err != nil {
|
2018-11-28 22:41:17 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-22 18:05:13 +00:00
|
|
|
|
for _, krel := range moddirs {
|
|
|
|
|
if strings.Contains(pkgname, krel.Name()) {
|
2023-03-22 18:20:45 +00:00
|
|
|
|
if force {
|
|
|
|
|
slog.Info().Msg("Reinstall")
|
|
|
|
|
} else {
|
2023-03-22 18:24:09 +00:00
|
|
|
|
slog.Info().Msg("Already installed")
|
2023-03-22 18:20:45 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
2023-03-22 18:05:13 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-05 16:14:30 +00:00
|
|
|
|
volumes := c.Volumes
|
|
|
|
|
|
2023-04-07 17:47:54 +00:00
|
|
|
|
c.Volumes.LibModules = ""
|
|
|
|
|
c.Volumes.UsrSrc = ""
|
|
|
|
|
c.Volumes.Boot = ""
|
2023-04-05 16:14:30 +00:00
|
|
|
|
|
2023-03-22 18:08:48 +00:00
|
|
|
|
slog.Debug().Msgf("Installing kernel")
|
2023-03-22 18:05:13 +00:00
|
|
|
|
|
2023-04-07 17:47:54 +00:00
|
|
|
|
cmd := "true"
|
|
|
|
|
|
2019-08-20 18:57:45 +00:00
|
|
|
|
switch sk.DistroType {
|
|
|
|
|
case config.Ubuntu:
|
2023-03-29 17:15:27 +00:00
|
|
|
|
var headerspkg string
|
|
|
|
|
if headers {
|
|
|
|
|
headerspkg = strings.Replace(pkgname, "image", "headers", -1)
|
|
|
|
|
}
|
2018-12-02 19:54:09 +00:00
|
|
|
|
|
2023-04-07 17:47:54 +00:00
|
|
|
|
cmd += fmt.Sprintf(" && apt-get install -y %s %s", pkgname, headerspkg)
|
2019-08-20 18:57:45 +00:00
|
|
|
|
case config.CentOS:
|
|
|
|
|
imagepkg := strings.Replace(pkgname, "-devel", "", -1)
|
|
|
|
|
|
|
|
|
|
version := strings.Replace(pkgname, "kernel-devel-", "", -1)
|
|
|
|
|
|
2023-03-29 17:15:27 +00:00
|
|
|
|
if !headers {
|
|
|
|
|
pkgname = ""
|
|
|
|
|
}
|
2023-04-07 17:47:54 +00:00
|
|
|
|
cmd += fmt.Sprintf(" && yum -y install %s %s", imagepkg,
|
2019-08-20 18:57:45 +00:00
|
|
|
|
pkgname)
|
2018-11-28 22:41:17 +00:00
|
|
|
|
|
2023-05-09 14:40:06 +00:00
|
|
|
|
cmd += fmt.Sprintf(" && dracut --add-drivers 'e1000 ext4' -fv "+
|
|
|
|
|
"/boot/initramfs-%s.img %s", version, version)
|
|
|
|
|
case config.OracleLinux:
|
|
|
|
|
var headerspkg string
|
|
|
|
|
if headers {
|
|
|
|
|
if strings.Contains(pkgname, "uek") {
|
|
|
|
|
headerspkg = strings.Replace(pkgname,
|
|
|
|
|
"kernel-uek", "kernel-uek-devel", -1)
|
|
|
|
|
} else {
|
|
|
|
|
headerspkg = strings.Replace(pkgname,
|
|
|
|
|
"kernel", "kernel-devel", -1)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cmd += fmt.Sprintf(" && yum -y install %s %s", pkgname, headerspkg)
|
|
|
|
|
|
|
|
|
|
var version string
|
|
|
|
|
if strings.Contains(pkgname, "uek") {
|
|
|
|
|
version = strings.Replace(pkgname, "kernel-uek-", "", -1)
|
|
|
|
|
} else {
|
|
|
|
|
version = strings.Replace(pkgname, "kernel-", "", -1)
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-09 17:53:39 +00:00
|
|
|
|
if sk.DistroRelease <= "7" {
|
|
|
|
|
cmd += fmt.Sprintf(" && dracut -v --add-drivers 'e1000 ext4' -f "+
|
|
|
|
|
"/boot/initramfs-%s.img %s", version, version)
|
|
|
|
|
} else {
|
|
|
|
|
cmd += fmt.Sprintf(" && dracut -v --add-drivers 'ata_piix libata' --force-drivers 'e1000 ext4 sd_mod' -f "+
|
|
|
|
|
"/boot/initramfs-%s.img %s", version, version)
|
|
|
|
|
}
|
2023-03-22 17:45:56 +00:00
|
|
|
|
default:
|
|
|
|
|
err = fmt.Errorf("%s not yet supported", sk.DistroType.String())
|
2023-02-15 10:52:36 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-06 14:48:12 +00:00
|
|
|
|
c.Args = append(c.Args, "-v", volumes.LibModules+":/target/lib/modules")
|
|
|
|
|
c.Args = append(c.Args, "-v", volumes.UsrSrc+":/target/usr/src")
|
|
|
|
|
c.Args = append(c.Args, "-v", volumes.Boot+":/target/boot")
|
|
|
|
|
|
2023-04-07 17:47:54 +00:00
|
|
|
|
cmd += " && cp -r /boot /target/"
|
|
|
|
|
cmd += " && cp -r /lib/modules /target/lib/"
|
|
|
|
|
cmd += " && cp -r /usr/src /target/usr/"
|
2023-04-06 14:48:12 +00:00
|
|
|
|
|
2023-04-07 17:47:54 +00:00
|
|
|
|
_, err = c.Run("", cmd)
|
2023-04-05 16:14:30 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-22 18:08:48 +00:00
|
|
|
|
slog.Debug().Msgf("Success")
|
2018-11-28 22:41:17 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-07 10:27:59 +00:00
|
|
|
|
func findKernelFile(files []os.FileInfo, kname string) (name string, err error) {
|
2018-11-28 23:53:47 +00:00
|
|
|
|
for _, file := range files {
|
2019-08-20 18:57:45 +00:00
|
|
|
|
if strings.HasPrefix(file.Name(), "vmlinuz") {
|
2018-11-28 23:53:47 +00:00
|
|
|
|
if strings.Contains(file.Name(), kname) {
|
2023-04-07 10:27:59 +00:00
|
|
|
|
name = file.Name()
|
|
|
|
|
return
|
2018-11-28 23:53:47 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-22 17:45:56 +00:00
|
|
|
|
|
2023-04-07 10:27:59 +00:00
|
|
|
|
err = errors.New("cannot find kernel")
|
|
|
|
|
return
|
2018-11-28 23:53:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-07 10:27:59 +00:00
|
|
|
|
func findInitrdFile(files []os.FileInfo, kname string) (name string, err error) {
|
2018-11-28 23:53:47 +00:00
|
|
|
|
for _, file := range files {
|
2019-08-20 18:57:45 +00:00
|
|
|
|
if strings.HasPrefix(file.Name(), "initrd") ||
|
|
|
|
|
strings.HasPrefix(file.Name(), "initramfs") {
|
|
|
|
|
|
2018-11-28 23:53:47 +00:00
|
|
|
|
if strings.Contains(file.Name(), kname) {
|
2023-04-07 10:27:59 +00:00
|
|
|
|
name = file.Name()
|
|
|
|
|
return
|
2018-11-28 23:53:47 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-22 17:45:56 +00:00
|
|
|
|
|
2023-04-07 10:27:59 +00:00
|
|
|
|
err = errors.New("cannot find kernel")
|
|
|
|
|
return
|
2018-11-28 23:53:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-06 19:05:21 +00:00
|
|
|
|
func genRootfsImage(d containerImageInfo, download bool) (rootfs string, err error) {
|
2018-11-29 00:01:14 +00:00
|
|
|
|
usr, err := user.Current()
|
|
|
|
|
if err != nil {
|
2019-08-20 09:09:38 +00:00
|
|
|
|
return
|
2018-11-29 00:01:14 +00:00
|
|
|
|
}
|
2023-04-06 19:05:21 +00:00
|
|
|
|
imageFile := d.Name + ".img"
|
2019-08-20 09:09:38 +00:00
|
|
|
|
|
|
|
|
|
imagesPath := usr.HomeDir + "/.out-of-tree/images/"
|
|
|
|
|
os.MkdirAll(imagesPath, os.ModePerm)
|
|
|
|
|
|
|
|
|
|
rootfs = imagesPath + imageFile
|
|
|
|
|
if !exists(rootfs) {
|
|
|
|
|
if download {
|
2023-04-06 21:09:12 +00:00
|
|
|
|
log.Info().Msgf("%v not available, start download", imageFile)
|
2019-08-20 09:09:38 +00:00
|
|
|
|
err = downloadImage(imagesPath, imageFile)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return
|
2018-11-28 23:53:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-20 09:09:38 +00:00
|
|
|
|
func updateKernelsCfg(host, download bool) (err error) {
|
2018-12-01 14:23:01 +00:00
|
|
|
|
newkcfg := config.KernelConfig{}
|
|
|
|
|
|
2019-08-16 05:05:26 +00:00
|
|
|
|
if host {
|
|
|
|
|
// Get host kernels
|
2019-08-20 09:09:38 +00:00
|
|
|
|
newkcfg, err = genHostKernels(download)
|
2019-08-16 05:05:26 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get docker kernels
|
2023-04-06 19:05:21 +00:00
|
|
|
|
dockerImages, err := listContainerImages()
|
2019-08-16 05:05:26 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-01 14:23:01 +00:00
|
|
|
|
for _, d := range dockerImages {
|
2023-03-29 17:25:32 +00:00
|
|
|
|
err = listContainersKernels(d, &newkcfg, download)
|
2018-12-01 14:23:01 +00:00
|
|
|
|
if err != nil {
|
2023-04-06 19:05:21 +00:00
|
|
|
|
log.Print("gen kernels", d.Name, ":", err)
|
2018-12-01 14:23:01 +00:00
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stripkcfg := config.KernelConfig{}
|
|
|
|
|
for _, nk := range newkcfg.Kernels {
|
|
|
|
|
if !hasKernel(nk, stripkcfg) {
|
|
|
|
|
stripkcfg.Kernels = append(stripkcfg.Kernels, nk)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
buf, err := toml.Marshal(&stripkcfg)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
buf = append([]byte("# Autogenerated\n# DO NOT EDIT\n\n"), buf...)
|
|
|
|
|
|
|
|
|
|
usr, err := user.Current()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO move all cfg path values to one provider
|
|
|
|
|
kernelsCfgPath := usr.HomeDir + "/.out-of-tree/kernels.toml"
|
|
|
|
|
err = ioutil.WriteFile(kernelsCfgPath, buf, 0644)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-22 17:45:56 +00:00
|
|
|
|
log.Info().Msgf("%s is successfully updated", kernelsCfgPath)
|
2018-12-01 14:23:01 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-06 19:05:21 +00:00
|
|
|
|
func listContainersKernels(dii containerImageInfo, newkcfg *config.KernelConfig,
|
2019-08-20 09:09:38 +00:00
|
|
|
|
download bool) (err error) {
|
2018-11-28 23:53:47 +00:00
|
|
|
|
|
2023-03-22 17:45:56 +00:00
|
|
|
|
rootfs, err := genRootfsImage(dii, download)
|
2018-11-28 23:53:47 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-06 19:05:21 +00:00
|
|
|
|
c, err := NewContainer(dii.Name, time.Hour)
|
2018-11-28 23:53:47 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-03-22 17:45:56 +00:00
|
|
|
|
|
|
|
|
|
moddirs, err := ioutil.ReadDir(c.Volumes.LibModules)
|
2018-11-28 23:53:47 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-22 17:45:56 +00:00
|
|
|
|
bootfiles, err := ioutil.ReadDir(c.Volumes.Boot)
|
2019-08-20 09:09:38 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-22 17:45:56 +00:00
|
|
|
|
for _, krel := range moddirs {
|
2023-04-07 10:27:59 +00:00
|
|
|
|
log.Debug().Msgf("generate config entry for %s", krel.Name())
|
|
|
|
|
|
|
|
|
|
var kernelFile, initrdFile string
|
|
|
|
|
kernelFile, err = findKernelFile(bootfiles, krel.Name())
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Warn().Msgf("cannot find kernel %s", krel.Name())
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
initrdFile, err = findInitrdFile(bootfiles, krel.Name())
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Warn().Msgf("cannot find initrd %s", krel.Name())
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-28 23:53:47 +00:00
|
|
|
|
ki := config.KernelInfo{
|
2018-12-01 14:23:01 +00:00
|
|
|
|
DistroType: dii.DistroType,
|
|
|
|
|
DistroRelease: dii.DistroRelease,
|
2023-03-22 17:45:56 +00:00
|
|
|
|
KernelRelease: krel.Name(),
|
2023-04-06 19:05:21 +00:00
|
|
|
|
ContainerName: dii.Name,
|
2018-11-28 23:53:47 +00:00
|
|
|
|
|
2023-04-07 10:27:59 +00:00
|
|
|
|
KernelPath: c.Volumes.Boot + "/" + kernelFile,
|
|
|
|
|
InitrdPath: c.Volumes.Boot + "/" + initrdFile,
|
2023-03-22 17:45:56 +00:00
|
|
|
|
ModulesPath: c.Volumes.LibModules + "/" + krel.Name(),
|
2023-02-15 10:52:36 +00:00
|
|
|
|
|
|
|
|
|
RootFS: rootfs,
|
2018-11-28 23:53:47 +00:00
|
|
|
|
}
|
|
|
|
|
newkcfg.Kernels = append(newkcfg.Kernels, ki)
|
2023-03-23 20:28:42 +00:00
|
|
|
|
}
|
2023-03-22 21:16:00 +00:00
|
|
|
|
|
2023-03-23 20:28:42 +00:00
|
|
|
|
for _, cmd := range []string{
|
|
|
|
|
"find /boot -type f -exec chmod a+r {} \\;",
|
|
|
|
|
} {
|
2023-04-07 16:43:56 +00:00
|
|
|
|
_, err = c.Run(tempDirBase, cmd)
|
2023-03-23 20:28:42 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
2023-03-22 21:16:00 +00:00
|
|
|
|
}
|
2018-11-28 23:53:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func hasKernel(ki config.KernelInfo, kcfg config.KernelConfig) bool {
|
|
|
|
|
for _, sk := range kcfg.Kernels {
|
|
|
|
|
if sk == ki {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-05 11:29:31 +00:00
|
|
|
|
func shuffleStrings(a []string) []string {
|
2019-08-12 22:58:34 +00:00
|
|
|
|
// Fisher–Yates shuffle
|
|
|
|
|
for i := len(a) - 1; i > 0; i-- {
|
|
|
|
|
j := rand.Intn(i + 1)
|
|
|
|
|
a[i], a[j] = a[j], a[i]
|
|
|
|
|
}
|
|
|
|
|
return a
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-07 20:52:45 +00:00
|
|
|
|
func setSigintHandler(variable *bool) {
|
|
|
|
|
c := make(chan os.Signal, 1)
|
|
|
|
|
signal.Notify(c, os.Interrupt)
|
|
|
|
|
go func() {
|
|
|
|
|
counter := 0
|
|
|
|
|
for _ = range c {
|
|
|
|
|
if counter == 0 {
|
|
|
|
|
*variable = true
|
|
|
|
|
log.Warn().Msg("shutdown requested, finishing work")
|
|
|
|
|
log.Info().Msg("^C a couple of times more for an unsafe exit")
|
|
|
|
|
} else if counter >= 3 {
|
|
|
|
|
log.Fatal().Msg("unsafe exit")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
counter += 1
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-30 00:05:50 +00:00
|
|
|
|
func generateKernels(km config.KernelMask, registry string,
|
2023-04-05 16:24:45 +00:00
|
|
|
|
commands []config.DockerCommand, max, retries int64,
|
2023-04-07 20:52:45 +00:00
|
|
|
|
download, force, headers, shuffle bool, shutdown *bool) (err error) {
|
2019-08-29 22:49:59 +00:00
|
|
|
|
|
2023-03-22 17:45:56 +00:00
|
|
|
|
log.Info().Msgf("Generating for kernel mask %v", km)
|
2019-08-20 09:09:38 +00:00
|
|
|
|
|
2023-04-06 19:05:21 +00:00
|
|
|
|
_, err = genRootfsImage(containerImageInfo{Name: km.DockerName()},
|
2019-08-20 09:09:38 +00:00
|
|
|
|
download)
|
2023-04-07 20:52:45 +00:00
|
|
|
|
if err != nil || *shutdown {
|
2019-08-20 09:09:38 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-30 00:05:50 +00:00
|
|
|
|
err = generateBaseDockerImage(registry, commands, km)
|
2023-04-07 20:52:45 +00:00
|
|
|
|
if err != nil || *shutdown {
|
2018-12-02 19:54:09 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var pkgs []string
|
2019-08-20 18:57:45 +00:00
|
|
|
|
switch km.DistroType {
|
|
|
|
|
case config.Ubuntu:
|
2023-04-05 12:31:46 +00:00
|
|
|
|
pkgs, err = matchDebImagePkg(km.DockerName(), km.ReleaseMask)
|
2019-08-20 18:57:45 +00:00
|
|
|
|
case config.CentOS:
|
|
|
|
|
pkgs, err = matchCentOSDevelPkg(km.DockerName(),
|
|
|
|
|
km.ReleaseMask, true)
|
2023-05-09 14:40:06 +00:00
|
|
|
|
case config.OracleLinux:
|
|
|
|
|
pkgs, err = matchOracleLinuxPkg(km.DockerName(), km.ReleaseMask)
|
2019-08-20 18:57:45 +00:00
|
|
|
|
default:
|
|
|
|
|
err = fmt.Errorf("%s not yet supported", km.DistroType.String())
|
|
|
|
|
}
|
2023-04-07 20:52:45 +00:00
|
|
|
|
if err != nil || *shutdown {
|
2018-12-02 19:54:09 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-05 11:29:31 +00:00
|
|
|
|
if shuffle {
|
|
|
|
|
pkgs = shuffleStrings(pkgs)
|
|
|
|
|
}
|
|
|
|
|
for i, pkg := range pkgs {
|
2019-08-12 22:58:34 +00:00
|
|
|
|
if max <= 0 {
|
2023-03-18 21:30:07 +00:00
|
|
|
|
log.Print("Max is reached")
|
2019-08-12 22:58:34 +00:00
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-07 21:03:31 +00:00
|
|
|
|
if *shutdown {
|
|
|
|
|
err = nil
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-03-22 18:05:13 +00:00
|
|
|
|
log.Info().Msgf("%d/%d %s", i+1, len(pkgs), pkg)
|
2019-08-12 22:58:34 +00:00
|
|
|
|
|
2023-04-05 16:24:45 +00:00
|
|
|
|
var attempt int64
|
|
|
|
|
for {
|
|
|
|
|
attempt++
|
|
|
|
|
|
2023-04-07 20:52:45 +00:00
|
|
|
|
if *shutdown {
|
|
|
|
|
err = nil
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-05 16:24:45 +00:00
|
|
|
|
err = installKernel(km, pkg, force, headers)
|
|
|
|
|
if err == nil {
|
|
|
|
|
max--
|
2023-04-05 16:36:09 +00:00
|
|
|
|
break
|
2023-04-05 16:24:45 +00:00
|
|
|
|
} else if attempt >= retries {
|
2023-04-07 13:30:30 +00:00
|
|
|
|
log.Error().Err(err).Msg("install kernel")
|
|
|
|
|
log.Debug().Msg("skip")
|
|
|
|
|
break
|
2023-04-05 16:24:45 +00:00
|
|
|
|
} else {
|
|
|
|
|
log.Warn().Err(err).Msg("install kernel")
|
2023-04-05 16:40:20 +00:00
|
|
|
|
time.Sleep(time.Second)
|
2023-04-06 14:32:05 +00:00
|
|
|
|
log.Info().Msg("retry")
|
2023-04-05 16:24:45 +00:00
|
|
|
|
}
|
2019-08-12 22:58:34 +00:00
|
|
|
|
}
|
2018-12-02 19:54:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|