Skip to content

Commit 02ae192

Browse files
committed
fix(dockerutil): GetImageMetadata: sniff container UsrLibDir
1 parent 361631d commit 02ae192

File tree

2 files changed

+44
-9
lines changed

2 files changed

+44
-9
lines changed

cli/docker.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,10 @@ func runDockerCVM(ctx context.Context, log slog.Logger, client dockerutil.Client
620620
mountpoint := bind.Path
621621
if strings.HasPrefix(mountpoint, flags.hostUsrLibDir) {
622622
mountpoint = filepath.Join(
623-
"/usr/lib",
623+
// Note: we used to mount into /usr/lib, but this can change
624+
// based on the distro inside the container. We are essentially
625+
// mimicking the behavior of the nvidia container runtime.
626+
imgMeta.UsrLibDir,
624627
strings.TrimPrefix(mountpoint, strings.TrimSuffix(flags.hostUsrLibDir, "/")),
625628
)
626629
}

dockerutil/image.go

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,19 @@ import (
2222

2323
const diskFullStorageDriver = "vfs"
2424

25+
// Adapted from github.com/NVIDIA/nvidia-container-toolkit/internal/lookup/library.go
26+
// These are destination candidates for the /usr/lib directory in the container,
27+
// in order of priority.
28+
// Depending on the inner image, the desired location may vary.
29+
// Note that we are excluding some nvidia-specific directories here and also
30+
// include a fallback to /usr/lib.
31+
var usrLibCandidates = []string{
32+
"/usr/lib/x86_64-linux-gnu", // Debian uses a multiarch /usr/lib directory
33+
"/usr/lib/aarch64-linux-gnu", // Above but for arm64.
34+
"/usr/lib64", // Red Hat and friends.
35+
"/usr/lib", // Fallback.
36+
}
37+
2538
type PullImageConfig struct {
2639
Client Client
2740
Image string
@@ -148,10 +161,11 @@ func processImagePullEvents(r io.Reader, fn ImagePullProgressFn) error {
148161
}
149162

150163
type ImageMetadata struct {
151-
UID string
152-
GID string
153-
HomeDir string
154-
HasInit bool
164+
UID string
165+
GID string
166+
HomeDir string
167+
HasInit bool
168+
UsrLibDir string
155169
}
156170

157171
// GetImageMetadata returns metadata about an image such as the UID/GID of the
@@ -226,11 +240,29 @@ func GetImageMetadata(ctx context.Context, client Client, img, username string)
226240
return ImageMetadata{}, xerrors.Errorf("no users returned for username %s", username)
227241
}
228242

243+
// Find the "best" usr lib directory for the container.
244+
var foundUsrLibDir string
245+
for _, candidate := range usrLibCandidates {
246+
_, err := ExecContainer(ctx, client, ExecConfig{
247+
ContainerID: inspect.ID,
248+
Cmd: "stat",
249+
Args: []string{candidate},
250+
})
251+
if err == nil {
252+
foundUsrLibDir = candidate
253+
break
254+
}
255+
}
256+
if foundUsrLibDir == "" {
257+
return ImageMetadata{}, xerrors.Errorf("no eligible /usr/lib directory found in container")
258+
}
259+
229260
return ImageMetadata{
230-
UID: users[0].Uid,
231-
GID: users[0].Gid,
232-
HomeDir: users[0].HomeDir,
233-
HasInit: initExists,
261+
UID: users[0].Uid,
262+
GID: users[0].Gid,
263+
HomeDir: users[0].HomeDir,
264+
HasInit: initExists,
265+
UsrLibDir: foundUsrLibDir,
234266
}, nil
235267
}
236268

0 commit comments

Comments
 (0)