Skip to content

Commit c20024a

Browse files
committed
Extract deb and rpm packages to single image
This change swithces to using a single image for the NVIDIA Container Toolkit contianer. Here the contents of the architecture-specific deb and rpm packages are extracted to a known root. These contents can then be installed using the updated installation mechanism which has been updated to detect the source root based on the packaging type. Signed-off-by: Evan Lezar <[email protected]>
1 parent de3d736 commit c20024a

File tree

6 files changed

+148
-32
lines changed

6 files changed

+148
-32
lines changed

cmd/nvidia-ctk-installer/main.go

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/NVIDIA/nvidia-container-toolkit/cmd/nvidia-ctk-installer/toolkit"
1515
"github.com/NVIDIA/nvidia-container-toolkit/internal/info"
1616
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
17+
"github.com/NVIDIA/nvidia-container-toolkit/internal/lookup"
1718
)
1819

1920
const (
@@ -36,10 +37,11 @@ var signalReceived = make(chan bool, 1)
3637
type options struct {
3738
toolkitInstallDir string
3839

39-
noDaemon bool
40-
runtime string
41-
pidFile string
42-
sourceRoot string
40+
noDaemon bool
41+
runtime string
42+
pidFile string
43+
sourceRoot string
44+
packageType string
4345

4446
toolkitOptions toolkit.Options
4547
runtimeOptions runtime.Options
@@ -123,11 +125,17 @@ func (a app) build() *cli.App {
123125
EnvVars: []string{"TOOLKIT_INSTALL_DIR", "ROOT"},
124126
},
125127
&cli.StringFlag{
126-
Name: "source-root",
127-
Value: "/",
128-
Usage: "The folder where the required toolkit artifacts can be found",
128+
Name: "toolkit-source-root",
129+
Usage: "The folder where the required toolkit artifacts can be found. If this is not specified, the path /artifacts/{{ .ToolkitPackageType }} is used where ToolkitPackageType is the resolved package type",
129130
Destination: &options.sourceRoot,
130-
EnvVars: []string{"SOURCE_ROOT"},
131+
EnvVars: []string{"TOOLKIT_SOURCE_ROOT"},
132+
},
133+
&cli.StringFlag{
134+
Name: "toolkit-package-type",
135+
Usage: "specify the package type to use for the toolkit. One of ['deb', 'rpm', 'auto', '']. If 'auto' or '' are used, the type is inferred automatically.",
136+
Value: "auto",
137+
Destination: &options.packageType,
138+
EnvVars: []string{"TOOLKIT_PACKAGE_TYPE"},
131139
},
132140
&cli.StringFlag{
133141
Name: "pid-file",
@@ -145,6 +153,15 @@ func (a app) build() *cli.App {
145153
}
146154

147155
func (a *app) Before(c *cli.Context, o *options) error {
156+
if o.sourceRoot == "" {
157+
sourceRoot, err := a.resolveSourceRoot(o.runtimeOptions.HostRootMount, o.packageType)
158+
if err != nil {
159+
return fmt.Errorf("failed to resolve source root: %v", err)
160+
}
161+
a.logger.Infof("Resolved source root to %v", sourceRoot)
162+
o.sourceRoot = sourceRoot
163+
}
164+
148165
a.toolkit = toolkit.NewInstaller(
149166
toolkit.WithLogger(a.logger),
150167
toolkit.WithSourceRoot(o.sourceRoot),
@@ -277,3 +294,35 @@ func (a *app) shutdown(pidFile string) {
277294
a.logger.Warningf("Unable to remove pidfile: %v", err)
278295
}
279296
}
297+
298+
func (a *app) resolveSourceRoot(hostRoot string, packageType string) (string, error) {
299+
resolvedPackageType, err := a.resolvePackageType(hostRoot, packageType)
300+
if err != nil {
301+
return "", err
302+
}
303+
switch resolvedPackageType {
304+
case "deb":
305+
return "/artifacts/deb", nil
306+
case "rpm":
307+
return "/artifacts/rpm", nil
308+
default:
309+
return "", fmt.Errorf("invalid package type: %v", resolvedPackageType)
310+
}
311+
}
312+
313+
func (a *app) resolvePackageType(hostRoot string, packageType string) (rPackageTypes string, rerr error) {
314+
if packageType != "" && packageType != "auto" {
315+
return packageType, nil
316+
}
317+
318+
locator := lookup.NewExecutableLocator(a.logger, hostRoot)
319+
if candidates, err := locator.Locate("/usr/bin/rpm"); err == nil && len(candidates) > 0 {
320+
return "rpm", nil
321+
}
322+
323+
if candidates, err := locator.Locate("/usr/bin/dpkg"); err == nil && len(candidates) > 0 {
324+
return "deb", nil
325+
}
326+
327+
return "deb", nil
328+
}

cmd/nvidia-ctk-installer/main_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ swarm-resource = ""
418418
"--driver-root-ctr-path=" + hostRoot,
419419
"--pid-file=" + filepath.Join(testRoot, "toolkit.pid"),
420420
"--restart-mode=none",
421-
"--source-root=" + filepath.Join(artifactRoot, "deb"),
421+
"--toolkit-source-root=" + filepath.Join(artifactRoot, "deb"),
422422
}
423423

424424
err := app.Run(append(testArgs, tc.args...))

cmd/nvidia-ctk-installer/toolkit/installer/installer.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,16 @@ var _ Installer = (*toolkitInstaller)(nil)
4747

4848
// New creates a toolkit installer with the specified options.
4949
func New(opts ...Option) (Installer, error) {
50-
t := &toolkitInstaller{}
50+
t := &toolkitInstaller{
51+
sourceRoot: "/",
52+
}
5153
for _, opt := range opts {
5254
opt(t)
5355
}
5456

5557
if t.logger == nil {
5658
t.logger = logger.New()
5759
}
58-
if t.sourceRoot == "" {
59-
t.sourceRoot = "/"
60-
}
6160
if t.artifactRoot == nil {
6261
artifactRoot, err := newArtifactRoot(t.logger, t.sourceRoot)
6362
if err != nil {

cmd/nvidia-ctk-installer/toolkit/toolkit.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,8 @@ func Flags(opts *Options) []cli.Flag {
215215

216216
// An Installer is used to install the NVIDIA Container Toolkit from the toolkit container.
217217
type Installer struct {
218-
logger logger.Interface
218+
logger logger.Interface
219+
219220
sourceRoot string
220221
// toolkitRoot specifies the destination path at which the toolkit is installed.
221222
toolkitRoot string

deployments/container/Dockerfile.ubi8

Lines changed: 75 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,33 +47,92 @@ ARG VERSION="N/A"
4747
ARG GIT_COMMIT="unknown"
4848
RUN make PREFIX=/artifacts cmd-nvidia-ctk-installer
4949

50-
FROM nvcr.io/nvidia/cuda:12.8.1-base-ubi8
51-
52-
ENV NVIDIA_DISABLE_REQUIRE="true"
53-
ENV NVIDIA_VISIBLE_DEVICES=void
54-
ENV NVIDIA_DRIVER_CAPABILITIES=utility
50+
# The packaging stage collects the deb and rpm packages built for supported
51+
# architectures.
52+
FROM nvcr.io/nvidia/cuda:12.8.1-base-ubi8 AS packaging
5553

5654
ARG ARTIFACTS_ROOT
57-
ARG PACKAGE_DIST
58-
COPY ${ARTIFACTS_ROOT}/${PACKAGE_DIST} /artifacts/packages/${PACKAGE_DIST}
55+
COPY ${ARTIFACTS_ROOT} /artifacts/packages/
5956

6057
WORKDIR /artifacts/packages
6158

59+
# build-args are added to the manifest.txt file below.
60+
ARG PACKAGE_DIST
6261
ARG PACKAGE_VERSION
62+
ARG GIT_BRANCH
63+
ARG GIT_COMMIT
64+
ARG GIT_COMMIT_SHORT
65+
ARG SOURCE_DATE_EPOCH
66+
ARG VERSION
67+
68+
# Create a manifest.txt file with the absolute paths of all deb and rpm packages in the container
69+
RUN echo "#IMAGE_EPOCH=$(date '+%s')" > /artifacts/manifest.txt && \
70+
env | sed 's/^/#/g' >> /artifacts/manifest.txt && \
71+
find /artifacts/packages -iname '*.deb' -o -iname '*.rpm' >> /artifacts/manifest.txt
72+
73+
RUN mkdir /licenses && mv /NGC-DL-CONTAINER-LICENSE /licenses/NGC-DL-CONTAINER-LICENSE
74+
75+
# The debpackages stage is used to extract the contents of deb packages.
76+
FROM nvcr.io/nvidia/cuda:12.8.1-base-ubuntu20.04 AS debpackages
77+
6378
ARG TARGETARCH
64-
ENV PACKAGE_ARCH=${TARGETARCH}
79+
ARG PACKAGE_DIST_DEB=ubuntu18.04
6580

66-
RUN PACKAGE_ARCH=${PACKAGE_ARCH/amd64/x86_64} && PACKAGE_ARCH=${PACKAGE_ARCH/arm64/aarch64} && \
67-
yum localinstall -y \
68-
${PACKAGE_DIST}/${PACKAGE_ARCH}/libnvidia-container1-1.*.rpm \
69-
${PACKAGE_DIST}/${PACKAGE_ARCH}/libnvidia-container-tools-1.*.rpm \
70-
${PACKAGE_DIST}/${PACKAGE_ARCH}/nvidia-container-toolkit*-${PACKAGE_VERSION}*.rpm
81+
COPY --from=packaging /artifacts/packages/${PACKAGE_DIST_DEB} /deb-packages
7182

72-
WORKDIR /work
83+
RUN mkdir -p /artifacts/deb
84+
RUN set -eux; \
85+
\
86+
case "${TARGETARCH}" in \
87+
x86_64 | amd64) ARCH='amd64' ;; \
88+
ppc64el | ppc64le) ARCH='ppc64le' ;; \
89+
aarch64 | arm64) ARCH='arm64' ;; \
90+
*) echo "unsupported architecture" ; exit 1 ;; \
91+
esac; \
92+
for p in $(ls /deb-packages/${ARCH}/*.deb); do dpkg-deb -xv $p /artifacts/deb/; done
93+
94+
# The rpmpackages stage is used to extract the contents of the rpm packages.
95+
FROM nvcr.io/nvidia/cuda:12.8.1-base-ubi8 AS rpmpackages
96+
RUN dnf install -y cpio
97+
98+
ARG TARGETARCH
99+
ARG PACKAGE_DIST_RPM=centos7
100+
101+
COPY --from=packaging /artifacts/packages/${PACKAGE_DIST_RPM} /rpm-packages
102+
103+
RUN mkdir -p /artifacts/rpm
104+
RUN set -eux; \
105+
\
106+
case "${TARGETARCH}" in \
107+
x86_64 | amd64) ARCH='x86_64' ;; \
108+
ppc64el | ppc64le) ARCH='ppc64le' ;; \
109+
aarch64 | arm64) ARCH='aarch64' ;; \
110+
*) echo "unsupported architecture" ; exit 1 ;; \
111+
esac; \
112+
for p in $(ls /rpm-packages/${ARCH}/*.rpm); do rpm2cpio $p | cpio -idmv -D /artifacts/rpm; done
113+
114+
# The artifacts image serves as an intermediate stage to collect the artifacts
115+
# From the previous stages:
116+
# - The extracted deb packages
117+
# - The extracted rpm packages
118+
# - The nvidia-ctk-installer binary
119+
FROM nvcr.io/nvidia/cuda:12.8.1-base-ubi8 AS artifacts
120+
121+
COPY --from=rpmpackages /artifacts/rpm /artifacts/rpm
122+
COPY --from=debpackages /artifacts/deb /artifacts/deb
123+
COPY --from=build /artifacts/bin /artifacts/build
124+
125+
FROM nvcr.io/nvidia/cuda:12.8.1-base-ubi8
73126

74-
COPY --from=build /artifacts/nvidia-ctk-installer /work/nvidia-ctk-installer
75-
RUN ln -s nvidia-ctk-installer nvidia-toolkit
127+
ENV NVIDIA_DISABLE_REQUIRE="true"
128+
ENV NVIDIA_VISIBLE_DEVICES=void
129+
ENV NVIDIA_DRIVER_CAPABILITIES=utility
76130

131+
COPY --from=artifacts /artifacts/rpm /artifacts/rpm
132+
COPY --from=artifacts /artifacts/deb /artifacts/deb
133+
COPY --from=artifacts /artifacts/build /work
134+
135+
WORKDIR /work
77136
ENV PATH=/work:$PATH
78137

79138
ARG VERSION

deployments/container/Makefile

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,13 @@ $(IMAGE_TARGETS): image-%: $(ARTIFACTS_ROOT)
9090
--provenance=false --sbom=false \
9191
$(DOCKER_BUILD_OPTIONS) \
9292
$(DOCKER_BUILD_PLATFORM_OPTIONS) \
93+
$(INTERMEDIATE_TARGET) \
9394
--tag $(IMAGE) \
9495
--build-arg ARTIFACTS_ROOT="$(ARTIFACTS_ROOT)" \
9596
--build-arg GOLANG_VERSION="$(GOLANG_VERSION)" \
9697
--build-arg PACKAGE_DIST="$(PACKAGE_DIST)" \
98+
--build-arg PACKAGE_DIST_DEB="$(PACKAGE_DIST_DEB)" \
99+
--build-arg PACKAGE_DIST_RPM="$(PACKAGE_DIST_RPM)" \
97100
--build-arg PACKAGE_VERSION="$(PACKAGE_VERSION)" \
98101
--build-arg VERSION="$(VERSION)" \
99102
--build-arg GIT_COMMIT="$(GIT_COMMIT)" \
@@ -103,14 +106,19 @@ $(IMAGE_TARGETS): image-%: $(ARTIFACTS_ROOT)
103106
-f $(DOCKERFILE) \
104107
$(CURDIR)
105108

109+
110+
PACKAGE_DIST_DEB = ubuntu18.04
111+
# TODO: This needs to be set to centos8 for ppc64le builds
112+
PACKAGE_DIST_RPM = centos7
113+
106114
build-ubuntu%: DOCKERFILE_SUFFIX := ubuntu
107115
build-ubuntu%: PACKAGE_DIST = ubuntu18.04
108116

109117
build-ubi8: DOCKERFILE_SUFFIX := ubi8
110118
build-ubi8: PACKAGE_DIST = centos7
111119

112-
build-packaging: DOCKERFILE_SUFFIX := packaging
113-
build-packaging: PACKAGE_ARCH := amd64
120+
build-packaging: DOCKERFILE_SUFFIX := ubi8
121+
build-packaging: INTERMEDIATE_TARGET := --target=packaging
114122
build-packaging: PACKAGE_DIST = all
115123

116124
# Test targets

0 commit comments

Comments
 (0)