2023-05-11 19:26:54 +00:00
|
|
|
package debian
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2023-05-14 16:54:12 +00:00
|
|
|
"sort"
|
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
|
|
|
|
2023-05-14 16:54:12 +00:00
|
|
|
"github.com/Masterminds/semver"
|
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"
|
|
|
|
"code.dumpstack.io/tools/out-of-tree/config"
|
2023-05-11 19:26:54 +00:00
|
|
|
"code.dumpstack.io/tools/out-of-tree/distro/debian/snapshot"
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
type DebianKernel struct {
|
2023-05-14 10:27:16 +00:00
|
|
|
Version DebianKernelVersion
|
|
|
|
Image snapshot.Package
|
2023-05-14 11:06:54 +00:00
|
|
|
Headers []snapshot.Package
|
2023-05-14 10:27:16 +00:00
|
|
|
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-11 19:26:54 +00:00
|
|
|
}
|
|
|
|
|
2023-05-14 16:54:12 +00:00
|
|
|
func (dk DebianKernel) HasDependency(pkgname string) bool {
|
|
|
|
for _, deppkg := range dk.Dependencies {
|
|
|
|
if strings.Contains(deppkg.Name, pkgname) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-11 19:26:54 +00:00
|
|
|
func GetDebianKernel(version string) (dk DebianKernel, err error) {
|
|
|
|
dk.Version.Package = version
|
|
|
|
|
2023-05-14 13:59:25 +00:00
|
|
|
regex := `^(linux-(image|headers)-[a-z+~0-9\.\-]*-(common|amd64|amd64-unsigned)|linux-kbuild-.*)$`
|
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-14 14:34:11 +00:00
|
|
|
"exp",
|
2023-05-11 20:45:53 +00:00
|
|
|
}
|
2023-05-11 21:47:43 +00:00
|
|
|
|
2023-05-14 11:40:36 +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
|
2023-05-14 11:06:54 +00:00
|
|
|
dk.Headers = append(dk.Headers, p)
|
|
|
|
} else {
|
|
|
|
dk.Dependencies = append(dk.Dependencies, p)
|
2023-05-11 19:26:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !imageFound {
|
2023-05-14 11:06:54 +00:00
|
|
|
err = ErrNoImagePackage
|
2023-05-11 19:26:54 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !headersFound {
|
2023-05-14 11:06:54 +00:00
|
|
|
err = ErrNoHeadersPackage
|
2023-05-11 19:26:54 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-14 11:06:54 +00:00
|
|
|
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)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2023-05-12 15:00:50 +00:00
|
|
|
|
2023-05-13 19:48:01 +00:00
|
|
|
// GetCachedKernel by deb package name
|
|
|
|
func GetCachedKernel(deb string) (dk DebianKernel, err error) {
|
|
|
|
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 tmpdk DebianKernel
|
|
|
|
tmpdk, err = c.Get(version)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-05-14 11:06:54 +00:00
|
|
|
if deb == tmpdk.Image.Deb.Name {
|
2023-05-13 19:48:01 +00:00
|
|
|
dk = tmpdk
|
2023-05-14 11:06:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, h := range tmpdk.Headers {
|
|
|
|
if deb == h.Deb.Name {
|
|
|
|
dk = tmpdk
|
|
|
|
return
|
|
|
|
}
|
2023-05-13 19:48:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-14 16:54:12 +00:00
|
|
|
func kbuildVersion(versions []string, kpkgver string) string {
|
|
|
|
sort.Slice(versions, func(i, j int) bool {
|
|
|
|
return kver(versions[i]).GreaterThan(kver(versions[j]))
|
|
|
|
})
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use the first version that is newer than the kernel
|
|
|
|
|
|
|
|
if ver.LessThan(cver) {
|
|
|
|
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-14 12:37:45 +00:00
|
|
|
var (
|
|
|
|
CachePath string
|
|
|
|
RefetchDays int = 7
|
|
|
|
)
|
|
|
|
|
|
|
|
func GetKernels() (kernels []DebianKernel, err error) {
|
|
|
|
if CachePath == "" {
|
|
|
|
CachePath = config.File("debian.cache")
|
|
|
|
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()
|
|
|
|
|
2023-05-14 16:54:12 +00:00
|
|
|
linuxToolsVersions, 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 {
|
2023-05-14 16:54:12 +00:00
|
|
|
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 {
|
2023-05-14 16:54:12 +00:00
|
|
|
// TODO move this scope to function
|
2023-05-12 15:00:50 +00:00
|
|
|
slog := log.With().Str("version", version).Logger()
|
|
|
|
slog.Debug().Msgf("%03d/%03d", i, len(versions))
|
|
|
|
|
|
|
|
var dk DebianKernel
|
|
|
|
|
|
|
|
dk, err = c.Get(version)
|
|
|
|
if err == nil && !dk.Internal.Invalid {
|
|
|
|
slog.Debug().Msgf("found in cache")
|
|
|
|
kernels = append(kernels, dk)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if dk.Internal.Invalid {
|
2023-05-14 12:37:45 +00:00
|
|
|
refetch := dk.Internal.LastFetch.AddDate(0, 0, RefetchDays)
|
2023-05-12 15:00:50 +00:00
|
|
|
if refetch.After(time.Now()) {
|
2023-05-14 12:37:45 +00:00
|
|
|
slog.Debug().Msgf("refetch at %v", RefetchDays)
|
2023-05-12 15:00:50 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
dk.Internal.LastFetch = time.Now()
|
|
|
|
}
|
|
|
|
|
2023-05-14 16:54:12 +00:00
|
|
|
if !dk.HasDependency("kbuild") {
|
|
|
|
if !kver(dk.Version.Package).LessThan(kver("4.5-rc0")) {
|
|
|
|
dk.Internal.Invalid = true
|
|
|
|
dk.Internal.LastFetch = time.Now()
|
|
|
|
} else {
|
|
|
|
// 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.
|
|
|
|
kbuildpkg, err := findKbuild(
|
|
|
|
linuxToolsVersions,
|
|
|
|
dk.Version.Package,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
dk.Internal.Invalid = true
|
|
|
|
dk.Internal.LastFetch = time.Now()
|
|
|
|
} else {
|
|
|
|
dk.Dependencies = append(
|
|
|
|
dk.Dependencies,
|
|
|
|
kbuildpkg,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-12 15:00:50 +00:00
|
|
|
err = c.Put(dk)
|
|
|
|
if err != nil {
|
|
|
|
slog.Error().Err(err).Msg("put to cache")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
slog.Debug().Msgf("%s cached", version)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|