From 152cf2857fd651d361c465887f35a035030042e2 Mon Sep 17 00:00:00 2001 From: Lucas Alvares Gomes Date: Thu, 19 Jan 2017 16:22:30 +0000 Subject: [PATCH] Limit memory usage when running qemu-img With multiple qemu-img processes running at the same time the host machine can quickly run out of memory. This patch is limiting the memory space to 1 GiB when invoking qemu-img. Change-Id: If791452edc38c4732ba2b96220e7969c72b234dd Closes-Bug: #1657808 (cherry picked from commit 4ae48d0b212c16c8b49d4f1144c073b3a3206597) --- ironic_lib/disk_utils.py | 8 ++++++-- ironic_lib/tests/test_disk_utils.py | 6 ++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/ironic_lib/disk_utils.py b/ironic_lib/disk_utils.py index c43c1180..190dd72c 100644 --- a/ironic_lib/disk_utils.py +++ b/ironic_lib/disk_utils.py @@ -73,6 +73,9 @@ MAX_CONFIG_DRIVE_SIZE_MB = 64 # Maximum disk size supported by MBR is 2TB (2 * 1024 * 1024 MB) MAX_DISK_SIZE_MB_SUPPORTED_BY_MBR = 2097152 +# Limit the memory address space to 1 GiB when running qemu-img +QEMU_IMG_LIMITS = processutils.ProcessLimits(address_space=1 * units.Gi) + def list_partitions(device): """Get partitions information from given device. @@ -291,14 +294,15 @@ def qemu_img_info(path): return imageutils.QemuImgInfo() out, err = utils.execute('env', 'LC_ALL=C', 'LANG=C', - 'qemu-img', 'info', path) + 'qemu-img', 'info', path, + prlimit=QEMU_IMG_LIMITS) return imageutils.QemuImgInfo(out) def convert_image(source, dest, out_format, run_as_root=False): """Convert image to other format.""" cmd = ('qemu-img', 'convert', '-O', out_format, source, dest) - utils.execute(*cmd, run_as_root=run_as_root) + utils.execute(*cmd, run_as_root=run_as_root, prlimit=QEMU_IMG_LIMITS) def populate_image(src, dst): diff --git a/ironic_lib/tests/test_disk_utils.py b/ironic_lib/tests/test_disk_utils.py index 782fd4fb..4a0d45b5 100644 --- a/ironic_lib/tests/test_disk_utils.py +++ b/ironic_lib/tests/test_disk_utils.py @@ -635,7 +635,8 @@ class OtherFunctionTestCase(test_base.BaseTestCase): disk_utils.qemu_img_info('img') path_exists_mock.assert_called_once_with('img') execute_mock.assert_called_once_with('env', 'LC_ALL=C', 'LANG=C', - 'qemu-img', 'info', 'img') + 'qemu-img', 'info', 'img', + prlimit=mock.ANY) qemu_img_info_mock.assert_called_once_with('out') @mock.patch.object(utils, 'execute', autospec=True) @@ -643,7 +644,8 @@ class OtherFunctionTestCase(test_base.BaseTestCase): disk_utils.convert_image('source', 'dest', 'out_format') execute_mock.assert_called_once_with('qemu-img', 'convert', '-O', 'out_format', 'source', 'dest', - run_as_root=False) + run_as_root=False, + prlimit=mock.ANY) @mock.patch.object(os.path, 'getsize', autospec=True) @mock.patch.object(disk_utils, 'qemu_img_info', autospec=True)