From: Michael Tremer Date: Fri, 7 May 2021 14:19:11 +0000 (+0000) Subject: disk: Create a fake disk in test mode X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=89990e4ca7c82f714d23a5f137ace3993d726749;p=people%2Fms%2Fbricklayer.git disk: Create a fake disk in test mode Signed-off-by: Michael Tremer --- diff --git a/src/python/disk.py b/src/python/disk.py index 5e9293f..8a64ea8 100644 --- a/src/python/disk.py +++ b/src/python/disk.py @@ -22,12 +22,15 @@ import logging import parted from . import step +from . import util from .errors import * from .i18n import _ # Setup logging log = logging.getLogger("bricklayer.disk") +TEST_DISK_SIZE = 4294967296 # 4GiB + class Disks(object): """ Disks abstraction class @@ -46,18 +49,37 @@ class Disks(object): log.debug("Scanning for disks...") - for device in parted.getAllDevices(): - disk = Disk(self.bricklayer, device) + # Perform a fake scan in test mode + if self.bricklayer.test: + self._scan_test() - # Skip whatever isn't suitable - if not disk.supported: - continue + # Otherwise scan for all devices + else: + for device in parted.getAllDevices(): + disk = Disk(self.bricklayer, device) - self.disks.append(disk) + # Skip whatever isn't suitable + if not disk.supported: + continue + + self.disks.append(disk) # Sort them alphabetically self.disks.sort() + def _scan_test(self): + """ + Fake scan in test mode and create a temporary image + """ + path = util.create_sparse_file("disk", TEST_DISK_SIZE) + + # Open the file with parted + device = parted.Device(path) + + # Create a Disk object + disk = Disk(self.bricklayer, device) + self.disks.append(disk) + @property def supported(self): """ diff --git a/src/python/util.py b/src/python/util.py index f5d26fb..9f10685 100644 --- a/src/python/util.py +++ b/src/python/util.py @@ -18,6 +18,9 @@ # # ############################################################################### +import os +import tempfile + def config_read(f): """ Read a configuration file in key/value format @@ -40,3 +43,14 @@ def config_read(f): res[key] = value return res + +def create_sparse_file(name, size): + fd, path = tempfile.mkstemp(prefix="bricklayer-", suffix="-%s" % name) + + # Truncate the file at size (to make it sparse) + os.ftruncate(fd, size) + + # Close the file immediately + os.close(fd) + + return path