2018-11-17 20:24:09 +00:00
|
|
|
|
// Copyright 2018 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 main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
2018-11-28 22:41:17 +00:00
|
|
|
|
"io/ioutil"
|
|
|
|
|
"log"
|
2019-08-12 22:58:34 +00:00
|
|
|
|
"math"
|
|
|
|
|
"math/rand"
|
2018-11-28 22:41:17 +00:00
|
|
|
|
"os"
|
|
|
|
|
"os/exec"
|
|
|
|
|
"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"
|
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 {
|
|
|
|
|
NoDownload bool `help:"do not download qemu image while kernel generation"`
|
|
|
|
|
UseHost bool `help:"also use host kernels"`
|
|
|
|
|
|
|
|
|
|
List KernelListCmd `cmd:"" help:"list kernels"`
|
|
|
|
|
Autogen KernelAutogenCmd `cmd:"" help:"generate kernels based on the current config"`
|
|
|
|
|
DockerRegen KernelDockerRegenCmd `cmd:"" help:"regenerate kernels config from out_of_tree_* docker images"`
|
|
|
|
|
Genall KernelGenallCmd `cmd:"" help:"generate all kernels for distro"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type KernelListCmd struct{}
|
|
|
|
|
|
|
|
|
|
func (cmd *KernelListCmd) Run(g *Globals) (err error) {
|
|
|
|
|
kcfg, err := config.ReadKernelConfig(g.Config.Kernels)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
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 {
|
|
|
|
|
Max int64 `help:"download random kernels from set defined by regex in release_mask, but no more than X for each of release_mask" default:"100500"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func kernelAutogenHandler(workPath, registry string,
|
|
|
|
|
commands []config.DockerCommand,
|
|
|
|
|
max int64, host, download bool) (err error) {
|
|
|
|
|
return errors.New("MEH")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (cmd *KernelAutogenCmd) Run(kernelCmd *KernelCmd, g *Globals) (err error) {
|
|
|
|
|
ka, err := config.ReadArtifactConfig(g.WorkDir + "/.out-of-tree.toml")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, sk := range ka.SupportedKernels {
|
|
|
|
|
if sk.DistroRelease == "" {
|
|
|
|
|
err = errors.New("Please set distro_release")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = generateKernels(sk, g.Config.Docker.Registry, g.Config.Docker.Commands, cmd.Max, !kernelCmd.NoDownload)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return updateKernelsCfg(kernelCmd.UseHost, !kernelCmd.NoDownload)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type KernelDockerRegenCmd struct{}
|
|
|
|
|
|
|
|
|
|
func (cmd *KernelDockerRegenCmd) Run(kernelCmd *KernelCmd, g *Globals) (err error) {
|
|
|
|
|
dockerImages, err := listDockerImages()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, d := range dockerImages {
|
|
|
|
|
var imagePath string
|
|
|
|
|
imagePath, err = dockerImagePath(config.KernelMask{
|
|
|
|
|
DistroType: d.DistroType,
|
|
|
|
|
DistroRelease: d.DistroRelease,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cmd := exec.Command("docker", "build", "-t",
|
|
|
|
|
d.ContainerName, imagePath)
|
|
|
|
|
var rawOutput []byte
|
|
|
|
|
rawOutput, err = cmd.CombinedOutput()
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Println("docker build:", string(rawOutput))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = kickImage(d.ContainerName)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Println("kick image", d.ContainerName, ":", err)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = copyKernels(d.ContainerName)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Println("copy kernels", d.ContainerName, ":", err)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
km := config.KernelMask{
|
|
|
|
|
DistroType: distroType,
|
|
|
|
|
DistroRelease: cmd.Ver,
|
|
|
|
|
ReleaseMask: ".*",
|
|
|
|
|
}
|
|
|
|
|
err = generateKernels(km, g.Config.Docker.Registry, g.Config.Docker.Commands, math.MaxInt64, !kernelCmd.NoDownload)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return updateKernelsCfg(kernelCmd.UseHost, !kernelCmd.NoDownload)
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-02 19:54:09 +00:00
|
|
|
|
func matchDebianHeadersPkg(container, mask string, generic bool) (
|
|
|
|
|
pkgs []string, err error) {
|
2018-11-28 22:41:17 +00:00
|
|
|
|
|
2018-12-02 19:54:09 +00:00
|
|
|
|
cmd := "apt-cache search linux-headers | cut -d ' ' -f 1"
|
2018-12-08 02:53:29 +00:00
|
|
|
|
output, err := dockerRun(time.Minute, container, "/tmp", cmd)
|
2018-11-28 22:41:17 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-02 19:54:09 +00:00
|
|
|
|
r, err := regexp.Compile("linux-headers-" + mask)
|
2018-11-28 22:41:17 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-08 02:53:29 +00:00
|
|
|
|
kernels := r.FindAll([]byte(output), -1)
|
2018-11-28 22:41:17 +00:00
|
|
|
|
|
|
|
|
|
for _, k := range kernels {
|
|
|
|
|
pkg := string(k)
|
|
|
|
|
if generic && !strings.HasSuffix(pkg, "generic") {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2018-12-02 19:54:09 +00:00
|
|
|
|
if pkg == "linux-headers-generic" {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2018-11-28 22:41:17 +00:00
|
|
|
|
pkgs = append(pkgs, pkg)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
output, err := dockerRun(time.Minute, container, "/tmp", cmd)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r, err := regexp.Compile("kernel-devel-" + mask)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, k := range r.FindAll([]byte(output), -1) {
|
|
|
|
|
pkgs = append(pkgs, string(k))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
path = usr.HomeDir + "/.out-of-tree/"
|
|
|
|
|
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-01-30 11:15:12 +00:00
|
|
|
|
cmd := exec.Command("docker", "images", "-q", sk.DockerName())
|
|
|
|
|
rawOutput, err := cmd.CombinedOutput()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if exists(dockerPath) && string(rawOutput) != "" {
|
2018-11-28 22:41:17 +00:00
|
|
|
|
log.Printf("Base image for %s:%s found",
|
|
|
|
|
sk.DistroType.String(), sk.DistroRelease)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-10 02:39:55 +00:00
|
|
|
|
log.Printf("Base image for %s:%s not found, start generating",
|
|
|
|
|
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 {
|
|
|
|
|
switch c.DistroType {
|
|
|
|
|
case config.Ubuntu:
|
|
|
|
|
d += "RUN " + c.Command + "\n"
|
|
|
|
|
case config.CentOS:
|
|
|
|
|
d += "RUN " + c.Command + "\n"
|
|
|
|
|
case config.Debian:
|
|
|
|
|
d += "RUN " + c.Command + "\n"
|
|
|
|
|
default:
|
|
|
|
|
err = fmt.Errorf("%s not yet supported",
|
|
|
|
|
sk.DistroType.String())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-28 22:41:17 +00:00
|
|
|
|
switch sk.DistroType {
|
|
|
|
|
case config.Ubuntu:
|
|
|
|
|
d += "ENV DEBIAN_FRONTEND=noninteractive\n"
|
2019-09-05 18:18:18 +00:00
|
|
|
|
if sk.DistroRelease >= "16.04" {
|
|
|
|
|
from := "http://.*ubuntu/"
|
|
|
|
|
to := "mirror://mirrors.ubuntu.com/mirrors.txt"
|
|
|
|
|
file := "/etc/apt/sources.list"
|
|
|
|
|
s := fmt.Sprintf("sed -i 's;%s;%s;' %s", from, to, file)
|
|
|
|
|
d += "RUN " + s + "\n"
|
|
|
|
|
}
|
2018-11-28 22:41:17 +00:00
|
|
|
|
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"
|
|
|
|
|
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 {
|
|
|
|
|
log.Println("Old CentOS requires `vsyscall=emulate` " +
|
|
|
|
|
"on the latest kernels")
|
|
|
|
|
log.Println("Check out `A note about vsyscall` " +
|
|
|
|
|
"at https://hub.docker.com/_/centos")
|
|
|
|
|
log.Println("See also https://lwn.net/Articles/446528/")
|
|
|
|
|
err = fmt.Errorf("vsyscall is not available")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
|
if sk.DistroRelease == "8" {
|
|
|
|
|
// FIXME CentOS Vault repository list for 8 is empty
|
|
|
|
|
// at the time of this fix; check for it and use a
|
|
|
|
|
// workaround if it's still empty
|
|
|
|
|
d += `RUN grep enabled /etc/yum.repos.d/CentOS-Vault.repo` +
|
|
|
|
|
` || echo -e '[8.0.1905]\nbaseurl=http://vault.centos.org/8.0.1905/BaseOS/$basearch/os/'` +
|
|
|
|
|
` >> /etc/yum.repos.d/CentOS-Vault.repo` + "\n"
|
|
|
|
|
}
|
|
|
|
|
|
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 {
|
2020-05-30 13:42:04 +00:00
|
|
|
|
d += "RUN yum -y install drpm grub2-tools-minimal " +
|
|
|
|
|
"elfutils-libelf-devel\n"
|
2020-05-30 12:40:12 +00:00
|
|
|
|
}
|
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-01-30 11:15:12 +00:00
|
|
|
|
cmd = exec.Command("docker", "build", "-t", sk.DockerName(), imagePath)
|
|
|
|
|
rawOutput, err = cmd.CombinedOutput()
|
2018-11-28 22:41:17 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
log.Printf("Base image for %s:%s generating error, see log",
|
|
|
|
|
sk.DistroType.String(), sk.DistroRelease)
|
|
|
|
|
log.Println(string(rawOutput))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log.Printf("Base image for %s:%s generating success",
|
|
|
|
|
sk.DistroType.String(), sk.DistroRelease)
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func dockerImageAppend(sk config.KernelMask, pkgname string) (err error) {
|
|
|
|
|
imagePath, err := dockerImagePath(sk)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
raw, err := ioutil.ReadFile(imagePath + "/Dockerfile")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if strings.Contains(string(raw), pkgname) {
|
|
|
|
|
// already installed kernel
|
|
|
|
|
log.Printf("kernel %s for %s:%s is already exists",
|
|
|
|
|
pkgname, sk.DistroType.String(), sk.DistroRelease)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-20 18:57:45 +00:00
|
|
|
|
var s string
|
|
|
|
|
|
|
|
|
|
switch sk.DistroType {
|
|
|
|
|
case config.Ubuntu:
|
|
|
|
|
imagepkg := strings.Replace(pkgname, "headers", "image", -1)
|
2018-12-02 19:54:09 +00:00
|
|
|
|
|
2019-08-20 18:57:45 +00:00
|
|
|
|
log.Printf("Start adding kernel %s for %s:%s",
|
|
|
|
|
imagepkg, sk.DistroType.String(), sk.DistroRelease)
|
2018-11-28 22:41:17 +00:00
|
|
|
|
|
2019-08-20 18:57:45 +00:00
|
|
|
|
s = fmt.Sprintf("RUN apt-get install -y %s %s\n", imagepkg,
|
|
|
|
|
pkgname)
|
|
|
|
|
case config.CentOS:
|
|
|
|
|
imagepkg := strings.Replace(pkgname, "-devel", "", -1)
|
|
|
|
|
|
|
|
|
|
log.Printf("Start adding kernel %s for %s:%s",
|
|
|
|
|
imagepkg, sk.DistroType.String(), sk.DistroRelease)
|
|
|
|
|
|
|
|
|
|
version := strings.Replace(pkgname, "kernel-devel-", "", -1)
|
|
|
|
|
|
|
|
|
|
s = fmt.Sprintf("RUN yum -y install %s %s\n", imagepkg,
|
|
|
|
|
pkgname)
|
|
|
|
|
s += fmt.Sprintf("RUN dracut --add-drivers 'e1000 ext4' -f "+
|
|
|
|
|
"/boot/initramfs-%s.img %s\n", version, version)
|
|
|
|
|
default:
|
|
|
|
|
err = fmt.Errorf("%s not yet supported", sk.DistroType.String())
|
2019-12-26 13:16:38 +00:00
|
|
|
|
return
|
2019-08-20 18:57:45 +00:00
|
|
|
|
}
|
2018-11-28 22:41:17 +00:00
|
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(imagePath+"/Dockerfile",
|
|
|
|
|
append(raw, []byte(s)...), 0644)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cmd := exec.Command("docker", "build", "-t", sk.DockerName(), imagePath)
|
|
|
|
|
rawOutput, err := cmd.CombinedOutput()
|
|
|
|
|
if err != nil {
|
|
|
|
|
// Fallback to previous state
|
|
|
|
|
werr := ioutil.WriteFile(imagePath+"/Dockerfile", raw, 0644)
|
|
|
|
|
if werr != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log.Printf("Add kernel %s for %s:%s error, see log",
|
|
|
|
|
pkgname, sk.DistroType.String(), sk.DistroRelease)
|
|
|
|
|
log.Println(string(rawOutput))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log.Printf("Add kernel %s for %s:%s success",
|
|
|
|
|
pkgname, sk.DistroType.String(), sk.DistroRelease)
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func kickImage(name string) (err error) {
|
|
|
|
|
cmd := exec.Command("docker", "run", name, "bash", "-c", "ls")
|
|
|
|
|
_, err = cmd.CombinedOutput()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func copyKernels(name string) (err error) {
|
|
|
|
|
cmd := exec.Command("docker", "ps", "-a")
|
|
|
|
|
rawOutput, err := cmd.CombinedOutput()
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Println(string(rawOutput))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r, err := regexp.Compile(".*" + name)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var containerID string
|
|
|
|
|
|
|
|
|
|
what := r.FindAll(rawOutput, -1)
|
|
|
|
|
for _, w := range what {
|
|
|
|
|
containerID = strings.Fields(string(w))[0]
|
2023-01-29 20:41:33 +00:00
|
|
|
|
_, err = exec.Command("which", "podman").CombinedOutput()
|
|
|
|
|
if err != nil {
|
|
|
|
|
break
|
|
|
|
|
}
|
2018-11-28 22:41:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
usr, err := user.Current()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
target := usr.HomeDir + "/.out-of-tree/kernels/"
|
|
|
|
|
if !exists(target) {
|
|
|
|
|
os.MkdirAll(target, os.ModePerm)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cmd = exec.Command("docker", "cp", containerID+":/boot/.", target)
|
|
|
|
|
rawOutput, err = cmd.CombinedOutput()
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Println(string(rawOutput))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-28 23:53:47 +00:00
|
|
|
|
func genKernelPath(files []os.FileInfo, kname string) string {
|
|
|
|
|
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) {
|
|
|
|
|
return file.Name()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return "unknown"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func genInitrdPath(files []os.FileInfo, kname string) string {
|
|
|
|
|
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) {
|
|
|
|
|
return file.Name()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return "unknown"
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-20 09:09:38 +00:00
|
|
|
|
func genRootfsImage(d dockerImageInfo, 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
|
|
|
|
}
|
2018-12-01 14:23:01 +00:00
|
|
|
|
imageFile := d.ContainerName + ".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 {
|
|
|
|
|
log.Println(imageFile, "not exists, start downloading...")
|
|
|
|
|
err = downloadImage(imagesPath, imageFile)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return
|
2018-11-28 23:53:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-01 14:23:01 +00:00
|
|
|
|
type dockerImageInfo struct {
|
|
|
|
|
ContainerName string
|
|
|
|
|
DistroType config.DistroType
|
|
|
|
|
DistroRelease string // 18.04/7.4.1708/9.1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func listDockerImages() (diis []dockerImageInfo, err error) {
|
|
|
|
|
cmd := exec.Command("docker", "images")
|
|
|
|
|
rawOutput, err := cmd.CombinedOutput()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r, err := regexp.Compile("out_of_tree_.*")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
containers := r.FindAll(rawOutput, -1)
|
|
|
|
|
for _, c := range containers {
|
|
|
|
|
container := strings.Fields(string(c))[0]
|
|
|
|
|
|
|
|
|
|
s := strings.Replace(container, "__", ".", -1)
|
|
|
|
|
values := strings.Split(s, "_")
|
|
|
|
|
distro, ver := values[3], values[4]
|
|
|
|
|
|
|
|
|
|
dii := dockerImageInfo{
|
|
|
|
|
ContainerName: container,
|
|
|
|
|
DistroRelease: ver,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dii.DistroType, err = config.NewDistroType(distro)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
diis = append(diis, dii)
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
dockerImages, err := listDockerImages()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-01 14:23:01 +00:00
|
|
|
|
for _, d := range dockerImages {
|
2019-08-20 09:09:38 +00:00
|
|
|
|
err = genDockerKernels(d, &newkcfg, download)
|
2018-12-01 14:23:01 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
log.Println("gen kernels", d.ContainerName, ":", 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...)
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log.Println(kernelsCfgPath, "is successfully updated")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-20 09:09:38 +00:00
|
|
|
|
func genDockerKernels(dii dockerImageInfo, newkcfg *config.KernelConfig,
|
|
|
|
|
download bool) (err error) {
|
2018-11-28 23:53:47 +00:00
|
|
|
|
|
2018-12-01 14:23:01 +00:00
|
|
|
|
name := dii.ContainerName
|
2018-11-28 23:53:47 +00:00
|
|
|
|
cmd := exec.Command("docker", "run", name, "ls", "/lib/modules")
|
|
|
|
|
rawOutput, err := cmd.CombinedOutput()
|
|
|
|
|
if err != nil {
|
2018-12-01 14:23:01 +00:00
|
|
|
|
log.Println(string(rawOutput), err)
|
2018-11-28 23:53:47 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
usr, err := user.Current()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
kernelsBase := usr.HomeDir + "/.out-of-tree/kernels/"
|
|
|
|
|
files, err := ioutil.ReadDir(kernelsBase)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-20 09:09:38 +00:00
|
|
|
|
rootfs, err := genRootfsImage(dii, download)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-28 23:53:47 +00:00
|
|
|
|
for _, k := range strings.Fields(string(rawOutput)) {
|
|
|
|
|
ki := config.KernelInfo{
|
2018-12-01 14:23:01 +00:00
|
|
|
|
DistroType: dii.DistroType,
|
|
|
|
|
DistroRelease: dii.DistroRelease,
|
2018-11-28 23:53:47 +00:00
|
|
|
|
KernelRelease: k,
|
|
|
|
|
ContainerName: name,
|
|
|
|
|
|
|
|
|
|
KernelPath: kernelsBase + genKernelPath(files, k),
|
|
|
|
|
InitrdPath: kernelsBase + genInitrdPath(files, k),
|
2019-08-20 09:09:38 +00:00
|
|
|
|
RootFS: rootfs,
|
2018-11-28 23:53:47 +00:00
|
|
|
|
}
|
|
|
|
|
newkcfg.Kernels = append(newkcfg.Kernels, ki)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func hasKernel(ki config.KernelInfo, kcfg config.KernelConfig) bool {
|
|
|
|
|
for _, sk := range kcfg.Kernels {
|
|
|
|
|
if sk == ki {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-12 22:58:34 +00:00
|
|
|
|
func shuffle(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
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-30 00:05:50 +00:00
|
|
|
|
func generateKernels(km config.KernelMask, registry string,
|
|
|
|
|
commands []config.DockerCommand, max int64,
|
2019-08-29 22:49:59 +00:00
|
|
|
|
download bool) (err error) {
|
|
|
|
|
|
2019-08-12 22:58:34 +00:00
|
|
|
|
log.Println("Generating for kernel mask", km)
|
2019-08-20 09:09:38 +00:00
|
|
|
|
|
|
|
|
|
_, err = genRootfsImage(dockerImageInfo{ContainerName: km.DockerName()},
|
|
|
|
|
download)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-30 00:05:50 +00:00
|
|
|
|
err = generateBaseDockerImage(registry, commands, km)
|
2018-12-02 19:54:09 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var pkgs []string
|
2019-08-20 18:57:45 +00:00
|
|
|
|
switch km.DistroType {
|
|
|
|
|
case config.Ubuntu:
|
|
|
|
|
pkgs, err = matchDebianHeadersPkg(km.DockerName(),
|
|
|
|
|
km.ReleaseMask, true)
|
|
|
|
|
case config.CentOS:
|
|
|
|
|
pkgs, err = matchCentOSDevelPkg(km.DockerName(),
|
|
|
|
|
km.ReleaseMask, true)
|
|
|
|
|
default:
|
|
|
|
|
err = fmt.Errorf("%s not yet supported", km.DistroType.String())
|
|
|
|
|
}
|
2018-12-02 19:54:09 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-12 22:58:34 +00:00
|
|
|
|
for i, pkg := range shuffle(pkgs) {
|
|
|
|
|
if max <= 0 {
|
|
|
|
|
log.Println("Max is reached")
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log.Println(i, "/", len(pkgs), pkg)
|
|
|
|
|
|
|
|
|
|
err = dockerImageAppend(km, pkg)
|
|
|
|
|
if err == nil {
|
2019-08-17 09:35:36 +00:00
|
|
|
|
max--
|
2019-08-12 22:58:34 +00:00
|
|
|
|
} else {
|
|
|
|
|
log.Println("dockerImageAppend", err)
|
|
|
|
|
}
|
2018-12-02 19:54:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = kickImage(km.DockerName())
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Println("kick image", km.DockerName(), ":", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = copyKernels(km.DockerName())
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Println("copy kernels", km.DockerName(), ":", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|