forked from canonical/lxd
/
storage_ceph_utils.go
1673 lines (1488 loc) · 47.3 KB
/
storage_ceph_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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
"strconv"
"strings"
"syscall"
"github.com/lxc/lxd/lxd/db"
"github.com/lxc/lxd/shared"
"github.com/lxc/lxd/shared/logger"
"github.com/pborman/uuid"
)
// cephOSDPoolExists checks whether a given OSD pool exists.
func cephOSDPoolExists(ClusterName string, poolName string, userName string) bool {
_, err := shared.RunCommand(
"ceph",
"--name", fmt.Sprintf("client.%s", userName),
"--cluster", ClusterName,
"osd",
"pool",
"get",
poolName,
"size")
if err != nil {
return false
}
return true
}
// cephOSDPoolDestroy destroys an OSD pool.
// - A call to cephOSDPoolDestroy will destroy a pool including any storage
// volumes that still exist in the pool.
// - In case the OSD pool that is supposed to be deleted does not exist this
// command will still exit 0. This means that if the caller wants to be sure
// that this call actually deleted an OSD pool it needs to check for the
// existence of the pool first.
func cephOSDPoolDestroy(clusterName string, poolName string, userName string) error {
_, err := shared.RunCommand("ceph",
"--name", fmt.Sprintf("client.%s", userName),
"--cluster", clusterName,
"osd",
"pool",
"delete",
poolName,
poolName,
"--yes-i-really-really-mean-it")
if err != nil {
return err
}
return nil
}
// cephRBDVolumeCreate creates an RBD storage volume.
// Note that the set of features is intentionally limited is intentionally
// limited by passing --image-feature explicitly. This is done to ensure that
// the chances of a conflict between the features supported by the userspace
// library and the kernel module are minimized. Otherwise random panics might
// occur.
func cephRBDVolumeCreate(clusterName string, poolName string, volumeName string,
volumeType string, size string, userName string) error {
_, err := shared.RunCommand(
"rbd",
"--id", userName,
"--image-feature", "layering,",
"--cluster", clusterName,
"--pool", poolName,
"--size", size,
"create",
fmt.Sprintf("%s_%s", volumeType, volumeName))
return err
}
// cephRBDVolumeExists checks whether a given RBD storage volume exists.
func cephRBDVolumeExists(clusterName string, poolName string, volumeName string,
volumeType string, userName string) bool {
_, err := shared.RunCommand(
"rbd",
"--id", userName,
"--cluster", clusterName,
"--pool", poolName,
"image-meta",
"list",
fmt.Sprintf("%s_%s", volumeType, volumeName))
if err != nil {
return false
}
return true
}
// cephRBDVolumeSnapshotExists checks whether a given RBD snapshot exists.
func cephRBDSnapshotExists(clusterName string, poolName string,
volumeName string, volumeType string, snapshotName string,
userName string) bool {
_, err := shared.RunCommand(
"rbd",
"--id", userName,
"--cluster", clusterName,
"--pool", poolName,
"info",
fmt.Sprintf("%s_%s@%s", volumeType, volumeName, snapshotName))
if err != nil {
return false
}
return true
}
// cephRBDVolumeDelete deletes an RBD storage volume.
// - In case the RBD storage volume that is supposed to be deleted does not
// exist this command will still exit 0. This means that if the caller wants
// to be sure that this call actually deleted an RBD storage volume it needs
// to check for the existence of the pool first.
func cephRBDVolumeDelete(clusterName string, poolName string, volumeName string,
volumeType string, userName string) error {
_, err := shared.RunCommand(
"rbd",
"--id", userName,
"--cluster", clusterName,
"--pool", poolName,
"rm",
fmt.Sprintf("%s_%s", volumeType, volumeName))
if err != nil {
return err
}
return nil
}
// cephRBDVolumeMap maps a given RBD storage volume
// This will ensure that the RBD storage volume is accessible as a block device
// in the /dev directory and is therefore necessary in order to mount it.
func cephRBDVolumeMap(clusterName string, poolName string, volumeName string,
volumeType string, userName string) (string, error) {
devPath, err := shared.RunCommand(
"rbd",
"--id", userName,
"--cluster", clusterName,
"--pool", poolName,
"map",
fmt.Sprintf("%s_%s", volumeType, volumeName))
if err != nil {
return "", err
}
idx := strings.Index(devPath, "/dev/rbd")
if idx < 0 {
return "", fmt.Errorf("Failed to detect mapped device path")
}
devPath = devPath[idx:]
return strings.TrimSpace(devPath), nil
}
// cephRBDVolumeUnmap unmaps a given RBD storage volume
// This is a precondition in order to delete an RBD storage volume can.
func cephRBDVolumeUnmap(clusterName string, poolName string, volumeName string,
volumeType string, userName string, unmapUntilEINVAL bool) error {
unmapImageName := fmt.Sprintf("%s_%s", volumeType, volumeName)
again:
_, err := shared.RunCommand(
"rbd",
"--id", userName,
"--cluster", clusterName,
"--pool", poolName,
"unmap",
unmapImageName)
if err != nil {
runError, ok := err.(shared.RunError)
if ok {
exitError, ok := runError.Err.(*exec.ExitError)
if ok {
waitStatus := exitError.Sys().(syscall.WaitStatus)
if waitStatus.ExitStatus() == 22 {
// EINVAL (already unmapped)
return nil
}
}
}
return err
}
if unmapUntilEINVAL {
goto again
}
return nil
}
// cephRBDVolumeSnapshotUnmap unmaps a given RBD snapshot
// This is a precondition in order to delete an RBD snapshot can.
func cephRBDVolumeSnapshotUnmap(clusterName string, poolName string,
volumeName string, volumeType string, snapshotName string,
userName string, unmapUntilEINVAL bool) error {
unmapSnapshotName := fmt.Sprintf("%s_%s@%s", volumeType, volumeName,
snapshotName)
again:
_, err := shared.RunCommand(
"rbd",
"--id", userName,
"--cluster", clusterName,
"--pool", poolName,
"unmap",
unmapSnapshotName)
if err != nil {
runError, ok := err.(shared.RunError)
if ok {
exitError, ok := runError.Err.(*exec.ExitError)
if ok {
waitStatus := exitError.Sys().(syscall.WaitStatus)
if waitStatus.ExitStatus() == 22 {
// EINVAL (already unmapped)
return nil
}
}
}
return err
}
if unmapUntilEINVAL {
goto again
}
return nil
}
// cephRBDSnapshotCreate creates a read-write snapshot of a given RBD storage
// volume
func cephRBDSnapshotCreate(clusterName string, poolName string,
volumeName string, volumeType string, snapshotName string,
userName string) error {
_, err := shared.RunCommand(
"rbd",
"--id", userName,
"--cluster", clusterName,
"--pool", poolName,
"snap",
"create",
"--snap", snapshotName,
fmt.Sprintf("%s_%s", volumeType, volumeName))
if err != nil {
return err
}
return nil
}
// cephRBDSnapshotsPurge deletes all snapshot of a given RBD storage volume
// Note that this will only succeed if none of the snapshots are protected.
func cephRBDSnapshotsPurge(clusterName string, poolName string,
volumeName string, volumeType string, userName string) error {
_, err := shared.RunCommand(
"rbd",
"--id", userName,
"--cluster", clusterName,
"--pool", poolName,
"snap",
"purge",
fmt.Sprintf("%s_%s", volumeType, volumeName))
if err != nil {
return err
}
return nil
}
// cephRBDSnapshotProtect protects a given snapshot from being deleted
// This is a precondition to be able to create RBD clones from a given snapshot.
func cephRBDSnapshotProtect(clusterName string, poolName string,
volumeName string, volumeType string, snapshotName string,
userName string) error {
_, err := shared.RunCommand(
"rbd",
"--id", userName,
"--cluster", clusterName,
"--pool", poolName,
"snap",
"protect",
"--snap", snapshotName,
fmt.Sprintf("%s_%s", volumeType, volumeName))
if err != nil {
runError, ok := err.(shared.RunError)
if ok {
exitError, ok := runError.Err.(*exec.ExitError)
if ok {
waitStatus := exitError.Sys().(syscall.WaitStatus)
if waitStatus.ExitStatus() == 16 {
// EBUSY (snapshot already protected)
return nil
}
}
}
return err
}
return nil
}
// cephRBDSnapshotUnprotect unprotects a given snapshot
// - This is a precondition to be able to delete an RBD snapshot.
// - This command will only succeed if the snapshot does not have any clones.
func cephRBDSnapshotUnprotect(clusterName string, poolName string,
volumeName string, volumeType string, snapshotName string,
userName string) error {
_, err := shared.RunCommand(
"rbd",
"--id", userName,
"--cluster", clusterName,
"--pool", poolName,
"snap",
"unprotect",
"--snap", snapshotName,
fmt.Sprintf("%s_%s", volumeType, volumeName))
if err != nil {
runError, ok := err.(shared.RunError)
if ok {
exitError, ok := runError.Err.(*exec.ExitError)
if ok {
waitStatus := exitError.Sys().(syscall.WaitStatus)
if waitStatus.ExitStatus() == 22 {
// EBUSY (snapshot already unprotected)
return nil
}
}
}
return err
}
return nil
}
// cephRBDCloneCreate creates a clone from a protected RBD snapshot
func cephRBDCloneCreate(sourceClusterName string, sourcePoolName string,
sourceVolumeName string, sourceVolumeType string,
sourceSnapshotName string, targetPoolName string,
targetVolumeName string, targetVolumeType string,
userName string) error {
_, err := shared.RunCommand(
"rbd",
"--id", userName,
"--cluster", sourceClusterName,
"--image-feature", "layering",
"clone",
fmt.Sprintf("%s/%s_%s@%s", sourcePoolName, sourceVolumeType,
sourceVolumeName, sourceSnapshotName),
fmt.Sprintf("%s/%s_%s", targetPoolName, targetVolumeType,
targetVolumeName))
if err != nil {
return err
}
return nil
}
// cephRBDSnapshotListClones list all clones of an RBD snapshot
func cephRBDSnapshotListClones(clusterName string, poolName string,
volumeName string, volumeType string,
snapshotName string, userName string) ([]string, error) {
msg, err := shared.RunCommand(
"rbd",
"--id", userName,
"--cluster", clusterName,
"--pool", poolName,
"children",
"--image", fmt.Sprintf("%s_%s", volumeType, volumeName),
"--snap", snapshotName)
if err != nil {
return nil, err
}
msg = strings.TrimSpace(msg)
clones := strings.Fields(msg)
if len(clones) == 0 {
return nil, db.NoSuchObjectError
}
return clones, nil
}
// cephRBDVolumeMarkDeleted marks an RBD storage volume as being in "zombie"
// state
// An RBD storage volume that is in zombie state is not tracked in LXD's
// database anymore but still needs to be kept around for the sake of any
// dependent storage entities in the storage pool. This usually happens when an
// RBD storage volume has protected snapshots; a scenario most common when
// creating a sparse copy of a container or when LXD updated an image and the
// image still has dependent container clones.
func cephRBDVolumeMarkDeleted(clusterName string, poolName string,
volumeType string, oldVolumeName string, newVolumeName string,
userName string, suffix string) error {
deletedName := fmt.Sprintf("%s/zombie_%s_%s", poolName, volumeType,
newVolumeName)
if suffix != "" {
deletedName = fmt.Sprintf("%s_%s", deletedName, suffix)
}
_, err := shared.RunCommand(
"rbd",
"--id", userName,
"--cluster", clusterName,
"mv",
fmt.Sprintf("%s/%s_%s", poolName, volumeType, oldVolumeName),
deletedName)
if err != nil {
return err
}
return nil
}
// cephRBDVolumeUnmarkDeleted unmarks an RBD storage volume as being in "zombie"
// state
// - An RBD storage volume that is in zombie is not tracked in LXD's database
// anymore but still needs to be kept around for the sake of any dependent
// storage entities in the storage pool.
// - This function is mostly used when a user has deleted the storage volume of
// an image from the storage pool and then triggers a container creation. If
// LXD detects that the storage volume for the given hash already exists in
// the pool but is marked as "zombie" it will unmark it as a zombie instead of
// creating another storage volume for the image.
func cephRBDVolumeUnmarkDeleted(clusterName string, poolName string,
volumeName string, volumeType string, userName string, oldSuffix string,
newSuffix string) error {
oldName := fmt.Sprintf("%s/zombie_%s_%s", poolName, volumeType, volumeName)
if oldSuffix != "" {
oldName = fmt.Sprintf("%s_%s", oldName, oldSuffix)
}
newName := fmt.Sprintf("%s/%s_%s", poolName, volumeType, volumeName)
if newSuffix != "" {
newName = fmt.Sprintf("%s_%s", newName, newSuffix)
}
_, err := shared.RunCommand(
"rbd",
"--id", userName,
"--cluster", clusterName,
"mv",
oldName,
newName)
if err != nil {
return err
}
return nil
}
// cephRBDVolumeRename renames a given RBD storage volume
// Note that this usually requires that the image be unmapped under its original
// name, then renamed, and finally will be remapped again. If it is not unmapped
// under its original name and the callers maps it under its new name the image
// will be mapped twice. This will prevent it from being deleted.
func cephRBDVolumeRename(clusterName string, poolName string, volumeType string,
oldVolumeName string, newVolumeName string, userName string) error {
_, err := shared.RunCommand(
"rbd",
"--id", userName,
"--cluster", clusterName,
"mv",
fmt.Sprintf("%s/%s_%s", poolName, volumeType, oldVolumeName),
fmt.Sprintf("%s/%s_%s", poolName, volumeType, newVolumeName))
if err != nil {
return err
}
return nil
}
// cephRBDVolumeRename renames a given RBD storage volume
// Note that if the snapshot is mapped - which it usually shouldn't be - this
// usually requires that the snapshot be unmapped under its original name, then
// renamed, and finally will be remapped again. If it is not unmapped under its
// original name and the caller maps it under its new name the snapshot will be
// mapped twice. This will prevent it from being deleted.
func cephRBDVolumeSnapshotRename(clusterName string, poolName string,
volumeName string, volumeType string, oldSnapshotName string,
newSnapshotName string, userName string) error {
_, err := shared.RunCommand(
"rbd",
"--id", userName,
"--cluster", clusterName,
"snap",
"rename",
fmt.Sprintf("%s/%s_%s@%s", poolName, volumeType, volumeName,
oldSnapshotName),
fmt.Sprintf("%s/%s_%s@%s", poolName, volumeType, volumeName,
newSnapshotName))
if err != nil {
return err
}
return nil
}
// cephRBDVolumeGetParent will return the snapshot the RBD clone was created
// from
// - If the RBD storage volume is not a clone then this function will return
// db.NoSuchObjectError.
// - The snapshot will be returned as
// <osd-pool-name>/<rbd-volume-name>@<rbd-snapshot-name>
// The caller will usually want to parse this according to its needs. This
// helper library provides two small functions to do this but see below.
func cephRBDVolumeGetParent(clusterName string, poolName string,
volumeName string, volumeType string, userName string) (string, error) {
msg, err := shared.RunCommand(
"rbd",
"--id", userName,
"--cluster", clusterName,
"--pool", poolName,
"info",
fmt.Sprintf("%s_%s", volumeType, volumeName))
if err != nil {
return "", err
}
idx := strings.Index(msg, "parent: ")
if idx == -1 {
return "", db.NoSuchObjectError
}
msg = msg[(idx + len("parent: ")):]
msg = strings.TrimSpace(msg)
idx = strings.Index(msg, "\n")
if idx == -1 {
return "", fmt.Errorf("Unexpected parsing error")
}
msg = msg[:idx]
msg = strings.TrimSpace(msg)
return msg, nil
}
// cephRBDSnapshotDelete deletes an RBD snapshot
// This requires that the snapshot does not have any clones and is unmapped and
// unprotected.
func cephRBDSnapshotDelete(clusterName string, poolName string,
volumeName string, volumeType string, snapshotName string,
userName string) error {
_, err := shared.RunCommand(
"rbd",
"--id", userName,
"--cluster", clusterName,
"--pool", poolName,
"snap",
"rm",
fmt.Sprintf("%s_%s@%s", volumeType, volumeName, snapshotName))
if err != nil {
return err
}
return nil
}
// cephRBDVolumeCopy copies an RBD storage volume
// This is a non-sparse copy which doesn't introduce any dependency relationship
// between the source RBD storage volume and the target RBD storage volume. The
// operations is similar to creating an empty RBD storage volume and rsyncing
// the contents of the source RBD storage volume into it.
func cephRBDVolumeCopy(clusterName string, oldVolumeName string,
newVolumeName string, userName string) error {
_, err := shared.RunCommand(
"rbd",
"--id", userName,
"--cluster", clusterName,
"cp",
oldVolumeName,
newVolumeName)
if err != nil {
return err
}
return nil
}
// cephRBDVolumeListSnapshots retrieves the snapshots of an RBD storage volume
// The format of the snapshot names is simply the part after the @. So given a
// valid RBD path relative to a pool
// <osd-pool-name>/<rbd-storage-volume>@<rbd-snapshot-name>
// this will only return
// <rbd-snapshot-name>
func cephRBDVolumeListSnapshots(clusterName string, poolName string,
volumeName string, volumeType string,
userName string) ([]string, error) {
msg, err := shared.RunCommand(
"rbd",
"--id", userName,
"--format", "json",
"--cluster", clusterName,
"--pool", poolName,
"snap",
"ls", fmt.Sprintf("%s_%s", volumeType, volumeName))
if err != nil {
return []string{}, err
}
var data []map[string]interface{}
err = json.Unmarshal([]byte(msg), &data)
if err != nil {
return []string{}, err
}
snapshots := []string{}
for _, v := range data {
_, ok := v["name"]
if !ok {
return []string{}, fmt.Errorf("No \"name\" property found")
}
name, ok := v["name"].(string)
if !ok {
return []string{}, fmt.Errorf("\"name\" property did not have string type")
}
name = strings.TrimSpace(name)
snapshots = append(snapshots, name)
}
if len(snapshots) == 0 {
return []string{}, db.NoSuchObjectError
}
return snapshots, nil
}
// cephRBDVolumeRestore restores an RBD storage volume to the state of one of
// its snapshots
func cephRBDVolumeRestore(clusterName string, poolName string, volumeName string,
volumeType string, snapshotName string, userName string) error {
_, err := shared.RunCommand(
"rbd",
"--id", userName,
"--cluster", clusterName,
"--pool", poolName,
"snap",
"rollback",
"--snap", snapshotName,
fmt.Sprintf("%s_%s", volumeType, volumeName))
if err != nil {
return err
}
return nil
}
// getRBDSize returns the size the RBD storage volume is supposed to be created
// with
func (s *storageCeph) getRBDSize() (string, error) {
sz, err := shared.ParseByteSizeString(s.volume.Config["size"])
if err != nil {
return "", err
}
// Safety net: Set to default value.
if sz == 0 {
sz, _ = shared.ParseByteSizeString("10GB")
}
return fmt.Sprintf("%dB", sz), nil
}
// getRBDFilesystem returns the filesystem the RBD storage volume is supposed to
// be created with
func (s *storageCeph) getRBDFilesystem() string {
if s.volume.Config["block.filesystem"] != "" {
return s.volume.Config["block.filesystem"]
}
if s.pool.Config["volume.block.filesystem"] != "" {
return s.pool.Config["volume.block.filesystem"]
}
return "ext4"
}
// getRBDMountOptions returns the mount options the storage volume is supposed
// to be mounted with
// The option string that is returned needs to be passed to the approriate
// helper (currently named "lxdResolveMountoptions") which will take on the job
// of splitting it into appropriate flags and string options.
func (s *storageCeph) getRBDMountOptions() string {
if s.volume.Config["block.mount_options"] != "" {
return s.volume.Config["block.mount_options"]
}
if s.pool.Config["volume.block.mount_options"] != "" {
return s.pool.Config["volume.block.mount_options"]
}
return "discard"
}
// copyWithoutSnapshotsFull creates a non-sparse copy of a container
// This does not introduce a dependency relation between the source RBD storage
// volume and the target RBD storage volume.
func (s *storageCeph) copyWithoutSnapshotsFull(target container,
source container) error {
logger.Debugf(`Creating non-sparse copy of RBD storage volume for `+
`container "%s" -> "%s" without snapshots`, source.Name(),
target.Name())
sourceIsSnapshot := source.IsSnapshot()
sourceContainerName := source.Name()
targetContainerName := target.Name()
oldVolumeName := fmt.Sprintf("%s/container_%s", s.OSDPoolName,
sourceContainerName)
newVolumeName := fmt.Sprintf("%s/container_%s", s.OSDPoolName,
targetContainerName)
if sourceIsSnapshot {
sourceContainerOnlyName, sourceSnapshotOnlyName, _ :=
containerGetParentAndSnapshotName(sourceContainerName)
oldVolumeName = fmt.Sprintf("%s/container_%s@snapshot_%s",
s.OSDPoolName, sourceContainerOnlyName,
sourceSnapshotOnlyName)
}
err := cephRBDVolumeCopy(s.ClusterName, oldVolumeName, newVolumeName,
s.UserName)
if err != nil {
logger.Debugf(`Failed to create full RBD copy "%s" -> `+
`"%s": %s`, source.Name(), target.Name(), err)
return err
}
_, err = cephRBDVolumeMap(s.ClusterName, s.OSDPoolName, targetContainerName,
storagePoolVolumeTypeNameContainer, s.UserName)
if err != nil {
logger.Errorf(`Failed to map RBD storage volume for image `+
`"%s" on storage pool "%s": %s`, targetContainerName,
s.pool.Name, err)
return err
}
targetContainerMountPoint := getContainerMountPoint(s.pool.Name,
target.Name())
err = createContainerMountpoint(targetContainerMountPoint, target.Path(),
target.IsPrivileged())
if err != nil {
return err
}
ourMount, err := target.StorageStart()
if err != nil {
return err
}
if ourMount {
defer target.StorageStop()
}
err = target.TemplateApply("copy")
if err != nil {
logger.Errorf(`Failed to apply copy template for container `+
`"%s": %s`, target.Name(), err)
return err
}
logger.Debugf(`Applied copy template for container "%s"`, target.Name())
logger.Debugf(`Created non-sparse copy of RBD storage volume for `+
`container "%s" -> "%s" without snapshots`, source.Name(),
target.Name())
return nil
}
// copyWithoutSnapshotsFull creates a sparse copy of a container
// This introduces a dependency relation between the source RBD storage volume
// and the target RBD storage volume.
func (s *storageCeph) copyWithoutSnapshotsSparse(target container,
source container) error {
logger.Debugf(`Creating sparse copy of RBD storage volume for `+
`container "%s" -> "%s" without snapshots`, source.Name(),
target.Name())
sourceIsSnapshot := source.IsSnapshot()
sourceContainerName := source.Name()
targetContainerName := target.Name()
sourceContainerOnlyName := sourceContainerName
sourceSnapshotOnlyName := ""
snapshotName := fmt.Sprintf("zombie_snapshot_%s",
uuid.NewRandom().String())
if sourceIsSnapshot {
sourceContainerOnlyName, sourceSnapshotOnlyName, _ =
containerGetParentAndSnapshotName(sourceContainerName)
snapshotName = fmt.Sprintf("snapshot_%s", sourceSnapshotOnlyName)
} else {
// create snapshot
err := cephRBDSnapshotCreate(s.ClusterName, s.OSDPoolName,
sourceContainerName, storagePoolVolumeTypeNameContainer,
snapshotName, s.UserName)
if err != nil {
logger.Errorf(`Failed to create snapshot for RBD `+
`storage volume for image "%s" on storage `+
`pool "%s": %s`, targetContainerName,
s.pool.Name, err)
return err
}
}
// protect volume so we can create clones of it
err := cephRBDSnapshotProtect(s.ClusterName, s.OSDPoolName,
sourceContainerOnlyName, storagePoolVolumeTypeNameContainer,
snapshotName, s.UserName)
if err != nil {
logger.Errorf(`Failed to protect snapshot for RBD storage `+
`volume for image "%s" on storage pool "%s": %s`,
snapshotName, s.pool.Name, err)
return err
}
err = cephRBDCloneCreate(s.ClusterName, s.OSDPoolName,
sourceContainerOnlyName, storagePoolVolumeTypeNameContainer,
snapshotName, s.OSDPoolName, targetContainerName,
storagePoolVolumeTypeNameContainer, s.UserName)
if err != nil {
logger.Errorf(`Failed to clone new RBD storage volume for `+
`container "%s": %s`, targetContainerName, err)
return err
}
RBDDevPath, err := cephRBDVolumeMap(s.ClusterName, s.OSDPoolName,
targetContainerName, storagePoolVolumeTypeNameContainer,
s.UserName)
if err != nil {
logger.Errorf(`Failed to map RBD storage volume for image `+
`"%s" on storage pool "%s": %s`, targetContainerName,
s.pool.Name, err)
return err
}
// Generate a new xfs's UUID
RBDFilesystem := s.getRBDFilesystem()
msg, err := fsGenerateNewUUID(RBDFilesystem, RBDDevPath)
if err != nil {
logger.Errorf("Failed to create new \"%s\" UUID for container \"%s\" on storage pool \"%s\": %s", RBDFilesystem, targetContainerName, s.pool.Name, msg)
return err
}
targetContainerMountPoint := getContainerMountPoint(s.pool.Name,
target.Name())
err = createContainerMountpoint(targetContainerMountPoint,
target.Path(), target.IsPrivileged())
if err != nil {
return err
}
ourMount, err := target.StorageStart()
if err != nil {
return err
}
if ourMount {
defer target.StorageStop()
}
err = target.TemplateApply("copy")
if err != nil {
logger.Errorf(`Failed to apply copy template for container `+
`"%s": %s`, target.Name(), err)
return err
}
logger.Debugf(`Applied copy template for container "%s"`, target.Name())
logger.Debugf(`Created sparse copy of RBD storage volume for `+
`container "%s" -> "%s" without snapshots`, source.Name(),
target.Name())
return nil
}
// copyWithSnapshots creates a non-sparse copy of a container including its
// snapshots
// This does not introduce a dependency relation between the source RBD storage
// volume and the target RBD storage volume.
func (s *storageCeph) copyWithSnapshots(sourceVolumeName string,
targetVolumeName string, sourceParentSnapshot string) error {
logger.Debugf(`Creating non-sparse copy of RBD storage volume `+
`"%s -> "%s"`, sourceVolumeName, targetVolumeName)
args := []string{
"export-diff",
"--id", s.UserName,
"--cluster", s.ClusterName,
sourceVolumeName,
}
if sourceParentSnapshot != "" {
args = append(args, "--from-snap", sourceParentSnapshot)
}
// redirect output to stdout
args = append(args, "-")
rbdSendCmd := exec.Command("rbd", args...)
rbdRecvCmd := exec.Command(
"rbd",
"--id", s.UserName,
"import-diff",
"--cluster", s.ClusterName,
"-",
targetVolumeName)
rbdRecvCmd.Stdin, _ = rbdSendCmd.StdoutPipe()
rbdRecvCmd.Stdout = os.Stdout
rbdRecvCmd.Stderr = os.Stderr
err := rbdRecvCmd.Start()
if err != nil {
return err
}
err = rbdSendCmd.Run()
if err != nil {
return err
}
err = rbdRecvCmd.Wait()
if err != nil {
return err
}
logger.Debugf(`Created non-sparse copy of RBD storage volume `+
`"%s -> "%s"`, sourceVolumeName, targetVolumeName)
return nil
}
// cephContainerDelete deletes the RBD storage volume of a container including
// any dependencies
// - This function takes care to delete any RBD storage entities that are marked
// as zombie and whose existence is solely dependent on the RBD storage volume
// for the container to be deleted.
// - This function will mark any storage entities of the container to be deleted
// as zombies in case any RBD storage entities in the storage pool have a
// dependency relation with it.
// - This function uses a C-style convention to return error or success simply
// because it is more elegant and simple than the go way.
// The function will return
// -1 on error
// 0 if the RBD storage volume has been deleted
// 1 if the RBD storage volume has been marked as a zombie
// - cephContainerDelete in conjunction with cephContainerSnapshotDelete
// recurses through an OSD storage pool to find and delete any storage
// entities that were kept around because of dependency relations but are not
// deletable.
func cephContainerDelete(clusterName string, poolName string, volumeName string,
volumeType string, userName string) int {
logEntry := fmt.Sprintf("%s/%s_%s", poolName, volumeType, volumeName)
snaps, err := cephRBDVolumeListSnapshots(clusterName, poolName,
volumeName, volumeType, userName)
if err == nil {
var zombies int
for _, snap := range snaps {
logEntry := fmt.Sprintf("%s/%s_%s@%s", poolName,
volumeType, volumeName, snap)
ret := cephContainerSnapshotDelete(clusterName,
poolName, volumeName, volumeType, snap, userName)
if ret < 0 {
logger.Errorf(`Failed to delete RBD storage `+
`volume "%s"`, logEntry)
return -1
} else if ret == 1 {
logger.Debugf(`Marked RBD storage volume "%s" `+
`as zombie`, logEntry)
zombies++
} else {
logger.Debugf(`Deleted RBD storage volume "%s"`,
logEntry)
}
}
if zombies > 0 {
// unmap
err = cephRBDVolumeUnmap(clusterName, poolName,
volumeName, volumeType, userName, true)
if err != nil {
logger.Errorf(`Failed to unmap RBD storage `+
`volume "%s": %s`, logEntry, err)
return -1
}
logger.Debugf(`Unmapped RBD storage volume "%s"`,
logEntry)
if strings.HasPrefix(volumeType, "zombie_") {
logger.Debugf(`RBD storage volume "%s" `+
`already marked as zombie`, logEntry)
return 1
}
newVolumeName := fmt.Sprintf("%s_%s", volumeName,
uuid.NewRandom().String())
err := cephRBDVolumeMarkDeleted(clusterName, poolName,
volumeType, volumeName, newVolumeName, userName,
"")