diff options
author | Sebastian Bachmann <hello@reox.at> | 2022-01-02 13:25:25 +0100 |
---|---|---|
committer | Sebastian Bachmann <hello@reox.at> | 2022-01-02 13:25:25 +0100 |
commit | d2437ac95bc0c346d32feb4acf650656414da7ff (patch) | |
tree | 776720b610e67ef9f53f58ae338a73812e902eeb | |
parent | 55b4931b80b5b740f595efd82dc6bf0e9aceda5b (diff) | |
download | vmdb2-d2437ac95bc0c346d32feb4acf650656414da7ff.tar.gz |
Fix part-naming and add timeout while waiting
When installing on a blockdevice which device name ends in a number,
a 'p' is added between the device name and the partition number.
This will be the case, for example, when installing to /dev/nbd0 or
/dev/nvme0.
Further, added a timeout to the wait_for_file_to_exist function,
in order not to hang when a partition was not found.
Both changes should resolve #46
-rw-r--r-- | vmdb/plugins/mkpart_plugin.py | 32 |
1 files changed, 28 insertions, 4 deletions
diff --git a/vmdb/plugins/mkpart_plugin.py b/vmdb/plugins/mkpart_plugin.py index cc842ff..316a9e1 100644 --- a/vmdb/plugins/mkpart_plugin.py +++ b/vmdb/plugins/mkpart_plugin.py @@ -16,6 +16,8 @@ # =*= License: GPL-3+ =*= +import itertools +import logging import os import stat import time @@ -89,16 +91,30 @@ class MkpartStepRunner(vmdb.StepRunnerInterface): output = output.decode("UTF-8") partitions = [line.split(":")[0] for line in output.splitlines() if ":" in line] return [ - word if word.startswith("/") else "{}{}".format(device, word) + word if word.startswith("/") else "{}{}{}".format( + device, + # If the device name ends in a number, + # a 'p' is added between device and part number + 'p' if device[-1].isnumeric() else '', + word + ) for word in partitions ] def diff_partitions(self, old, new): return [line for line in new if line not in old] - def wait_for_file_to_exist(self, filename): - while not os.path.exists(filename): - time.sleep(1) + def wait_for_file_to_exist(self, filename, timeout=60): + logging.debug("Starting to wait for file %s to come to life", filename) + if timeout is not None: + logging.debug("Waiting %s second(s) before timeout", timeout) + for count in itertools.count(): + if os.path.exists(filename): + return + if timeout is None or count < timeout: + time.sleep(1) + else: + raise TimeoutOnWaitingForFile(filename) class MkpartError(Exception): @@ -117,3 +133,11 @@ class UnexpectedNewPartitions(MkpartError): "Expected only one new partition to exist after mkpart, " "but found {}".format(" ".join(diff)) ) + + +class TimeoutOnWaitingForFile(MkpartError): + def __init__(self, part): + super().__init__( + "Waited for file {} to come to life, but timeout was " + "reached while waiting. ".format(part) + ) |