From a9dc6550d548cd75be97d51eea44bbd3666b238d Mon Sep 17 00:00:00 2001 From: Evan Lezar Date: Mon, 8 Aug 2022 17:11:42 +0200 Subject: [PATCH] Use nvinfo package from go-nvlib Signed-off-by: Evan Lezar --- internal/info/auto.go | 6 ++-- internal/info/info.go | 65 -------------------------------------- internal/modifier/tegra.go | 4 +-- 3 files changed, 6 insertions(+), 69 deletions(-) delete mode 100644 internal/info/info.go diff --git a/internal/info/auto.go b/internal/info/auto.go index 89d2bbb6..c70265f8 100644 --- a/internal/info/auto.go +++ b/internal/info/auto.go @@ -16,6 +16,8 @@ package info +import "gitlab.com/nvidia/cloud-native/go-nvlib/pkg/nvinfo" + // Logger is a basic interface for logging to allow these functions to be called // from code where logrus is not used. type Logger interface { @@ -32,10 +34,10 @@ func ResolveAutoMode(logger Logger, mode string) (rmode string) { logger.Infof("Auto-detected mode as '%v'", rmode) }() - isTegra, reason := IsTegraSystem() + isTegra, reason := nvinfo.IsTegraSystem() logger.Debugf("Is Tegra-based system? %v: %v", isTegra, reason) - hasNVML, reason := HasNVML() + hasNVML, reason := nvinfo.HasNVML() logger.Debugf("Has NVML? %v: %v", hasNVML, reason) if isTegra && !hasNVML { diff --git a/internal/info/info.go b/internal/info/info.go deleted file mode 100644 index 00050916..00000000 --- a/internal/info/info.go +++ /dev/null @@ -1,65 +0,0 @@ -/** -# Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. -# -# 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 info - -import ( - "fmt" - "os" - "strings" - - "github.com/NVIDIA/go-nvml/pkg/dl" -) - -// HasNVML returns true if NVML is detected on the sytems -func HasNVML() (bool, string) { - const ( - nvmlLibraryName = "libnvidia-ml.so.1" - nvmlLibraryLoadFlags = dl.RTLD_LAZY - ) - lib := dl.New(nvmlLibraryName, nvmlLibraryLoadFlags) - if err := lib.Open(); err != nil { - return false, fmt.Sprintf("could not load NVML: %v", err) - } - defer lib.Close() - - return true, "found NVML library" -} - -// IsTegraSystem returns true if the system is detected as a Tegra-based system -func IsTegraSystem() (bool, string) { - const tegraReleaseFile = "/etc/nv_tegra_release" - const tegraFamilyFile = "/sys/devices/soc0/family" - - if info, err := os.Stat(tegraReleaseFile); err == nil && !info.IsDir() { - return true, fmt.Sprintf("%v found", tegraReleaseFile) - } - - if info, err := os.Stat(tegraFamilyFile); err != nil || info.IsDir() { - return false, fmt.Sprintf("%v file not found", tegraFamilyFile) - } - - contents, err := os.ReadFile(tegraFamilyFile) - if err != nil { - return false, fmt.Sprintf("could not read %v", tegraFamilyFile) - } - - if strings.HasPrefix(strings.ToLower(string(contents)), "tegra") { - return true, fmt.Sprintf("%v has 'tegra' prefix", tegraFamilyFile) - } - - return false, fmt.Sprintf("%v has no 'tegra' prefix", tegraFamilyFile) -} diff --git a/internal/modifier/tegra.go b/internal/modifier/tegra.go index 547f6216..459d1f0d 100644 --- a/internal/modifier/tegra.go +++ b/internal/modifier/tegra.go @@ -18,15 +18,15 @@ package modifier import ( "github.com/NVIDIA/nvidia-container-toolkit/internal/discover" - "github.com/NVIDIA/nvidia-container-toolkit/internal/info" "github.com/NVIDIA/nvidia-container-toolkit/internal/lookup" "github.com/NVIDIA/nvidia-container-toolkit/internal/oci" "github.com/sirupsen/logrus" + "gitlab.com/nvidia/cloud-native/go-nvlib/pkg/nvinfo" ) // NewTegraPlatformFiles creates a modifier to inject the Tegra platform files into a container. func NewTegraPlatformFiles(logger *logrus.Logger) (oci.SpecModifier, error) { - isTegra, _ := info.IsTegraSystem() + isTegra, _ := nvinfo.IsTegraSystem() if !isTegra { return nil, nil }