-
Notifications
You must be signed in to change notification settings - Fork 43
fix(remount): relocate libraries along with their symlinks #255
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
Merged
Merged
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 hidden or 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 hidden or 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,86 @@ | ||
package ebutil | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
// Container runtimes like NVIDIA mount individual libraries into the container | ||
// (e.g. `<libname>.so.<driver_version>`) and create symlinks for them | ||
// (e.g. `<libname>.so.1`). This code helps with finding the right library | ||
// directory for the target Linux distribution as well as locating the symlinks. | ||
// | ||
// Please see [#143 (comment)] for further details. | ||
// | ||
// [#143 (comment)]: https://github.com/coder/envbuilder/issues/143#issuecomment-2192405828 | ||
|
||
// Based on https://github.com/NVIDIA/libnvidia-container/blob/v1.15.0/src/common.h#L29 | ||
const usrLibDir = "/usr/lib64" | ||
|
||
const debianVersionFile = "/etc/debian_version" | ||
|
||
// libraryDirectoryPath returns the library directory. It returns a multiarch | ||
// directory if the distribution is Debian or a derivative. | ||
// | ||
// Based on https://github.com/NVIDIA/libnvidia-container/blob/v1.15.0/src/nvc_container.c#L152-L165 | ||
func libraryDirectoryPath(m mounter) (string, error) { | ||
// Debian and its derivatives use a multiarch directory scheme. | ||
if _, err := m.Stat(debianVersionFile); err != nil && !errors.Is(err, os.ErrNotExist) { | ||
return "", fmt.Errorf("check if debian: %w", err) | ||
} else if err == nil { | ||
return usrLibMultiarchDir, nil | ||
} | ||
|
||
return usrLibDir, nil | ||
} | ||
|
||
// libraryDirectorySymlinks returns a mapping of each library (basename) with a | ||
// list of their symlinks (basename). Libraries with no symlinks do not appear | ||
// in the mapping. | ||
func libraryDirectorySymlinks(m mounter, libDir string) (map[string][]string, error) { | ||
des, err := m.ReadDir(libDir) | ||
mtojek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err != nil { | ||
return nil, fmt.Errorf("read directory %s: %w", libDir, err) | ||
} | ||
|
||
libsSymlinks := make(map[string][]string) | ||
for _, de := range des { | ||
if de.IsDir() { | ||
continue | ||
} | ||
|
||
if de.Type()&os.ModeSymlink != os.ModeSymlink { | ||
// Not a symlink. Skip. | ||
continue | ||
} | ||
|
||
symlink := filepath.Join(libDir, de.Name()) | ||
path, err := m.EvalSymlinks(symlink) | ||
if err != nil { | ||
return nil, fmt.Errorf("eval symlink %s: %w", symlink, err) | ||
} | ||
|
||
path = filepath.Base(path) | ||
if _, ok := libsSymlinks[path]; !ok { | ||
libsSymlinks[path] = make([]string, 0, 1) | ||
} | ||
|
||
libsSymlinks[path] = append(libsSymlinks[path], de.Name()) | ||
} | ||
|
||
return libsSymlinks, nil | ||
} | ||
|
||
// moveLibSymlinks moves a list of symlinks from source to destination directory. | ||
func moveLibSymlinks(m mounter, symlinks []string, srcDir, destDir string) error { | ||
for _, l := range symlinks { | ||
oldpath := filepath.Join(srcDir, l) | ||
newpath := filepath.Join(destDir, l) | ||
if err := m.Rename(oldpath, newpath); err != nil { | ||
return fmt.Errorf("move symlink %s => %s: %w", oldpath, newpath, err) | ||
} | ||
} | ||
return nil | ||
} |
This file contains hidden or 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,7 @@ | ||
//go:build amd64 | ||
|
||
package ebutil | ||
|
||
// Based on https://github.com/NVIDIA/libnvidia-container/blob/v1.15.0/src/common.h#L36 | ||
|
||
const usrLibMultiarchDir = "/usr/lib/x86_64-linux-gnu" |
This file contains hidden or 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,7 @@ | ||
//go:build arm64 | ||
|
||
package ebutil | ||
|
||
// Based on https://github.com/NVIDIA/libnvidia-container/blob/v1.15.0/src/common.h#L52 | ||
|
||
const usrLibMultiarchDir = "/usr/lib/aarch64-linux-gnu" |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.