Skip to content

Commit 338f588

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 a2e2a44 commit 338f588

File tree

6 files changed

+155
-29
lines changed

6 files changed

+155
-29
lines changed

cmd/nvidia-ctk-installer/main.go

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@ var signalReceived = make(chan bool, 1)
3636
type options struct {
3737
toolkitInstallDir string
3838

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

4445
toolkitOptions toolkit.Options
4546
runtimeOptions runtime.Options
@@ -123,11 +124,18 @@ func (a app) build() *cli.App {
123124
EnvVars: []string{"TOOLKIT_INSTALL_DIR", "ROOT"},
124125
},
125126
&cli.StringFlag{
126-
Name: "source-root",
127+
Name: "toolkit-source-root",
127128
Value: "/",
128129
Usage: "The folder where the required toolkit artifacts can be found",
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 := 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,33 @@ func (a *app) shutdown(pidFile string) {
277294
a.logger.Warningf("Unable to remove pidfile: %v", err)
278295
}
279296
}
297+
298+
func resolveSourceRoot(hostRoot string, packageType string) (string, error) {
299+
resolvedPackageType, err := 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 resolvePackageType(hostRoot string, packageType string) (rPackageTypes string, rerr error) {
314+
if packageType != "" && packageType != "auto" {
315+
return packageType, nil
316+
}
317+
318+
if info, err := os.Stat(filepath.Join(hostRoot, "/usr/bin/rpm")); err != nil && !info.IsDir() {
319+
return "rpm", nil
320+
}
321+
if info, err := os.Stat(filepath.Join(hostRoot, "/usr/bin/dpkg")); err != nil && !info.IsDir() {
322+
return "deb", nil
323+
}
324+
325+
return "deb", nil
326+
}

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 {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
*
3+
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
*
18+
*/
19+
package installer

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: 68 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,33 +47,85 @@ 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+
FROM nvcr.io/nvidia/cuda:12.8.1-base-ubi8 AS packaging
5551

5652
ARG ARTIFACTS_ROOT
57-
ARG PACKAGE_DIST
58-
COPY ${ARTIFACTS_ROOT}/${PACKAGE_DIST} /artifacts/packages/${PACKAGE_DIST}
53+
COPY ${ARTIFACTS_ROOT} /artifacts/packages/
5954

6055
WORKDIR /artifacts/packages
6156

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

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
80+
COPY --from=packaging /artifacts/packages/${PACKAGE_DIST_RPM} /rpm-packages
7181

72-
WORKDIR /work
82+
RUN mkdir -p /artifacts/rpm
83+
RUN set -eux; \
84+
\
85+
case "${TARGETARCH}" in \
86+
x86_64 | amd64) ARCH='x86_64' ;; \
87+
ppc64el | ppc64le) ARCH='ppc64le' ;; \
88+
aarch64 | arm64) ARCH='aarch64' ;; \
89+
*) echo "unsupported architecture" ; exit 1 ;; \
90+
esac; \
91+
for p in $(ls /rpm-packages/${ARCH}/*.rpm); do rpm2cpio $p | cpio -idmv -D /artifacts/rpm; done
92+
93+
# The debpackages stage is used to extract the contents of deb packages.
94+
FROM nvcr.io/nvidia/cuda:12.8.1-base-ubuntu20.04 AS debpackages
7395

74-
COPY --from=build /artifacts/nvidia-ctk-installer /work/nvidia-ctk-installer
75-
RUN ln -s nvidia-ctk-installer nvidia-toolkit
96+
ARG TARGETARCH
97+
ARG PACKAGE_DIST_DEB=ubuntu18.04
98+
99+
COPY --from=packaging /artifacts/packages/${PACKAGE_DIST_DEB} /deb-packages
100+
101+
RUN mkdir -p /artifacts/deb
102+
RUN set -eux; \
103+
\
104+
case "${TARGETARCH}" in \
105+
x86_64 | amd64) ARCH='amd64' ;; \
106+
ppc64el | ppc64le) ARCH='ppc64le' ;; \
107+
aarch64 | arm64) ARCH='arm64' ;; \
108+
*) echo "unsupported architecture" ; exit 1 ;; \
109+
esac; \
110+
for p in $(ls /deb-packages/${ARCH}/*.deb); do dpkg-deb -xv $p /artifacts/deb/; done
76111

112+
FROM nvcr.io/nvidia/cuda:12.8.1-base-ubi8 AS artifacts
113+
114+
COPY --from=rpmpackages /artifacts/rpm /artifacts/rpm
115+
COPY --from=debpackages /artifacts/deb /artifacts/deb
116+
COPY --from=build /artifacts/bin /artifacts/build
117+
118+
FROM nvcr.io/nvidia/cuda:12.6.2-base-ubi8
119+
120+
ENV NVIDIA_DISABLE_REQUIRE="true"
121+
ENV NVIDIA_VISIBLE_DEVICES=void
122+
ENV NVIDIA_DRIVER_CAPABILITIES=utility
123+
124+
COPY --from=artifacts /artifacts/rpm /artifacts/rpm
125+
COPY --from=artifacts /artifacts/deb /artifacts/deb
126+
COPY --from=artifacts /artifacts/build /work
127+
128+
WORKDIR /work
77129
ENV PATH=/work:$PATH
78130

79131
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)