From 2d6db97b43c60c09c049ec804c57c308ca9bfbe8 Mon Sep 17 00:00:00 2001 From: Mikhail Klementev Date: Thu, 16 Feb 2023 10:21:44 +0000 Subject: [PATCH] Add support for applying patches --- config/config.go | 8 +++++++ gen.go | 3 +++ pew.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+) diff --git a/config/config.go b/config/config.go index 09d076a..5fdc5cf 100644 --- a/config/config.go +++ b/config/config.go @@ -115,6 +115,12 @@ type FileTransfer struct { Remote string } +type Patch struct { + Path string + Source string + Script string +} + // Artifact is for .out-of-tree.toml type Artifact struct { Name string @@ -140,6 +146,8 @@ type Artifact struct { DisableKpti bool } + Patches []Patch + Make struct { Target string } diff --git a/gen.go b/gen.go index d690fb6..5a55138 100644 --- a/gen.go +++ b/gen.go @@ -39,6 +39,9 @@ func genConfig(at config.ArtifactType) (err error) { a.Preload = append(a.Preload, config.PreloadModule{ Repo: "Repo name (e.g. https://github.com/openwall/lkrg)", }) + a.Patches = append(a.Patches, config.Patch{ + Path: "/path/to/profiling.patch", + }) buf, err := toml.Marshal(&a) if err != nil { diff --git a/pew.go b/pew.go index d6968ca..2b1c2af 100644 --- a/pew.go +++ b/pew.go @@ -157,6 +157,59 @@ func dockerRun(timeout time.Duration, container, workdir, command string) ( return } +func sh(workdir, cmd string) (output string, err error) { + command := exec.Command("sh", "-c", "cd "+workdir+" && "+cmd) + raw, err := command.CombinedOutput() + output = string(raw) + if err != nil { + e := fmt.Sprintf("%v %v output: %v", cmd, err, output) + err = errors.New(e) + } + return +} + +func applyPatches(src string, ka config.Artifact) (err error) { + for i, patch := range ka.Patches { + name := fmt.Sprintf("patch_%02d", i) + + path := src + "/" + name + ".diff" + if patch.Source != "" && patch.Path != "" { + err = errors.New("path and source are mutually exclusive") + return + } else if patch.Source != "" { + err = os.WriteFile(path, []byte(patch.Source), 0644) + if err != nil { + return + } + } else if patch.Path != "" { + err = copy.Copy(patch.Path, path) + if err != nil { + return + } + } + + if patch.Source != "" || patch.Path != "" { + _, err = sh(src, "patch < "+path) + if err != nil { + return + } + } + + if patch.Script != "" { + script := src + "/" + name + ".sh" + err = os.WriteFile(script, []byte(patch.Script), 0755) + if err != nil { + return + } + _, err = sh(src, script) + if err != nil { + return + } + } + } + return +} + func build(tmp string, ka config.Artifact, ki config.KernelInfo, dockerTimeout time.Duration) (outPath, output string, err error) { @@ -169,6 +222,11 @@ func build(tmp string, ka config.Artifact, ki config.KernelInfo, return } + err = applyPatches(tmpSourcePath, ka) + if err != nil { + return + } + outPath = tmpSourcePath + "/" + target if ka.Type == config.KernelModule { outPath += ".ko"