2019-06-13 20:51:31 +00:00
|
|
|
package libpod
|
|
|
|
|
|
|
|
import (
|
2022-06-30 14:47:21 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2019-09-11 14:53:21 +00:00
|
|
|
"time"
|
|
|
|
|
2022-01-18 09:14:48 +00:00
|
|
|
"github.com/containers/podman/v4/libpod/define"
|
2019-06-13 20:51:31 +00:00
|
|
|
"github.com/containers/storage"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
// StorageContainer represents a container present in c/storage but not in
|
|
|
|
// libpod.
|
|
|
|
type StorageContainer struct {
|
|
|
|
ID string
|
|
|
|
Names []string
|
2019-09-11 14:53:21 +00:00
|
|
|
Image string
|
|
|
|
CreateTime time.Time
|
2019-06-13 20:51:31 +00:00
|
|
|
PresentInLibpod bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListStorageContainers lists all containers visible to c/storage.
|
|
|
|
func (r *Runtime) ListStorageContainers() ([]*StorageContainer, error) {
|
|
|
|
finalCtrs := []*StorageContainer{}
|
|
|
|
|
|
|
|
ctrs, err := r.store.Containers()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ctr := range ctrs {
|
|
|
|
storageCtr := new(StorageContainer)
|
|
|
|
storageCtr.ID = ctr.ID
|
|
|
|
storageCtr.Names = ctr.Names
|
2019-09-11 14:53:21 +00:00
|
|
|
storageCtr.Image = ctr.ImageID
|
|
|
|
storageCtr.CreateTime = ctr.Created
|
2019-06-13 20:51:31 +00:00
|
|
|
|
|
|
|
// Look up if container is in state
|
|
|
|
hasCtr, err := r.state.HasContainer(ctr.ID)
|
|
|
|
if err != nil {
|
2022-09-10 11:40:39 +00:00
|
|
|
return nil, fmt.Errorf("looking up container %s in state: %w", ctr.ID, err)
|
2019-06-13 20:51:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
storageCtr.PresentInLibpod = hasCtr
|
|
|
|
|
|
|
|
finalCtrs = append(finalCtrs, storageCtr)
|
|
|
|
}
|
|
|
|
|
|
|
|
return finalCtrs, nil
|
|
|
|
}
|
|
|
|
|
2020-10-03 10:56:42 +00:00
|
|
|
func (r *Runtime) StorageContainer(idOrName string) (*storage.Container, error) {
|
|
|
|
return r.store.Container(idOrName)
|
|
|
|
}
|
|
|
|
|
2019-06-13 20:51:31 +00:00
|
|
|
// RemoveStorageContainer removes a container from c/storage.
|
|
|
|
// The container WILL NOT be removed if it exists in libpod.
|
|
|
|
// Accepts ID or full name of container.
|
|
|
|
// If force is set, the container will be unmounted first to ensure removal.
|
|
|
|
func (r *Runtime) RemoveStorageContainer(idOrName string, force bool) error {
|
|
|
|
targetID, err := r.store.Lookup(idOrName)
|
|
|
|
if err != nil {
|
2022-06-30 14:47:21 +00:00
|
|
|
if errors.Is(err, storage.ErrLayerUnknown) {
|
|
|
|
return fmt.Errorf("no container with ID or name %q found: %w", idOrName, define.ErrNoSuchCtr)
|
2019-06-13 20:51:31 +00:00
|
|
|
}
|
2022-09-10 11:40:39 +00:00
|
|
|
return fmt.Errorf("looking up container %q: %w", idOrName, err)
|
2019-06-13 20:51:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup returns an ID but it's not guaranteed to be a container ID.
|
|
|
|
// So we can still error here.
|
|
|
|
ctr, err := r.store.Container(targetID)
|
|
|
|
if err != nil {
|
2022-06-30 14:47:21 +00:00
|
|
|
if errors.Is(err, storage.ErrContainerUnknown) {
|
|
|
|
return fmt.Errorf("%q does not refer to a container: %w", idOrName, define.ErrNoSuchCtr)
|
2019-06-13 20:51:31 +00:00
|
|
|
}
|
2022-09-10 11:40:39 +00:00
|
|
|
return fmt.Errorf("retrieving container %q: %w", idOrName, err)
|
2019-06-13 20:51:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Error out if the container exists in libpod
|
|
|
|
exists, err := r.state.HasContainer(ctr.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if exists {
|
2022-06-30 14:47:21 +00:00
|
|
|
return fmt.Errorf("refusing to remove %q as it exists in libpod as container %s: %w", idOrName, ctr.ID, define.ErrCtrExists)
|
2019-06-13 20:51:31 +00:00
|
|
|
}
|
|
|
|
|
Add support for 'image' volume driver
We added the concept of image volumes in 2.2.0, to support
inspecting an image from within a container. However, this is a
strictly read-only mount, with no modification allowed.
By contrast, the new `image` volume driver creates a c/storage
container as its underlying storage, so we have a read/write
layer. This, in and of itself, is not especially interesting, but
what it will enable in the future is. If we add a new command to
allow these image volumes to be committed, we can now distribute
volumes - and changes to them - via a standard OCI image registry
(which is rather new and quite exciting).
Future work in this area:
- Add support for `podman volume push` (commit volume changes and
push resulting image to OCI registry).
- Add support for `podman volume pull` (currently, we require
that the image a volume is created from be already pulled; it
would be simpler if we had a dedicated command that did the
pull and made a volume from it)
- Add support for scratch images (make an empty image on demand
to use as the base of the volume)
- Add UOR support to `podman volume push` and
`podman volume pull` to enable both with non-image volume
drivers
Signed-off-by: Matthew Heon <matthew.heon@pm.me>
2022-09-16 19:00:37 +00:00
|
|
|
// Error out if this is an image-backed volume
|
|
|
|
allVols, err := r.state.AllVolumes()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, vol := range allVols {
|
|
|
|
if vol.config.Driver == define.VolumeDriverImage && vol.config.StorageID == ctr.ID {
|
|
|
|
return fmt.Errorf("refusing to remove %q as it exists in libpod as an image-backed volume %s: %w", idOrName, vol.Name(), define.ErrCtrExists)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-13 20:51:31 +00:00
|
|
|
if !force {
|
|
|
|
timesMounted, err := r.store.Mounted(ctr.ID)
|
|
|
|
if err != nil {
|
2022-06-30 14:47:21 +00:00
|
|
|
if errors.Is(err, storage.ErrContainerUnknown) {
|
2019-06-13 20:51:31 +00:00
|
|
|
// Container was removed from under us.
|
|
|
|
// It's gone, so don't bother erroring.
|
2020-12-01 21:15:14 +00:00
|
|
|
logrus.Infof("Storage for container %s already removed", ctr.ID)
|
2019-06-13 20:51:31 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-09-16 10:13:21 +00:00
|
|
|
logrus.Warnf("Checking if container %q is mounted, attempting to delete: %v", idOrName, err)
|
2019-06-13 20:51:31 +00:00
|
|
|
}
|
|
|
|
if timesMounted > 0 {
|
2022-06-30 14:47:21 +00:00
|
|
|
return fmt.Errorf("container %q is mounted and cannot be removed without using force: %w", idOrName, define.ErrCtrStateInvalid)
|
2019-06-13 20:51:31 +00:00
|
|
|
}
|
2020-01-13 12:01:45 +00:00
|
|
|
} else if _, err := r.store.Unmount(ctr.ID, true); err != nil {
|
2021-09-16 10:13:21 +00:00
|
|
|
if errors.Is(err, storage.ErrContainerUnknown) {
|
2020-01-13 12:01:45 +00:00
|
|
|
// Container again gone, no error
|
2020-12-01 21:15:14 +00:00
|
|
|
logrus.Infof("Storage for container %s already removed", ctr.ID)
|
2020-01-13 12:01:45 +00:00
|
|
|
return nil
|
2019-06-13 20:51:31 +00:00
|
|
|
}
|
2021-09-16 10:13:21 +00:00
|
|
|
logrus.Warnf("Unmounting container %q while attempting to delete storage: %v", idOrName, err)
|
2019-06-13 20:51:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := r.store.DeleteContainer(ctr.ID); err != nil {
|
2022-06-30 14:47:21 +00:00
|
|
|
if errors.Is(err, storage.ErrNotAContainer) || errors.Is(err, storage.ErrContainerUnknown) {
|
2019-06-13 20:51:31 +00:00
|
|
|
// Container again gone, no error
|
2020-12-01 21:15:14 +00:00
|
|
|
logrus.Infof("Storage for container %s already removed", ctr.ID)
|
2019-06-13 20:51:31 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-09-10 11:40:39 +00:00
|
|
|
return fmt.Errorf("removing storage for container %q: %w", idOrName, err)
|
2019-06-13 20:51:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|