util: add function to get the kernel version

Signed-off-by: Niels de Vos <ndevos@redhat.com>
This commit is contained in:
Niels de Vos 2020-06-17 20:30:09 +02:00 committed by mergify[bot]
parent 2dc0bffc0a
commit 209a5e5602
3 changed files with 25 additions and 6 deletions

View File

@ -29,7 +29,6 @@ import (
"github.com/ceph/ceph-csi/internal/util" "github.com/ceph/ceph-csi/internal/util"
"golang.org/x/sys/unix"
"k8s.io/klog" "k8s.io/klog"
) )
@ -151,12 +150,10 @@ func loadAvailableMounters(conf *util.Config) error {
klog.Errorf("failed to run mount.ceph %v", err) klog.Errorf("failed to run mount.ceph %v", err)
} else { } else {
// fetch the current running kernel info // fetch the current running kernel info
utsname := unix.Utsname{} release, kvErr := util.KernelVersion()
err = unix.Uname(&utsname) if kvErr != nil {
if err != nil { return kvErr
return err
} }
release := string(utsname.Release[:64])
if conf.ForceKernelCephFS || kernelSupportsQuota(release) { if conf.ForceKernelCephFS || kernelSupportsQuota(release) {
klog.V(1).Infof("loaded mounter: %s", volumeMounterKernel) klog.V(1).Infof("loaded mounter: %s", volumeMounterKernel)

View File

@ -25,6 +25,7 @@ import (
"time" "time"
"github.com/pkg/errors" "github.com/pkg/errors"
"golang.org/x/sys/unix"
"google.golang.org/grpc/codes" "google.golang.org/grpc/codes"
"google.golang.org/grpc/status" "google.golang.org/grpc/status"
"k8s.io/apimachinery/pkg/util/validation" "k8s.io/apimachinery/pkg/util/validation"
@ -139,6 +140,17 @@ func ValidateDriverName(driverName string) error {
return err 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 // GenerateVolID generates a volume ID based on passed in parameters and version, to be returned
// to the CO system // to the CO system
func GenerateVolID(ctx context.Context, monitors string, cr *Credentials, locationID int64, pool, clusterID, objUUID string, volIDVersion uint16) (string, error) { func GenerateVolID(ctx context.Context, monitors string, cr *Credentials, locationID int64, pool, clusterID, objUUID string, volIDVersion uint16) (string, error) {

View File

@ -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?!")
}
}