-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
version.go
161 lines (133 loc) · 4.39 KB
/
version.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
//go:build linux
package kernel
import (
"fmt"
"regexp"
"strconv"
"github.com/cilium/ebpf/features"
)
var versionRegex = regexp.MustCompile(`^(\d+)\.(\d+)(?:\.(\d+))?.*$`)
// Version is a numerical representation of a kernel version
type Version uint32
// String returns a string representing the version in x.x.x format
func (v Version) String() string {
return fmt.Sprintf("%d.%d.%d", v.Major(), v.Minor(), v.Patch())
}
// Major returns the major number of the version code
func (v Version) Major() uint8 {
return (uint8)(v >> 16)
}
// Minor returns the minor number of the version code
func (v Version) Minor() uint8 {
return (uint8)((v >> 8) & 0xff)
}
// Patch returns the patch number of the version code
func (v Version) Patch() uint8 {
return (uint8)(v & 0xff)
}
// HostVersion returns the running kernel version of the host
func HostVersion() (Version, error) {
lvc, err := features.LinuxVersionCode()
if err != nil {
return 0, err
}
return Version(lvc), nil
}
// MustHostVersion returns the running kernel version of the host
func MustHostVersion() Version {
lvc, err := features.LinuxVersionCode()
if err != nil {
panic(err)
}
return Version(lvc)
}
// ParseVersion parses a string in the format of x.x.x to a Version
func ParseVersion(s string) Version {
var a, b, c byte
fmt.Sscanf(s, "%d.%d.%d", &a, &b, &c)
return VersionCode(a, b, c)
}
// VersionCode returns a Version computed from the individual parts of a x.x.x version
func VersionCode(major, minor, patch byte) Version {
// KERNEL_VERSION(a,b,c) = (a << 16) + (b << 8) + (c)
// Per https://github.com/torvalds/linux/blob/db7c953555388571a96ed8783ff6c5745ba18ab9/Makefile#L1250
return Version((uint32(major) << 16) + (uint32(minor) << 8) + uint32(patch))
}
// ParseReleaseString converts a release string with format
// 4.4.2[-1] to a kernel version number in LINUX_VERSION_CODE format.
// That is, for kernel "a.b.c", the version number will be (a<<16 + b<<8 + c)
func ParseReleaseString(releaseString string) (Version, error) {
versionParts := versionRegex.FindStringSubmatch(releaseString)
if len(versionParts) < 3 {
return 0, fmt.Errorf("got invalid release version %q (expected format '4.3.2-1')", releaseString)
}
var major, minor, patch uint64
var err error
major, err = strconv.ParseUint(versionParts[1], 10, 8)
if err != nil {
return 0, err
}
minor, err = strconv.ParseUint(versionParts[2], 10, 8)
if err != nil {
return 0, err
}
// patch is optional
if len(versionParts) >= 4 {
patch, _ = strconv.ParseUint(versionParts[3], 10, 8)
}
// clamp patch/sublevel to 255 EARLY in 4.14.252 because they merged this too early:
// https://github.com/torvalds/linux/commit/e131e0e880f942f138c4b5e6af944c7ddcd7ec96
if major == 4 && minor == 14 && patch >= 252 {
patch = 255
}
// https://github.com/torvalds/linux/commit/a256aac5b7000bdf1232ed2bbd674582c0ab27ec
if major == 4 && minor == 19 && patch >= 222 {
patch = 255
}
return VersionCode(byte(major), byte(minor), byte(patch)), nil
}
// UbuntuKernelVersion represents a version from an ubuntu kernel
// Please see: https://ubuntu.com/kernel for the documentation of this scheme
type UbuntuKernelVersion struct {
Major int
Minor int
Patch int // always 0
Abi int
Flavor string
}
var ubuntuKernelVersionRegex = regexp.MustCompile(`^(\d+)\.(\d+)\.(0)-(\d+)-([[:lower:]-]+)$`)
// NewUbuntuKernelVersion parses the ubuntu release string and returns a structure with each extracted fields
func NewUbuntuKernelVersion(unameRelease string) (*UbuntuKernelVersion, error) {
match := ubuntuKernelVersionRegex.FindStringSubmatch(unameRelease)
if len(match) == 0 {
return nil, fmt.Errorf("failed to parse ubuntu kernel version")
}
major, err := strconv.ParseInt(match[1], 10, 0)
if err != nil {
return nil, err
}
minor, err := strconv.ParseInt(match[2], 10, 0)
if err != nil {
return nil, err
}
patch, err := strconv.ParseInt(match[3], 10, 0)
if err != nil {
return nil, err
}
abi, err := strconv.ParseInt(match[4], 10, 0)
if err != nil {
return nil, err
}
flavor := match[5]
return &UbuntuKernelVersion{
Major: int(major),
Minor: int(minor),
Patch: int(patch),
Abi: int(abi),
Flavor: flavor,
}, nil
}