summaryrefslogtreecommitdiff
path: root/libpod/volume_internal.go
blob: 88d940370d8b025d11dd2224ae95d35992f8feb7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package libpod

import (
	"os"
	"path/filepath"

	"github.com/containers/podman/v2/libpod/define"
	"github.com/pkg/errors"
)

// Creates a new volume
func newVolume(runtime *Runtime) *Volume {
	volume := new(Volume)
	volume.config = new(VolumeConfig)
	volume.state = new(VolumeState)
	volume.runtime = runtime
	volume.config.Labels = make(map[string]string)
	volume.config.Options = make(map[string]string)
	volume.state.NeedsCopyUp = true
	return volume
}

// teardownStorage deletes the volume from volumePath
func (v *Volume) teardownStorage() error {
	if v.UsesVolumeDriver() {
		return nil
	}

	// TODO: Should this be converted to use v.config.MountPoint?
	return os.RemoveAll(filepath.Join(v.runtime.config.Engine.VolumePath, v.Name()))
}

// Volumes with options set, or a filesystem type, or a device to mount need to
// be mounted and unmounted.
func (v *Volume) needsMount() bool {
	// Non-local driver always needs mount
	if v.UsesVolumeDriver() {
		return true
	}

	// Local driver with options needs mount
	return len(v.config.Options) > 0
}

// update() updates the volume state from the DB.
func (v *Volume) update() error {
	if err := v.runtime.state.UpdateVolume(v); err != nil {
		return err
	}
	if !v.valid {
		return define.ErrVolumeRemoved
	}
	return nil
}

// save() saves the volume state to the DB
func (v *Volume) save() error {
	return v.runtime.state.SaveVolume(v)
}

// Refresh volume state after a restart.
func (v *Volume) refresh() error {
	lock, err := v.runtime.lockManager.AllocateAndRetrieveLock(v.config.LockID)
	if err != nil {
		return errors.Wrapf(err, "error acquiring lock %d for volume %s", v.config.LockID, v.Name())
	}
	v.lock = lock

	return nil
}