From 29c88f4b2d084085bf9151f4f42e470ad55e2ddd Mon Sep 17 00:00:00 2001 From: John Mulligan Date: Tue, 24 Mar 2020 19:56:53 -0400 Subject: [PATCH] cephfs: add functions for Init and SetMountPerms The Init functions initializes the connection without mounting the fs. This function must be called before setting the perms but before creating the mount. SetMountPerms accepts a UserPerm object to set the fs ownership info on the mount. The corresponding tests verifies that the UserPerm can be applied and it effects the gid of newly created dirs for that mount. Signed-off-by: John Mulligan --- cephfs/cephfs.go | 8 ++++++ cephfs/mount_perms_mimic.go | 22 ++++++++++++++++ cephfs/mount_perms_mimic_test.go | 45 ++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 cephfs/mount_perms_mimic.go create mode 100644 cephfs/mount_perms_mimic_test.go diff --git a/cephfs/cephfs.go b/cephfs/cephfs.go index 689d9d9..6a8876d 100644 --- a/cephfs/cephfs.go +++ b/cephfs/cephfs.go @@ -96,6 +96,14 @@ func (mount *MountInfo) GetConfigOption(option string) (string, error) { return value, nil } +// Init the file system client without actually mounting the file system. +// +// Implements: +// int ceph_init(struct ceph_mount_info *cmount); +func (mount *MountInfo) Init() error { + return getError(C.ceph_init(mount.mount)) +} + // Mount the file system, establishing a connection capable of I/O. // // Implements: diff --git a/cephfs/mount_perms_mimic.go b/cephfs/mount_perms_mimic.go new file mode 100644 index 0000000..604bf65 --- /dev/null +++ b/cephfs/mount_perms_mimic.go @@ -0,0 +1,22 @@ +// +build !luminous +// +// ceph_mount_perms_set available in mimic & later + +package cephfs + +/* +#cgo LDFLAGS: -lcephfs +#cgo CPPFLAGS: -D_FILE_OFFSET_BITS=64 +#include +*/ +import "C" + +// SetMountPerms applies the given UserPerm to the mount object, which it will +// then use to define the connection's ownership credentials. +// This function must be called after Init but before Mount. +// +// Implements: +// int ceph_mount_perms_set(struct ceph_mount_info *cmount, UserPerm *perm); +func (mount *MountInfo) SetMountPerms(perm *UserPerm) error { + return getError(C.ceph_mount_perms_set(mount.mount, perm.userPerm)) +} diff --git a/cephfs/mount_perms_mimic_test.go b/cephfs/mount_perms_mimic_test.go new file mode 100644 index 0000000..86c98ee --- /dev/null +++ b/cephfs/mount_perms_mimic_test.go @@ -0,0 +1,45 @@ +// +build !luminous + +package cephfs + +import ( + "os" + "path" + "syscall" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestSetMountPerms(t *testing.T) { + mount, err := CreateMount() + require.NoError(t, err) + require.NotNil(t, mount) + defer func() { assert.NoError(t, mount.Release()) }() + + err = mount.ReadDefaultConfigFile() + require.NoError(t, err) + + err = mount.Init() + assert.NoError(t, err) + + uperm := NewUserPerm(0, 500, []int{0, 500, 501}) + err = mount.SetMountPerms(uperm) + assert.NoError(t, err) + + err = mount.Mount() + assert.NoError(t, err) + defer func() { assert.NoError(t, mount.Unmount()) }() + + t.Run("checkStat", func(t *testing.T) { + useMount(t) + dirname := "/check-mount-perms" + err := mount.MakeDir(dirname, 0755) + assert.NoError(t, err) + defer mount.RemoveDir(dirname) + s, err := os.Stat(path.Join(CephMountDir, dirname)) + require.NoError(t, err) + assert.EqualValues(t, s.Sys().(*syscall.Stat_t).Gid, 500) + }) +}