-
Notifications
You must be signed in to change notification settings - Fork 258
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
Create .so symlinks for driver libraries in container #326
Open
elezar
wants to merge
2
commits into
NVIDIA:main
Choose a base branch
from
elezar:CNT-4766/create-so-symlinks
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's probably fine for our purposes, but should this actually be
".[0-9]+"
t include multi-digit versions?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it should. I was thinking one should probably rather extract the
SONAME
from the target library and do it that way, but thought this heuristic was enough.One issue is that a
Glob
pattern is not a regex and the+
is not available there.I suppose instead of including this logic, we chould just symlink
.so -> .so.driverVersion
directly and bypass the.so.[0-9]
symlink here. Thoughts?