| 1 | // Package mtab contains tools to work with /etc/mtab file. |
| 2 | package mtab |
| 3 | |
| 4 | import ( |
| 5 | "bufio" |
| 6 | "io" |
| 7 | "os" |
| 8 | "strings" |
| 9 | ) |
| 10 | |
| 11 | type MountPoint struct { |
| 12 | Dev string |
| 13 | Mount string |
| 14 | Type string |
| 15 | Opts string |
| 16 | } |
| 17 | |
| 18 | // Mounts returns a list of mount point from /etc/mtab. |
| 19 | func Mounts() ([]MountPoint, error) { |
| 20 | file, err := os.Open("/etc/mtab") |
| 21 | if err != nil { |
| 22 | return nil, err |
| 23 | } |
| 24 | defer file.Close() |
| 25 | r := bufio.NewReader(file) |
| 26 | var out []MountPoint |
| 27 | for { |
| 28 | line, err := r.ReadString('\n') |
| 29 | if err == io.EOF { |
| 30 | break |
| 31 | } else if err != nil { |
| 32 | return nil, err |
| 33 | } |
| 34 | fields := strings.Fields(line) |
| 35 | out = append(out, MountPoint{ |
| 36 | Dev: fields[0], |
| 37 | Mount: fields[1], |
| 38 | Type: fields[2], |
| 39 | Opts: fields[3], |
| 40 | }) |
| 41 | } |
| 42 | return out, nil |
| 43 | } |