From 209a5e5602d175d135c3f8413878bbca965273e6 Mon Sep 17 00:00:00 2001 From: Niels de Vos Date: Wed, 17 Jun 2020 20:30:09 +0200 Subject: [PATCH] util: add function to get the kernel version Signed-off-by: Niels de Vos --- internal/cephfs/volumemounter.go | 9 +++------ internal/util/util.go | 12 ++++++++++++ internal/util/util_test.go | 10 ++++++++++ 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/internal/cephfs/volumemounter.go b/internal/cephfs/volumemounter.go index 4da6f0c10..2a70abf1f 100644 --- a/internal/cephfs/volumemounter.go +++ b/internal/cephfs/volumemounter.go @@ -29,7 +29,6 @@ import ( "github.com/ceph/ceph-csi/internal/util" - "golang.org/x/sys/unix" "k8s.io/klog" ) @@ -151,12 +150,10 @@ func loadAvailableMounters(conf *util.Config) error { klog.Errorf("failed to run mount.ceph %v", err) } else { // fetch the current running kernel info - utsname := unix.Utsname{} - err = unix.Uname(&utsname) - if err != nil { - return err + release, kvErr := util.KernelVersion() + if kvErr != nil { + return kvErr } - release := string(utsname.Release[:64]) if conf.ForceKernelCephFS || kernelSupportsQuota(release) { klog.V(1).Infof("loaded mounter: %s", volumeMounterKernel) diff --git a/internal/util/util.go b/internal/util/util.go index 77f7cc5d9..bb4163486 100644 --- a/internal/util/util.go +++ b/internal/util/util.go @@ -25,6 +25,7 @@ import ( "time" "github.com/pkg/errors" + "golang.org/x/sys/unix" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" "k8s.io/apimachinery/pkg/util/validation" @@ -139,6 +140,17 @@ func ValidateDriverName(driverName string) error { return err } +// KernelVersion returns the version of the running Unix (like) system from the +// 'utsname' structs 'release' component. +func KernelVersion() (string, error) { + utsname := unix.Utsname{} + err := unix.Uname(&utsname) + if err != nil { + return "", err + } + return string(utsname.Release[:]), nil +} + // GenerateVolID generates a volume ID based on passed in parameters and version, to be returned // to the CO system func GenerateVolID(ctx context.Context, monitors string, cr *Credentials, locationID int64, pool, clusterID, objUUID string, volIDVersion uint16) (string, error) { diff --git a/internal/util/util_test.go b/internal/util/util_test.go index 0a4cd3af2..a8451883c 100644 --- a/internal/util/util_test.go +++ b/internal/util/util_test.go @@ -142,3 +142,13 @@ func TestRoundOffVolSize(t *testing.T) { }) } } + +func TestKernelVersion(t *testing.T) { + version, err := KernelVersion() + if err != nil { + t.Errorf("failed to get kernel version: %s", err) + } + if version == "" { + t.Error("version is empty, this is unexpected?!") + } +}