From e71a6c90bf569e6c9638d6ad86191b397a2bc5e9 Mon Sep 17 00:00:00 2001 From: "Joshua M. Boniface" Date: Sun, 12 Sep 2021 19:54:51 -0400 Subject: [PATCH] Add pool size check when resizing volumes Closes #140 --- daemon-common/ceph.py | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/daemon-common/ceph.py b/daemon-common/ceph.py index 94936221..4e0a0007 100644 --- a/daemon-common/ceph.py +++ b/daemon-common/ceph.py @@ -480,16 +480,16 @@ def getVolumeInformation(zkhandler, pool, volume): def add_volume(zkhandler, pool, name, size): + # Add 'B' if the volume is in bytes + if re.match(r'^[0-9]+$', size): + size = '{}B'.format(size) + # 1. Verify the size of the volume pool_information = getPoolInformation(zkhandler, pool) size_bytes = format_bytes_fromhuman(size) if size_bytes >= int(pool_information['stats']['free_bytes']): return False, 'ERROR: Requested volume size is greater than the available free space in the pool' - # Add 'B' if the volume is in bytes - if re.match(r'^[0-9]+$', size): - size = '{}B'.format(size) - # 2. Create the volume retcode, stdout, stderr = common.run_os_command('rbd create --size {} {}/{}'.format(size, pool, name)) if retcode: @@ -540,12 +540,18 @@ def resize_volume(zkhandler, pool, name, size): if re.match(r'^[0-9]+$', size): size = '{}B'.format(size) - # 1. Resize the volume + # 1. Verify the size of the volume + pool_information = getPoolInformation(zkhandler, pool) + size_bytes = format_bytes_fromhuman(size) + if size_bytes >= int(pool_information['stats']['free_bytes']): + return False, 'ERROR: Requested volume size is greater than the available free space in the pool' + + # 2. Resize the volume retcode, stdout, stderr = common.run_os_command('rbd resize --size {} {}/{}'.format(size, pool, name)) if retcode: return False, 'ERROR: Failed to resize RBD volume "{}" to size "{}" in pool "{}": {}'.format(name, size, pool, stderr) - # 2a. Determine the node running this VM if applicable + # 3a. Determine the node running this VM if applicable active_node = None volume_vm_name = name.split('_')[0] retcode, vm_info = vm.get_info(zkhandler, volume_vm_name) @@ -555,7 +561,7 @@ def resize_volume(zkhandler, pool, name, size): if disk['name'] == '{}/{}'.format(pool, name): active_node = vm_info['node'] volume_id = disk['dev'] - # 2b. Perform a live resize in libvirt if the VM is running + # 3b. Perform a live resize in libvirt if the VM is running if active_node is not None and vm_info.get('state', '') == 'start': import libvirt # Run the libvirt command against the target host @@ -569,11 +575,11 @@ def resize_volume(zkhandler, pool, name, size): except Exception: pass - # 2. Get volume stats + # 4. Get volume stats retcode, stdout, stderr = common.run_os_command('rbd info --format json {}/{}'.format(pool, name)) volstats = stdout - # 3. Add the new volume to Zookeeper + # 5. Update the volume in Zookeeper zkhandler.write([ (('volume', f'{pool}/{name}'), ''), (('volume.stats', f'{pool}/{name}'), volstats),