Move resolving bind mount logic from k8s

This commit is contained in:
Masaki Kimura 2018-11-07 02:05:19 +00:00 committed by Huamin Chen
parent ea75a9d162
commit 0e60dabca3

View File

@ -19,6 +19,8 @@ package rbd
import (
"fmt"
"os"
"os/exec"
"regexp"
"strings"
"github.com/golang/glog"
@ -215,3 +217,30 @@ func (ns *nodeServer) NodeUnstageVolume(
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)
}