9
"github.com/containers/podman/v5/libpod/define"
10
"github.com/containers/podman/v5/libpod/events"
11
"github.com/containers/podman/v5/pkg/domain/entities/reports"
14
// Contains the public Runtime API for volumes
16
// A VolumeCreateOption is a functional option which alters the Volume created by
18
type VolumeCreateOption func(*Volume) error
20
// VolumeFilter is a function to determine whether a volume is included in command
21
// output. Volumes to be outputted are tested using the function. a true return will
22
// include the volume, a false return will exclude it.
23
type VolumeFilter func(*Volume) bool
25
// RemoveVolume removes a volumes
26
func (r *Runtime) RemoveVolume(ctx context.Context, v *Volume, force bool, timeout *uint) error {
28
return define.ErrRuntimeStopped
32
if ok, _ := r.state.HasVolume(v.Name()); !ok {
33
// Volume probably already removed
34
// Or was never in the runtime to begin with
38
return r.removeVolume(ctx, v, force, timeout, false)
41
// GetVolume retrieves a volume given its full name.
42
func (r *Runtime) GetVolume(name string) (*Volume, error) {
44
return nil, define.ErrRuntimeStopped
47
vol, err := r.state.Volume(name)
55
// LookupVolume retrieves a volume by unambiguous partial name.
56
func (r *Runtime) LookupVolume(name string) (*Volume, error) {
58
return nil, define.ErrRuntimeStopped
61
vol, err := r.state.LookupVolume(name)
69
// HasVolume checks to see if a volume with the given name exists
70
func (r *Runtime) HasVolume(name string) (bool, error) {
72
return false, define.ErrRuntimeStopped
75
return r.state.HasVolume(name)
78
// Volumes retrieves all volumes
79
// Filters can be provided which will determine which volumes are included in the
80
// output. If multiple filters are used, a volume will be returned if
81
// any of the filters are matched
82
func (r *Runtime) Volumes(filters ...VolumeFilter) ([]*Volume, error) {
84
return nil, define.ErrRuntimeStopped
87
vols, err := r.state.AllVolumes()
92
if len(filters) == 0 {
96
volsFiltered := make([]*Volume, 0, len(vols))
97
for _, vol := range vols {
99
for _, filter := range filters {
100
include = include || filter(vol)
104
volsFiltered = append(volsFiltered, vol)
108
return volsFiltered, nil
111
// GetAllVolumes retrieves all the volumes
112
func (r *Runtime) GetAllVolumes() ([]*Volume, error) {
114
return nil, define.ErrRuntimeStopped
117
return r.state.AllVolumes()
120
// PruneVolumes removes unused volumes from the system
121
func (r *Runtime) PruneVolumes(ctx context.Context, filterFuncs []VolumeFilter) ([]*reports.PruneReport, error) {
122
preports := make([]*reports.PruneReport, 0)
123
vols, err := r.Volumes(filterFuncs...)
128
for _, vol := range vols {
129
report := new(reports.PruneReport)
130
volSize, err := vol.Size()
134
report.Size = volSize
135
report.Id = vol.Name()
137
if err := r.RemoveVolume(ctx, vol, false, timeout); err != nil {
138
if !errors.Is(err, define.ErrVolumeBeingUsed) && !errors.Is(err, define.ErrVolumeRemoved) {
141
// We didn't remove the volume for some reason
145
vol.newVolumeEvent(events.Prune)
147
preports = append(preports, report)