Skip to content

Commit 20e4dca

Browse files
committed
implement golang wrapper to replace shell scripts
Some platforms and Kubernetes distributions do not include a shell. This patch replaces the shell wrapper scripts with a small Go program.
1 parent 5827434 commit 20e4dca

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed

tools/container/wrapper/wrapper.go

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/**
2+
# Copyright (c) 2024, 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 main
18+
19+
import (
20+
"bufio"
21+
"errors"
22+
"io/fs"
23+
"log"
24+
"os"
25+
"os/exec"
26+
"path/filepath"
27+
"strings"
28+
29+
"golang.org/x/sys/unix"
30+
)
31+
32+
func main() {
33+
program, err := os.Executable()
34+
if err != nil {
35+
log.Fatalf("failed to get executable: %v", err)
36+
}
37+
if isRuntimeWrapper(program) && !isNvidiaModuleLoaded() {
38+
log.Println("nvidia driver modules are not yet loaded, invoking runc directly")
39+
program, err := exec.LookPath("runc")
40+
if err != nil {
41+
log.Fatalf("failed to find runc: %v", err)
42+
}
43+
argv := []string{"runc"}
44+
argv = append(argv, os.Args[1:]...)
45+
execve(program, argv, os.Environ())
46+
}
47+
argv := makeArgv(program)
48+
envv := makeEnvv(program)
49+
execve(program+".real", argv, envv)
50+
}
51+
52+
func isRuntimeWrapper(program string) bool {
53+
return filepath.Base(program) == "nvidia-container-runtime" ||
54+
filepath.Base(program) == "nvidia-container-runtime.cdi" ||
55+
filepath.Base(program) == "nvidia-container-runtime.legacy"
56+
}
57+
58+
func isNvidiaModuleLoaded() bool {
59+
_, err := os.Stat("/proc/driver/nvidia/version")
60+
return err == nil
61+
}
62+
63+
func makeArgv(program string) []string {
64+
argv := []string{os.Args[0] + ".real"}
65+
f, err := os.Open(program + ".argv")
66+
if err != nil {
67+
if !errors.Is(err, fs.ErrNotExist) {
68+
log.Printf("failed to open argv file: %v", err)
69+
}
70+
return append(argv, os.Args[1:]...)
71+
}
72+
defer f.Close()
73+
scanner := bufio.NewScanner(f)
74+
for scanner.Scan() {
75+
argv = append(argv, scanner.Text())
76+
}
77+
if err := scanner.Err(); err != nil {
78+
log.Fatalf("failed to read argv file: %v", err)
79+
}
80+
return append(argv, os.Args[1:]...)
81+
}
82+
83+
func makeEnvv(program string) []string {
84+
f, err := os.Open(program + ".envv")
85+
if err != nil {
86+
if !errors.Is(err, fs.ErrNotExist) {
87+
log.Printf("failed to open env file: %v", err)
88+
}
89+
return os.Environ()
90+
}
91+
defer f.Close()
92+
var env []string
93+
scanner := bufio.NewScanner(f)
94+
for scanner.Scan() {
95+
kv := strings.SplitN(scanner.Text(), "=", 2)
96+
if strings.HasPrefix(kv[0], "<") {
97+
kv[0] = kv[0][1:]
98+
kv[1] = kv[1] + ":" + os.Getenv(kv[0])
99+
} else if strings.HasPrefix(kv[0], ">") {
100+
kv[0] = kv[0][1:]
101+
kv[1] = os.Getenv(kv[0]) + ":" + kv[1]
102+
}
103+
env = append(env, kv[0]+"="+kv[1])
104+
}
105+
if err := scanner.Err(); err != nil {
106+
log.Fatalf("failed to read argv file: %v", err)
107+
}
108+
return append(env, os.Environ()...)
109+
}
110+
111+
func execve(program string, argv []string, envv []string) {
112+
if err := unix.Exec(program, argv, envv); err != nil {
113+
log.Fatalf("failed to exec %s: %v", program, err)
114+
}
115+
}

0 commit comments

Comments
 (0)