Skip to content

Commit

Permalink
Ensure that the DF endpoint updated volume refcount
Browse files Browse the repository at this point in the history
The field was already exposed already in the `system df` output
so this just required a bit of plumbing and testing.

As part of this, fix `podman systemd df` volume in-use logic.
Previously, volumes were only considered to be in use if the
container using them was running. This does not match Docker's
behavior, where a volume is considered in use as long as a
container exists that uses the volume, even if said container is
not running.

Fixes containers#15720

Signed-off-by: Matthew Heon <matthew.heon@pm.me>
  • Loading branch information
mheon committed Sep 12, 2022
1 parent 94864cb commit 975d65e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pkg/api/handlers/compat/system.go
Expand Up @@ -76,7 +76,7 @@ func GetDiskUsage(w http.ResponseWriter, r *http.Request) {
Scope: "local",
Status: nil,
UsageData: &docker.VolumeUsageData{
RefCount: 1,
RefCount: int64(o.Links),
Size: o.Size,
},
}
Expand Down
8 changes: 1 addition & 7 deletions pkg/domain/infra/abi/system.go
Expand Up @@ -333,7 +333,6 @@ func (ic *ContainerEngine) SystemDf(ctx context.Context, options entities.System
dfVolumes := make([]*entities.SystemDfVolumeReport, 0, len(vols))
for _, v := range vols {
var reclaimableSize uint64
var consInUse int
mountPoint, err := v.MountPoint()
if err != nil {
return nil, err
Expand All @@ -355,14 +354,9 @@ func (ic *ContainerEngine) SystemDf(ctx context.Context, options entities.System
if len(inUse) == 0 {
reclaimableSize = volSize
}
for _, viu := range inUse {
if cutil.StringInSlice(viu, runningContainers) {
consInUse++
}
}
report := entities.SystemDfVolumeReport{
VolumeName: v.Name(),
Links: consInUse,
Links: len(inUse),
Size: int64(volSize),
ReclaimableSize: int64(reclaimableSize),
}
Expand Down
19 changes: 18 additions & 1 deletion test/apiv2/45-system.at
@@ -1,5 +1,4 @@
# -*- sh -*-
#
# system related tests
#

Expand All @@ -25,6 +24,24 @@ t GET system/df 200 '.Volumes[0].Name=foo1'

t GET libpod/system/df 200 '.Volumes[0].VolumeName=foo1'

# Verify that no containers reference the volume
t GET system/df 200 '.Volumes[0].UsageData.RefCount=0'

# Make a container using the volume
podman pull $IMAGE &>/dev/null
t POST containers/create Image=$IMAGE Volumes='{"/test":{}}' HostConfig='{"Binds":["foo1:/test"]}' 201 \
.Id~[0-9a-f]\\{64\\}
cid=$(jq -r '.Id' <<<"$output")

# Verify that one container references the volume
t GET system/df 200 '.Volumes[0].UsageData.RefCount=1'

# Remove the container
t DELETE containers/$cid?v=true 204

# Verify that no containers reference the volume
t GET system/df 200 '.Volumes[0].UsageData.RefCount=0'

# Create two more volumes to test pruneing
t POST libpod/volumes/create \
Name=foo2 \
Expand Down

0 comments on commit 975d65e

Please sign in to comment.