Skip to content

Commit bedd086

Browse files
committed
[no-relnote] Refactor ldconfig hooks
Signed-off-by: Evan Lezar <[email protected]>
1 parent 0a6d383 commit bedd086

File tree

12 files changed

+250
-556
lines changed

12 files changed

+250
-556
lines changed

cmd/nvidia-cdi-hook/create-soname-symlinks/ldconfig_other.go

Lines changed: 0 additions & 51 deletions
This file was deleted.

cmd/nvidia-cdi-hook/create-soname-symlinks/safe-exec_linux.go

Lines changed: 0 additions & 58 deletions
This file was deleted.

cmd/nvidia-cdi-hook/create-soname-symlinks/safe-exec_other.go

Lines changed: 0 additions & 28 deletions
This file was deleted.

cmd/nvidia-cdi-hook/create-soname-symlinks/soname-symlinks.go

Lines changed: 19 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,11 @@ import (
2222
"fmt"
2323
"log"
2424
"os"
25-
"strings"
2625

2726
"github.com/moby/sys/reexec"
2827
"github.com/urfave/cli/v2"
2928

30-
"github.com/NVIDIA/nvidia-container-toolkit/internal/config"
29+
"github.com/NVIDIA/nvidia-container-toolkit/internal/ldconfig"
3130
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
3231
"github.com/NVIDIA/nvidia-container-toolkit/internal/oci"
3332
)
@@ -117,14 +116,15 @@ func (m command) run(c *cli.Context, cfg *options) error {
117116
return fmt.Errorf("failed to determined container root: %v", err)
118117
}
119118

120-
args := []string{
119+
cmd, err := ldconfig.NewRunner(
121120
reexecUpdateLdCacheCommandName,
122-
strings.TrimPrefix(config.NormalizeLDConfigPath("@"+cfg.ldconfigPath), "@"),
121+
cfg.ldconfigPath,
123122
containerRootDir,
123+
cfg.folders.Value()...,
124+
)
125+
if err != nil {
126+
return err
124127
}
125-
args = append(args, cfg.folders.Value()...)
126-
127-
cmd := createReexecCommand(args)
128128

129129
return cmd.Run()
130130
}
@@ -137,55 +137,30 @@ func createSonameSymlinksHandler() {
137137
}
138138
}
139139

140-
// createSonameSymlinks is invoked from a reexec'd handler and provides namespace
141-
// isolation for the operations performed by this hook.
142-
// At the point where this is invoked, we are in a new mount namespace that is
143-
// cloned from the parent.
140+
// createSonameSymlinks runs ldconfig enusures that soname symlinks are created
141+
// in the specified directories.
142+
// It is invoked from a reexec'd handler and provides namespace isolation for
143+
// the operations performed by this hook. At the point where this is invoked,
144+
// we are in a new mount namespace that is cloned from the parent.
144145
//
145146
// args[0] is the reexec initializer function name
146147
// args[1] is the path of the ldconfig binary on the host
147148
// args[2] is the container root directory
148-
// The remaining args are folders that need to be added to the ldcache.
149+
// The remaining args are folders where soname symlinks need to be created.
149150
func createSonameSymlinks(args []string) error {
150151
if len(args) < 3 {
151152
return fmt.Errorf("incorrect arguments: %v", args)
152153
}
153154
hostLdconfigPath := args[1]
154155
containerRootDirPath := args[2]
155156

156-
// To prevent leaking the parent proc filesystem, we create a new proc mount
157-
// in the container root.
158-
if err := mountProc(containerRootDirPath); err != nil {
159-
return fmt.Errorf("error mounting /proc: %w", err)
160-
}
161-
162-
// We mount the host ldconfig before we pivot root since host paths are not
163-
// visible after the pivot root operation.
164-
ldconfigPath, err := mountLdConfig(hostLdconfigPath, containerRootDirPath)
157+
ldconfig, err := ldconfig.New(
158+
hostLdconfigPath,
159+
containerRootDirPath,
160+
)
165161
if err != nil {
166-
return fmt.Errorf("error mounting host ldconfig: %w", err)
167-
}
168-
169-
// We pivot to the container root for the new process, this further limits
170-
// access to the host.
171-
if err := pivotRoot(containerRootDirPath); err != nil {
172-
return fmt.Errorf("error running pivot_root: %w", err)
173-
}
174-
175-
return runLdconfig(ldconfigPath, args[3:]...)
176-
}
177-
178-
// runLdconfig runs the ldconfig binary and ensures that soname symlinks are
179-
// created in the specified directories.
180-
func runLdconfig(ldconfigPath string, directories ...string) error {
181-
args := []string{
182-
"ldconfig",
183-
// Specify -n to only process the specified folders.
184-
"-n",
185-
// Explicitly disable updating the LDCache.
186-
"-N",
162+
return fmt.Errorf("failed to construct ldconfig runner: %w", err)
187163
}
188-
args = append(args, directories...)
189164

190-
return SafeExec(ldconfigPath, args, nil)
165+
return ldconfig.CreateSonameSymlinks(args[3:]...)
191166
}

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

Lines changed: 0 additions & 46 deletions
This file was deleted.

0 commit comments

Comments
 (0)