@@ -22,12 +22,11 @@ import (
22
22
"fmt"
23
23
"log"
24
24
"os"
25
- "strings"
26
25
27
26
"github.com/moby/sys/reexec"
28
27
"github.com/urfave/cli/v2"
29
28
30
- "github.com/NVIDIA/nvidia-container-toolkit/internal/config "
29
+ "github.com/NVIDIA/nvidia-container-toolkit/internal/ldconfig "
31
30
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
32
31
"github.com/NVIDIA/nvidia-container-toolkit/internal/oci"
33
32
)
@@ -117,14 +116,15 @@ func (m command) run(c *cli.Context, cfg *options) error {
117
116
return fmt .Errorf ("failed to determined container root: %v" , err )
118
117
}
119
118
120
- args := [] string {
119
+ cmd , err := ldconfig . NewRunner (
121
120
reexecUpdateLdCacheCommandName ,
122
- strings . TrimPrefix ( config . NormalizeLDConfigPath ( "@" + cfg .ldconfigPath ), "@" ) ,
121
+ cfg .ldconfigPath ,
123
122
containerRootDir ,
123
+ cfg .folders .Value ()... ,
124
+ )
125
+ if err != nil {
126
+ return err
124
127
}
125
- args = append (args , cfg .folders .Value ()... )
126
-
127
- cmd := createReexecCommand (args )
128
128
129
129
return cmd .Run ()
130
130
}
@@ -137,55 +137,30 @@ func createSonameSymlinksHandler() {
137
137
}
138
138
}
139
139
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.
144
145
//
145
146
// args[0] is the reexec initializer function name
146
147
// args[1] is the path of the ldconfig binary on the host
147
148
// 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 .
149
150
func createSonameSymlinks (args []string ) error {
150
151
if len (args ) < 3 {
151
152
return fmt .Errorf ("incorrect arguments: %v" , args )
152
153
}
153
154
hostLdconfigPath := args [1 ]
154
155
containerRootDirPath := args [2 ]
155
156
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
+ )
165
161
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 )
187
163
}
188
- args = append (args , directories ... )
189
164
190
- return SafeExec ( ldconfigPath , args , nil )
165
+ return ldconfig . CreateSonameSymlinks ( args [ 3 :] ... )
191
166
}
0 commit comments