podman run/create -v relative filepath support

podman currently does not support relative volume paths. Add parsing for relative paths in specgen, converting
whatever volume was given to an absolute path.

Signed-off-by: Charlie Doern <cdoern@redhat.com>
This commit is contained in:
Charlie Doern
2022-06-21 15:41:10 -04:00
parent 2382955c6a
commit 2eda547dcd
2 changed files with 43 additions and 1 deletions

View File

@ -1,6 +1,7 @@
package specgen package specgen
import ( import (
"path/filepath"
"strings" "strings"
"github.com/containers/common/pkg/parse" "github.com/containers/common/pkg/parse"
@ -56,7 +57,6 @@ func GenVolumeMounts(volumeFlag []string) (map[string]spec.Mount, map[string]*Na
overlayVolumes := make(map[string]*OverlayVolume) overlayVolumes := make(map[string]*OverlayVolume)
volumeFormatErr := errors.Errorf("incorrect volume format, should be [host-dir:]ctr-dir[:option]") volumeFormatErr := errors.Errorf("incorrect volume format, should be [host-dir:]ctr-dir[:option]")
for _, vol := range volumeFlag { for _, vol := range volumeFlag {
var ( var (
options []string options []string
@ -71,6 +71,20 @@ func GenVolumeMounts(volumeFlag []string) (map[string]spec.Mount, map[string]*Na
} }
src = splitVol[0] src = splitVol[0]
// Support relative paths beginning with ./
if strings.HasPrefix(src, "./") {
path, err := filepath.EvalSymlinks(src)
if err != nil {
return nil, nil, nil, err
}
src, err = filepath.Abs(path)
if err != nil {
return nil, nil, nil, err
}
splitVol[0] = src
}
if len(splitVol) == 1 { if len(splitVol) == 1 {
// This is an anonymous named volume. Only thing given // This is an anonymous named volume. Only thing given
// is destination. // is destination.

View File

@ -953,4 +953,32 @@ USER testuser`, fedoraMinimal)
Expect(volMount).Should(Exit(0)) Expect(volMount).Should(Exit(0))
Expect(volMount.OutputToString()).To(Equal("1000:1000")) Expect(volMount.OutputToString()).To(Equal("1000:1000"))
}) })
It("podman run -v with a relative dir", func() {
mountPath := filepath.Join(podmanTest.TempDir, "vol")
err = os.Mkdir(mountPath, 0755)
Expect(err).ToNot(HaveOccurred())
defer func() {
err := os.RemoveAll(mountPath)
Expect(err).ToNot(HaveOccurred())
}()
f, err := os.CreateTemp(mountPath, "podman")
Expect(err).ToNot(HaveOccurred())
cwd, err := os.Getwd()
Expect(err).ToNot(HaveOccurred())
err = os.Chdir(mountPath)
Expect(err).ToNot(HaveOccurred())
defer func() {
err := os.Chdir(cwd)
Expect(err).ToNot(HaveOccurred())
}()
run := podmanTest.Podman([]string{"run", "-it", "--security-opt", "label=disable", "-v", "./:" + dest, ALPINE, "ls", dest})
run.WaitWithDefaultTimeout()
Expect(run).Should(Exit(0))
Expect(run.OutputToString()).Should(ContainSubstring(strings.TrimLeft("/vol/", f.Name())))
})
}) })