github.com/openebs/node-disk-manager@v1.9.1-0.20230225014141-4531f06ffa1e/pkg/sysfs/device.go (about) 1 /* 2 Copyright 2020 The OpenEBS Authors 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package sysfs 18 19 import ( 20 "fmt" 21 "strings" 22 ) 23 24 // Device represents a blockdevice using its sysfs path. 25 type Device struct { 26 // deviceName is the name of the device node sda, sdb, dm-0 etc 27 deviceName string 28 29 // Path of the blockdevice. eg: /dev/sda, /dev/dm-0 30 path string 31 32 // SysPath of the blockdevice. eg: /sys/devices/pci0000:00/0000:00:1f.2/ata1/host0/target0:0:0/0:0:0:0/block/sda/ 33 sysPath string 34 } 35 36 // NewSysFsDeviceFromDevPath is used to get sysfs device struct from the device devpath 37 // The sysfs device struct contains the device name along with the syspath 38 func NewSysFsDeviceFromDevPath(devPath string) (*Device, error) { 39 devName := strings.Replace(devPath, "/dev/", "", 1) 40 if len(devName) == 0 { 41 return nil, fmt.Errorf("unable to create sysfs device from devPath for device: %s, error: device name empty", devPath) 42 } 43 44 sysPath, err := getDeviceSysPath(devPath) 45 if err != nil { 46 return nil, fmt.Errorf("unable to create sysfs device from devpath for device: %s, error: %v", devPath, err) 47 } 48 49 dev := &Device{ 50 deviceName: devName, 51 path: devPath, 52 sysPath: sysPath, 53 } 54 return dev, nil 55 }