gopsutil icon indicating copy to clipboard operation
gopsutil copied to clipboard

[Bug] wrong kernel version on debian

Open the-hotmann opened this issue 3 years ago • 3 comments

Describe the bug When I run on my debian system "uname -r" I do get: 5.19.0-1-amd64 (debian package version) when I run "uname -v" I do get: #1 SMP PREEMPT_DYNAMIC Debian 5.19.6-1 (2022-09-01) 5.19.6-1 (kernel version)

  • uname -r (Kernel Release)
  • uname -v (Kernel Version)

So 5.19.0-1-amd64 or 5.19.6-1 now is the real kernel version. But it must be the second (5.19.6-1) as 5.19.0-1-amd64 is just debians mapping of versions. You can see here: https://packages.debian.org/sid/linux-image-amd64 image 5.19.0-1-amd64 is debians version mapping and this 5.19.6-1 is the real official kernel version.

When running: kernel_version, _ := host.KernelVersion() kernel_version equals 5.19.0-1-amd64 which does not represent the kernel version, but debian's own version.

I open this Bug report, as I think there is a legit chance of getting it fixed, since the real kernel version is available.

I in general think, these functions should change/exist:

  1. KernelVersion() (change to real kernel version) 5.19.6-1 (in my case)

  2. KernelRelease() (this would be the current KernelVersion() implementation, renamed to KernelRelease()) 5.19.0-1-amd64 (in my case)

  3. KernelBuildDate() (would love to see added) 2022-09-01 (in my case)

That would make things better and more precise

Thanks in advance!

P.S.: I am open for a conversation on this

the-hotmann avatar Sep 22 '22 10:09 the-hotmann

Using the kernel version as in uname -v is completely unreliable, here are some results of uname -r -v on various hosts I have access to

4.9.0-9-amd64 #1 SMP Debian 4.9.168-1+deb9u2 (2019-05-13)
4.15.18-30-pve #1 SMP PVE 4.15.18-58 (Fri, 12 Jun 2020 13:53:01 +0200)
5.10.46-scaleway #1 SMP Thu Jul 22 12:00:15 UTC 2021
4.5.2-armada375 #1 SMP Tue Oct 25 11:52:56 CEST 2016
5.10.0-8-amd64 #1 SMP Debian 5.10.46-4 (2021-08-03)
3.18.31-perf-g3180dc0 #1 SMP PREEMPT Sun May 13 10:32:05 UTC 2018

And these are even all Debian hosts (minus the last result).

Regarding the API changes, this is not possible as we want to have the same API on all supported OSes and this is linux-specific. You should call unix.Uname() and use the properties populated in the Utsname struct you are interested in, as gopsutil does in host.KernelVersion()

Lomanic avatar Oct 23 '22 00:10 Lomanic

Thank you for your answer!

I just used uname -v and uname -r to show, that host.KernelVersion() does give me back the value of uname -r and not the real kernel version, but the kernel (debian) release.

I will see if I can manage to modify it, so it gives me the correct kernel version. Thanks! ㅤ ㅤ

EDIT:

I managed to work around, by reading /proc/sys/kernel/version. This works everywhere and gave me the 100% correct kernel version.

Here my (probably bad) code snippet:

// READ LINE
func ReadLine(r io.Reader, lineNum int) (line string) {
	sc := bufio.NewScanner(r)
	var lastLine int
	for sc.Scan() {
		lastLine++
		if lastLine == lineNum {
			return sc.Text()
		}
	}
	return line
}

// KERNEL_VERSION
kernel_file := "/proc/sys/kernel/version"
if _, err := os.Stat(kernel_file); err == nil {
	f, _ := os.Open(kernel_file)
	defer f.Close()
	kernel_string := strings.Split(ReadLine(f, 1), " ")
	kv_re := regexp.MustCompile(`(^[0-9]*\.[0-9]*\.[0-9]*-[0-9]*)`)
	for _, a := range kernel_string {
		ok := kv_re.MatchString(a)
		if ok {
			fmt.Println("Kernel Version: ", a)
			break
		}
	}
}

Maybe there are more easy ways, but this one works aswell. Now my kernel_version is properly displayed (6.0.2-1). This solution btw also works within docker containers (even docker scratch), just tested it.

the-hotmann avatar Oct 23 '22 15:10 the-hotmann

/proc/sys/kernel/version is what is returned by uname -v, what I wrote in https://github.com/shirou/gopsutil/issues/1355#issuecomment-1287962225 similarly applies.

Lomanic avatar Dec 19 '22 17:12 Lomanic