From: Michael Tremer Date: Sat, 8 May 2021 13:47:24 +0000 (+0000) Subject: Implement passing disk images on the command line X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=5567dbe85d970c7f6c8105e92dc199347cee5645;p=people%2Fms%2Fbricklayer.git Implement passing disk images on the command line This is helpful for testing when a test image can be created and passed to the installer. Signed-off-by: Michael Tremer --- diff --git a/src/bricklayer b/src/bricklayer index 0ecd262..da90911 100644 --- a/src/bricklayer +++ b/src/bricklayer @@ -45,6 +45,8 @@ class Cli(object): help=_("Enable test mode (do not perform any actions)")) parser.add_argument("--unattended", action="store_true", help=_("Enable unattended mode")) + parser.add_argument("--disk", nargs="*", dest="disks", default=[], + help=_("A disk image file or device which will be used")) # Parse arguments return parser.parse_args() diff --git a/src/python/__init__.py b/src/python/__init__.py index d62cb12..d010994 100644 --- a/src/python/__init__.py +++ b/src/python/__init__.py @@ -39,7 +39,7 @@ class Bricklayer(object): """ Bricklayer's base class """ - def __init__(self, test=False, debug=False, unattended=False): + def __init__(self, test=False, debug=False, unattended=False, disks=[]): self.test = test self.unattended = unattended @@ -60,6 +60,8 @@ class Bricklayer(object): # Hardware self.disks = disk.Disks(self) + for path in disks: + self.disks.add_disk(path) # Initialise the text user interface self.tui = tui.Tui(self) diff --git a/src/python/disk.py b/src/python/disk.py index 22d3fff..a808b70 100644 --- a/src/python/disk.py +++ b/src/python/disk.py @@ -19,7 +19,9 @@ ############################################################################### import logging +import os import parted +import stat from . import step from . import util @@ -51,44 +53,42 @@ class Disks(object): log.debug("Scanning for disks...") - # Perform a fake scan in test mode - if self.bricklayer.test: - self._scan_test() + for device in parted.getAllDevices(): + disk = Disk(self.bricklayer, device) - # Otherwise scan for all devices - else: - for device in parted.getAllDevices(): - disk = Disk(self.bricklayer, device) - - # Skip whatever isn't suitable - if not disk.supported: - continue + # Skip whatever isn't suitable + if not disk.supported: + continue - self.disks.append(disk) + self.disks.append(disk) # Sort them alphabetically self.disks.sort() - def _scan_test(self): + def add_disk(self, path): """ - Fake scan in test mode and create a temporary image + Adds the disk at path """ - path = util.create_sparse_file("disk", TEST_DISK_SIZE) + # Check if the disk is already on the list + for disk in self.disks: + if disk.path == path: + return disk - # Connect to a loop device - loop_device = self._losetup(path) + st = os.stat(path) - # Open the file with parted - device = parted.Device(loop_device) + # Setup regular files as loop devices + if stat.S_ISREG(st.st_mode): + path = self._losetup(path) # Create a Disk object - disk = Disk(self.bricklayer, device) + disk = Disk(self.bricklayer, path) self.disks.append(disk) + return disk + def _losetup(self, path): # Find a free loop device device = self.bricklayer.command(["losetup", "-f"]) - device = device.rstrip() # Connect the image to the loop device @@ -134,6 +134,9 @@ class Disk(object): self.bricklayer = bricklayer # The parted device + if not isinstance(device, parted.Device): + device = parted.Device(device) + self.device = device # The parted disk (with a blank partition table) diff --git a/src/python/util.py b/src/python/util.py index 7cb6868..e73c1fa 100644 --- a/src/python/util.py +++ b/src/python/util.py @@ -18,9 +18,6 @@ # # ############################################################################### -import os -import tempfile - def config_read(f): """ Read a configuration file in key/value format @@ -44,17 +41,6 @@ def config_read(f): 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 - def format_size(s): units = ( "%.0f ",