Skip to content

Commit 068a888

Browse files
committed
[no-relnote] Add LibCudaPath to Driver type
This change moves the logic to locate libcuda.so files to the Driver type. Signed-off-by: Evan Lezar <[email protected]>
1 parent 15d5a7f commit 068a888

File tree

6 files changed

+41
-161
lines changed

6 files changed

+41
-161
lines changed

internal/discover/graphics.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"github.com/NVIDIA/nvidia-container-toolkit/internal/info/proc"
2828
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
2929
"github.com/NVIDIA/nvidia-container-toolkit/internal/lookup"
30-
"github.com/NVIDIA/nvidia-container-toolkit/internal/lookup/cuda"
3130
"github.com/NVIDIA/nvidia-container-toolkit/internal/lookup/root"
3231
)
3332

@@ -221,14 +220,11 @@ func (d graphicsDriverLibraries) isDriverLibrary(filename string, libraryName st
221220
// If the library cannot be located an empty root is returned.
222221
// If the version string cannot be extracted, the generic *.* pattern is returned.
223222
func getCUDALibRootAndVersionPattern(logger logger.Interface, driver *root.Driver) (string, string) {
224-
libCudaPaths, err := cuda.New(
225-
driver.Libraries(),
226-
).Locate(".*.*")
223+
libcudaPath, err := driver.LibCudaPath()
227224
if err != nil {
228225
logger.Warningf("failed to locate libcuda.so: %v; using *.*", err)
229226
return "", "*.*"
230227
}
231-
libcudaPath := libCudaPaths[0]
232228

233229
libRoot := filepath.Dir(libcudaPath)
234230
version := strings.TrimPrefix(filepath.Base(libcudaPath), "libcuda.so.")

internal/lookup/cuda/cuda.go

Lines changed: 0 additions & 39 deletions
This file was deleted.

internal/lookup/cuda/cuda_test.go

Lines changed: 0 additions & 105 deletions
This file was deleted.

internal/lookup/root/root.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package root
1818

1919
import (
20+
"fmt"
2021
"os"
2122
"path/filepath"
2223
"strings"
@@ -79,6 +80,36 @@ func (r *Driver) Libraries() lookup.Locator {
7980
)
8081
}
8182

83+
// LibCudaPath returns the path to libcuda.so.RM_VERSION in the driver installation.
84+
func (r *Driver) LibCudaPath() (string, error) {
85+
candidates, err := r.Libraries().Locate("libcuda.so.1")
86+
if err != nil {
87+
return "", err
88+
}
89+
90+
pattern := ".*.*"
91+
var matched []string
92+
for _, candidate := range candidates {
93+
// It is not guaranteed that candidate is a resolved symlink.
94+
resolved, err := filepath.EvalSymlinks(candidate)
95+
if err != nil {
96+
r.logger.Warningf("ignoring error resolving symlink %v: %v", candidate, err)
97+
continue
98+
}
99+
matches, _ := filepath.Match("libcuda.so"+pattern, filepath.Base(resolved))
100+
if !matches {
101+
r.logger.Debugf("skipping candidate %v", resolved)
102+
continue
103+
}
104+
matched = append(matched, candidate)
105+
}
106+
107+
if len(matched) == 0 {
108+
return "", fmt.Errorf("%v %w", "libcuda.so"+pattern, lookup.ErrNotFound)
109+
}
110+
return matched[0], nil
111+
}
112+
82113
// Configs returns a locator for driver configs.
83114
// If configSearchPaths is specified, these paths are used as absolute paths,
84115
// otherwise, /etc and /usr/share are searched.

pkg/nvcdi/driver-nvml.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
"github.com/NVIDIA/nvidia-container-toolkit/internal/discover"
2929
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
3030
"github.com/NVIDIA/nvidia-container-toolkit/internal/lookup"
31-
"github.com/NVIDIA/nvidia-container-toolkit/internal/lookup/cuda"
3231
"github.com/NVIDIA/nvidia-container-toolkit/internal/lookup/root"
3332
)
3433

@@ -194,13 +193,16 @@ func NewDriverBinariesDiscoverer(logger logger.Interface, driverRoot string) dis
194193
func getVersionLibs(logger logger.Interface, driver *root.Driver, version string) ([]string, error) {
195194
logger.Infof("Using driver version %v", version)
196195

197-
libCudaPaths, err := cuda.New(
198-
driver.Libraries(),
199-
).Locate("." + version)
196+
libCudaPath, err := driver.LibCudaPath()
200197
if err != nil {
201-
return nil, fmt.Errorf("failed to locate libcuda.so.%v: %v", version, err)
198+
return nil, fmt.Errorf("failed to locate libcuda.so: %w", err)
202199
}
203-
libRoot := filepath.Dir(libCudaPaths[0])
200+
matched, err := filepath.Match("libcuda.so."+version, libCudaPath)
201+
if err != nil || !matched {
202+
return nil, fmt.Errorf("failed to locate libcuda.so.%v: %w", version, err)
203+
}
204+
205+
libRoot := filepath.Dir(libCudaPath)
204206

205207
libraries := lookup.NewFileLocator(
206208
lookup.WithLogger(logger),

pkg/nvcdi/management.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727

2828
"github.com/NVIDIA/nvidia-container-toolkit/internal/discover"
2929
"github.com/NVIDIA/nvidia-container-toolkit/internal/edits"
30-
"github.com/NVIDIA/nvidia-container-toolkit/internal/lookup/cuda"
3130
"github.com/NVIDIA/nvidia-container-toolkit/internal/nvsandboxutils"
3231
"github.com/NVIDIA/nvidia-container-toolkit/pkg/nvcdi/spec"
3332
)
@@ -100,15 +99,11 @@ func (m *managementlib) getCudaVersion() (string, error) {
10099
return version, nil
101100
}
102101

103-
libCudaPaths, err := cuda.New(
104-
m.driver.Libraries(),
105-
).Locate(".*.*")
102+
libCudaPath, err := m.driver.LibCudaPath()
106103
if err != nil {
107104
return "", fmt.Errorf("failed to locate libcuda.so: %v", err)
108105
}
109106

110-
libCudaPath := libCudaPaths[0]
111-
112107
version = strings.TrimPrefix(filepath.Base(libCudaPath), "libcuda.so.")
113108

114109
return version, nil

0 commit comments

Comments
 (0)