/
layer0.go
301 lines (267 loc) · 7.84 KB
/
layer0.go
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package layer0
import (
"context"
"fmt"
"os"
"path"
"strings"
"sync"
"sync/atomic"
"github.com/sirupsen/logrus"
"github.com/docker/docker/daemon/graphdriver"
"github.com/docker/docker/daemon/graphdriver/overlay"
"github.com/docker/docker/pkg/archive"
"github.com/docker/docker/pkg/idtools"
"github.com/docker/docker/pkg/parsers"
"github.com/libopenstorage/openstorage/api"
"github.com/libopenstorage/openstorage/graph"
"github.com/libopenstorage/openstorage/pkg/options"
"github.com/libopenstorage/openstorage/volume"
volumedrivers "github.com/libopenstorage/openstorage/volume/drivers"
)
// Layer0 implemenation piggy backs on existing overlay graphdriver implementation
// to provide persistent storage for the uppermost/writeable layer in the
// container rootfs. The persistent storage is derived from one of the OSD volume drivers.
// To use this as the graphdriver in Docker with aws as the backend volume provider:
//
// DOCKER_STORAGE_OPTIONS= -s layer0 --storage-opt layer0.volume_driver=aws
// Layer0Vol represents the volume
type Layer0Vol struct {
// id self referential ID
id string
// parent image string
parent string
// path where the external volume is mounted.
path string
// volumeID mapping to this external volume
volumeID string
// ref keeps track of mount and unmounts.
ref int32
}
// Layer0 implements the graphdriver interface
type Layer0 struct {
sync.Mutex
// Driver is an implementation of GraphDriver. Only select methods are overridden
graphdriver.Driver
// home base string
home string
// volumes maintains a map of currently mounted volumes.
volumes map[string]*Layer0Vol
// volDriver is the volume driver used for the writeable layer.
volDriver volume.VolumeDriver
}
// Layer0Graphdriver options. This should be passed in as a st
const (
// Name of the driver
Name = "layer0"
// Type of the driver
Type = api.DriverType_DRIVER_TYPE_GRAPH
// Layer0VolumeDriver constant
Layer0VolumeDriver = "layer0.volume_driver"
)
func init() {
graph.Register(Name, Init)
}
// Init initializes the driver
func Init(home string, options []string, uidMaps, gidMaps []idtools.IDMap) (graphdriver.Driver, error) {
var volumeDriver string
for _, option := range options {
key, val, err := parsers.ParseKeyValueOpt(option)
if err != nil {
return nil, err
}
switch key {
case Layer0VolumeDriver:
volumeDriver = val
default:
return nil, fmt.Errorf("Unknown option %s\n", key)
}
}
logrus.Infof("Layer0 volume driver: %v", volumeDriver)
volDriver, err := volumedrivers.Get(volumeDriver)
if err != nil {
return nil, err
}
ov, err := overlay.Init(home, options, uidMaps, gidMaps)
if err != nil {
volDriver.Shutdown()
return nil, err
}
d := &Layer0{
Driver: ov,
home: home,
volumes: make(map[string]*Layer0Vol),
volDriver: volDriver,
}
return d, nil
}
func (l *Layer0) isLayer0Parent(id string) (string, bool) {
// This relies on an <instance_id>-init volume being created for
// every new container.
if strings.HasSuffix(id, "-init") {
return strings.TrimSuffix(id, "-init"), true
}
return "", false
}
func (l *Layer0) isLayer0(id string) bool {
if strings.HasSuffix(id, "-init") {
baseID := strings.TrimSuffix(id, "-init")
if _, ok := l.volumes[baseID]; !ok {
l.volumes[baseID] = &Layer0Vol{id: baseID}
}
return false
}
_, ok := l.volumes[id]
return ok
}
func (l *Layer0) loID(id string) string {
return id + "-vol"
}
func (l *Layer0) realID(id string) string {
if l.isLayer0(id) {
return path.Join(l.loID(id), id)
}
return id
}
func (l *Layer0) create(id, parent string) (string, *Layer0Vol, error) {
l.Lock()
defer l.Unlock()
// If this is the parent of the Layer0, add an entry for it.
baseID, l0 := l.isLayer0Parent(id)
if l0 {
l.volumes[baseID] = &Layer0Vol{id: baseID, parent: parent}
return id, nil, nil
}
// Don't do anything if this is not layer 0
if !l.isLayer0(id) {
return id, nil, nil
}
vol, ok := l.volumes[id]
if !ok {
logrus.Warnf("Failed to find layer0 volume for id %v", id)
return id, nil, nil
}
// Query volume for Layer 0
vols, err := l.volDriver.Enumerate(&api.VolumeLocator{Name: vol.parent}, nil)
// If we don't find a volume configured for this image,
// then don't track layer0
if err != nil || vols == nil {
logrus.Infof("Failed to find configured volume for id %v", vol.parent)
delete(l.volumes, id)
return id, nil, nil
}
// Find a volume that is available.
index := -1
for i, v := range vols {
if len(v.AttachPath) == 0 {
index = i
break
}
}
if index == -1 {
logrus.Infof("Failed to find free volume for id %v", vol.parent)
delete(l.volumes, id)
return id, nil, nil
}
mountPath := path.Join(l.home, l.loID(id))
os.MkdirAll(mountPath, 0755)
// If this is a block driver, first attach the volume.
if l.volDriver.Type() == api.DriverType_DRIVER_TYPE_BLOCK {
_, err := l.volDriver.Attach(context.TODO(), vols[index].Id, nil)
if err != nil {
logrus.Errorf("Failed to attach volume %v", vols[index].Id)
delete(l.volumes, id)
return id, nil, nil
}
}
err = l.volDriver.Mount(context.TODO(), vols[index].Id, mountPath, nil)
if err != nil {
logrus.Errorf("Failed to mount volume %v at path %v",
vols[index].Id, mountPath)
delete(l.volumes, id)
return id, nil, nil
}
vol.path = mountPath
vol.volumeID = vols[index].Id
vol.ref = 1
return l.realID(id), vol, nil
}
// Create creates a new and empty filesystem layer
func (l *Layer0) Create(id string, parent string, mountLabel string, storageOpts map[string]string) error {
id, vol, err := l.create(id, parent)
if err != nil {
return err
}
err = l.Driver.Create(id, parent, mountLabel, storageOpts)
if err != nil || vol == nil {
return err
}
// This is layer0. Restore saved upper dir, if one exists.
savedUpper := path.Join(vol.path, "upper")
if _, err := os.Stat(savedUpper); err != nil {
// It's not an error if didn't have a saved upper
return nil
}
// We found a saved upper, restore to newly created upper.
upperDir := path.Join(path.Join(l.home, id), "upper")
os.RemoveAll(upperDir)
return os.Rename(savedUpper, upperDir)
}
// Remove removes a layer based on its id
func (l *Layer0) Remove(id string) error {
if !l.isLayer0(id) {
return l.Driver.Remove(l.realID(id))
}
l.Lock()
defer l.Unlock()
var err error
v, ok := l.volumes[id]
if ok {
atomic.AddInt32(&v.ref, -1)
if v.ref == 0 {
// Save the upper dir and blow away the rest.
upperDir := path.Join(path.Join(l.home, l.realID(id)), "upper")
err := os.Rename(upperDir, path.Join(v.path, "upper"))
if err != nil {
logrus.Warnf("Failed in rename(%v): %v", id, err)
}
l.Driver.Remove(l.realID(id))
opts := make(map[string]string)
opts[options.OptionsDeleteAfterUnmount] = "true"
err = l.volDriver.Unmount(context.TODO(), v.volumeID, v.path, opts)
if l.volDriver.Type() == api.DriverType_DRIVER_TYPE_BLOCK {
_ = l.volDriver.Detach(context.TODO(), v.volumeID, nil)
}
err = os.RemoveAll(v.path)
delete(l.volumes, v.id)
}
} else {
logrus.Warnf("Failed to find layer0 vol for id %v", id)
}
return err
}
// Get returns the mountpoint for the layered filesystem
func (l *Layer0) Get(id string, mountLabel string) (string, error) {
id = l.realID(id)
return l.Driver.Get(id, mountLabel)
}
// Put releases the system resources for the specified id
func (l *Layer0) Put(id string) error {
id = l.realID(id)
return l.Driver.Put(id)
}
// ApplyDiff extracts the changeset between the specified layer and its parent
func (l *Layer0) ApplyDiff(id string, parent string, diff archive.Reader) (size int64, err error) {
id = l.realID(id)
return l.Driver.ApplyDiff(id, parent, diff)
}
// Exists checks if leyr exists
func (l *Layer0) Exists(id string) bool {
id = l.realID(id)
return l.Driver.Exists(id)
}
// GetMetadata returns key-value pairs
func (l *Layer0) GetMetadata(id string) (map[string]string, error) {
id = l.realID(id)
return l.Driver.GetMetadata(id)
}