diff options
author | Lars Wirzenius <liw@liw.fi> | 2020-04-12 11:44:04 +0000 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2020-04-12 11:44:04 +0000 |
commit | 5ec8a0e5cadd31473c8f44dd703ccf25d73845f9 (patch) | |
tree | 371dac57af727e1596a72138dee54aaf0bcdd04c | |
parent | 0f8cf7d39927bd6ae3948b623af9acfecddfe90d (diff) | |
parent | 0c67f0365ad81441e99ee941b64e0fb80ae2396d (diff) | |
download | vmdb2-5ec8a0e5cadd31473c8f44dd703ccf25d73845f9.tar.gz |
Merge branch 'waitdev' into 'master'
Fix: wait for /dev entry for new real partition to exist in mkpart
Closes #3
See merge request larswirzenius/vmdb2!17
-rw-r--r-- | NEWS | 4 | ||||
-rw-r--r-- | vmdb/plugins/mkpart_plugin.py | 46 |
2 files changed, 47 insertions, 3 deletions
@@ -19,6 +19,10 @@ Version 0.14.1+git, not yet released This change should NOT require changes to existing, working .vmdb files. +* The `mkpart` step now waits for the new partition device to exist + before it's finished. This only applies when the target is a real + disk, not a disk image. + Version 0.14.1, not yet released ------------------------------------ diff --git a/vmdb/plugins/mkpart_plugin.py b/vmdb/plugins/mkpart_plugin.py index 6b37d8c..bdd0159 100644 --- a/vmdb/plugins/mkpart_plugin.py +++ b/vmdb/plugins/mkpart_plugin.py @@ -19,6 +19,7 @@ import os import stat +import time import cliapp @@ -54,12 +55,28 @@ class MkpartStepRunner(vmdb.StepRunnerInterface): orig = self.list_partitions(device) vmdb.runcmd(['parted', '-s', device, 'mkpart', part_type, fs_type, start, end]) + new = self.list_partitions(device) + diff = self.diff_partitions(orig, new) + + if len(diff) == 0: + raise ExpectedNewPartition() + + if len(diff) > 1: + raise UnexpectedNewPartitions(diff) state.tags.append(tag) + + # If device is a real block device (e.g, /dev/sdb), the + # parition we have in diff are also real devices (e.g., + # /dev/sdb1), and we should remember those in tags. + # + # If, however, device is a disk image file (e.g, foo.img), the + # partition in diff is not a device file but something like + # foo.img1. We don't need to remember that in tags. The user + # will use the kpartx step later to add those partitions into + # tags. if self.is_block_dev(device): - new = self.list_partitions(device) - diff = self.diff_partitions(orig, new) - assert len(diff) == 1 + self.wait_for_file_to_exist(diff[0]) vmdb.progress('remembering partition {} as {}'.format(diff[0], tag)) state.tags.set_dev(tag, diff[0]) @@ -86,3 +103,26 @@ class MkpartStepRunner(vmdb.StepRunnerInterface): for line in new if line not in old ] + + def wait_for_file_to_exist(self, filename): + while not os.file.exists(filename): + time.sleep(1) + + +class MkpartError(cliapp.AppException): + + pass + + +class ExpectedNewPartition(MkpartError): + + def __init__(self): + super().__init__('Expected a new partition to exist after mkpart') + + +class UnexpectedNewPartitions(MkpartError): + + def __init__(self, diff): + super().__init__( + 'Expected only one new partition to exist after mkpart, ' + 'but found {}'.format(' '.join(diff))) |