Rework libraries and add tests

Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
Evan Lezar 2024-06-12 17:36:23 +02:00
parent f26425d3fd
commit b00ef73dae
2 changed files with 213 additions and 41 deletions

View File

@ -50,17 +50,7 @@ func NewDRMNodesDiscoverer(logger logger.Interface, devices image.VisibleDevices
// NewGraphicsMountsDiscoverer creates a discoverer for the mounts required by graphics tools such as vulkan.
func NewGraphicsMountsDiscoverer(logger logger.Interface, driver *root.Driver, nvidiaCDIHookPath string) (Discover, error) {
libraries := NewMounts(
logger,
driver.Libraries(),
driver.Root,
[]string{
"libnvidia-egl-gbm.so.*",
"libnvidia-egl-wayland.so.*",
"libnvidia-allocator.so.*",
"libnvidia-vulkan-producer.so.*",
},
)
libraries := newGraphicsLibrariesDiscoverer(logger, driver, nvidiaCDIHookPath)
jsonMounts := NewMounts(
logger,
@ -77,66 +67,78 @@ func NewGraphicsMountsDiscoverer(logger logger.Interface, driver *root.Driver, n
},
)
symlinks := newGraphicsDriverSymlinks(logger, libraries, nvidiaCDIHookPath)
xorg := optionalXorgDiscoverer(logger, driver, nvidiaCDIHookPath)
discover := Merge(
libraries,
jsonMounts,
symlinks,
xorg,
)
return discover, nil
}
type graphicsDriverSymlinks struct {
None
type graphicsDriverLibraries struct {
Discover
logger logger.Interface
libraries Discover
nvidiaCDIHookPath string
}
var _ Discover = (*graphicsDriverSymlinks)(nil)
var _ Discover = (*graphicsDriverLibraries)(nil)
func newGraphicsDriverSymlinks(logger logger.Interface, libraries Discover, nvidiaCDIHookPath string) Discover {
return &graphicsDriverSymlinks{
func newGraphicsLibrariesDiscoverer(logger logger.Interface, driver *root.Driver, nvidiaCDIHookPath string) Discover {
libraries := NewMounts(
logger,
driver.Libraries(),
driver.Root,
[]string{
"libnvidia-egl-gbm.so.*",
"libnvidia-egl-wayland.so.*",
// We include the following libraries to have them available for
// symlink creation below:
// If CDI injection is used, these should already be detected as:
// * libnvidia-allocator.so.RM_VERSION
// * libnvidia-vulkan-producer.so.RM_VERSION
// but need to be handled for the legacy case too.
"libnvidia-allocator.so.*",
"libnvidia-vulkan-producer.so.*",
},
)
return &graphicsDriverLibraries{
Discover: libraries,
logger: logger,
libraries: libraries,
nvidiaCDIHookPath: nvidiaCDIHookPath,
}
}
// Create necessary library symlinks for graphics drivers
func (d graphicsDriverSymlinks) Hooks() ([]Hook, error) {
mounts, err := d.libraries.Mounts()
func (d graphicsDriverLibraries) Hooks() ([]Hook, error) {
mounts, err := d.Discover.Mounts()
if err != nil {
return nil, fmt.Errorf("failed to get library mounts: %v", err)
}
links := []string{}
var links []string
for _, mount := range mounts {
filename := filepath.Base(mount.HostPath)
// nvidia-drm_gbm.so is a symlink to libnvidia-allocator.so
// Make sure this is actually available
if strings.HasPrefix(filename, "libnvidia-allocator.so.") {
linkDir := filepath.Dir(mount.Path)
linkPath := filepath.Join(linkDir, "gbm", "nvidia-drm_gbm.so")
target := filepath.Join("..", filename)
links = append(links, fmt.Sprintf("%s::%s", target, linkPath))
}
// Address the vulkan-producer lib for nvidia drivers prior driver version 545
if strings.HasPrefix(filename, "libnvidia-vulkan-producer.so.") {
linkDir := filepath.Dir(mount.Path)
linkPath := filepath.Join(linkDir, "libnvidia-vulkan-producer.so")
dir, filename := filepath.Split(mount.Path)
switch {
case strings.HasPrefix(filename, "libnvidia-allocator.so."):
// gbm/nvidia-drm_gbm.so is a symlink to ../libnvidia-allocator.so.1 which
// in turn symlinks to libnvidia-allocator.so.RM_VERSION and is created
// when ldconfig is run in the container.
// create libnvidia-allocate.so.1 -> libnvidia-allocate.so.RM_VERSION symlink
links = append(links, fmt.Sprintf("%s::%s", filename, filepath.Join(dir, "libnvidia-allocator.so.1")))
// create gbm/nvidia-drm_gbm.so -> ../libnvidia-allocate.so.1 symlink
linkPath := filepath.Join(dir, "gbm", "nvidia-drm_gbm.so")
links = append(links, fmt.Sprintf("%s::%s", "../libnvidia-allocator.so.1", linkPath))
case strings.HasPrefix(filename, "libnvidia-vulkan-producer.so."):
// libnvidia-vulkan-producer.so is a drirect symlink to libnvidia-vulkan-producer.so.RM_VERSION
// create libnvidia-vulkan-producer.so -> libnvidia-vulkan-producer.so.RM_VERSION symlink
linkPath := filepath.Join(dir, "libnvidia-vulkan-producer.so")
links = append(links, fmt.Sprintf("%s::%s", filename, linkPath))
}
}
if len(links) == 0 {
return nil, nil
}

View File

@ -0,0 +1,170 @@
/**
# Copyright 2024 NVIDIA CORPORATION
#
# 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 discover
import (
"testing"
testlog "github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/require"
)
func TestGraphicsLibrariesDiscoverer(t *testing.T) {
logger, _ := testlog.NewNullLogger()
testCases := []struct {
description string
libraries *DiscoverMock
expectedMounts []Mount
expectedHooks []Hook
}{
{
description: "none discovered",
libraries: &DiscoverMock{
MountsFunc: func() ([]Mount, error) {
mounts := []Mount{
{
Path: "/usr/lib64/libnvidia-egl-gbm.so.123.45.67",
},
}
return mounts, nil
},
},
expectedMounts: []Mount{
{
Path: "/usr/lib64/libnvidia-egl-gbm.so.123.45.67",
},
},
},
{
description: "libnvidia-allocator discovered",
libraries: &DiscoverMock{
MountsFunc: func() ([]Mount, error) {
mounts := []Mount{
{
Path: "/usr/lib64/libnvidia-allocator.so.123.45.67",
},
}
return mounts, nil
},
},
expectedMounts: []Mount{
{
Path: "/usr/lib64/libnvidia-allocator.so.123.45.67",
},
},
expectedHooks: []Hook{
{
Lifecycle: "createContainer",
Path: "/usr/bin/nvidia-cdi-hook",
Args: []string{"nvidia-cdi-hook", "create-symlinks",
"--link", "libnvidia-allocator.so.123.45.67::/usr/lib64/libnvidia-allocator.so.1",
"--link", "../libnvidia-allocator.so.1::/usr/lib64/gbm/nvidia-drm_gbm.so",
},
},
},
},
{
description: "libnvidia-vulkan-producer discovered",
libraries: &DiscoverMock{
MountsFunc: func() ([]Mount, error) {
mounts := []Mount{
{
Path: "/usr/lib64/libnvidia-vulkan-producer.so.123.45.67",
},
}
return mounts, nil
},
},
expectedMounts: []Mount{
{
Path: "/usr/lib64/libnvidia-vulkan-producer.so.123.45.67",
},
},
expectedHooks: []Hook{
{
Lifecycle: "createContainer",
Path: "/usr/bin/nvidia-cdi-hook",
Args: []string{"nvidia-cdi-hook", "create-symlinks",
"--link", "libnvidia-vulkan-producer.so.123.45.67::/usr/lib64/libnvidia-vulkan-producer.so",
},
},
},
},
{
description: "libnvidia-allocator and libnvidia-vulkan-producer discovered",
libraries: &DiscoverMock{
MountsFunc: func() ([]Mount, error) {
mounts := []Mount{
{
Path: "/usr/lib64/libnvidia-allocator.so.123.45.67",
},
{
Path: "/usr/lib64/libnvidia-vulkan-producer.so.123.45.67",
},
}
return mounts, nil
},
},
expectedMounts: []Mount{
{
Path: "/usr/lib64/libnvidia-allocator.so.123.45.67",
},
{
Path: "/usr/lib64/libnvidia-vulkan-producer.so.123.45.67",
},
},
expectedHooks: []Hook{
{
Lifecycle: "createContainer",
Path: "/usr/bin/nvidia-cdi-hook",
Args: []string{"nvidia-cdi-hook", "create-symlinks",
"--link", "libnvidia-allocator.so.123.45.67::/usr/lib64/libnvidia-allocator.so.1",
"--link", "../libnvidia-allocator.so.1::/usr/lib64/gbm/nvidia-drm_gbm.so",
"--link", "libnvidia-vulkan-producer.so.123.45.67::/usr/lib64/libnvidia-vulkan-producer.so",
},
},
},
},
}
for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
d := &graphicsDriverLibraries{
Discover: tc.libraries,
logger: logger,
nvidiaCDIHookPath: "/usr/bin/nvidia-cdi-hook",
}
devices, err := d.Devices()
require.NoError(t, err)
require.Empty(t, devices)
require.Len(t, tc.libraries.calls.Devices, 1)
mounts, err := d.Mounts()
require.NoError(t, err)
require.EqualValues(t, tc.expectedMounts, mounts)
require.Len(t, tc.libraries.calls.Mounts, 1)
hooks, err := d.Hooks()
require.NoError(t, err)
require.EqualValues(t, tc.expectedHooks, hooks)
require.Len(t, tc.libraries.calls.Mounts, 2)
require.Len(t, tc.libraries.calls.Hooks, 0)
})
}
}