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

     1  package libpod
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  
     7  	"github.com/containers/libpod/libpod/define"
     8  	"github.com/pkg/errors"
     9  )
    10  
    11  // Creates a new volume
    12  func newVolume(runtime *Runtime) (*Volume, error) {
    13  	volume := new(Volume)
    14  	volume.config = new(VolumeConfig)
    15  	volume.state = new(VolumeState)
    16  	volume.runtime = runtime
    17  	volume.config.Labels = make(map[string]string)
    18  	volume.config.Options = make(map[string]string)
    19  	volume.state.NeedsCopyUp = true
    20  
    21  	return volume, nil
    22  }
    23  
    24  // teardownStorage deletes the volume from volumePath
    25  func (v *Volume) teardownStorage() error {
    26  	return os.RemoveAll(filepath.Join(v.runtime.config.Engine.VolumePath, v.Name()))
    27  }
    28  
    29  // Volumes with options set, or a filesystem type, or a device to mount need to
    30  // be mounted and unmounted.
    31  func (v *Volume) needsMount() bool {
    32  	return len(v.config.Options) > 0 && v.config.Driver == define.VolumeDriverLocal
    33  }
    34  
    35  // update() updates the volume state from the DB.
    36  func (v *Volume) update() error {
    37  	if err := v.runtime.state.UpdateVolume(v); err != nil {
    38  		return err
    39  	}
    40  	if !v.valid {
    41  		return define.ErrVolumeRemoved
    42  	}
    43  	return nil
    44  }
    45  
    46  // save() saves the volume state to the DB
    47  func (v *Volume) save() error {
    48  	return v.runtime.state.SaveVolume(v)
    49  }
    50  
    51  // Refresh volume state after a restart.
    52  func (v *Volume) refresh() error {
    53  	lock, err := v.runtime.lockManager.AllocateAndRetrieveLock(v.config.LockID)
    54  	if err != nil {
    55  		return errors.Wrapf(err, "error acquiring lock %d for volume %s", v.config.LockID, v.Name())
    56  	}
    57  	v.lock = lock
    58  
    59  	return nil
    60  }