Skip to content

Commit 52be432

Browse files
committed
[no-relnote] Refactor Ldconfig construction
Signed-off-by: Evan Lezar <elezar@nvidia.com>
1 parent 8c75460 commit 52be432

File tree

3 files changed

+45
-49
lines changed

3 files changed

+45
-49
lines changed

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

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -142,25 +142,11 @@ func createSonameSymlinksHandler() {
142142
// It is invoked from a reexec'd handler and provides namespace isolation for
143143
// the operations performed by this hook. At the point where this is invoked,
144144
// we are in a new mount namespace that is cloned from the parent.
145-
//
146-
// args[0] is the reexec initializer function name
147-
// args[1] is the path of the ldconfig binary on the host
148-
// args[2] is the container root directory
149-
// The remaining args are directories where soname symlinks need to be created.
150145
func createSonameSymlinks(args []string) error {
151-
if len(args) < 3 {
152-
return fmt.Errorf("incorrect arguments: %v", args)
153-
}
154-
hostLdconfigPath := args[1]
155-
containerRootDirPath := args[2]
156-
157-
ldconfig, err := ldconfig.New(
158-
hostLdconfigPath,
159-
containerRootDirPath,
160-
)
146+
ldconfig, err := ldconfig.NewFromArgs(args...)
161147
if err != nil {
162148
return fmt.Errorf("failed to construct ldconfig runner: %w", err)
163149
}
164150

165-
return ldconfig.CreateSonameSymlinks(args[3:]...)
151+
return ldconfig.CreateSonameSymlinks()
166152
}

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

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -140,25 +140,11 @@ func updateLdCacheHandler() {
140140
// It is invoked from a reexec'd handler and provides namespace isolation for
141141
// the operations performed by this hook. At the point where this is invoked,
142142
// we are in a new mount namespace that is cloned from the parent.
143-
//
144-
// args[0] is the reexec initializer function name
145-
// args[1] is the path of the ldconfig binary on the host
146-
// args[2] is the container root directory
147-
// The remaining args are folders where soname symlinks need to be created.
148143
func updateLdCache(args []string) error {
149-
if len(args) < 3 {
150-
return fmt.Errorf("incorrect arguments: %v", args)
151-
}
152-
hostLdconfigPath := args[1]
153-
containerRootDirPath := args[2]
154-
155-
ldconfig, err := ldconfig.New(
156-
hostLdconfigPath,
157-
containerRootDirPath,
158-
)
144+
ldconfig, err := ldconfig.NewFromArgs(args...)
159145
if err != nil {
160146
return fmt.Errorf("failed to construct ldconfig runner: %w", err)
161147
}
162148

163-
return ldconfig.UpdateLDCache(args[3:]...)
149+
return ldconfig.UpdateLDCache()
164150
}

internal/ldconfig/ldconfig.go

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package ldconfig
1919

2020
import (
21+
"flag"
2122
"fmt"
2223
"os"
2324
"os/exec"
@@ -39,40 +40,63 @@ const (
3940
type Ldconfig struct {
4041
ldconfigPath string
4142
inRoot string
43+
directories []string
4244
}
4345

4446
// NewRunner creates an exec.Cmd that can be used to run ldconfig.
4547
func NewRunner(id string, ldconfigPath string, containerRoot string, additionalargs ...string) (*exec.Cmd, error) {
4648
args := []string{
4749
id,
48-
strings.TrimPrefix(config.NormalizeLDConfigPath("@"+ldconfigPath), "@"),
49-
containerRoot,
50+
"--ldconfig-path", strings.TrimPrefix(config.NormalizeLDConfigPath("@"+ldconfigPath), "@"),
51+
"--container-root", containerRoot,
5052
}
5153
args = append(args, additionalargs...)
5254

5355
return createReexecCommand(args)
5456
}
5557

56-
// New creates an Ldconfig struct that is used to perform operations on the
57-
// ldcache and libraries in a particular root (e.g. a container).
58-
func New(ldconfigPath string, inRoot string) (*Ldconfig, error) {
59-
l := &Ldconfig{
60-
ldconfigPath: ldconfigPath,
61-
inRoot: inRoot,
62-
}
63-
if ldconfigPath == "" {
58+
// NewFromArgs creates an Ldconfig struct from the args passed to the Cmd
59+
// above.
60+
// This struct is used to perform operations on the ldcache and libraries in a
61+
// particular root (e.g. a container).
62+
//
63+
// args[0] is the reexec initializer function name
64+
// The following flags are required:
65+
//
66+
// --ldconfig-path=LDCONFIG_PATH the path to ldconfig on the host
67+
// --container-root=CONTAINER_ROOT the path in which ldconfig must be run
68+
//
69+
// The remaining args are folders where soname symlinks need to be created.
70+
func NewFromArgs(args ...string) (*Ldconfig, error) {
71+
if len(args) < 1 {
72+
return nil, fmt.Errorf("incorrect arguments: %v", args)
73+
}
74+
fs := flag.NewFlagSet(args[1], flag.ExitOnError)
75+
ldconfigPath := fs.String("ldconfig-path", "", "the path to ldconfig on the host")
76+
containerRoot := fs.String("container-root", "", "the path in which ldconfig must be run")
77+
if err := fs.Parse(args[1:]); err != nil {
78+
return nil, err
79+
}
80+
81+
if *ldconfigPath == "" {
6482
return nil, fmt.Errorf("an ldconfig path must be specified")
6583
}
66-
if inRoot == "" || inRoot == "/" {
84+
if *containerRoot == "" || *containerRoot == "/" {
6785
return nil, fmt.Errorf("ldconfig must be run in the non-system root")
6886
}
87+
88+
l := &Ldconfig{
89+
ldconfigPath: *ldconfigPath,
90+
inRoot: *containerRoot,
91+
directories: fs.Args(),
92+
}
6993
return l, nil
7094
}
7195

7296
// CreateSonameSymlinks uses ldconfig to create the soname symlinks in the
7397
// specified directories.
74-
func (l *Ldconfig) CreateSonameSymlinks(directories ...string) error {
75-
if len(directories) == 0 {
98+
func (l *Ldconfig) CreateSonameSymlinks() error {
99+
if len(l.directories) == 0 {
76100
return nil
77101
}
78102
ldconfigPath, err := l.prepareRoot()
@@ -87,12 +111,12 @@ func (l *Ldconfig) CreateSonameSymlinks(directories ...string) error {
87111
// Specify -n to only process the specified directories.
88112
"-n",
89113
}
90-
args = append(args, directories...)
114+
args = append(args, l.directories...)
91115

92116
return SafeExec(ldconfigPath, args, nil)
93117
}
94118

95-
func (l *Ldconfig) UpdateLDCache(directories ...string) error {
119+
func (l *Ldconfig) UpdateLDCache() error {
96120
ldconfigPath, err := l.prepareRoot()
97121
if err != nil {
98122
return err
@@ -115,12 +139,12 @@ func (l *Ldconfig) UpdateLDCache(directories ...string) error {
115139
// containing the required directories, otherwise we add the specified
116140
// directories to the ldconfig command directly.
117141
if l.ldsoconfdDirectoryExists() {
118-
err := createLdsoconfdFile(ldsoconfdFilenamePattern, directories...)
142+
err := createLdsoconfdFile(ldsoconfdFilenamePattern, l.directories...)
119143
if err != nil {
120144
return fmt.Errorf("failed to update ld.so.conf.d: %w", err)
121145
}
122146
} else {
123-
args = append(args, directories...)
147+
args = append(args, l.directories...)
124148
}
125149

126150
return SafeExec(ldconfigPath, args, nil)

0 commit comments

Comments
 (0)