From 3e066e29202118e6cca60be516ddc08cc7a9060e Mon Sep 17 00:00:00 2001 From: Matthew Heon Date: Fri, 29 Mar 2019 10:20:16 -0400 Subject: Volume force-remove now removed dependent containers Signed-off-by: Matthew Heon --- docs/podman-volume-rm.1.md | 3 ++- libpod/runtime_volume_linux.go | 26 +++++++++++++++++++++----- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/docs/podman-volume-rm.1.md b/docs/podman-volume-rm.1.md index c23d7675c..8c3765235 100644 --- a/docs/podman-volume-rm.1.md +++ b/docs/podman-volume-rm.1.md @@ -21,7 +21,8 @@ Remove all volumes. **-f**, **--force**="" -Remove a volume by force, even if it is being used by a container +Remove a volume by force. +If it is being used by containers, the containers will be removed first. **--help** diff --git a/libpod/runtime_volume_linux.go b/libpod/runtime_volume_linux.go index 39c2f808d..dcefcde4e 100644 --- a/libpod/runtime_volume_linux.go +++ b/libpod/runtime_volume_linux.go @@ -99,11 +99,27 @@ func (r *Runtime) removeVolume(ctx context.Context, v *Volume, force bool) error return errors.Wrapf(ErrVolumeBeingUsed, "volume %s is being used by the following container(s): %s", v.Name(), depsStr) } - // TODO: force-removing a volume makes *no sense* - // I do not believe we should be allowing this at all. - // For now, return angry errors. - // TODO: need to do something more sane in this case - return errors.Wrapf(ErrVolumeBeingUsed, "TODO: FIXME - still refusing to remove because force-removing an in-use volume is not good.") + // We need to remove all containers using the volume + for _, dep := range deps { + ctr, err := r.state.Container(dep) + if err != nil { + // If the container's removed, no point in + // erroring. + if errors.Cause(err) == ErrNoSuchCtr || errors.Cause(err) == ErrCtrRemoved { + continue + } + + return errors.Wrapf(err, "error removing container %s that depends on volume %s", dep, v.Name()) + } + + // TODO: do we want to set force here when removing + // containers? + // I'm inclined to say no, in case someone accidentally + // wipes a container they're using... + if err := r.removeContainer(ctx, ctr, false, true); err != nil { + return errors.Wrapf(err, "error removing container %s that depends on volume %s", ctr.ID(), v.Name()) + } + } } // Set volume as invalid so it can no longer be used -- cgit v1.2.3-54-g00ecf