8
"github.com/containers/podman/v5/libpod/define"
9
"github.com/containers/podman/v5/libpod/lock"
10
"github.com/containers/podman/v5/libpod/plugin"
11
"github.com/containers/storage/pkg/directory"
14
// Volume is a libpod named volume.
15
// Named volumes may be shared by multiple containers, and may be created using
16
// more complex options than normal bind mounts. They may be backed by a mounted
17
// filesystem on the host.
24
plugin *plugin.VolumePlugin
29
// VolumeConfig holds the volume's immutable configuration.
30
type VolumeConfig struct {
31
// Name of the volume.
32
Name string `json:"name"`
33
// ID of the volume's lock.
34
LockID uint32 `json:"lockID"`
35
// Labels for the volume.
36
Labels map[string]string `json:"labels"`
37
// The volume driver. Empty string or local does not activate a volume
38
// driver, all other values will.
39
Driver string `json:"volumeDriver"`
40
// The location the volume is mounted at.
41
MountPoint string `json:"mountPoint"`
42
// Time the volume was created.
43
CreatedTime time.Time `json:"createdAt,omitempty"`
44
// Options to pass to the volume driver. For the local driver, this is
45
// a list of mount options. For other drivers, they are passed to the
46
// volume driver handling the volume.
47
Options map[string]string `json:"volumeOptions,omitempty"`
48
// Whether this volume is anonymous (will be removed on container exit)
49
IsAnon bool `json:"isAnon"`
50
// UID the volume will be created as.
52
// GID the volume will be created as.
54
// Size maximum of the volume.
55
Size uint64 `json:"size"`
56
// Inodes maximum of the volume.
57
Inodes uint64 `json:"inodes"`
58
// DisableQuota indicates that the volume should completely disable using any
60
DisableQuota bool `json:"disableQuota,omitempty"`
61
// Timeout allows users to override the default driver timeout of 5 seconds
62
Timeout *uint `json:"timeout,omitempty"`
63
// StorageName is the name of the volume in c/storage. Only used for
65
StorageName string `json:"storageName,omitempty"`
66
// StorageID is the ID of the volume in c/storage. Only used for image
68
StorageID string `json:"storageID,omitempty"`
69
// StorageImageID is the ID of the image the volume was based off of.
70
// Only used for image volumes.
71
StorageImageID string `json:"storageImageID,omitempty"`
72
// MountLabel is the SELinux label to assign to mount points
73
MountLabel string `json:"mountlabel,omitempty"`
76
// VolumeState holds the volume's mutable state.
77
// Volumes are not guaranteed to have a state. Only volumes using the Local
78
// driver that have mount options set will create a state.
79
type VolumeState struct {
80
// Mountpoint is the location where the volume was mounted.
81
// This is only used for volumes using a volume plugin, which will mount
82
// at non-standard locations.
83
MountPoint string `json:"mountPoint,omitempty"`
84
// MountCount is the number of times this volume has been requested to
86
// It is incremented on mount() and decremented on unmount().
87
// On incrementing from 0, the volume will be mounted on the host.
88
// On decrementing to 0, the volume will be unmounted on the host.
89
MountCount uint `json:"mountCount"`
90
// NeedsCopyUp indicates that the next time the volume is mounted into
91
// a container, the container will "copy up" the contents of the
92
// mountpoint into the volume.
93
// This should only be done once. As such, this is set at container
94
// create time, then cleared after the copy up is done and never set
96
NeedsCopyUp bool `json:"notYetMounted,omitempty"`
97
// NeedsChown indicates that the next time the volume is mounted into
98
// a container, the container will chown the volume to the container process
100
NeedsChown bool `json:"notYetChowned,omitempty"`
101
// UIDChowned is the UID the volume was chowned to.
102
UIDChowned int `json:"uidChowned,omitempty"`
103
// GIDChowned is the GID the volume was chowned to.
104
GIDChowned int `json:"gidChowned,omitempty"`
107
// Name retrieves the volume's name
108
func (v *Volume) Name() string {
112
// Returns the size on disk of volume
113
func (v *Volume) Size() (uint64, error) {
114
size, err := directory.Size(v.config.MountPoint)
115
return uint64(size), err
118
// Driver retrieves the volume's driver.
119
func (v *Volume) Driver() string {
120
return v.config.Driver
123
// Scope retrieves the volume's scope.
124
// Libpod does not implement volume scoping, and this is provided solely for
125
// Docker compatibility. It returns only "local".
126
func (v *Volume) Scope() string {
130
// Labels returns the volume's labels
131
func (v *Volume) Labels() map[string]string {
132
labels := make(map[string]string)
133
for key, value := range v.config.Labels {
139
// MountPoint returns the volume's mountpoint on the host
140
func (v *Volume) MountPoint() (string, error) {
141
// For the sake of performance, avoid locking unless we have to.
142
if v.UsesVolumeDriver() || v.config.Driver == define.VolumeDriverImage {
144
defer v.lock.Unlock()
146
if err := v.update(); err != nil {
151
return v.mountPoint(), nil
154
// MountCount returns the volume's mountcount on the host from state
155
// Useful in determining if volume is using plugin or a filesystem mount and its mount
156
func (v *Volume) MountCount() (uint, error) {
158
defer v.lock.Unlock()
159
if err := v.update(); err != nil {
162
return v.state.MountCount, nil
165
// Internal-only helper for volume mountpoint
166
func (v *Volume) mountPoint() string {
167
if v.UsesVolumeDriver() || v.config.Driver == define.VolumeDriverImage {
168
return v.state.MountPoint
171
return v.config.MountPoint
174
// Options return the volume's options
175
func (v *Volume) Options() map[string]string {
176
options := make(map[string]string)
177
for k, v := range v.config.Options {
183
// Anonymous returns whether this volume is anonymous. Anonymous volumes were
184
// created with a container, and will be removed when that container is removed.
185
func (v *Volume) Anonymous() bool {
186
return v.config.IsAnon
189
// UID returns the UID the volume will be created as.
190
func (v *Volume) UID() (int, error) {
192
defer v.lock.Unlock()
194
if err := v.update(); err != nil {
201
// Internal, unlocked accessor for UID.
202
func (v *Volume) uid() int {
203
if v.state.UIDChowned > 0 {
204
return v.state.UIDChowned
209
// GID returns the GID the volume will be created as.
210
func (v *Volume) GID() (int, error) {
212
defer v.lock.Unlock()
214
if err := v.update(); err != nil {
221
// Internal, unlocked accessor for GID.
222
func (v *Volume) gid() int {
223
if v.state.GIDChowned > 0 {
224
return v.state.GIDChowned
229
// CreatedTime returns the time the volume was created at. It was not tracked
230
// for some time, so older volumes may not contain one.
231
func (v *Volume) CreatedTime() time.Time {
232
return v.config.CreatedTime
235
// Config returns the volume's configuration.
236
func (v *Volume) Config() (*VolumeConfig, error) {
237
config := VolumeConfig{}
238
err := JSONDeepCopy(v.config, &config)
242
// VolumeInUse goes through the container dependencies of a volume
243
// and checks if the volume is being used by any container.
244
func (v *Volume) VolumeInUse() ([]string, error) {
246
defer v.lock.Unlock()
249
return nil, define.ErrVolumeRemoved
251
return v.runtime.state.VolumeInUse(v)
254
// IsDangling returns whether this volume is dangling (unused by any
256
func (v *Volume) IsDangling() (bool, error) {
257
ctrs, err := v.VolumeInUse()
261
return len(ctrs) == 0, nil
264
// UsesVolumeDriver determines whether the volume uses a volume driver. Volume
265
// drivers are pluggable backends for volumes that will manage the storage and
267
func (v *Volume) UsesVolumeDriver() bool {
268
if v.config.Driver == define.VolumeDriverImage {
269
if _, ok := v.runtime.config.Engine.VolumePlugins[v.config.Driver]; ok {
274
return !(v.config.Driver == define.VolumeDriverLocal || v.config.Driver == "")
277
func (v *Volume) Mount() (string, error) {
279
defer v.lock.Unlock()
281
return v.config.MountPoint, err
284
func (v *Volume) Unmount() error {
286
defer v.lock.Unlock()
287
return v.unmount(false)
290
func (v *Volume) NeedsMount() bool {
291
return v.needsMount()