From 6e380c9247cbb2d64df8bcc8333f191e08a294d4 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Sat, 21 Mar 2015 21:05:24 -0700 Subject: [PATCH] Add support for /proc/self/mountinfo --- pkg/os/scsi/constants_linux.go | 1 + pkg/os/scsi/errors.go | 7 ++++ pkg/os/scsi/scsi_linux.go | 62 ++++++++++++++++++++++++++++++++-- 3 files changed, 68 insertions(+), 2 deletions(-) diff --git a/pkg/os/scsi/constants_linux.go b/pkg/os/scsi/constants_linux.go index d7116419..044506f2 100644 --- a/pkg/os/scsi/constants_linux.go +++ b/pkg/os/scsi/constants_linux.go @@ -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" ) diff --git a/pkg/os/scsi/errors.go b/pkg/os/scsi/errors.go index dcb7ef68..6b118fe1 100644 --- a/pkg/os/scsi/errors.go +++ b/pkg/os/scsi/errors.go @@ -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" +} diff --git a/pkg/os/scsi/scsi_linux.go b/pkg/os/scsi/scsi_linux.go index 24d6937f..2a7d5f10 100644 --- a/pkg/os/scsi/scsi_linux.go +++ b/pkg/os/scsi/scsi_linux.go @@ -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, "/")