github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/libpod/volume.go (about)

     1  package libpod
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/containers/libpod/libpod/lock"
     7  )
     8  
     9  // Volume is a libpod named volume.
    10  // Named volumes may be shared by multiple containers, and may be created using
    11  // more complex options than normal bind mounts. They may be backed by a mounted
    12  // filesystem on the host.
    13  type Volume struct {
    14  	config *VolumeConfig
    15  	state  *VolumeState
    16  
    17  	valid   bool
    18  	runtime *Runtime
    19  	lock    lock.Locker
    20  }
    21  
    22  // VolumeConfig holds the volume's immutable configuration.
    23  type VolumeConfig struct {
    24  	// Name of the volume.
    25  	Name string `json:"name"`
    26  	// ID of the volume's lock.
    27  	LockID uint32 `json:"lockID"`
    28  	// Labels for the volume.
    29  	Labels map[string]string `json:"labels"`
    30  	// The volume driver. Empty string or local does not activate a volume
    31  	// driver, all other volumes will.
    32  	Driver string `json:"volumeDriver"`
    33  	// The location the volume is mounted at.
    34  	MountPoint string `json:"mountPoint"`
    35  	// Time the volume was created.
    36  	CreatedTime time.Time `json:"createdAt,omitempty"`
    37  	// Options to pass to the volume driver. For the local driver, this is
    38  	// a list of mount options. For other drivers, they are passed to the
    39  	// volume driver handling the volume.
    40  	Options map[string]string `json:"volumeOptions,omitempty"`
    41  	// Whether this volume is anonymous (will be removed on container exit)
    42  	IsAnon bool `json:"isAnon"`
    43  	// UID the volume will be created as.
    44  	UID int `json:"uid"`
    45  	// GID the volume will be created as.
    46  	GID int `json:"gid"`
    47  }
    48  
    49  // VolumeState holds the volume's mutable state.
    50  // Volumes are not guaranteed to have a state. Only volumes using the Local
    51  // driver that have mount options set will create a state.
    52  type VolumeState struct {
    53  	// MountCount is the number of times this volume has been requested to
    54  	// be mounted.
    55  	// It is incremented on mount() and decremented on unmount().
    56  	// On incrementing from 0, the volume will be mounted on the host.
    57  	// On decrementing to 0, the volume will be unmounted on the host.
    58  	MountCount uint `json:"mountCount"`
    59  	// NeedsCopyUp indicates that the next time the volume is mounted into
    60  	// a container, the container will "copy up" the contents of the
    61  	// mountpoint into the volume.
    62  	// This should only be done once. As such, this is set at container
    63  	// create time, then cleared after the copy up is done and never set
    64  	// again.
    65  	NeedsCopyUp bool `json:"notYetMounted,omitempty"`
    66  }
    67  
    68  // Name retrieves the volume's name
    69  func (v *Volume) Name() string {
    70  	return v.config.Name
    71  }
    72  
    73  // Driver retrieves the volume's driver.
    74  func (v *Volume) Driver() string {
    75  	return v.config.Driver
    76  }
    77  
    78  // Scope retrieves the volume's scope.
    79  // Libpod does not implement volume scoping, and this is provided solely for
    80  // Docker compatibility. It returns only "local".
    81  func (v *Volume) Scope() string {
    82  	return "local"
    83  }
    84  
    85  // Labels returns the volume's labels
    86  func (v *Volume) Labels() map[string]string {
    87  	labels := make(map[string]string)
    88  	for key, value := range v.config.Labels {
    89  		labels[key] = value
    90  	}
    91  	return labels
    92  }
    93  
    94  // MountPoint returns the volume's mountpoint on the host
    95  func (v *Volume) MountPoint() string {
    96  	return v.config.MountPoint
    97  }
    98  
    99  // Options return the volume's options
   100  func (v *Volume) Options() map[string]string {
   101  	options := make(map[string]string)
   102  	for k, v := range v.config.Options {
   103  		options[k] = v
   104  	}
   105  	return options
   106  }
   107  
   108  // Anonymous returns whether this volume is anonymous. Anonymous volumes were
   109  // created with a container, and will be removed when that container is removed.
   110  func (v *Volume) Anonymous() bool {
   111  	return v.config.IsAnon
   112  }
   113  
   114  // UID returns the UID the volume will be created as.
   115  func (v *Volume) UID() int {
   116  	return v.config.UID
   117  }
   118  
   119  // GID returns the GID the volume will be created as.
   120  func (v *Volume) GID() int {
   121  	return v.config.GID
   122  }
   123  
   124  // CreatedTime returns the time the volume was created at. It was not tracked
   125  // for some time, so older volumes may not contain one.
   126  func (v *Volume) CreatedTime() time.Time {
   127  	return v.config.CreatedTime
   128  }
   129  
   130  // Config returns the volume's configuration.
   131  func (v *Volume) Config() (*VolumeConfig, error) {
   132  	config := VolumeConfig{}
   133  	err := JSONDeepCopy(v.config, &config)
   134  	return &config, err
   135  }