mirror of
https://github.com/ceph/ceph-csi.git
synced 2024-12-18 11:00:25 +00:00
Move resolving bind mount logic from k8s
This commit is contained in:
parent
ea75a9d162
commit
0e60dabca3
@ -19,6 +19,8 @@ package rbd
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
@ -215,3 +217,30 @@ func (ns *nodeServer) NodeUnstageVolume(
|
|||||||
|
|
||||||
return nil, status.Error(codes.Unimplemented, "")
|
return nil, status.Error(codes.Unimplemented, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func resolveBindMountedBlockDevice(mountPath string) (string, error) {
|
||||||
|
cmd := exec.Command("findmnt", "-n", "-o", "SOURCE", "--first-only", "--target", mountPath)
|
||||||
|
out, err := cmd.CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
glog.V(2).Infof("Failed findmnt command for path %s: %s %v", mountPath, out, err)
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return parseFindMntResolveSource(string(out))
|
||||||
|
}
|
||||||
|
|
||||||
|
// parse output of "findmnt -o SOURCE --first-only --target" and return just the SOURCE
|
||||||
|
func parseFindMntResolveSource(out string) (string, error) {
|
||||||
|
// cut trailing newline
|
||||||
|
out = strings.TrimSuffix(out, "\n")
|
||||||
|
// Check if out is a mounted device
|
||||||
|
reMnt := regexp.MustCompile("^(/[^/]+(?:/[^/]*)*)$")
|
||||||
|
if match := reMnt.FindStringSubmatch(out); match != nil {
|
||||||
|
return match[1], nil
|
||||||
|
}
|
||||||
|
// Check if out is a block device
|
||||||
|
reBlk := regexp.MustCompile("^devtmpfs\\[(/[^/]+(?:/[^/]*)*)\\]$")
|
||||||
|
if match := reBlk.FindStringSubmatch(out); match != nil {
|
||||||
|
return fmt.Sprintf("/dev%s", match[1]), nil
|
||||||
|
}
|
||||||
|
return "", fmt.Errorf("parseFindMntResolveSource: %s doesn't match to any expected findMnt output", out)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user