2023-03-18 21:30:07 +00:00
|
|
|
// Copyright 2023 Mikhail Klementev. All rights reserved.
|
2019-08-31 07:50:29 +00:00
|
|
|
// Use of this source code is governed by a AGPLv3 license
|
|
|
|
// (or later) that can be found in the LICENSE file.
|
|
|
|
|
2023-03-18 21:30:07 +00:00
|
|
|
//go:build linux
|
2019-08-31 07:50:29 +00:00
|
|
|
// +build linux
|
|
|
|
|
2023-05-13 10:47:47 +00:00
|
|
|
package kernel
|
2019-08-31 07:50:29 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
|
2023-03-18 21:30:07 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2019-08-31 07:50:29 +00:00
|
|
|
"github.com/zcalusic/sysinfo"
|
2023-03-18 21:30:07 +00:00
|
|
|
|
|
|
|
"code.dumpstack.io/tools/out-of-tree/config"
|
2023-05-13 10:14:45 +00:00
|
|
|
"code.dumpstack.io/tools/out-of-tree/container"
|
2023-05-13 09:17:57 +00:00
|
|
|
"code.dumpstack.io/tools/out-of-tree/fs"
|
2019-08-31 07:50:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func genHostKernels(download bool) (kcfg config.KernelConfig, err error) {
|
|
|
|
si := sysinfo.SysInfo{}
|
|
|
|
si.GetSysInfo()
|
|
|
|
|
|
|
|
distroType, err := config.NewDistroType(si.OS.Vendor)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := exec.Command("ls", "/lib/modules")
|
2023-03-19 13:14:14 +00:00
|
|
|
log.Debug().Msgf("%v", cmd)
|
|
|
|
|
2019-08-31 07:50:29 +00:00
|
|
|
rawOutput, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
2023-03-18 21:30:07 +00:00
|
|
|
log.Print(string(rawOutput), err)
|
2019-08-31 07:50:29 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
kernelsBase := "/boot/"
|
2023-04-07 10:27:59 +00:00
|
|
|
bootfiles, err := ioutil.ReadDir(kernelsBase)
|
2019-08-31 07:50:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// only for compatibility, docker is not really used
|
2023-05-13 10:14:45 +00:00
|
|
|
dii := container.Image{
|
2023-04-06 19:05:21 +00:00
|
|
|
Name: config.KernelMask{
|
2019-08-31 07:50:29 +00:00
|
|
|
DistroType: distroType,
|
|
|
|
DistroRelease: si.OS.Version,
|
|
|
|
}.DockerName(),
|
|
|
|
}
|
|
|
|
|
2023-05-13 10:47:47 +00:00
|
|
|
rootfs, err := GenRootfsImage(dii, download)
|
2019-08-31 07:50:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-07 10:27:59 +00:00
|
|
|
for _, krel := range strings.Fields(string(rawOutput)) {
|
|
|
|
log.Debug().Msgf("generate config entry for %s", krel)
|
|
|
|
|
|
|
|
var kernelFile, initrdFile string
|
|
|
|
kernelFile, err = findKernelFile(bootfiles, krel)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn().Msgf("cannot find kernel %s", krel)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
initrdFile, err = findInitrdFile(bootfiles, krel)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn().Msgf("cannot find initrd %s", krel)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-08-31 07:50:29 +00:00
|
|
|
ki := config.KernelInfo{
|
|
|
|
DistroType: distroType,
|
|
|
|
DistroRelease: si.OS.Version,
|
2023-04-07 10:27:59 +00:00
|
|
|
KernelRelease: krel,
|
2019-08-31 07:50:29 +00:00
|
|
|
|
2023-04-07 10:27:59 +00:00
|
|
|
KernelSource: "/lib/modules/" + krel + "/build",
|
2019-08-31 07:50:29 +00:00
|
|
|
|
2023-04-07 10:27:59 +00:00
|
|
|
KernelPath: kernelsBase + kernelFile,
|
|
|
|
InitrdPath: kernelsBase + initrdFile,
|
2019-08-31 07:50:29 +00:00
|
|
|
RootFS: rootfs,
|
|
|
|
}
|
|
|
|
|
2023-04-07 10:27:59 +00:00
|
|
|
vmlinux := "/usr/lib/debug/boot/vmlinux-" + krel
|
2023-03-18 21:30:07 +00:00
|
|
|
log.Print("vmlinux", vmlinux)
|
2023-05-13 09:17:57 +00:00
|
|
|
if fs.PathExists(vmlinux) {
|
2019-08-31 07:50:29 +00:00
|
|
|
ki.VmlinuxPath = vmlinux
|
|
|
|
}
|
|
|
|
|
|
|
|
kcfg.Kernels = append(kcfg.Kernels, ki)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|