2023-05-11 19:26:54 +00:00
|
|
|
package debian
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"strings"
|
2023-05-12 00:07:51 +00:00
|
|
|
"time"
|
2023-05-11 19:26:54 +00:00
|
|
|
|
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-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 11:06:54 +00:00
|
|
|
regex := `^linux-(image|headers)-[a-z+~0-9\.\-]*-(common|amd64|amd64-unsigned)$`
|
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-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 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-12 15:00:50 +00:00
|
|
|
versions, err := snapshot.SourcePackageVersions("linux")
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("get source package versions")
|
|
|
|
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()
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.Put(dk)
|
|
|
|
if err != nil {
|
|
|
|
slog.Error().Err(err).Msg("put to cache")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
slog.Debug().Msgf("%s cached", version)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|