2020-06-14 20:14:59 +00:00
|
|
|
// Copyright 2020 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 (
|
|
|
|
"crypto/sha1"
|
|
|
|
"encoding/hex"
|
|
|
|
"errors"
|
|
|
|
"os"
|
|
|
|
"os/user"
|
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-git/go-git/v5"
|
2023-03-18 21:30:07 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2020-06-14 20:14:59 +00:00
|
|
|
|
|
|
|
"code.dumpstack.io/tools/out-of-tree/config"
|
2023-05-13 09:17:57 +00:00
|
|
|
"code.dumpstack.io/tools/out-of-tree/fs"
|
2020-06-14 20:14:59 +00:00
|
|
|
"code.dumpstack.io/tools/out-of-tree/qemu"
|
|
|
|
)
|
|
|
|
|
|
|
|
func preloadModules(q *qemu.System, ka config.Artifact, ki config.KernelInfo,
|
|
|
|
dockerTimeout time.Duration) (err error) {
|
|
|
|
|
|
|
|
for _, pm := range ka.Preload {
|
|
|
|
err = preload(q, ki, pm, dockerTimeout)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func preload(q *qemu.System, ki config.KernelInfo, pm config.PreloadModule,
|
|
|
|
dockerTimeout time.Duration) (err error) {
|
|
|
|
|
|
|
|
var workPath, cache string
|
|
|
|
if pm.Path != "" {
|
2023-03-18 21:30:07 +00:00
|
|
|
log.Print("Use non-git path for preload module (no cache)")
|
2020-06-14 20:14:59 +00:00
|
|
|
workPath = pm.Path
|
|
|
|
} else if pm.Repo != "" {
|
|
|
|
workPath, cache, err = cloneOrPull(pm.Repo, ki)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
errors.New("No repo/path in preload entry")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = buildAndInsmod(workPath, q, ki, dockerTimeout, cache)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(pm.TimeoutAfterLoad.Duration)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildAndInsmod(workPath string, q *qemu.System, ki config.KernelInfo,
|
|
|
|
dockerTimeout time.Duration, cache string) (err error) {
|
|
|
|
|
2023-05-13 11:31:47 +00:00
|
|
|
tmp, err := fs.TempDir()
|
2020-06-14 20:14:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
|
|
|
|
var artifact string
|
2023-05-13 09:17:57 +00:00
|
|
|
if fs.PathExists(cache) {
|
2020-06-14 20:14:59 +00:00
|
|
|
artifact = cache
|
|
|
|
} else {
|
|
|
|
artifact, err = buildPreload(workPath, tmp, ki, dockerTimeout)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if cache != "" {
|
|
|
|
err = copyFile(artifact, cache)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
output, err := q.CopyAndInsmod(artifact)
|
|
|
|
if err != nil {
|
2023-03-18 21:30:07 +00:00
|
|
|
log.Print(output)
|
2020-06-14 20:14:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildPreload(workPath, tmp string, ki config.KernelInfo,
|
|
|
|
dockerTimeout time.Duration) (artifact string, err error) {
|
|
|
|
|
|
|
|
ka, err := config.ReadArtifactConfig(workPath + "/.out-of-tree.toml")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ka.SourcePath = workPath
|
|
|
|
|
2023-05-18 16:07:24 +00:00
|
|
|
km := config.KernelMask{
|
|
|
|
Distro: ki.Distro,
|
|
|
|
ReleaseMask: ki.KernelRelease,
|
2020-06-14 20:14:59 +00:00
|
|
|
}
|
|
|
|
ka.SupportedKernels = []config.KernelMask{km}
|
|
|
|
|
|
|
|
if ka.Docker.Timeout.Duration != 0 {
|
|
|
|
dockerTimeout = ka.Docker.Timeout.Duration
|
|
|
|
}
|
|
|
|
|
2023-05-02 11:10:57 +00:00
|
|
|
_, artifact, _, err = build(log.Logger, tmp, ka, ki, dockerTimeout)
|
2020-06-14 20:14:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func cloneOrPull(repo string, ki config.KernelInfo) (workPath, cache string, err error) {
|
|
|
|
usr, err := user.Current()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
base := filepath.Join(usr.HomeDir, "/.out-of-tree/preload/")
|
|
|
|
workPath = filepath.Join(base, "/repos/", sha1sum(repo))
|
|
|
|
|
|
|
|
var r *git.Repository
|
2023-05-13 09:17:57 +00:00
|
|
|
if fs.PathExists(workPath) {
|
2020-06-14 20:14:59 +00:00
|
|
|
r, err = git.PlainOpen(workPath)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var w *git.Worktree
|
|
|
|
w, err = r.Worktree()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = w.Pull(&git.PullOptions{})
|
|
|
|
if err != nil && err != git.NoErrAlreadyUpToDate {
|
2023-03-18 21:30:07 +00:00
|
|
|
log.Print(repo, "pull error:", err)
|
2020-06-14 20:14:59 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
r, err = git.PlainClone(workPath, false, &git.CloneOptions{URL: repo})
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ref, err := r.Head()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cachedir := filepath.Join(base, "/cache/")
|
|
|
|
os.MkdirAll(cachedir, 0700)
|
|
|
|
|
|
|
|
filename := sha1sum(repo + ki.KernelPath + ref.Hash().String())
|
|
|
|
cache = filepath.Join(cachedir, filename)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func sha1sum(data string) string {
|
|
|
|
h := sha1.Sum([]byte(data))
|
|
|
|
return hex.EncodeToString(h[:])
|
|
|
|
}
|