diff --git a/cinder/tests/unit/volume/drivers/test_lvm_driver.py b/cinder/tests/unit/volume/drivers/test_lvm_driver.py index 96ceff4f6a4..34c28f36b5f 100644 --- a/cinder/tests/unit/volume/drivers/test_lvm_driver.py +++ b/cinder/tests/unit/volume/drivers/test_lvm_driver.py @@ -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'}]) diff --git a/cinder/volume/drivers/lvm.py b/cinder/volume/drivers/lvm.py index 21002c8cb6e..885c4487795 100644 --- a/cinder/volume/drivers/lvm.py +++ b/cinder/volume/drivers/lvm.py @@ -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,