1
0

feat: match the gcc version to distinguish between Debian releases

This commit is contained in:
dump_stack() 2023-05-24 09:12:33 +00:00
parent ae00b57471
commit 48ba7b7c7b
Signed by: dump_stack
GPG Key ID: BE44DA8C062D87DC
2 changed files with 62 additions and 62 deletions

View File

@ -1,12 +1,10 @@
package debian
import (
"errors"
"fmt"
"os"
"path"
"path/filepath"
"regexp"
"strings"
"github.com/rs/zerolog/log"
@ -70,7 +68,7 @@ func (d Debian) Packages() (packages []string, err error) {
p := dk.Image.Deb.Name[:len(dk.Image.Deb.Name)-4] // w/o .deb
var kr Release
kr, err = kernelRelease(p)
kr, err = kernelRelease(dk)
if err != nil {
log.Warn().Err(err).Msg("")
continue
@ -139,44 +137,51 @@ func ReleaseFromString(s string) (r Release) {
r = Buster
case "11", "bullseye":
r = Bullseye
case "12", "bookworm":
r = Bookworm
default:
r = None
}
return
}
func kernelRelease(deb string) (r Release, err error) {
// linux-image-4.17.0-2-amd64 -> 4.17
re := regexp.MustCompile(`([0-9]*\.[0-9]*)`)
sver := re.FindString(deb)
if sver == "" {
err = errors.New("empty result")
return
}
version := kver(sver)
func kernelRelease(dk DebianKernel) (r Release, err error) {
var gcc string
for _, dep := range dk.Dependencies {
if !strings.HasPrefix(dep.Name, "linux-compiler-gcc-") {
continue
}
if version.LessThan(kver("3.0-rc0")) {
err = errors.New("not supported")
return
gcc = strings.Replace(dep.Name, "linux-compiler-gcc-", "", -1)
gcc = strings.Replace(gcc, "-x86", "", -1)
break
}
if version.LessThan(kver("3.8-rc0")) {
// Wheezy 3.2
// >=3.8 breaks initramfs-tools << 0.110~
// Wheezy initramfs-tools version is 0.109.1
switch gcc {
case "", "4.4", "4.6", "4.7":
// Note that we are catching an empty string, which
// means there is no linux-compiler-gcc- package
// present, which is the case with old Debian
// kernels. As the MR API only returns kernels from
// Wheezy onwards, we can safely assume that this is
// the correct release.
r = Wheezy
} else if version.LessThan(kver("4.9-rc0")) {
// Jessie 3.16
case "4.8", "4.9":
r = Jessie
} else if version.LessThan(kver("4.19-rc0")) {
// Stretch 4.9
case "5":
// No kernels compiled with gcc-5 have reached stable
r = None
case "6":
r = Stretch
} else if version.LessThan(kver("5.10-rc0")) {
// Buster 4.19
case "7", "8":
r = Buster
} else {
// Bullseye 5.10
case "9", "10":
r = Bullseye
case "11", "12":
r = Bookworm
default:
err = fmt.Errorf("unknown release with gcc-%s", gcc)
}
return

View File

@ -8,41 +8,6 @@ import (
"code.dumpstack.io/tools/out-of-tree/distro"
)
func TestKernelRelease(t *testing.T) {
type testcase struct {
Deb string
Release Release
}
for _, tc := range []testcase{
testcase{"linux-image-3.2.0-0.bpo.4-amd64_3.2.41-2+deb7u2~bpo60+1_amd64", Wheezy},
testcase{"linux-image-3.15-trunk-amd64_3.15.5-1~exp1_amd64", Jessie},
testcase{"linux-image-3.16-rc5-amd64_3.16~rc5-1~exp1_amd64", Jessie},
testcase{"linux-image-4.15.0-3-amd64_4.15.17-1_amd64", Stretch},
testcase{"linux-image-4.16.0-rc5-amd64_4.16~rc5-1~exp1_amd64", Stretch},
testcase{"linux-image-4.17.0-2-amd64", Stretch},
testcase{"linux-image-4.18.0-0.bpo.3-amd64_4.18.20-2~bpo9+1_amd64", Stretch},
testcase{"linux-image-4.19.0-rc2-amd64-unsigned_4.19~rc2-1~exp1_amd64", Buster},
testcase{"linux-image-4.20.0-trunk-amd64-unsigned_4.20-1~exp1_amd64", Buster},
testcase{"linux-image-4.8.0-2-amd64-unsigned_4.8.15-2_amd64", Jessie},
testcase{"linux-image-4.9.0-rc3-amd64-unsigned_4.9~rc3-1~exp1_amd64", Stretch},
testcase{"linux-image-5.0.0-trunk-amd64-unsigned_5.0.1-1~exp1_amd64", Buster},
testcase{"linux-image-5.9.0-rc4-amd64-unsigned_5.9~rc4-1~exp1_amd64", Buster},
testcase{"linux-image-5.10.0-rc4-amd64-unsigned_5.10~rc4-1~exp1_amd64", Bullseye},
testcase{"linux-image-5.16.0-rc5-amd64-unsigned_5.16~rc5-1~exp1_amd64", Bullseye},
testcase{"linux-image-6.1.0-0.deb11.7-amd64-unsigned", Bullseye},
testcase{"linux-image-6.1.0-8-amd64-unsigned", Bullseye},
} {
r, err := kernelRelease(tc.Deb)
if err != nil {
t.Fatal(err)
}
if r != tc.Release {
t.Fatalf("%v -> %v != %v", tc.Deb, r, tc.Release)
}
}
}
func TestDebian(t *testing.T) {
assert := assert.New(t)
@ -52,3 +17,33 @@ func TestDebian(t *testing.T) {
assert.NotEmpty(u.Packages())
}
func TestKernelRelease(t *testing.T) {
kernels, err := GetKernels()
if err != nil {
t.Fatal(err)
}
for _, k := range kernels {
r, err := kernelRelease(k)
if err != nil {
t.Log(k.Version, r, err)
}
}
for _, k := range kernels {
r, err := kernelRelease(k)
if err != nil {
continue
}
if r == Wheezy {
t.Log("Wheezy", k.Version)
}
if r == Jessie {
t.Log("Jessie", k.Version)
}
}
}