2018-08-08 13:50:15 +00:00
|
|
|
package libpod
|
|
|
|
|
2019-07-15 18:55:20 +00:00
|
|
|
import (
|
|
|
|
"time"
|
2019-08-27 17:45:11 +00:00
|
|
|
|
2020-07-28 12:23:45 +00:00
|
|
|
"github.com/containers/podman/v2/libpod/define"
|
|
|
|
"github.com/containers/podman/v2/libpod/lock"
|
2019-07-15 18:55:20 +00:00
|
|
|
)
|
|
|
|
|
2019-08-30 20:09:17 +00:00
|
|
|
// Volume is a libpod named volume.
|
|
|
|
// Named volumes may be shared by multiple containers, and may be created using
|
|
|
|
// more complex options than normal bind mounts. They may be backed by a mounted
|
|
|
|
// filesystem on the host.
|
2018-08-08 13:50:15 +00:00
|
|
|
type Volume struct {
|
|
|
|
config *VolumeConfig
|
2019-08-30 20:09:17 +00:00
|
|
|
state *VolumeState
|
2018-08-08 13:50:15 +00:00
|
|
|
|
|
|
|
valid bool
|
|
|
|
runtime *Runtime
|
2019-08-27 17:45:11 +00:00
|
|
|
lock lock.Locker
|
2018-08-08 13:50:15 +00:00
|
|
|
}
|
|
|
|
|
2019-08-30 20:09:17 +00:00
|
|
|
// VolumeConfig holds the volume's immutable configuration.
|
2018-08-08 13:50:15 +00:00
|
|
|
type VolumeConfig struct {
|
2019-08-27 17:45:11 +00:00
|
|
|
// Name of the volume.
|
2018-09-23 18:01:29 +00:00
|
|
|
Name string `json:"name"`
|
2019-08-27 17:45:11 +00:00
|
|
|
// ID of the volume's lock.
|
|
|
|
LockID uint32 `json:"lockID"`
|
|
|
|
// Labels for the volume.
|
|
|
|
Labels map[string]string `json:"labels"`
|
|
|
|
// The volume driver. Empty string or local does not activate a volume
|
|
|
|
// driver, all other volumes will.
|
2019-08-29 20:14:18 +00:00
|
|
|
Driver string `json:"volumeDriver"`
|
2019-08-27 17:45:11 +00:00
|
|
|
// The location the volume is mounted at.
|
|
|
|
MountPoint string `json:"mountPoint"`
|
|
|
|
// Time the volume was created.
|
|
|
|
CreatedTime time.Time `json:"createdAt,omitempty"`
|
|
|
|
// Options to pass to the volume driver. For the local driver, this is
|
|
|
|
// a list of mount options. For other drivers, they are passed to the
|
|
|
|
// volume driver handling the volume.
|
2019-08-30 20:09:17 +00:00
|
|
|
Options map[string]string `json:"volumeOptions,omitempty"`
|
Deprecate & remove IsCtrSpecific in favor of IsAnon
In Podman 1.6.3, we added support for anonymous volumes - fixing
our old, broken support for named volumes that were created with
containers. Unfortunately, this reused the database field we used
for the old implementation, and toggled volume removal on for
`podman run --rm` - so now, we were removing *named* volumes
created with older versions of Podman.
We can't modify these old volumes in the DB, so the next-safest
thing to do is swap to a new field to indicate volumes should be
removed. Problem: Volumes created with 1.6.3 and up until this
lands, even anonymous volumes, will not be removed. However, this
is safer than removing too many volumes, as we were doing before.
Fixes #5009
Signed-off-by: Matthew Heon <matthew.heon@pm.me>
2020-01-29 19:04:51 +00:00
|
|
|
// Whether this volume is anonymous (will be removed on container exit)
|
|
|
|
IsAnon bool `json:"isAnon"`
|
2019-08-27 17:45:11 +00:00
|
|
|
// UID the volume will be created as.
|
|
|
|
UID int `json:"uid"`
|
|
|
|
// GID the volume will be created as.
|
|
|
|
GID int `json:"gid"`
|
2018-08-08 13:50:15 +00:00
|
|
|
}
|
|
|
|
|
2019-08-30 20:09:17 +00:00
|
|
|
// VolumeState holds the volume's mutable state.
|
|
|
|
// Volumes are not guaranteed to have a state. Only volumes using the Local
|
|
|
|
// driver that have mount options set will create a state.
|
|
|
|
type VolumeState struct {
|
|
|
|
// MountCount is the number of times this volume has been requested to
|
|
|
|
// be mounted.
|
|
|
|
// It is incremented on mount() and decremented on unmount().
|
|
|
|
// On incrementing from 0, the volume will be mounted on the host.
|
|
|
|
// On decrementing to 0, the volume will be unmounted on the host.
|
|
|
|
MountCount uint `json:"mountCount"`
|
2019-09-06 17:52:13 +00:00
|
|
|
// NeedsCopyUp indicates that the next time the volume is mounted into
|
|
|
|
// a container, the container will "copy up" the contents of the
|
|
|
|
// mountpoint into the volume.
|
|
|
|
// This should only be done once. As such, this is set at container
|
|
|
|
// create time, then cleared after the copy up is done and never set
|
|
|
|
// again.
|
|
|
|
NeedsCopyUp bool `json:"notYetMounted,omitempty"`
|
2020-06-24 12:44:28 +00:00
|
|
|
// NeedsChown indicates that the next time the volume is mounted into
|
|
|
|
// a container, the container will chown the volume to the container process
|
|
|
|
// UID/GID.
|
|
|
|
NeedsChown bool `json:"notYetChowned,omitempty"`
|
|
|
|
// UIDChowned is the UID the volume was chowned to.
|
|
|
|
UIDChowned int `json:"uidChowned,omitempty"`
|
|
|
|
// GIDChowned is the GID the volume was chowned to.
|
|
|
|
GIDChowned int `json:"gidChowned,omitempty"`
|
2019-08-30 20:09:17 +00:00
|
|
|
}
|
|
|
|
|
2018-08-08 13:50:15 +00:00
|
|
|
// Name retrieves the volume's name
|
|
|
|
func (v *Volume) Name() string {
|
|
|
|
return v.config.Name
|
|
|
|
}
|
|
|
|
|
2019-07-15 18:55:20 +00:00
|
|
|
// Driver retrieves the volume's driver.
|
|
|
|
func (v *Volume) Driver() string {
|
|
|
|
return v.config.Driver
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scope retrieves the volume's scope.
|
|
|
|
// Libpod does not implement volume scoping, and this is provided solely for
|
2019-08-02 19:30:31 +00:00
|
|
|
// Docker compatibility. It returns only "local".
|
2019-07-15 18:55:20 +00:00
|
|
|
func (v *Volume) Scope() string {
|
|
|
|
return "local"
|
|
|
|
}
|
|
|
|
|
2018-08-08 13:50:15 +00:00
|
|
|
// Labels returns the volume's labels
|
|
|
|
func (v *Volume) Labels() map[string]string {
|
|
|
|
labels := make(map[string]string)
|
|
|
|
for key, value := range v.config.Labels {
|
|
|
|
labels[key] = value
|
|
|
|
}
|
|
|
|
return labels
|
|
|
|
}
|
|
|
|
|
|
|
|
// MountPoint returns the volume's mountpoint on the host
|
|
|
|
func (v *Volume) MountPoint() string {
|
|
|
|
return v.config.MountPoint
|
|
|
|
}
|
|
|
|
|
|
|
|
// Options return the volume's options
|
|
|
|
func (v *Volume) Options() map[string]string {
|
|
|
|
options := make(map[string]string)
|
2019-09-03 19:03:44 +00:00
|
|
|
for k, v := range v.config.Options {
|
|
|
|
options[k] = v
|
2018-08-08 13:50:15 +00:00
|
|
|
}
|
|
|
|
return options
|
|
|
|
}
|
|
|
|
|
Deprecate & remove IsCtrSpecific in favor of IsAnon
In Podman 1.6.3, we added support for anonymous volumes - fixing
our old, broken support for named volumes that were created with
containers. Unfortunately, this reused the database field we used
for the old implementation, and toggled volume removal on for
`podman run --rm` - so now, we were removing *named* volumes
created with older versions of Podman.
We can't modify these old volumes in the DB, so the next-safest
thing to do is swap to a new field to indicate volumes should be
removed. Problem: Volumes created with 1.6.3 and up until this
lands, even anonymous volumes, will not be removed. However, this
is safer than removing too many volumes, as we were doing before.
Fixes #5009
Signed-off-by: Matthew Heon <matthew.heon@pm.me>
2020-01-29 19:04:51 +00:00
|
|
|
// Anonymous returns whether this volume is anonymous. Anonymous volumes were
|
|
|
|
// created with a container, and will be removed when that container is removed.
|
|
|
|
func (v *Volume) Anonymous() bool {
|
|
|
|
return v.config.IsAnon
|
2019-02-26 17:16:58 +00:00
|
|
|
}
|
2019-07-15 18:55:20 +00:00
|
|
|
|
|
|
|
// UID returns the UID the volume will be created as.
|
2020-06-24 12:44:28 +00:00
|
|
|
func (v *Volume) UID() (int, error) {
|
|
|
|
v.lock.Lock()
|
|
|
|
defer v.lock.Unlock()
|
|
|
|
|
|
|
|
if !v.valid {
|
|
|
|
return -1, define.ErrVolumeRemoved
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.state.UIDChowned > 0 {
|
|
|
|
return v.state.UIDChowned, nil
|
|
|
|
}
|
|
|
|
return v.config.UID, nil
|
2019-07-15 18:55:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GID returns the GID the volume will be created as.
|
2020-06-24 12:44:28 +00:00
|
|
|
func (v *Volume) GID() (int, error) {
|
|
|
|
v.lock.Lock()
|
|
|
|
defer v.lock.Unlock()
|
|
|
|
|
|
|
|
if !v.valid {
|
|
|
|
return -1, define.ErrVolumeRemoved
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.state.GIDChowned > 0 {
|
|
|
|
return v.state.GIDChowned, nil
|
|
|
|
}
|
|
|
|
return v.config.GID, nil
|
2019-07-15 18:55:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreatedTime returns the time the volume was created at. It was not tracked
|
|
|
|
// for some time, so older volumes may not contain one.
|
|
|
|
func (v *Volume) CreatedTime() time.Time {
|
|
|
|
return v.config.CreatedTime
|
|
|
|
}
|
2020-02-23 17:00:09 +00:00
|
|
|
|
|
|
|
// Config returns the volume's configuration.
|
|
|
|
func (v *Volume) Config() (*VolumeConfig, error) {
|
|
|
|
config := VolumeConfig{}
|
|
|
|
err := JSONDeepCopy(v.config, &config)
|
|
|
|
return &config, err
|
|
|
|
}
|
2020-05-01 17:57:41 +00:00
|
|
|
|
|
|
|
// VolumeInUse goes through the container dependencies of a volume
|
|
|
|
// and checks if the volume is being used by any container.
|
2020-06-24 18:10:29 +00:00
|
|
|
func (v *Volume) VolumeInUse() ([]string, error) {
|
2020-05-01 17:57:41 +00:00
|
|
|
v.lock.Lock()
|
|
|
|
defer v.lock.Unlock()
|
|
|
|
|
|
|
|
if !v.valid {
|
|
|
|
return nil, define.ErrVolumeRemoved
|
|
|
|
}
|
|
|
|
return v.runtime.state.VolumeInUse(v)
|
|
|
|
}
|
2020-06-24 18:10:29 +00:00
|
|
|
|
|
|
|
// IsDangling returns whether this volume is dangling (unused by any
|
|
|
|
// containers).
|
|
|
|
func (v *Volume) IsDangling() (bool, error) {
|
|
|
|
ctrs, err := v.VolumeInUse()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return len(ctrs) == 0, nil
|
|
|
|
}
|