forked from canonical/lxd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage_volumes_utils.go
580 lines (479 loc) · 14.9 KB
/
storage_volumes_utils.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
package main
import (
"fmt"
"path/filepath"
"strings"
"github.com/lxc/lxd/lxd/db"
"github.com/lxc/lxd/lxd/state"
"github.com/lxc/lxd/shared"
"github.com/lxc/lxd/shared/api"
"github.com/lxc/lxd/shared/version"
)
// XXX: backward compatible declarations, introduced when the db code was
// extracted to its own package. We should eventually clean this up.
const (
storagePoolVolumeTypeContainer = db.StoragePoolVolumeTypeContainer
storagePoolVolumeTypeImage = db.StoragePoolVolumeTypeImage
storagePoolVolumeTypeCustom = db.StoragePoolVolumeTypeCustom
)
const (
storagePoolVolumeTypeNameContainer = db.StoragePoolVolumeTypeNameContainer
storagePoolVolumeTypeNameImage = db.StoragePoolVolumeTypeNameImage
storagePoolVolumeTypeNameCustom = db.StoragePoolVolumeTypeNameCustom
)
// Leave the string type in here! This guarantees that go treats this is as a
// typed string constant. Removing it causes go to treat these as untyped string
// constants which is not what we want.
const (
storagePoolVolumeAPIEndpointContainers string = "containers"
storagePoolVolumeAPIEndpointImages string = "images"
storagePoolVolumeAPIEndpointCustom string = "custom"
)
var supportedVolumeTypes = []int{storagePoolVolumeTypeContainer, storagePoolVolumeTypeImage, storagePoolVolumeTypeCustom}
func storagePoolVolumeTypeNameToType(volumeTypeName string) (int, error) {
switch volumeTypeName {
case storagePoolVolumeTypeNameContainer:
return storagePoolVolumeTypeContainer, nil
case storagePoolVolumeTypeNameImage:
return storagePoolVolumeTypeImage, nil
case storagePoolVolumeTypeNameCustom:
return storagePoolVolumeTypeCustom, nil
}
return -1, fmt.Errorf("invalid storage volume type name")
}
func storagePoolVolumeTypeNameToAPIEndpoint(volumeTypeName string) (string, error) {
switch volumeTypeName {
case storagePoolVolumeTypeNameContainer:
return storagePoolVolumeAPIEndpointContainers, nil
case storagePoolVolumeTypeNameImage:
return storagePoolVolumeAPIEndpointImages, nil
case storagePoolVolumeTypeNameCustom:
return storagePoolVolumeAPIEndpointCustom, nil
}
return "", fmt.Errorf("invalid storage volume type name")
}
func storagePoolVolumeTypeToName(volumeType int) (string, error) {
return db.StoragePoolVolumeTypeToName(volumeType)
}
func storagePoolVolumeTypeToAPIEndpoint(volumeType int) (string, error) {
switch volumeType {
case storagePoolVolumeTypeContainer:
return storagePoolVolumeAPIEndpointContainers, nil
case storagePoolVolumeTypeImage:
return storagePoolVolumeAPIEndpointImages, nil
case storagePoolVolumeTypeCustom:
return storagePoolVolumeAPIEndpointCustom, nil
}
return "", fmt.Errorf("invalid storage volume type")
}
func storagePoolVolumeUpdate(state *state.State, poolName string, volumeName string, volumeType int, newDescription string, newConfig map[string]string) error {
s, err := storagePoolVolumeInit(state, poolName, volumeName, volumeType)
if err != nil {
return err
}
oldWritable := s.GetStoragePoolVolumeWritable()
newWritable := oldWritable
// Backup the current state
oldDescription := oldWritable.Description
oldConfig := map[string]string{}
err = shared.DeepCopy(&oldWritable.Config, &oldConfig)
if err != nil {
return err
}
// Define a function which reverts everything. Defer this function
// so that it doesn't need to be explicitly called in every failing
// return path. Track whether or not we want to undo the changes
// using a closure.
undoChanges := true
defer func() {
if undoChanges {
s.SetStoragePoolVolumeWritable(&oldWritable)
}
}()
// Diff the configurations
changedConfig := []string{}
userOnly := true
for key := range oldConfig {
if oldConfig[key] != newConfig[key] {
if !strings.HasPrefix(key, "user.") {
userOnly = false
}
if !shared.StringInSlice(key, changedConfig) {
changedConfig = append(changedConfig, key)
}
}
}
for key := range newConfig {
if oldConfig[key] != newConfig[key] {
if !strings.HasPrefix(key, "user.") {
userOnly = false
}
if !shared.StringInSlice(key, changedConfig) {
changedConfig = append(changedConfig, key)
}
}
}
// Apply config changes if there are any
if len(changedConfig) != 0 {
newWritable.Description = newDescription
newWritable.Config = newConfig
// Update the storage pool
if !userOnly {
err = s.StoragePoolVolumeUpdate(&newWritable, changedConfig)
if err != nil {
return err
}
}
// Apply the new configuration
s.SetStoragePoolVolumeWritable(&newWritable)
}
poolID, err := state.DB.StoragePoolGetID(poolName)
if err != nil {
return err
}
// Update the database if something changed
if len(changedConfig) != 0 || newDescription != oldDescription {
err = state.DB.StoragePoolVolumeUpdate(volumeName, volumeType, poolID, newDescription, newConfig)
if err != nil {
return err
}
}
// Success, update the closure to mark that the changes should be kept.
undoChanges = false
return nil
}
func storagePoolVolumeUsedByContainersGet(s *state.State, volumeName string,
volumeTypeName string) ([]string, error) {
cts, err := s.DB.ContainersList(db.CTypeRegular)
if err != nil {
return []string{}, err
}
ctsUsingVolume := []string{}
volumeNameWithType := fmt.Sprintf("%s/%s", volumeTypeName, volumeName)
for _, ct := range cts {
c, err := containerLoadByName(s, ct)
if err != nil {
continue
}
for _, dev := range c.LocalDevices() {
if dev["type"] != "disk" {
continue
}
// Make sure that we don't compare against stuff like
// "container////bla" but only against "container/bla".
cleanSource := filepath.Clean(dev["source"])
if cleanSource == volumeName || cleanSource == volumeNameWithType {
ctsUsingVolume = append(ctsUsingVolume, ct)
}
}
}
return ctsUsingVolume, nil
}
func storagePoolVolumeUpdateUsers(d *Daemon, oldPoolName string,
oldVolumeName string, newPoolName string, newVolumeName string) error {
s := d.State()
// update all containers
cts, err := s.DB.ContainersList(db.CTypeRegular)
if err != nil {
return err
}
for _, ct := range cts {
c, err := containerLoadByName(s, ct)
if err != nil {
continue
}
devices := c.LocalDevices()
for k := range devices {
if devices[k]["type"] != "disk" {
continue
}
// Can't be a storage volume.
if filepath.IsAbs(devices[k]["source"]) {
continue
}
if filepath.Clean(devices[k]["pool"]) != oldPoolName {
continue
}
dir, file := filepath.Split(devices[k]["source"])
dir = filepath.Clean(dir)
if dir != storagePoolVolumeTypeNameCustom {
continue
}
file = filepath.Clean(file)
if file != oldVolumeName {
continue
}
// found entry
if oldPoolName != newPoolName {
devices[k]["pool"] = newPoolName
}
if oldVolumeName != newVolumeName {
newSource := newVolumeName
if dir != "" {
newSource = fmt.Sprintf("%s/%s", storagePoolVolumeTypeNameCustom, newVolumeName)
}
devices[k]["source"] = newSource
}
}
args := db.ContainerArgs{
Architecture: c.Architecture(),
Description: c.Description(),
Config: c.LocalConfig(),
Devices: devices,
Ephemeral: c.IsEphemeral(),
Profiles: c.Profiles()}
err = c.Update(args, false)
if err != nil {
return err
}
}
// update all profiles
profiles, err := s.DB.Profiles()
if err != nil {
return err
}
for _, pName := range profiles {
id, profile, err := s.DB.ProfileGet(pName)
if err != nil {
return err
}
for k := range profile.Devices {
if profile.Devices[k]["type"] != "disk" {
continue
}
// Can't be a storage volume.
if filepath.IsAbs(profile.Devices[k]["source"]) {
continue
}
if filepath.Clean(profile.Devices[k]["pool"]) != oldPoolName {
continue
}
dir, file := filepath.Split(profile.Devices[k]["source"])
dir = filepath.Clean(dir)
if dir != storagePoolVolumeTypeNameCustom {
continue
}
file = filepath.Clean(file)
if file != oldVolumeName {
continue
}
// found entry
if oldPoolName != newPoolName {
profile.Devices[k]["pool"] = newPoolName
}
if oldVolumeName != newVolumeName {
newSource := newVolumeName
if dir != "" {
newSource = fmt.Sprintf("%s/%s", storagePoolVolumeTypeNameCustom, newVolumeName)
}
profile.Devices[k]["source"] = newSource
}
}
pUpdate := api.ProfilePut{}
pUpdate.Config = profile.Config
pUpdate.Description = profile.Description
pUpdate.Devices = profile.Devices
err = doProfileUpdate(d, pName, id, profile, pUpdate)
if err != nil {
return err
}
}
return nil
}
func storagePoolVolumeUsedByRunningContainersWithProfilesGet(s *state.State,
poolName string, volumeName string, volumeTypeName string,
runningOnly bool) ([]string, error) {
cts, err := s.DB.ContainersList(db.CTypeRegular)
if err != nil {
return []string{}, err
}
ctsUsingVolume := []string{}
volumeNameWithType := fmt.Sprintf("%s/%s", volumeTypeName, volumeName)
for _, ct := range cts {
c, err := containerLoadByName(s, ct)
if err != nil {
continue
}
if runningOnly && !c.IsRunning() {
continue
}
for _, dev := range c.ExpandedDevices() {
if dev["type"] != "disk" {
continue
}
if dev["pool"] != poolName {
continue
}
// Make sure that we don't compare against stuff like
// "container////bla" but only against "container/bla".
cleanSource := filepath.Clean(dev["source"])
if cleanSource == volumeName || cleanSource == volumeNameWithType {
ctsUsingVolume = append(ctsUsingVolume, ct)
}
}
}
return ctsUsingVolume, nil
}
// volumeUsedBy = append(volumeUsedBy, fmt.Sprintf("/%s/containers/%s", version.APIVersion, ct))
func storagePoolVolumeUsedByGet(s *state.State, volumeName string, volumeTypeName string) ([]string, error) {
// Handle container volumes
if volumeTypeName == "container" {
cName, sName, snap := containerGetParentAndSnapshotName(volumeName)
if snap {
return []string{fmt.Sprintf("/%s/containers/%s/snapshots/%s", version.APIVersion, cName, sName)}, nil
}
return []string{fmt.Sprintf("/%s/containers/%s", version.APIVersion, cName)}, nil
}
// Handle image volumes
if volumeTypeName == "image" {
return []string{fmt.Sprintf("/%s/images/%s", version.APIVersion, volumeName)}, nil
}
// Look for containers using this volume
ctsUsingVolume, err := storagePoolVolumeUsedByContainersGet(s,
volumeName, volumeTypeName)
if err != nil {
return []string{}, err
}
volumeUsedBy := []string{}
for _, ct := range ctsUsingVolume {
volumeUsedBy = append(volumeUsedBy,
fmt.Sprintf("/%s/containers/%s", version.APIVersion, ct))
}
profiles, err := profilesUsingPoolVolumeGetNames(s.DB, volumeName, volumeTypeName)
if err != nil {
return []string{}, err
}
if len(volumeUsedBy) == 0 && len(profiles) == 0 {
return []string{}, nil
}
for _, pName := range profiles {
volumeUsedBy = append(volumeUsedBy, fmt.Sprintf("/%s/profiles/%s", version.APIVersion, pName))
}
return volumeUsedBy, nil
}
func profilesUsingPoolVolumeGetNames(db *db.Node, volumeName string, volumeType string) ([]string, error) {
usedBy := []string{}
profiles, err := db.Profiles()
if err != nil {
return usedBy, err
}
for _, pName := range profiles {
_, profile, err := db.ProfileGet(pName)
if err != nil {
return usedBy, err
}
volumeNameWithType := fmt.Sprintf("%s/%s", volumeType, volumeName)
for _, v := range profile.Devices {
if v["type"] != "disk" {
continue
}
// Can't be a storage volume.
if filepath.IsAbs(v["source"]) {
continue
}
// Make sure that we don't compare against stuff
// like "container////bla" but only against
// "container/bla".
cleanSource := filepath.Clean(v["source"])
if cleanSource == volumeName || cleanSource == volumeNameWithType {
usedBy = append(usedBy, pName)
}
}
}
return usedBy, nil
}
func storagePoolVolumeDBCreate(s *state.State, poolName string, volumeName, volumeDescription string, volumeTypeName string, volumeConfig map[string]string) error {
// Check that the name of the new storage volume is valid. (For example.
// zfs pools cannot contain "/" in their names.)
err := storageValidName(volumeName)
if err != nil {
return err
}
// Convert the volume type name to our internal integer representation.
volumeType, err := storagePoolVolumeTypeNameToType(volumeTypeName)
if err != nil {
return err
}
// Load storage pool the volume will be attached to.
poolID, poolStruct, err := s.DB.StoragePoolGet(poolName)
if err != nil {
return err
}
// Check that a storage volume of the same storage volume type does not
// already exist.
volumeID, _ := s.DB.StoragePoolVolumeGetTypeID(volumeName, volumeType, poolID)
if volumeID > 0 {
return fmt.Errorf("a storage volume of type %s does already exist", volumeTypeName)
}
// Make sure that we don't pass a nil to the next function.
if volumeConfig == nil {
volumeConfig = map[string]string{}
}
// Validate the requested storage volume configuration.
err = storageVolumeValidateConfig(poolName, volumeConfig, poolStruct)
if err != nil {
return err
}
err = storageVolumeFillDefault(poolName, volumeConfig, poolStruct)
if err != nil {
return err
}
// Create the database entry for the storage volume.
_, err = s.DB.StoragePoolVolumeCreate(volumeName, volumeDescription, volumeType, poolID, volumeConfig)
if err != nil {
return fmt.Errorf("Error inserting %s of type %s into database: %s", poolName, volumeTypeName, err)
}
return nil
}
func storagePoolVolumeCreateInternal(state *state.State, poolName string, vol *api.StorageVolumesPost) error {
volumeName := vol.Name
volumeDescription := vol.Description
volumeTypeName := vol.Type
volumeConfig := vol.Config
if vol.Source.Name != "" {
// Initialize instance of new pool to translate properties
// between storage drivers.
s, err := storagePoolInit(state, poolName)
if err != nil {
return err
}
driver := s.GetStorageTypeName()
newConfig, err := storageVolumePropertiesTranslate(vol.Config, driver)
if err != nil {
return err
}
vol.Config = newConfig
volumeConfig = newConfig
}
// Create database entry for new storage volume.
err := storagePoolVolumeDBCreate(state, poolName, volumeName, volumeDescription, volumeTypeName, volumeConfig)
if err != nil {
return err
}
// Convert the volume type name to our internal integer representation.
poolID, err := state.DB.StoragePoolGetID(poolName)
if err != nil {
return err
}
volumeType, err := storagePoolVolumeTypeNameToType(volumeTypeName)
if err != nil {
state.DB.StoragePoolVolumeDelete(volumeName, volumeType, poolID)
return err
}
// Initialize new storage volume on the target storage pool.
s, err := storagePoolVolumeInit(state, poolName, volumeName, volumeType)
if err != nil {
state.DB.StoragePoolVolumeDelete(volumeName, volumeType, poolID)
return err
}
if vol.Source.Name == "" {
err = s.StoragePoolVolumeCreate()
} else {
err = s.StoragePoolVolumeCopy(&vol.Source)
}
if err != nil {
state.DB.StoragePoolVolumeDelete(volumeName, volumeType, poolID)
return err
}
return nil
}