Add support for /proc/self/mountinfo

This commit is contained in:
Harshavardhana
2015-03-21 21:18:46 -07:00
parent 394ef0daa8
commit 6e380c9247
3 changed files with 68 additions and 2 deletions
+1
View File
@@ -25,6 +25,7 @@ var (
SysfsClassBlock = "/sys/class/block/"
SysfsClassScsiDevices = "/sys/class/scsi_device/"
Udev = "/dev/"
Mountinfo = "/proc/self/mountinfo"
DevDiskByIDDir = "/dev/disk/by-id"
)
+7
View File
@@ -57,3 +57,10 @@ type NoAttributesFound struct{}
func (e NoAttributesFound) Error() string {
return "No Scsi Attributes Found"
}
// CorruptedMountInfo - mount info corrupted
type CorruptedMountInfo struct{}
func (e CorruptedMountInfo) Error() string {
return "mount info corrupted"
}
+60 -2
View File
@@ -19,8 +19,10 @@
package scsi
import (
// "fmt"
"bufio"
"io"
"io/ioutil"
"os"
"path"
"strings"
)
@@ -28,9 +30,17 @@ import (
// NOTE : supporting virtio based scsi devices
// is out of scope for this implementation
var optFields = []string{
"shared:",
"master:",
"propagate_from:",
"unbindable",
}
// Devices - list of all scsi disks
type Devices struct {
List []Disk
List []Disk
Mountinfo []map[string]string
}
// Disk - struct which carries per disk name, scsi and disk attributes
@@ -154,6 +164,51 @@ func (d *Disk) getDiskAttrs(disk string) error {
return nil
}
// getMountInfo - populates all mount points on the disk
func (d *Devices) getMountInfo() error {
fl, err := os.Open(Mountinfo)
defer fl.Close()
if err != nil {
return err
}
reader := bufio.NewReader(fl)
for err == nil {
var line string
line, err = reader.ReadString('\n')
if err != nil {
break
}
splits := strings.Fields(line)
if len(splits) > 11 {
return CorruptedMountInfo{}
}
mount := make(map[string]string)
mount["mountID"] = splits[0]
mount["parentID"] = splits[1]
mount["majorminor"] = splits[2]
mount["root"] = splits[3]
mount["mountPoint"] = splits[4]
mount["mountOptions"] = splits[5]
if len(splits) <= 10 {
mount["separator"] = splits[6]
mount["fsType"] = splits[7]
mount["mountSource"] = splits[8]
mount["superBlockOptions"] = splits[9]
} else if len(splits) == 11 {
mount["optFields"] = splits[6]
mount["separator"] = splits[7]
mount["fsType"] = splits[8]
mount["mountSource"] = splits[9]
mount["superBlockOptions"] = splits[10]
}
d.Mountinfo = append(d.Mountinfo, mount)
}
if err != io.EOF {
return err
}
return nil
}
// Get - get queries local system and populates all the attributes
func (d *Devices) Get() error {
var scsidevices []string
@@ -169,6 +224,9 @@ func (d *Devices) Get() error {
return NoDevicesFoundOnSystem{}
}
if err := d.getMountInfo(); err != nil {
return err
}
for _, scsi := range scsidevices {
var _scsi Disk
scsiAttrPath := path.Join(SysfsScsiDevices, scsi, "/")