From 0f30b1349d430bcb564e9bf736f98de6bc511e65 Mon Sep 17 00:00:00 2001 From: OpenGnSys Support Team Date: Thu, 15 Feb 2024 10:26:26 +0100 Subject: fs: disentagle dry-run ntfsresize loop to probe for best shrink size Revisit 5056b8f0d5ab ("fs: validate ntfsresize dry-run output") that has introduced a possible infinity loop. Disentangle this loop while at it: iterate until best smallest size is found by probing. --- src/utils/fs.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/utils/fs.py b/src/utils/fs.py index fbbbd3c..651801c 100644 --- a/src/utils/fs.py +++ b/src/utils/fs.py @@ -235,19 +235,27 @@ def _reduce_ntfsresize(partdev): size = int(out_info.split('device size: ')[1].split(' ')[0]) new_size = int(int(out_info.split('resize at ')[1].split(' ')[0])*1.1+1024) - # Dry-run loop to test if resizing is actually possible. This is required by ntfsresize. - returncode = 1 - while new_size < size and returncode != 0: + # Run ntfsresize with -n to to probe for the smallest size, this loop is + # intentional. Acumulate size until ntfsresize in dry-run mode fails, then + # use such size. + while new_size < size: cmd_resize_dryrun = shlex.split(f'ntfsresize -Pfns {new_size:.0f} {partdev}') proc_resize_dryrun = subprocess.run(cmd_resize_dryrun, stdout=subprocess.PIPE, encoding='utf-8') - returncode = proc_resize_dryrun.returncode + + # valid new size found, stop probing + if proc_resize_dryrun.returncode == 0: + break + out_resize_dryrun = proc_resize_dryrun.stdout.strip() if 'Nothing to do: NTFS volume size is already OK.' in out_resize_dryrun: - logging.warn('ntfsresize reports nothing to do. Is the target filesystem already shrunken?') + logging.info('ntfsresize reports nothing to do. Is the target filesystem already shrunken?') break - if out_resize_dryrun.find('Needed relocations : ') != -1: - extra_size = int(out_resize_dryrun.split('Needed relocations : ')[1].split(' ')[0])*1.1+1024 - new_size += int(extra_size) + + if out_resize_dryrun.find('Needed relocations : ') == -1: + break + + extra_size = int(out_resize_dryrun.split('Needed relocations : ')[1].split(' ')[0])*1.1+1024 + new_size += int(extra_size) if new_size < size: cmd_resize = shlex.split(f'ntfsresize -fs {new_size:.0f} {partdev}') -- cgit v1.2.3-18-g5258