From 15c53d83999d90af3a6cf16f99b6572794a0a124 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Thu, 19 Mar 2015 15:00:45 -0700 Subject: [PATCH] Add scsi detection package for mc. This is a generic usable package to be wired up with donut, eventually facilitating functionalities like 'add-disk', 'remove-disk' --- pkg/os/scsi/constants_linux.go | 64 ++++++++++++ pkg/os/scsi/errors.go | 52 ++++++++++ pkg/os/scsi/scsi_linux.go | 173 +++++++++++++++++++++++++++++++++ pkg/os/scsi/scsi_linux_test.go | 40 ++++++++ pkg/os/scsi/utils_linux.go | 48 +++++++++ 5 files changed, 377 insertions(+) create mode 100644 pkg/os/scsi/constants_linux.go create mode 100644 pkg/os/scsi/errors.go create mode 100644 pkg/os/scsi/scsi_linux.go create mode 100644 pkg/os/scsi/scsi_linux_test.go create mode 100644 pkg/os/scsi/utils_linux.go diff --git a/pkg/os/scsi/constants_linux.go b/pkg/os/scsi/constants_linux.go new file mode 100644 index 00000000..265f45da --- /dev/null +++ b/pkg/os/scsi/constants_linux.go @@ -0,0 +1,64 @@ +/* + * Minimalist Object Storage, (C) 2015 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 + +// From 2.6.x kernel onwards, no need to support procfs +var ( + SysfsScsiDevices = "/sys/bus/scsi/devices/" + SysfsBlock = "/sys/block/" + SysfsClassScsiDevices = "/sys/class/scsi_device/" + Udev = "/dev/" + DevDiskByIDDir = "/dev/disk/by-id" +) + +// ScsiDeviceTypes - non exhaustive list of all the device types in linux +var ScsiDeviceTypes = []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", +} diff --git a/pkg/os/scsi/errors.go b/pkg/os/scsi/errors.go new file mode 100644 index 00000000..538e9a08 --- /dev/null +++ b/pkg/os/scsi/errors.go @@ -0,0 +1,52 @@ +/* + * Minimalist Object Storage, (C) 2015 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. + */ + +package scsi + +// NoDiskAttributesFound - no disk attributes found +type NoDiskAttributesFound struct{} + +func (e NoDiskAttributesFound) Error() string { + return "No Disk Attributes Found" +} + +// NoDiskQueueAttributesFound - no disk queue attributes found +type NoDiskQueueAttributesFound struct{} + +func (e NoDiskQueueAttributesFound) Error() string { + return "No Disk Queue Attributes Found" +} + +// NoDevicesFoundOnSystem - no scsi disks found on a given system +type NoDevicesFoundOnSystem struct{} + +func (e NoDevicesFoundOnSystem) Error() string { + return "No scsi devices found on the system" +} + +// AddressPointsToMultipleBlockDevices - scsi address points to multiple block devices +type AddressPointsToMultipleBlockDevices struct{} + +func (e AddressPointsToMultipleBlockDevices) Error() string { + return "Scsi address points to multiple block devices" +} + +// NoAttributesFound - no disk scsi attributes found +type NoAttributesFound struct{} + +func (e NoAttributesFound) Error() string { + return "No Scsi Attributes Found" +} diff --git a/pkg/os/scsi/scsi_linux.go b/pkg/os/scsi/scsi_linux.go new file mode 100644 index 00000000..73854008 --- /dev/null +++ b/pkg/os/scsi/scsi_linux.go @@ -0,0 +1,173 @@ +/* + * Minimalist Object Storage, (C) 2015 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" + "path" +) + +// NOTE : supporting virtio based scsi devices +// is out of scope for this implementation + +// Devices - list of all scsi disks +type Devices struct { + List []Disk +} + +// Disk - struct which carries per disk name, scsi and disk attributes +type Disk struct { + Name string + Scsiattrmap map[string][]byte + Diskattrmap map[string][]byte +} + +// getDisk - populates all the disk related attributes +func (d *Devices) getDisk(disk string) (map[string][]byte, error) { + var diskAttrsList []string + var diskQueueAttrs []string + aggrAttrMap := make(map[string][]byte) + + sysfsBlockDev := path.Join(SysfsBlock, disk) + sysfsBlockDevQueue := path.Join(sysfsBlockDev, "/queue") + + scsiFiles, err := ioutil.ReadDir(sysfsBlockDev) + if err != nil { + return nil, err + } + + scsiQueueFiles, err := ioutil.ReadDir(sysfsBlockDevQueue) + if err != nil { + return nil, err + } + + for _, sf := range scsiFiles { + if sf.IsDir() { + continue + } + // Skip symlinks + if !sf.Mode().IsRegular() { + continue + } + // Skip, not readable, write-only + if sf.Mode().Perm() == 128 { + continue + } + diskAttrsList = append(diskAttrsList, sf.Name()) + } + + for _, sf := range scsiQueueFiles { + if sf.IsDir() { + continue + } + // Skip symlinks + if !sf.Mode().IsRegular() { + continue + } + // Skip, not readable, write-only + if sf.Mode().Perm() == 128 { + continue + } + diskQueueAttrs = append(diskQueueAttrs, sf.Name()) + } + + if len(diskAttrsList) == 0 { + return nil, NoDiskAttributesFound{} + } + + if len(diskQueueAttrs) == 0 { + return nil, NoDiskQueueAttributesFound{} + } + + diskAttrMap := getattrs(sysfsBlockDev, diskAttrsList) + diskQueueAttrMap := getattrs(sysfsBlockDevQueue, diskQueueAttrs) + + for k, v := range diskAttrMap { + aggrAttrMap[k] = v + } + + for k, v := range diskQueueAttrMap { + aggrAttrMap[k] = v + } + + return aggrAttrMap, nil +} + +// Get - get queries local system and populates all the attributes +func (d *Devices) Get() error { + var scsidevices []string + var scsiAttrList []string + + sysFiles, err := ioutil.ReadDir(SysfsScsiDevices) + if err != nil { + return err + } + + scsidevices = filterdevices(sysFiles) + if len(scsidevices) == 0 { + return NoDevicesFoundOnSystem{} + } + + for _, scsi := range scsidevices { + var _scsi Disk + scsiAttrPath := path.Join(SysfsScsiDevices, scsi, "/") + scsiAttrs, err := ioutil.ReadDir(scsiAttrPath) + if err != nil { + return err + } + scsiBlockPath := path.Join(SysfsScsiDevices, scsi, "/block") + scsidevList, err := ioutil.ReadDir(scsiBlockPath) + if err != nil { + return err + } + + if len(scsidevList) > 1 { + return AddressPointsToMultipleBlockDevices{} + } + + _scsi.Name = path.Join(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 NoAttributesFound{} + } + attrMap := getattrs(scsiAttrPath, scsiAttrList) + _scsi.Scsiattrmap = attrMap + _scsi.Diskattrmap, err = d.getDisk(scsidevList[0].Name()) + if err != nil { + return err + } + d.List = append(d.List, _scsi) + } + return nil +} diff --git a/pkg/os/scsi/scsi_linux_test.go b/pkg/os/scsi/scsi_linux_test.go new file mode 100644 index 00000000..b7c312df --- /dev/null +++ b/pkg/os/scsi/scsi_linux_test.go @@ -0,0 +1,40 @@ +/* + * Minimalist Object Storage, (C) 2015 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 ( + . "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) + + c.Assert(len(d.List[0].Scsiattrmap), Not(Equals), 0) + c.Assert(len(d.List[0].Diskattrmap), Not(Equals), 0) +} diff --git a/pkg/os/scsi/utils_linux.go b/pkg/os/scsi/utils_linux.go new file mode 100644 index 00000000..5b2383d1 --- /dev/null +++ b/pkg/os/scsi/utils_linux.go @@ -0,0 +1,48 @@ +/* + * Minimalist Object Storage, (C) 2015 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 +}