class LVM(object):
"""LVM object to enable various LVM related operations."""
- def __init__(self, vg_name, create_vg=False,
- physical_volumes=None):
+ def __init__(self,
+ vg_name,
+ create_vg=False,
+ physical_volumes=None,
+ lvm_type='default',
+ executor=putils.execute):
"""Initialize the LVM object.
The LVM object is based on an LVM VolumeGroup, one instantiation
self.vg_available_space = 0
self.vg_lv_count = 0
self.vg_uuid = None
+ self._execute = executor
if create_vg and physical_volumes is not None:
self.pv_list = physical_volumes
"""
exists = False
cmd = ['vgs', '--noheadings', '-o', 'name']
- (out, err) = putils.execute(*cmd, root_helper='sudo', run_as_root=True)
+ (out, err) = self._execute(*cmd, root_helper='sudo', run_as_root=True)
if out is not None:
volume_groups = out.split()
def _create_vg(self, pv_list):
cmd = ['vgcreate', self.vg_name, ','.join(pv_list)]
- putils.execute(*cmd, root_helper='sudo', run_as_root=True)
+ self._execute(*cmd, root_helper='sudo', run_as_root=True)
def _get_vg_uuid(self):
- (out, err) = putils.execute('vgs', '--noheadings',
- '-o uuid', self.vg_name)
+ (out, err) = self._execute('vgs', '--noheadings',
+ '-o uuid', self.vg_name)
if out is not None:
return out.split()
else:
:param mirror_count: Use LVM mirroring with specified count
"""
+
size = self._size_str(size_str)
cmd = ['lvcreate', '-n', name, self.vg_name]
if lv_type == 'thin':
if mirror_count > 0:
cmd += ['-m', mirror_count, '--nosync']
- terras = int(size[:-1]) / 1024.0
+ terras = int(size_str[:-1]) / 1024.0
if terras >= 1.5:
rsize = int(2 ** math.ceil(math.log(terras) / math.log(2)))
# NOTE(vish): Next power of two for region size. See:
# http://red.ht/U2BPOD
cmd += ['-R', str(rsize)]
- putils.execute(*cmd,
- root_helper='sudo',
- run_as_root=True)
+ self._execute(*cmd,
+ root_helper='sudo',
+ run_as_root=True)
def create_lv_snapshot(self, name, source_lv_name, lv_type='default'):
"""Creates a snapshot of a logical volume.
size = source_lvref['size']
cmd += ['-L', size]
- putils.execute(*cmd,
- root_helper='sudo',
- run_as_root=True)
+ self._execute(*cmd,
+ root_helper='sudo',
+ run_as_root=True)
def delete(self, name):
"""Delete logical volume or snapshot.
:param name: Name of LV to delete
"""
- putils.execute('lvremove',
- '-f',
- '%s/%s' % (self.vg_name, name),
- root_helper='sudo', run_as_root=True)
+ self._execute('lvremove',
+ '-f',
+ '%s/%s' % (self.vg_name, name),
+ root_helper='sudo', run_as_root=True)
def revert(self, snapshot_name):
"""Revert an LV from snapshot.
:param snapshot_name: Name of snapshot to revert
"""
- putils.execute('lvconvert', '--merge',
- snapshot_name, root_helper='sudo',
- run_as_root=True)
+ self._execute('lvconvert', '--merge',
+ snapshot_name, root_helper='sudo',
+ run_as_root=True)
super(BrickLvmTestCase, self).setUp()
self.stubs.Set(processutils, 'execute',
self.fake_execute)
- self.vg = brick.LVM(self.configuration.volume_group_name)
+ self.vg = brick.LVM(self.configuration.volume_group_name,
+ False, None, 'default', self.fake_execute)
def failed_fake_execute(obj, *cmd, **kwargs):
return ("\n", "fake-error")
def fake_execute(obj, *cmd, **kwargs):
cmd_string = ', '.join(cmd)
data = "\n"
-
if 'vgs, --noheadings, -o, name' == cmd_string:
data = " fake-volumes\n"
if 'vgs, --version' in cmd_string:
return (data, "")
def test_vg_exists(self):
- self.stubs.Set(processutils, 'execute', self.fake_execute)
self.assertEqual(self.vg._vg_exists(), True)
- self.stubs.Set(processutils, 'execute', self.failed_fake_execute)
- self.assertEqual(self.vg._vg_exists(), False)
-
def test_get_vg_uuid(self):
- self.stubs.Set(processutils, 'execute', self.fake_execute)
self.assertEqual(self.vg._get_vg_uuid()[0],
'kVxztV-dKpG-Rz7E-xtKY-jeju-QsYU-SLG6Z1')
def test_get_all_volumes(self):
- self.stubs.Set(processutils, 'execute', self.fake_execute)
out = self.vg.get_volumes()
self.assertEqual(out[0]['name'], 'fake-1')
self.assertEqual(out[0]['vg'], 'fake-volumes')
def test_get_volume(self):
- self.stubs.Set(processutils, 'execute', self.fake_execute)
self.assertEqual(self.vg.get_volume('fake-1')['name'], 'fake-1')
def test_get_all_physical_volumes(self):
- self.stubs.Set(processutils, 'execute', self.fake_execute)
pvs = self.vg.get_all_physical_volumes()
self.assertEqual(len(pvs), 3)
def test_get_physical_volumes(self):
- self.stubs.Set(processutils, 'execute', self.fake_execute)
pvs = self.vg.get_physical_volumes()
self.assertEqual(len(pvs), 1)
def test_get_volume_groups(self):
- self.stubs.Set(processutils, 'execute', self.fake_execute)
self.assertEqual(len(self.vg.get_all_volume_groups()), 3)
self.assertEqual(len(self.vg.get_all_volume_groups('fake-volumes')), 1)
def test_update_vg_info(self):
- self.stubs.Set(processutils, 'execute', self.fake_execute)
self.assertEqual(self.vg.update_volume_group_info()['name'],
'fake-volumes')
self.stubs.Set(processutils, 'execute', self.fake_execute)
self.assertTrue(self.vg.supports_thin_provisioning())
- self.stubs.Set(processutils, 'execute', self.fake_pretend_lvm_version)
- self.assertTrue(self.vg.supports_thin_provisioning())
-
self.stubs.Set(processutils, 'execute', self.fake_old_lvm_version)
self.assertFalse(self.vg.supports_thin_provisioning())