Merge pull request #132 from harshavardhana/pr_out_add_linux_scsi_disk_detection_and_attribute_mapping
commit
c58d22977a
@ -0,0 +1 @@ |
||||
scsi |
@ -0,0 +1,64 @@ |
||||
/* |
||||
* Mini Object Storage, (C) 2014 Minio, Inc. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
// !build linux,amd64
|
||||
|
||||
package scsi |
||||
|
||||
var ( |
||||
// From 2.6.x kernel onwards, no need to support procfs
|
||||
SYSFSROOT = "/sys" |
||||
SYSFS_SCSI_DEVICES = "/sys/bus/scsi/devices/" |
||||
SYSFS_BLOCK = "/block/" |
||||
SYSFS_CLASS_SCSI_DEVICES = "/sys/class/scsi_device/" |
||||
UDEV = "/dev/" |
||||
DEV_DISK_BYID_DIR = "/dev/disk/by-id" |
||||
) |
||||
|
||||
var SCSI_DEVICE_TYPES = []string{ |
||||
"disk", |
||||
"tape", |
||||
"printer", |
||||
"process", |
||||
"worm", |
||||
"cd/dvd", |
||||
"scanner", |
||||
"optical", |
||||
"mediumx", |
||||
"comms", |
||||
"(0xa)", |
||||
"(0xb)", |
||||
"storage", |
||||
"enclosu", |
||||
"sim disk", |
||||
"optical rd", |
||||
"bridge", |
||||
"osd", |
||||
"adi", |
||||
"sec man", |
||||
"zbc", |
||||
"(0x15)", |
||||
"(0x16)", |
||||
"(0x17)", |
||||
"(0x18)", |
||||
"(0x19)", |
||||
"(0x1a)", |
||||
"(0x1b)", |
||||
"(0x1c)", |
||||
"(0x1e)", |
||||
"wlun", |
||||
"no dev", |
||||
} |
@ -0,0 +1,96 @@ |
||||
/* |
||||
* Mini Object Storage, (C) 2014 Minio, Inc. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
// !build linux,amd64
|
||||
|
||||
package scsi |
||||
|
||||
import ( |
||||
"errors" |
||||
"io/ioutil" |
||||
"path" |
||||
) |
||||
|
||||
// NOTE : supporting virtio based scsi devices
|
||||
// is out of scope for this implementation
|
||||
|
||||
type _Scsi struct { |
||||
device string |
||||
attrMap map[string][]byte |
||||
} |
||||
|
||||
type Devices struct { |
||||
List []_Scsi |
||||
} |
||||
|
||||
func (d *Devices) Get() error { |
||||
var scsidevices []string |
||||
var scsiAttrList []string |
||||
|
||||
sysfs := path.Join(SYSFS_SCSI_DEVICES) |
||||
sysFiles, err := ioutil.ReadDir(sysfs) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
scsidevices = filterdevices(sysFiles) |
||||
if len(scsidevices) == 0 { |
||||
return errors.New("No scsi devices found on the system") |
||||
} |
||||
|
||||
for _, scsi := range scsidevices { |
||||
var _scsi _Scsi |
||||
scsiAttrPath := path.Join(sysfs, scsi, "/") |
||||
scsiAttrs, err := ioutil.ReadDir(scsiAttrPath) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
scsiBlockPath := path.Join(sysfs, scsi, SYSFS_BLOCK) |
||||
scsidevList, err := ioutil.ReadDir(scsiBlockPath) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
if len(scsidevList) > 1 { |
||||
return errors.New("Scsi address points to multiple block devices") |
||||
} |
||||
|
||||
_scsi.device = UDEV + scsidevList[0].Name() |
||||
for _, sa := range scsiAttrs { |
||||
// Skip directories
|
||||
if sa.IsDir() { |
||||
continue |
||||
} |
||||
// Skip symlinks
|
||||
if !sa.Mode().IsRegular() { |
||||
continue |
||||
} |
||||
// Skip, not readable, write-only
|
||||
if sa.Mode().Perm() == 128 { |
||||
continue |
||||
} |
||||
scsiAttrList = append(scsiAttrList, sa.Name()) |
||||
} |
||||
|
||||
if len(scsiAttrList) == 0 { |
||||
return errors.New("No scsi attributes found") |
||||
} |
||||
attrMap := getattrs(scsiAttrPath, scsiAttrList) |
||||
_scsi.attrMap = attrMap |
||||
d.List = append(d.List, _scsi) |
||||
} |
||||
return nil |
||||
} |
@ -0,0 +1,20 @@ |
||||
// !build linux,amd64
|
||||
package scsi |
||||
|
||||
import ( |
||||
. "gopkg.in/check.v1" |
||||
"testing" |
||||
) |
||||
|
||||
type MySuite struct{} |
||||
|
||||
var _ = Suite(&MySuite{}) |
||||
|
||||
func Test(t *testing.T) { TestingT(t) } |
||||
|
||||
func (s *MySuite) TestSCSI(c *C) { |
||||
var d Devices |
||||
err := d.Get() |
||||
c.Assert(err, IsNil) |
||||
c.Assert(len(d.List), Equals, 1) |
||||
} |
@ -0,0 +1,48 @@ |
||||
/* |
||||
* Mini Object Storage, (C) 2014 Minio, Inc. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
// !build linux,amd64
|
||||
|
||||
package scsi |
||||
|
||||
import ( |
||||
"io/ioutil" |
||||
"os" |
||||
"path" |
||||
"strings" |
||||
) |
||||
|
||||
func getattrs(scsiAttrPath string, scsiAttrList []string) map[string][]byte { |
||||
attrMap := make(map[string][]byte) |
||||
for _, attr := range scsiAttrList { |
||||
value, _ := ioutil.ReadFile(path.Join(scsiAttrPath, attr)) |
||||
attrMap[attr] = value |
||||
} |
||||
return attrMap |
||||
} |
||||
|
||||
func filterdevices(files []os.FileInfo) (scsidevices []string) { |
||||
for _, fi := range files { |
||||
if strings.Contains(fi.Name(), "host") { |
||||
continue |
||||
} |
||||
if strings.Contains(fi.Name(), "target") { |
||||
continue |
||||
} |
||||
scsidevices = append(scsidevices, fi.Name()) |
||||
} |
||||
return scsidevices |
||||
} |
Loading…
Reference in new issue