Skip to content

Commit

Permalink
Convert to using /sys/class/scsi_devices instead of /proc/scsi/sg/dev…
Browse files Browse the repository at this point in the history
…ices, move go-sysfs to forked repo
  • Loading branch information
bensallen committed May 7, 2016
1 parent 83bc30d commit c33790f
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 69 deletions.
6 changes: 4 additions & 2 deletions lib/enclosure.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package sastopo

func (d *Device) updateEnclosureSerial() error {
import "github.com/bensallen/go-sysfs"

func (d *Device) updateEnclosureSerial(obj sysfs.Object) error {
switch d.Model {
case "SA4600":
return nil
default:
sn, err := vpd80(d.ID)
sn, err := vpd80(obj)
if err != nil {
return err
}
Expand Down
69 changes: 40 additions & 29 deletions lib/sg.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
package sastopo

import (
"os"
"strconv"

"github.com/gwenn/yacr"
"github.com/ungerik/go-sysfs"
"github.com/bensallen/go-sysfs"
)

// Device is a SCSI Generic Device
type Device struct {
Host int
Chan int
ID int
Lun int
ID string
Type int
Opens int
Qdepth int
Busy bool
Online bool
Vendor string
Model string
Rev string
Expand All @@ -29,30 +22,27 @@ type Device struct {
HBA *HBA
}

func itob(i int) bool {
if i == 0 {
return false
}
return true
}

// updateSysfsAttrs adds or updates Model, Vendor, Rev, and SasAddress from sysfs for a SG device
func (d *Device) updateSysfsAttrs() error {
sysfsObject := sysfs.Class.Object("scsi_generic").SubObject("sg" + strconv.Itoa(d.ID)).SubObject("device")
func (d *Device) updateSysfsAttrs(obj sysfs.Object) error {

model, err := sysfsObject.Attribute("model").Read()
model, err := obj.Attribute("model").Read()
if err != nil {
return err
}
vendor, err := obj.Attribute("vendor").Read()
if err != nil {
return err
}
vendor, err := sysfsObject.Attribute("vendor").Read()
rev, err := obj.Attribute("rev").Read()
if err != nil {
return err
}
rev, err := sysfsObject.Attribute("rev").Read()
sasAddress, err := obj.Attribute("sas_address").Read()
if err != nil {
return err
}
sasAddress, err := sysfsObject.Attribute("sas_address").Read()

devType, err := obj.Attribute("type").ReadInt()
if err != nil {
return err
}
Expand All @@ -61,37 +51,38 @@ func (d *Device) updateSysfsAttrs() error {
d.Vendor = vendor
d.Rev = rev
d.SasAddress = sasAddress
d.Type, _ = devType

return nil
}

func (d *Device) updateDriveSerial() error {
sn, err := vpd80(d.ID)
func (d *Device) updateDriveSerial(obj sysfs.Object) error {
sn, err := vpd80(obj)
if err != nil {
return err
}
d.Serial = sn
return nil
}

func (d *Device) updateSerial() error {
func (d *Device) updateSerial(obj sysfs.Object) error {
switch d.Type {
case 0:
if err := d.updateDriveSerial(); err != nil {
if err := d.updateDriveSerial(obj); err != nil {
return err
}
case 13:
if err := d.updateEnclosureSerial(); err != nil {
if err := d.updateEnclosureSerial(obj); err != nil {
return err
}
default:
return &errUnknownType{"dev: /dev/sg" + strconv.Itoa(d.ID) + " type: " + strconv.Itoa(d.Type)}
return &errUnknownType{"dev: " + d.ID + " type: " + strconv.Itoa(d.Type)}
}
return nil
}

// SgDevices returns map[int]Device of all SG devices
func SgDevices(sgDevicesPath string) (map[int]*Device, error) {
/*func SgDevices(sgDevicesPath string) (map[int]*Device, error) {
var devices = map[int]*Device{}
file, err := os.Open(sgDevicesPath)
Expand Down Expand Up @@ -127,4 +118,24 @@ func SgDevices(sgDevicesPath string) (map[int]*Device, error) {
}
err = file.Close()
return devices, err
}*/

// SgDevices2 returns map[int]Device of all SG devices
func SgDevices2() (map[string]*Device, error) {
var devices = map[string]*Device{}
sysfsObjects := sysfs.Class.Object("scsi_device").SubObjects()

for d := 0; d <= len(sysfsObjects); d++ {
devices[sysfsObjects[d].Name()] = &Device{
ID: sysfsObjects[d].Name(),
}
if err := devices[sysfsObjects[d].Name()].updateSysfsAttrs(sysfsObjects[d].SubObject("device")); err != nil {
return devices, err
}
if err := devices[sysfsObjects[d].Name()].updateSerial(sysfsObjects[d].SubObject("device")); err != nil {
return devices, err
}
}
return devices, nil

}
39 changes: 6 additions & 33 deletions lib/vpd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,17 @@ package sastopo

import (
"io"
"os"
"strconv"

"github.com/bensallen/go-sysfs"
)

// trimPoints loops through a []byte looking for left trim and right trim points
// for trimming 0x00 (null) and 0x20 (ascii space)
func trimPoints(line []byte) (start int, stop int) {
func vpd80(obj sysfs.Object) (string, error) {
vpdPg80, err := obj.Attribute("vpd_pg80")

//left trim point
for i := 0; i < len(line); i++ {
if line[i] == 0x20 || line[i] == 0x00 {
continue
} else {
start = i
break
}
}
line := make([]byte, 128)

//right trim point
for i := len(line) - 1; i >= 0; i-- {
if line[i] == 0x20 || line[i] == 0x00 {
continue
} else {
stop = i + 1
break
}
}
return start, stop
}
line, n, err := vpdPg80.ReadBytes(4, 128)

func vpd80(sg int) (string, error) {
file, err := os.Open("/sys/class/scsi_generic/sg" + strconv.Itoa(sg) + "/device/vpd_pg80")
defer file.Close()
if err != nil {
return "", err
}
line := make([]byte, 128)
n, err := file.ReadAt(line, 4)
if err != nil && err != io.EOF {
return "", err
} else if n == 0 {
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func main() {
devices, err := sastopo.SgDevices("/proc/scsi/sg/devices")
devices, err := sastopo.SgDevices2()
if err != nil {
fmt.Print(err)
}
Expand Down
File renamed without changes.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.

0 comments on commit c33790f

Please sign in to comment.