From c1ec4add81fc81bbae4b8e50ee22acd705f02ff6 Mon Sep 17 00:00:00 2001 From: Mikhail Klementev Date: Tue, 23 May 2023 21:33:50 +0000 Subject: [PATCH] refactor: move kernelinfo to distro module --- config/config.go | 37 +++---------------------------------- container/container.go | 6 +++--- db.go | 5 +++-- debug.go | 3 ++- distro/debian/debian.go | 2 +- distro/kernel.go | 32 ++++++++++++++++++++++++++++++++ images.go | 3 ++- kernel/kernel.go | 2 +- kernel/kernel_linux.go | 2 +- pew.go | 10 +++++----- preload.go | 11 ++++++----- 11 files changed, 59 insertions(+), 54 deletions(-) create mode 100644 distro/kernel.go diff --git a/config/config.go b/config/config.go index 56b4521..95ece3f 100644 --- a/config/config.go +++ b/config/config.go @@ -165,7 +165,7 @@ type Artifact struct { Preload []PreloadModule } -func (ka Artifact) checkSupport(ki KernelInfo, km Target) ( +func (ka Artifact) checkSupport(ki distro.KernelInfo, km Target) ( supported bool, err error) { if ki.Distro.ID != km.Distro.ID { @@ -184,7 +184,7 @@ func (ka Artifact) checkSupport(ki KernelInfo, km Target) ( } // Supported returns true if given kernel is supported by artifact -func (ka Artifact) Supported(ki KernelInfo) (supported bool, err error) { +func (ka Artifact) Supported(ki distro.KernelInfo) (supported bool, err error) { for _, km := range ka.Targets { supported, err = ka.checkSupport(ki, km) if supported { @@ -195,40 +195,9 @@ func (ka Artifact) Supported(ki KernelInfo) (supported bool, err error) { return } -// ByRootFS is sorting by .RootFS lexicographically -type ByRootFS []KernelInfo - -func (a ByRootFS) Len() int { return len(a) } -func (a ByRootFS) Swap(i, j int) { a[i], a[j] = a[j], a[i] } -func (a ByRootFS) Less(i, j int) bool { return a[i].RootFS < a[j].RootFS } - -// KernelInfo defines kernels.toml entries -type KernelInfo struct { - Distro distro.Distro - - // Must be *exactly* same as in `uname -r` - KernelVersion string - - KernelRelease string - - // Build-time information - KernelSource string // module/exploit will be build on host - ContainerName string - - // Runtime information - KernelPath string - InitrdPath string - ModulesPath string - - RootFS string - - // Debug symbols - VmlinuxPath string -} - // KernelConfig is the ~/.out-of-tree/kernels.toml configuration description type KernelConfig struct { - Kernels []KernelInfo + Kernels []distro.KernelInfo } func readFileAll(path string) (buf []byte, err error) { diff --git a/container/container.go b/container/container.go index bbcc3ee..0bb33b3 100644 --- a/container/container.go +++ b/container/container.go @@ -120,7 +120,7 @@ func New(dist distro.Distro) (c Container, err error) { return } -func NewFromKernelInfo(ki config.KernelInfo) ( +func NewFromKernelInfo(ki distro.KernelInfo) ( c Container, err error) { c.name = ki.ContainerName @@ -328,7 +328,7 @@ func (c Container) Run(workdir string, cmds []string) (out string, err error) { return } -func (c Container) Kernels() (kernels []config.KernelInfo, err error) { +func (c Container) Kernels() (kernels []distro.KernelInfo, err error) { var libmodules, boot string for _, volume := range c.Volumes { switch volume.Dest { @@ -365,7 +365,7 @@ func (c Container) Kernels() (kernels []config.KernelInfo, err error) { continue } - ki := config.KernelInfo{ + ki := distro.KernelInfo{ Distro: c.dist, KernelVersion: krel.Name(), KernelRelease: krel.Name(), diff --git a/db.go b/db.go index 0e14280..dfc447c 100644 --- a/db.go +++ b/db.go @@ -13,6 +13,7 @@ import ( _ "github.com/mattn/go-sqlite3" "code.dumpstack.io/tools/out-of-tree/config" + "code.dumpstack.io/tools/out-of-tree/distro" "code.dumpstack.io/tools/out-of-tree/qemu" ) @@ -28,7 +29,7 @@ type logEntry struct { qemu.System config.Artifact - config.KernelInfo + distro.KernelInfo phasesResult } @@ -121,7 +122,7 @@ func getVersion(db *sql.DB) (version int, err error) { } func addToLog(db *sql.DB, q *qemu.System, ka config.Artifact, - ki config.KernelInfo, res *phasesResult, tag string) (err error) { + ki distro.KernelInfo, res *phasesResult, tag string) (err error) { stmt, err := db.Prepare("INSERT INTO log (name, type, tag, " + "distro_type, distro_release, kernel_release, " + diff --git a/debug.go b/debug.go index cd22eba..36ec0fb 100644 --- a/debug.go +++ b/debug.go @@ -15,6 +15,7 @@ import ( "gopkg.in/logrusorgru/aurora.v2" "code.dumpstack.io/tools/out-of-tree/config" + "code.dumpstack.io/tools/out-of-tree/distro" "code.dumpstack.io/tools/out-of-tree/fs" "code.dumpstack.io/tools/out-of-tree/qemu" ) @@ -222,7 +223,7 @@ func (cmd *DebugCmd) Run(g *Globals) (err error) { } func firstSupported(kcfg config.KernelConfig, ka config.Artifact, - kernel string) (ki config.KernelInfo, err error) { + kernel string) (ki distro.KernelInfo, err error) { km, err := kernelMask(kernel) if err != nil { diff --git a/distro/debian/debian.go b/distro/debian/debian.go index 48ec47a..eb60db8 100644 --- a/distro/debian/debian.go +++ b/distro/debian/debian.go @@ -320,7 +320,7 @@ func ContainerKernels(d container.Image, kcfg *config.KernelConfig) (err error) release := strings.Replace(pkgname, "linux-image-", "", -1) - ki := config.KernelInfo{ + ki := distro.KernelInfo{ Distro: d.Distro, KernelVersion: path.Base(modules), KernelRelease: release, diff --git a/distro/kernel.go b/distro/kernel.go new file mode 100644 index 0000000..5008bd8 --- /dev/null +++ b/distro/kernel.go @@ -0,0 +1,32 @@ +package distro + +// ByRootFS is sorting by .RootFS lexicographically +type ByRootFS []KernelInfo + +func (a ByRootFS) Len() int { return len(a) } +func (a ByRootFS) Swap(i, j int) { a[i], a[j] = a[j], a[i] } +func (a ByRootFS) Less(i, j int) bool { return a[i].RootFS < a[j].RootFS } + +// KernelInfo defines kernels.toml entries +type KernelInfo struct { + Distro Distro + + // Must be *exactly* same as in `uname -r` + KernelVersion string + + KernelRelease string + + // Build-time information + KernelSource string // module/exploit will be build on host + ContainerName string + + // Runtime information + KernelPath string + InitrdPath string + ModulesPath string + + RootFS string + + // Debug symbols + VmlinuxPath string +} diff --git a/images.go b/images.go index 8662620..06fee97 100644 --- a/images.go +++ b/images.go @@ -13,6 +13,7 @@ import ( "time" "code.dumpstack.io/tools/out-of-tree/config" + "code.dumpstack.io/tools/out-of-tree/distro" "code.dumpstack.io/tools/out-of-tree/fs" "code.dumpstack.io/tools/out-of-tree/qemu" ) @@ -57,7 +58,7 @@ func (cmd *ImageEditCmd) Run(g *Globals) (err error) { return errors.New("No kernels found") } - ki := config.KernelInfo{} + ki := distro.KernelInfo{} for _, k := range kcfg.Kernels { if k.RootFS == image { ki = k diff --git a/kernel/kernel.go b/kernel/kernel.go index 6c828fa..3803c12 100644 --- a/kernel/kernel.go +++ b/kernel/kernel.go @@ -270,7 +270,7 @@ func listContainersKernels(dii container.Image, newkcfg *config.KernelConfig, return } -func hasKernel(ki config.KernelInfo, kcfg config.KernelConfig) bool { +func hasKernel(ki distro.KernelInfo, kcfg config.KernelConfig) bool { for _, sk := range kcfg.Kernels { if sk == ki { return true diff --git a/kernel/kernel_linux.go b/kernel/kernel_linux.go index 90405ca..9f6efe7 100644 --- a/kernel/kernel_linux.go +++ b/kernel/kernel_linux.go @@ -76,7 +76,7 @@ func genHostKernels(download bool) (kcfg config.KernelConfig, err error) { continue } - ki := config.KernelInfo{ + ki := distro.KernelInfo{ Distro: distro.Distro{ ID: distroType, Release: si.OS.Version, diff --git a/pew.go b/pew.go index 7beefd8..4131003 100644 --- a/pew.go +++ b/pew.go @@ -251,7 +251,7 @@ func applyPatches(src string, ka config.Artifact) (err error) { } func build(flog zerolog.Logger, tmp string, ka config.Artifact, - ki config.KernelInfo, dockerTimeout time.Duration) ( + ki distro.KernelInfo, dockerTimeout time.Duration) ( outdir, outpath, output string, err error) { target := fmt.Sprintf("%d", rand.Int()) @@ -404,7 +404,7 @@ func copyFile(sourcePath, destinationPath string) (err error) { return destinationFile.Close() } -func dumpResult(q *qemu.System, ka config.Artifact, ki config.KernelInfo, +func dumpResult(q *qemu.System, ka config.Artifact, ki distro.KernelInfo, res *phasesResult, dist, tag, binary string, db *sql.DB) { colored := "" @@ -544,7 +544,7 @@ func copyTest(q *qemu.System, testPath string, ka config.Artifact) ( return } -func copyStandardModules(q *qemu.System, ki config.KernelInfo) (err error) { +func copyStandardModules(q *qemu.System, ki distro.KernelInfo) (err error) { _, err = q.Command("root", "mkdir -p /lib/modules/"+ki.KernelRelease) if err != nil { return @@ -576,7 +576,7 @@ func copyStandardModules(q *qemu.System, ki config.KernelInfo) (err error) { } func (cmd PewCmd) testArtifact(swg *sizedwaitgroup.SizedWaitGroup, - ka config.Artifact, ki config.KernelInfo) { + ka config.Artifact, ki distro.KernelInfo) { defer swg.Done() @@ -797,7 +797,7 @@ func (cmd PewCmd) testArtifact(swg *sizedwaitgroup.SizedWaitGroup, } } -func shuffleKernels(a []config.KernelInfo) []config.KernelInfo { +func shuffleKernels(a []distro.KernelInfo) []distro.KernelInfo { // Fisher–Yates shuffle for i := len(a) - 1; i > 0; i-- { j := rand.Intn(i + 1) diff --git a/preload.go b/preload.go index 6a8e448..36f016e 100644 --- a/preload.go +++ b/preload.go @@ -16,11 +16,12 @@ import ( "github.com/rs/zerolog/log" "code.dumpstack.io/tools/out-of-tree/config" + "code.dumpstack.io/tools/out-of-tree/distro" "code.dumpstack.io/tools/out-of-tree/fs" "code.dumpstack.io/tools/out-of-tree/qemu" ) -func preloadModules(q *qemu.System, ka config.Artifact, ki config.KernelInfo, +func preloadModules(q *qemu.System, ka config.Artifact, ki distro.KernelInfo, dockerTimeout time.Duration) (err error) { for _, pm := range ka.Preload { @@ -32,7 +33,7 @@ func preloadModules(q *qemu.System, ka config.Artifact, ki config.KernelInfo, return } -func preload(q *qemu.System, ki config.KernelInfo, pm config.PreloadModule, +func preload(q *qemu.System, ki distro.KernelInfo, pm config.PreloadModule, dockerTimeout time.Duration) (err error) { var workPath, cache string @@ -57,7 +58,7 @@ func preload(q *qemu.System, ki config.KernelInfo, pm config.PreloadModule, return } -func buildAndInsmod(workPath string, q *qemu.System, ki config.KernelInfo, +func buildAndInsmod(workPath string, q *qemu.System, ki distro.KernelInfo, dockerTimeout time.Duration, cache string) (err error) { tmp, err := fs.TempDir() @@ -90,7 +91,7 @@ func buildAndInsmod(workPath string, q *qemu.System, ki config.KernelInfo, return } -func buildPreload(workPath, tmp string, ki config.KernelInfo, +func buildPreload(workPath, tmp string, ki distro.KernelInfo, dockerTimeout time.Duration) (artifact string, err error) { ka, err := config.ReadArtifactConfig(workPath + "/.out-of-tree.toml") @@ -114,7 +115,7 @@ func buildPreload(workPath, tmp string, ki config.KernelInfo, return } -func cloneOrPull(repo string, ki config.KernelInfo) (workPath, cache string, +func cloneOrPull(repo string, ki distro.KernelInfo) (workPath, cache string, err error) { base := config.Dir("preload")