Check if directory is a mountpoint

How to check if golang if a directory is a mountpoint and what is the device behind the mountpoint?

What OS? What kind of mount: FAT drive, WebDAV, CIFS, …?

  • Linux (rhel and ubuntu)
  • multipath device (e.g : /dev/mapper/mpathd)
  • The filesystem on the mountpoint is less relevant, just want to know if dirX is a mountpoint and if so what is the device of this mountpoint?

I can do something simple like this:
func getMount(mountdir string) (string, error){
res, err := exec.Command(“sh”, “-c”, fmt.Sprintf(“mount | grep -w %s”, mountdir)).Output()
if err != nil{
return “”, err
}
lines := strings.Split(string(res[:]), “\n”)
if len(lines) != 1{
return “”, fmt.Errorf(“bad mount output”)
}
fields := strings.Fields(lines[0])
if len(fields) != 6{
return “”, fmt.Errorf(“bad mount line formating”)
}
device=fields[0]
return device, nill
}

but of cause its better to do it in golang way

Maybe os.File.Stat() contains something useful?

It returns a os.FileInfo.

1 Like

You can parse /proc/self/mountinfo to get a list of all mount points and their backing device.

You can then maybe use multipath to get any further information your looking for.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.