forked from rposudnevskiy/RBDSR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRBDSR.py
611 lines (509 loc) · 27.6 KB
/
RBDSR.py
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
#!/usr/bin/python
#
# Copyright (C) Roman V. Posudnevskiy ([email protected])
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation; version 2.1 only.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
from datetime import datetime
from xmlrpclib import DateTime
import time
import SR, VDI, SRCommand, util
import os, re
import xs_errors
import xmlrpclib
import string
import cephutils
import scsiutil
import xml.dom.minidom
import blktap2
CAPABILITIES = ["VDI_CREATE", "VDI_DELETE", "VDI_ATTACH", "VDI_DETACH", "VDI_CLONE",
"VDI_SNAPSHOT", "VDI_INTRODUCE", "VDI_RESIZE", "VDI_RESIZE_ONLINE",
"ATOMIC_PAUSE", "VDI_UPDATE", "SR_SCAN", "SR_UPDATE", "SR_ATTACH",
"SR_DETACH", "SR_PROBE", "SR_METADATA"]
CONFIGURATION = []
DRIVER_INFO = {
'name': 'RBD',
'description': 'Handles virtual disks on CEPH RBD devices',
'vendor': 'Roman V. Posudnevskiy',
'copyright': '(c) 2016 Roman V. Posudnevskiy',
'driver_version': '1.0',
'required_api_version': '1.0',
'capabilities': CAPABILITIES,
'configuration': CONFIGURATION
}
TYPE = "rbd"
PROVISIONING_TYPES = ["thin", "thick"]
PROVISIONING_DEFAULT = "thick"
MODE_TYPES = ["kernel", "fuse", "nbd"]
MODE_DEFAULT = "nbd"
class RBDSR(SR.SR, cephutils.SR):
"""Ceph Block Devices storage repository"""
def _loadvdis(self):
if self.vdis:
return
RBDVDIs = self._get_vdilist(self.CEPH_POOL_NAME)
#xapi_session = self.session.xenapi
#sm_config = xapi_session.SR.get_sm_config(self.sr_ref)
vdis = self.session.xenapi.SR.get_VDIs(self.sr_ref)
vdi_uuids = set([])
for vdi in vdis:
vdi_uuids.add(self.session.xenapi.VDI.get_uuid(vdi))
for vdi_uuid in RBDVDIs.keys():
#name = RBDVDIs[vdi_uuid]['image']
if RBDVDIs[vdi_uuid].has_key('snapshot'):
parent_vdi_uuid = self._get_vdi_uuid(RBDVDIs[vdi_uuid]['image'])
parent_vdi_info = self._get_vdi_info(parent_vdi_uuid)
if parent_vdi_info.has_key('VDI_LABEL'):
label = parent_vdi_info['VDI_LABEL']
else:
label = ''
if parent_vdi_info.has_key('VDI_DESCRIPTION'):
description = parent_vdi_info['VDI_DESCRIPTION']
else:
description = ''
if vdi_uuid not in vdi_uuids:
#VDI doesn't exist
self.vdis[vdi_uuid] = RBDVDI(self, vdi_uuid, label)
self.vdis[vdi_uuid].size = str(RBDVDIs[parent_vdi_uuid]['size'])
self.vdis[vdi_uuid].is_a_snapshot = True
self.vdis[vdi_uuid].description = description
if parent_vdi_uuid not in vdi_uuids:
if self.vdis.has_key(parent_vdi_uuid):
self.vdis[vdi_uuid].snapshot_of = self.vdis[parent_vdi_uuid]
else:
self.vdis[parent_vdi_uuid] = RBDVDI(self, parent_vdi_uuid, label)
self.vdis[parent_vdi_uuid].size = str(RBDVDIs[parent_vdi_uuid]['size'])
self.vdis[parent_vdi_uuid].sm_config["vdi_type"] = 'aio'
else:
base_vdi_ref = self.session.xenapi.VDI.get_by_uuid(parent_vdi_uuid)
self.vdis[vdi_uuid].snapshot_of = base_vdi_ref
if parent_vdi_info.has_key(RBDVDIs[vdi_uuid]['snapshot']):
self.vdis[vdi_uuid].snapshot_time = str(parent_vdi_info[RBDVDIs[vdi_uuid]['snapshot']])
self.vdis[vdi_uuid].read_only = True
self.vdis[vdi_uuid].sm_config['snapshot-of'] = parent_vdi_uuid
self.vdis[vdi_uuid].sm_config["vdi_type"] = 'aio'
self.vdis[vdi_uuid].path = self._get_snap_path(parent_vdi_uuid, vdi_uuid)
else:
#VDI exists
vdi_ref = self.session.xenapi.VDI.get_by_uuid(vdi_uuid)
self.session.xenapi.VDI.set_virtual_size(vdi_ref, str(RBDVDIs[parent_vdi_uuid]['size']))
self.session.xenapi.VDI.set_physical_utilisation(vdi_ref, str(RBDVDIs[parent_vdi_uuid]['size']))
parent_vdi_ref = self.session.xenapi.VDI.get_by_uuid(parent_vdi_uuid)
self.session.xenapi.VDI.set_is_a_snapshot(vdi_ref, True)
self.session.xenapi.VDI.set_name_description(vdi_ref, description)
self.session.xenapi.VDI.set_snapshot_of(vdi_ref, parent_vdi_ref)
if parent_vdi_info.has_key(RBDVDIs[vdi_uuid]['snapshot']):
self.session.xenapi.VDI.set_snapshot_time(vdi_ref, str(parent_vdi_info[RBDVDIs[vdi_uuid]['snapshot']]))
self.session.xenapi.VDI.set_read_only(vdi_ref, True)
self.session.xenapi.VDI.remove_from_sm_config(vdi_ref, 'snapshot-of')
self.session.xenapi.VDI.add_to_sm_config(vdi_ref, 'snapshot-of', parent_vdi_uuid)
else:
vdi_info = self._get_vdi_info(vdi_uuid)
if vdi_info.has_key('VDI_LABEL'):
label = vdi_info['VDI_LABEL']
else:
label = ''
if vdi_info.has_key('VDI_DESCRIPTION'):
description = vdi_info['VDI_DESCRIPTION']
else:
description = ''
if vdi_uuid not in vdi_uuids:
#VDI doesn't exist
if not self.vdis.has_key(vdi_uuid):
self.vdis[vdi_uuid] = RBDVDI(self, vdi_uuid, label)
self.vdis[vdi_uuid].description = description
self.vdis[vdi_uuid].size = str(RBDVDIs[vdi_uuid]['size'])
self.vdis[vdi_uuid].sm_config["vdi_type"] = 'aio'
else:
#VDI exists
vdi_ref = self.session.xenapi.VDI.get_by_uuid(vdi_uuid)
self.session.xenapi.VDI.set_virtual_size(vdi_ref, str(RBDVDIs[vdi_uuid]['size']))
self.session.xenapi.VDI.set_physical_utilisation(vdi_ref, str(RBDVDIs[vdi_uuid]['size']))
self.session.xenapi.VDI.set_name_description(vdi_ref, description)
#self.session.xenapi.VDI.add_to_sm_config(vdi_ref, 'vdi_type', 'aio')
def handles(type):
"""Do we handle this type?"""
if type == TYPE:
return True
return False
handles = staticmethod(handles)
def content_type(self, sr_uuid):
"""Returns the content_type XML"""
return SR.SR.content_type(self, sr_uuid)
def vdi(self, uuid):
"""Create a VDI class"""
if not self.vdis.has_key(uuid):
self.vdis[uuid] = RBDVDI(self, uuid, '')
return self.vdis[uuid]
def probe(self):
util.SMlog("RBDSR.probe for %s" % self.uuid)
return self._srlist_toxml()
def load(self, sr_uuid):
"""Initialises the SR"""
self.sr_vditype = 'rbd'
self.provision = PROVISIONING_DEFAULT
self.mode = MODE_DEFAULT
self.uuid = sr_uuid
other_config = self.session.xenapi.SR.get_other_config(self.sr_ref)
ceph_user = None
if other_config:
ceph_user = other_config.get('user')
util.SMlog("RBDSR.load using cephx user %s" % ceph_user)
cephutils.SR.load(self,sr_uuid, ceph_user)
def attach(self, sr_uuid):
"""Std. attach"""
util.SMlog("RBDSR.attach for %s" % self.uuid)
if not self.RBDPOOLs.has_key(self.uuid):
raise xs_errors.XenError('SRUnavailable',opterr='no pool with uuid: %s' % sr_uuid)
cephutils.SR.attach(self, sr_uuid)
def update(self, sr_uuid):
self.scan(sr_uuid)
def detach(self, sr_uuid):
"""Std. detach"""
cephutils.SR.detach(self, sr_uuid)
def scan(self, sr_uuid):
"""Scan"""
cephutils.SR.scan(self, sr_uuid)
self.physical_size = self.RBDPOOLs[sr_uuid]['stats']['max_avail'] + self.RBDPOOLs[sr_uuid]['stats']['bytes_used']
self.physical_utilisation = self.RBDPOOLs[sr_uuid]['stats']['bytes_used']
self.virtual_allocation = self._get_allocated_size()
self._loadvdis()
self._db_update()
scanrecord = SR.ScanRecord(self)
scanrecord.synchronise_existing()
scanrecord.synchronise_new()
def create(self, sr_uuid, size):
self.attach(sr_uuid)
self.detach(sr_uuid)
def delete(self, sr_uuid):
pass
def _updateStats(self, sr_uuid, virtAllocDelta):
valloc = int(self.session.xenapi.SR.get_virtual_allocation(self.sr_ref))
self.virtual_allocation = valloc + int(virtAllocDelta)
self.session.xenapi.SR.set_virtual_allocation(self.sr_ref, str(self.virtual_allocation))
self.session.xenapi.SR.set_physical_utilisation(self.sr_ref, str(self.RBDPOOLs[sr_uuid]['stats']['bytes_used']))
class RBDVDI(VDI.VDI, cephutils.VDI):
def load(self, vdi_uuid):
self.loaded = False
self.vdi_type = 'aio'
self.uuid = vdi_uuid
self.location = vdi_uuid
self.mode = self.sr.mode
self.exists = False
cephutils.VDI.load(self, vdi_uuid)
def __init__(self, mysr, uuid, label):
self.uuid = uuid
VDI.VDI.__init__(self, mysr, uuid)
self.label = label
self.vdi_type = 'aio'
self.read_only = False
self.shareable = False
self.issnap = False
self.hidden = False
self.sm_config = {}
def create(self, sr_uuid, vdi_uuid, size):
util.SMlog("RBDVDI.create for %s" % self.uuid)
if self.exists:
raise xs_errors.XenError('VDIExists')
if not self.sr._isSpaceAvailable(size):
util.SMlog('RBDVDI.create: vdi size is too big: ' + \
'(vdi size: %d, sr free space size: %d)' % (size, self.sr.RBDPOOLs[sr_uuid]['stats']['max_avail']))
raise xs_errors.XenError('VDISize', opterr='vdi size is too big: vdi size: %d, sr free space size: %d' % (size, self.sr.RBDPOOLs[sr_uuid]['stats']['max_avail']))
cephutils.VDI.create(self, sr_uuid, vdi_uuid, size)
self.size = size
self.utilisation = size
self.sm_config["vdi_type"] = 'aio'
self.ref = self._db_introduce()
self.sr._updateStats(self.sr.uuid, self.size)
return VDI.VDI.get_params(self)
def delete(self, sr_uuid, vdi_uuid):
util.SMlog("RBDVDI.delete for %s" % self.uuid)
vdis = self.session.xenapi.SR.get_VDIs(self.sr.sr_ref)
clones_uuids = set([])
has_a_snapshot = False
has_a_clone = False
for tmp_vdi in vdis:
tmp_vdi_uuid = self.session.xenapi.VDI.get_uuid(tmp_vdi)
tmp_sm_config = self.session.xenapi.VDI.get_sm_config(tmp_vdi)
if tmp_sm_config.has_key("snapshot-of"):
if tmp_sm_config["snapshot-of"] == vdi_uuid:
has_a_snapshot = True
elif tmp_sm_config.has_key("clone-of"):
if tmp_sm_config["clone-of"] == vdi_uuid:
has_a_clone = True
clones_uuids.add(tmp_vdi_uuid)
if has_a_snapshot == True:
# reverting of VM snapshot
self_vdi_ref = self.session.xenapi.VDI.get_by_uuid(vdi_uuid)
new_uuid = util.gen_uuid()
self.snaps = self.session.xenapi.VDI.get_snapshots(self_vdi_ref)
# renaming base image
self._rename_image(vdi_uuid, new_uuid)
for snap in self.snaps:
util.SMlog("RBDVDI.delete set snapshot_of = %s for %s" % (self.uuid, self.session.xenapi.VDI.get_uuid(snap)))
self.session.xenapi.VDI.add_to_sm_config(snap, 'new_uuid', new_uuid)
self.session.xenapi.VDI.add_to_sm_config(snap, 'rollback', 'true')
else:
# deleting of VDI
self_vdi_ref = self.session.xenapi.VDI.get_by_uuid(vdi_uuid)
self_sm_config = self.session.xenapi.VDI.get_sm_config(self_vdi_ref)
if self_sm_config.has_key("snapshot-of"):
if has_a_clone == True:
for clone_uuid in clones_uuids:
clone_vdi_ref = self.session.xenapi.VDI.get_by_uuid(vdi_uuid)
self.session.xenapi.VDI.remove_from_sm_config(clone_vdi_ref, "clone-of")
self._flatten_clone(clone_uuid)
if self_sm_config.has_key("compose"):
self._delete_snapshot(self_sm_config["compose_vdi1"], vdi_uuid)
self._delete_vdi(self_sm_config["compose_vdi1"])
self.sr.forget_vdi(self_sm_config["compose_vdi1"])
else:
self._delete_snapshot(self_sm_config["snapshot-of"], vdi_uuid)
else:
self._delete_vdi(vdi_uuid)
self.size = int(self.session.xenapi.VDI.get_virtual_size(self_vdi_ref))
self.sr._updateStats(self.sr.uuid, -self.size)
self._db_forget()
def attach(self, sr_uuid, vdi_uuid):
util.SMlog("RBDVDI.attach for %s" % self.uuid)
vdi_ref = self.sr.srcmd.params['vdi_ref']
sm_config = self.session.xenapi.VDI.get_sm_config(vdi_ref)
if not hasattr(self,'xenstore_data'):
self.xenstore_data = {}
self.xenstore_data.update(scsiutil.update_XS_SCSIdata(self.uuid, scsiutil.gen_synthetic_page_data(self.uuid)))
self.xenstore_data['storage-type']='rbd'
self.xenstore_data['vdi-type']=self.vdi_type
self.attached = True
self.session.xenapi.VDI.remove_from_sm_config(vdi_ref, 'attached')
self.session.xenapi.VDI.add_to_sm_config(vdi_ref, 'attached', 'true')
self.size = int(self.session.xenapi.VDI.get_virtual_size(vdi_ref))
##########
vdis = self.session.xenapi.SR.get_VDIs(self.sr.sr_ref)
has_a_snapshot = False
for tmp_vdi in vdis:
tmp_vdi_uuid = self.session.xenapi.VDI.get_uuid(tmp_vdi)
tmp_sm_config = self.session.xenapi.VDI.get_sm_config(tmp_vdi)
if tmp_sm_config.has_key("snapshot-of"):
if tmp_sm_config["snapshot-of"] == vdi_uuid:
has_a_snapshot = True
if tmp_sm_config.has_key("sxm_mirror"):
sxm_mirror_vdi = vdi_uuid
########## SXM VDIs
if sm_config.has_key("base_mirror"):
if has_a_snapshot:
# it's a mirror vdi of storage migrating VM
# it's attached first
self.session.xenapi.VDI.add_to_sm_config(vdi_ref, 'sxm_mirror', 'true')
# creating dm snapshot dev
self._setup_mirror(vdi_uuid, self.size)
else:
# it's a base vdi of storage migrating VM
# it's attached after mirror VDI and mirror snapshot VDI has been created
self._map_VHD(vdi_uuid)
########## not SXM VDIs
else:
# it's not SXM VDI, just attach it
self._map_VHD(vdi_uuid)
if not util.pathexists(self.path):
raise xs_errors.XenError('VDIUnavailable', opterr='Could not find: %s' % self.path)
return VDI.VDI.attach(self, self.sr.uuid, self.uuid)
def detach(self, sr_uuid, vdi_uuid):
self._unmap_VHD(vdi_uuid)
self.attached = False
vdi_ref = self.sr.srcmd.params['vdi_ref']
self.session.xenapi.VDI.remove_from_sm_config(vdi_ref, 'attached')
def clone(self, sr_uuid, snap_uuid):
util.SMlog("RBDVDI.clone for %s snapshot"% (snap_uuid))
snap_vdi_ref = self.session.xenapi.VDI.get_by_uuid(snap_uuid)
if self.session.xenapi.VDI.get_sharable(snap_vdi_ref):
return snap_vdi_ref.get_params()
snap_sm_config = self.session.xenapi.VDI.get_sm_config(snap_vdi_ref)
if snap_sm_config.has_key("snapshot-of"):
base_uuid = snap_sm_config["snapshot-of"]
else:
snapVDI = self._snapshot(sr_uuid, snap_uuid)
base_uuid = snap_uuid
snap_uuid = snapVDI.uuid
self.sr.scan(self.sr.uuid)
util.SMlog("RBDVDI.clone base_uuid = %s"% (base_uuid))
if snap_sm_config.has_key("rollback"):
if snap_sm_config["rollback"] == 'true':
util.SMlog("RBDVDI.clone reverting %s to %s"% (snap_uuid, base_uuid))
# executing rollback of snapshot (reverting VM to snapshot)
new_uuid = snap_sm_config["new_uuid"]
self._rollback_snapshot(new_uuid, snap_uuid)
baseVDI = RBDVDI(self.sr, new_uuid, self.session.xenapi.VDI.get_name_label(snap_vdi_ref))
baseVDI.path = self.sr._get_path(new_uuid)
baseVDI.location = baseVDI.uuid
baseVDI.size = self.session.xenapi.VDI.get_virtual_size(snap_vdi_ref)
baseVDI.sm_config["vdi_type"] = 'aio'
baseVDI.sm_config["reverted"] = 'true'
base_vdi_ref = baseVDI._db_introduce()
vdis = self.session.xenapi.SR.get_VDIs(self.sr.sr_ref)
for tmp_vdi in vdis:
tmp_vdi_uuid = self.session.xenapi.VDI.get_uuid(tmp_vdi)
tmp_sm_config = self.session.xenapi.VDI.get_sm_config(tmp_vdi)
if tmp_sm_config.has_key("rollback"):
if tmp_sm_config.has_key("new_uuid"):
if tmp_sm_config["new_uuid"] == new_uuid:
sm_config = self.session.xenapi.VDI.get_sm_config(tmp_vdi)
del sm_config['snapshot-of']
sm_config['snapshot-of'] = new_uuid
del sm_config['rollback']
del sm_config['new_uuid']
self.session.xenapi.VDI.set_sm_config(tmp_vdi, sm_config)
return baseVDI.get_params()
else:
base_vdi_info = self._get_vdi_info(base_uuid)
base_vdi_ref = self.session.xenapi.VDI.get_by_uuid(base_uuid)
if base_vdi_info.has_key('VDI_LABEL'):
base_vdi_label = base_vdi_info['VDI_LABEL']
else:
base_vdi_label = ''
clone_uuid = util.gen_uuid()
cloneVDI = RBDVDI(self.sr, clone_uuid, base_vdi_label)
self._do_clone(base_uuid, snap_uuid, clone_uuid, base_vdi_label)
cloneVDI.path = self.sr._get_path(clone_uuid)
cloneVDI.location = cloneVDI.uuid
cloneVDI.sm_config["vdi_type"] = 'aio'
cloneVDI.sm_config["clone-of"] = snap_uuid
clone_vdi_ref = cloneVDI._db_introduce()
self.session.xenapi.VDI.set_physical_utilisation(clone_vdi_ref, self.session.xenapi.VDI.get_physical_utilisation(base_vdi_ref))
self.session.xenapi.VDI.set_virtual_size(clone_vdi_ref, self.session.xenapi.VDI.get_virtual_size(base_vdi_ref))
self.sr._updateStats(self.sr.uuid, self.session.xenapi.VDI.get_virtual_size(base_vdi_ref))
return cloneVDI.get_params()
def snapshot(self, sr_uuid, vdi_uuid):
return self._snapshot(sr_uuid, vdi_uuid).get_params()
def _snapshot(self, sr_uuid, vdi_uuid):
util.SMlog("RBDVDI.snapshot for %s" % (vdi_uuid))
#secondary = None
#if not blktap2.VDI.tap_pause(self.session, sr_uuid, vdi_uuid):
# raise util.SMException("failed to pause VDI %s" % vdi_uuid)
vdi_ref = self.session.xenapi.VDI.get_by_uuid(vdi_uuid)
sm_config = self.session.xenapi.VDI.get_sm_config(vdi_ref)
base_uuid = vdi_uuid
snap_uuid = util.gen_uuid()
vdi_info = self._get_vdi_info(vdi_uuid)
if vdi_info.has_key('VDI_LABEL'):
orig_label = vdi_info['VDI_LABEL']
else:
orig_label = ''
snapVDI = RBDVDI(self.sr, snap_uuid, "%s%s" % (orig_label, " (snapshot)"))
self._do_snapshot(base_uuid, snap_uuid)
snapVDI.path = self.sr._get_snap_path(base_uuid, snap_uuid)
snapVDI.issnap = True
snapVDI.read_only = True
snapVDI.location = snapVDI.uuid
snapVDI.size = self.session.xenapi.VDI.get_virtual_size(vdi_ref)
snapVDI.sm_config["vdi_type"] = 'aio'
snapVDI.sm_config["snapshot-of"] = base_uuid
snap_vdi_ref = snapVDI._db_introduce()
self.session.xenapi.VDI.set_physical_utilisation(snap_vdi_ref, self.session.xenapi.VDI.get_physical_utilisation(vdi_ref))
self.session.xenapi.VDI.set_virtual_size(snap_vdi_ref, self.session.xenapi.VDI.get_virtual_size(vdi_ref))
self.size = int(self.session.xenapi.VDI.get_virtual_size(vdi_ref))
self.sr._updateStats(self.sr.uuid, self.size)
#blktap2.VDI.tap_unpause(self.session, sr_uuid, vdi_uuid, secondary)
return snapVDI
def resize(self, sr_uuid, vdi_uuid, size):
"""Resize the given VDI to size <size> MB. Size can
be any valid disk size greater than [or smaller than]
the current value."""
util.SMlog("RBDVDI.resize for %s" % self.uuid)
if not self.sr._isSpaceAvailable(sr_uuid, size):
util.SMlog('vdi_resize: vdi size is too big: ' + \
'(vdi size: %d, sr free space size: %d)' % (size, self.sr.RBDPOOLs[sr_uuid]['stats']['max_avail']))
raise xs_errors.XenError('VDISize', opterr='vdi size is too big')
if size < self.size:
util.SMlog('vdi_resize: shrinking not supported yet: ' + \
'(current size: %d, new size: %d)' % (self.size, size))
raise xs_errors.XenError('VDISize', opterr='shrinking not allowed')
if size == self.size:
return VDI.VDI.get_params(self)
oldSize = self.size
cephutils.VDI.resize(self, sr_uuid, vdi_uuid, size)
self.size = size
self.utilisation = self.size
vdi_ref = self.sr.srcmd.params['vdi_ref']
self.session.xenapi.VDI.set_virtual_size(vdi_ref, str(self.size))
self.session.xenapi.VDI.set_physical_utilisation(vdi_ref,
str(self.utilisation))
self.sr._updateStats(self.sr.uuid, self.size - oldSize)
return VDI.VDI.get_params(self)
def resize_online(self, sr_uuid, vdi_uuid, size):
"""Resize the given VDI which may have active VBDs, which have
been paused for the duration of this call."""
return resize(sr_uuid, vdi_uuid, size)
def compose(self, sr_uuid, vdi1_uuid, vdi2_uuid):
util.SMlog("RBDSR.compose for %s -> %s" % (vdi2_uuid, vdi1_uuid))
#if not blktap2.VDI.tap_pause(self.session, sr_uuid, vdi2_uuid):
# raise util.SMException("failed to pause VDI %s" % vdi2_uuid)
vdis = self.session.xenapi.SR.get_VDIs(self.sr.sr_ref)
for tmp_vdi in vdis:
tmp_vdi_uuid = self.session.xenapi.VDI.get_uuid(tmp_vdi)
tmp_sm_config = self.session.xenapi.VDI.get_sm_config(tmp_vdi)
if tmp_sm_config.has_key("snapshot-of"):
if tmp_sm_config["snapshot-of"] == vdi2_uuid:
snap_vdi_ref = self.session.xenapi.VDI.get_by_uuid(tmp_vdi_uuid)
snap_uuid = tmp_vdi_uuid
self.session.xenapi.VDI.add_to_sm_config(snap_vdi_ref, 'compose', 'true')
self.session.xenapi.VDI.add_to_sm_config(snap_vdi_ref, 'compose_vdi1', vdi1_uuid)
self.session.xenapi.VDI.add_to_sm_config(snap_vdi_ref, 'compose_vdi2', vdi2_uuid)
vdi1_ref = self.session.xenapi.VDI.get_by_uuid(vdi1_uuid)
self.size = int(self.session.xenapi.VDI.get_virtual_size(vdi1_ref))
self._merge_diffs(vdi2_uuid, snap_uuid, vdi1_uuid, self.size)
self.session.xenapi.VDI.remove_from_sm_config(snap_vdi_ref, 'snapshot-of')
self.session.xenapi.VDI.add_to_sm_config(snap_vdi_ref, 'snapshot-of', vdi1_uuid)
#blktap2.VDI.tap_unpause(self.session, sr_uuid, vdi2_uuid, None)
def update(self, sr_uuid, vdi_uuid):
util.SMlog("RBDSR.update for %s" % vdi_uuid)
self_vdi_ref = self.session.xenapi.VDI.get_by_uuid(vdi_uuid)
if not self.session.xenapi.VDI.get_is_a_snapshot(self_vdi_ref):
vdis = self.session.xenapi.SR.get_VDIs(self.sr.sr_ref)
self.snaps = {}
has_snapshots = False
for tmp_vdi_ref in vdis:
tmp_vdi_uuid = self.session.xenapi.VDI.get_uuid(tmp_vdi_ref)
tmp_sm_config = self.session.xenapi.VDI.get_sm_config(tmp_vdi_ref)
if tmp_sm_config.has_key("snapshot-of"):
if tmp_sm_config["snapshot-of"] == vdi_uuid:
has_snapshots = True
self.snaps[tmp_vdi_uuid]=self.session.xenapi.VDI.get_snapshot_time(tmp_vdi_ref)
self.label = self.session.xenapi.VDI.get_name_label(self_vdi_ref)
self.description = self.session.xenapi.VDI.get_name_description(self_vdi_ref)
cephutils.VDI.update(self, sr_uuid, vdi_uuid)
sm_config = self.session.xenapi.VDI.get_sm_config(self_vdi_ref)
if sm_config.has_key('reverted'):
if sm_config['reverted'] == 'true':
del sm_config['reverted']
self.session.xenapi.VDI.set_sm_config(self_vdi_ref, sm_config)
else:
if has_snapshots == True:
for snapshot_uuid in self.snaps.keys():
util.SMlog("RBDVDI.update start setting snapshots")
snapshot_vdi_ref = self.session.xenapi.VDI.get_by_uuid(snapshot_uuid)
self.session.xenapi.VDI.set_name_label(snapshot_vdi_ref, self.session.xenapi.VDI.get_name_label(self_vdi_ref))
self.session.xenapi.VDI.set_name_description(snapshot_vdi_ref, self.session.xenapi.VDI.get_name_description(self_vdi_ref))
util.SMlog("RBDVDI.update finish setting snapshots")
else:
self_vdi_sm_config = self.session.xenapi.VDI.get_sm_config(self_vdi_ref)
base_vdi_uuid = self_vdi_sm_config["snapshot-of"]
base_vdi_ref = self.session.xenapi.VDI.get_by_uuid(base_vdi_uuid)
self.label=self.session.xenapi.VDI.get_name_label(base_vdi_ref)
self.description=self.session.xenapi.VDI.get_name_description(base_vdi_ref)
self.snaps = {}
self.snaps[vdi_uuid]=self.session.xenapi.VDI.get_snapshot_time(self_vdi_ref)
cephutils.VDI.update(self, sr_uuid, base_vdi_uuid)
if __name__ == '__main__':
SRCommand.run(RBDSR, DRIVER_INFO)
else:
SR.registerSR(RBDSR)