Cloning a volume from a snapshot in thin lv

To create a volume from a snapshot in thin lv,
we can create a snapshot for the snapshot to
create a volume same as snapshot. And active it.

Change-Id: Ib12752e541f522a9091a2bb2a56bec4193e5f3e7
Closes-Bug: #1707098
This commit is contained in:
wanghongxu 2017-07-28 09:47:45 +08:00
parent 48630526e2
commit 5821813828
2 changed files with 30 additions and 13 deletions

View File

@ -212,9 +212,7 @@ class LVMVolumeDriverTestCase(test_driver.BaseDriverTestCase):
lvm_driver = lvm.LVMVolumeDriver(configuration=self.configuration,
db=db)
with mock.patch.object(lvm_driver, 'vg'), \
mock.patch.object(lvm_driver, '_create_volume'), \
mock.patch.object(volutils, 'copy_volume') as mock_copy:
with mock.patch.object(lvm_driver, 'vg'):
# Test case for thin LVM
lvm_driver._sparse_copy_volume = True
@ -225,16 +223,25 @@ class LVMVolumeDriverTestCase(test_driver.BaseDriverTestCase):
lvm_driver.create_volume_from_snapshot(dst_volume,
snapshot_ref)
volume_path = lvm_driver.local_path(dst_volume)
snapshot_path = lvm_driver.local_path(snapshot_ref)
volume_size = 1024
block_size = '1M'
mock_copy.assert_called_with(snapshot_path,
volume_path,
volume_size,
block_size,
execute=lvm_driver._execute,
sparse=True)
def test_create_volume_from_snapshot_sparse_extend(self):
self.configuration.lvm_type = 'thin'
lvm_driver = lvm.LVMVolumeDriver(configuration=self.configuration,
db=db)
with mock.patch.object(lvm_driver, 'vg'), \
mock.patch.object(lvm_driver, 'extend_volume') as mock_extend:
# Test case for thin LVM
lvm_driver._sparse_copy_volume = True
src_volume = tests_utils.create_volume(self.context)
snapshot_ref = tests_utils.create_snapshot(self.context,
src_volume['id'])
dst_volume = tests_utils.create_volume(self.context)
dst_volume['size'] = snapshot_ref['volume_size'] + 1
lvm_driver.create_volume_from_snapshot(dst_volume,
snapshot_ref)
mock_extend.assert_called_with(dst_volume, dst_volume['size'])
@mock.patch.object(cinder.volume.utils, 'get_all_volume_groups',
return_value=[{'name': 'cinder-volumes'}])

View File

@ -409,6 +409,16 @@ class LVMVolumeDriver(driver.VolumeDriver):
def create_volume_from_snapshot(self, volume, snapshot):
"""Creates a volume from a snapshot."""
if self.configuration.lvm_type == 'thin':
self.vg.create_lv_snapshot(volume['name'],
self._escape_snapshot(snapshot['name']),
self.configuration.lvm_type)
if volume['size'] > snapshot['volume_size']:
LOG.debug("Resize the new volume to %s.", volume['size'])
self.extend_volume(volume, volume['size'])
self.vg.activate_lv(volume['name'], is_snapshot=True,
permanent=True)
return
self._create_volume(volume['name'],
self._sizestr(volume['size']),
self.configuration.lvm_type,