mirror of
https://github.com/TECHNOFAB11/zfs-localpv.git
synced 2025-12-11 22:10:11 +01:00
chore(refactor): move xfs and mount code out of zfs package
Signed-off-by: Pawan <pawan@mayadata.io>
This commit is contained in:
parent
935a544538
commit
d537bd3655
5 changed files with 86 additions and 59 deletions
|
|
@ -27,6 +27,7 @@ import (
|
|||
"github.com/openebs/zfs-localpv/pkg/mgmt/restore"
|
||||
"github.com/openebs/zfs-localpv/pkg/mgmt/snapshot"
|
||||
"github.com/openebs/zfs-localpv/pkg/mgmt/volume"
|
||||
"github.com/openebs/zfs-localpv/pkg/mount"
|
||||
"github.com/openebs/zfs-localpv/pkg/zfs"
|
||||
"golang.org/x/net/context"
|
||||
"golang.org/x/sys/unix"
|
||||
|
|
@ -349,7 +350,7 @@ func (ns *node) NodeGetVolumeStats(
|
|||
return nil, status.Error(codes.InvalidArgument, "path is not provided")
|
||||
}
|
||||
|
||||
if zfs.IsMountPath(path) == false {
|
||||
if mount.IsMountPath(path) == false {
|
||||
return nil, status.Error(codes.InvalidArgument, "path is not a mount path")
|
||||
}
|
||||
|
||||
|
|
|
|||
64
pkg/mount/mount_utils.go
Normal file
64
pkg/mount/mount_utils.go
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
/*
|
||||
Copyright 2020 The OpenEBS Authors
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package mount
|
||||
|
||||
import (
|
||||
"k8s.io/kubernetes/pkg/util/mount"
|
||||
)
|
||||
|
||||
// GetMounts gets mountpoints for the specified volume
|
||||
func GetMounts(dev string) ([]string, error) {
|
||||
|
||||
var (
|
||||
currentMounts []string
|
||||
err error
|
||||
mountList []mount.MountPoint
|
||||
)
|
||||
|
||||
mounter := mount.New("")
|
||||
// Get list of mounted paths present with the node
|
||||
if mountList, err = mounter.List(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, mntInfo := range mountList {
|
||||
if mntInfo.Device == dev {
|
||||
currentMounts = append(currentMounts, mntInfo.Path)
|
||||
}
|
||||
}
|
||||
return currentMounts, nil
|
||||
}
|
||||
|
||||
// IsMountPath returns true if path is a mount path
|
||||
func IsMountPath(path string) bool {
|
||||
|
||||
var (
|
||||
err error
|
||||
mountList []mount.MountPoint
|
||||
)
|
||||
|
||||
mounter := mount.New("")
|
||||
// Get list of mounted paths present with the node
|
||||
if mountList, err = mounter.List(); err != nil {
|
||||
return false
|
||||
}
|
||||
for _, mntInfo := range mountList {
|
||||
if mntInfo.Path == path {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
|
@ -14,9 +14,10 @@ See the License for the specific language governing permissions and
|
|||
limitations under the License.
|
||||
*/
|
||||
|
||||
package zfs
|
||||
package xfs
|
||||
|
||||
import (
|
||||
"github.com/openebs/zfs-localpv/pkg/mount"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
|
|
@ -26,9 +27,9 @@ import (
|
|||
"k8s.io/klog"
|
||||
)
|
||||
|
||||
func xfsTempMount(volume string) error {
|
||||
device := ZFSDevPath + volume
|
||||
pvol := strings.Split(volume, "/")
|
||||
func xfsTempMount(device string) error {
|
||||
pvol := strings.Split(device, "/")
|
||||
volname := pvol[len(pvol)-1]
|
||||
|
||||
// evaluate the symlink to get the dev path for volume
|
||||
dev, err := filepath.EvalSymlinks(device)
|
||||
|
|
@ -37,7 +38,7 @@ func xfsTempMount(volume string) error {
|
|||
}
|
||||
|
||||
// create a temporary directory to mount the xfs file system
|
||||
tmpdir := "/tmp/" + pvol[1]
|
||||
tmpdir := "/tmp/" + volname
|
||||
err = os.Mkdir(tmpdir, 0755)
|
||||
if os.IsNotExist(err) {
|
||||
klog.Errorf("xfs: failed to create tmpdir %s error: %s", tmpdir, err.Error())
|
||||
|
|
@ -49,7 +50,7 @@ func xfsTempMount(volume string) error {
|
|||
* in previous attempt. Checking here if device is not mounted then only attempt
|
||||
* to mount it, otherwise proceed with the umount.
|
||||
*/
|
||||
curMounts, err := GetMounts(dev)
|
||||
curMounts, err := mount.GetMounts(dev)
|
||||
if err != nil {
|
||||
klog.Errorf("xfs: get mounts failed dev: %s err: %v", device, err.Error())
|
||||
return err
|
||||
|
|
@ -58,7 +59,7 @@ func xfsTempMount(volume string) error {
|
|||
cmd := exec.Command("mount", "-o", "nouuid", device, tmpdir)
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
klog.Errorf("xfs: failed to mount volume %s => %s error: %s", device, tmpdir, string(out))
|
||||
klog.Errorf("xfs: failed to mount device %s => %s error: %s", device, tmpdir, string(out))
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
|
|
@ -90,11 +91,11 @@ func xfsTempMount(volume string) error {
|
|||
* There might be something there in the xfs log, we have to clear them
|
||||
* so that filesystem is clean and we can generate the UUID for it.
|
||||
*/
|
||||
func xfsGenerateUUID(volume string) error {
|
||||
device := ZFSDevPath + volume
|
||||
|
||||
// GenerateUUID generates a new UUID for the given device
|
||||
func GenerateUUID(device string) error {
|
||||
// temporary mount the volume with nouuid to replay the logs
|
||||
err := xfsTempMount(volume)
|
||||
err := xfsTempMount(device)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -103,9 +104,9 @@ func xfsGenerateUUID(volume string) error {
|
|||
cmd := exec.Command("xfs_admin", "-U", "generate", device)
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
klog.Errorf("xfs: uuid generate failed %s error: %s", volume, string(out))
|
||||
klog.Errorf("xfs: uuid generate failed for device %s error: %s", device, string(out))
|
||||
return err
|
||||
}
|
||||
klog.Infof("xfs: generated UUID for the cloned volume %s \n %v", volume, string(out))
|
||||
klog.Infof("xfs: generated UUID for the device %s \n %v", device, string(out))
|
||||
return nil
|
||||
}
|
||||
|
|
@ -21,6 +21,7 @@ import (
|
|||
"os/exec"
|
||||
|
||||
apis "github.com/openebs/zfs-localpv/pkg/apis/openebs.io/zfs/v1"
|
||||
mnt "github.com/openebs/zfs-localpv/pkg/mount"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
"k8s.io/klog"
|
||||
|
|
@ -123,49 +124,6 @@ func UmountVolume(vol *apis.ZFSVolume, targetPath string,
|
|||
return nil
|
||||
}
|
||||
|
||||
// GetMounts gets mountpoints for the specified volume
|
||||
func GetMounts(dev string) ([]string, error) {
|
||||
|
||||
var (
|
||||
currentMounts []string
|
||||
err error
|
||||
mountList []mount.MountPoint
|
||||
)
|
||||
|
||||
mounter := mount.New("")
|
||||
// Get list of mounted paths present with the node
|
||||
if mountList, err = mounter.List(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, mntInfo := range mountList {
|
||||
if mntInfo.Device == dev {
|
||||
currentMounts = append(currentMounts, mntInfo.Path)
|
||||
}
|
||||
}
|
||||
return currentMounts, nil
|
||||
}
|
||||
|
||||
// IsMountPath returns true if path is a mount path
|
||||
func IsMountPath(path string) bool {
|
||||
|
||||
var (
|
||||
err error
|
||||
mountList []mount.MountPoint
|
||||
)
|
||||
|
||||
mounter := mount.New("")
|
||||
// Get list of mounted paths present with the node
|
||||
if mountList, err = mounter.List(); err != nil {
|
||||
return false
|
||||
}
|
||||
for _, mntInfo := range mountList {
|
||||
if mntInfo.Path == path {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func verifyMountRequest(vol *apis.ZFSVolume, mountpath string) error {
|
||||
if len(mountpath) == 0 {
|
||||
return status.Error(codes.InvalidArgument, "verifyMount: mount path missing in request")
|
||||
|
|
@ -195,7 +153,7 @@ func verifyMountRequest(vol *apis.ZFSVolume, mountpath string) error {
|
|||
* be unmounted before proceeding to the mount
|
||||
* operation.
|
||||
*/
|
||||
currentMounts, err := GetMounts(devicePath)
|
||||
currentMounts, err := mnt.GetMounts(devicePath)
|
||||
if err != nil {
|
||||
klog.Errorf("can not get mounts for volume:%s dev %s err: %v",
|
||||
vol.Name, devicePath, err.Error())
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@ import (
|
|||
|
||||
apis "github.com/openebs/zfs-localpv/pkg/apis/openebs.io/zfs/v1"
|
||||
"github.com/openebs/zfs-localpv/pkg/btrfs"
|
||||
"github.com/openebs/zfs-localpv/pkg/xfs"
|
||||
k8serrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
"k8s.io/klog"
|
||||
"strings"
|
||||
|
|
@ -440,7 +441,8 @@ func CreateClone(vol *apis.ZFSVolume) error {
|
|||
}
|
||||
|
||||
if vol.Spec.FsType == "xfs" {
|
||||
return xfsGenerateUUID(volume)
|
||||
device := ZFSDevPath + volume
|
||||
return xfs.GenerateUUID(device)
|
||||
}
|
||||
if vol.Spec.FsType == "btrfs" {
|
||||
device := ZFSDevPath + volume
|
||||
|
|
@ -814,7 +816,8 @@ func CreateRestore(rstr *apis.ZFSRestore) error {
|
|||
* so that we can mount it.
|
||||
*/
|
||||
if vol.Spec.FsType == "xfs" {
|
||||
return xfsGenerateUUID(volume)
|
||||
device := ZFSDevPath + volume
|
||||
return xfs.GenerateUUID(device)
|
||||
}
|
||||
if vol.Spec.FsType == "btrfs" {
|
||||
device := ZFSDevPath + volume
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue