When creating a new backing file, we can optionally fully claim its storage in the fs by writing to all of it, or simply seek to the end and write a byte, in which case the fs may allocate it as a sparse file. Signed-off-by: Andy Grover <agrover@xxxxxxxxxx> --- targetcli/ui_backstore.py | 43 +++++++++++++++++++++++++++++-------------- 1 files changed, 29 insertions(+), 14 deletions(-) diff --git a/targetcli/ui_backstore.py b/targetcli/ui_backstore.py index ad25732..b8f6963 100644 --- a/targetcli/ui_backstore.py +++ b/targetcli/ui_backstore.py @@ -226,7 +226,26 @@ class UIFileIOBackstore(UIBackstore): def __init__(self, parent): UIBackstore.__init__(self, 'fileio', parent) - def ui_command_create(self, name, file_or_dev, size=None, buffered=None): + def _create_file(self, filename, size, sparse=True): + f = open(filename, "w+") + try: + if sparse: + f.seek(size-1) + f.write("\0") + else: + self.shell.log.info("Writing %s bytes" % size) + while size > 0: + write_size = min(size, 1024) + f.write("\0" * write_size) + size -= write_size + except IOError: + f.close() + os.remove(filename) + raise ExecutionError("Could not expand file to size") + f.close() + + def ui_command_create(self, name, file_or_dev, size=None, buffered=None, + sparse=None): ''' Creates a FileIO storage object. If I{file_or_dev} is a path to a regular file to be used as backend, then the I{size} parameter is @@ -235,7 +254,10 @@ class UIFileIOBackstore(UIBackstore): the file to be used, I{file} the path to the file or I{dev} the path to a block device. The I{buffered} parameter is a boolean stating whether or not to enable buffered mode. It is disabled by default - (synchronous mode). + (synchronous mode). The I{sparse} parameter is only applicable when + creating a new backing file. It is a boolean stating if the + created file should be created as a sparse file (the default), or + fully initialized. SIZE SYNTAX =========== @@ -249,8 +271,10 @@ class UIFileIOBackstore(UIBackstore): ''' self.assert_root() self.assert_available_so_name(name) - self.shell.log.debug("Using params size=%s buffered=%s" - % (size, buffered)) + self.shell.log.debug("Using params size=%s buffered=%s sparse=%s" + % (size, buffered, sparse)) + + sparse = self.ui_eval_param(sparse, 'bool', True) backstore = FileIOBackstore(self.next_hba_index(), mode='create') @@ -275,16 +299,7 @@ class UIFileIOBackstore(UIBackstore): if not size: raise ExecutionError("Attempting to create file for new" + " fileio backstore, need a size") - - f = open(file_or_dev, "w+") - try: - f.seek(human_to_bytes(size)-1) - f.write('\0') - except IOError: - f.close() - os.remove(file_or_dev) - raise ExecutionError("Could not expand file to size") - f.close() + self._create_file(file_or_dev, human_to_bytes(size), sparse) try: so = FileIOStorageObject( -- 1.7.1 -- To unsubscribe from this list: send the line "unsubscribe target-devel" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html