diff options
Diffstat (limited to 'libpod/runtime_volume.go')
-rw-r--r-- | libpod/runtime_volume.go | 80 |
1 files changed, 75 insertions, 5 deletions
diff --git a/libpod/runtime_volume.go b/libpod/runtime_volume.go index 3921758ee..11f37ad4b 100644 --- a/libpod/runtime_volume.go +++ b/libpod/runtime_volume.go @@ -2,6 +2,9 @@ package libpod import ( "context" + "github.com/pkg/errors" + "github.com/sirupsen/logrus" + "strings" ) // Contains the public Runtime API for volumes @@ -16,7 +19,7 @@ type VolumeCreateOption func(*Volume) error type VolumeFilter func(*Volume) bool // RemoveVolume removes a volumes -func (r *Runtime) RemoveVolume(ctx context.Context, v *Volume, force, prune bool) error { +func (r *Runtime) RemoveVolume(ctx context.Context, v *Volume, force bool) error { r.lock.Lock() defer r.lock.Unlock() @@ -32,10 +35,39 @@ func (r *Runtime) RemoveVolume(ctx context.Context, v *Volume, force, prune bool } } - v.lock.Lock() - defer v.lock.Unlock() + return r.removeVolume(ctx, v, force) +} + +// RemoveVolumes removes a slice of volumes or all with a force bool +func (r *Runtime) RemoveVolumes(ctx context.Context, volumes []string, all, force bool) ([]string, error) { + var ( + vols []*Volume + err error + deletedVols []string + ) + if all { + vols, err = r.Volumes() + if err != nil { + return nil, errors.Wrapf(err, "unable to get all volumes") + } + } else { + for _, i := range volumes { + vol, err := r.GetVolume(i) + if err != nil { + return nil, err + } + vols = append(vols, vol) + } + } - return r.removeVolume(ctx, v, force, prune) + for _, vol := range vols { + if err := r.RemoveVolume(ctx, vol, force); err != nil { + return deletedVols, err + } + logrus.Debugf("removed volume %s", vol.Name()) + deletedVols = append(deletedVols, vol.Name()) + } + return deletedVols, nil } // GetVolume retrieves a volume by its name @@ -47,7 +79,21 @@ func (r *Runtime) GetVolume(name string) (*Volume, error) { return nil, ErrRuntimeStopped } - return r.state.Volume(name) + vol, err := r.state.Volume(name) + if err == nil { + return vol, err + } + + vols, err := r.GetAllVolumes() + if err != nil { + return nil, err + } + for _, v := range vols { + if strings.HasPrefix(v.Name(), name) { + return v, nil + } + } + return nil, errors.Errorf("unable to find volume %s", name) } // HasVolume checks to see if a volume with the given name exists @@ -105,3 +151,27 @@ func (r *Runtime) GetAllVolumes() ([]*Volume, error) { return r.state.AllVolumes() } + +// PruneVolumes removes unused volumes from the system +func (r *Runtime) PruneVolumes(ctx context.Context) ([]string, []error) { + var ( + prunedIDs []string + pruneErrors []error + ) + vols, err := r.GetAllVolumes() + if err != nil { + pruneErrors = append(pruneErrors, err) + return nil, pruneErrors + } + + for _, vol := range vols { + if err := r.RemoveVolume(ctx, vol, false); err != nil { + if errors.Cause(err) != ErrVolumeBeingUsed && errors.Cause(err) != ErrVolumeRemoved { + pruneErrors = append(pruneErrors, err) + } + continue + } + prunedIDs = append(prunedIDs, vol.Name()) + } + return prunedIDs, pruneErrors +} |