2023-05-13 10:47:47 +00:00
|
|
|
|
// Copyright 2023 Mikhail Klementev. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by a AGPLv3 license
|
|
|
|
|
// (or later) that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
package kernel
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
"math/rand"
|
|
|
|
|
"os"
|
|
|
|
|
"os/signal"
|
2023-05-21 20:31:47 +00:00
|
|
|
|
"path/filepath"
|
2023-05-18 21:37:07 +00:00
|
|
|
|
"regexp"
|
2023-05-13 10:47:47 +00:00
|
|
|
|
"runtime"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/naoina/toml"
|
2023-05-21 21:43:18 +00:00
|
|
|
|
"github.com/remeh/sizedwaitgroup"
|
2023-05-13 10:47:47 +00:00
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
|
|
|
|
|
"code.dumpstack.io/tools/out-of-tree/cache"
|
|
|
|
|
"code.dumpstack.io/tools/out-of-tree/config"
|
|
|
|
|
"code.dumpstack.io/tools/out-of-tree/container"
|
2023-05-18 16:07:24 +00:00
|
|
|
|
"code.dumpstack.io/tools/out-of-tree/distro"
|
2023-05-13 19:51:06 +00:00
|
|
|
|
"code.dumpstack.io/tools/out-of-tree/distro/debian"
|
2023-05-18 11:08:23 +00:00
|
|
|
|
"code.dumpstack.io/tools/out-of-tree/distro/oraclelinux"
|
2023-05-18 09:49:48 +00:00
|
|
|
|
"code.dumpstack.io/tools/out-of-tree/distro/ubuntu"
|
2023-05-13 10:47:47 +00:00
|
|
|
|
"code.dumpstack.io/tools/out-of-tree/fs"
|
|
|
|
|
)
|
|
|
|
|
|
2023-05-18 21:37:07 +00:00
|
|
|
|
func MatchPackages(km config.Target) (packages []string, err error) {
|
|
|
|
|
pkgs, err := km.Distro.Packages()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
2023-05-13 10:47:47 +00:00
|
|
|
|
}
|
2023-05-18 21:37:07 +00:00
|
|
|
|
|
|
|
|
|
r, err := regexp.Compile(km.Kernel.Regex)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, pkg := range pkgs {
|
|
|
|
|
if r.MatchString(pkg) {
|
|
|
|
|
packages = append(packages, pkg)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-13 10:47:47 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func vsyscallAvailable() (available bool, err error) {
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
buf, err := ioutil.ReadFile("/proc/self/maps")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
available = strings.Contains(string(buf), "[vsyscall]")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-18 18:27:51 +00:00
|
|
|
|
func installKernel(sk config.Target, pkgname string, force, headers bool) (err error) {
|
2023-05-13 10:47:47 +00:00
|
|
|
|
slog := log.With().
|
2023-05-18 16:07:24 +00:00
|
|
|
|
Str("distro_type", sk.Distro.ID.String()).
|
|
|
|
|
Str("distro_release", sk.Distro.Release).
|
2023-05-13 10:47:47 +00:00
|
|
|
|
Str("pkg", pkgname).
|
|
|
|
|
Logger()
|
|
|
|
|
|
2023-05-23 16:54:34 +00:00
|
|
|
|
c, err := container.New(sk.Distro) // TODO conf
|
2023-05-13 10:47:47 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-22 14:28:28 +00:00
|
|
|
|
searchdir := ""
|
|
|
|
|
for _, volume := range c.Volumes {
|
|
|
|
|
if volume.Dest == "/lib/modules" {
|
|
|
|
|
searchdir = volume.Src
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-14 22:16:32 +00:00
|
|
|
|
|
2023-05-18 16:07:24 +00:00
|
|
|
|
if sk.Distro.ID == distro.Debian {
|
2023-05-15 10:08:34 +00:00
|
|
|
|
// TODO We need some kind of API for that
|
2023-05-14 22:16:32 +00:00
|
|
|
|
searchdir = config.Dir("volumes", sk.DockerName())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
moddirs, err := ioutil.ReadDir(searchdir)
|
2023-05-13 10:47:47 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, krel := range moddirs {
|
|
|
|
|
if strings.Contains(pkgname, krel.Name()) {
|
|
|
|
|
if force {
|
|
|
|
|
slog.Info().Msg("Reinstall")
|
|
|
|
|
} else {
|
|
|
|
|
slog.Info().Msg("Already installed")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-18 16:07:24 +00:00
|
|
|
|
if sk.Distro.ID == distro.Debian {
|
2023-05-15 10:08:34 +00:00
|
|
|
|
// Debian has different kernels (package version) by the
|
|
|
|
|
// same name (ABI), so we need to separate /boot
|
2023-05-18 11:34:46 +00:00
|
|
|
|
c.Volumes = debian.Volumes(sk, pkgname)
|
2023-05-15 10:08:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-13 10:47:47 +00:00
|
|
|
|
slog.Debug().Msgf("Installing kernel")
|
|
|
|
|
|
2023-05-18 12:07:59 +00:00
|
|
|
|
var commands []string
|
2023-05-13 10:47:47 +00:00
|
|
|
|
|
2023-05-18 11:42:25 +00:00
|
|
|
|
// TODO install/cleanup kernel interface
|
2023-05-18 16:07:24 +00:00
|
|
|
|
switch sk.Distro.ID {
|
|
|
|
|
case distro.Ubuntu:
|
2023-05-18 11:50:17 +00:00
|
|
|
|
commands, err = ubuntu.Install(sk, pkgname, headers)
|
2023-05-18 11:46:12 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
2023-05-13 10:47:47 +00:00
|
|
|
|
}
|
2023-05-18 11:46:12 +00:00
|
|
|
|
defer func() {
|
|
|
|
|
if err != nil {
|
2023-05-18 11:50:17 +00:00
|
|
|
|
ubuntu.Cleanup(sk, pkgname)
|
2023-05-18 11:46:12 +00:00
|
|
|
|
}
|
|
|
|
|
}()
|
2023-05-18 16:07:24 +00:00
|
|
|
|
case distro.OracleLinux, distro.CentOS:
|
2023-05-18 11:42:25 +00:00
|
|
|
|
commands, err = oraclelinux.Install(sk, pkgname, headers)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
2023-05-13 10:47:47 +00:00
|
|
|
|
}
|
2023-05-18 11:42:25 +00:00
|
|
|
|
defer func() {
|
|
|
|
|
if err != nil {
|
|
|
|
|
oraclelinux.Cleanup(sk, pkgname)
|
|
|
|
|
}
|
|
|
|
|
}()
|
2023-05-18 16:07:24 +00:00
|
|
|
|
case distro.Debian:
|
2023-05-18 11:34:46 +00:00
|
|
|
|
commands, err = debian.Install(sk, pkgname, headers)
|
2023-05-14 06:53:32 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-05-16 09:24:34 +00:00
|
|
|
|
defer func() {
|
|
|
|
|
if err != nil {
|
2023-05-18 11:34:46 +00:00
|
|
|
|
debian.Cleanup(sk, pkgname)
|
2023-05-15 13:07:56 +00:00
|
|
|
|
}
|
2023-05-16 09:24:34 +00:00
|
|
|
|
}()
|
2023-05-13 10:47:47 +00:00
|
|
|
|
default:
|
2023-05-18 16:07:24 +00:00
|
|
|
|
err = fmt.Errorf("%s not yet supported", sk.Distro.ID.String())
|
2023-05-13 10:47:47 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-18 12:07:59 +00:00
|
|
|
|
cmd := "true"
|
|
|
|
|
for _, command := range commands {
|
|
|
|
|
cmd += fmt.Sprintf(" && %s", command)
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-22 14:28:28 +00:00
|
|
|
|
for i := range c.Volumes {
|
|
|
|
|
c.Volumes[i].Dest = "/target" + c.Volumes[i].Dest
|
|
|
|
|
}
|
2023-05-13 10:47:47 +00:00
|
|
|
|
|
|
|
|
|
cmd += " && cp -r /boot /target/"
|
|
|
|
|
cmd += " && cp -r /lib/modules /target/lib/"
|
2023-05-18 16:07:24 +00:00
|
|
|
|
if sk.Distro.ID == distro.Debian {
|
2023-05-17 16:15:58 +00:00
|
|
|
|
cmd += " && cp -rL /usr/src /target/usr/"
|
|
|
|
|
} else {
|
|
|
|
|
cmd += " && cp -r /usr/src /target/usr/"
|
|
|
|
|
}
|
2023-05-13 10:47:47 +00:00
|
|
|
|
|
|
|
|
|
_, err = c.Run("", cmd)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
slog.Debug().Msgf("Success")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GenRootfsImage(d container.Image, download bool) (rootfs string, err error) {
|
2023-05-21 20:31:47 +00:00
|
|
|
|
imagesPath := config.Dir("images")
|
2023-05-13 10:47:47 +00:00
|
|
|
|
imageFile := d.Name + ".img"
|
|
|
|
|
|
2023-05-21 20:31:47 +00:00
|
|
|
|
rootfs = filepath.Join(imagesPath, imageFile)
|
2023-05-13 10:47:47 +00:00
|
|
|
|
if !fs.PathExists(rootfs) {
|
|
|
|
|
if download {
|
|
|
|
|
log.Info().Msgf("%v not available, start download", imageFile)
|
|
|
|
|
err = cache.DownloadQemuImage(imagesPath, imageFile)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func UpdateKernelsCfg(host, download bool) (err error) {
|
|
|
|
|
newkcfg := config.KernelConfig{}
|
|
|
|
|
|
|
|
|
|
if host {
|
|
|
|
|
// Get host kernels
|
|
|
|
|
newkcfg, err = genHostKernels(download)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get docker kernels
|
|
|
|
|
dockerImages, err := container.Images()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, d := range dockerImages {
|
2023-05-14 07:19:35 +00:00
|
|
|
|
// TODO Requires changing the idea of how we list
|
|
|
|
|
// kernels from containers to distro/-related
|
|
|
|
|
// functions.
|
|
|
|
|
if strings.Contains(d.Name, "debian") {
|
|
|
|
|
err = debian.ContainerKernels(d, &newkcfg)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Print("gen kernels", d.Name, ":", err)
|
|
|
|
|
}
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-13 10:47:47 +00:00
|
|
|
|
err = listContainersKernels(d, &newkcfg, download)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Print("gen kernels", d.Name, ":", err)
|
|
|
|
|
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...)
|
|
|
|
|
|
2023-05-21 20:31:47 +00:00
|
|
|
|
kernelsCfgPath := config.File("kernels.toml")
|
2023-05-13 10:47:47 +00:00
|
|
|
|
err = ioutil.WriteFile(kernelsCfgPath, buf, 0644)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log.Info().Msgf("%s is successfully updated", kernelsCfgPath)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func listContainersKernels(dii container.Image, newkcfg *config.KernelConfig,
|
|
|
|
|
download bool) (err error) {
|
|
|
|
|
|
2023-05-23 16:54:34 +00:00
|
|
|
|
c, err := container.New(dii.Distro)
|
2023-05-13 10:47:47 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-23 16:54:34 +00:00
|
|
|
|
kernels, err := c.Kernels()
|
2023-05-13 10:47:47 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-23 16:54:34 +00:00
|
|
|
|
newkcfg.Kernels = append(newkcfg.Kernels, kernels...)
|
2023-05-13 10:47: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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func shuffleStrings(a []string) []string {
|
|
|
|
|
// 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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FIXME too many parameters
|
2023-05-18 18:27:51 +00:00
|
|
|
|
func GenerateKernels(km config.Target, registry string,
|
2023-05-21 21:43:18 +00:00
|
|
|
|
commands []config.DockerCommand, max, retries, threads int,
|
2023-05-13 10:47:47 +00:00
|
|
|
|
download, force, headers, shuffle, update bool,
|
|
|
|
|
shutdown *bool) (err error) {
|
|
|
|
|
|
2023-05-23 13:20:48 +00:00
|
|
|
|
container.Commands = commands
|
|
|
|
|
container.Registry = registry
|
|
|
|
|
|
2023-05-13 10:47:47 +00:00
|
|
|
|
log.Info().Msgf("Generating for kernel mask %v", km)
|
|
|
|
|
|
|
|
|
|
_, err = GenRootfsImage(container.Image{Name: km.DockerName()},
|
|
|
|
|
download)
|
|
|
|
|
if err != nil || *shutdown {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pkgs, err := MatchPackages(km)
|
|
|
|
|
if err != nil || *shutdown {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if shuffle {
|
|
|
|
|
pkgs = shuffleStrings(pkgs)
|
|
|
|
|
}
|
2023-05-21 21:43:18 +00:00
|
|
|
|
|
|
|
|
|
swg := sizedwaitgroup.New(threads)
|
|
|
|
|
|
2023-05-13 10:47:47 +00:00
|
|
|
|
for i, pkg := range pkgs {
|
|
|
|
|
if *shutdown {
|
|
|
|
|
err = nil
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-05-22 06:58:13 +00:00
|
|
|
|
|
2023-05-21 21:43:18 +00:00
|
|
|
|
swg.Add()
|
2023-05-22 06:58:13 +00:00
|
|
|
|
|
|
|
|
|
if max <= 0 {
|
|
|
|
|
log.Print("Max is reached")
|
2023-05-22 07:20:35 +00:00
|
|
|
|
swg.Done()
|
2023-05-22 06:58:13 +00:00
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-23 13:46:16 +00:00
|
|
|
|
log.Info().Msgf("%d/%d %s", i+1, len(pkgs), pkg)
|
|
|
|
|
|
|
|
|
|
go func(p string) {
|
2023-05-21 21:43:18 +00:00
|
|
|
|
defer swg.Done()
|
|
|
|
|
var attempt int
|
|
|
|
|
for {
|
|
|
|
|
attempt++
|
|
|
|
|
|
|
|
|
|
if *shutdown {
|
|
|
|
|
err = nil
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-23 13:46:16 +00:00
|
|
|
|
err = installKernel(km, p, force, headers)
|
2023-05-21 21:43:18 +00:00
|
|
|
|
if err == nil {
|
|
|
|
|
max--
|
|
|
|
|
break
|
|
|
|
|
} else if attempt >= retries {
|
|
|
|
|
log.Error().Err(err).Msg("install kernel")
|
|
|
|
|
log.Debug().Msg("skip")
|
|
|
|
|
break
|
|
|
|
|
} else {
|
|
|
|
|
log.Warn().Err(err).Msg("install kernel")
|
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
|
log.Info().Msg("retry")
|
|
|
|
|
}
|
2023-05-13 10:47:47 +00:00
|
|
|
|
}
|
2023-05-23 13:46:16 +00:00
|
|
|
|
}(pkg)
|
2023-05-13 10:47:47 +00:00
|
|
|
|
}
|
2023-05-21 21:43:18 +00:00
|
|
|
|
swg.Wait()
|
2023-05-13 10:47:47 +00:00
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|