Skip to content

Add envvar for libcuda.so parent dir to CDI spec #1030

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/nvidia-ctk-installer/container/toolkit/toolkit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ devices:
hostPath: /host/driver/root/dev/nvidia-caps-imex-channels/channel2047
containerEdits:
env:
- LIBCUDA_SO_PARENT_DIRECTORY_CONTAINER_PATH=/lib/x86_64-linux-gnu
- NVIDIA_VISIBLE_DEVICES=void
hooks:
- hookName: createContainer
Expand Down
1 change: 1 addition & 0 deletions cmd/nvidia-ctk/cdi/generate/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ devices:
hostPath: {{ .driverRoot }}/dev/nvidia0
containerEdits:
env:
- LIBCUDA_SO_PARENT_DIRECTORY_CONTAINER_PATH=/lib/x86_64-linux-gnu
- NVIDIA_VISIBLE_DEVICES=void
deviceNodes:
- path: /dev/nvidiactl
Expand Down
15 changes: 15 additions & 0 deletions internal/discover/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type cache struct {

sync.Mutex
devices []Device
envVars []EnvVar
hooks []Hook
mounts []Mount
}
Expand Down Expand Up @@ -51,6 +52,20 @@ func (c *cache) Devices() ([]Device, error) {
return c.devices, nil
}

func (c *cache) EnvVars() ([]EnvVar, error) {
c.Lock()
defer c.Unlock()

if c.envVars == nil {
envVars, err := c.d.EnvVars()
if err != nil {
return nil, err
}
c.envVars = envVars
}
return c.envVars, nil
}

func (c *cache) Hooks() ([]Hook, error) {
c.Lock()
defer c.Unlock()
Expand Down
7 changes: 7 additions & 0 deletions internal/discover/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ type Device struct {
Path string
}

// EnvVar represents a discovered environment variable.
type EnvVar struct {
Name string
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am more of a Key/Value mind, but not a blocker.

Value string
}

// Mount represents a discovered mount.
type Mount struct {
HostPath string
Expand All @@ -41,6 +47,7 @@ type Hook struct {
//go:generate moq -rm -fmt=goimports -stub -out discover_mock.go . Discover
type Discover interface {
Devices() ([]Device, error)
EnvVars() ([]EnvVar, error)
Mounts() ([]Mount, error)
Hooks() ([]Hook, error)
}
41 changes: 41 additions & 0 deletions internal/discover/discover_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions internal/discover/envvar.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# 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

var _ Discover = (*EnvVar)(nil)

// Devices returns an empty list of devices for a EnvVar discoverer.
func (e EnvVar) Devices() ([]Device, error) {
return nil, nil
}

// EnvVars returns an empty list of envs for a EnvVar discoverer.
func (e EnvVar) EnvVars() ([]EnvVar, error) {
return []EnvVar{e}, nil
}

// Mounts returns an empty list of mounts for a EnvVar discoverer.
func (e EnvVar) Mounts() ([]Mount, error) {
return nil, nil
}

// Hooks allows the Hook type to also implement the Discoverer interface.
// It returns a single hook
func (e EnvVar) Hooks() ([]Hook, error) {
return nil, nil
}
13 changes: 13 additions & 0 deletions internal/discover/first-valid.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ func (f firstOf) Devices() ([]Device, error) {
return nil, errs
}

func (f firstOf) EnvVars() ([]EnvVar, error) {
var errs error
for _, d := range f {
envs, err := d.EnvVars()
if err != nil {
errs = errors.Join(errs, err)
continue
}
return envs, nil
}
return nil, errs
}

func (f firstOf) Hooks() ([]Hook, error) {
var errs error
for _, d := range f {
Expand Down
5 changes: 5 additions & 0 deletions internal/discover/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ func (h Hook) Devices() ([]Device, error) {
return nil, nil
}

// EnvVars returns an empty list of envs for a Hook discoverer.
func (h Hook) EnvVars() ([]EnvVar, error) {
return nil, nil
}

// Mounts returns an empty list of mounts for a Hook discoverer.
func (h Hook) Mounts() ([]Mount, error) {
return nil, nil
Expand Down
15 changes: 15 additions & 0 deletions internal/discover/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,21 @@ func (d list) Devices() ([]Device, error) {
return allDevices, nil
}

// EnvVars returns all environment variables from the included discoverers.
func (d list) EnvVars() ([]EnvVar, error) {
var allEnvs []EnvVar

for i, di := range d.discoverers {
envs, err := di.EnvVars()
if err != nil {
return nil, fmt.Errorf("error discovering envs for discoverer %v: %w", i, err)
}
allEnvs = append(allEnvs, envs...)
}

return allEnvs, nil
}

// Mounts returns all mounts from the included discoverers
func (d list) Mounts() ([]Mount, error) {
var allMounts []Mount
Expand Down
5 changes: 5 additions & 0 deletions internal/discover/none.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ func (e None) Devices() ([]Device, error) {
return nil, nil
}

// EnvVars returns an empty list of devices
func (e None) EnvVars() ([]EnvVar, error) {
return nil, nil
}

// Mounts returns an empty list of mounts
func (e None) Mounts() ([]Mount, error) {
return nil, nil
Expand Down
9 changes: 9 additions & 0 deletions internal/edits/edits.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ func FromDiscoverer(d discover.Discover) (*cdi.ContainerEdits, error) {
return nil, fmt.Errorf("failed to discover devices: %v", err)
}

envs, err := d.EnvVars()
if err != nil {
return nil, fmt.Errorf("failed to discover environment variables: %w", err)
}

mounts, err := d.Mounts()
if err != nil {
return nil, fmt.Errorf("failed to discover mounts: %v", err)
Expand All @@ -74,6 +79,10 @@ func FromDiscoverer(d discover.Discover) (*cdi.ContainerEdits, error) {
c.Append(edits)
}

for _, e := range envs {
c.Append(envvar(e).toEdits())
}

for _, m := range mounts {
c.Append(mount(m).toEdits())
}
Expand Down
39 changes: 39 additions & 0 deletions internal/edits/envvar.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# 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 edits

import (
"fmt"

"tags.cncf.io/container-device-interface/pkg/cdi"
"tags.cncf.io/container-device-interface/specs-go"

"github.com/NVIDIA/nvidia-container-toolkit/internal/discover"
)

type envvar discover.EnvVar

// toEdits converts a discovered envvar to CDI Container Edits.
func (d envvar) toEdits() *cdi.ContainerEdits {
e := cdi.ContainerEdits{
ContainerEdits: &specs.ContainerEdits{
Env: []string{fmt.Sprintf("%s=%s", d.Name, d.Value)},
},
}
return &e
}
5 changes: 5 additions & 0 deletions internal/platform-support/dgpu/by-path-hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ func (d *byPathHookDiscoverer) Devices() ([]discover.Device, error) {
return nil, nil
}

// EnvVars returns the empty list for the by-path hook discoverer
func (d *byPathHookDiscoverer) EnvVars() ([]discover.EnvVar, error) {
return nil, nil
}

// Hooks returns the hooks for the GPU device.
// The following hooks are detected:
// 1. A hook to create /dev/dri/by-path symlinks
Expand Down
4 changes: 4 additions & 0 deletions internal/platform-support/dgpu/nvsandboxutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ func (d *nvsandboxutilsDGPU) Devices() ([]discover.Device, error) {
return devices, nil
}

func (d *nvsandboxutilsDGPU) EnvVars() ([]discover.EnvVar, error) {
return nil, nil
}

// Hooks returns a hook to create the by-path symlinks for the discovered devices.
func (d *nvsandboxutilsDGPU) Hooks() ([]discover.Hook, error) {
if len(d.deviceLinks) == 0 {
Expand Down
Loading