From 0e60dabca34abc802e607a5e700395376f624d51 Mon Sep 17 00:00:00 2001 From: Masaki Kimura Date: Wed, 7 Nov 2018 02:05:19 +0000 Subject: [PATCH] Move resolving bind mount logic from k8s --- pkg/rbd/nodeserver.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/pkg/rbd/nodeserver.go b/pkg/rbd/nodeserver.go index a2b7785f1..3cea36dc7 100644 --- a/pkg/rbd/nodeserver.go +++ b/pkg/rbd/nodeserver.go @@ -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) +}