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 (
|
2024-02-20 12:18:43 +00:00
|
|
|
"os"
|
2019-08-31 07:50:29 +00:00
|
|
|
"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
|
|
|
|
2024-02-20 12:18:43 +00:00
|
|
|
"code.dumpstack.io/tools/out-of-tree/container"
|
2023-05-18 16:07:24 +00:00
|
|
|
"code.dumpstack.io/tools/out-of-tree/distro"
|
2023-05-13 09:17:57 +00:00
|
|
|
"code.dumpstack.io/tools/out-of-tree/fs"
|
2019-08-31 07:50:29 +00:00
|
|
|
)
|
|
|
|
|
2023-05-23 22:00:20 +00:00
|
|
|
func GenHostKernels(download bool) (kernels []distro.KernelInfo, err error) {
|
2019-08-31 07:50:29 +00:00
|
|
|
si := sysinfo.SysInfo{}
|
|
|
|
si.GetSysInfo()
|
|
|
|
|
2023-05-18 16:07:24 +00:00
|
|
|
distroType, err := distro.NewID(si.OS.Vendor)
|
2019-08-31 07:50:29 +00:00
|
|
|
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 {
|
2024-05-23 10:19:46 +00:00
|
|
|
log.Error().Err(err).Msg(string(rawOutput))
|
2019-08-31 07:50:29 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
kernelsBase := "/boot/"
|
2024-02-20 12:18:43 +00:00
|
|
|
bootfiles, err := os.ReadDir(kernelsBase)
|
2019-08-31 07:50:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-15 15:24:29 +00:00
|
|
|
dist := distro.Distro{
|
|
|
|
ID: distroType,
|
|
|
|
Release: si.OS.Version,
|
2019-08-31 07:50:29 +00:00
|
|
|
}
|
|
|
|
|
2023-06-15 15:24:29 +00:00
|
|
|
rootfs, err := GenRootfsImage(dist.RootFS(), 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
|
2024-02-20 12:18:43 +00:00
|
|
|
kernelFile, err = container.FindKernel(bootfiles, krel)
|
2023-04-07 10:27:59 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Warn().Msgf("cannot find kernel %s", krel)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-02-20 12:18:43 +00:00
|
|
|
initrdFile, err = container.FindInitrd(bootfiles, krel)
|
2023-04-07 10:27:59 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Warn().Msgf("cannot find initrd %s", krel)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-05-23 21:33:50 +00:00
|
|
|
ki := distro.KernelInfo{
|
2023-05-18 16:07:24 +00:00
|
|
|
Distro: distro.Distro{
|
|
|
|
ID: distroType,
|
|
|
|
Release: 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
|
2024-05-23 10:19:46 +00:00
|
|
|
log.Info().Msgf("vmlinux %s", vmlinux)
|
2023-05-13 09:17:57 +00:00
|
|
|
if fs.PathExists(vmlinux) {
|
2019-08-31 07:50:29 +00:00
|
|
|
ki.VmlinuxPath = vmlinux
|
|
|
|
}
|
|
|
|
|
2023-05-23 22:00:20 +00:00
|
|
|
kernels = append(kernels, ki)
|
2019-08-31 07:50:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|