def create(image, size=None, format=None, qcow2Compat=None, backing=None, backingFormat=None, preallocation=None, unsafe=False): cmd = [_qemuimg.cmd, "create"] cwdPath = None if format: cmd.extend(("-f", format)) if format == FORMAT.QCOW2: qcow2Compat = _validate_qcow2_compat(qcow2Compat) cmd.extend(('-o', 'compat=' + qcow2Compat)) if backing: if not os.path.isabs(backing): cwdPath = os.path.dirname(image) cmd.extend(("-b", backing)) if backingFormat: cmd.extend(("-F", backingFormat)) if preallocation: cmd.extend(("-o", "preallocation=" + _get_preallocation(preallocation, format))) if unsafe: cmd.append('-u') cmd.append(image) if size is not None: cmd.append(str(size)) return operation.Command(cmd, cwd=cwdPath)
def allocate(image, size, offset=0): """ Creates a new 'allocate' operation object, that will create new file and preallocate disk space for it when run. Operation can be aborted during it's execution. :param str image: filename with path :param int size: expected file size in bytes :param int offset: start allocating from that offset, specified in bytes. :return operation object, encapsulating fallocate helper call """ # This is the only sane way to run python scripts that work with both # python2 and python3 in the tests. # TODO: Remove when we drop python 2. cmd = [sys.executable, _FALLOCATE] if offset > 0: cmd.extend(("--offset", str(offset))) cmd.append(str(size)) cmd.append(image) return operation.Command(cmd)
def test_error(self): op = operation.Command( ["sh", "-c", "echo -n out >&1; echo -n err >&2; exit 1"]) with self.assertRaises(cmdutils.Error) as e: op.run() self.assertEqual(e.exception.rc, 1) self.assertEqual(e.exception.out, b"out") self.assertEqual(e.exception.err, b"err")
def test_error(self): op = operation.Command( ["sh", "-c", "echo -n out >&1; echo -n err >&2; exit 1"]) out = bytearray() with self.assertRaises(cmdutils.Error) as e: for data in op.watch(): out += data self.assertEqual(e.exception.rc, 1) self.assertEqual(e.exception.err, b"err") self.assertEqual(out, b"out")
def bitmap_update(image, bitmap, enable): cmd = [_qemuimg.cmd, "bitmap"] if enable: cmd.append("--enable") else: cmd.append("--disable") cmd.extend([image, bitmap]) cwdPath = os.path.dirname(image) return operation.Command(cmd, cwd=cwdPath)
def bitmap_merge(src_image, src_bitmap, src_fmt, dst_image, dst_bitmap): cmd = [ _qemuimg.cmd, "bitmap", "--merge", src_bitmap, "-F", src_fmt, "-b", src_image, dst_image, dst_bitmap, ] cwdPath = os.path.dirname(src_image) return operation.Command(cmd, cwd=cwdPath)
def _run_dd(path, offset, block_size, count, task): op = operation.Command([ constants.EXT_DD, "if=/dev/zero", "of=%s" % path, "bs=%d" % block_size, "count=%d" % count, "seek=%d" % offset, "oflag=direct,seek_bytes", "conv=notrunc", ]) with task.abort_callback(op.abort): op.run()
def bitmap_add(image, bitmap, enable=True, granularity=None): cmd = [_qemuimg.cmd, "bitmap", "--add"] if enable: cmd.append("--enable") else: cmd.append("--disable") cmd.extend([image, bitmap]) if granularity: cmd.extend(("-g", str(granularity))) cwdPath = os.path.dirname(image) return operation.Command(cmd, cwd=cwdPath)
def zeroout_operation(device, size): """ Returns an operation.Command object to zero a block device using "blkdiscard --zeroout". Arguments: device (str): The path to the block device to zero. size (int): The number of bytes to zero. """ return operation.Command([ _blkdiscard.cmd, "--zeroout", "--step", "%d" % OPTIMAL_DISCARD_STEP, "--length", "%d" % size, device, ])
def compare(img1, img2, img1_format=None, img2_format=None, strict=False): cmd = [_qemuimg.cmd, "compare", "-p"] if img1_format: cmd.extend(('-f', img1_format)) if img2_format: cmd.extend(('-F', img2_format)) if strict: cmd.append("-s") cmd.extend([img1, img2]) cwdPath = os.path.dirname(img1) return operation.Command(cmd, cwd=cwdPath)
def rebase(image, backing, format=None, backingFormat=None, unsafe=False): cmd = [_qemuimg.cmd, "rebase", "-t", "none", "-T", "none"] if unsafe: cmd.extend(("-u",)) if format: cmd.extend(("-f", format)) if backingFormat: cmd.extend(("-F", backingFormat)) cmd.extend(("-b", backing, image)) cwdPath = None if os.path.isabs(backing) else os.path.dirname(image) return operation.Command(cmd, cwd=cwdPath)
def test_abort_running(self): op = operation.Command(["sleep", "5"]) aborted = threading.Event() def run(): try: op.run() except exception.ActionStopped: aborted.set() t = concurrent.thread(run) t.start() try: # TODO: add way to wait until operation is stated? time.sleep(0.5) op.abort() finally: t.join() self.assertTrue(aborted.is_set())
def allocate(image, size, offset=0): """ Creates a new 'allocate' operation object, that will create new file and preallocate disk space for it when run. Operation can be aborted during it's execution. :param str image: filename with path :param int size: expected file size in bytes :param int offset: start allocating from that offset, specified in bytes. :return operation object, encapsulating fallocate helper call """ cmd = [_FALLOCATE] if offset > 0: cmd.extend(("--offset", str(offset))) cmd.append(str(size)) cmd.append(image) return operation.Command(cmd)
def test_abort_created(self): op = operation.Command(["sleep", "5"]) op.abort() with self.assertRaises(exception.ActionStopped): op.run()
def test_output(self): op = operation.Command(["echo", "-n", "out"]) out = op.run() self.assertEqual(out, b"out")
def test_run_once(self): op = operation.Command(["true"]) op.run() with self.assertRaises(RuntimeError): op.run()
def test_failure(self): op = operation.Command(["false"]) with self.assertRaises(cmdutils.Error): op.run()
def test_success(self): op = operation.Command(["true"]) op.run()
def test_abort_terminated(self): op = operation.Command(["true"]) list(op.watch()) op.abort()
def test_output(self): op = operation.Command(["echo", "-n", "out"]) out = bytearray() for data in op.watch(): out += data self.assertEqual(out, b"out")
def failing_rebase(*args, **kw): return operation.Command("/usr/bin/false")
def __init__(self, cmd, cwd=None): self._operation = operation.Command(cmd, cwd=cwd) self._progress = 0.0
def test_abort_terminated(self): op = operation.Command(["true"]) op.run() op.abort()
def test_success(self): op = operation.Command(["true"]) received = list(op.watch()) self.assertEqual(received, [])
def bitmap_remove(image, bitmap): cmd = [_qemuimg.cmd, "bitmap", "--remove", image, bitmap] cwdPath = os.path.dirname(image) return operation.Command(cmd, cwd=cwdPath)