Coverage for drivers/LVMSR.py : 48%
Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1#!/usr/bin/python3
2#
3# Copyright (C) Citrix Systems Inc.
4#
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU Lesser General Public License as published
7# by the Free Software Foundation; version 2.1 only.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU Lesser General Public License for more details.
13#
14# You should have received a copy of the GNU Lesser General Public License
15# along with this program; if not, write to the Free Software Foundation, Inc.,
16# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17#
18# LVMSR: VHD and QCOW2 on LVM storage repository
19#
21from sm_typing import Dict, List, override
23import SR
24from SR import deviceCheck
25import VDI
26import SRCommand
27import util
28import lvutil
29import lvmcache
30import scsiutil
31import lock
32import os
33import sys
34import time
35import errno
36import xs_errors
37import cleanup
38import blktap2
39from journaler import Journaler
40from refcounter import RefCounter
41from ipc import IPCFlag
42from constants import NS_PREFIX_LVM, VG_LOCATION, VG_PREFIX, CBT_BLOCK_SIZE
43from cowutil import CowUtil, getCowUtil, getImageStringFromVdiType, getVdiTypeFromImageFormat
44from lvmcowutil import LV_PREFIX, LvmCowUtil
45from lvmanager import LVActivator
46from vditype import VdiType
47import XenAPI # pylint: disable=import-error
48import re
49from srmetadata import ALLOCATION_TAG, NAME_LABEL_TAG, NAME_DESCRIPTION_TAG, \
50 UUID_TAG, IS_A_SNAPSHOT_TAG, SNAPSHOT_OF_TAG, TYPE_TAG, VDI_TYPE_TAG, \
51 READ_ONLY_TAG, MANAGED_TAG, SNAPSHOT_TIME_TAG, METADATA_OF_POOL_TAG, \
52 LVMMetadataHandler, METADATA_OBJECT_TYPE_VDI, \
53 METADATA_OBJECT_TYPE_SR, METADATA_UPDATE_OBJECT_TYPE_TAG
54from metadata import retrieveXMLfromFile, _parseXML
55from xmlrpc.client import DateTime
56import glob
57from constants import CBTLOG_TAG
58from fairlock import Fairlock
59DEV_MAPPER_ROOT = os.path.join('/dev/mapper', VG_PREFIX)
61geneology: Dict[str, List[str]] = {}
62CAPABILITIES = ["SR_PROBE", "SR_UPDATE", "SR_TRIM",
63 "VDI_CREATE", "VDI_DELETE", "VDI_ATTACH", "VDI_DETACH", "VDI_MIRROR",
64 "VDI_CLONE", "VDI_SNAPSHOT", "VDI_RESIZE", "ATOMIC_PAUSE",
65 "VDI_RESET_ON_BOOT/2", "VDI_UPDATE", "VDI_CONFIG_CBT",
66 "VDI_ACTIVATE", "VDI_DEACTIVATE"]
68CONFIGURATION = [['device', 'local device path (required) (e.g. /dev/sda3)']]
70DRIVER_INFO = {
71 'name': 'Local VHD and QCOW2 on LVM',
72 'description': 'SR plugin which represents disks as VHD and QCOW2 disks on ' + \
73 'Logical Volumes within a locally-attached Volume Group',
74 'vendor': 'XenSource Inc',
75 'copyright': '(C) 2008 XenSource Inc',
76 'driver_version': '1.0',
77 'required_api_version': '1.0',
78 'capabilities': CAPABILITIES,
79 'configuration': CONFIGURATION
80 }
82OPS_EXCLUSIVE = [
83 "sr_create", "sr_delete", "sr_attach", "sr_detach", "sr_scan",
84 "sr_update", "vdi_create", "vdi_delete", "vdi_resize", "vdi_snapshot",
85 "vdi_clone"]
87# Log if snapshot pauses VM for more than this many seconds
88LONG_SNAPTIME = 60
90class LVMSR(SR.SR):
91 DRIVER_TYPE = 'lvhd'
93 PROVISIONING_TYPES = ["thin", "thick"]
94 PROVISIONING_DEFAULT = "thick"
95 THIN_PLUGIN = "lvhd-thin"
97 PLUGIN_ON_SLAVE = "on-slave"
98 PLUGIN_ON_MASTER = "on-master"
100 FLAG_USE_VHD = "use_vhd"
101 MDVOLUME_NAME = "MGT"
103 ALLOCATION_QUANTUM = "allocation_quantum"
104 INITIAL_ALLOCATION = "initial_allocation"
106 LOCK_RETRY_INTERVAL = 3
107 LOCK_RETRY_ATTEMPTS = 10
109 TEST_MODE_KEY = "testmode"
110 TEST_MODE_VHD_FAIL_REPARENT_BEGIN = "vhd_fail_reparent_begin"
111 TEST_MODE_VHD_FAIL_REPARENT_LOCATOR = "vhd_fail_reparent_locator"
112 TEST_MODE_VHD_FAIL_REPARENT_END = "vhd_fail_reparent_end"
113 TEST_MODE_VHD_FAIL_RESIZE_BEGIN = "vhd_fail_resize_begin"
114 TEST_MODE_VHD_FAIL_RESIZE_DATA = "vhd_fail_resize_data"
115 TEST_MODE_VHD_FAIL_RESIZE_METADATA = "vhd_fail_resize_metadata"
116 TEST_MODE_VHD_FAIL_RESIZE_END = "vhd_fail_resize_end"
118 ENV_VAR_VHD_TEST = {
119 TEST_MODE_VHD_FAIL_REPARENT_BEGIN:
120 "VHD_UTIL_TEST_FAIL_REPARENT_BEGIN",
121 TEST_MODE_VHD_FAIL_REPARENT_LOCATOR:
122 "VHD_UTIL_TEST_FAIL_REPARENT_LOCATOR",
123 TEST_MODE_VHD_FAIL_REPARENT_END:
124 "VHD_UTIL_TEST_FAIL_REPARENT_END",
125 TEST_MODE_VHD_FAIL_RESIZE_BEGIN:
126 "VHD_UTIL_TEST_FAIL_RESIZE_BEGIN",
127 TEST_MODE_VHD_FAIL_RESIZE_DATA:
128 "VHD_UTIL_TEST_FAIL_RESIZE_DATA_MOVED",
129 TEST_MODE_VHD_FAIL_RESIZE_METADATA:
130 "VHD_UTIL_TEST_FAIL_RESIZE_METADATA_MOVED",
131 TEST_MODE_VHD_FAIL_RESIZE_END:
132 "VHD_UTIL_TEST_FAIL_RESIZE_END"
133 }
134 testMode = ""
136 legacyMode = True
138 @override
139 @staticmethod
140 def handles(type) -> bool:
141 """Returns True if this SR class understands the given dconf string"""
142 # we can pose as LVMSR or EXTSR for compatibility purposes
143 if __name__ == '__main__':
144 name = sys.argv[0]
145 else:
146 name = __name__
147 if name.endswith("LVMSR"):
148 return type == "lvm"
149 elif name.endswith("EXTSR"):
150 return type == "ext"
151 return type == LVMSR.DRIVER_TYPE
153 def __init__(self, srcmd, sr_uuid):
154 SR.SR.__init__(self, srcmd, sr_uuid)
155 self._init_image_formats()
157 @override
158 def load(self, sr_uuid) -> None:
159 self.ops_exclusive = OPS_EXCLUSIVE
161 self.isMaster = False
162 if 'SRmaster' in self.dconf:
163 self.isMaster = self.dconf['SRmaster'] == 'true'
164 elif self.sr_ref and self.session:
165 # SR.from_uuid() builds dconf straight from the PBD's stored
166 # device-config, which never carries the synthetic SRmaster key;
167 # XAPI only injects that key into the params of a live SM call.
168 self.isMaster = not self.is_shared() or util.is_master(self.session)
170 self.lock = lock.Lock(lock.LOCK_TYPE_SR, self.uuid)
171 self.sr_vditype = SR.DEFAULT_TAP
172 self.uuid = sr_uuid
173 self.vgname = VG_PREFIX + self.uuid
174 self.path = os.path.join(VG_LOCATION, self.vgname)
175 self.mdpath = os.path.join(self.path, self.MDVOLUME_NAME)
176 self.provision = self.PROVISIONING_DEFAULT
178 has_sr_ref = self.srcmd.params.get("sr_ref")
179 if has_sr_ref:
180 self.other_conf = self.session.xenapi.SR.get_other_config(self.sr_ref)
181 else:
182 self.other_conf = None
184 self.lvm_conf = None
185 if self.other_conf:
186 self.lvm_conf = self.other_conf.get('lvm-conf')
188 try:
189 self.lvmCache = lvmcache.LVMCache(self.vgname, self.lvm_conf)
190 except:
191 raise xs_errors.XenError('SRUnavailable', \
192 opterr='Failed to initialise the LVMCache')
193 self.lvActivator = LVActivator(self.uuid, self.lvmCache)
194 self.journaler = Journaler(self.lvmCache)
195 if not has_sr_ref:
196 return # must be a probe call
197 # Test for thick vs thin provisioning conf parameter
198 if 'allocation' in self.dconf: 198 ↛ 199line 198 didn't jump to line 199, because the condition on line 198 was never true
199 if self.dconf['allocation'] in self.PROVISIONING_TYPES:
200 self.provision = self.dconf['allocation']
201 else:
202 raise xs_errors.XenError('InvalidArg', \
203 opterr='Allocation parameter must be one of %s' % self.PROVISIONING_TYPES)
205 if self.other_conf.get(self.TEST_MODE_KEY): 205 ↛ 209line 205 didn't jump to line 209, because the condition on line 205 was never false
206 self.testMode = self.other_conf[self.TEST_MODE_KEY]
207 self._prepareTestMode()
209 self.sm_config = self.session.xenapi.SR.get_sm_config(self.sr_ref)
210 # sm_config flag overrides PBD, if any
211 if self.sm_config.get('allocation') in self.PROVISIONING_TYPES:
212 self.provision = self.sm_config.get('allocation')
214 if self.sm_config.get(self.FLAG_USE_VHD) == "true":
215 self.legacyMode = False
217 if lvutil._checkVG(self.vgname):
218 if self.isMaster and not self.cmd in ["vdi_attach", "vdi_detach", 218 ↛ 221line 218 didn't jump to line 221, because the condition on line 218 was never false
219 "vdi_activate", "vdi_deactivate", "nop"]:
220 self._undoAllJournals()
221 if not self.cmd in ["sr_attach", "sr_probe"]:
222 self._checkMetadataVolume()
224 self.mdexists = False
226 # get a VDI -> TYPE map from the storage
227 contains_uuid_regex = \
228 re.compile("^.*[0-9a-f]{8}-(([0-9a-f]{4})-){3}[0-9a-f]{12}.*")
229 self.storageVDIs = {}
231 for key in self.lvmCache.lvs.keys(): 231 ↛ 233line 231 didn't jump to line 233, because the loop on line 231 never started
232 # if the lvname has a uuid in it
233 type = None
234 vdi = None
235 if contains_uuid_regex.search(key) is not None:
236 for vdi_type, prefix in LV_PREFIX.items():
237 if key.startswith(prefix):
238 vdi = key[len(prefix):]
239 self.storageVDIs[vdi] = vdi_type
240 break
242 # check if metadata volume exists
243 try:
244 self.mdexists = self.lvmCache.checkLV(self.MDVOLUME_NAME)
245 except:
246 pass
248 @override
249 def cleanup(self) -> None:
250 # we don't need to hold the lock to dec refcounts of activated LVs
251 if not self.lvActivator.deactivateAll(): 251 ↛ 252line 251 didn't jump to line 252, because the condition on line 251 was never true
252 raise util.SMException("failed to deactivate LVs")
254 def updateSRMetadata(self, allocation):
255 try:
256 # Add SR specific SR metadata
257 sr_info = \
258 {ALLOCATION_TAG: allocation,
259 UUID_TAG: self.uuid,
260 NAME_LABEL_TAG: util.to_plain_string(self.session.xenapi.SR.get_name_label(self.sr_ref)),
261 NAME_DESCRIPTION_TAG: util.to_plain_string(self.session.xenapi.SR.get_name_description(self.sr_ref))
262 }
264 vdi_info = {}
265 for vdi in self.session.xenapi.SR.get_VDIs(self.sr_ref):
266 vdi_uuid = self.session.xenapi.VDI.get_uuid(vdi)
267 is_a_snapshot = int(self.session.xenapi.VDI.get_is_a_snapshot(vdi))
269 vdi_type = self.session.xenapi.VDI.get_sm_config(vdi).get('vdi_type')
270 if not vdi_type:
271 raise xs_errors.XenError('MetadataError', opterr=f"Missing `vdi_type` for VDI {vdi_uuid}")
273 # Create the VDI entry in the SR metadata
274 vdi_info[vdi_uuid] = \
275 {
276 UUID_TAG: vdi_uuid,
277 NAME_LABEL_TAG: util.to_plain_string(self.session.xenapi.VDI.get_name_label(vdi)),
278 NAME_DESCRIPTION_TAG: util.to_plain_string(self.session.xenapi.VDI.get_name_description(vdi)),
279 IS_A_SNAPSHOT_TAG: \
280 is_a_snapshot,
281 SNAPSHOT_OF_TAG: \
282 self.session.xenapi.VDI.get_uuid(self.session.xenapi.VDI.get_snapshot_of(vdi)) if bool(is_a_snapshot) else '',
283 SNAPSHOT_TIME_TAG: \
284 self.session.xenapi.VDI.get_snapshot_time(vdi),
285 TYPE_TAG: \
286 self.session.xenapi.VDI.get_type(vdi),
287 VDI_TYPE_TAG: \
288 vdi_type,
289 READ_ONLY_TAG: \
290 int(self.session.xenapi.VDI.get_read_only(vdi)),
291 METADATA_OF_POOL_TAG: \
292 self.session.xenapi.VDI.get_metadata_of_pool(vdi),
293 MANAGED_TAG: \
294 int(self.session.xenapi.VDI.get_managed(vdi))
295 }
296 LVMMetadataHandler(self.mdpath).writeMetadata(sr_info, vdi_info)
298 except Exception as e:
299 raise xs_errors.XenError('MetadataError', \
300 opterr='Error upgrading SR Metadata: %s' % str(e))
302 def syncMetadataAndStorage(self):
303 try:
304 # if a VDI is present in the metadata but not in the storage
305 # then delete it from the metadata
306 vdi_info = LVMMetadataHandler(self.mdpath, False).getMetadata()[1]
307 for vdi in list(vdi_info.keys()):
308 update_map = {}
309 if not vdi_info[vdi][UUID_TAG] in set(self.storageVDIs.keys()): 309 ↛ 316line 309 didn't jump to line 316, because the condition on line 309 was never false
310 # delete this from metadata
311 LVMMetadataHandler(self.mdpath). \
312 deleteVdiFromMetadata(vdi_info[vdi][UUID_TAG])
313 else:
314 # search for this in the metadata, compare types
315 # self.storageVDIs is a map of vdi_uuid to vdi_type
316 if vdi_info[vdi][VDI_TYPE_TAG] != \
317 self.storageVDIs[vdi_info[vdi][UUID_TAG]]:
318 # storage type takes authority
319 update_map[METADATA_UPDATE_OBJECT_TYPE_TAG] \
320 = METADATA_OBJECT_TYPE_VDI
321 update_map[UUID_TAG] = vdi_info[vdi][UUID_TAG]
322 update_map[VDI_TYPE_TAG] = \
323 self.storageVDIs[vdi_info[vdi][UUID_TAG]]
324 LVMMetadataHandler(self.mdpath) \
325 .updateMetadata(update_map)
326 else:
327 # This should never happen
328 pass
330 except Exception as e:
331 raise xs_errors.XenError('MetadataError', \
332 opterr='Error synching SR Metadata and storage: %s' % str(e))
334 def syncMetadataAndXapi(self):
335 try:
336 # get metadata
337 (sr_info, vdi_info) = \
338 LVMMetadataHandler(self.mdpath, False).getMetadata()
340 # First synch SR parameters
341 self.update(self.uuid)
343 # Now update the VDI information in the metadata if required
344 for vdi_offset in vdi_info.keys():
345 try:
346 vdi_ref = \
347 self.session.xenapi.VDI.get_by_uuid( \
348 vdi_info[vdi_offset][UUID_TAG])
349 except:
350 # may be the VDI is not in XAPI yet dont bother
351 continue
353 new_name_label = util.to_plain_string(self.session.xenapi.VDI.get_name_label(vdi_ref))
354 new_name_description = util.to_plain_string(self.session.xenapi.VDI.get_name_description(vdi_ref))
356 if vdi_info[vdi_offset][NAME_LABEL_TAG] != new_name_label or \
357 vdi_info[vdi_offset][NAME_DESCRIPTION_TAG] != \
358 new_name_description:
359 update_map = {}
360 update_map[METADATA_UPDATE_OBJECT_TYPE_TAG] = \
361 METADATA_OBJECT_TYPE_VDI
362 update_map[UUID_TAG] = vdi_info[vdi_offset][UUID_TAG]
363 update_map[NAME_LABEL_TAG] = new_name_label
364 update_map[NAME_DESCRIPTION_TAG] = new_name_description
365 LVMMetadataHandler(self.mdpath) \
366 .updateMetadata(update_map)
367 except Exception as e:
368 raise xs_errors.XenError('MetadataError', \
369 opterr='Error synching SR Metadata and XAPI: %s' % str(e))
371 def _checkMetadataVolume(self):
372 util.SMlog("Entering _checkMetadataVolume")
373 self.mdexists = self.lvmCache.checkLV(self.MDVOLUME_NAME)
374 if self.isMaster: 374 ↛ 390line 374 didn't jump to line 390, because the condition on line 374 was never false
375 if self.mdexists and self.cmd == "sr_attach":
376 try:
377 # activate the management volume
378 # will be deactivated at detach time
379 self.lvmCache.activateNoRefcount(self.MDVOLUME_NAME)
380 self._synchSmConfigWithMetaData()
381 util.SMlog("Sync SR metadata and the state on the storage.")
382 self.syncMetadataAndStorage()
383 self.syncMetadataAndXapi()
384 except Exception as e:
385 util.SMlog("Exception in _checkMetadataVolume, " \
386 "Error: %s." % str(e))
387 elif not self.mdexists and not self.legacyMode:
388 self._introduceMetaDataVolume()
390 if self.mdexists:
391 self.legacyMode = False
393 def _synchSmConfigWithMetaData(self):
394 util.SMlog("Synching sm-config with metadata volume")
396 try:
397 # get SR info from metadata
398 sr_info = {}
399 map = {}
400 sr_info = LVMMetadataHandler(self.mdpath, False).getMetadata()[0]
402 if sr_info == {}: 402 ↛ 403line 402 didn't jump to line 403, because the condition on line 402 was never true
403 raise Exception("Failed to get SR information from metadata.")
405 if "allocation" in sr_info: 405 ↛ 409line 405 didn't jump to line 409, because the condition on line 405 was never false
406 self.provision = sr_info.get("allocation")
407 map['allocation'] = sr_info.get("allocation")
408 else:
409 raise Exception("Allocation key not found in SR metadata. "
410 "SR info found: %s" % sr_info)
412 except Exception as e:
413 raise xs_errors.XenError(
414 'MetadataError',
415 opterr='Error reading SR params from '
416 'metadata Volume: %s' % str(e))
417 try:
418 map[self.FLAG_USE_VHD] = 'true'
419 self.session.xenapi.SR.set_sm_config(self.sr_ref, map)
420 except:
421 raise xs_errors.XenError(
422 'MetadataError',
423 opterr='Error updating sm_config key')
425 def _introduceMetaDataVolume(self):
426 util.SMlog("Creating Metadata volume")
427 try:
428 config = {}
429 self.lvmCache.create(self.MDVOLUME_NAME, 4 * 1024 * 1024)
431 # activate the management volume, will be deactivated at detach time
432 self.lvmCache.activateNoRefcount(self.MDVOLUME_NAME)
434 name_label = util.to_plain_string( \
435 self.session.xenapi.SR.get_name_label(self.sr_ref))
436 name_description = util.to_plain_string( \
437 self.session.xenapi.SR.get_name_description(self.sr_ref))
438 config[self.FLAG_USE_VHD] = "true"
439 config['allocation'] = self.provision
440 self.session.xenapi.SR.set_sm_config(self.sr_ref, config)
442 # Add the SR metadata
443 self.updateSRMetadata(self.provision)
444 except Exception as e:
445 raise xs_errors.XenError('MetadataError', \
446 opterr='Error introducing Metadata Volume: %s' % str(e))
448 def _removeMetadataVolume(self):
449 if self.mdexists:
450 try:
451 self.lvmCache.remove(self.MDVOLUME_NAME)
452 except:
453 raise xs_errors.XenError('MetadataError', \
454 opterr='Failed to delete MGT Volume')
456 def _refresh_size(self):
457 """
458 Refreshs the size of the backing device.
459 Return true if all paths/devices agree on the same size.
460 """
461 if hasattr(self, 'SCSIid'): 461 ↛ 463line 461 didn't jump to line 463, because the condition on line 461 was never true
462 # LVMoHBASR, LVMoISCSISR
463 return scsiutil.refresh_lun_size_by_SCSIid(getattr(self, 'SCSIid'))
464 else:
465 # LVMSR
466 devices = self.dconf['device'].split(',')
467 scsiutil.refreshdev(devices)
468 return True
470 def _expand_size(self):
471 """
472 Expands the size of the SR by growing into additional availiable
473 space, if extra space is availiable on the backing device.
474 Needs to be called after a successful call of _refresh_size.
475 """
476 currentvgsize = lvutil._getVGstats(self.vgname)['physical_size']
477 # We are comparing PV- with VG-sizes that are aligned. Need a threshold
478 resizethreshold = 100 * 1024 * 1024 # 100MB
479 devices = self.dconf['device'].split(',')
480 totaldevicesize = 0
481 for device in devices:
482 totaldevicesize = totaldevicesize + scsiutil.getsize(device)
483 if totaldevicesize >= (currentvgsize + resizethreshold):
484 try:
485 if hasattr(self, 'SCSIid'): 485 ↛ 487line 485 didn't jump to line 487, because the condition on line 485 was never true
486 # LVMoHBASR, LVMoISCSISR might have slaves
487 scsiutil.refresh_lun_size_by_SCSIid_on_slaves(self.session,
488 getattr(self, 'SCSIid'))
489 util.SMlog("LVMSR._expand_size for %s will resize the pv." %
490 self.uuid)
491 for pv in lvutil.get_pv_for_vg(self.vgname):
492 lvutil.resizePV(pv)
493 except:
494 util.logException("LVMSR._expand_size for %s failed to resize"
495 " the PV" % self.uuid)
497 @override
498 @deviceCheck
499 def create(self, uuid, size) -> None:
500 util.SMlog("LVMSR.create for %s" % self.uuid)
501 if not self.isMaster:
502 util.SMlog('sr_create blocked for non-master')
503 raise xs_errors.XenError('LVMMaster')
505 if lvutil._checkVG(self.vgname):
506 raise xs_errors.XenError('SRExists')
508 # Check none of the devices already in use by other PBDs
509 if util.test_hostPBD_devs(self.session, uuid, self.dconf['device']):
510 raise xs_errors.XenError('SRInUse')
512 # Check serial number entry in SR records
513 for dev in self.dconf['device'].split(','):
514 if util.test_scsiserial(self.session, dev):
515 raise xs_errors.XenError('SRInUse')
517 lvutil.createVG(self.dconf['device'], self.vgname)
519 #Update serial number string
520 scsiutil.add_serial_record(self.session, self.sr_ref, \
521 scsiutil.devlist_to_serialstring(self.dconf['device'].split(',')))
523 # since this is an SR.create turn off legacy mode
524 self.session.xenapi.SR.add_to_sm_config(self.sr_ref, \
525 self.FLAG_USE_VHD, 'true')
527 @override
528 def delete(self, uuid) -> None:
529 util.SMlog("LVMSR.delete for %s" % self.uuid)
530 if not self.isMaster:
531 raise xs_errors.XenError('LVMMaster')
532 cleanup.gc_force(self.session, self.uuid)
534 success = True
535 for fileName in glob.glob(DEV_MAPPER_ROOT + '*'):
536 if util.extractSRFromDevMapper(fileName) != self.uuid:
537 continue
539 if util.doesFileHaveOpenHandles(fileName):
540 util.SMlog("LVMSR.delete: The dev mapper entry %s has open " \
541 "handles" % fileName)
542 success = False
543 continue
545 # Now attempt to remove the dev mapper entry
546 if not lvutil.removeDevMapperEntry(fileName, False):
547 success = False
548 continue
550 try:
551 lvname = os.path.basename(fileName.replace('-', '/'). \
552 replace('//', '-'))
553 lpath = os.path.join(self.path, lvname)
554 os.unlink(lpath)
555 except OSError as e:
556 if e.errno != errno.ENOENT:
557 util.SMlog("LVMSR.delete: failed to remove the symlink for " \
558 "file %s. Error: %s" % (fileName, str(e)))
559 success = False
561 if success:
562 try:
563 if util.pathexists(self.path):
564 os.rmdir(self.path)
565 except Exception as e:
566 util.SMlog("LVMSR.delete: failed to remove the symlink " \
567 "directory %s. Error: %s" % (self.path, str(e)))
568 success = False
570 self._removeMetadataVolume()
571 self.lvmCache.refresh()
572 if LvmCowUtil.getVolumeInfo(self.lvmCache):
573 raise xs_errors.XenError('SRNotEmpty')
575 if not success:
576 raise Exception("LVMSR delete failed, please refer to the log " \
577 "for details.")
579 lvutil.removeVG(self.dconf['device'], self.vgname)
580 self._cleanup()
582 @override
583 def attach(self, uuid) -> None:
584 util.SMlog("LVMSR.attach for %s" % self.uuid)
586 self._cleanup(True) # in case of host crashes, if detach wasn't called
588 if not util.match_uuid(self.uuid) or not lvutil._checkVG(self.vgname): 588 ↛ 589line 588 didn't jump to line 589, because the condition on line 588 was never true
589 raise xs_errors.XenError('SRUnavailable', \
590 opterr='no such volume group: %s' % self.vgname)
592 # Refresh the metadata status
593 self._checkMetadataVolume()
595 refreshsizeok = self._refresh_size()
597 if self.isMaster: 597 ↛ 608line 597 didn't jump to line 608, because the condition on line 597 was never false
598 if refreshsizeok: 598 ↛ 602line 598 didn't jump to line 602, because the condition on line 598 was never false
599 self._expand_size()
601 # Update SCSIid string
602 util.SMlog("Calling devlist_to_serial")
603 scsiutil.add_serial_record(
604 self.session, self.sr_ref,
605 scsiutil.devlist_to_serialstring(self.dconf['device'].split(',')))
607 # Test Legacy Mode Flag and update if COW volumes exist
608 if self.isMaster and self.legacyMode: 608 ↛ 609line 608 didn't jump to line 609, because the condition on line 608 was never true
609 vdiInfo = LvmCowUtil.getVDIInfo(self.lvmCache)
610 for uuid, info in vdiInfo.items():
611 if VdiType.isCowImage(info.vdiType):
612 self.legacyMode = False
613 map = self.session.xenapi.SR.get_sm_config(self.sr_ref)
614 self._introduceMetaDataVolume()
615 break
617 # Set the block scheduler
618 for dev in self.dconf['device'].split(','):
619 self.block_setscheduler(dev)
621 @override
622 def detach(self, uuid) -> None:
623 util.SMlog("LVMSR.detach for %s" % self.uuid)
624 cleanup.abort(self.uuid)
626 # Do a best effort cleanup of the dev mapper entries
627 # go through all devmapper entries for this VG
628 success = True
629 for fileName in glob.glob(DEV_MAPPER_ROOT + '*'):
630 if util.extractSRFromDevMapper(fileName) != self.uuid: 630 ↛ 631line 630 didn't jump to line 631, because the condition on line 630 was never true
631 continue
633 with Fairlock('devicemapper'):
634 # check if any file has open handles
635 if util.doesFileHaveOpenHandles(fileName):
636 # if yes, log this and signal failure
637 util.SMlog(
638 f"LVMSR.detach: The dev mapper entry {fileName} has "
639 "open handles")
640 success = False
641 continue
643 # Now attempt to remove the dev mapper entry
644 if not lvutil.removeDevMapperEntry(fileName, False): 644 ↛ 645line 644 didn't jump to line 645, because the condition on line 644 was never true
645 success = False
646 continue
648 # also remove the symlinks from /dev/VG-XenStorage-SRUUID/*
649 try:
650 lvname = os.path.basename(fileName.replace('-', '/'). \
651 replace('//', '-'))
652 lvname = os.path.join(self.path, lvname)
653 util.force_unlink(lvname)
654 except Exception as e:
655 util.SMlog("LVMSR.detach: failed to remove the symlink for " \
656 "file %s. Error: %s" % (fileName, str(e)))
657 success = False
659 # now remove the directory where the symlinks are
660 # this should pass as the directory should be empty by now
661 if success:
662 try:
663 if util.pathexists(self.path): 663 ↛ 664line 663 didn't jump to line 664, because the condition on line 663 was never true
664 os.rmdir(self.path)
665 except Exception as e:
666 util.SMlog("LVMSR.detach: failed to remove the symlink " \
667 "directory %s. Error: %s" % (self.path, str(e)))
668 success = False
670 if not success:
671 raise Exception("SR detach failed, please refer to the log " \
672 "for details.")
674 # Don't delete lock files on the master as it will break the locking
675 # between SM and any GC thread that survives through SR.detach.
676 # However, we should still delete lock files on slaves as it is the
677 # only place to do so.
678 self._cleanup(self.isMaster)
680 @override
681 def forget_vdi(self, uuid) -> None:
682 if not self.legacyMode:
683 LVMMetadataHandler(self.mdpath).deleteVdiFromMetadata(uuid)
684 super(LVMSR, self).forget_vdi(uuid)
686 @override
687 def scan(self, uuid) -> None:
688 activated_lvs = set()
689 try:
690 util.SMlog("LVMSR.scan for %s" % self.uuid)
691 if not self.isMaster: 691 ↛ 692line 691 didn't jump to line 692, because the condition on line 691 was never true
692 util.SMlog('sr_scan blocked for non-master')
693 raise xs_errors.XenError('LVMMaster')
695 if self._refresh_size(): 695 ↛ 697line 695 didn't jump to line 697, because the condition on line 695 was never false
696 self._expand_size()
697 self.lvmCache.refresh()
698 cbt_vdis = self.lvmCache.getTagged(CBTLOG_TAG)
699 self._loadvdis()
700 stats = lvutil._getVGstats(self.vgname)
701 self.physical_size = stats['physical_size']
702 self.physical_utilisation = stats['physical_utilisation']
704 # Now check if there are any VDIs in the metadata, which are not in
705 # XAPI
706 if self.mdexists: 706 ↛ 825line 706 didn't jump to line 825, because the condition on line 706 was never false
707 vdiToSnaps: Dict[str, List[str]] = {}
708 # get VDIs from XAPI
709 vdis = self.session.xenapi.SR.get_VDIs(self.sr_ref)
710 vdi_uuids = set([])
711 for vdi in vdis:
712 vdi_uuids.add(self.session.xenapi.VDI.get_uuid(vdi))
714 info = LVMMetadataHandler(self.mdpath, False).getMetadata()[1]
716 for vdi in list(info.keys()):
717 vdi_uuid = info[vdi][UUID_TAG]
718 if bool(int(info[vdi][IS_A_SNAPSHOT_TAG])): 718 ↛ 719line 718 didn't jump to line 719, because the condition on line 718 was never true
719 if info[vdi][SNAPSHOT_OF_TAG] in vdiToSnaps:
720 vdiToSnaps[info[vdi][SNAPSHOT_OF_TAG]].append(vdi_uuid)
721 else:
722 vdiToSnaps[info[vdi][SNAPSHOT_OF_TAG]] = [vdi_uuid]
724 if vdi_uuid not in vdi_uuids:
725 vdi_type = info[vdi][VDI_TYPE_TAG]
726 sm_config = {}
727 sm_config['vdi_type'] = vdi_type
728 lvname = "%s%s" % (LV_PREFIX[sm_config['vdi_type']], vdi_uuid)
729 if not self.lvmCache.checkLV(lvname):
730 util.SMlog("VDI %s is present in metadata but "
731 "its LV %s does not exist in the VG; "
732 "removing from metadata." %
733 (vdi_uuid, lvname))
734 LVMMetadataHandler(self.mdpath). \
735 deleteVdiFromMetadata(vdi_uuid)
736 continue
737 util.SMlog("Introduce VDI %s as it is present in " \
738 "metadata and not in XAPI." % vdi_uuid)
739 self.lvActivator.activate(
740 vdi_uuid, lvname, LVActivator.NORMAL)
741 activated_lvs.add(vdi_uuid)
742 lvPath = os.path.join(self.path, lvname)
744 if not VdiType.isCowImage(vdi_type):
745 size = self.lvmCache.getSize(LV_PREFIX[vdi_type] + vdi_uuid)
746 utilisation = \
747 util.roundup(lvutil.LVM_SIZE_INCREMENT,
748 int(size))
749 else:
750 cowutil = getCowUtil(vdi_type)
751 lvmcowutil = LvmCowUtil(cowutil)
753 parent = cowutil.getParentNoCheck(lvPath)
755 if parent is not None: 755 ↛ 756line 755 didn't jump to line 756, because the condition on line 755 was never true
756 sm_config['vhd-parent'] = parent[parent.find('-') + 1:]
757 size = cowutil.getSizeVirt(lvPath)
758 if self.provision == "thin": 758 ↛ 759line 758 didn't jump to line 759, because the condition on line 758 was never true
759 utilisation = util.roundup(
760 lvutil.LVM_SIZE_INCREMENT,
761 cowutil.calcOverheadEmpty(max(size, cowutil.getDefaultPreallocationSizeVirt()))
762 )
763 else:
764 utilisation = lvmcowutil.calcVolumeSize(int(size))
766 vdi_ref = self.session.xenapi.VDI.db_introduce(
767 vdi_uuid,
768 info[vdi][NAME_LABEL_TAG],
769 info[vdi][NAME_DESCRIPTION_TAG],
770 self.sr_ref,
771 info[vdi][TYPE_TAG],
772 False,
773 bool(int(info[vdi][READ_ONLY_TAG])),
774 {},
775 vdi_uuid,
776 {},
777 sm_config)
779 self.session.xenapi.VDI.set_managed(vdi_ref,
780 bool(int(info[vdi][MANAGED_TAG])))
781 self.session.xenapi.VDI.set_virtual_size(vdi_ref,
782 str(size))
783 self.session.xenapi.VDI.set_physical_utilisation( \
784 vdi_ref, str(utilisation))
785 self.session.xenapi.VDI.set_is_a_snapshot( \
786 vdi_ref, bool(int(info[vdi][IS_A_SNAPSHOT_TAG])))
787 if bool(int(info[vdi][IS_A_SNAPSHOT_TAG])): 787 ↛ 788line 787 didn't jump to line 788, because the condition on line 787 was never true
788 self.session.xenapi.VDI.set_snapshot_time( \
789 vdi_ref, DateTime(info[vdi][SNAPSHOT_TIME_TAG]))
790 if info[vdi][TYPE_TAG] == 'metadata': 790 ↛ 791line 790 didn't jump to line 791, because the condition on line 790 was never true
791 self.session.xenapi.VDI.set_metadata_of_pool( \
792 vdi_ref, info[vdi][METADATA_OF_POOL_TAG])
794 # Update CBT status of disks either just added
795 # or already in XAPI
796 cbt_logname = "%s.%s" % (vdi_uuid, CBTLOG_TAG)
797 if cbt_logname in cbt_vdis: 797 ↛ 798line 797 didn't jump to line 798, because the condition on line 797 was never true
798 vdi_ref = self.session.xenapi.VDI.get_by_uuid(vdi_uuid)
799 self.session.xenapi.VDI.set_cbt_enabled(vdi_ref, True)
800 # For existing VDIs, update local state too
801 # Scan in base class SR updates existing VDIs
802 # again based on local states
803 if vdi_uuid in self.vdis:
804 self.vdis[vdi_uuid].cbt_enabled = True
805 cbt_vdis.remove(cbt_logname)
807 # Now set the snapshot statuses correctly in XAPI
808 for srcvdi in vdiToSnaps.keys(): 808 ↛ 809line 808 didn't jump to line 809, because the loop on line 808 never started
809 try:
810 srcref = self.session.xenapi.VDI.get_by_uuid(srcvdi)
811 except:
812 # the source VDI no longer exists, continue
813 continue
815 for snapvdi in vdiToSnaps[srcvdi]:
816 try:
817 # this might fail in cases where its already set
818 snapref = \
819 self.session.xenapi.VDI.get_by_uuid(snapvdi)
820 self.session.xenapi.VDI.set_snapshot_of(snapref, srcref)
821 except Exception as e:
822 util.SMlog("Setting snapshot failed. " \
823 "Error: %s" % str(e))
825 if cbt_vdis: 825 ↛ 836line 825 didn't jump to line 836, because the condition on line 825 was never false
826 # If we have items remaining in this list,
827 # they are cbt_metadata VDI that XAPI doesn't know about
828 # Add them to self.vdis and they'll get added to the DB
829 for cbt_vdi in cbt_vdis: 829 ↛ 830line 829 didn't jump to line 830, because the loop on line 829 never started
830 cbt_uuid = cbt_vdi.split(".")[0]
831 new_vdi = self.vdi(cbt_uuid)
832 new_vdi.ty = "cbt_metadata"
833 new_vdi.cbt_enabled = True
834 self.vdis[cbt_uuid] = new_vdi
836 super(LVMSR, self).scan(uuid)
837 self._kickGC()
839 finally:
840 for vdi in activated_lvs:
841 self.lvActivator.deactivate(
842 vdi, LVActivator.NORMAL, False)
844 @override
845 def update(self, uuid) -> None:
846 if not lvutil._checkVG(self.vgname): 846 ↛ 847line 846 didn't jump to line 847, because the condition on line 846 was never true
847 return
848 self._updateStats(uuid, 0)
850 if self.legacyMode: 850 ↛ 851line 850 didn't jump to line 851, because the condition on line 850 was never true
851 return
853 # synch name_label in metadata with XAPI
854 update_map = {}
855 update_map = {METADATA_UPDATE_OBJECT_TYPE_TAG: \
856 METADATA_OBJECT_TYPE_SR,
857 NAME_LABEL_TAG: util.to_plain_string( \
858 self.session.xenapi.SR.get_name_label(self.sr_ref)),
859 NAME_DESCRIPTION_TAG: util.to_plain_string( \
860 self.session.xenapi.SR.get_name_description(self.sr_ref))
861 }
862 LVMMetadataHandler(self.mdpath).updateMetadata(update_map)
864 def _updateStats(self, uuid, virtAllocDelta):
865 valloc = int(self.session.xenapi.SR.get_virtual_allocation(self.sr_ref))
866 self.virtual_allocation = valloc + virtAllocDelta
867 util.SMlog("Setting virtual_allocation of SR %s to %d" %
868 (uuid, self.virtual_allocation))
869 stats = lvutil._getVGstats(self.vgname)
870 self.physical_size = stats['physical_size']
871 self.physical_utilisation = stats['physical_utilisation']
872 self._db_update()
874 @override
875 @deviceCheck
876 def probe(self) -> str:
877 return lvutil.srlist_toxml(
878 lvutil.scan_srlist(VG_PREFIX, self.dconf['device']),
879 VG_PREFIX,
880 ('metadata' in self.srcmd.params['sr_sm_config'] and \
881 self.srcmd.params['sr_sm_config']['metadata'] == 'true'))
883 @override
884 def vdi(self, uuid) -> VDI.VDI:
885 return LVMVDI(self, uuid)
887 def _loadvdis(self):
888 self.virtual_allocation = 0
889 self.vdiInfo = LvmCowUtil.getVDIInfo(self.lvmCache)
890 self.allVDIs = {}
892 for uuid, info in self.vdiInfo.items():
893 if uuid.startswith(cleanup.SR.TMP_RENAME_PREFIX): 893 ↛ 894line 893 didn't jump to line 894, because the condition on line 893 was never true
894 continue
895 if info.scanError: 895 ↛ 896line 895 didn't jump to line 896, because the condition on line 895 was never true
896 raise xs_errors.XenError('VDIUnavailable', \
897 opterr='Error scanning VDI %s' % uuid)
898 self.vdis[uuid] = self.allVDIs[uuid] = self.vdi(uuid)
899 if not self.vdis[uuid].hidden: 899 ↛ 892line 899 didn't jump to line 892, because the condition on line 899 was never false
900 self.virtual_allocation += self.vdis[uuid].utilisation
902 for uuid, vdi in self.vdis.items():
903 if vdi.parent: 903 ↛ 904line 903 didn't jump to line 904, because the condition on line 903 was never true
904 if vdi.parent in self.vdis:
905 self.vdis[vdi.parent].read_only = True
906 if vdi.parent in geneology:
907 geneology[vdi.parent].append(uuid)
908 else:
909 geneology[vdi.parent] = [uuid]
911 # Now remove all hidden leaf nodes to avoid introducing records that
912 # will be GC'ed
913 for uuid in list(self.vdis.keys()):
914 if uuid not in geneology and self.vdis[uuid].hidden: 914 ↛ 915line 914 didn't jump to line 915, because the condition on line 914 was never true
915 util.SMlog("Scan found hidden leaf (%s), ignoring" % uuid)
916 del self.vdis[uuid]
918 def _ensureSpaceAvailable(self, amount_needed):
919 space_available = lvutil._getVGstats(self.vgname)['freespace']
920 if (space_available < amount_needed): 920 ↛ 921line 920 didn't jump to line 921, because the condition on line 920 was never true
921 util.SMlog("Not enough space! free space: %d, need: %d" % \
922 (space_available, amount_needed))
923 raise xs_errors.XenError('SRNoSpace')
925 def _handleInterruptedCloneOps(self):
926 entries = self.journaler.getAll(LVMVDI.JRN_CLONE)
927 for uuid, val in entries.items(): 927 ↛ 928line 927 didn't jump to line 928, because the loop on line 927 never started
928 util.fistpoint.activate("LVHDRT_clone_vdi_before_undo_clone", self.uuid)
929 self._handleInterruptedCloneOp(uuid, val)
930 util.fistpoint.activate("LVHDRT_clone_vdi_after_undo_clone", self.uuid)
931 self.journaler.remove(LVMVDI.JRN_CLONE, uuid)
933 def _handleInterruptedCoalesceLeaf(self):
934 entries = self.journaler.getAll(cleanup.VDI.JRN_LEAF)
935 if len(entries) > 0: 935 ↛ 936line 935 didn't jump to line 936, because the condition on line 935 was never true
936 util.SMlog("*** INTERRUPTED COALESCE-LEAF OP DETECTED ***")
937 cleanup.gc_force(self.session, self.uuid)
938 self.lvmCache.refresh()
940 def _handleInterruptedCloneOp(self, origUuid, jval, forceUndo=False):
941 """Either roll back or finalize the interrupted snapshot/clone
942 operation. Rolling back is unsafe if the leaf images have already been
943 in use and written to. However, it is always safe to roll back while
944 we're still in the context of the failed snapshot operation since the
945 VBD is paused for the duration of the operation"""
946 util.SMlog("*** INTERRUPTED CLONE OP: for %s (%s)" % (origUuid, jval))
947 lvs = LvmCowUtil.getVolumeInfo(self.lvmCache)
948 baseUuid, clonUuid = jval.split("_")
950 # is there a "base copy" VDI?
951 if not lvs.get(baseUuid):
952 # no base copy: make sure the original is there
953 if lvs.get(origUuid):
954 util.SMlog("*** INTERRUPTED CLONE OP: nothing to do")
955 return
956 raise util.SMException("base copy %s not present, " \
957 "but no original %s found" % (baseUuid, origUuid))
959 vdis = LvmCowUtil.getVDIInfo(self.lvmCache)
960 base = vdis[baseUuid]
961 cowutil = getCowUtil(base.vdiType)
963 if forceUndo:
964 util.SMlog("Explicit revert")
965 self._undoCloneOp(cowutil, lvs, origUuid, baseUuid, clonUuid)
966 return
968 if not lvs.get(origUuid) or (clonUuid and not lvs.get(clonUuid)):
969 util.SMlog("One or both leaves missing => revert")
970 self._undoCloneOp(cowutil, lvs, origUuid, baseUuid, clonUuid)
971 return
973 if vdis[origUuid].scanError or (clonUuid and vdis[clonUuid].scanError):
974 util.SMlog("One or both leaves invalid => revert")
975 self._undoCloneOp(cowutil, lvs, origUuid, baseUuid, clonUuid)
976 return
978 orig = vdis[origUuid]
979 self.lvActivator.activate(baseUuid, base.lvName, False)
980 self.lvActivator.activate(origUuid, orig.lvName, False)
981 if orig.parentUuid != baseUuid:
982 parent = vdis[orig.parentUuid]
983 self.lvActivator.activate(parent.uuid, parent.lvName, False)
984 origPath = os.path.join(self.path, orig.lvName)
986 if cowutil.check(origPath) != CowUtil.CheckResult.Success:
987 util.SMlog("Orig image invalid => revert")
988 self._undoCloneOp(cowutil, lvs, origUuid, baseUuid, clonUuid)
989 return
991 if clonUuid:
992 clon = vdis[clonUuid]
993 clonPath = os.path.join(self.path, clon.lvName)
994 self.lvActivator.activate(clonUuid, clon.lvName, False)
995 if cowutil.check(clonPath) != CowUtil.CheckResult.Success:
996 util.SMlog("Clon image invalid => revert")
997 self._undoCloneOp(cowutil, lvs, origUuid, baseUuid, clonUuid)
998 return
1000 util.SMlog("Snapshot appears valid, will not roll back")
1001 self._completeCloneOp(cowutil, vdis, origUuid, baseUuid, clonUuid)
1003 def _undoCloneOp(self, cowutil, lvs, origUuid, baseUuid, clonUuid):
1004 base = lvs[baseUuid]
1005 basePath = os.path.join(self.path, base.name)
1007 # make the parent RW
1008 if base.readonly:
1009 self.lvmCache.setReadonly(base.name, False)
1011 ns = NS_PREFIX_LVM + self.uuid
1012 origRefcountBinary = RefCounter.check(origUuid, ns)[1]
1013 origRefcountNormal = 0
1015 # un-hide the parent
1016 if VdiType.isCowImage(base.vdiType):
1017 self.lvActivator.activate(baseUuid, base.name, False)
1018 origRefcountNormal = 1
1019 imageInfo = cowutil.getInfo(basePath, LvmCowUtil.extractUuid, False)
1020 if imageInfo.hidden:
1021 cowutil.setHidden(basePath, False)
1022 elif base.hidden:
1023 self.lvmCache.setHidden(base.name, False)
1025 # remove the child nodes
1026 if clonUuid and lvs.get(clonUuid):
1027 if not VdiType.isCowImage(lvs[clonUuid].vdiType):
1028 raise util.SMException("clone %s not a COW image" % clonUuid)
1029 self.lvmCache.remove(lvs[clonUuid].name)
1030 if self.lvActivator.get(clonUuid, False):
1031 self.lvActivator.remove(clonUuid, False)
1032 if lvs.get(origUuid):
1033 self.lvmCache.remove(lvs[origUuid].name)
1035 # inflate the parent to fully-allocated size
1036 if VdiType.isCowImage(base.vdiType):
1037 lvmcowutil = LvmCowUtil(cowutil)
1038 fullSize = lvmcowutil.calcVolumeSize(imageInfo.sizeVirt)
1039 lvmcowutil.inflate(self.journaler, self.uuid, baseUuid, base.vdiType, fullSize)
1041 # rename back
1042 origLV = LV_PREFIX[base.vdiType] + origUuid
1043 self.lvmCache.rename(base.name, origLV)
1044 RefCounter.reset(baseUuid, ns)
1045 if self.lvActivator.get(baseUuid, False):
1046 self.lvActivator.replace(baseUuid, origUuid, origLV, False)
1047 RefCounter.set(origUuid, origRefcountNormal, origRefcountBinary, ns)
1049 # At this stage, tapdisk and SM vdi will be in paused state. Remove
1050 # flag to facilitate vm deactivate
1051 origVdiRef = self.session.xenapi.VDI.get_by_uuid(origUuid)
1052 self.session.xenapi.VDI.remove_from_sm_config(origVdiRef, 'paused')
1054 # update LVM metadata on slaves
1055 slaves = util.get_slaves_attached_on(self.session, [origUuid])
1056 LvmCowUtil.refreshVolumeOnSlaves(self.session, self.uuid, self.vgname,
1057 origLV, origUuid, slaves)
1059 util.SMlog("*** INTERRUPTED CLONE OP: rollback success")
1061 def _completeCloneOp(self, cowutil, vdis, origUuid, baseUuid, clonUuid):
1062 """Finalize the interrupted snapshot/clone operation. This must not be
1063 called from the live snapshot op context because we attempt to pause/
1064 unpause the VBD here (the VBD is already paused during snapshot, so it
1065 would cause a deadlock)"""
1066 base = vdis[baseUuid]
1067 clon = None
1068 if clonUuid:
1069 clon = vdis[clonUuid]
1071 cleanup.abort(self.uuid)
1073 # make sure the parent is hidden and read-only
1074 if not base.hidden:
1075 if not VdiType.isCowImage(base.vdiType):
1076 self.lvmCache.setHidden(base.lvName)
1077 else:
1078 basePath = os.path.join(self.path, base.lvName)
1079 cowutil.setHidden(basePath)
1080 if not base.lvReadonly:
1081 self.lvmCache.setReadonly(base.lvName, True)
1083 # NB: since this snapshot-preserving call is only invoked outside the
1084 # snapshot op context, we assume the LVM metadata on the involved slave
1085 # has by now been refreshed and do not attempt to do it here
1087 # Update the original record
1088 try:
1089 vdi_ref = self.session.xenapi.VDI.get_by_uuid(origUuid)
1090 sm_config = self.session.xenapi.VDI.get_sm_config(vdi_ref)
1091 type = self.session.xenapi.VDI.get_type(vdi_ref)
1092 sm_config["vdi_type"] = vdis[origUuid].vdiType
1093 sm_config['vhd-parent'] = baseUuid
1094 self.session.xenapi.VDI.set_sm_config(vdi_ref, sm_config)
1095 except XenAPI.Failure:
1096 util.SMlog("ERROR updating the orig record")
1098 # introduce the new VDI records
1099 if clonUuid:
1100 try:
1101 clon_vdi = VDI.VDI(self, clonUuid)
1102 clon_vdi.read_only = False
1103 clon_vdi.location = clonUuid
1104 clon_vdi.utilisation = clon.sizeLV
1105 clon_vdi.sm_config = {
1106 "vdi_type": clon.vdiType,
1107 "vhd-parent": baseUuid}
1109 if not self.legacyMode:
1110 LVMMetadataHandler(self.mdpath). \
1111 ensureSpaceIsAvailableForVdis(1)
1113 clon_vdi_ref = clon_vdi._db_introduce()
1114 util.SMlog("introduced clon VDI: %s (%s)" % \
1115 (clon_vdi_ref, clonUuid))
1117 vdi_info = {UUID_TAG: clonUuid,
1118 NAME_LABEL_TAG: clon_vdi.label,
1119 NAME_DESCRIPTION_TAG: clon_vdi.description,
1120 IS_A_SNAPSHOT_TAG: 0,
1121 SNAPSHOT_OF_TAG: '',
1122 SNAPSHOT_TIME_TAG: '',
1123 TYPE_TAG: type,
1124 VDI_TYPE_TAG: clon_vdi.sm_config['vdi_type'],
1125 READ_ONLY_TAG: int(clon_vdi.read_only),
1126 MANAGED_TAG: int(clon_vdi.managed),
1127 METADATA_OF_POOL_TAG: ''
1128 }
1130 if not self.legacyMode:
1131 LVMMetadataHandler(self.mdpath).addVdi(vdi_info)
1133 except XenAPI.Failure:
1134 util.SMlog("ERROR introducing the clon record")
1136 try:
1137 base_vdi = VDI.VDI(self, baseUuid) # readonly parent
1138 base_vdi.label = "base copy"
1139 base_vdi.read_only = True
1140 base_vdi.location = baseUuid
1141 base_vdi.size = base.sizeVirt
1142 base_vdi.utilisation = base.sizeLV
1143 base_vdi.managed = False
1144 base_vdi.sm_config = {
1145 "vdi_type": base.vdiType,
1146 "vhd-parent": baseUuid}
1148 if not self.legacyMode:
1149 LVMMetadataHandler(self.mdpath).ensureSpaceIsAvailableForVdis(1)
1151 base_vdi_ref = base_vdi._db_introduce()
1152 util.SMlog("introduced base VDI: %s (%s)" % \
1153 (base_vdi_ref, baseUuid))
1155 vdi_info = {UUID_TAG: baseUuid,
1156 NAME_LABEL_TAG: base_vdi.label,
1157 NAME_DESCRIPTION_TAG: base_vdi.description,
1158 IS_A_SNAPSHOT_TAG: 0,
1159 SNAPSHOT_OF_TAG: '',
1160 SNAPSHOT_TIME_TAG: '',
1161 TYPE_TAG: type,
1162 VDI_TYPE_TAG: base_vdi.sm_config['vdi_type'],
1163 READ_ONLY_TAG: int(base_vdi.read_only),
1164 MANAGED_TAG: int(base_vdi.managed),
1165 METADATA_OF_POOL_TAG: ''
1166 }
1168 if not self.legacyMode:
1169 LVMMetadataHandler(self.mdpath).addVdi(vdi_info)
1170 except XenAPI.Failure:
1171 util.SMlog("ERROR introducing the base record")
1173 util.SMlog("*** INTERRUPTED CLONE OP: complete")
1175 def _undoAllJournals(self):
1176 """Undo all COW image & SM interrupted journaled operations. This call must
1177 be serialized with respect to all operations that create journals"""
1178 # undoing interrupted inflates must be done first, since undoing COW images
1179 # ops might require inflations
1180 self.lock.acquire()
1181 try:
1182 self._undoAllInflateJournals()
1183 self._undoAllCowJournals()
1184 self._handleInterruptedCloneOps()
1185 self._handleInterruptedCoalesceLeaf()
1186 finally:
1187 self.lock.release()
1188 self.cleanup()
1190 def _undoAllInflateJournals(self):
1191 entries = self.journaler.getAll(LvmCowUtil.JOURNAL_INFLATE)
1192 if len(entries) == 0:
1193 return
1194 self._loadvdis()
1195 for uuid, val in entries.items():
1196 vdi = self.vdis.get(uuid)
1197 if vdi: 1197 ↛ 1217line 1197 didn't jump to line 1217, because the condition on line 1197 was never false
1198 util.SMlog("Found inflate journal %s, deflating %s to %s" % \
1199 (uuid, vdi.path, val))
1200 if vdi.readonly: 1200 ↛ 1201line 1200 didn't jump to line 1201, because the condition on line 1200 was never true
1201 self.lvmCache.setReadonly(vdi.lvname, False)
1202 self.lvActivator.activate(uuid, vdi.lvname, False)
1203 currSizeLV = self.lvmCache.getSize(vdi.lvname)
1205 cowutil = getCowUtil(vdi.vdi_type)
1206 lvmcowutil = LvmCowUtil(cowutil)
1208 footer_size = cowutil.getFooterSize()
1209 util.zeroOut(vdi.path, currSizeLV - footer_size, footer_size)
1210 lvmcowutil.deflate(self.lvmCache, vdi.lvname, int(val))
1211 if vdi.readonly: 1211 ↛ 1212line 1211 didn't jump to line 1212, because the condition on line 1211 was never true
1212 self.lvmCache.setReadonly(vdi.lvname, True)
1213 if "true" == self.session.xenapi.SR.get_shared(self.sr_ref): 1213 ↛ 1214line 1213 didn't jump to line 1214, because the condition on line 1213 was never true
1214 LvmCowUtil.refreshVolumeOnAllSlaves(
1215 self.session, self.uuid, self.vgname, vdi.lvname, uuid
1216 )
1217 self.journaler.remove(LvmCowUtil.JOURNAL_INFLATE, uuid)
1218 delattr(self, "vdiInfo")
1219 delattr(self, "allVDIs")
1221 def _undoAllCowJournals(self):
1222 """
1223 Check if there are COW journals in existence and revert them.
1224 """
1225 journals = LvmCowUtil.getAllResizeJournals(self.lvmCache)
1226 if len(journals) == 0: 1226 ↛ 1228line 1226 didn't jump to line 1228, because the condition on line 1226 was never false
1227 return
1228 self._loadvdis()
1230 for uuid, jlvName in journals:
1231 vdi = self.vdis[uuid]
1232 util.SMlog("Found COW journal %s, reverting %s" % (uuid, vdi.path))
1233 cowutil = getCowUtil(vdi.vdi_type)
1234 lvmcowutil = LvmCowUtil(cowutil)
1236 self.lvActivator.activate(uuid, vdi.lvname, False)
1237 self.lvmCache.activateNoRefcount(jlvName)
1238 fullSize = lvmcowutil.calcVolumeSize(vdi.size)
1239 lvmcowutil.inflate(self.journaler, self.uuid, vdi.uuid, vdi.vdi_type, fullSize)
1240 try:
1241 jFile = os.path.join(self.path, jlvName)
1242 cowutil.revert(vdi.path, jFile)
1243 except util.CommandException:
1244 util.logException("COW journal revert")
1245 cowutil.check(vdi.path)
1246 util.SMlog("COW image revert failed but COW image ok: removing journal")
1247 # Attempt to reclaim unused space
1250 imageInfo = cowutil.getInfo(vdi.path, LvmCowUtil.extractUuid, False)
1251 NewSize = lvmcowutil.calcVolumeSize(imageInfo.sizeVirt)
1252 if NewSize < fullSize:
1253 lvmcowutil.deflate(self.lvmCache, vdi.lvname, int(NewSize))
1254 LvmCowUtil.refreshVolumeOnAllSlaves(self.session, self.uuid, self.vgname, vdi.lvname, uuid)
1255 self.lvmCache.remove(jlvName)
1256 delattr(self, "vdiInfo")
1257 delattr(self, "allVDIs")
1259 def call_on_slave(self, args, host_refs, message: str):
1260 master_ref = util.get_this_host_ref(self.session)
1261 for hostRef in host_refs:
1262 if hostRef == master_ref: 1262 ↛ 1263line 1262 didn't jump to line 1263, because the condition on line 1262 was never true
1263 continue
1264 util.SMlog(f"{message} on slave {hostRef}")
1265 rv = self.session.xenapi.host.call_plugin(
1266 hostRef, self.PLUGIN_ON_SLAVE, "multi", args)
1267 util.SMlog("call-plugin returned: %s" % rv)
1268 if not rv: 1268 ↛ 1269line 1268 didn't jump to line 1269, because the condition on line 1268 was never true
1269 raise Exception('plugin %s failed' % self.PLUGIN_ON_SLAVE)
1271 def _updateSlavesOnClone(self, hostRefs, origOldLV, origLV,
1272 baseUuid, baseLV):
1273 """We need to reactivate the original LV on each slave (note that the
1274 name for the original LV might change), as well as init the refcount
1275 for the base LV"""
1276 args = {"vgName": self.vgname,
1277 "action1": "refresh",
1278 "lvName1": origLV,
1279 "action2": "activate",
1280 "ns2": NS_PREFIX_LVM + self.uuid,
1281 "lvName2": baseLV,
1282 "uuid2": baseUuid}
1284 message = f"Updating {origOldLV}, {origLV}, {baseLV}"
1285 self.call_on_slave(args, hostRefs, message)
1287 def _updateSlavesOnCBTClone(self, hostRefs, cbtlog):
1288 """Reactivate and refresh CBT log file on slaves"""
1289 args = {"vgName": self.vgname,
1290 "action1": "deactivateNoRefcount",
1291 "lvName1": cbtlog,
1292 "action2": "refresh",
1293 "lvName2": cbtlog}
1295 message = f"Updating {cbtlog}"
1296 self.call_on_slave(args, hostRefs, message)
1298 def _updateSlavesOnRemove(self, hostRefs, baseUuid, baseLV):
1299 """Tell the slave we deleted the base image"""
1300 args = {"vgName": self.vgname,
1301 "action1": "cleanupLockAndRefcount",
1302 "uuid1": baseUuid,
1303 "ns1": NS_PREFIX_LVM + self.uuid}
1305 message = f"Cleaning locks for {baseLV}"
1306 self.call_on_slave(args, hostRefs, message)
1308 def _deactivateOnSlave(self, hostRefs, lvname):
1309 """Tell the slave we need to deactivate the base image"""
1310 args = {
1311 "vgName": self.vgname,
1312 "action1": "deactivateNoRefcount",
1313 "lvName1": lvname}
1315 message = f"Deactivating {lvname}"
1316 self.call_on_slave(args, hostRefs, message)
1318 def _cleanup(self, skipLockCleanup=False):
1319 """delete stale refcounter, flag, and lock files"""
1320 RefCounter.resetAll(NS_PREFIX_LVM + self.uuid)
1321 IPCFlag(self.uuid).clearAll()
1322 if not skipLockCleanup: 1322 ↛ 1323line 1322 didn't jump to line 1323, because the condition on line 1322 was never true
1323 lock.Lock.cleanupAll(self.uuid)
1324 lock.Lock.cleanupAll(NS_PREFIX_LVM + self.uuid)
1326 def _prepareTestMode(self):
1327 util.SMlog("Test mode: %s" % self.testMode)
1328 if self.ENV_VAR_VHD_TEST.get(self.testMode): 1328 ↛ 1329line 1328 didn't jump to line 1329, because the condition on line 1328 was never true
1329 os.environ[self.ENV_VAR_VHD_TEST[self.testMode]] = "yes"
1330 util.SMlog("Setting env %s" % self.ENV_VAR_VHD_TEST[self.testMode])
1332 def _kickGC(self):
1333 util.SMlog("Kicking GC")
1334 cleanup.start_gc_service(self.uuid)
1336 def ensureCBTSpace(self, virtual_size=0):
1337 # Ensure we have space for at least one LV
1338 size = max(util.roundup(CBT_BLOCK_SIZE, virtual_size//CBT_BLOCK_SIZE), self.journaler.LV_SIZE)
1339 self._ensureSpaceAvailable(size)
1342class LVMVDI(VDI.VDI):
1344 JRN_CLONE = "clone" # journal entry type for the clone operation
1346 @override
1347 def load(self, vdi_uuid) -> None:
1348 self.lock = self.sr.lock
1349 self.lvActivator = self.sr.lvActivator
1350 self.loaded = False
1351 if self.sr.legacyMode or util.fistpoint.is_active("xenrt_default_vdi_type_legacy"):
1352 self._setType(VdiType.RAW)
1353 self.uuid = vdi_uuid
1354 self.location = self.uuid
1355 self.exists = True
1357 if hasattr(self.sr, "vdiInfo") and self.sr.vdiInfo.get(self.uuid):
1358 self._initFromVDIInfo(self.sr.vdiInfo[self.uuid])
1359 if self.parent: 1359 ↛ 1360line 1359 didn't jump to line 1360, because the condition on line 1359 was never true
1360 self.sm_config_override['vhd-parent'] = self.parent
1361 else:
1362 self.sm_config_override['vhd-parent'] = None
1363 return
1365 # scan() didn't run: determine the type of the VDI manually
1366 if self._determineType(): 1366 ↛ 1369line 1366 didn't jump to line 1369, because the condition on line 1366 was never false
1367 return
1369 image_format = None
1371 # the VDI must be in the process of being created
1372 self.exists = False
1374 vdi_sm_config = self.sr.srcmd.params.get("vdi_sm_config")
1375 if vdi_sm_config:
1376 image_format = self.sr.read_config_image_format(vdi_sm_config)
1378 if not image_format:
1379 if self.sr.srcmd.cmd == "vdi_create":
1380 size = int(self.sr.srcmd.params['args'][0])
1381 # In the case of vdi_create, the first parameter is size.
1382 # We need it to validate the vdi_type choice
1383 for image_format in self.sr.preferred_image_formats:
1384 vdi_type = self.sr._resolve_vdi_type_from_image_format(image_format)
1385 cowutil = getCowUtil(vdi_type)
1386 try:
1387 cowutil.validateAndRoundImageSize(size)
1388 break
1389 except xs_errors.SROSError:
1390 util.SMlog(f"We won't be able to create the VDI with format {vdi_type}.")
1391 else:
1392 # For cbt_metadata, we found ourselves here without being a vdi_create, we don't have size
1393 util.SMlog("Not a vdi_create, must be cbtlog")
1394 image_format = self.sr.preferred_image_formats[0]
1395 self._setType(self.sr._resolve_vdi_type_from_image_format(image_format))
1397 if self.sr.legacyMode and self.sr.cmd == 'vdi_create' and VdiType.isCowImage(self.vdi_type):
1398 raise xs_errors.XenError('VDICreate', opterr='Cannot create COW type disk in legacy mode')
1400 self.lvname = "%s%s" % (LV_PREFIX[self.vdi_type], vdi_uuid)
1401 self.path = os.path.join(self.sr.path, self.lvname)
1403 @override
1404 def create(self, sr_uuid, vdi_uuid, size) -> str:
1405 util.SMlog("LVMVDI.create for %s" % self.uuid)
1406 if not self.sr.isMaster:
1407 raise xs_errors.XenError('LVMMaster')
1408 if self.exists:
1409 raise xs_errors.XenError('VDIExists')
1411 size = self.cowutil.validateAndRoundImageSize(int(size))
1413 util.SMlog("LVMVDI.create: type = %s, %s (size=%s)" % \
1414 (self.vdi_type, self.path, size))
1415 lvSize = 0
1416 self.sm_config = self.sr.srcmd.params["vdi_sm_config"]
1417 if not VdiType.isCowImage(self.vdi_type):
1418 lvSize = util.roundup(lvutil.LVM_SIZE_INCREMENT, int(size))
1419 else:
1420 if self.sr.provision == "thin":
1421 lvSize = util.roundup(
1422 lvutil.LVM_SIZE_INCREMENT,
1423 self.cowutil.calcOverheadEmpty(max(size, self.cowutil.getDefaultPreallocationSizeVirt()))
1424 )
1425 elif self.sr.provision == "thick":
1426 lvSize = self.lvmcowutil.calcVolumeSize(int(size))
1428 self.sr._ensureSpaceAvailable(lvSize)
1430 try:
1431 self.sr.lvmCache.create(self.lvname, lvSize)
1432 if not VdiType.isCowImage(self.vdi_type):
1433 self.size = self.sr.lvmCache.getSize(self.lvname)
1434 else:
1435 self.cowutil.create(
1436 self.path, int(size), False, self.cowutil.getDefaultPreallocationSizeVirt()
1437 )
1438 self.size = self.cowutil.getSizeVirt(self.path)
1439 self.sr.lvmCache.deactivateNoRefcount(self.lvname)
1440 except util.CommandException as e:
1441 util.SMlog("Unable to create VDI")
1442 self.sr.lvmCache.remove(self.lvname)
1443 raise xs_errors.XenError('VDICreate', opterr='error %d' % e.code)
1445 self.utilisation = lvSize
1446 self.sm_config["vdi_type"] = self.vdi_type
1447 self.sm_config["image-format"] = getImageStringFromVdiType(self.vdi_type)
1449 if not self.sr.legacyMode:
1450 LVMMetadataHandler(self.sr.mdpath).ensureSpaceIsAvailableForVdis(1)
1452 self.ref = self._db_introduce()
1453 self.sr._updateStats(self.sr.uuid, self.size)
1455 vdi_info = {UUID_TAG: self.uuid,
1456 NAME_LABEL_TAG: util.to_plain_string(self.label),
1457 NAME_DESCRIPTION_TAG: util.to_plain_string(self.description),
1458 IS_A_SNAPSHOT_TAG: 0,
1459 SNAPSHOT_OF_TAG: '',
1460 SNAPSHOT_TIME_TAG: '',
1461 TYPE_TAG: self.ty,
1462 VDI_TYPE_TAG: self.vdi_type,
1463 READ_ONLY_TAG: int(self.read_only),
1464 MANAGED_TAG: int(self.managed),
1465 METADATA_OF_POOL_TAG: ''
1466 }
1468 if not self.sr.legacyMode:
1469 LVMMetadataHandler(self.sr.mdpath).addVdi(vdi_info)
1471 return VDI.VDI.get_params(self)
1473 @override
1474 def delete(self, sr_uuid, vdi_uuid, data_only=False) -> None:
1475 util.SMlog("LVMVDI.delete for %s" % self.uuid)
1476 try:
1477 self._loadThis()
1478 except xs_errors.SRException as e:
1479 # Catch 'VDI doesn't exist' exception
1480 if e.errno == 46:
1481 return super(LVMVDI, self).delete(sr_uuid, vdi_uuid, data_only)
1482 raise
1484 vdi_ref = self.sr.srcmd.params['vdi_ref']
1485 if not self.session.xenapi.VDI.get_managed(vdi_ref):
1486 raise xs_errors.XenError("VDIDelete", \
1487 opterr="Deleting non-leaf node not permitted")
1489 if not self.hidden:
1490 self._markHidden()
1492 if not data_only:
1493 # Remove from XAPI and delete from MGT
1494 self._db_forget()
1495 else:
1496 # If this is a data_destroy call, don't remove from XAPI db
1497 # Only delete from MGT
1498 if not self.sr.legacyMode:
1499 LVMMetadataHandler(self.sr.mdpath).deleteVdiFromMetadata(self.uuid)
1501 # deactivate here because it might be too late to do it in the "final"
1502 # step: GC might have removed the LV by then
1503 if self.sr.lvActivator.get(self.uuid, False):
1504 self.sr.lvActivator.deactivate(self.uuid, False)
1506 try:
1507 self.sr.lvmCache.remove(self.lvname)
1508 self.sr.lock.cleanup(vdi_uuid, NS_PREFIX_LVM + sr_uuid)
1509 self.sr.lock.cleanupAll(vdi_uuid)
1510 except xs_errors.SRException as e:
1511 util.SMlog(
1512 "Failed to remove the volume (maybe is leaf coalescing) "
1513 "for %s err:%d" % (self.uuid, e.errno))
1515 self.sr._updateStats(self.sr.uuid, -self.size)
1516 self.sr._kickGC()
1517 return super(LVMVDI, self).delete(sr_uuid, vdi_uuid, data_only)
1519 @override
1520 def attach(self, sr_uuid, vdi_uuid) -> str:
1521 util.SMlog("LVMVDI.attach for %s" % self.uuid)
1522 if self.sr.journaler.hasJournals(self.uuid):
1523 raise xs_errors.XenError('VDIUnavailable',
1524 opterr='Interrupted operation detected on this VDI, '
1525 'scan SR first to trigger auto-repair')
1527 writable = ('args' not in self.sr.srcmd.params) or \
1528 (self.sr.srcmd.params['args'][0] == "true")
1529 needInflate = True
1530 if not VdiType.isCowImage(self.vdi_type) or not writable:
1531 needInflate = False
1532 else:
1533 self._loadThis()
1534 if self.utilisation >= self.lvmcowutil.calcVolumeSize(self.size):
1535 needInflate = False
1537 if needInflate:
1538 try:
1539 self._prepareThin(True, self.vdi_type)
1540 except:
1541 util.logException("attach")
1542 raise xs_errors.XenError('LVMProvisionAttach')
1544 if self.cowutil.isCoalesceableOnRemote():
1545 # We make the chain RW before it's used so it can be coalesced online
1546 self._make_chain_rw()
1548 try:
1549 return self._attach()
1550 finally:
1551 if not self.sr.lvActivator.deactivateAll():
1552 util.SMlog("Failed to deactivate LVs back (%s)" % self.uuid)
1554 @override
1555 def detach(self, sr_uuid, vdi_uuid) -> None:
1556 util.SMlog("LVMVDI.detach for %s" % self.uuid)
1557 self._loadThis()
1558 already_deflated = (self.utilisation < \
1559 self.lvmcowutil.calcVolumeSize(self.size))
1560 needDeflate = True
1561 if not VdiType.isCowImage(self.vdi_type) or already_deflated:
1562 needDeflate = False
1563 elif self.sr.provision == "thick":
1564 needDeflate = False
1565 # except for snapshots, which are always deflated
1566 if self.sr.srcmd.cmd != 'vdi_detach_from_config':
1567 vdi_ref = self.sr.srcmd.params['vdi_ref']
1568 snap = self.session.xenapi.VDI.get_is_a_snapshot(vdi_ref)
1569 if snap:
1570 needDeflate = True
1572 if needDeflate:
1573 try:
1574 self._prepareThin(False, self.vdi_type)
1575 except:
1576 util.logException("_prepareThin")
1577 raise xs_errors.XenError('VDIUnavailable', opterr='deflate')
1579 try:
1580 self._detach()
1581 finally:
1582 if not self.sr.lvActivator.deactivateAll():
1583 raise xs_errors.XenError("SMGeneral", opterr="deactivation")
1585 # We only support offline resize
1586 @override
1587 def resize(self, sr_uuid, vdi_uuid, size) -> str:
1588 util.SMlog("LVMVDI.resize for %s" % self.uuid)
1589 if not self.sr.isMaster:
1590 raise xs_errors.XenError('LVMMaster')
1592 self._loadThis()
1593 if self.hidden:
1594 raise xs_errors.XenError('VDIUnavailable', opterr='hidden VDI')
1596 if size < self.size:
1597 util.SMlog('vdi_resize: shrinking not supported: ' + \
1598 '(current size: %d, new size: %d)' % (self.size, size))
1599 raise xs_errors.XenError('VDISize', opterr='shrinking not allowed')
1601 size = self.cowutil.validateAndRoundImageSize(int(size))
1603 if size == self.size:
1604 return VDI.VDI.get_params(self)
1606 if not VdiType.isCowImage(self.vdi_type):
1607 lvSizeOld = self.size
1608 lvSizeNew = util.roundup(lvutil.LVM_SIZE_INCREMENT, size)
1609 else:
1610 lvSizeOld = self.utilisation
1611 lvSizeNew = self.lvmcowutil.calcVolumeSize(size)
1612 if self.sr.provision == "thin":
1613 # VDI is currently deflated, so keep it deflated
1614 lvSizeNew = lvSizeOld
1615 assert(lvSizeNew >= lvSizeOld)
1616 spaceNeeded = lvSizeNew - lvSizeOld
1617 self.sr._ensureSpaceAvailable(spaceNeeded)
1619 oldSize = self.size
1620 if not VdiType.isCowImage(self.vdi_type):
1621 self.sr.lvmCache.setSize(self.lvname, lvSizeNew)
1622 self.size = self.sr.lvmCache.getSize(self.lvname)
1623 self.utilisation = self.size
1624 else:
1625 if lvSizeNew != lvSizeOld:
1626 self.lvmcowutil.inflate(self.sr.journaler, self.sr.uuid, self.uuid, self.vdi_type, lvSizeNew)
1627 if self.vdi_type == VdiType.QCOW2:
1628 # We only do this for QCOW2 since qemu-img need to read the chain to resize
1629 self._chainSetActive(True, True)
1630 self.cowutil.setSizeVirtFast(self.path, size)
1631 self.size = self.cowutil.getSizeVirt(self.path)
1632 self.utilisation = self.sr.lvmCache.getSize(self.lvname)
1634 vdi_ref = self.sr.srcmd.params['vdi_ref']
1635 self.session.xenapi.VDI.set_virtual_size(vdi_ref, str(self.size))
1636 self.session.xenapi.VDI.set_physical_utilisation(vdi_ref,
1637 str(self.utilisation))
1638 self.sr._updateStats(self.sr.uuid, self.size - oldSize)
1639 super(LVMVDI, self).resize_cbt(self.sr.uuid, self.uuid, self.size)
1640 if self.vdi_type == VdiType.QCOW2:
1641 self._chainSetActive(False, True)
1642 return VDI.VDI.get_params(self)
1644 @override
1645 def clone(self, sr_uuid, vdi_uuid) -> str:
1646 return self._do_snapshot(
1647 sr_uuid, vdi_uuid, VDI.SNAPSHOT_DOUBLE, cloneOp=True)
1649 @override
1650 def compose(self, sr_uuid, vdi1, vdi2) -> None:
1651 util.SMlog("LVMSR.compose for %s -> %s" % (vdi2, vdi1))
1652 if not VdiType.isCowImage(self.vdi_type):
1653 raise xs_errors.XenError('Unimplemented')
1655 parent_uuid = vdi1
1656 parent_lvname = LV_PREFIX[self.vdi_type] + parent_uuid
1657 assert(self.sr.lvmCache.checkLV(parent_lvname))
1658 parent_path = os.path.join(self.sr.path, parent_lvname)
1660 self.sr.lvActivator.activate(self.uuid, self.lvname, False)
1661 self.sr.lvActivator.activate(parent_uuid, parent_lvname, False)
1663 self.cowutil.setParent(self.path, parent_path, False)
1664 self.cowutil.setHidden(parent_path)
1665 self.sr.session.xenapi.VDI.set_managed(self.sr.srcmd.params['args'][0], False)
1667 if not blktap2.VDI.tap_refresh(self.session, self.sr.uuid, self.uuid,
1668 True):
1669 raise util.SMException("failed to refresh VDI %s" % self.uuid)
1671 util.SMlog("Compose done")
1673 def reset_leaf(self, sr_uuid, vdi_uuid):
1674 util.SMlog("LVMSR.reset_leaf for %s" % vdi_uuid)
1675 if not VdiType.isCowImage(self.vdi_type):
1676 raise xs_errors.XenError('Unimplemented')
1678 self.sr.lvActivator.activate(self.uuid, self.lvname, False)
1680 # safety check
1681 if not self.cowutil.hasParent(self.path):
1682 raise util.SMException("ERROR: VDI %s has no parent, " + \
1683 "will not reset contents" % self.uuid)
1685 self.cowutil.killData(self.path)
1687 def _attach(self):
1688 self._chainSetActive(True, True, True)
1689 if not util.pathexists(self.path):
1690 raise xs_errors.XenError('VDIUnavailable', \
1691 opterr='Could not find: %s' % self.path)
1693 if not hasattr(self, 'xenstore_data'):
1694 self.xenstore_data = {}
1696 self.xenstore_data.update(scsiutil.update_XS_SCSIdata(self.uuid, \
1697 scsiutil.gen_synthetic_page_data(self.uuid)))
1699 self.xenstore_data['storage-type'] = 'lvm'
1700 self.xenstore_data['vdi-type'] = self.vdi_type
1702 self.attached = True
1703 self.sr.lvActivator.persist()
1704 return VDI.VDI.attach(self, self.sr.uuid, self.uuid)
1706 def _detach(self):
1707 self._chainSetActive(False, True)
1708 self.attached = False
1710 @override
1711 def _do_snapshot(self, sr_uuid, vdi_uuid, snapType,
1712 cloneOp=False, secondary=None, cbtlog=None, is_mirror_destination=False) -> str:
1713 # If cbt enabled, save file consistency state
1714 if cbtlog is not None:
1715 if blktap2.VDI.tap_status(self.session, vdi_uuid): 1715 ↛ 1716line 1715 didn't jump to line 1716, because the condition on line 1715 was never true
1716 consistency_state = False
1717 else:
1718 consistency_state = True
1719 util.SMlog("Saving log consistency state of %s for vdi: %s" %
1720 (consistency_state, vdi_uuid))
1721 else:
1722 consistency_state = None
1724 pause_time = time.time()
1725 if not blktap2.VDI.tap_pause(self.session, sr_uuid, vdi_uuid): 1725 ↛ 1726line 1725 didn't jump to line 1726, because the condition on line 1725 was never true
1726 raise util.SMException("failed to pause VDI %s" % vdi_uuid)
1728 snapResult = None
1729 try:
1730 snapResult = self._snapshot(snapType, cloneOp, cbtlog, consistency_state, is_mirror_destination)
1731 except Exception as e1:
1732 try:
1733 blktap2.VDI.tap_unpause(self.session, sr_uuid, vdi_uuid,
1734 secondary=None)
1735 except Exception as e2:
1736 util.SMlog('WARNING: failed to clean up failed snapshot: '
1737 '%s (error ignored)' % e2)
1738 raise
1739 self.disable_leaf_on_secondary(vdi_uuid, secondary=secondary)
1740 blktap2.VDI.tap_unpause(self.session, sr_uuid, vdi_uuid, secondary)
1741 unpause_time = time.time()
1742 if (unpause_time - pause_time) > LONG_SNAPTIME: 1742 ↛ 1743line 1742 didn't jump to line 1743, because the condition on line 1742 was never true
1743 util.SMlog('WARNING: snapshot paused VM for %s seconds' %
1744 (unpause_time - pause_time))
1745 return snapResult
1747 def _snapshot(self, snapType, cloneOp=False, cbtlog=None, cbt_consistency=None, is_mirror_destination=False):
1748 util.SMlog("LVMVDI._snapshot for %s (type %s)" % (self.uuid, snapType))
1750 if not self.sr.isMaster: 1750 ↛ 1751line 1750 didn't jump to line 1751, because the condition on line 1750 was never true
1751 raise xs_errors.XenError('LVMMaster')
1752 if self.sr.legacyMode: 1752 ↛ 1753line 1752 didn't jump to line 1753, because the condition on line 1752 was never true
1753 raise xs_errors.XenError('Unimplemented', opterr='In legacy mode')
1755 self._loadThis()
1756 if self.hidden: 1756 ↛ 1757line 1756 didn't jump to line 1757, because the condition on line 1756 was never true
1757 raise xs_errors.XenError('VDISnapshot', opterr='hidden VDI')
1759 snapVdiType = self.sr._get_snap_vdi_type(self.vdi_type, self.size)
1761 self.sm_config = self.session.xenapi.VDI.get_sm_config( \
1762 self.sr.srcmd.params['vdi_ref'])
1763 if "type" in self.sm_config and self.sm_config['type'] == 'raw': 1763 ↛ 1764line 1763 didn't jump to line 1764, because the condition on line 1763 was never true
1764 if not util.fistpoint.is_active("testsm_clone_allow_raw"):
1765 raise xs_errors.XenError('Unimplemented', \
1766 opterr='Raw VDI, snapshot or clone not permitted')
1768 # we must activate the entire image chain because the real parent could
1769 # theoretically be anywhere in the chain if all images under it are empty
1770 self._chainSetActive(True, False)
1771 if not util.pathexists(self.path): 1771 ↛ 1772line 1771 didn't jump to line 1772, because the condition on line 1771 was never true
1772 raise xs_errors.XenError('VDIUnavailable', \
1773 opterr='VDI unavailable: %s' % (self.path))
1775 if VdiType.isCowImage(self.vdi_type): 1775 ↛ 1783line 1775 didn't jump to line 1783, because the condition on line 1775 was never false
1776 depth = self.cowutil.getDepth(self.path)
1777 if depth == -1: 1777 ↛ 1778line 1777 didn't jump to line 1778, because the condition on line 1777 was never true
1778 raise xs_errors.XenError('VDIUnavailable', \
1779 opterr='failed to get COW depth')
1780 elif depth >= self.cowutil.getMaxChainLength(): 1780 ↛ 1781line 1780 didn't jump to line 1781, because the condition on line 1780 was never true
1781 raise xs_errors.XenError('SnapshotChainTooLong')
1783 self.issnap = self.session.xenapi.VDI.get_is_a_snapshot( \
1784 self.sr.srcmd.params['vdi_ref'])
1786 fullpr = self.lvmcowutil.calcVolumeSize(self.size)
1787 thinpr = util.roundup(
1788 lvutil.LVM_SIZE_INCREMENT,
1789 self.cowutil.calcOverheadEmpty(max(self.size, self.cowutil.getDefaultPreallocationSizeVirt()))
1790 )
1791 lvSizeOrig = thinpr
1792 lvSizeClon = thinpr
1794 hostRefs = []
1795 if self.sr.cmd == "vdi_snapshot":
1796 hostRefs = util.get_hosts_attached_on(self.session, [self.uuid])
1797 if hostRefs: 1797 ↛ 1799line 1797 didn't jump to line 1799, because the condition on line 1797 was never false
1798 lvSizeOrig = fullpr
1799 if self.sr.provision == "thick": 1799 ↛ 1805line 1799 didn't jump to line 1805, because the condition on line 1799 was never false
1800 if not self.issnap:
1801 lvSizeOrig = fullpr
1802 if self.sr.cmd != "vdi_snapshot":
1803 lvSizeClon = fullpr
1805 if (snapType == VDI.SNAPSHOT_SINGLE or 1805 ↛ 1807line 1805 didn't jump to line 1807, because the condition on line 1805 was never true
1806 snapType == VDI.SNAPSHOT_INTERNAL):
1807 lvSizeClon = 0
1809 # the space required must include 2 journal LVs: a clone journal and an
1810 # inflate journal (for the failure handling
1811 size_req = lvSizeOrig + lvSizeClon + 2 * self.sr.journaler.LV_SIZE
1812 lvSizeBase = self.size
1813 if VdiType.isCowImage(self.vdi_type): 1813 ↛ 1816line 1813 didn't jump to line 1816, because the condition on line 1813 was never false
1814 lvSizeBase = util.roundup(lvutil.LVM_SIZE_INCREMENT, self.cowutil.getSizePhys(self.path))
1815 size_req -= (self.utilisation - lvSizeBase)
1816 self.sr._ensureSpaceAvailable(size_req)
1818 if hostRefs:
1819 self.sr._deactivateOnSlave(hostRefs, self.lvname)
1821 baseUuid = util.gen_uuid()
1822 origUuid = self.uuid
1823 clonUuid = ""
1824 if snapType == VDI.SNAPSHOT_DOUBLE: 1824 ↛ 1826line 1824 didn't jump to line 1826, because the condition on line 1824 was never false
1825 clonUuid = util.gen_uuid()
1826 jval = "%s_%s" % (baseUuid, clonUuid)
1827 self.sr.journaler.create(self.JRN_CLONE, origUuid, jval)
1828 util.fistpoint.activate("LVHDRT_clone_vdi_after_create_journal", self.sr.uuid)
1830 try:
1831 # self becomes the "base vdi"
1832 origOldLV = self.lvname
1833 baseLV = LV_PREFIX[self.vdi_type] + baseUuid
1834 self.sr.lvmCache.rename(self.lvname, baseLV)
1835 self.sr.lvActivator.replace(self.uuid, baseUuid, baseLV, False)
1836 RefCounter.set(baseUuid, 1, 0, NS_PREFIX_LVM + self.sr.uuid)
1837 self.uuid = baseUuid
1838 self.lvname = baseLV
1839 self.path = os.path.join(self.sr.path, baseLV)
1840 self.label = "base copy"
1841 self.read_only = True
1842 self.location = self.uuid
1843 self.managed = False
1845 # shrink the base copy to the minimum - we do it before creating
1846 # the snapshot volumes to avoid requiring double the space
1847 if VdiType.isCowImage(self.vdi_type): 1847 ↛ 1850line 1847 didn't jump to line 1850, because the condition on line 1847 was never false
1848 self.lvmcowutil.deflate(self.sr.lvmCache, self.lvname, lvSizeBase)
1849 self.utilisation = lvSizeBase
1850 util.fistpoint.activate("LVHDRT_clone_vdi_after_shrink_parent", self.sr.uuid)
1852 snapVDI = self._createSnap(origUuid, snapVdiType, lvSizeOrig, False, is_mirror_destination)
1853 util.fistpoint.activate("LVHDRT_clone_vdi_after_first_snap", self.sr.uuid)
1854 snapVDI2 = None
1855 if snapType == VDI.SNAPSHOT_DOUBLE: 1855 ↛ 1861line 1855 didn't jump to line 1861, because the condition on line 1855 was never false
1856 snapVDI2 = self._createSnap(clonUuid, snapVdiType, lvSizeClon, True)
1857 # If we have CBT enabled on the VDI,
1858 # set CBT status for the new snapshot disk
1859 if cbtlog:
1860 snapVDI2.cbt_enabled = True
1861 util.fistpoint.activate("LVHDRT_clone_vdi_after_second_snap", self.sr.uuid)
1863 # note: it is important to mark the parent hidden only AFTER the
1864 # new image children have been created, which are referencing it;
1865 # otherwise we would introduce a race with GC that could reclaim
1866 # the parent before we snapshot it
1867 if not VdiType.isCowImage(self.vdi_type): 1867 ↛ 1868line 1867 didn't jump to line 1868, because the condition on line 1867 was never true
1868 self.sr.lvmCache.setHidden(self.lvname)
1869 else:
1870 self.cowutil.setHidden(self.path)
1871 util.fistpoint.activate("LVHDRT_clone_vdi_after_parent_hidden", self.sr.uuid)
1873 # set the base copy to ReadOnly
1874 if not self.cowutil.isCoalesceableOnRemote(): 1874 ↛ 1881line 1874 didn't jump to line 1881, because the condition on line 1874 was never false
1875 # For QCOW2, the whole chain need to be
1876 # writable for tapdisk to do the coalesce.
1877 # In this case, we don't mark the base copy RO.
1878 self.sr.lvmCache.setReadonly(self.lvname, True)
1879 util.fistpoint.activate("LVHDRT_clone_vdi_after_parent_ro", self.sr.uuid)
1881 if hostRefs:
1882 self.sr._updateSlavesOnClone(hostRefs, origOldLV,
1883 snapVDI.lvname, self.uuid, self.lvname)
1885 # Update cbt files if user created snapshot (SNAPSHOT_DOUBLE)
1886 if snapType == VDI.SNAPSHOT_DOUBLE and cbtlog:
1887 snapVDI._cbt_snapshot(clonUuid, cbt_consistency)
1888 if hostRefs: 1888 ↛ 1902line 1888 didn't jump to line 1902, because the condition on line 1888 was never false
1889 cbtlog_file = self._get_cbt_logname(snapVDI.uuid)
1890 try:
1891 self.sr._updateSlavesOnCBTClone(hostRefs, cbtlog_file)
1892 except:
1893 alert_name = "VDI_CBT_SNAPSHOT_FAILED"
1894 alert_str = ("Creating CBT snapshot for {} failed"
1895 .format(snapVDI.uuid))
1896 snapVDI._disable_cbt_on_error(alert_name, alert_str)
1897 pass
1899 except (util.SMException, XenAPI.Failure) as e:
1900 util.logException("LVMVDI._snapshot")
1901 self._failClone(origUuid, jval, str(e))
1902 util.fistpoint.activate("LVHDRT_clone_vdi_before_remove_journal", self.sr.uuid)
1904 self.sr.journaler.remove(self.JRN_CLONE, origUuid)
1906 return self._finishSnapshot(snapVDI, snapVDI2, hostRefs, cloneOp, snapType)
1908 def _createSnap(self, snapUuid, snapVdiType, snapSizeLV, isNew, is_mirror_destination=False):
1909 """Snapshot self and return the snapshot VDI object"""
1911 snapLV = LV_PREFIX[snapVdiType] + snapUuid
1912 snapPath = os.path.join(self.sr.path, snapLV)
1913 self.sr.lvmCache.create(snapLV, int(snapSizeLV))
1914 util.fistpoint.activate("LVHDRT_clone_vdi_after_lvcreate", self.sr.uuid)
1915 if isNew:
1916 RefCounter.set(snapUuid, 1, 0, NS_PREFIX_LVM + self.sr.uuid)
1917 self.sr.lvActivator.add(snapUuid, snapLV, False)
1918 parentRaw = (self.vdi_type == VdiType.RAW)
1919 self.cowutil.snapshot(
1920 snapPath, self.path, parentRaw, max(self.size, self.cowutil.getDefaultPreallocationSizeVirt()), is_mirror_image=is_mirror_destination
1921 )
1922 snapParent = self.cowutil.getParent(snapPath, LvmCowUtil.extractUuid)
1924 snapVDI = LVMVDI(self.sr, snapUuid)
1925 snapVDI.read_only = False
1926 snapVDI.location = snapUuid
1927 snapVDI.size = self.size
1928 snapVDI.utilisation = snapSizeLV
1929 snapVDI.sm_config = dict()
1930 for key, val in self.sm_config.items():
1931 if key not in [ 1931 ↛ 1934line 1931 didn't jump to line 1934, because the condition on line 1931 was never true
1932 "type", "vdi_type", "vhd-parent", "paused", "relinking", "activating"] and \
1933 not key.startswith("host_"):
1934 snapVDI.sm_config[key] = val
1935 snapVDI.sm_config["vdi_type"] = snapVdiType
1936 snapVDI.sm_config["vhd-parent"] = snapParent
1937 # TODO: fix the raw snapshot case
1938 snapVDI.sm_config["image-format"] = getImageStringFromVdiType(self.vdi_type)
1939 snapVDI.lvname = snapLV
1940 return snapVDI
1942 def _finishSnapshot(self, snapVDI, snapVDI2, hostRefs, cloneOp=False, snapType=None):
1943 if snapType is not VDI.SNAPSHOT_INTERNAL: 1943 ↛ 1945line 1943 didn't jump to line 1945, because the condition on line 1943 was never false
1944 self.sr._updateStats(self.sr.uuid, self.size)
1945 basePresent = True
1947 # Verify parent locator field of both children and delete basePath if
1948 # unused
1949 snapParent = snapVDI.sm_config["vhd-parent"]
1950 snap2Parent = ""
1951 if snapVDI2: 1951 ↛ 1953line 1951 didn't jump to line 1953, because the condition on line 1951 was never false
1952 snap2Parent = snapVDI2.sm_config["vhd-parent"]
1953 if snapParent != self.uuid and \ 1953 ↛ 1987line 1953 didn't jump to line 1987, because the condition on line 1953 was never false
1954 (not snapVDI2 or snap2Parent != self.uuid):
1955 util.SMlog("%s != %s != %s => deleting unused base %s" % \
1956 (snapParent, self.uuid, snap2Parent, self.lvname))
1957 RefCounter.put(self.uuid, False, NS_PREFIX_LVM + self.sr.uuid)
1959 # The removed LV could still be activated on a slave host if it's
1960 # part of a VM currently running there, we need to deactivate it
1961 # before it gets removed to avoid a LV leak.
1962 if hostRefs:
1963 self.sr._deactivateOnSlave(hostRefs, self.lvname)
1965 self.sr.lvmCache.remove(self.lvname)
1966 self.sr.lvActivator.remove(self.uuid, False)
1967 if hostRefs:
1968 self.sr._updateSlavesOnRemove(hostRefs, self.uuid, self.lvname)
1969 basePresent = False
1970 else:
1971 # assign the _binary_ refcount of the original VDI to the new base
1972 # VDI (but as the normal refcount, since binary refcounts are only
1973 # for leaf nodes). The normal refcount of the child is not
1974 # transferred to to the base VDI because normal refcounts are
1975 # incremented and decremented individually, and not based on the
1976 # image chain (i.e., the child's normal refcount will be decremented
1977 # independently of its parent situation). Add 1 for this clone op.
1978 # Note that we do not need to do protect the refcount operations
1979 # below with per-VDI locking like we do in lvutil because at this
1980 # point we have exclusive access to the VDIs involved. Other SM
1981 # operations are serialized by the Agent or with the SR lock, and
1982 # any coalesce activations are serialized with the SR lock. (The
1983 # coalesce activates the coalesced VDI pair in the beginning, which
1984 # cannot affect the VDIs here because they cannot possibly be
1985 # involved in coalescing at this point, and at the relinkSkip step
1986 # that activates the children, which takes the SR lock.)
1987 ns = NS_PREFIX_LVM + self.sr.uuid
1988 (cnt, bcnt) = RefCounter.check(snapVDI.uuid, ns)
1989 RefCounter.set(self.uuid, bcnt + 1, 0, ns)
1991 # the "paused" and "host_*" sm-config keys are special and must stay on
1992 # the leaf without being inherited by anyone else
1993 for key in [x for x in self.sm_config.keys() if x == "paused" or x.startswith("host_")]: 1993 ↛ 1994line 1993 didn't jump to line 1994, because the loop on line 1993 never started
1994 snapVDI.sm_config[key] = self.sm_config[key]
1995 del self.sm_config[key]
1997 # Introduce any new VDI records & update the existing one
1998 type = self.session.xenapi.VDI.get_type( \
1999 self.sr.srcmd.params['vdi_ref'])
2000 if snapVDI2: 2000 ↛ 2042line 2000 didn't jump to line 2042, because the condition on line 2000 was never false
2001 LVMMetadataHandler(self.sr.mdpath).ensureSpaceIsAvailableForVdis(1)
2002 vdiRef = snapVDI2._db_introduce()
2003 if cloneOp:
2004 vdi_info = {UUID_TAG: snapVDI2.uuid,
2005 NAME_LABEL_TAG: util.to_plain_string( \
2006 self.session.xenapi.VDI.get_name_label( \
2007 self.sr.srcmd.params['vdi_ref'])),
2008 NAME_DESCRIPTION_TAG: util.to_plain_string( \
2009 self.session.xenapi.VDI.get_name_description(self.sr.srcmd.params['vdi_ref'])),
2010 IS_A_SNAPSHOT_TAG: 0,
2011 SNAPSHOT_OF_TAG: '',
2012 SNAPSHOT_TIME_TAG: '',
2013 TYPE_TAG: type,
2014 VDI_TYPE_TAG: snapVDI2.sm_config['vdi_type'],
2015 READ_ONLY_TAG: 0,
2016 MANAGED_TAG: int(snapVDI2.managed),
2017 METADATA_OF_POOL_TAG: ''
2018 }
2019 else:
2020 util.SMlog("snapshot VDI params: %s" % \
2021 self.session.xenapi.VDI.get_snapshot_time(vdiRef))
2022 vdi_info = {UUID_TAG: snapVDI2.uuid,
2023 NAME_LABEL_TAG: util.to_plain_string( \
2024 self.session.xenapi.VDI.get_name_label( \
2025 self.sr.srcmd.params['vdi_ref'])),
2026 NAME_DESCRIPTION_TAG: util.to_plain_string( \
2027 self.session.xenapi.VDI.get_name_description(self.sr.srcmd.params['vdi_ref'])),
2028 IS_A_SNAPSHOT_TAG: 1,
2029 SNAPSHOT_OF_TAG: snapVDI.uuid,
2030 SNAPSHOT_TIME_TAG: '',
2031 TYPE_TAG: type,
2032 VDI_TYPE_TAG: snapVDI2.sm_config['vdi_type'],
2033 READ_ONLY_TAG: 0,
2034 MANAGED_TAG: int(snapVDI2.managed),
2035 METADATA_OF_POOL_TAG: ''
2036 }
2038 LVMMetadataHandler(self.sr.mdpath).addVdi(vdi_info)
2039 util.SMlog("vdi_clone: introduced 2nd snap VDI: %s (%s)" % \
2040 (vdiRef, snapVDI2.uuid))
2042 if basePresent: 2042 ↛ 2043line 2042 didn't jump to line 2043, because the condition on line 2042 was never true
2043 LVMMetadataHandler(self.sr.mdpath).ensureSpaceIsAvailableForVdis(1)
2044 vdiRef = self._db_introduce()
2045 vdi_info = {UUID_TAG: self.uuid,
2046 NAME_LABEL_TAG: self.label,
2047 NAME_DESCRIPTION_TAG: self.description,
2048 IS_A_SNAPSHOT_TAG: 0,
2049 SNAPSHOT_OF_TAG: '',
2050 SNAPSHOT_TIME_TAG: '',
2051 TYPE_TAG: type,
2052 VDI_TYPE_TAG: self.sm_config['vdi_type'],
2053 READ_ONLY_TAG: 1,
2054 MANAGED_TAG: 0,
2055 METADATA_OF_POOL_TAG: ''
2056 }
2058 LVMMetadataHandler(self.sr.mdpath).addVdi(vdi_info)
2059 util.SMlog("vdi_clone: introduced base VDI: %s (%s)" % \
2060 (vdiRef, self.uuid))
2062 # Update the original record
2063 vdi_ref = self.sr.srcmd.params['vdi_ref']
2064 self.session.xenapi.VDI.set_sm_config(vdi_ref, snapVDI.sm_config)
2065 self.session.xenapi.VDI.set_physical_utilisation(vdi_ref, \
2066 str(snapVDI.utilisation))
2068 # Return the info on the new snap VDI
2069 snap = snapVDI2
2070 if not snap: 2070 ↛ 2071line 2070 didn't jump to line 2071, because the condition on line 2070 was never true
2071 snap = self
2072 if not basePresent:
2073 # a single-snapshot of an empty VDI will be a noop, resulting
2074 # in no new VDIs, so return the existing one. The GC wouldn't
2075 # normally try to single-snapshot an empty image of course, but
2076 # if an external snapshot operation manages to sneak in right
2077 # before a snapshot-coalesce phase, we would get here
2078 snap = snapVDI
2079 return snap.get_params()
2081 def _setType(self, vdiType: str) -> None:
2082 self.vdi_type = vdiType
2083 self.cowutil = getCowUtil(self.vdi_type)
2084 self.lvmcowutil = LvmCowUtil(self.cowutil)
2086 def _initFromVDIInfo(self, vdiInfo):
2087 self._setType(vdiInfo.vdiType)
2088 self.lvname = vdiInfo.lvName
2089 self.size = vdiInfo.sizeVirt
2090 self.utilisation = vdiInfo.sizeLV
2091 self.hidden = vdiInfo.hidden
2092 if self.hidden: 2092 ↛ 2093line 2092 didn't jump to line 2093, because the condition on line 2092 was never true
2093 self.managed = False
2094 self.active = vdiInfo.lvActive
2095 self.readonly = vdiInfo.lvReadonly
2096 self.parent = vdiInfo.parentUuid
2097 self.path = os.path.join(self.sr.path, self.lvname)
2098 if hasattr(self, "sm_config_override"): 2098 ↛ 2101line 2098 didn't jump to line 2101, because the condition on line 2098 was never false
2099 self.sm_config_override["vdi_type"] = self.vdi_type
2100 else:
2101 self.sm_config_override = {'vdi_type': self.vdi_type}
2102 self.loaded = True
2104 def _initFromLVInfo(self, lvInfo):
2105 self._setType(lvInfo.vdiType)
2106 self.lvname = lvInfo.name
2107 self.size = lvInfo.size
2108 self.utilisation = lvInfo.size
2109 self.hidden = lvInfo.hidden
2110 self.active = lvInfo.active
2111 self.readonly = lvInfo.readonly
2112 self.parent = ''
2113 self.path = os.path.join(self.sr.path, self.lvname)
2114 if hasattr(self, "sm_config_override"): 2114 ↛ 2117line 2114 didn't jump to line 2117, because the condition on line 2114 was never false
2115 self.sm_config_override["vdi_type"] = self.vdi_type
2116 else:
2117 self.sm_config_override = {'vdi_type': self.vdi_type}
2118 if 'vhd-parent' in self.sm_config_override: 2118 ↛ 2119line 2118 didn't jump to line 2119, because the condition on line 2118 was never true
2119 self.parent = self.sm_config_override['vhd-parent']
2120 if not VdiType.isCowImage(self.vdi_type): 2120 ↛ 2121line 2120 didn't jump to line 2121, because the condition on line 2120 was never true
2121 self.loaded = True
2123 def _initFromImageInfo(self, imageInfo):
2124 self.size = imageInfo.sizeVirt
2125 if self.parent == '' or (imageInfo.parentUuid != '' and imageInfo.parentUuid != self.parent): 2125 ↛ 2127line 2125 didn't jump to line 2127, because the condition on line 2125 was never false
2126 self.parent = imageInfo.parentUuid
2127 self.hidden = imageInfo.hidden
2128 self.loaded = True
2130 def _determineType(self):
2131 """
2132 Determine whether this is a RAW or a COW VDI.
2133 """
2134 if "vdi_ref" in self.sr.srcmd.params:
2135 vdi_ref = self.sr.srcmd.params["vdi_ref"]
2136 sm_config = self.session.xenapi.VDI.get_sm_config(vdi_ref)
2137 if sm_config.get("vdi_type"):
2138 self._setType(sm_config["vdi_type"])
2139 prefix = LV_PREFIX[self.vdi_type]
2140 self.lvname = "%s%s" % (prefix, self.uuid)
2141 self.path = os.path.join(self.sr.path, self.lvname)
2142 self.sm_config_override = sm_config
2143 return True
2145 # LVM commands can be costly, so check the file directly first in case
2146 # the LV is active
2147 found = False
2148 for vdi_type, prefix in LV_PREFIX.items():
2149 lvname = "%s%s" % (prefix, self.uuid)
2150 path = os.path.join(self.sr.path, lvname)
2151 if util.pathexists(path):
2152 if found: 2152 ↛ 2153line 2152 didn't jump to line 2153, because the condition on line 2152 was never true
2153 raise xs_errors.XenError('VDILoad',
2154 opterr="multiple VDI's: uuid %s" % self.uuid)
2155 found = True
2156 self._setType(vdi_type)
2157 self.lvname = lvname
2158 self.path = path
2159 if found:
2160 return True
2162 # now list all LV's
2163 if not lvutil._checkVG(self.sr.vgname): 2163 ↛ 2165line 2163 didn't jump to line 2165, because the condition on line 2163 was never true
2164 # when doing attach_from_config, the VG won't be there yet
2165 return False
2167 lvs = LvmCowUtil.getVolumeInfo(self.sr.lvmCache)
2168 if lvs.get(self.uuid): 2168 ↛ 2171line 2168 didn't jump to line 2171, because the condition on line 2168 was never false
2169 self._initFromLVInfo(lvs[self.uuid])
2170 return True
2171 return False
2173 def _loadThis(self):
2174 """
2175 Load VDI info for this VDI and activate the LV if it's COW. We
2176 don't do it in VDI.load() because not all VDI operations need it.
2177 """
2178 if self.loaded: 2178 ↛ 2179line 2178 didn't jump to line 2179, because the condition on line 2178 was never true
2179 if VdiType.isCowImage(self.vdi_type):
2180 self.sr.lvActivator.activate(self.uuid, self.lvname, False)
2181 return
2182 try:
2183 lvs = LvmCowUtil.getVolumeInfo(self.sr.lvmCache, self.lvname)
2184 except util.CommandException as e:
2185 raise xs_errors.XenError('VDIUnavailable',
2186 opterr='%s (LV scan error)' % os.strerror(abs(e.code)))
2187 if not lvs.get(self.uuid): 2187 ↛ 2188line 2187 didn't jump to line 2188, because the condition on line 2187 was never true
2188 raise xs_errors.XenError('VDIUnavailable', opterr='LV not found')
2189 self._initFromLVInfo(lvs[self.uuid])
2190 if VdiType.isCowImage(self.vdi_type): 2190 ↛ 2196line 2190 didn't jump to line 2196, because the condition on line 2190 was never false
2191 self.sr.lvActivator.activate(self.uuid, self.lvname, False)
2192 imageInfo = self.cowutil.getInfo(self.path, LvmCowUtil.extractUuid, False)
2193 if not imageInfo: 2193 ↛ 2194line 2193 didn't jump to line 2194, because the condition on line 2193 was never true
2194 raise xs_errors.XenError('VDIUnavailable', opterr='getInfo failed')
2195 self._initFromImageInfo(imageInfo)
2196 self.loaded = True
2198 def _chainSetActive(self, active, binary, persistent=False):
2199 if binary: 2199 ↛ 2200line 2199 didn't jump to line 2200, because the condition on line 2199 was never true
2200 (count, bcount) = RefCounter.checkLocked(self.uuid,
2201 NS_PREFIX_LVM + self.sr.uuid)
2202 if (active and bcount > 0) or (not active and bcount == 0):
2203 return # this is a redundant activation/deactivation call
2205 vdiList = {self.uuid: self.lvname}
2206 if VdiType.isCowImage(self.vdi_type): 2206 ↛ 2208line 2206 didn't jump to line 2208, because the condition on line 2206 was never false
2207 vdiList = self.cowutil.getParentChain(self.lvname, LvmCowUtil.extractUuid, self.sr.vgname)
2208 for uuid, lvName in vdiList.items():
2209 binaryParam = binary
2210 if uuid != self.uuid: 2210 ↛ 2211line 2210 didn't jump to line 2211, because the condition on line 2210 was never true
2211 binaryParam = False # binary param only applies to leaf nodes
2212 if active: 2212 ↛ 2219line 2212 didn't jump to line 2219, because the condition on line 2212 was never false
2213 self.sr.lvActivator.activate(uuid, lvName, binaryParam,
2214 persistent)
2215 else:
2216 # just add the LVs for deactivation in the final (cleanup)
2217 # step. The LVs must not have been activated during the current
2218 # operation
2219 self.sr.lvActivator.add(uuid, lvName, binaryParam)
2221 def _failClone(self, uuid, jval, msg):
2222 try:
2223 self.sr._handleInterruptedCloneOp(uuid, jval, True)
2224 self.sr.journaler.remove(self.JRN_CLONE, uuid)
2225 except Exception as e:
2226 util.SMlog('WARNING: failed to clean up failed snapshot: ' \
2227 ' %s (error ignored)' % e)
2228 raise xs_errors.XenError('VDIClone', opterr=msg)
2230 def _markHidden(self):
2231 if not VdiType.isCowImage(self.vdi_type):
2232 self.sr.lvmCache.setHidden(self.lvname)
2233 else:
2234 self.cowutil.setHidden(self.path)
2235 self.hidden = 1
2237 def _prepareThin(self, attach, vdiType):
2238 origUtilisation = self.sr.lvmCache.getSize(self.lvname)
2239 if self.sr.isMaster:
2240 # the master can prepare the VDI locally
2241 if attach:
2242 self.lvmcowutil.attachThin(self.sr.journaler, self.sr.uuid, self.uuid, self.vdi_type)
2243 else:
2244 self.lvmcowutil.detachThin(self.session, self.sr.lvmCache, self.sr.uuid, self.uuid, self.vdi_type)
2245 else:
2246 fn = "attach"
2247 if not attach:
2248 fn = "detach"
2249 pools = self.session.xenapi.pool.get_all()
2250 master = self.session.xenapi.pool.get_master(pools[0])
2251 rv = self.session.xenapi.host.call_plugin(
2252 master,
2253 self.sr.THIN_PLUGIN,
2254 fn,
2255 {
2256 "srUuid": self.sr.uuid,
2257 "vdiUuid": self.uuid,
2258 "vdiType": vdiType
2259 }
2260 )
2261 util.SMlog("call-plugin returned: %s" % rv)
2262 if not rv:
2263 raise Exception('plugin %s failed' % self.sr.THIN_PLUGIN)
2264 # refresh to pick up the size change on this slave
2265 self.sr.lvmCache.activateNoRefcount(self.lvname, True)
2267 self.utilisation = self.sr.lvmCache.getSize(self.lvname)
2268 if origUtilisation != self.utilisation:
2269 vdi_ref = self.sr.srcmd.params['vdi_ref']
2270 self.session.xenapi.VDI.set_physical_utilisation(vdi_ref,
2271 str(self.utilisation))
2272 stats = lvutil._getVGstats(self.sr.vgname)
2273 sr_utilisation = stats['physical_utilisation']
2274 self.session.xenapi.SR.set_physical_utilisation(self.sr.sr_ref,
2275 str(sr_utilisation))
2277 def _make_chain_rw(self):
2278 """
2279 In the case of QCOW2, we need the chain to be RW for the coalesce to be done by tapdisk.
2280 Setting the chain to be RW early avoids having to stop the tapdisk so LVM can acknowledge the chain RW on the slave.
2281 And while making the change to RW from the slave works without the refresh, it can cause LVM metadata corruption.
2282 """
2283 # TODO(XCPNG-3689): We could check if any VDI on the chain is RO before doing the call to master to change this
2284 if self.sr.isMaster:
2285 lvmcache = self.sr.lvmCache
2286 for uuid, lvName in self.cowutil.getParentChain(self.lvname, LvmCowUtil.extractUuid, self.sr.vgname).items():
2287 lvmcache.setReadonly(lvName, False)
2288 else:
2289 master = util.get_master_ref(self.session)
2290 response = self.session.xenapi.host.call_plugin(
2291 master,
2292 self.sr.PLUGIN_ON_SLAVE,
2293 "make_chain_rw",
2294 {
2295 "vgName": self.sr.vgname,
2296 "lvName": self.lvname,
2297 "vdiType": self.vdi_type
2298 }
2299 )
2300 util.SMlog(f"call-plugin returned: {response}")
2301 if not response:
2302 raise Exception(f"plugin {self.sr.PLUGIN_ON_SLAVE} failed")
2304 @override
2305 def update(self, sr_uuid, vdi_uuid) -> None:
2306 if self.sr.legacyMode:
2307 return
2309 #Synch the name_label of this VDI on storage with the name_label in XAPI
2310 vdi_ref = self.session.xenapi.VDI.get_by_uuid(self.uuid)
2311 update_map = {}
2312 update_map[METADATA_UPDATE_OBJECT_TYPE_TAG] = \
2313 METADATA_OBJECT_TYPE_VDI
2314 update_map[UUID_TAG] = self.uuid
2315 update_map[NAME_LABEL_TAG] = util.to_plain_string( \
2316 self.session.xenapi.VDI.get_name_label(vdi_ref))
2317 update_map[NAME_DESCRIPTION_TAG] = util.to_plain_string( \
2318 self.session.xenapi.VDI.get_name_description(vdi_ref))
2319 update_map[SNAPSHOT_TIME_TAG] = \
2320 self.session.xenapi.VDI.get_snapshot_time(vdi_ref)
2321 update_map[METADATA_OF_POOL_TAG] = \
2322 self.session.xenapi.VDI.get_metadata_of_pool(vdi_ref)
2323 LVMMetadataHandler(self.sr.mdpath).updateMetadata(update_map)
2325 @override
2326 def _ensure_cbt_space(self) -> None:
2327 # We need virtual_size to compute the size in case of a bigger VDI
2328 self.sr.ensureCBTSpace(self.size)
2330 @override
2331 def _create_cbt_log(self) -> str:
2332 logname = self._get_cbt_logname(self.uuid)
2333 logsize = max(util.roundup(CBT_BLOCK_SIZE, self.size//CBT_BLOCK_SIZE), self.sr.journaler.LV_SIZE)
2334 # We choose 4MiB as the minimum for the log size to maintain the old behavior and compute the correct amount
2335 # if we need a bigger LV for the CBT (can happen with big QCOW2)
2336 self.sr.lvmCache.create(logname, logsize, CBTLOG_TAG)
2337 logpath = super(LVMVDI, self)._create_cbt_log()
2338 self.sr.lvmCache.deactivateNoRefcount(logname)
2339 return logpath
2341 @override
2342 def _delete_cbt_log(self) -> None:
2343 logpath = self._get_cbt_logpath(self.uuid)
2344 if not self._cbt_log_exists(logpath):
2345 return
2346 logname = self._get_cbt_logname(self.uuid)
2347 if not self.sr.isMaster and self.sr.is_shared():
2348 master = util.get_master_ref(self.session)
2349 response = self.session.xenapi.host.call_plugin(
2350 master,
2351 self.sr.PLUGIN_ON_MASTER,
2352 "delete_cbt_log",
2353 {
2354 "vgName": self.sr.vgname,
2355 "lvName": logname,
2356 }
2357 )
2358 util.SMlog(f"on-master.delete_cbt_log call-plugin returned: {response}")
2359 if not response:
2360 raise Exception(f"plugin {self.sr.PLUGIN_ON_MASTER} failed")
2361 lvutil._lvmBugCleanup(logpath)
2362 return
2363 self.sr.lvmCache.remove(logname)
2365 @override
2366 def _rename(self, oldpath, newpath) -> None:
2367 oldname = os.path.basename(oldpath)
2368 newname = os.path.basename(newpath)
2369 self.sr.lvmCache.rename(oldname, newname)
2371 @override
2372 def update_slaves_on_cbt_disable(self, cbtlog) -> None:
2373 args = {
2374 "vgName": self.sr.vgname,
2375 "action1": "deactivateNoRefcount",
2376 "lvName1": cbtlog
2377 }
2379 host_refs = util.get_hosts_attached_on(self.session, [self.uuid])
2381 message = f"Deactivating {cbtlog}"
2382 self.sr.call_on_slave(args, host_refs, message)
2384 @override
2385 def _activate_cbt_log(self, lv_name) -> bool:
2386 self.sr.lvmCache.refresh()
2387 if not self.sr.lvmCache.is_active(lv_name): 2387 ↛ 2388line 2387 didn't jump to line 2388, because the condition on line 2387 was never true
2388 try:
2389 self.sr.lvmCache.activateNoRefcount(lv_name)
2390 return True
2391 except Exception as e:
2392 util.SMlog("Exception in _activate_cbt_log, "
2393 "Error: %s." % str(e))
2394 raise
2395 else:
2396 return False
2398 @override
2399 def _deactivate_cbt_log(self, lv_name) -> None:
2400 try:
2401 self.sr.lvmCache.deactivateNoRefcount(lv_name)
2402 except Exception as e:
2403 util.SMlog("Exception in _deactivate_cbt_log, Error: %s." % str(e))
2404 raise
2406 @override
2407 def _cbt_log_exists(self, logpath) -> bool:
2408 return lvutil.exists(logpath)
2410if __name__ == '__main__': 2410 ↛ 2411line 2410 didn't jump to line 2411, because the condition on line 2410 was never true
2411 SRCommand.run(LVMSR, DRIVER_INFO)
2412else:
2413 SR.registerSR(LVMSR)