Skip to content

Commit

Permalink
config: Allow specifying helper dirs with $BINDIR as base directory
Browse files Browse the repository at this point in the history
This should make it easier to locate helper binaries relative to where the main
binary was installed, which should be useful in installations such as Homebrew
which install packages under a versioned directory.

Use a `$BINDIR` magic token as a prefix in the helper path to indicate it should
be relative to the directory where the binary is located. This is somewhat familiar
to the syntax used in the shell and Makefile and is still quite explicit about the
behavior (as opposed to, say, making all relative paths be relative to the directory
of the binary.)

Update `podman` config on Darwin to look for helpers such as `gvproxy` under
`$BINDIR/../libexec/podman`, which is the ultimate objective of this code change.

Tested: Updated vendored package in podman, built it with `podman-remote`,
copied `gvproxy` to a `libexec/podman` at the same level as `bin/podman` and
confirmed that `podman machine start` worked as expected.

Fixes: containers/podman#12161

Related: PR containers#946

Signed-off-by: Filipe Brandenburger <[email protected]>
  • Loading branch information
filbranden committed Aug 22, 2022
1 parent 6946042 commit 49192b9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
27 changes: 27 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ const (
_configPath = "containers/containers.conf"
// UserOverrideContainersConfig holds the containers config path overridden by the rootless user
UserOverrideContainersConfig = ".config/" + _configPath
// Token prefix for looking for helper binary under $BINDIR
bindirPrefix = "$BINDIR"
)

// RuntimeStateStore is a constant indicating which state store implementation
Expand Down Expand Up @@ -1245,13 +1247,35 @@ func (c *Config) ActiveDestination() (uri, identity string, err error) {
// If searchPATH is set to true it will also search in $PATH.
func (c *Config) FindHelperBinary(name string, searchPATH bool) (string, error) {
dirList := c.Engine.HelperBinariesDir
var bindirPath string
var bindirErr error

// If set, search this directory first. This is used in testing.
if dir, found := os.LookupEnv("CONTAINERS_HELPER_BINARY_DIR"); found {
dirList = append([]string{dir}, dirList...)
}

for _, path := range dirList {
if path == bindirPrefix || strings.HasPrefix(path, bindirPrefix+string(filepath.Separator)) {
// Calculate the path to the executable first time we encounter a $BINDIR prefix.
if bindirPath == "" && bindirErr == nil {
podman, bindirErr := os.Executable()
if bindirErr == nil {
bindirPath = filepath.Dir(podman)
}
}
// If there's an error, don't stop the search for the helper binary, but store the
// error message to include in the final error result if one is returned.
if bindirPath == "" {
continue
}
// Replace the $BINDIR prefix with the path to the directory of the current binary.
if path == bindirPrefix {
path = bindirPath
} else {
path = filepath.Join(bindirPath, strings.TrimPrefix(path, bindirPrefix+string(filepath.Separator)))
}
}
fullpath := filepath.Join(path, name)
if fi, err := os.Stat(fullpath); err == nil && fi.Mode().IsRegular() {
return fullpath, nil
Expand All @@ -1261,6 +1285,9 @@ func (c *Config) FindHelperBinary(name string, searchPATH bool) (string, error)
return exec.LookPath(name)
}
configHint := "To resolve this error, set the helper_binaries_dir key in the `[engine]` section of containers.conf to the directory containing your helper binaries."
if bindirErr != nil {
configHint = fmt.Sprintf("Error looking up path relative to %s: [%s] %s", bindirPrefix, bindirErr, configHint)
}
if len(c.Engine.HelperBinariesDir) == 0 {
return "", fmt.Errorf("could not find %q because there are no helper binary directories configured. %s", name, configHint)
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/config/config_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,6 @@ var defaultHelperBinariesDir = []string{
"/usr/local/lib/podman",
"/usr/libexec/podman",
"/usr/lib/podman",
// Relative to the binary directory
"$BINDIR/../libexec/podman",
}

0 comments on commit 49192b9

Please sign in to comment.