-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move kernel version check to its own pkg (#373)
Signed-off-by: Mohamed Mahmoud <[email protected]>
- Loading branch information
1 parent
0709e78
commit 7acdf8d
Showing
4 changed files
with
83 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package kernel | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
"syscall" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
var ( | ||
versionRegex = regexp.MustCompile(`^(\d+)\.(\d+).(\d+).*$`) | ||
kernelVersion uint32 | ||
log = logrus.WithField("component", "kernel") | ||
) | ||
|
||
func init() { | ||
var err error | ||
kernelVersion, err = currentKernelVersion() | ||
if err != nil { | ||
log.Errorf("failed to get current kernel version: %v", err) | ||
} | ||
} | ||
|
||
func IsKernelOlderThan(version string) bool { | ||
refVersion, err := kernelVersionFromReleaseString(version) | ||
if err != nil { | ||
log.Warnf("failed to get kernel version from release string: %v", err) | ||
return false | ||
} | ||
return kernelVersion != 0 && kernelVersion < refVersion | ||
} | ||
|
||
// kernelVersionFromReleaseString 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 kernelVersionFromReleaseString(releaseString string) (uint32, error) { | ||
versionParts := versionRegex.FindStringSubmatch(releaseString) | ||
if len(versionParts) != 4 { | ||
return 0, fmt.Errorf("got invalid release version %q (expected format '4.3.2-1')", releaseString) | ||
} | ||
major, err := strconv.Atoi(versionParts[1]) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
minor, err := strconv.Atoi(versionParts[2]) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
patch, err := strconv.Atoi(versionParts[3]) | ||
if err != nil { | ||
return 0, err | ||
} | ||
out := major*256*256 + minor*256 + patch | ||
return uint32(out), nil | ||
} | ||
|
||
func currentKernelVersion() (uint32, error) { | ||
var buf syscall.Utsname | ||
if err := syscall.Uname(&buf); err != nil { | ||
return 0, err | ||
} | ||
releaseString := strings.Trim(utsnameStr(buf.Release[:]), "\x00") | ||
return kernelVersionFromReleaseString(releaseString) | ||
} | ||
|
||
func utsnameStr[T int8 | uint8](in []T) string { | ||
out := make([]byte, len(in)) | ||
for i := 0; i < len(in); i++ { | ||
if in[i] == 0 { | ||
break | ||
} | ||
out = append(out, byte(in[i])) | ||
} | ||
return string(out) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package utils | ||
package kernel | ||
|
||
import ( | ||
"testing" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters