Skip to content

Commit 337987d

Browse files
committed
Consolidate logic to determine driver version
This change adds functions to the root.Driver type for returning (or determining) the driver version as well as the libcuda.so.VERSION path. This allows us to cleanup the logic around extracting the version which relies on: 1. libnvsandboxutils 2. libnvidia-ml 3. The suffix of the libcuda.so.VERSION file Signed-off-by: Evan Lezar <[email protected]>
1 parent 06d90b0 commit 337987d

File tree

11 files changed

+342
-215
lines changed

11 files changed

+342
-215
lines changed

internal/discover/compat_libs.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,14 @@ import (
44
"strings"
55

66
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
7-
"github.com/NVIDIA/nvidia-container-toolkit/internal/lookup/root"
87
)
98

109
// NewCUDACompatHookDiscoverer creates a discoverer for a enable-cuda-compat hook.
1110
// This hook is responsible for setting up CUDA compatibility in the container and depends on the host driver version.
12-
func NewCUDACompatHookDiscoverer(logger logger.Interface, hookCreator HookCreator, driver *root.Driver) Discover {
13-
_, cudaVersionPattern := getCUDALibRootAndVersionPattern(logger, driver)
11+
func NewCUDACompatHookDiscoverer(logger logger.Interface, hookCreator HookCreator, version string) Discover {
1412
var args []string
15-
if !strings.Contains(cudaVersionPattern, "*") {
16-
args = append(args, "--host-driver-version="+cudaVersionPattern)
13+
if version != "" && !strings.Contains(version, "*") {
14+
args = append(args, "--host-driver-version="+version)
1715
}
1816

1917
return hookCreator.Create("enable-cuda-compat", args...)

internal/discover/graphics.go

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
"github.com/NVIDIA/nvidia-container-toolkit/internal/info/proc"
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

@@ -51,7 +50,10 @@ func NewDRMNodesDiscoverer(logger logger.Interface, devices image.VisibleDevices
5150

5251
// NewGraphicsMountsDiscoverer creates a discoverer for the mounts required by graphics tools such as vulkan.
5352
func NewGraphicsMountsDiscoverer(logger logger.Interface, driver *root.Driver, hookCreator HookCreator) (Discover, error) {
54-
libraries := newGraphicsLibrariesDiscoverer(logger, driver, hookCreator)
53+
libraries, err := newGraphicsLibrariesDiscoverer(logger, driver, hookCreator)
54+
if err != nil {
55+
return nil, fmt.Errorf("failed to construct discoverer for graphics libraries: %w", err)
56+
}
5557

5658
configs := NewMounts(
5759
logger,
@@ -113,8 +115,15 @@ type graphicsDriverLibraries struct {
113115

114116
var _ Discover = (*graphicsDriverLibraries)(nil)
115117

116-
func newGraphicsLibrariesDiscoverer(logger logger.Interface, driver *root.Driver, hookCreator HookCreator) Discover {
117-
cudaLibRoot, cudaVersionPattern := getCUDALibRootAndVersionPattern(logger, driver)
118+
func newGraphicsLibrariesDiscoverer(logger logger.Interface, driver *root.Driver, hookCreator HookCreator) (Discover, error) {
119+
cudaVersionPattern, err := driver.Version()
120+
if err != nil {
121+
return nil, fmt.Errorf("failed to get driver version: %w", err)
122+
}
123+
cudaLibRoot, err := driver.GetLibcudaParentDir()
124+
if err != nil {
125+
return nil, fmt.Errorf("failed to get libcuda.so parent directory: %w", err)
126+
}
118127

119128
libraries := NewMounts(
120129
logger,
@@ -155,7 +164,7 @@ func newGraphicsLibrariesDiscoverer(logger logger.Interface, driver *root.Driver
155164
Discover: Merge(libraries, xorgLibraries),
156165
logger: logger,
157166
hookCreator: hookCreator,
158-
}
167+
}, nil
159168
}
160169

161170
// Mounts discovers the required libraries and filters out libnvidia-allocator.so.
@@ -228,30 +237,6 @@ func (d graphicsDriverLibraries) isDriverLibrary(filename string, libraryName st
228237
return match
229238
}
230239

231-
// getCUDALibRootAndVersionPattern returns the parent directory and the version
232-
// suffix of the libcuda.so.*.* library at the driver root.
233-
// If the library cannot be located an empty root is returned.
234-
// If the version string cannot be extracted, the generic *.* pattern is returned.
235-
func getCUDALibRootAndVersionPattern(logger logger.Interface, driver *root.Driver) (string, string) {
236-
libCudaPaths, err := cuda.New(
237-
driver.Libraries(),
238-
).Locate(".*.*")
239-
if err != nil {
240-
logger.Warningf("failed to locate libcuda.so: %v; using *.*", err)
241-
return "", "*.*"
242-
}
243-
libcudaPath := libCudaPaths[0]
244-
245-
libRoot := filepath.Dir(libcudaPath)
246-
version := strings.TrimPrefix(filepath.Base(libcudaPath), "libcuda.so.")
247-
if version == "" {
248-
logger.Warningf("failed to extract version from %v; using *.*", libcudaPath)
249-
version = "*.*"
250-
}
251-
252-
return driver.RelativeToRoot(libRoot), version
253-
}
254-
255240
// buildXOrgSearchPaths returns the ordered list of search paths for XOrg files.
256241
func buildXOrgSearchPaths(libRoot string) []string {
257242
var paths []string

internal/lookup/cuda/cuda.go

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

internal/lookup/cuda/cuda_test.go renamed to internal/lookup/root/cuda_test.go

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# limitations under the License.
1515
**/
1616

17-
package cuda
17+
package root
1818

1919
import (
2020
"fmt"
@@ -35,19 +35,19 @@ func TestLocate(t *testing.T) {
3535
testCases := []struct {
3636
description string
3737
libcudaPath string
38-
expected []string
38+
expected string
3939
expectedError error
4040
}{
4141
{
4242
description: "no libcuda does not resolve library",
4343
libcudaPath: "",
44-
expected: []string{},
44+
expected: "",
4545
expectedError: lookup.ErrNotFound,
4646
},
4747
{
4848
description: "no-ldcache searches /usr/lib64",
4949
libcudaPath: "/usr/lib64/libcuda.so.123.34",
50-
expected: []string{"/usr/lib64/libcuda.so.123.34"},
50+
expected: "/usr/lib64/libcuda.so.123.34",
5151
expectedError: nil,
5252
},
5353
}
@@ -58,26 +58,20 @@ func TestLocate(t *testing.T) {
5858
require.NoError(t, err)
5959

6060
l := New(
61-
lookup.NewLibraryLocator(
62-
lookup.WithLogger(logger),
63-
lookup.WithRoot(driverRoot),
64-
),
61+
WithLogger(logger),
62+
WithDriverRoot(driverRoot),
6563
)
6664

67-
candidates, err := l.Locate(".*")
65+
libcudasoPath, err := l.GetLibcudasoPath()
6866
require.ErrorIs(t, err, tc.expectedError)
6967

70-
var strippedCandidates []string
71-
for _, c := range candidates {
72-
// NOTE: We need to strip `/private` on MacOs due to symlink resolution
73-
strippedCandidates = append(strippedCandidates, strings.TrimPrefix(c, "/private"))
74-
}
75-
var expectedWithRoot []string
76-
for _, e := range tc.expected {
77-
expectedWithRoot = append(expectedWithRoot, filepath.Join(driverRoot, e))
68+
// NOTE: We need to strip `/private` on MacOs due to symlink resolution
69+
stripped := strings.TrimPrefix(libcudasoPath, "/private")
70+
if tc.expected != "" {
71+
tc.expected = filepath.Join(driverRoot, tc.expected)
7872
}
7973

80-
require.EqualValues(t, expectedWithRoot, strippedCandidates)
74+
require.Equal(t, tc.expected, stripped)
8175
})
8276
}
8377
}

internal/lookup/root/options.go

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,45 @@ package root
1818

1919
import "github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
2020

21-
type Option func(*Driver)
21+
type options struct {
22+
logger logger.Interface
23+
// Root represents the root from the perspective of the driver libraries and binaries.
24+
Root string
25+
// librarySearchPaths specifies explicit search paths for discovering libraries.
26+
librarySearchPaths []string
27+
// configSearchPaths specified explicit search paths for discovering driver config files.
28+
configSearchPaths []string
29+
versioner Versioner
30+
}
31+
32+
type Option func(*options)
2233

2334
func WithLogger(logger logger.Interface) Option {
24-
return func(d *Driver) {
25-
d.logger = logger
35+
return func(o *options) {
36+
o.logger = logger
2637
}
2738
}
2839

2940
func WithDriverRoot(root string) Option {
30-
return func(d *Driver) {
31-
d.Root = root
41+
return func(o *options) {
42+
o.Root = root
3243
}
3344
}
3445

3546
func WithLibrarySearchPaths(paths ...string) Option {
36-
return func(d *Driver) {
37-
d.librarySearchPaths = paths
47+
return func(o *options) {
48+
o.librarySearchPaths = paths
3849
}
3950
}
4051

4152
func WithConfigSearchPaths(paths ...string) Option {
42-
return func(d *Driver) {
43-
d.configSearchPaths = paths
53+
return func(o *options) {
54+
o.configSearchPaths = paths
55+
}
56+
}
57+
58+
func WithVersioner(versioner Versioner) Option {
59+
return func(o *options) {
60+
o.versioner = versioner
4461
}
4562
}

0 commit comments

Comments
 (0)