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
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):
"""
# #
###############################################################################
+import os
+import tempfile
+
def config_read(f):
"""
Read a configuration file in key/value format
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