Skip to content

Commit af89fb0

Browse files
committed
Move CreateLdsoconfdFile to ContainerRoot
Signed-off-by: Evan Lezar <[email protected]>
1 parent 03a1b23 commit af89fb0

File tree

5 files changed

+111
-138
lines changed

5 files changed

+111
-138
lines changed

cmd/nvidia-cdi-hook/cudacompat/cudacompat.go

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package cudacompat
1818

1919
import (
2020
"fmt"
21-
"os"
2221
"path/filepath"
2322
"strconv"
2423
"strings"
@@ -118,7 +117,7 @@ func (m command) run(_ *cli.Context, cfg *options) error {
118117
return nil
119118
}
120119

121-
return m.createLdsoconfdFile(containerRoot, cudaCompatLdsoconfdFilenamePattern, containerForwardCompatDir)
120+
return containerRoot.CreateLdsoconfdFile(cudaCompatLdsoconfdFilenamePattern, containerForwardCompatDir)
122121
}
123122

124123
func (m command) getContainerForwardCompatDir(containerRoot utils.ContainerRoot, hostDriverVersion string) (string, error) {
@@ -171,51 +170,6 @@ func (m command) getContainerForwardCompatDir(containerRoot utils.ContainerRoot,
171170
return resolvedCompatDir, nil
172171
}
173172

174-
// createLdsoconfdFile creates a file at /etc/ld.so.conf.d/ in the specified root.
175-
// The file is created at /etc/ld.so.conf.d/{{ .pattern }} using `CreateTemp` and
176-
// contains the specified directories on each line.
177-
func (m command) createLdsoconfdFile(in utils.ContainerRoot, pattern string, dirs ...string) error {
178-
if len(dirs) == 0 {
179-
m.logger.Debugf("No directories to add to /etc/ld.so.conf")
180-
return nil
181-
}
182-
183-
ldsoconfdDir, err := in.Resolve("/etc/ld.so.conf.d")
184-
if err != nil {
185-
return err
186-
}
187-
if err := os.MkdirAll(ldsoconfdDir, 0755); err != nil {
188-
return fmt.Errorf("failed to create ld.so.conf.d: %w", err)
189-
}
190-
191-
configFile, err := os.CreateTemp(ldsoconfdDir, pattern)
192-
if err != nil {
193-
return fmt.Errorf("failed to create config file: %w", err)
194-
}
195-
defer configFile.Close()
196-
197-
m.logger.Debugf("Adding directories %v to %v", dirs, configFile.Name())
198-
199-
added := make(map[string]bool)
200-
for _, dir := range dirs {
201-
if added[dir] {
202-
continue
203-
}
204-
_, err = configFile.WriteString(fmt.Sprintf("%s\n", dir))
205-
if err != nil {
206-
return fmt.Errorf("failed to update config file: %w", err)
207-
}
208-
added[dir] = true
209-
}
210-
211-
// The created file needs to be world readable for the cases where the container is run as a non-root user.
212-
if err := configFile.Chmod(0644); err != nil {
213-
return fmt.Errorf("failed to chmod config file: %w", err)
214-
}
215-
216-
return nil
217-
}
218-
219173
// extractMajorVersion parses a version string and returns the major version as an int.
220174
func extractMajorVersion(version string) (int, error) {
221175
majorString := strings.SplitN(version, ".", 2)[0]

cmd/nvidia-cdi-hook/cudacompat/cudacompat_test.go

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -138,47 +138,3 @@ func TestCompatLibs(t *testing.T) {
138138
})
139139
}
140140
}
141-
142-
func TestUpdateLdconfig(t *testing.T) {
143-
logger, _ := testlog.NewNullLogger()
144-
testCases := []struct {
145-
description string
146-
folders []string
147-
expectedContents string
148-
}{
149-
{
150-
description: "no folders; have no contents",
151-
},
152-
{
153-
description: "single folder is added",
154-
folders: []string{"/usr/local/cuda/compat"},
155-
expectedContents: "/usr/local/cuda/compat\n",
156-
},
157-
}
158-
159-
for _, tc := range testCases {
160-
t.Run(tc.description, func(t *testing.T) {
161-
containerRootDir := t.TempDir()
162-
c := command{
163-
logger: logger,
164-
}
165-
err := c.createLdsoconfdFile(utils.ContainerRoot(containerRootDir), cudaCompatLdsoconfdFilenamePattern, tc.folders...)
166-
require.NoError(t, err)
167-
168-
matches, err := filepath.Glob(filepath.Join(containerRootDir, "/etc/ld.so.conf.d/00-compat-*.conf"))
169-
require.NoError(t, err)
170-
171-
if tc.expectedContents == "" {
172-
require.Empty(t, matches)
173-
return
174-
}
175-
176-
require.Len(t, matches, 1)
177-
contents, err := os.ReadFile(matches[0])
178-
require.NoError(t, err)
179-
180-
require.EqualValues(t, tc.expectedContents, string(contents))
181-
})
182-
}
183-
184-
}

cmd/nvidia-cdi-hook/update-ldcache/update-ldcache.go

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package ldcache
1919
import (
2020
"errors"
2121
"fmt"
22-
"os"
2322
"path/filepath"
2423
"strings"
2524

@@ -131,7 +130,7 @@ func (m command) run(c *cli.Context, cfg *options) error {
131130

132131
folders := cfg.folders.Value()
133132
if containerRoot.HasPath("/etc/ld.so.conf.d") {
134-
err := m.createLdsoconfdFile(containerRoot, ldsoconfdFilenamePattern, folders...)
133+
err := containerRoot.CreateLdsoconfdFile(ldsoconfdFilenamePattern, folders...)
135134
if err != nil {
136135
return fmt.Errorf("failed to update ld.so.conf.d: %v", err)
137136
}
@@ -152,48 +151,3 @@ func (m command) run(c *cli.Context, cfg *options) error {
152151
func (m command) resolveLDConfigPath(path string) string {
153152
return strings.TrimPrefix(config.NormalizeLDConfigPath("@"+path), "@")
154153
}
155-
156-
// createLdsoconfdFile creates a file at /etc/ld.so.conf.d/ in the specified root.
157-
// The file is created at /etc/ld.so.conf.d/{{ .pattern }} using `CreateTemp` and
158-
// contains the specified directories on each line.
159-
func (m command) createLdsoconfdFile(in utils.ContainerRoot, pattern string, dirs ...string) error {
160-
if len(dirs) == 0 {
161-
m.logger.Debugf("No directories to add to /etc/ld.so.conf")
162-
return nil
163-
}
164-
165-
ldsoconfdDir, err := in.Resolve("/etc/ld.so.conf.d")
166-
if err != nil {
167-
return err
168-
}
169-
if err := os.MkdirAll(ldsoconfdDir, 0755); err != nil {
170-
return fmt.Errorf("failed to create ld.so.conf.d: %w", err)
171-
}
172-
173-
configFile, err := os.CreateTemp(ldsoconfdDir, pattern)
174-
if err != nil {
175-
return fmt.Errorf("failed to create config file: %w", err)
176-
}
177-
defer configFile.Close()
178-
179-
m.logger.Debugf("Adding directories %v to %v", dirs, configFile.Name())
180-
181-
added := make(map[string]bool)
182-
for _, dir := range dirs {
183-
if added[dir] {
184-
continue
185-
}
186-
_, err = configFile.WriteString(fmt.Sprintf("%s\n", dir))
187-
if err != nil {
188-
return fmt.Errorf("failed to update config file: %w", err)
189-
}
190-
added[dir] = true
191-
}
192-
193-
// The created file needs to be world readable for the cases where the container is run as a non-root user.
194-
if err := configFile.Chmod(0644); err != nil {
195-
return fmt.Errorf("failed to chmod config file: %w", err)
196-
}
197-
198-
return nil
199-
}

cmd/nvidia-cdi-hook/utils/container-root.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package utils
1818

1919
import (
20+
"fmt"
2021
"os"
2122
"path/filepath"
2223

@@ -26,6 +27,48 @@ import (
2627
// A ContainerRoot represents the root filesystem of a container.
2728
type ContainerRoot string
2829

30+
// CreateLdsoconfdFile creates a file at /etc/ld.so.conf.d/ in the specified container root.
31+
// The file is created at /etc/ld.so.conf.d/{{ .pattern }} using `CreateTemp` and
32+
// contains the specified directories on each line.
33+
func (r ContainerRoot) CreateLdsoconfdFile(pattern string, dirs ...string) error {
34+
if len(dirs) == 0 {
35+
return nil
36+
}
37+
38+
ldsoconfdDir, err := r.Resolve("/etc/ld.so.conf.d")
39+
if err != nil {
40+
return err
41+
}
42+
if err := os.MkdirAll(ldsoconfdDir, 0755); err != nil {
43+
return fmt.Errorf("failed to create ld.so.conf.d: %w", err)
44+
}
45+
46+
configFile, err := os.CreateTemp(ldsoconfdDir, pattern)
47+
if err != nil {
48+
return fmt.Errorf("failed to create config file: %w", err)
49+
}
50+
defer configFile.Close()
51+
52+
added := make(map[string]bool)
53+
for _, dir := range dirs {
54+
if added[dir] {
55+
continue
56+
}
57+
_, err = configFile.WriteString(fmt.Sprintf("%s\n", dir))
58+
if err != nil {
59+
return fmt.Errorf("failed to update config file: %w", err)
60+
}
61+
added[dir] = true
62+
}
63+
64+
// The created file needs to be world readable for the cases where the container is run as a non-root user.
65+
if err := configFile.Chmod(0644); err != nil {
66+
return fmt.Errorf("failed to chmod config file: %w", err)
67+
}
68+
69+
return nil
70+
}
71+
2972
// GlobFiles matches the specified pattern in the container root.
3073
// The files that match must be regular files.
3174
func (r ContainerRoot) GlobFiles(pattern string) ([]string, error) {
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
**/
16+
17+
package utils
18+
19+
import (
20+
"os"
21+
"path/filepath"
22+
"testing"
23+
24+
"github.com/stretchr/testify/require"
25+
)
26+
27+
func TestUpdateLdconfig(t *testing.T) {
28+
testCases := []struct {
29+
description string
30+
folders []string
31+
expectedContents string
32+
}{
33+
{
34+
description: "no folders; have no contents",
35+
},
36+
{
37+
description: "single folder is added",
38+
folders: []string{"/usr/local/cuda/compat"},
39+
expectedContents: "/usr/local/cuda/compat\n",
40+
},
41+
}
42+
43+
for _, tc := range testCases {
44+
t.Run(tc.description, func(t *testing.T) {
45+
containerRootDir := t.TempDir()
46+
c := ContainerRoot(containerRootDir)
47+
err := c.CreateLdsoconfdFile("00-compat-*.conf", tc.folders...)
48+
require.NoError(t, err)
49+
50+
matches, err := filepath.Glob(filepath.Join(containerRootDir, "/etc/ld.so.conf.d/00-compat-*.conf"))
51+
require.NoError(t, err)
52+
53+
if tc.expectedContents == "" {
54+
require.Empty(t, matches)
55+
return
56+
}
57+
58+
require.Len(t, matches, 1)
59+
contents, err := os.ReadFile(matches[0])
60+
require.NoError(t, err)
61+
62+
require.EqualValues(t, tc.expectedContents, string(contents))
63+
})
64+
}
65+
66+
}

0 commit comments

Comments
 (0)