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()
"""
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
# 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)
###############################################################################
import logging
+import os
import parted
+import stat
from . import step
from . import util
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
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)
# #
###############################################################################
-import os
-import tempfile
-
def config_read(f):
"""
Read a configuration file in key/value format
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 ",