From defc676b3de2ffc5558ba451bfdb45040de1c188 Mon Sep 17 00:00:00 2001 From: gman Date: Thu, 22 Mar 2018 14:01:10 +0100 Subject: [PATCH] cephfs/volume: added the ability to use either FUSE or ceph kernel client --- pkg/cephfs/cephconf.go | 17 +++++++++++++++++ pkg/cephfs/volume.go | 43 +++++++++++++++++++++++++++++++----------- 2 files changed, 49 insertions(+), 11 deletions(-) diff --git a/pkg/cephfs/cephconf.go b/pkg/cephfs/cephconf.go index 155a2319e..ba7609431 100644 --- a/pkg/cephfs/cephconf.go +++ b/pkg/cephfs/cephconf.go @@ -37,15 +37,19 @@ caps mon = "allow r" caps osd = "allow {{perms .ReadOnly}}" ` +const cephSecret = `{{.Key}}` + const ( cephConfigRoot = "/etc/ceph" cephConfigFileName = "ceph.conf" cephKeyringFileNameFmt = "ceph.client.%s.keyring" + cephSecretFileNameFmt = "ceph.client.%s.secret" ) var ( cephConfigTempl *template.Template cephKeyringTempl *template.Template + cephSecretTempl *template.Template ) func init() { @@ -61,6 +65,7 @@ func init() { cephConfigTempl = template.Must(template.New("config").Parse(cephConfig)) cephKeyringTempl = template.Must(template.New("keyring").Funcs(fm).Parse(cephKeyring)) + cephSecretTempl = template.Must(template.New("secret").Parse(cephSecret)) } type cephConfigWriter interface { @@ -102,3 +107,15 @@ type cephKeyringData struct { func (d *cephKeyringData) writeToFile() error { return writeCephTemplate(fmt.Sprintf(cephKeyringFileNameFmt, d.User), 0600, cephKeyringTempl, d) } + +type cephSecretData struct { + User, Key string +} + +func (d *cephSecretData) writeToFile() error { + return writeCephTemplate(fmt.Sprintf(cephSecretFileNameFmt, d.User), 0600, cephSecretTempl, d) +} + +func getCephSecretPath(user string) string { + return path.Join(cephConfigRoot, fmt.Sprintf(cephSecretFileNameFmt, user)) +} diff --git a/pkg/cephfs/volume.go b/pkg/cephfs/volume.go index c51e18a8a..febdca282 100644 --- a/pkg/cephfs/volume.go +++ b/pkg/cephfs/volume.go @@ -21,13 +21,19 @@ import ( "os" ) -type volume struct { - RootPath string - User string +const ( + volumeMounter_fuse = "fuse" + volumeMounter_kernel = "kernel" +) + +type volumeMounter interface { + mount(mountPoint string, volOptions *volumeOptions) error } -func (vol *volume) mount(mountPoint string) error { - out, err := execCommand("ceph-fuse", mountPoint, "-n", "client."+vol.User, "-r", vol.RootPath) +type fuseMounter struct{} + +func (m *fuseMounter) mount(mountPoint string, volOptions *volumeOptions) error { + out, err := execCommand("ceph-fuse", mountPoint, "-n", "client."+volOptions.User, "-r", volOptions.RootPath) if err != nil { return fmt.Errorf("cephfs: ceph-fuse failed with following error: %s\ncephfs: cephf-fuse output: %s", err, out) } @@ -35,19 +41,34 @@ func (vol *volume) mount(mountPoint string) error { return nil } -func (vol *volume) unmount() error { - out, err := execCommand("fusermount", "-u", vol.RootPath) +type kernelMounter struct{} + +func (m *kernelMounter) mount(mountPoint string, volOptions *volumeOptions) error { + out, err := execCommand("modprobe", "ceph") if err != nil { - return fmt.Errorf("cephfs: fusermount failed with following error: %v\ncephfs: fusermount output: %s", err, out) + return fmt.Errorf("cephfs: modprobe failed with following error, %s\ncephfs: modprobe output: %s", err, out) + } + + args := [...]string{ + "-t", "ceph", + fmt.Sprintf("%s:%s", volOptions.Monitors, volOptions.RootPath), + mountPoint, + "-o", + fmt.Sprintf("name=%s,secretfile=%s", volOptions.User, getCephSecretPath(volOptions.User)), + } + + out, err = execCommand("mount", args[:]...) + if err != nil { + return fmt.Errorf("cephfs: mount.ceph failed with following error: %s\ncephfs: mount.ceph output: %s", err, out) } return nil } -func unmountVolume(root string) error { - out, err := execCommand("fusermount", "-u", root) +func unmountVolume(mountPoint string) error { + out, err := execCommand("umount", mountPoint) if err != nil { - return fmt.Errorf("cephfs: fusermount failed with following error: %v\ncephfs: fusermount output: %s", err, out) + return fmt.Errorf("cephfs: umount failed with following error: %v\ncephfs: umount output: %s", err, out) } return nil