-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create .so symlinks for driver libraries in container
This change adds an opt-in feature for creating .so symlinks to all injected driver files in a contianer. If features.dot-so-symlinks = true is set in the config.toml, the creation of symlinks for driver files is enabled. This can also be triggered on a per-container basis using the envvar NVIDIA_DOT_SO_SYMLINKS=enabled. Signed-off-by: Evan Lezar <[email protected]>
- Loading branch information
Showing
11 changed files
with
252 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
cmd/nvidia-ctk/hook/create-dot-so-symlinks/create-dot-so-symlinks.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/** | ||
# Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. | ||
# | ||
# 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 dotsosymlinks | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/urfave/cli/v2" | ||
|
||
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger" | ||
"github.com/NVIDIA/nvidia-container-toolkit/internal/lookup" | ||
"github.com/NVIDIA/nvidia-container-toolkit/internal/oci" | ||
) | ||
|
||
type command struct { | ||
logger logger.Interface | ||
} | ||
|
||
type config struct { | ||
containerSpec string | ||
driverVersion string | ||
} | ||
|
||
// NewCommand constructs a hook command with the specified logger | ||
func NewCommand(logger logger.Interface) *cli.Command { | ||
c := command{ | ||
logger: logger, | ||
} | ||
return c.build() | ||
} | ||
|
||
// build | ||
func (m command) build() *cli.Command { | ||
cfg := config{} | ||
|
||
// Create the '' command | ||
c := cli.Command{ | ||
Name: "create-dot-so-symlinks", | ||
Usage: "A hook to create .so symlinks in the container.", | ||
Action: func(c *cli.Context) error { | ||
return m.run(c, &cfg) | ||
}, | ||
} | ||
|
||
c.Flags = []cli.Flag{ | ||
&cli.StringFlag{ | ||
Name: "container-spec", | ||
Usage: "Specify the path to the OCI container spec. If empty or '-' the spec will be read from STDIN", | ||
Destination: &cfg.containerSpec, | ||
}, | ||
&cli.StringFlag{ | ||
Name: "driver-version", | ||
Usage: "specify the driver version for which the symlinks are to be created. This assumes driver libraries have the .so.`VERSION` suffix.", | ||
Destination: &cfg.driverVersion, | ||
Required: true, | ||
}, | ||
} | ||
|
||
return &c | ||
} | ||
|
||
func (m command) run(c *cli.Context, cfg *config) error { | ||
s, err := oci.LoadContainerState(cfg.containerSpec) | ||
if err != nil { | ||
return fmt.Errorf("failed to load container state: %v", err) | ||
} | ||
|
||
containerRoot, err := s.GetContainerRoot() | ||
if err != nil { | ||
return fmt.Errorf("failed to determined container root: %v", err) | ||
} | ||
|
||
locator := lookup.NewLibraryLocator( | ||
lookup.WithLogger(m.logger), | ||
lookup.WithRoot(containerRoot), | ||
lookup.WithOptional(true), | ||
) | ||
libs, err := locator.Locate("*.so." + cfg.driverVersion) | ||
if err != nil { | ||
return fmt.Errorf("failed to locate libraries for driver version %v: %v", cfg.driverVersion, err) | ||
} | ||
|
||
for _, lib := range libs { | ||
if !strings.HasSuffix(lib, ".so."+cfg.driverVersion) { | ||
continue | ||
} | ||
libSoPath := strings.TrimSuffix(lib, "."+cfg.driverVersion) | ||
libSoXPaths, err := filepath.Glob(libSoPath + ".[0-9]") | ||
if len(libSoXPaths) != 1 || err != nil { | ||
continue | ||
} | ||
err = os.Symlink(filepath.Base(libSoXPaths[0]), libSoPath) | ||
if err != nil { | ||
continue | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
# 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 | ||
|
||
// NewDotSoSymlinksDiscoverer creates a discoverer that generates a hook to create .so symlinks in | ||
// a container. | ||
func NewDotSoSymlinksDiscoverer(nvidiaCTKPath string, version string) Discover { | ||
return CreateNvidiaCTKHook( | ||
nvidiaCTKPath, | ||
"create-dot-so-symlinks", | ||
"--driver-version", version, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.