-
Notifications
You must be signed in to change notification settings - Fork 547
/
snapshot.go
341 lines (291 loc) · 9.21 KB
/
snapshot.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
/*
Copyright 2020 The Ceph-CSI Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package rbd
import (
"context"
"errors"
"fmt"
librbd "github.com/ceph/go-ceph/rbd"
"github.com/container-storage-interface/spec/lib/go/csi"
"google.golang.org/protobuf/types/known/timestamppb"
"github.com/ceph/ceph-csi/internal/rbd/types"
"github.com/ceph/ceph-csi/internal/util"
"github.com/ceph/ceph-csi/internal/util/log"
)
func createRBDClone(
ctx context.Context,
parentVol, cloneRbdVol *rbdVolume,
snap *rbdSnapshot,
) error {
// create snapshot
err := parentVol.createSnapshot(ctx, snap)
if err != nil {
log.ErrorLog(ctx, "failed to create snapshot %s: %v", snap, err)
return err
}
snap.RbdImageName = parentVol.RbdImageName
// create clone image and delete snapshot
err = cloneRbdVol.cloneRbdImageFromSnapshot(ctx, snap, parentVol)
if err != nil {
log.ErrorLog(
ctx,
"failed to clone rbd image %s from snapshot %s: %v",
cloneRbdVol.RbdImageName,
snap.RbdSnapName,
err)
err = fmt.Errorf(
"failed to clone rbd image %s from snapshot %s: %w",
cloneRbdVol.RbdImageName,
snap.RbdSnapName,
err)
}
errSnap := parentVol.deleteSnapshot(ctx, snap)
if errSnap != nil {
log.ErrorLog(ctx, "failed to delete snapshot: %v", errSnap)
delErr := cloneRbdVol.Delete(ctx)
if delErr != nil {
log.ErrorLog(ctx, "failed to delete rbd image: %s with error: %v", cloneRbdVol, delErr)
}
return err
}
return nil
}
// cleanUpSnapshot removes the RBD-snapshot (rbdSnap) from the RBD-image
// (parentVol) and deletes the RBD-image rbdVol.
func cleanUpSnapshot(
ctx context.Context,
parentVol *rbdVolume,
rbdSnap *rbdSnapshot,
rbdVol *rbdVolume,
) error {
err := parentVol.deleteSnapshot(ctx, rbdSnap)
if err != nil {
if !errors.Is(err, ErrSnapNotFound) {
log.ErrorLog(ctx, "failed to delete snapshot %q: %v", rbdSnap, err)
return err
}
}
if rbdVol != nil {
err := rbdVol.Delete(ctx)
if err != nil {
if !errors.Is(err, ErrImageNotFound) {
log.ErrorLog(ctx, "failed to delete rbd image %q with error: %v", rbdVol, err)
return err
}
}
}
return nil
}
func (rv *rbdVolume) toSnapshot() *rbdSnapshot {
return &rbdSnapshot{
rbdImage: rbdImage{
ClusterID: rv.ClusterID,
VolID: rv.VolID,
VolSize: rv.VolSize,
Monitors: rv.Monitors,
Pool: rv.Pool,
JournalPool: rv.JournalPool,
RadosNamespace: rv.RadosNamespace,
RbdImageName: rv.RbdImageName,
ImageID: rv.ImageID,
CreatedAt: rv.CreatedAt,
// copyEncryptionConfig cannot be used here because the volume and the
// snapshot will have the same volumeID which cases the panic in
// copyEncryptionConfig function.
blockEncryption: rv.blockEncryption,
fileEncryption: rv.fileEncryption,
},
}
}
func (rbdSnap *rbdSnapshot) toVolume() *rbdVolume {
return &rbdVolume{
rbdImage: rbdImage{
ClusterID: rbdSnap.ClusterID,
VolID: rbdSnap.VolID,
Monitors: rbdSnap.Monitors,
Pool: rbdSnap.Pool,
JournalPool: rbdSnap.JournalPool,
RadosNamespace: rbdSnap.RadosNamespace,
RbdImageName: rbdSnap.RbdSnapName,
ImageID: rbdSnap.ImageID,
CreatedAt: rbdSnap.CreatedAt,
// copyEncryptionConfig cannot be used here because the volume and the
// snapshot will have the same volumeID which cases the panic in
// copyEncryptionConfig function.
blockEncryption: rbdSnap.blockEncryption,
fileEncryption: rbdSnap.fileEncryption,
},
}
}
func (rbdSnap *rbdSnapshot) ToCSI(ctx context.Context) (*csi.Snapshot, error) {
created, err := rbdSnap.GetCreationTime(ctx)
if err != nil {
return nil, err
}
return &csi.Snapshot{
SizeBytes: rbdSnap.VolSize,
SnapshotId: rbdSnap.VolID,
SourceVolumeId: rbdSnap.SourceVolumeID,
CreationTime: timestamppb.New(*created),
ReadyToUse: true,
GroupSnapshotId: rbdSnap.groupID,
}, nil
}
func undoSnapshotCloning(
ctx context.Context,
parentVol *rbdVolume,
rbdSnap *rbdSnapshot,
cloneVol *rbdVolume,
cr *util.Credentials,
) error {
err := cleanUpSnapshot(ctx, parentVol, rbdSnap, cloneVol)
if err != nil {
log.ErrorLog(ctx, "failed to clean up %s or %s: %v", cloneVol, rbdSnap, err)
return err
}
err = undoSnapReservation(ctx, rbdSnap, cr)
return err
}
// NewSnapshotByID creates a new rbdSnapshot from the rbdVolume.
//
// Parameters:
// - name of the new rbd-image backing the snapshot
// - id of the rbd-snapshot to clone
//
// FIXME: When resolving the Snapshot, the RbdImageName will be set to the name
// of the parent image. This is can cause issues when not accounting for that
// and the Snapshot is deleted; instead of deleting the snapshot image, the
// parent image is removed.
//
//nolint:gocyclo,cyclop // TODO: reduce complexity.
func (rv *rbdVolume) NewSnapshotByID(
ctx context.Context,
cr *util.Credentials,
name string,
id uint64,
) (types.Snapshot, error) {
snap := rv.toSnapshot()
snap.RequestName = name
srcVolID, err := rv.GetID(ctx)
if err != nil {
return nil, err
}
snap.SourceVolumeID = srcVolID
// reserveSnap sets snap.{RbdSnapName,ReservedID,VolID}
err = reserveSnap(ctx, snap, rv, cr)
if err != nil {
return nil, fmt.Errorf("failed to create a reservation in the journal for snapshot image %q: %w", snap, err)
}
defer func() {
if err != nil {
undoErr := undoSnapReservation(ctx, snap, cr)
if undoErr != nil {
log.WarningLog(ctx, "failed undoing reservation of snapshot %q: %v", name, undoErr)
}
}
}()
// A new snapshot image will be created, and needs to have a unique
// name.
// FIXME: the journal contains rv.RbdImageName as SourceName. When
// resolving the snapshot image, snap.RbdImageName will be set to the
// original RbdImageName/SourceName (incorrect). This is fixed-up in
// rbdManager.GetSnapshotByID(), this needs to be done cleaner.
snap.RbdImageName = snap.RbdSnapName
err = rv.Connect(cr)
if err != nil {
return nil, err
}
err = rv.openIoctx()
if err != nil {
return nil, err
}
options, err := rv.constructImageOptions(ctx)
if err != nil {
return nil, err
}
defer options.Destroy()
err = options.SetUint64(librbd.ImageOptionCloneFormat, 2)
if err != nil {
return nil, err
}
// indicator to remove the snapshot after a failure
removeSnap := true
var snapImage *librbd.Snapshot
log.DebugLog(ctx, "going to clone snapshot image %q from image %q with snapshot ID %d", snap, rv, id)
err = librbd.CloneImageByID(rv.ioctx, rv.RbdImageName, id, rv.ioctx, snap.RbdImageName, options)
if err != nil && !errors.Is(librbd.ErrExist, err) {
log.ErrorLog(ctx, "failed to clone snapshot %q with id %d: %v", snap, id, err)
return nil, fmt.Errorf("failed to clone %q with snapshot id %d as new image %q: %w", rv.RbdImageName, id, snap, err)
}
defer func() {
if !removeSnap {
// success, no need to remove the snapshot image
return
}
if snapImage != nil {
err = snapImage.Remove()
if err != nil {
log.ErrorLog(ctx, "failed to remove snapshot of image %q after failure: %v", snap, err)
}
}
err = librbd.RemoveImage(rv.ioctx, snap.RbdImageName)
if err != nil {
log.ErrorLog(ctx, "failed to remove snapshot image %q after failure: %v", snap, err)
}
}()
// update the snapshot image in the journal, after the image info is updated
j, err := snapJournal.Connect(snap.Monitors, snap.RadosNamespace, cr)
if err != nil {
return nil, fmt.Errorf("snapshot image %q failed to connect to journal: %w", snap, err)
}
defer j.Destroy()
err = snap.Connect(cr)
if err != nil {
return nil, fmt.Errorf("failed to connect snapshot image %q: %w", snap, err)
}
defer snap.Destroy(ctx)
image, err := snap.open()
if err != nil {
return nil, fmt.Errorf("failed to open snapshot image %q: %w", snap, err)
}
defer image.Close()
snapImage, err = image.CreateSnapshot(snap.RbdSnapName)
if err != nil && !errors.Is(librbd.ErrExist, err) {
return nil, fmt.Errorf("failed to create snapshot on image %q: %w", snap, err)
}
err = snap.repairImageID(ctx, j, true)
if err != nil {
return nil, fmt.Errorf("failed to repair image id for snapshot image %q: %w", snap, err)
}
// all ok, don't remove the snapshot image in a defer statement
removeSnap = false
return snap, nil
}
func (rbdSnap *rbdSnapshot) SetVolumeGroup(ctx context.Context, cr *util.Credentials, groupID string) error {
vi := util.CSIIdentifier{}
err := vi.DecomposeCSIID(rbdSnap.VolID)
if err != nil {
return err
}
j, err := snapJournal.Connect(rbdSnap.Monitors, rbdSnap.RadosNamespace, cr)
if err != nil {
return fmt.Errorf("snapshot %q failed to connect to journal: %w", rbdSnap, err)
}
defer j.Destroy()
err = j.StoreGroupID(ctx, rbdSnap.Pool, vi.ObjectUUID, groupID)
if err != nil {
return fmt.Errorf("failed to set volume group ID for snapshot %q: %w", rbdSnap, err)
}
rbdSnap.groupID = groupID
return nil
}