Skip to content

Commit

Permalink
Merge pull request #14301 from rhatdan/volume
Browse files Browse the repository at this point in the history
Support setting image_volume_mode in containers.conf
  • Loading branch information
openshift-merge-robot authored Jun 2, 2022
2 parents 8b972ff + fb16397 commit 13cdf86
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 44 deletions.
5 changes: 2 additions & 3 deletions cmd/podman/common/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,8 @@ func DefineCreateFlags(cmd *cobra.Command, cf *entities.ContainerCreateOptions,
_ = cmd.RegisterFlagCompletionFunc(hostUserFlagName, completion.AutocompleteNone)

imageVolumeFlagName := "image-volume"
createFlags.StringVar(
&cf.ImageVolume,
imageVolumeFlagName, DefaultImageVolume,
createFlags.String(
imageVolumeFlagName, containerConfig.Engine.ImageVolumeMode,
`Tells podman how to handle the builtin image volumes ("bind"|"tmpfs"|"ignore")`,
)
_ = cmd.RegisterFlagCompletionFunc(imageVolumeFlagName, AutocompleteImageVolume)
Expand Down
3 changes: 0 additions & 3 deletions cmd/podman/common/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ import (
)

var (

// DefaultImageVolume default value
DefaultImageVolume = "bind"
// Pull in configured json library
json = registry.JSONLibrary()
)
19 changes: 14 additions & 5 deletions cmd/podman/containers/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,25 @@ func init() {
createFlags(containerCreateCommand)
}

func create(cmd *cobra.Command, args []string) error {
var (
err error
)
func commonFlags(cmd *cobra.Command) error {
var err error
flags := cmd.Flags()
cliVals.Net, err = common.NetFlagsToNetOptions(nil, *flags)
if err != nil {
return err
}

if cmd.Flags().Changed("image-volume") {
cliVals.ImageVolume = cmd.Flag("image-volume").Value.String()
}
return nil
}

func create(cmd *cobra.Command, args []string) error {
if err := commonFlags(cmd); err != nil {
return err
}

// Check if initctr is used with --pod and the value is correct
if initctr := InitContainerType; cmd.Flags().Changed("init-ctr") {
if !cmd.Flags().Changed("pod") {
Expand All @@ -123,7 +132,7 @@ func create(cmd *cobra.Command, args []string) error {
cliVals.InitContainerType = initctr
}

cliVals, err = CreateInit(cmd, cliVals, false)
cliVals, err := CreateInit(cmd, cliVals, false)
if err != nil {
return err
}
Expand Down
12 changes: 5 additions & 7 deletions cmd/podman/containers/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ func init() {
}

func run(cmd *cobra.Command, args []string) error {
var err error
if err := commonFlags(cmd); err != nil {
return err
}

// TODO: Breaking change should be made fatal in next major Release
if cliVals.TTY && cliVals.Interactive && !term.IsTerminal(int(os.Stdin.Fd())) {
Expand All @@ -122,14 +124,10 @@ func run(cmd *cobra.Command, args []string) error {
}
}

flags := cmd.Flags()
cliVals.Net, err = common.NetFlagsToNetOptions(nil, *flags)
if err != nil {
return err
}
runOpts.CIDFile = cliVals.CIDFile
runOpts.Rm = cliVals.Rm
if cliVals, err = CreateInit(cmd, cliVals, false); err != nil {
cliVals, err := CreateInit(cmd, cliVals, false)
if err != nil {
return err
}

Expand Down
17 changes: 1 addition & 16 deletions pkg/specgenutil/createparse.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,5 @@ func validate(c *entities.ContainerCreateOptions) error {
return err
}

var imageVolType = map[string]string{
"bind": "",
"tmpfs": "",
"ignore": "",
}
if _, ok := imageVolType[c.ImageVolume]; !ok {
switch {
case c.IsInfra:
c.ImageVolume = "bind"
case c.IsClone: // the image volume type will be deduced later from the container we are cloning
return nil
default:
return errors.Errorf("invalid image-volume type %q. Pick one of bind, tmpfs, or ignore", c.ImageVolume)
}
}
return nil
return config.ValidateImageVolumeMode(c.ImageVolume)
}
22 changes: 12 additions & 10 deletions pkg/specgenutil/specgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,11 @@ func setNamespaces(s *specgen.SpecGenerator, c *entities.ContainerCreateOptions)
}

func FillOutSpecGen(s *specgen.SpecGenerator, c *entities.ContainerCreateOptions, args []string) error {
var (
err error
)
rtc, err := config.Default()
if err != nil {
return err
}

// validate flags as needed
if err := validate(c); err != nil {
return err
Expand Down Expand Up @@ -479,8 +481,13 @@ func FillOutSpecGen(s *specgen.SpecGenerator, c *entities.ContainerCreateOptions
if len(s.HostUsers) == 0 || len(c.HostUsers) != 0 {
s.HostUsers = c.HostUsers
}
if len(s.ImageVolumeMode) == 0 || len(c.ImageVolume) != 0 {
s.ImageVolumeMode = c.ImageVolume
if len(c.ImageVolume) != 0 {
if len(s.ImageVolumeMode) == 0 {
s.ImageVolumeMode = c.ImageVolume
}
}
if len(s.ImageVolumeMode) == 0 {
s.ImageVolumeMode = rtc.Engine.ImageVolumeMode
}
if s.ImageVolumeMode == "bind" {
s.ImageVolumeMode = "anonymous"
Expand Down Expand Up @@ -550,11 +557,6 @@ func FillOutSpecGen(s *specgen.SpecGenerator, c *entities.ContainerCreateOptions
s.CgroupsMode = c.CgroupsMode
}
if s.CgroupsMode == "" {
rtc, err := config.Default()
if err != nil {
return err
}

s.CgroupsMode = rtc.Cgroups()
}

Expand Down
39 changes: 39 additions & 0 deletions test/system/160-volumes.bats
Original file line number Diff line number Diff line change
Expand Up @@ -411,4 +411,43 @@ NeedsChown | true
fi
}

@test "podman --image-volume" {
tmpdir=$PODMAN_TMPDIR/volume-test
mkdir -p $tmpdir
containerfile=$tmpdir/Containerfile
cat >$containerfile <<EOF
FROM $IMAGE
VOLUME /data
EOF
fs=$(stat -f -c %T .)
run_podman build -t volume_image $tmpdir

containersconf=$tmpdir/containers.conf
cat >$containersconf <<EOF
[engine]
image_volume_mode="tmpfs"
EOF

run_podman run --image-volume tmpfs --rm volume_image stat -f -c %T /data
is "$output" "tmpfs" "Should be tmpfs"

run_podman 1 run --image-volume ignore --rm volume_image stat -f -c %T /data
is "$output" "stat: can't read file system information for '/data': No such file or directory" "Should fail with /data does not exists"

CONTAINERS_CONF="$containersconf" run_podman run --rm volume_image stat -f -c %T /data
is "$output" "tmpfs" "Should be tmpfs"

CONTAINERS_CONF="$containersconf" run_podman run --image-volume bind --rm volume_image stat -f -c %T /data
assert "$output" != "tmpfs" "Should match hosts $fs"

CONTAINERS_CONF="$containersconf" run_podman run --image-volume tmpfs --rm volume_image stat -f -c %T /data
is "$output" "tmpfs" "Should be tmpfs"

CONTAINERS_CONF="$containersconf" run_podman 1 run --image-volume ignore --rm volume_image stat -f -c %T /data
is "$output" "stat: can't read file system information for '/data': No such file or directory" "Should fail with /data does not exists"

run_podman rm --all --force -t 0
run_podman image rm --force localhost/volume_image
}

# vim: filetype=sh

0 comments on commit 13cdf86

Please sign in to comment.