1
0
out-of-tree/kernel/kernel_linux.go

99 lines
2.0 KiB
Go
Raw Normal View History

2023-03-18 21:30:07 +00:00
// Copyright 2023 Mikhail Klementev. All rights reserved.
// 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
// +build linux
package kernel
import (
2024-02-20 12:18:43 +00:00
"os"
"os/exec"
"strings"
2023-03-18 21:30:07 +00:00
"github.com/rs/zerolog/log"
"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"
"code.dumpstack.io/tools/out-of-tree/distro"
"code.dumpstack.io/tools/out-of-tree/fs"
)
func GenHostKernels(download bool) (kernels []distro.KernelInfo, err error) {
si := sysinfo.SysInfo{}
si.GetSysInfo()
distroType, err := distro.NewID(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)
rawOutput, err := cmd.CombinedOutput()
if err != nil {
2023-03-18 21:30:07 +00:00
log.Print(string(rawOutput), err)
return
}
kernelsBase := "/boot/"
2024-02-20 12:18:43 +00:00
bootfiles, err := os.ReadDir(kernelsBase)
if err != nil {
return
}
2023-06-15 15:24:29 +00:00
dist := distro.Distro{
ID: distroType,
Release: si.OS.Version,
}
2023-06-15 15:24:29 +00:00
rootfs, err := GenRootfsImage(dist.RootFS(), download)
if err != nil {
return
}
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)
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)
if err != nil {
log.Warn().Msgf("cannot find initrd %s", krel)
continue
}
ki := distro.KernelInfo{
Distro: distro.Distro{
ID: distroType,
Release: si.OS.Version,
},
KernelRelease: krel,
KernelSource: "/lib/modules/" + krel + "/build",
KernelPath: kernelsBase + kernelFile,
InitrdPath: kernelsBase + initrdFile,
RootFS: rootfs,
}
vmlinux := "/usr/lib/debug/boot/vmlinux-" + krel
2023-03-18 21:30:07 +00:00
log.Print("vmlinux", vmlinux)
if fs.PathExists(vmlinux) {
ki.VmlinuxPath = vmlinux
}
kernels = append(kernels, ki)
}
return
}