]> git.ipfire.org Git - people/ms/bricklayer.git/commitdiff
Implement passing disk images on the command line
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 8 May 2021 13:47:24 +0000 (13:47 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 8 May 2021 13:47:24 +0000 (13:47 +0000)
This is helpful for testing when a test image can be created and passed
to the installer.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/bricklayer
src/python/__init__.py
src/python/disk.py
src/python/util.py

index 0ecd26208697e60bd9312d43d1c83a8ccd168068..da9091163dfe004f7af7cbdd9aa2ef61648fbb49 100644 (file)
@@ -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()
index d62cb12414bc59e28266a0dd4e34e73f3e40bf8c..d01099419d379f62cc0af37ef24d894d4961fafe 100644 (file)
@@ -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)
index 22d3fffadf1612bd3a475f82ea871ec60aa65bef..a808b705eec2861f882e7a39585ccf4f59665fd4 100644 (file)
@@ -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)
index 7cb68687a07cf03664a29e8b969cbddece4fceab..e73c1fa387d87b004c74ce6e5a91b1ab312013a0 100644 (file)
@@ -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 ",