1
0

refactor: move kernelinfo to distro module

This commit is contained in:
dump_stack() 2023-05-23 21:33:50 +00:00
parent 0edb0ac0af
commit c1ec4add81
Signed by: dump_stack
GPG Key ID: BE44DA8C062D87DC
11 changed files with 59 additions and 54 deletions

View File

@ -165,7 +165,7 @@ type Artifact struct {
Preload []PreloadModule Preload []PreloadModule
} }
func (ka Artifact) checkSupport(ki KernelInfo, km Target) ( func (ka Artifact) checkSupport(ki distro.KernelInfo, km Target) (
supported bool, err error) { supported bool, err error) {
if ki.Distro.ID != km.Distro.ID { 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 // 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 { for _, km := range ka.Targets {
supported, err = ka.checkSupport(ki, km) supported, err = ka.checkSupport(ki, km)
if supported { if supported {
@ -195,40 +195,9 @@ func (ka Artifact) Supported(ki KernelInfo) (supported bool, err error) {
return 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 // KernelConfig is the ~/.out-of-tree/kernels.toml configuration description
type KernelConfig struct { type KernelConfig struct {
Kernels []KernelInfo Kernels []distro.KernelInfo
} }
func readFileAll(path string) (buf []byte, err error) { func readFileAll(path string) (buf []byte, err error) {

View File

@ -120,7 +120,7 @@ func New(dist distro.Distro) (c Container, err error) {
return return
} }
func NewFromKernelInfo(ki config.KernelInfo) ( func NewFromKernelInfo(ki distro.KernelInfo) (
c Container, err error) { c Container, err error) {
c.name = ki.ContainerName c.name = ki.ContainerName
@ -328,7 +328,7 @@ func (c Container) Run(workdir string, cmds []string) (out string, err error) {
return return
} }
func (c Container) Kernels() (kernels []config.KernelInfo, err error) { func (c Container) Kernels() (kernels []distro.KernelInfo, err error) {
var libmodules, boot string var libmodules, boot string
for _, volume := range c.Volumes { for _, volume := range c.Volumes {
switch volume.Dest { switch volume.Dest {
@ -365,7 +365,7 @@ func (c Container) Kernels() (kernels []config.KernelInfo, err error) {
continue continue
} }
ki := config.KernelInfo{ ki := distro.KernelInfo{
Distro: c.dist, Distro: c.dist,
KernelVersion: krel.Name(), KernelVersion: krel.Name(),
KernelRelease: krel.Name(), KernelRelease: krel.Name(),

5
db.go
View File

@ -13,6 +13,7 @@ import (
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
"code.dumpstack.io/tools/out-of-tree/config" "code.dumpstack.io/tools/out-of-tree/config"
"code.dumpstack.io/tools/out-of-tree/distro"
"code.dumpstack.io/tools/out-of-tree/qemu" "code.dumpstack.io/tools/out-of-tree/qemu"
) )
@ -28,7 +29,7 @@ type logEntry struct {
qemu.System qemu.System
config.Artifact config.Artifact
config.KernelInfo distro.KernelInfo
phasesResult 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, 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, " + stmt, err := db.Prepare("INSERT INTO log (name, type, tag, " +
"distro_type, distro_release, kernel_release, " + "distro_type, distro_release, kernel_release, " +

View File

@ -15,6 +15,7 @@ import (
"gopkg.in/logrusorgru/aurora.v2" "gopkg.in/logrusorgru/aurora.v2"
"code.dumpstack.io/tools/out-of-tree/config" "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/fs"
"code.dumpstack.io/tools/out-of-tree/qemu" "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, 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) km, err := kernelMask(kernel)
if err != nil { if err != nil {

View File

@ -320,7 +320,7 @@ func ContainerKernels(d container.Image, kcfg *config.KernelConfig) (err error)
release := strings.Replace(pkgname, "linux-image-", "", -1) release := strings.Replace(pkgname, "linux-image-", "", -1)
ki := config.KernelInfo{ ki := distro.KernelInfo{
Distro: d.Distro, Distro: d.Distro,
KernelVersion: path.Base(modules), KernelVersion: path.Base(modules),
KernelRelease: release, KernelRelease: release,

32
distro/kernel.go Normal file
View File

@ -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
}

View File

@ -13,6 +13,7 @@ import (
"time" "time"
"code.dumpstack.io/tools/out-of-tree/config" "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/fs"
"code.dumpstack.io/tools/out-of-tree/qemu" "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") return errors.New("No kernels found")
} }
ki := config.KernelInfo{} ki := distro.KernelInfo{}
for _, k := range kcfg.Kernels { for _, k := range kcfg.Kernels {
if k.RootFS == image { if k.RootFS == image {
ki = k ki = k

View File

@ -270,7 +270,7 @@ func listContainersKernels(dii container.Image, newkcfg *config.KernelConfig,
return return
} }
func hasKernel(ki config.KernelInfo, kcfg config.KernelConfig) bool { func hasKernel(ki distro.KernelInfo, kcfg config.KernelConfig) bool {
for _, sk := range kcfg.Kernels { for _, sk := range kcfg.Kernels {
if sk == ki { if sk == ki {
return true return true

View File

@ -76,7 +76,7 @@ func genHostKernels(download bool) (kcfg config.KernelConfig, err error) {
continue continue
} }
ki := config.KernelInfo{ ki := distro.KernelInfo{
Distro: distro.Distro{ Distro: distro.Distro{
ID: distroType, ID: distroType,
Release: si.OS.Version, Release: si.OS.Version,

10
pew.go
View File

@ -251,7 +251,7 @@ func applyPatches(src string, ka config.Artifact) (err error) {
} }
func build(flog zerolog.Logger, tmp string, ka config.Artifact, 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) { outdir, outpath, output string, err error) {
target := fmt.Sprintf("%d", rand.Int()) target := fmt.Sprintf("%d", rand.Int())
@ -404,7 +404,7 @@ func copyFile(sourcePath, destinationPath string) (err error) {
return destinationFile.Close() 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) { res *phasesResult, dist, tag, binary string, db *sql.DB) {
colored := "" colored := ""
@ -544,7 +544,7 @@ func copyTest(q *qemu.System, testPath string, ka config.Artifact) (
return 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) _, err = q.Command("root", "mkdir -p /lib/modules/"+ki.KernelRelease)
if err != nil { if err != nil {
return return
@ -576,7 +576,7 @@ func copyStandardModules(q *qemu.System, ki config.KernelInfo) (err error) {
} }
func (cmd PewCmd) testArtifact(swg *sizedwaitgroup.SizedWaitGroup, func (cmd PewCmd) testArtifact(swg *sizedwaitgroup.SizedWaitGroup,
ka config.Artifact, ki config.KernelInfo) { ka config.Artifact, ki distro.KernelInfo) {
defer swg.Done() 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 {
// FisherYates shuffle // FisherYates shuffle
for i := len(a) - 1; i > 0; i-- { for i := len(a) - 1; i > 0; i-- {
j := rand.Intn(i + 1) j := rand.Intn(i + 1)

View File

@ -16,11 +16,12 @@ import (
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"code.dumpstack.io/tools/out-of-tree/config" "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/fs"
"code.dumpstack.io/tools/out-of-tree/qemu" "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) { dockerTimeout time.Duration) (err error) {
for _, pm := range ka.Preload { for _, pm := range ka.Preload {
@ -32,7 +33,7 @@ func preloadModules(q *qemu.System, ka config.Artifact, ki config.KernelInfo,
return 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) { dockerTimeout time.Duration) (err error) {
var workPath, cache string var workPath, cache string
@ -57,7 +58,7 @@ func preload(q *qemu.System, ki config.KernelInfo, pm config.PreloadModule,
return 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) { dockerTimeout time.Duration, cache string) (err error) {
tmp, err := fs.TempDir() tmp, err := fs.TempDir()
@ -90,7 +91,7 @@ func buildAndInsmod(workPath string, q *qemu.System, ki config.KernelInfo,
return return
} }
func buildPreload(workPath, tmp string, ki config.KernelInfo, func buildPreload(workPath, tmp string, ki distro.KernelInfo,
dockerTimeout time.Duration) (artifact string, err error) { dockerTimeout time.Duration) (artifact string, err error) {
ka, err := config.ReadArtifactConfig(workPath + "/.out-of-tree.toml") ka, err := config.ReadArtifactConfig(workPath + "/.out-of-tree.toml")
@ -114,7 +115,7 @@ func buildPreload(workPath, tmp string, ki config.KernelInfo,
return return
} }
func cloneOrPull(repo string, ki config.KernelInfo) (workPath, cache string, func cloneOrPull(repo string, ki distro.KernelInfo) (workPath, cache string,
err error) { err error) {
base := config.Dir("preload") base := config.Dir("preload")