From cdd6f7e4d5b6a142172b6a3700ee00aad0bc8055 Mon Sep 17 00:00:00 2001 From: Gabi Beyer Date: Thu, 11 Jul 2019 19:32:45 +0000 Subject: [PATCH] katautils: update paths to be configurable for rootless execution Before using the default ctrsMapTrePath, check whether the runtime is being ran rootlessly, and if so set the ctrsMapTreePath to the rootlessRuntimeDir configured by the libpod rootless library. Fixes: #1827 Signed-off-by: Gabi Beyer --- pkg/katautils/oci.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/pkg/katautils/oci.go b/pkg/katautils/oci.go index 0eb80d5846..ad0eaf2e0b 100644 --- a/pkg/katautils/oci.go +++ b/pkg/katautils/oci.go @@ -11,6 +11,9 @@ import ( "io/ioutil" "os" "path/filepath" + "strings" + + "github.com/kata-containers/runtime/pkg/rootless" ) const ctrsMappingDirMode = os.FileMode(0750) @@ -22,6 +25,11 @@ func SetCtrsMapTreePath(path string) { ctrsMapTreePath = path } +// doUpdatePath returns whether a ctrsMapTreePath needs to be updated with a rootless prefix +func doUpdatePath() bool { + return rootless.IsRootless() && !strings.HasPrefix(ctrsMapTreePath, rootless.GetRootlessDir()) +} + // FetchContainerIDMapping This function assumes it should find only one file inside the container // ID directory. If there are several files, we could not determine which // file name corresponds to the sandbox ID associated, and this would throw @@ -31,6 +39,10 @@ func FetchContainerIDMapping(containerID string) (string, error) { return "", fmt.Errorf("Missing container ID") } + if doUpdatePath() { + SetCtrsMapTreePath(filepath.Join(rootless.GetRootlessDir(), ctrsMapTreePath)) + } + dirPath := filepath.Join(ctrsMapTreePath, containerID) files, err := ioutil.ReadDir(dirPath) @@ -62,6 +74,9 @@ func AddContainerIDMapping(ctx context.Context, containerID, sandboxID string) e return fmt.Errorf("Missing sandbox ID") } + if doUpdatePath() { + SetCtrsMapTreePath(filepath.Join(rootless.GetRootlessDir(), ctrsMapTreePath)) + } parentPath := filepath.Join(ctrsMapTreePath, containerID) if err := os.RemoveAll(parentPath); err != nil { @@ -86,6 +101,9 @@ func DelContainerIDMapping(ctx context.Context, containerID string) error { return fmt.Errorf("Missing container ID") } + if doUpdatePath() { + SetCtrsMapTreePath(filepath.Join(rootless.GetRootlessDir(), ctrsMapTreePath)) + } path := filepath.Join(ctrsMapTreePath, containerID) return os.RemoveAll(path)