1
0

refactor: replace deprecated functions

This commit is contained in:
dump_stack() 2024-02-20 12:18:43 +00:00
parent 0a9b16a0f6
commit 9b69738163
Signed by: dump_stack
GPG Key ID: C9905BA72B5E02BB
3 changed files with 58 additions and 42 deletions

View File

@ -8,7 +8,6 @@ import (
"bufio"
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
@ -22,7 +21,6 @@ import (
"code.dumpstack.io/tools/out-of-tree/config"
"code.dumpstack.io/tools/out-of-tree/distro"
"code.dumpstack.io/tools/out-of-tree/fs"
)
var Runtime = "docker"
@ -377,6 +375,48 @@ func (c Container) Run(workdir string, cmds []string) (out string, err error) {
return
}
func FindKernel(entries []os.DirEntry, kname string) (name string, err error) {
for _, e := range entries {
var fi os.FileInfo
fi, err = e.Info()
if err != nil {
return
}
if strings.HasPrefix(fi.Name(), "vmlinuz") {
if strings.Contains(fi.Name(), kname) {
name = fi.Name()
return
}
}
}
err = errors.New("cannot find kernel")
return
}
func FindInitrd(entries []os.DirEntry, kname string) (name string, err error) {
for _, e := range entries {
var fi os.FileInfo
fi, err = e.Info()
if err != nil {
return
}
if strings.HasPrefix(fi.Name(), "initrd") ||
strings.HasPrefix(fi.Name(), "initramfs") {
if strings.Contains(fi.Name(), kname) {
name = fi.Name()
return
}
}
}
err = errors.New("cannot find kernel")
return
}
func (c Container) Kernels() (kernels []distro.KernelInfo, err error) {
if !c.Exist() {
return
@ -392,27 +432,33 @@ func (c Container) Kernels() (kernels []distro.KernelInfo, err error) {
}
}
moddirs, err := ioutil.ReadDir(libmodules)
moddirs, err := os.ReadDir(libmodules)
if err != nil {
return
}
bootfiles, err := ioutil.ReadDir(boot)
bootfiles, err := os.ReadDir(boot)
if err != nil {
return
}
for _, krel := range moddirs {
for _, e := range moddirs {
var krel os.FileInfo
krel, err = e.Info()
if err != nil {
return
}
c.Log.Debug().Msgf("generate config entry for %s", krel.Name())
var kernelFile, initrdFile string
kernelFile, err = fs.FindKernel(bootfiles, krel.Name())
kernelFile, err = FindKernel(bootfiles, krel.Name())
if err != nil {
c.Log.Warn().Msgf("cannot find kernel %s", krel.Name())
continue
}
initrdFile, err = fs.FindInitrd(bootfiles, krel.Name())
initrdFile, err = FindInitrd(bootfiles, krel.Name())
if err != nil {
c.Log.Warn().Msgf("cannot find initrd %s", krel.Name())
continue

View File

@ -70,33 +70,3 @@ func FindBySubstring(dir, substring string) (k string, err error) {
err = errors.New("not found")
return
}
func FindKernel(files []os.FileInfo, kname string) (name string, err error) {
for _, file := range files {
if strings.HasPrefix(file.Name(), "vmlinuz") {
if strings.Contains(file.Name(), kname) {
name = file.Name()
return
}
}
}
err = errors.New("cannot find kernel")
return
}
func FindInitrd(files []os.FileInfo, kname string) (name string, err error) {
for _, file := range files {
if strings.HasPrefix(file.Name(), "initrd") ||
strings.HasPrefix(file.Name(), "initramfs") {
if strings.Contains(file.Name(), kname) {
name = file.Name()
return
}
}
}
err = errors.New("cannot find kernel")
return
}

View File

@ -8,13 +8,14 @@
package kernel
import (
"io/ioutil"
"os"
"os/exec"
"strings"
"github.com/rs/zerolog/log"
"github.com/zcalusic/sysinfo"
"code.dumpstack.io/tools/out-of-tree/container"
"code.dumpstack.io/tools/out-of-tree/distro"
"code.dumpstack.io/tools/out-of-tree/fs"
)
@ -38,12 +39,11 @@ func GenHostKernels(download bool) (kernels []distro.KernelInfo, err error) {
}
kernelsBase := "/boot/"
bootfiles, err := ioutil.ReadDir(kernelsBase)
bootfiles, err := os.ReadDir(kernelsBase)
if err != nil {
return
}
// only for compatibility, docker is not really used
dist := distro.Distro{
ID: distroType,
Release: si.OS.Version,
@ -58,13 +58,13 @@ func GenHostKernels(download bool) (kernels []distro.KernelInfo, err error) {
log.Debug().Msgf("generate config entry for %s", krel)
var kernelFile, initrdFile string
kernelFile, err = fs.FindKernel(bootfiles, krel)
kernelFile, err = container.FindKernel(bootfiles, krel)
if err != nil {
log.Warn().Msgf("cannot find kernel %s", krel)
continue
}
initrdFile, err = fs.FindInitrd(bootfiles, krel)
initrdFile, err = container.FindInitrd(bootfiles, krel)
if err != nil {
log.Warn().Msgf("cannot find initrd %s", krel)
continue