Skip to content

Instantly share code, notes, and snippets.

@mvanotti
Created May 31, 2014 19:20
Show Gist options
  • Save mvanotti/66f23c32f1aa4cc2f042 to your computer and use it in GitHub Desktop.
Save mvanotti/66f23c32f1aa4cc2f042 to your computer and use it in GitHub Desktop.
Display all the filesystem mount points and their respective type.
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
// Display all the mounted filesystems and their respective type.
// usage: go run parsemounts.go
//
// It parses the /proc/mounts file. Each line of the file is a white-space separated
// list of fields. The first field is the mount device, the second field is the mount point
// an the third field is the filesystem type, the other fields are mount options.
// If there's a whitespace or a backslash in one of the fields, it gets escaped. This program also unescapes them.
func main() {
fsinfo := make(map[string]string)
f, err := os.Open("/proc/mounts")
if err != nil {
panic(err)
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
fields := strings.Fields(scanner.Text())
mnt := unescape(fields[1])
typ := unescape(fields[2])
fsinfo[mnt] = typ
}
for k, v := range fsinfo {
fmt.Printf("Mountpoint: %s\tType: %s\n", k, v)
}
if err := scanner.Err(); err != nil {
panic(err)
}
}
// unescape replaces the escape sequences that a string in /proc/mounts may have.
// It replaces octal whitespaces (\040, \011, \012) by their respective values,
// and after that, it replaces the escaped backslashes (\134).
func unescape(s string) string {
s = strings.Replace(s, "\\040", " ", -1)
s = strings.Replace(s, "\\011", "\t", -1)
s = strings.Replace(s, "\\012", "\n", -1)
s = strings.Replace(s, "\\134", "\\", -1)
return s
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment