mirror of
https://github.com/clearml/go-nvlib
synced 2025-05-13 16:20:46 +00:00
Use 'os' instead of 'ioutil' which is recommended starting with Go 1.16
Signed-off-by: Christopher Desiniotis <cdesiniotis@nvidia.com>
This commit is contained in:
parent
dc53500d0e
commit
e2d858daed
@ -20,7 +20,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"gitlab.com/nvidia/cloud-native/go-nvlib/pkg/nvpci"
|
"gitlab.com/nvidia/cloud-native/go-nvlib/pkg/nvpci"
|
||||||
"gitlab.com/nvidia/cloud-native/go-nvlib/pkg/nvpci/bytes"
|
"gitlab.com/nvidia/cloud-native/go-nvlib/pkg/nvpci/bytes"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
)
|
)
|
||||||
@ -34,7 +33,7 @@ var _ Interface = (*MockNvmdev)(nil)
|
|||||||
|
|
||||||
// NewMock creates new mock mediated (vGPU) and parent PCI devices and removes old devices
|
// NewMock creates new mock mediated (vGPU) and parent PCI devices and removes old devices
|
||||||
func NewMock() (mock *MockNvmdev, rerr error) {
|
func NewMock() (mock *MockNvmdev, rerr error) {
|
||||||
mdevParentsRootDir, err := ioutil.TempDir("", "")
|
mdevParentsRootDir, err := os.MkdirTemp(os.TempDir(), "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -43,7 +42,7 @@ func NewMock() (mock *MockNvmdev, rerr error) {
|
|||||||
os.RemoveAll(mdevParentsRootDir)
|
os.RemoveAll(mdevParentsRootDir)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
mdevDevicesRootDir, err := ioutil.TempDir("", "")
|
mdevDevicesRootDir, err := os.MkdirTemp(os.TempDir(), "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,6 @@ package nvmdev
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"gitlab.com/nvidia/cloud-native/go-nvlib/pkg/nvpci"
|
"gitlab.com/nvidia/cloud-native/go-nvlib/pkg/nvpci"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@ -67,7 +66,7 @@ func New() Interface {
|
|||||||
|
|
||||||
// GetAllParentDevices returns all NVIDIA Parent PCI devices on the system
|
// GetAllParentDevices returns all NVIDIA Parent PCI devices on the system
|
||||||
func (m *nvmdev) GetAllParentDevices() ([]*ParentDevice, error) {
|
func (m *nvmdev) GetAllParentDevices() ([]*ParentDevice, error) {
|
||||||
deviceDirs, err := ioutil.ReadDir(m.mdevParentsRoot)
|
deviceDirs, err := os.ReadDir(m.mdevParentsRoot)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unable to read PCI bus devices: %v", err)
|
return nil, fmt.Errorf("unable to read PCI bus devices: %v", err)
|
||||||
}
|
}
|
||||||
@ -101,7 +100,7 @@ func (m *nvmdev) GetAllParentDevices() ([]*ParentDevice, error) {
|
|||||||
|
|
||||||
// GetAllDevices returns all NVIDIA mdev (vGPU) devices on the system
|
// GetAllDevices returns all NVIDIA mdev (vGPU) devices on the system
|
||||||
func (m *nvmdev) GetAllDevices() ([]*Device, error) {
|
func (m *nvmdev) GetAllDevices() ([]*Device, error) {
|
||||||
deviceDirs, err := ioutil.ReadDir(m.mdevDevicesRoot)
|
deviceDirs, err := os.ReadDir(m.mdevDevicesRoot)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unable to read MDEV devices directory: %v", err)
|
return nil, fmt.Errorf("unable to read MDEV devices directory: %v", err)
|
||||||
}
|
}
|
||||||
@ -174,7 +173,7 @@ func (m mdev) parentDevicePath() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m mdev) Type() (string, error) {
|
func (m mdev) Type() (string, error) {
|
||||||
mdevType, err := ioutil.ReadFile(path.Join(string(m), "name"))
|
mdevType, err := os.ReadFile(path.Join(string(m), "name"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("unable to read mdev_type name for mdev %s: %v", m, err)
|
return "", fmt.Errorf("unable to read mdev_type name for mdev %s: %v", m, err)
|
||||||
}
|
}
|
||||||
@ -205,7 +204,7 @@ func NewParentDevice(devicePath string) (*ParentDevice, error) {
|
|||||||
}
|
}
|
||||||
mdevTypesMap := make(map[string]string)
|
mdevTypesMap := make(map[string]string)
|
||||||
for _, path := range paths {
|
for _, path := range paths {
|
||||||
name, err := ioutil.ReadFile(path)
|
name, err := os.ReadFile(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unable to read file %s: %v", path, err)
|
return nil, fmt.Errorf("unable to read file %s: %v", path, err)
|
||||||
}
|
}
|
||||||
@ -292,7 +291,7 @@ func (p *ParentDevice) GetAvailableMDEVInstances(mdevType string) (int, error) {
|
|||||||
return -1, nil
|
return -1, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
available, err := ioutil.ReadFile(filepath.Join(mdevPath, "available_instances"))
|
available, err := os.ReadFile(filepath.Join(mdevPath, "available_instances"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return -1, fmt.Errorf("unable to read available_instances file: %v", err)
|
return -1, fmt.Errorf("unable to read available_instances file: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -18,7 +18,7 @@ package nvpci
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"os"
|
||||||
|
|
||||||
"gitlab.com/nvidia/cloud-native/go-nvlib/pkg/nvpci/bytes"
|
"gitlab.com/nvidia/cloud-native/go-nvlib/pkg/nvpci/bytes"
|
||||||
)
|
)
|
||||||
@ -71,7 +71,7 @@ type PCICapabilities struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (cs *ConfigSpace) Read() (ConfigSpaceIO, error) {
|
func (cs *ConfigSpace) Read() (ConfigSpaceIO, error) {
|
||||||
config, err := ioutil.ReadFile(cs.Path)
|
config, err := os.ReadFile(cs.Path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to open file: %v", err)
|
return nil, fmt.Errorf("failed to open file: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -18,7 +18,6 @@ package nvpci
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
@ -34,7 +33,7 @@ var _ Interface = (*MockNvpci)(nil)
|
|||||||
|
|
||||||
// NewMockNvpci create new mock PCI and remove old devices
|
// NewMockNvpci create new mock PCI and remove old devices
|
||||||
func NewMockNvpci() (mock *MockNvpci, rerr error) {
|
func NewMockNvpci() (mock *MockNvpci, rerr error) {
|
||||||
rootDir, err := ioutil.TempDir("", "")
|
rootDir, err := os.MkdirTemp(os.TempDir(), "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -18,7 +18,6 @@ package nvpci
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"sort"
|
"sort"
|
||||||
@ -104,7 +103,7 @@ func (d *NvidiaPCIDevice) IsResetAvailable() bool {
|
|||||||
|
|
||||||
// Reset perform a reset to apply a new configuration at HW level
|
// Reset perform a reset to apply a new configuration at HW level
|
||||||
func (d *NvidiaPCIDevice) Reset() error {
|
func (d *NvidiaPCIDevice) Reset() error {
|
||||||
err := ioutil.WriteFile(path.Join(d.Path, "reset"), []byte("1"), 0)
|
err := os.WriteFile(path.Join(d.Path, "reset"), []byte("1"), 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to write to reset file: %v", err)
|
return fmt.Errorf("unable to write to reset file: %v", err)
|
||||||
}
|
}
|
||||||
@ -123,7 +122,7 @@ func NewFrom(root string) Interface {
|
|||||||
|
|
||||||
// GetAllDevices returns all Nvidia PCI devices on the system
|
// GetAllDevices returns all Nvidia PCI devices on the system
|
||||||
func (p *nvpci) GetAllDevices() ([]*NvidiaPCIDevice, error) {
|
func (p *nvpci) GetAllDevices() ([]*NvidiaPCIDevice, error) {
|
||||||
deviceDirs, err := ioutil.ReadDir(p.pciDevicesRoot)
|
deviceDirs, err := os.ReadDir(p.pciDevicesRoot)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unable to read PCI bus devices: %v", err)
|
return nil, fmt.Errorf("unable to read PCI bus devices: %v", err)
|
||||||
}
|
}
|
||||||
@ -159,7 +158,7 @@ func (p *nvpci) GetAllDevices() ([]*NvidiaPCIDevice, error) {
|
|||||||
func NewDevice(devicePath string) (*NvidiaPCIDevice, error) {
|
func NewDevice(devicePath string) (*NvidiaPCIDevice, error) {
|
||||||
address := path.Base(devicePath)
|
address := path.Base(devicePath)
|
||||||
|
|
||||||
vendor, err := ioutil.ReadFile(path.Join(devicePath, "vendor"))
|
vendor, err := os.ReadFile(path.Join(devicePath, "vendor"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unable to read PCI device vendor id for %s: %v", address, err)
|
return nil, fmt.Errorf("unable to read PCI device vendor id for %s: %v", address, err)
|
||||||
}
|
}
|
||||||
@ -173,7 +172,7 @@ func NewDevice(devicePath string) (*NvidiaPCIDevice, error) {
|
|||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
class, err := ioutil.ReadFile(path.Join(devicePath, "class"))
|
class, err := os.ReadFile(path.Join(devicePath, "class"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unable to read PCI device class for %s: %v", address, err)
|
return nil, fmt.Errorf("unable to read PCI device class for %s: %v", address, err)
|
||||||
}
|
}
|
||||||
@ -183,7 +182,7 @@ func NewDevice(devicePath string) (*NvidiaPCIDevice, error) {
|
|||||||
return nil, fmt.Errorf("unable to convert class string to uint32: %v", classStr)
|
return nil, fmt.Errorf("unable to convert class string to uint32: %v", classStr)
|
||||||
}
|
}
|
||||||
|
|
||||||
device, err := ioutil.ReadFile(path.Join(devicePath, "device"))
|
device, err := os.ReadFile(path.Join(devicePath, "device"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unable to read PCI device id for %s: %v", address, err)
|
return nil, fmt.Errorf("unable to read PCI device id for %s: %v", address, err)
|
||||||
}
|
}
|
||||||
@ -193,7 +192,7 @@ func NewDevice(devicePath string) (*NvidiaPCIDevice, error) {
|
|||||||
return nil, fmt.Errorf("unable to convert device string to uint16: %v", deviceStr)
|
return nil, fmt.Errorf("unable to convert device string to uint16: %v", deviceStr)
|
||||||
}
|
}
|
||||||
|
|
||||||
numa, err := ioutil.ReadFile(path.Join(devicePath, "numa_node"))
|
numa, err := os.ReadFile(path.Join(devicePath, "numa_node"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unable to read PCI NUMA node for %s: %v", address, err)
|
return nil, fmt.Errorf("unable to read PCI NUMA node for %s: %v", address, err)
|
||||||
}
|
}
|
||||||
@ -207,7 +206,7 @@ func NewDevice(devicePath string) (*NvidiaPCIDevice, error) {
|
|||||||
Path: path.Join(devicePath, "config"),
|
Path: path.Join(devicePath, "config"),
|
||||||
}
|
}
|
||||||
|
|
||||||
resource, err := ioutil.ReadFile(path.Join(devicePath, "resource"))
|
resource, err := os.ReadFile(path.Join(devicePath, "resource"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unable to read PCI resource file for %s: %v", address, err)
|
return nil, fmt.Errorf("unable to read PCI resource file for %s: %v", address, err)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user