Skip to content

Commit 2d5f82d

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 104e46e commit 2d5f82d

File tree

6 files changed

+56
-91
lines changed

6 files changed

+56
-91
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 & 55 deletions
This file was deleted.

internal/lookup/root/root.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717
package root
1818

1919
import (
20+
"fmt"
2021
"os"
2122
"path/filepath"
2223
"strings"
2324

2425
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
2526
"github.com/NVIDIA/nvidia-container-toolkit/internal/lookup"
27+
"github.com/NVIDIA/nvidia-container-toolkit/internal/lookup/symlinks"
2628
)
2729

2830
// Driver represents a filesystem in which a set of drivers or devices is defined.
@@ -79,6 +81,34 @@ func (r *Driver) Libraries() lookup.Locator {
7981
)
8082
}
8183

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

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

Lines changed: 16 additions & 19 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+
libCudaPath, err := l.LibCudaPath()
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))
78-
}
68+
libCudaPath = strings.TrimPrefix(libCudaPath, "/private")
7969

80-
require.EqualValues(t, expectedWithRoot, strippedCandidates)
70+
if tc.expected == "" {
71+
require.EqualValues(t, "", libCudaPath)
72+
} else {
73+
require.EqualValues(t, filepath.Join(driverRoot, tc.expected), libCudaPath)
74+
}
8175
})
8276
}
8377
}
@@ -101,5 +95,8 @@ func setupDriverRoot(t *testing.T, libCudaPath string) (string, error) {
10195
}
10296
defer libCuda.Close()
10397

98+
if err := os.Symlink(filepath.Base(libCudaPath), filepath.Join(driverRoot, filepath.Dir(libCudaPath), "libcuda.so.1")); err != nil {
99+
return "", err
100+
}
104101
return driverRoot, nil
105102
}

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)