1
0
out-of-tree/distro/debian/kernel.go

468 lines
9.8 KiB
Go
Raw Permalink Normal View History

2023-05-11 19:26:54 +00:00
package debian
import (
"errors"
"math"
2023-05-11 19:26:54 +00:00
"strings"
2023-05-12 00:07:51 +00:00
"time"
2023-05-11 19:26:54 +00:00
"github.com/Masterminds/semver"
"github.com/rs/zerolog"
2023-05-12 15:00:50 +00:00
"github.com/rs/zerolog/log"
2023-05-14 12:37:45 +00:00
"code.dumpstack.io/tools/out-of-tree/cache"
2024-02-20 13:25:31 +00:00
"code.dumpstack.io/tools/out-of-tree/config/dotfiles"
2023-05-11 19:26:54 +00:00
"code.dumpstack.io/tools/out-of-tree/distro/debian/snapshot"
2023-05-28 09:38:09 +00:00
"code.dumpstack.io/tools/out-of-tree/distro/debian/snapshot/metasnap"
2023-05-14 12:37:45 +00:00
"code.dumpstack.io/tools/out-of-tree/fs"
2023-05-11 19:26:54 +00:00
)
type DebianKernelVersion struct {
// linux-headers-4.17.0-2-amd64_4.17.14-1_amd64.deb
// Package version, e.g. "4.17.14-1"
// See tags in https://salsa.debian.org/kernel-team/linux
Package string
// ABI version, e.g. "4.17.0-2"
ABI string
}
func ParseKernelVersion(pkg string) (dkv DebianKernelVersion, err error) {
// -> 4.11.0-trunk-amd64_4.11-1~exp2_amd64.deb
pkg = strings.Replace(pkg, "linux-image-", "", -1)
// -> [4.11.0-trunk-amd64 4.11-1~exp2 amd64.deb]
fields := strings.Split(pkg, "_")
if len(fields) != 3 {
err = errors.New("incorrect input format")
return
}
// 4.11.0-trunk-amd64 -> 4.11.0-trunk
// TODO other archs?
dkv.ABI = strings.Split(fields[0], "-amd64")[0]
if dkv.ABI == "" {
err = errors.New("incorrect input format")
return
}
dkv.Package = fields[1]
if dkv.Package == "" {
err = errors.New("incorrect input format")
return
}
return
}
2023-05-11 19:26:54 +00:00
type DebianKernel struct {
Version DebianKernelVersion
Image snapshot.Package
Headers []snapshot.Package
Dependencies []snapshot.Package
2023-05-12 00:07:51 +00:00
// FIXME There is a better way
Internal struct {
Invalid bool
LastFetch time.Time
}
2023-05-28 09:38:09 +00:00
Release Release
2023-05-11 19:26:54 +00:00
}
func (dk DebianKernel) HasDependency(pkgname string) bool {
for _, deppkg := range dk.Dependencies {
if strings.Contains(deppkg.Name, pkgname) {
return true
}
}
return false
}
2023-05-15 11:14:59 +00:00
func (dk DebianKernel) Packages() (pkgs []snapshot.Package) {
pkgs = append(pkgs, dk.Image)
pkgs = append(pkgs, dk.Headers...)
pkgs = append(pkgs, dk.Dependencies...)
return
}
// use only for inline comparison
func kver(ver string) *semver.Version {
ver = strings.Replace(ver, "~", "-", -1)
ver = strings.Replace(ver, "+", "-", -1)
return semver.MustParse(ver)
}
2023-05-11 20:25:34 +00:00
var (
ErrNoBinaryPackages = errors.New("no binary packages found")
ErrNoHeadersPackage = errors.New("no headers package found")
ErrNoImagePackage = errors.New("no image package found")
)
2023-05-15 17:40:53 +00:00
func getDebianKernel(version string) (dk DebianKernel, err error) {
2023-05-28 09:38:09 +00:00
flog := log.With().
Str("version", version).
Logger()
2023-05-11 19:26:54 +00:00
dk.Version.Package = version
regex := `^(linux-(image|headers)-[a-z+~0-9\.\-]*-(common|amd64|amd64-unsigned)|linux-kbuild-.*|linux-compiler-.*-x86)$`
2023-05-11 19:26:54 +00:00
2023-05-11 22:17:31 +00:00
filter := []string{
"rt-amd64",
"cloud-amd64",
"all-amd64",
2023-05-14 14:05:10 +00:00
"dbg",
2023-05-11 20:45:53 +00:00
}
2023-05-11 21:47:43 +00:00
packages, err := snapshot.Packages("linux", version, regex,
[]string{"amd64", "all"}, filter)
2023-05-11 22:17:31 +00:00
if err != nil {
return
2023-05-11 21:47:43 +00:00
}
2023-05-11 20:25:34 +00:00
if len(packages) == 0 {
err = ErrNoBinaryPackages
return
2023-05-11 19:26:54 +00:00
}
var imageFound, headersFound bool
for _, p := range packages {
if strings.Contains(p.Name, "image") {
imageFound = true
dk.Image = p
} else if strings.Contains(p.Name, "headers") {
headersFound = true
dk.Headers = append(dk.Headers, p)
} else {
dk.Dependencies = append(dk.Dependencies, p)
2023-05-11 19:26:54 +00:00
}
}
if !imageFound {
err = ErrNoImagePackage
2023-05-11 19:26:54 +00:00
return
}
if !headersFound {
err = ErrNoHeadersPackage
2023-05-11 19:26:54 +00:00
return
}
s := strings.Replace(dk.Image.Name, "linux-image-", "", -1)
2023-05-11 19:26:54 +00:00
dk.Version.ABI = strings.Replace(s, "-amd64", "", -1)
dk.Release = getRelease(dk.Image)
2023-05-28 09:38:09 +00:00
if dk.Release == None {
flog.Warn().Msg("release not found")
} else {
flog.Debug().Msgf("release is %s", dk.Release.Name())
}
2023-05-11 19:26:54 +00:00
return
}
2023-05-12 15:00:50 +00:00
func getRelease(p snapshot.Package) Release {
repos, err := metasnap.GetRepos(p.Repo.Archive, p.Name, p.Arch, p.Version)
if err != nil {
log.Debug().Err(err).Msg("metasnap")
return None
}
for _, repo := range repos {
for _, rel := range ReleaseStrings[1:] {
switch repo.Suite {
2023-05-28 12:15:44 +00:00
case rel, rel + "-backports",
rel + "-updates",
rel + "-proposed-updates":
return ReleaseFromString(rel)
}
}
}
return None
}
2023-05-13 19:48:01 +00:00
// GetCachedKernel by deb package name
func getCachedKernel(deb string) (dk DebianKernel, err error) {
2023-05-13 19:48:01 +00:00
c, err := NewCache(CachePath)
if err != nil {
log.Error().Err(err).Msg("cache")
return
}
defer c.Close()
versions, err := c.GetVersions()
if err != nil {
log.Error().Err(err).Msg("get source package versions from cache")
return
}
for _, version := range versions {
var tmpdks []DebianKernel
tmpdks, err = c.Get(version)
2023-05-13 19:48:01 +00:00
if err != nil {
continue
}
tmpdk := tmpdks[0]
2023-05-13 19:48:01 +00:00
if deb == tmpdk.Image.Deb.Name {
2023-05-13 19:48:01 +00:00
dk = tmpdk
2023-05-15 18:17:31 +00:00
return
}
for _, h := range tmpdk.Headers {
if deb == h.Deb.Name {
dk = tmpdk
return
}
2023-05-13 19:48:01 +00:00
}
}
return
}
func kbuildVersion(versions []string, kpkgver string) string {
for _, v := range versions {
if v == kpkgver {
return v
}
}
ver := kver(kpkgver)
// Not able to find the exact version, try similar
for _, v := range versions {
cver := kver(v)
// It's certainly not fit for purpose if the major and
// minor versions aren't the same
if ver.Major() != cver.Major() {
continue
}
if ver.Minor() != cver.Minor() {
continue
}
return v
}
return ""
}
func findKbuild(versions []string, kpkgver string) (
pkg snapshot.Package, err error) {
version := kbuildVersion(versions, kpkgver)
if version == "" {
err = errors.New("cannot find kbuild version")
return
}
packages, err := snapshot.Packages("linux-tools", version,
`^linux-kbuild`, []string{"amd64"}, []string{"dbg"})
if err != nil {
return
}
if len(packages) == 0 {
err = errors.New("cannot find kbuild package")
}
pkg = packages[0]
return
}
2023-05-29 11:47:40 +00:00
func updateKbuild(toolsVersions []string, dk *DebianKernel) {
if !kver(dk.Version.Package).LessThan(kver("4.5-rc0")) {
return
}
var deps []snapshot.Package
for _, pkg := range dk.Dependencies {
if strings.Contains(pkg.Name, "kbuild") {
continue
}
deps = append(deps, pkg)
}
dk.Dependencies = deps
kbuildpkg, err := findKbuild(toolsVersions, dk.Version.Package)
if err != nil {
dk.Internal.Invalid = true
return
}
dk.Dependencies = append(dk.Dependencies, kbuildpkg)
}
func getKernelsByVersion(slog zerolog.Logger, c *Cache, toolsVersions []string,
2023-05-29 08:38:53 +00:00
version string, mode GetKernelsMode) (kernels []DebianKernel,
fromcache bool) {
var dk DebianKernel
dks, err := c.Get(version)
if err == nil {
dk = dks[0]
if !dk.Internal.Invalid {
2023-05-29 11:47:40 +00:00
// TODO refactor
slog.Trace().Msgf("found in cache")
2023-05-29 08:38:53 +00:00
if dk.Release == None && mode&UpdateRelease != 0 {
slog.Debug().Msg("update release")
dk.Release = getRelease(dk.Image)
if dk.Release != None {
slog.Debug().Msg("update cache")
err = c.Put([]DebianKernel{dk})
if err != nil {
slog.Error().Err(err).Msg("")
return
}
}
}
2023-05-29 11:47:40 +00:00
if mode&UpdateKbuild != 0 {
slog.Debug().Msg("update kbuild")
updateKbuild(toolsVersions, &dk)
slog.Debug().Msg("update cache")
err = c.Put([]DebianKernel{dk})
if err != nil {
slog.Error().Err(err).Msg("")
return
}
}
kernels = append(kernels, dk)
fromcache = true
return
}
}
if dk.Internal.Invalid {
refetch := dk.Internal.LastFetch.AddDate(0, 0, RefetchDays)
if refetch.After(time.Now()) {
slog.Trace().Msgf("refetch at %v", refetch)
return
}
}
dk, err = getDebianKernel(version)
if err != nil {
if err == ErrNoBinaryPackages {
slog.Warn().Err(err).Msg("")
} else {
slog.Error().Err(err).Msg("get debian kernel")
}
dk.Internal.Invalid = true
}
if !dk.HasDependency("kbuild") {
2023-05-29 11:47:40 +00:00
// Debian kernels prior to the 4.5 package
// version did not have a kbuild built from
// the linux source itself, but used the
// linux-tools source package.
if !kver(dk.Version.Package).LessThan(kver("4.5-rc0")) {
dk.Internal.Invalid = true
} else {
updateKbuild(toolsVersions, &dk)
}
}
dk.Internal.LastFetch = time.Now()
if !dk.Internal.Invalid {
kernels = append(kernels, dk)
}
err = c.Put([]DebianKernel{dk})
if err != nil {
slog.Error().Err(err).Msg("put to cache")
return
}
slog.Debug().Msgf("%s cached", version)
return
}
2023-05-14 12:37:45 +00:00
var (
CachePath string
RefetchDays int = 14
2023-05-14 12:37:45 +00:00
)
2023-05-29 08:38:53 +00:00
type GetKernelsMode int
const (
NoMode GetKernelsMode = iota
UpdateRelease
2023-05-29 11:47:40 +00:00
UpdateKbuild
2023-05-29 08:38:53 +00:00
)
// GetKernelsWithLimit is workaround for testing and building the
// first cache, which is heavily rate limited by snapshot.debian.org
2023-05-29 08:38:53 +00:00
func GetKernelsWithLimit(limit int, mode GetKernelsMode) (kernels []DebianKernel,
err error) {
2023-05-14 12:37:45 +00:00
if CachePath == "" {
2024-02-20 13:25:31 +00:00
CachePath = dotfiles.File("debian.cache")
2023-05-14 12:37:45 +00:00
log.Debug().Msgf("Use default kernels cache path: %s", CachePath)
if !fs.PathExists(CachePath) {
log.Debug().Msgf("No cache, download")
err = cache.DownloadDebianCache(CachePath)
if err != nil {
log.Debug().Err(err).Msg(
"No remote cache, will take some time")
}
}
} else {
log.Debug().Msgf("Debian kernels cache path: %s", CachePath)
}
c, err := NewCache(CachePath)
if err != nil {
log.Error().Err(err).Msg("cache")
return
}
defer c.Close()
toolsVersions, err := snapshot.SourcePackageVersions("linux-tools")
if err != nil {
log.Error().Err(err).Msg("get linux-tools source pkg versions")
return
}
2023-05-12 15:00:50 +00:00
versions, err := snapshot.SourcePackageVersions("linux")
if err != nil {
log.Error().Err(err).Msg("get linux source package versions")
2023-05-12 15:00:50 +00:00
return
}
2023-05-13 19:48:01 +00:00
err = c.PutVersions(versions)
if err != nil {
log.Error().Err(err).Msg("put source package versions to cache")
return
}
2023-05-12 15:00:50 +00:00
for i, version := range versions {
slog := log.With().Str("version", version).Logger()
2023-05-15 15:35:48 +00:00
slog.Trace().Msgf("%03d/%03d", i, len(versions))
2023-05-29 08:38:53 +00:00
vkernels, fromcache := getKernelsByVersion(slog, c, toolsVersions, version, mode)
kernels = append(kernels, vkernels...)
if !fromcache {
limit--
}
if limit <= 0 {
return
}
2023-05-12 15:00:50 +00:00
}
return
}
func GetKernels() (kernels []DebianKernel, err error) {
2023-05-29 08:38:53 +00:00
return GetKernelsWithLimit(math.MaxInt32, NoMode)
}