Skip to content

Commit

Permalink
Rename scsi directory to lib, move SgAttributes to a Device method up…
Browse files Browse the repository at this point in the history
…dateSysfsAttrs
  • Loading branch information
bensallen committed May 2, 2016
1 parent 684ab32 commit f919f99
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 80 deletions.
98 changes: 98 additions & 0 deletions lib/sg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package scsi

import (
"os"
"strconv"

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

// Device is a SCSI Generic Device
type Device struct {
Host int
Chan int
ID int
Lun int
Type int
Opens int
Qdepth int
Busy bool
Online bool
Model string
Vendor string
Rev string
SasAddress string
}

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")

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

d.Model = model
d.Vendor = vendor
d.Rev = rev
d.SasAddress = sasAddress

return nil
}

// SgDevices returns map[int]Device of all SG devices
func SgDevices(sgDevicesPath string) (map[int]*Device, error) {
var devices = map[int]*Device{}

file, err := os.Open(sgDevicesPath)
if err != nil {
return devices, err
}
r := yacr.NewReader(file, '\t', false, false)
var Host, Chan, ID, Lun, Type, Opens, Qdepth, Busy, Online int
for {
if n, err := r.ScanRecord(&Host, &Chan, &ID, &Lun, &Type, &Opens, &Qdepth, &Busy, &Online); err != nil {
break
} else if n != 9 {
break
}
devices[ID] = &Device{
Host: Host,
Chan: Chan,
ID: ID,
Lun: Lun,
Type: Type,
Opens: Opens,
Qdepth: Qdepth,
Busy: itob(Busy),
Online: itob(Online),
}
if err := devices[ID].updateSysfsAttrs(); err != nil {
return devices, err
}

}
err = file.Close()
return devices, err
}
20 changes: 20 additions & 0 deletions lib/sg_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package scsi

import "testing"

func TestSgDevices(t *testing.T) {
devices, err := SgDevices("/proc/scsi/sg/devices")
if err != nil {
t.Fatal(err)
}
if len(devices) == 0 {
t.Fatal("No values")
}
}

/*func TestSgAttributes(t *testing.T) {
attributes := SgAttributes(0)
if len(attributes) == 0 {
t.Fatal("No values")
}
}*/
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"fmt"

"gitlab.alcf.anl.gov/jlse/stopo/scsi"
"gitlab.alcf.anl.gov/jlse/sastopo/scsi"
)

func main() {
Expand Down
79 changes: 0 additions & 79 deletions scsi/sg.go

This file was deleted.

0 comments on commit f919f99

Please sign in to comment.