def test_full(self): with namedTemporaryDir() as tmpdir: filename = os.path.join(tmpdir, 'test') with io.open(filename, "wb") as f: f.write(b"x" * qcow2.CLUSTER_SIZE * 3) runs = qemuimg.map(filename) self.assertEqual(qcow2.count_clusters(runs), 3)
def test_empty_sparse(self): with namedTemporaryDir() as tmpdir: filename = os.path.join(tmpdir, 'test') with io.open(filename, "wb") as f: f.truncate(MB) runs = qemuimg.map(filename) self.assertEqual(qcow2.count_clusters(runs), 0)
def test_one_block(self, offset, length, expected_length, qcow2_compat): with namedTemporaryDir() as tmpdir: size = 1048576 image = os.path.join(tmpdir, "base.img") qemuimg.create(image, size=size, format=self.FORMAT, qcow2Compat=qcow2_compat) qemu_pattern_write(image, self.FORMAT, offset=offset, len=length, pattern=0xf0) expected = [ # run 1 - empty { "start": 0, "length": offset, "data": False, "zero": True, }, # run 2 - data { "start": offset, "length": expected_length, "data": True, "zero": False, }, # run 3 - empty { "start": offset + expected_length, "length": size - offset - expected_length, "data": False, "zero": True, }, ] self.check_map(qemuimg.map(image), expected)
def test_multiple_blocks(self): with namedTemporaryDir() as tmpdir: filename = os.path.join(tmpdir, 'test') with io.open(filename, "wb") as f: f.write(b"x") f.seek(16 * 1024) f.write(b"x") f.seek(42 * 1024) f.write(b"x") runs = qemuimg.map(filename) self.assertEqual(qcow2.count_clusters(runs), 1)
def test_big_sparse(self): with namedTemporaryDir() as tmpdir: filename = os.path.join(tmpdir, 'test') with io.open(filename, "wb") as f: f.truncate(1024 * MB) # First cluster f.write(b"x") # Second cluster f.seek(512 * MB) f.write(b"x") runs = qemuimg.map(filename) self.assertEqual(qcow2.count_clusters(runs), 2)
def test_empty_image(self, qcow2_compat): with namedTemporaryDir() as tmpdir: size = 1048576 image = os.path.join(tmpdir, "base.img") qemuimg.create(image, size=size, format=self.FORMAT, qcow2Compat=qcow2_compat) expected = [ # single run - empty { "start": 0, "length": size, "data": False, "zero": True, }, ] self.check_map(qemuimg.map(image), expected)
def estimate_size(filename): """ Estimating qcow2 file size once converted from raw to qcow2. The filename is a path (sparse or preallocated), or a path to preallocated block device. """ info = qemuimg.info(filename) if (info['format'] != qemuimg.FORMAT.RAW): raise ValueError("Estimate size is only supported for raw format. file" " %s is with format %s" % (filename, info['format'])) # Get used clusters and virtual size of destination volume. virtual_size = info['virtualsize'] meta_size = _estimate_metadata_size(virtual_size) runs = qemuimg.map(filename) used_clusters = count_clusters(runs) # Return the estimated size. return meta_size + used_clusters * CLUSTER_SIZE
def test_partial(self): with namedTemporaryDir() as tmpdir: filename = os.path.join(tmpdir, 'test') with io.open(filename, "wb") as f: f.truncate(MB) # First cluster f.seek(8192) f.write(b"x") # Second cluster f.seek(qcow2.CLUSTER_SIZE) f.write(b"x") f.seek(qcow2.CLUSTER_SIZE * 2 - 1) f.write(b"x") # Third cluster f.seek(qcow2.CLUSTER_SIZE * 2) f.write(b"x") runs = qemuimg.map(filename) self.assertEqual(qcow2.count_clusters(runs), 3)