Skip to content

Commit 07d0f1c

Browse files
committed
Address review comments
1 parent 773d748 commit 07d0f1c

File tree

4 files changed

+22
-22
lines changed

4 files changed

+22
-22
lines changed

internal/ebutil/libs.go

+16-8
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,25 @@ import (
77
"path/filepath"
88
)
99

10-
// Based on https://github.com/NVIDIA/libnvidia-container/blob/v1.15.0/src/common.h#L29
10+
// Container runtimes like NVIDIA mount individual libraries into the container
11+
// (e.g. `<libname>.so.<driver_version>`) and create symlinks for them
12+
// (e.g. `<libname>.so.1`). This code helps with finding the right library
13+
// directory for the target Linux distribution as well as locating the symlinks.
14+
//
15+
// Please see [#143 (comment)] for further details.
16+
//
17+
// [#143 (comment)]: https://github.com/coder/envbuilder/issues/143#issuecomment-2192405828
1118

19+
// Based on https://github.com/NVIDIA/libnvidia-container/blob/v1.15.0/src/common.h#L29
1220
const usrLibDir = "/usr/lib64"
1321

1422
const debianVersionFile = "/etc/debian_version"
1523

16-
// getLibDir returns the library directory. It returns a multiarch directory if
17-
// the distribution is Debian or a derivative.
24+
// libraryDirectoryPath returns the library directory. It returns a multiarch
25+
// directory if the distribution is Debian or a derivative.
1826
//
1927
// Based on https://github.com/NVIDIA/libnvidia-container/blob/v1.15.0/src/nvc_container.c#L152-L165
20-
func getLibDir(m mounter) (string, error) {
28+
func libraryDirectoryPath(m mounter) (string, error) {
2129
// Debian and its derivatives use a multiarch directory scheme.
2230
if _, err := m.Stat(debianVersionFile); err != nil && !errors.Is(err, os.ErrNotExist) {
2331
return "", fmt.Errorf("check if debian: %w", err)
@@ -28,9 +36,10 @@ func getLibDir(m mounter) (string, error) {
2836
return usrLibDir, nil
2937
}
3038

31-
// getLibsSymlinks returns the stats for all library symlinks if the library
32-
// directory exists.
33-
func getLibsSymlinks(m mounter, libDir string) (map[string][]string, error) {
39+
// libraryDirectorySymlinks returns a mapping of each library (basename) with a
40+
// list of their symlinks (basename). Libraries with no symlinks do not appear
41+
// in the mapping.
42+
func libraryDirectorySymlinks(m mounter, libDir string) (map[string][]string, error) {
3443
des, err := m.ReadDir(libDir)
3544
if err != nil {
3645
return nil, fmt.Errorf("read directory %s: %w", libDir, err)
@@ -54,7 +63,6 @@ func getLibsSymlinks(m mounter, libDir string) (map[string][]string, error) {
5463
}
5564

5665
path = filepath.Base(path)
57-
5866
if _, ok := libsSymlinks[path]; !ok {
5967
libsSymlinks[path] = make([]string, 0, 1)
6068
}

internal/ebutil/libs_ppc64le.go

-7
This file was deleted.

internal/ebutil/remount.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ func tempRemount(m mounter, logf func(notcodersdk.LogLevel, string, ...any), bas
4545
return func() error { return nil }, fmt.Errorf("get mounts: %w", err)
4646
}
4747

48-
libDir, err := getLibDir(m)
48+
libDir, err := libraryDirectoryPath(m)
4949
if err != nil {
5050
return func() error { return nil }, fmt.Errorf("get lib directory: %w", err)
5151
}
5252

53-
libsSymlinks, err := getLibsSymlinks(m, libDir)
53+
libsSymlinks, err := libraryDirectorySymlinks(m, libDir)
5454
if err != nil && !errors.Is(err, os.ErrNotExist) {
5555
return func() error { return nil }, fmt.Errorf("read lib symlinks: %w", err)
5656
}
@@ -66,7 +66,7 @@ func tempRemount(m mounter, logf func(notcodersdk.LogLevel, string, ...any), bas
6666
return
6767
}
6868

69-
newLibDir, err := getLibDir(m)
69+
newLibDir, err := libraryDirectoryPath(m)
7070
if err != nil {
7171
merr = multierror.Append(merr, fmt.Errorf("get new lib directory: %w", err))
7272
return
@@ -137,6 +137,7 @@ func remount(m mounter, src, dest, libDir string, libsSymlinks map[string][]stri
137137
if err != nil {
138138
return fmt.Errorf("ensure file path: %w", err)
139139
}
140+
// This ensure the file is created, it will not be used. It can be closed immediately.
140141
f.Close()
141142

142143
if symlinks, ok := libsSymlinks[stat.Name()]; ok {
@@ -154,7 +155,6 @@ func remount(m mounter, src, dest, libDir string, libsSymlinks map[string][]stri
154155
if err := m.Unmount(src, 0); err != nil {
155156
return fmt.Errorf("unmount orig src %s: %w", src, err)
156157
}
157-
158158
return nil
159159
}
160160

internal/ebutil/remount_internal_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ import (
1717
)
1818

1919
var expectedLibMultiarchDir = map[string]string{
20-
"amd64": "/usr/lib/x86_64-linux-gnu",
21-
"arm64": "/usr/lib/aarch64-linux-gnu",
22-
"ppc64le": "/usr/lib/powerpc64le-linux-gnu",
20+
"amd64": "/usr/lib/x86_64-linux-gnu",
21+
"arm64": "/usr/lib/aarch64-linux-gnu",
2322
}
2423

2524
func Test_tempRemount(t *testing.T) {

0 commit comments

Comments
 (0)