Skip to content

Commit 6b3a5fa

Browse files
committed
Use a tmpfs to store the modified params file
Instead of creating the params file on the host, we use a tmpfs mounted into the container instead. Signed-off-by: Evan Lezar <[email protected]>
1 parent ba21d0e commit 6b3a5fa

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

cmd/nvidia-cdi-hook/update-nvidia-params/mount_linux.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,16 @@
2020
package nvidiaparams
2121

2222
import (
23+
"fmt"
24+
2325
"golang.org/x/sys/unix"
2426
)
2527

28+
func createTmpFs(target string, size uint64) error {
29+
// return unix.Mount("tmpfs", target, "tmpfs", 0, fmt.Sprintf("ro,nosuid,nodev,noexec,size=%d", size))
30+
return unix.Mount("tmpfs", target, "tmpfs", 0, fmt.Sprintf("size=%d", size))
31+
}
32+
2633
func bindMountReadonly(source string, target string) error {
2734
return unix.Mount(source, target, "", unix.MS_BIND|unix.MS_RDONLY|unix.MS_NOSYMFOLLOW, "")
28-
2935
}

cmd/nvidia-cdi-hook/update-nvidia-params/mount_other.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ import (
2323
"fmt"
2424
)
2525

26+
func createTmpFs(target string, size uint64) error {
27+
return fmt.Errorf("not supported")
28+
}
29+
2630
func bindMountReadonly(source string, target string) error {
2731
return fmt.Errorf("not supported")
2832
}

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ func (m command) updateNvidiaParamsFromReader(r io.Reader, containerRoot string)
115115
var newLines []string
116116
scanner := bufio.NewScanner(r)
117117
var requiresModification bool
118+
var requiredSize uint64
118119
for scanner.Scan() {
119120
line := scanner.Text()
120121
if strings.HasPrefix(line, "ModifyDeviceFiles: ") {
@@ -128,6 +129,7 @@ func (m command) updateNvidiaParamsFromReader(r io.Reader, containerRoot string)
128129
}
129130
}
130131
newLines = append(newLines, line)
132+
requiredSize += uint64(len(line) + 5)
131133
}
132134
if err := scanner.Err(); err != nil {
133135
return fmt.Errorf("failed to read params file: %w", err)
@@ -137,7 +139,15 @@ func (m command) updateNvidiaParamsFromReader(r io.Reader, containerRoot string)
137139
return nil
138140
}
139141

140-
containerParamsFile, err := os.CreateTemp("", "nvct-params-*")
142+
tmpRoot, err := os.MkdirTemp("", "nvct-empty-dir*")
143+
if err != nil {
144+
return fmt.Errorf("failed to create temporary folder: %w", err)
145+
}
146+
if err := createTmpFs(tmpRoot, requiredSize); err != nil {
147+
return fmt.Errorf("failed to create tmpfs mount for params file: %w", err)
148+
}
149+
150+
containerParamsFile, err := os.Create(filepath.Join(tmpRoot, "nvct-params"))
141151
if err != nil {
142152
return fmt.Errorf("failed to create temporary params file: %w", err)
143153
}
@@ -147,7 +157,7 @@ func (m command) updateNvidiaParamsFromReader(r io.Reader, containerRoot string)
147157
return fmt.Errorf("failed to write temporary params file: %w", err)
148158
}
149159

150-
if err := containerParamsFile.Chmod(0o644); err != nil {
160+
if err := containerParamsFile.Chmod(0o444); err != nil {
151161
return fmt.Errorf("failed to set permissions on temporary params file: %w", err)
152162
}
153163

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ containerEdits:
8787
- /lib/x86_64-linux-gnu
8888
hookName: createContainer
8989
path: {{ .toolkitRoot }}/nvidia-cdi-hook
90+
- args:
91+
- nvidia-cdi-hook
92+
- update-nvidia-params
93+
hookName: createContainer
94+
path: {{ .toolkitRoot }}/nvidia-cdi-hook
9095
mounts:
9196
- containerPath: /lib/x86_64-linux-gnu/libcuda.so.999.88.77
9297
hostPath: /host/driver/root/lib/x86_64-linux-gnu/libcuda.so.999.88.77

0 commit comments

Comments
 (0)