Skip to content

Commit

Permalink
Start adding serial number fetching functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
bensallen committed May 5, 2016
1 parent 4244008 commit bc45f7a
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 2 deletions.
15 changes: 15 additions & 0 deletions lib/enclosure.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package sastopo

func (d *Device) updateEnclosureSerial() error {
switch d.Model {
case "SA4600":
default:
sn, err := vpd80(d.ID)
if err != nil {
return err
}
d.Serial = sn
}

return nil
}
9 changes: 9 additions & 0 deletions lib/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package sastopo

import "fmt"

type errUnknownType struct{ Msg string }

func (e *errUnknownType) Error() string {
return fmt.Sprintf("unknown device type: %s", e.Msg)
}
7 changes: 7 additions & 0 deletions lib/hba.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package sastopo

// HBA is a PCI SAS Host-bus Adapter
type HBA struct {
PciID string
Slot string
}
29 changes: 28 additions & 1 deletion lib/sg.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package scsi
package sastopo

import (
"os"
Expand All @@ -23,6 +23,10 @@ type Device struct {
Vendor string
Rev string
SasAddress string
Serial string
Slot int
Enclosure *Device
HBA *HBA
}

func itob(i int) bool {
Expand Down Expand Up @@ -61,6 +65,26 @@ func (d *Device) updateSysfsAttrs() error {
return nil
}

func (d *Device) updateDriveSerial() error {
return nil
}

func (d *Device) updateSerial() error {
switch d.Type {
case 0:
if err := d.updateDriveSerial(); err != nil {
return err
}
case 13:
if err := d.updateEnclosureSerial(); err != nil {
return err
}
default:
return &errUnknownType{"dev: /dev/sg" + strconv.Itoa(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) {
var devices = map[int]*Device{}
Expand Down Expand Up @@ -91,6 +115,9 @@ func SgDevices(sgDevicesPath string) (map[int]*Device, error) {
if err := devices[ID].updateSysfsAttrs(); err != nil {
return devices, err
}
if err := devices[ID].updateSerial(); err != nil {
return devices, err
}

}
err = file.Close()
Expand Down
2 changes: 1 addition & 1 deletion lib/sg_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package scsi
package sastopo

import "testing"

Expand Down
23 changes: 23 additions & 0 deletions lib/vpd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package sastopo

import (
"io"
"os"
"strconv"
)

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 {
return "", nil
}
return string(line[:n]), nil
}

0 comments on commit bc45f7a

Please sign in to comment.