Skip to contentSkip to conversation

block: block-backup live backup command

PATCH Superseded Stefan Hajnoczi 5 participants 17 messages Apr 23, 2013 – May 1, 2013 Block layer

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#1 lore raw

PATCH 1/3 block: add basic backup support to block driver

In reply to Stefan Hajnoczi

From: Dietmar Maurer <[email protected]>

backup_start() creates a block job that copies a point-in-time snapshot of a block device to a target block device.

We call backup_do_cow() for each write during backup. That function reads the original data from the block device before it gets overwritten. The data is then written to the target device.

The tracked_request infrastructure is used to serialize access. Both reads and writes are serialized if they overlap.

Currently backup cluster size is hardcoded to 65536 bytes.

[I made a number of changes to Dietmar's original patch and folded them in to make code review easy. Here is the full list:

* Drop BackupDumpFunc interface in favor of a target block device
* Detect zero clusters with buffer_is_zero()
* Don't write zero clusters to the target
* Use 0 delay instead of 1us, like other block jobs
* Unify creation/start functions into backup_start()
* Simplify cleanup, free bitmap in backup_run() instead of cb function
* Use HBitmap to avoid duplicating bitmap code
* Use bdrv_getlength() instead of accessing ->total_sectors directly
* Delete the backup.h header file, it is no longer necessary
* Move ./backup.c to block/backup.c
* Remove #ifdefed out code
* Coding style and whitespace cleanups

-- stefanha]

---
 block.c                   |  69 ++++++++++++-
 block/Makefile.objs       |   1 +
 block/backup.c            | 252 ++++++++++++++++++++++++++++++++++++++++++++++
 include/block/block.h     |   2 +
 include/block/block_int.h |  16 +++
 include/block/blockjob.h  |  10 ++
 6 files changed, 345 insertions(+), 5 deletions(-)
 create mode 100644 block/backup.c
block.c +64 -5
index aa9a533..c5c09b7 100644
@@ -54,6 +54,7 @@
 typedef enum {
     BDRV_REQ_COPY_ON_READ = 0x1,
     BDRV_REQ_ZERO_WRITE   = 0x2,
+    BDRV_REQ_BACKUP_ONLY  = 0x4,
 } BdrvRequestFlags;
 
 static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load);
@@ -1902,6 +1903,22 @@ void bdrv_round_to_clusters(BlockDriverState *bs,
     }
 }
 
+/**
+ * Round a region to job cluster boundaries
+ */
+static void round_to_job_clusters(BlockDriverState *bs,
+                                  int64_t sector_num, int nb_sectors,
+                                  int job_cluster_size,
+                                  int64_t *cluster_sector_num,
+                                  int *cluster_nb_sectors)
+{
+    int64_t c = job_cluster_size / BDRV_SECTOR_SIZE;
+
+    *cluster_sector_num = QEMU_ALIGN_DOWN(sector_num, c);
+    *cluster_nb_sectors = QEMU_ALIGN_UP(sector_num - *cluster_sector_num +
+                                        nb_sectors, c);
+}
+
 static bool tracked_request_overlaps(BdrvTrackedRequest *req,
                                      int64_t sector_num, int nb_sectors) {
     /*        aaaa   bbbb */
@@ -1916,7 +1933,9 @@ static bool tracked_request_overlaps(BdrvTrackedRequest *req,
 }
 
 static void coroutine_fn wait_for_overlapping_requests(BlockDriverState *bs,
-        int64_t sector_num, int nb_sectors)
+                                                       int64_t sector_num,
+                                                       int nb_sectors,
+                                                       int job_cluster_size)
 {
     BdrvTrackedRequest *req;
     int64_t cluster_sector_num;
@@ -1932,6 +1951,11 @@ static void coroutine_fn wait_for_overlapping_requests(BlockDriverState *bs,
     bdrv_round_to_clusters(bs, sector_num, nb_sectors,
                            &cluster_sector_num, &cluster_nb_sectors);
 
+    if (job_cluster_size) {
+        round_to_job_clusters(bs, sector_num, nb_sectors, job_cluster_size,
+                              &cluster_sector_num, &cluster_nb_sectors);
+    }
+
     do {
         retry = false;
         QLIST_FOREACH(req, &bs->tracked_requests, list) {
@@ -2507,12 +2531,24 @@ static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs,
         bs->copy_on_read_in_flight++;
     }
 
-    if (bs->copy_on_read_in_flight) {
-        wait_for_overlapping_requests(bs, sector_num, nb_sectors);
+    int job_cluster_size = bs->job && bs->job->cluster_size ?
+        bs->job->cluster_size : 0;
+
+    if (bs->copy_on_read_in_flight || job_cluster_size) {
+        wait_for_overlapping_requests(bs, sector_num, nb_sectors,
+                                      job_cluster_size);
     }
 
     tracked_request_begin(&req, bs, sector_num, nb_sectors, false);
 
+    if (bs->job && bs->job->job_type->before_read) {
+        ret = bs->job->job_type->before_read(bs, sector_num, nb_sectors, qiov);
+        if ((ret < 0) || (flags & BDRV_REQ_BACKUP_ONLY)) {
+            /* Note: We do not return any data to the caller */
+            goto out;
+        }
+    }
+
     if (flags & BDRV_REQ_COPY_ON_READ) {
         int pnum;
 
@@ -2556,6 +2592,17 @@ int coroutine_fn bdrv_co_copy_on_readv(BlockDriverState *bs,
                             BDRV_REQ_COPY_ON_READ);
 }
 
+int coroutine_fn bdrv_co_backup(BlockDriverState *bs,
+    int64_t sector_num, int nb_sectors)
+{
+    if (!bs->job) {
+        return -ENOTSUP;
+    }
+
+    return bdrv_co_do_readv(bs, sector_num, nb_sectors, NULL,
+                            BDRV_REQ_BACKUP_ONLY);
+}
+
 static int coroutine_fn bdrv_co_do_write_zeroes(BlockDriverState *bs,
     int64_t sector_num, int nb_sectors)
 {
@@ -2613,12 +2660,23 @@ static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs,
         bdrv_io_limits_intercept(bs, true, nb_sectors);
     }
 
-    if (bs->copy_on_read_in_flight) {
-        wait_for_overlapping_requests(bs, sector_num, nb_sectors);
+    int job_cluster_size = bs->job && bs->job->cluster_size ?
+        bs->job->cluster_size : 0;
+
+    if (bs->copy_on_read_in_flight || job_cluster_size) {
+        wait_for_overlapping_requests(bs, sector_num, nb_sectors,
+                                      job_cluster_size);
     }
 
     tracked_request_begin(&req, bs, sector_num, nb_sectors, true);
 
+    if (bs->job && bs->job->job_type->before_write) {
+        ret = bs->job->job_type->before_write(bs, sector_num, nb_sectors, qiov);
+        if (ret < 0) {
+            goto out;
+        }
+    }
+
     if (flags & BDRV_REQ_ZERO_WRITE) {
         ret = bdrv_co_do_write_zeroes(bs, sector_num, nb_sectors);
     } else {
@@ -2637,6 +2695,7 @@ static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs,
         bs->wr_highest_sector = sector_num + nb_sectors - 1;
     }
 
+out:
     tracked_request_end(&req);
 
     return ret;
block/Makefile.objs +1 -0
index 6c4b5bc..dcab6d3 100644
@@ -19,5 +19,6 @@ endif
 common-obj-y += stream.o
 common-obj-y += commit.o
 common-obj-y += mirror.o
+common-obj-y += backup.o
 
 $(obj)/curl.o: QEMU_CFLAGS+=$(CURL_CFLAGS)
block/backup.c new file +252 -0

252 lines not on this page · raw message

include/block/block.h +2 -0
index 1251c5c..19a41f4 100644
@@ -178,6 +178,8 @@ int coroutine_fn bdrv_co_readv(BlockDriverState *bs, int64_t sector_num,
     int nb_sectors, QEMUIOVector *qiov);
 int coroutine_fn bdrv_co_copy_on_readv(BlockDriverState *bs,
     int64_t sector_num, int nb_sectors, QEMUIOVector *qiov);
+int coroutine_fn bdrv_co_backup(BlockDriverState *bs,
+    int64_t sector_num, int nb_sectors);
 int coroutine_fn bdrv_co_writev(BlockDriverState *bs, int64_t sector_num,
     int nb_sectors, QEMUIOVector *qiov);
 /*
include/block/block_int.h +16 -0
index 6078dd3..bd51abf 100644
@@ -377,4 +377,20 @@ void mirror_start(BlockDriverState *bs, BlockDriverState *target,
                   BlockDriverCompletionFunc *cb,
                   void *opaque, Error **errp);
 
+/*
+ * backup_start:
+ * @bs: Block device to operate on.
+ * @target: Block device to write to.
+ * @speed: The maximum speed, in bytes per second, or 0 for unlimited.
+ * @cb: Completion function for the job.
+ * @opaque: Opaque pointer value passed to @cb.
+ *
+ * Start a backup operation on @bs.  Clusters in @bs are written to @target
+ * until the job is cancelled or manually completed.
+ */
+void backup_start(BlockDriverState *bs, BlockDriverState *target,
+                  int64_t speed,
+                  BlockDriverCompletionFunc *cb, void *opaque,
+                  Error **errp);
+
 #endif /* BLOCK_INT_H */
include/block/blockjob.h +10 -0
index c290d07..6f42495 100644
@@ -50,6 +50,13 @@ typedef struct BlockJobType {
      * manually.
      */
     void (*complete)(BlockJob *job, Error **errp);
+
+    /** tracked requests */
+    int coroutine_fn (*before_read)(BlockDriverState *bs, int64_t sector_num,
+                                    int nb_sectors, QEMUIOVector *qiov);
+    int coroutine_fn (*before_write)(BlockDriverState *bs, int64_t sector_num,
+                                     int nb_sectors, QEMUIOVector *qiov);
+
 } BlockJobType;
 
 /**
@@ -103,6 +110,9 @@ struct BlockJob {
     /** Speed that was set with @block_job_set_speed.  */
     int64_t speed;
 
+    /** tracked requests */
+    int cluster_size;
+
     /** The completion function that will be called when the job completes.  */
     BlockDriverCompletionFunc *cb;
 
Signature · 1 line
1.8.1.4
#0 lore raw

cover block: block-backup live backup command

This series adds a new QMP command, block-backup, which takes a point-in-time snapshot of a block device. The snapshot is copied out to a target block device. A simple example is:

block-backup device=virtio0 format=qcow2 target=backup-20130401.qcow2

The original block-backup blockjob was written by Dietmar Maurer <[email protected]>. He is currently busy but I feel the feature is worth pushing into QEMU since there has been interest. This is my version of his patch, plus the QMP command and qemu-iotests test case.

How is this different from block-stream and drive-mirror?
---------------------------------------------------------
Both block-stream and drive-mirror do not provide immediate point-in-time snapshots. Instead they copy data into a new file and then switch to it. In other words, the point at which the "snapshot" is taken cannot be controlled directly.

block-backup intercepts guest writes and saves data into the target block device before it is overwritten. The target block device can be a raw image file, backing files are not used to implement this feature.

How can block-backup be used?
-----------------------------
The simplest use-case is to copy a point-in-time snapshot to a local file.

More advanced users may wish to make the target an NBD URL. The NBD server listening on the other side can process the backup writes any way it wishes. I previously posted an RFC series with a backup server that streamed Dietmar's VMA backup archive format.

What's next for block-backup?
-----------------------------
The following enhancements are left for future patches:

1. QMP 'transaction' support. It is handy to atomically snapshot multiple block devices. We need qmp_transaction() support for this. Wenchao Xia is currently making qmp_transaction() extensible so new action types, like block-backup, can be added.

2. Sync modes like drive-mirror (top, full, none). This makes it possible to preserve the backing file chain.

Dietmar Maurer (1):
block: add basic backup support to block driver

Stefan Hajnoczi (2):
block: add block_backup QMP command
qemu-iotests: add 054 block-backup test case

 block.c                    |  69 ++++++++++++-
 block/Makefile.objs        |   1 +
 block/backup.c             | 252 +++++++++++++++++++++++++++++++++++++++++++++
 blockdev.c                 |  92 +++++++++++++++++
 include/block/block.h      |   2 +
 include/block/block_int.h  |  16 +++
 include/block/blockjob.h   |  10 ++
 qapi-schema.json           |  28 +++++
 qmp-commands.hx            |   6 ++
 tests/qemu-iotests/054     | 230 +++++++++++++++++++++++++++++++++++++++++
 tests/qemu-iotests/054.out |   5 +
 tests/qemu-iotests/group   |   1 +
 12 files changed, 707 insertions(+), 5 deletions(-)
 create mode 100644 block/backup.c
 create mode 100755 tests/qemu-iotests/054
 create mode 100644 tests/qemu-iotests/054.out
Signature · 1 line
1.8.1.4
#10 lore raw

PATCH 3/3 qemu-iotests: add 054 block-backup test case

Testing block-backup is similar to image streaming and drive mirroring. This test case is based on 041.

---
 tests/qemu-iotests/054     | 230 +++++++++++++++++++++++++++++++++++++++++++++
 tests/qemu-iotests/054.out |   5 +
 tests/qemu-iotests/group   |   1 +
 3 files changed, 236 insertions(+)
 create mode 100755 tests/qemu-iotests/054
 create mode 100644 tests/qemu-iotests/054.out
tests/qemu-iotests/054 new file +230 -0
new file mode 100755
index 0000000..506bf05
@@ -0,0 +1,230 @@
+#!/usr/bin/env python
+#
+# Tests for block-backup
+#
+# Copyright (C) 2013 Red Hat, Inc.
+#
+# Based on 041.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+
+import time
+import os
+import iotests
+from iotests import qemu_img, qemu_io
+
+test_img = os.path.join(iotests.test_dir, 'test.img')
+target_img = os.path.join(iotests.test_dir, 'target.img')
+
+class BlockBackupTestCase(iotests.QMPTestCase):
+    '''Abstract base class for block-backup test cases'''
+
+    def assert_no_active_backups(self):
+        result = self.vm.qmp('query-block-jobs')
+        self.assert_qmp(result, 'return', [])
+
+    def cancel_and_wait(self, drive='drive0'):
+        '''Cancel a block job and wait for it to finish'''
+        result = self.vm.qmp('block-job-cancel', device=drive)
+        self.assert_qmp(result, 'return', {})
+
+        cancelled = False
+        while not cancelled:
+            for event in self.vm.get_qmp_events(wait=True):
+                if event['event'] == 'BLOCK_JOB_COMPLETED' or \
+                   event['event'] == 'BLOCK_JOB_CANCELLED':
+                    self.assert_qmp(event, 'data/type', 'backup')
+                    self.assert_qmp(event, 'data/device', drive)
+                    cancelled = True
+
+        self.assert_no_active_backups()
+
+    def complete_and_wait(self):
+        completed = False
+        while not completed:
+            for event in self.vm.get_qmp_events(wait=True):
+                if event['event'] == 'BLOCK_JOB_COMPLETED':
+                    self.assert_qmp(event, 'data/type', 'backup')
+                    self.assert_qmp(event, 'data/device', 'drive0')
+                    self.assert_qmp(event, 'data/offset', self.image_len)
+                    self.assert_qmp(event, 'data/len', self.image_len)
+                    completed = True
+        self.assert_no_active_backups()
+
+    def compare_images(self, img1, img2):
+        try:
+            qemu_img('convert', '-f', iotests.imgfmt, '-O', 'raw', img1, img1 + '.raw')
+            qemu_img('convert', '-f', iotests.imgfmt, '-O', 'raw', img2, img2 + '.raw')
+            file1 = open(img1 + '.raw', 'r')
+            file2 = open(img2 + '.raw', 'r')
+            return file1.read() == file2.read()
+        finally:
+            if file1 is not None:
+                file1.close()
+            if file2 is not None:
+                file2.close()
+            try:
+                os.remove(img1 + '.raw')
+            except OSError:
+                pass
+            try:
+                os.remove(img2 + '.raw')
+            except OSError:
+                pass
+
+class TestSingleDrive(BlockBackupTestCase):
+    image_len = 120 * 1024 * 1024 # MB
+
+    def setUp(self):
+        qemu_img('create', '-f', iotests.imgfmt, test_img, str(TestSingleDrive.image_len))
+        self.vm = iotests.VM().add_drive(test_img)
+        self.vm.launch()
+
+    def tearDown(self):
+        self.vm.shutdown()
+        os.remove(test_img)
+        try:
+            os.remove(target_img)
+        except OSError:
+            pass
+
+    def test_complete(self):
+        self.assert_no_active_backups()
+
+        result = self.vm.qmp('block-backup', device='drive0',
+                             target=target_img)
+        self.assert_qmp(result, 'return', {})
+
+        self.complete_and_wait()
+        self.vm.shutdown()
+        self.assertTrue(self.compare_images(test_img, target_img),
+                        'target image does not match source after backup')
+
+    def test_cancel(self):
+        self.assert_no_active_backups()
+
+        result = self.vm.qmp('block-backup', device='drive0',
+                             target=target_img)
+        self.assert_qmp(result, 'return', {})
+
+        self.cancel_and_wait()
+
+    def test_pause(self):
+        self.assert_no_active_backups()
+
+        result = self.vm.qmp('block-backup', device='drive0',
+                             target=target_img)
+        self.assert_qmp(result, 'return', {})
+
+        result = self.vm.qmp('block-job-pause', device='drive0')
+        self.assert_qmp(result, 'return', {})
+
+        time.sleep(1)
+        result = self.vm.qmp('query-block-jobs')
+        offset = self.dictpath(result, 'return[0]/offset')
+
+        time.sleep(1)
+        result = self.vm.qmp('query-block-jobs')
+        self.assert_qmp(result, 'return[0]/offset', offset)
+
+        result = self.vm.qmp('block-job-resume', device='drive0')
+        self.assert_qmp(result, 'return', {})
+
+        self.complete_and_wait()
+        self.vm.shutdown()
+        self.assertTrue(self.compare_images(test_img, target_img),
+                        'target image does not match source after backup')
+
+    def test_medium_not_found(self):
+        result = self.vm.qmp('block-backup', device='ide1-cd0',
+                             target=target_img)
+        self.assert_qmp(result, 'error/class', 'GenericError')
+
+    def test_image_not_found(self):
+        result = self.vm.qmp('block-backup', device='drive0',
+                             mode='existing', target=target_img)
+        self.assert_qmp(result, 'error/class', 'GenericError')
+
+    def test_device_not_found(self):
+        result = self.vm.qmp('block-backup', device='nonexistent',
+                             target=target_img)
+        self.assert_qmp(result, 'error/class', 'DeviceNotFound')
+
+class TestSetSpeed(BlockBackupTestCase):
+    image_len = 80 * 1024 * 1024 # MB
+
+    def setUp(self):
+        qemu_img('create', '-f', iotests.imgfmt, test_img, str(TestSetSpeed.image_len))
+        self.vm = iotests.VM().add_drive(test_img)
+        self.vm.launch()
+
+    def tearDown(self):
+        self.vm.shutdown()
+        os.remove(test_img)
+        os.remove(target_img)
+
+    def test_set_speed(self):
+        self.assert_no_active_backups()
+
+        result = self.vm.qmp('block-backup', device='drive0',
+                             target=target_img)
+        self.assert_qmp(result, 'return', {})
+
+        # Default speed is 0
+        result = self.vm.qmp('query-block-jobs')
+        self.assert_qmp(result, 'return[0]/device', 'drive0')
+        self.assert_qmp(result, 'return[0]/speed', 0)
+
+        result = self.vm.qmp('block-job-set-speed', device='drive0', speed=8 * 1024 * 1024)
+        self.assert_qmp(result, 'return', {})
+
+        # Ensure the speed we set was accepted
+        result = self.vm.qmp('query-block-jobs')
+        self.assert_qmp(result, 'return[0]/device', 'drive0')
+        self.assert_qmp(result, 'return[0]/speed', 8 * 1024 * 1024)
+
+        self.cancel_and_wait()
+
+        # Check setting speed in block-backup works
+        result = self.vm.qmp('block-backup', device='drive0',
+                             target=target_img, speed=4*1024*1024)
+        self.assert_qmp(result, 'return', {})
+
+        result = self.vm.qmp('query-block-jobs')
+        self.assert_qmp(result, 'return[0]/device', 'drive0')
+        self.assert_qmp(result, 'return[0]/speed', 4 * 1024 * 1024)
+
+        self.cancel_and_wait()
+
+    def test_set_speed_invalid(self):
+        self.assert_no_active_backups()
+
+        result = self.vm.qmp('block-backup', device='drive0',
+                             target=target_img, speed=-1)
+        self.assert_qmp(result, 'error/class', 'GenericError')
+
+        self.assert_no_active_backups()
+
+        result = self.vm.qmp('block-backup', device='drive0',
+                             target=target_img)
+        self.assert_qmp(result, 'return', {})
+
+        result = self.vm.qmp('block-job-set-speed', device='drive0', speed=-1)
+        self.assert_qmp(result, 'error/class', 'GenericError')
+
+        self.cancel_and_wait()
+
+if __name__ == '__main__':
+    iotests.main(supported_fmts=['raw', 'qcow2'])
tests/qemu-iotests/054.out new file +5 -0
new file mode 100644
index 0000000..594c16f
@@ -0,0 +1,5 @@
+........
+----------------------------------------------------------------------
+Ran 8 tests
+
+OK
tests/qemu-iotests/group +1 -0
index 68eabda..f487a8f 100644
@@ -60,3 +60,4 @@
 051 rw auto
 052 rw auto backing
 053 rw auto
+054 rw auto
Signature · 1 line
1.8.1.4
#2 lore raw

PATCH 2/3 block: add block_backup QMP command

In reply to Stefan Hajnoczi

@block-backup

Start a point-in-time copy of a block device to a new destination.

@device: the name of the device whose writes should be mirrored.

@target: the target of the new image. If the file exists, or if it
is a device, the existing file/device will be used as the new destination. If it does not exist, a new file will be created.

@format: #optional the format of the new destination, default is to
probe if @mode is 'existing', else the format of the source

@mode: #optional whether and how QEMU should create a new image, default is
'absolute-paths'.

@speed: #optional the maximum speed, in bytes per second

Returns: nothing on success
If @device is not a valid block device, DeviceNotFound

---
 blockdev.c       | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 qapi-schema.json | 28 +++++++++++++++++
 qmp-commands.hx  |  6 ++++
 3 files changed, 126 insertions(+)
blockdev.c +92 -0
index 8a1652b..521d999 100644
@@ -1279,6 +1279,98 @@ void qmp_block_commit(const char *device,
     drive_get_ref(drive_get_by_blockdev(bs));
 }
 
+void qmp_block_backup(const char *device, const char *target,
+                      bool has_format, const char *format,
+                      bool has_mode, enum NewImageMode mode,
+                      bool has_speed, int64_t speed,
+                      Error **errp)
+{
+    BlockDriverState *bs;
+    BlockDriverState *target_bs;
+    BlockDriver *proto_drv;
+    BlockDriver *drv = NULL;
+    Error *local_err = NULL;
+    int flags;
+    uint64_t size;
+    int ret;
+
+    if (!has_speed) {
+        speed = 0;
+    }
+    if (!has_mode) {
+        mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
+    }
+
+    bs = bdrv_find(device);
+    if (!bs) {
+        error_set(errp, QERR_DEVICE_NOT_FOUND, device);
+        return;
+    }
+
+    if (!bdrv_is_inserted(bs)) {
+        error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device);
+        return;
+    }
+
+    if (!has_format) {
+        format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name;
+    }
+    if (format) {
+        drv = bdrv_find_format(format);
+        if (!drv) {
+            error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
+            return;
+        }
+    }
+
+    if (bdrv_in_use(bs)) {
+        error_set(errp, QERR_DEVICE_IN_USE, device);
+        return;
+    }
+
+    flags = bs->open_flags | BDRV_O_RDWR;
+
+    proto_drv = bdrv_find_protocol(target);
+    if (!proto_drv) {
+        error_set(errp, QERR_INVALID_BLOCK_FORMAT, format);
+        return;
+    }
+
+    bdrv_get_geometry(bs, &size);
+    size *= 512;
+    if (mode != NEW_IMAGE_MODE_EXISTING) {
+        assert(format && drv);
+        bdrv_img_create(target, format,
+                        NULL, NULL, NULL, size, flags, &local_err, false);
+    }
+
+    if (error_is_set(&local_err)) {
+        error_propagate(errp, local_err);
+        return;
+    }
+
+    target_bs = bdrv_new("");
+    ret = bdrv_open(target_bs, target, NULL, flags, drv);
+
+    if (ret < 0) {
+        bdrv_delete(target_bs);
+        error_set(errp, QERR_OPEN_FILE_FAILED, target);
+        return;
+    }
+
+    backup_start(bs, target_bs, speed, block_job_cb, bs, &local_err);
+    if (local_err != NULL) {
+        bdrv_delete(target_bs);
+        error_propagate(errp, local_err);
+        return;
+    }
+
+    /* Grab a reference so hotplug does not delete the BlockDriverState from
+     * underneath us.
+     */
+    drive_get_ref(drive_get_by_blockdev(bs));
+}
+
 #define DEFAULT_MIRROR_BUF_SIZE   (10 << 20)
 
 void qmp_drive_mirror(const char *device, const char *target,
qapi-schema.json +28 -0
index 751d3c2..903d2a5 100644
@@ -1715,6 +1715,34 @@
             '*speed': 'int' } }
 
 ##
+# @block-backup
+#
+# Start a point-in-time copy of a block device to a new destination.
+#
+# @device:  the name of the device whose writes should be mirrored.
+#
+# @target: the target of the new image. If the file exists, or if it
+#          is a device, the existing file/device will be used as the new
+#          destination.  If it does not exist, a new file will be created.
+#
+# @format: #optional the format of the new destination, default is to
+#          probe if @mode is 'existing', else the format of the source
+#
+# @mode: #optional whether and how QEMU should create a new image, default is
+#        'absolute-paths'.
+#
+# @speed:  #optional the maximum speed, in bytes per second
+#
+# Returns: nothing on success
+#          If @device is not a valid block device, DeviceNotFound
+#
+# Since 1.5
+##
+{ 'command': 'block-backup',
+  'data': { 'device': 'str', 'target': 'str', '*format': 'str',
+            '*mode': 'NewImageMode', '*speed': 'int' } }
+
+##
 # @drive-mirror
 #
 # Start mirroring a block device's writes to a new destination.
qmp-commands.hx +6 -0
index 4d65422..ce73e44 100644
@@ -889,6 +889,12 @@ EQMP
     },
 
     {
+        .name       = "block-backup",
+        .args_type  = "device:B,target:s,speed:i?,mode:s?,format:s?",
+        .mhandler.cmd_new = qmp_marshal_input_block_backup,
+    },
+
+    {
         .name       = "block-job-set-speed",
         .args_type  = "device:B,speed:o",
         .mhandler.cmd_new = qmp_marshal_input_block_job_set_speed,
Signature · 1 line
1.8.1.4
#11 lore raw
In reply to Stefan Hajnoczi
Quoted 10 lines · Stefan Hajnoczi

This series adds a new QMP command, block-backup, which takes a point-in-time snapshot of a block device. The snapshot is copied out to a target block device. A simple example is:

block-backup device=virtio0 format=qcow2 target=backup-20130401.qcow2

What's next for block-backup?
-----------------------------
The following enhancements are left for future patches:

Quoted 7 lines

1. QMP 'transaction' support. It is handy to atomically snapshot multiple block devices. We need qmp_transaction() support for this. Wenchao Xia is currently making qmp_transaction() extensible so new action types, like block-backup, can be added.

2. Sync modes like drive-mirror (top, full, none). This makes it possible to preserve the backing file chain.

Based on today's phone call, it sounds like this would mean adding optional parameters to the QMP command. We already did that for drive-mirror (1.4 has more parameters than 1.3), but without a way to introspect when those parameters are available, the new parameters aren't quite as useful. So we don't repeat that mistake, we need to decide whether this should still go into 1.5 with a plan of adding parameters for 1.6, or whether we should add a counterpart query-* command that makes it easy to determine how much of block-backup is supported, or [your suggestion here]....

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#12 lore raw

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Il 23/04/2013 18:49, Eric Blake ha scritto:

Quoted 9 lines

Based on today's phone call, it sounds like this would mean adding optional parameters to the QMP command. We already did that for drive-mirror (1.4 has more parameters than 1.3), but without a way to introspect when those parameters are available, the new parameters aren't quite as useful. So we don't repeat that mistake, we need to decide whether this should still go into 1.5 with a plan of adding parameters for 1.6, or whether we should add a counterpart query-* command that makes it easy to determine how much of block-backup is supported, or [your suggestion here]....

... libvirt can omit the optional parameters if they match the default and just try executing the command. If it gets an error, it just reports it.

That is, instead of

dictionary = json_object_new()
if (has_capability)
add argument to JSON dictionary
else if (argument != default)
return error

send JSON dictionary via QMP

do this

dictionary = json_object_new()
if (argument != default)
add argument to JSON dictionary
send JSON dictionary via QMP

Paolo
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.19 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQIcBAEBAgAGBQJRdr2AAAoJEBvWZb6bTYbydkAP/0EegweCJX37/XIJ9vtYy4c1 RdS1Jkg/t+KNfsmUp/Qs0JpYbbxhLbbsM9pboKc1LU2Mv5HZd2koibIYNOwTTN3q y2A/4Ecr8A+Q9pv8mblTb8HFea3FDNwtBugTQG7LM/CywYdAk6tg4qZ/0NEXVAPA XEmR0RXRP0ln52f/fByFuq2czg4VvXqygKeBtRJ20ROeUCNpNxYDjiBX03I/4OE8 B1iTkMy7fJwKoc34tOPZ04IzNIRI/Zxtl2my5j3wx7iheYUXNAYPrKpSKXwqYj3g x+jRe7A/QxUZpBTmIynBmDvOxut8ZaLqeWAHYWlcNZameWGSZJgsav5hMjaOVlS3 Kk0Tkm4rrgSYuzC+vhttvGaj9iacnRaG6IMmwyfq1TFcETXLH6ILt8sCxoO5dpHp XetKnEF4m1zmQDXceMbFLRZ3eOKMjwObf6QQIjLkrs/0Xc+T+GfsqUdN6ylY17Rk s6Jsx5ICbS4IUJlktkVv/U9n4VD7y9IBwRlglKgj86noHPFFkb/Z+C0cW8Zkwej/ ZsrjXTNB1abI6/X11PvoaqEnFauaradbtMfRbEmDMhBi+CoBZ9ps578OSn9+x1rn j+WYup9Z5qnDO+BvG+tDElaY6qQUR4ZaGOKo05ODjXkPd83Jn/pOjD2ioaa1dybY KP+0EDFS6SwiSX5jsoei
=C9uD
-----END PGP SIGNATURE-----

#13 lore raw
In reply to Eric Blake
Quoted 19 lines · Eric Blake
Quoted 10 lines · Stefan Hajnoczi

This series adds a new QMP command, block-backup, which takes a point-in-time snapshot of a block device. The snapshot is copied out to a target block device. A simple example is:

block-backup device=virtio0 format=qcow2 target=backup-20130401.qcow2

What's next for block-backup?
-----------------------------
The following enhancements are left for future patches:

Quoted 7 lines

1. QMP 'transaction' support. It is handy to atomically snapshot multiple block devices. We need qmp_transaction() support for this. Wenchao Xia is currently making qmp_transaction() extensible so new action types, like block-backup, can be added.

2. Sync modes like drive-mirror (top, full, none). This makes it possible to preserve the backing file chain.

Quoted 9 lines

Based on today's phone call, it sounds like this would mean adding optional parameters to the QMP command. We already did that for drive-mirror (1.4 has more parameters than 1.3), but without a way to introspect when those parameters are available, the new parameters aren't quite as useful. So we don't repeat that mistake, we need to decide whether this should still go into 1.5 with a plan of adding parameters for 1.6, or whether we should add a counterpart query-* command that makes it easy to determine how much of block-backup is supported, or [your suggestion here]....

I have ideas for the QMP optional parameters discussion that I'll share in another thread.

It's not a problem in this case because block-backup and the sync mode optional parameter will be added in the 1.6 release cycle.

In other words, since QMP is versioned by QEMU release number it's fine to have multiple commits that build up a QMP command - as long as they fall within the same release.

Stefan

#14 lore raw
Quoted 10 lines · Stefan Hajnoczi
Quoted 6 lines
Quoted 3 lines

2. Sync modes like drive-mirror (top, full, none). This makes it possible to preserve the backing file chain.

Based on today's phone call, it sounds like this would mean adding optional parameters to the QMP command.

It's not a problem in this case because block-backup and the sync mode optional parameter will be added in the 1.6 release cycle.

Ah, I misunderstood - I thought you were shooting for 1.5 for the initial release. But it definitely makes more sense if the entire command is waiting for 1.6, since we've already entered soft freeze for 1.5.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#3 lore raw

Re: [Qemu-devel] [PATCH 2/3] block: add block_backup QMP command

In reply to Stefan Hajnoczi
Quoted 9 lines · Stefan Hajnoczi

@block-backup

Start a point-in-time copy of a block device to a new destination.

@device: the name of the device whose writes should be mirrored.

+++ b/qapi-schema.json

qapi-schema.json line 1722

Quoted 8 lines
qapi-schema.json +5 -0
@@ -1715,6 +1715,34 @@
             '*speed': 'int' } }
 
 ##
+# @block-backup
+#
+# Start a point-in-time copy of a block device to a new destination.
+#
+# @device:  the name of the device whose writes should be mirrored.

two spaces?

Quoted 7 lines
qapi-schema.json +7 -0
+#
+# @target: the target of the new image. If the file exists, or if it
+#          is a device, the existing file/device will be used as the new
+#          destination.  If it does not exist, a new file will be created.
+#
+# @format: #optional the format of the new destination, default is to
+#          probe if @mode is 'existing', else the format of the source

qapi-schema.json line 1739

Quoted 10 lines
qapi-schema.json +10 -0
+#
+# @mode: #optional whether and how QEMU should create a new image, default is
+#        'absolute-paths'.
+#
+# @speed:  #optional the maximum speed, in bytes per second
+#
+# Returns: nothing on success
+#          If @device is not a valid block device, DeviceNotFound
+#
+# Since 1.5

1.6, now

qapi-schema.json line 1743

Quoted 4 lines
qapi-schema.json +4 -0
+##
+{ 'command': 'block-backup',
+  'data': { 'device': 'str', 'target': 'str', '*format': 'str',
+            '*mode': 'NewImageMode', '*speed': 'int' } }

Looks reasonable (since it is closely aligned to drive-mirror).

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#4 lore raw

Re: [Qemu-devel] [PATCH 2/3] block: add block_backup QMP command

Quoted 2 lines · Eric Blake
Quoted 1 line · Stefan Hajnoczi

@block-backup

[hit send too soon]

Subject line should mention block-backup, not block_backup.

Quoted 7 lines

qapi-schema.json line 1743

Quoted 4 lines
qapi-schema.json +4 -0
+##
+{ 'command': 'block-backup',
+  'data': { 'device': 'str', 'target': 'str', '*format': 'str',
+            '*mode': 'NewImageMode', '*speed': 'int' } }

Looks reasonable (since it is closely aligned to drive-mirror).

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#5 lore raw

Re: [Qemu-devel] [PATCH 2/3] block: add block_backup QMP command

In reply to Stefan Hajnoczi
Quoted 13 lines · Stefan Hajnoczi

@block-backup

Start a point-in-time copy of a block device to a new destination.

@device: the name of the device whose writes should be mirrored.

@target: the target of the new image. If the file exists, or if it
is a device, the existing file/device will be used as the new destination. If it does not exist, a new file will be created.

@format: #optional the format of the new destination, default is to
probe if @mode is 'existing', else the format of the source

Quoted 7 lines

@mode: #optional whether and how QEMU should create a new image, default is
'absolute-paths'.

@speed: #optional the maximum speed, in bytes per second

Returns: nothing on success
If @device is not a valid block device, DeviceNotFound

This starts a new block job type; I assume the existing block-job-cancel and query-block-jobs can track it.

I'd really love to see us change 'BlockJobInfo' to use an enum for 'type', instead of its open-coded 'str'. Likewise, the block-job related events in QMP/qmp-events.txt should be updated to refer to the enum instead of also being open-coded 'str'. Will this job be called "backup"?

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#15 lore raw
In reply to Stefan Hajnoczi
Quoted 18 lines · Stefan Hajnoczi

This series adds a new QMP command, block-backup, which takes a point-in-time snapshot of a block device. The snapshot is copied out to a target block device. A simple example is:

block-backup device=virtio0 format=qcow2 target=backup-20130401.qcow2

The original block-backup blockjob was written by Dietmar Maurer <[email protected]>. He is currently busy but I feel the feature is worth pushing into QEMU since there has been interest. This is my version of his patch, plus the QMP command and qemu-iotests test case.

How is this different from block-stream and drive-mirror?
---------------------------------------------------------
Both block-stream and drive-mirror do not provide immediate point-in-time snapshots. Instead they copy data into a new file and then switch to it. In other words, the point at which the "snapshot" is taken cannot be controlled directly.

Quoted 4 lines

block-backup intercepts guest writes and saves data into the target block device before it is overwritten. The target block device can be a raw image file, backing files are not used to implement this feature.

Besides, compared to snapshot-blkdev, it mainly brings better
performance by avoid merging later, however, other tool may be needed to form an incremental backup, which may be not related to this patch.
No objection to this patch, but perhaps a better way is using
internal snapshot by adding base/delta data export support.

Quoted 45 lines

How can block-backup be used?
-----------------------------
The simplest use-case is to copy a point-in-time snapshot to a local file.

More advanced users may wish to make the target an NBD URL. The NBD server listening on the other side can process the backup writes any way it wishes. I previously posted an RFC series with a backup server that streamed Dietmar's VMA backup archive format.

What's next for block-backup?
-----------------------------
The following enhancements are left for future patches:

1. QMP 'transaction' support. It is handy to atomically snapshot multiple block devices. We need qmp_transaction() support for this. Wenchao Xia is currently making qmp_transaction() extensible so new action types, like block-backup, can be added.

2. Sync modes like drive-mirror (top, full, none). This makes it possible to preserve the backing file chain.

Dietmar Maurer (1):
block: add basic backup support to block driver

Stefan Hajnoczi (2):
block: add block_backup QMP command
qemu-iotests: add 054 block-backup test case

  block.c                    |  69 ++++++++++++-
  block/Makefile.objs        |   1 +
  block/backup.c             | 252 +++++++++++++++++++++++++++++++++++++++++++++
  blockdev.c                 |  92 +++++++++++++++++
  include/block/block.h      |   2 +
  include/block/block_int.h  |  16 +++
  include/block/blockjob.h   |  10 ++
  qapi-schema.json           |  28 +++++
  qmp-commands.hx            |   6 ++
  tests/qemu-iotests/054     | 230 +++++++++++++++++++++++++++++++++++++++++
  tests/qemu-iotests/054.out |   5 +
  tests/qemu-iotests/group   |   1 +
  12 files changed, 707 insertions(+), 5 deletions(-)

create mode 100644 block/backup.c
create mode 100755 tests/qemu-iotests/054
create mode 100644 tests/qemu-iotests/054.out

Signature · 3 lines
Best Regards

Wenchao Xia
#6 lore raw

Re: [Qemu-devel] [PATCH 2/3] block: add block_backup QMP command

In reply to Eric Blake
Quoted 17 lines · Eric Blake
Quoted 16 lines · Stefan Hajnoczi

@block-backup

Start a point-in-time copy of a block device to a new destination.

@device: the name of the device whose writes should be mirrored.

@target: the target of the new image. If the file exists, or if it
is a device, the existing file/device will be used as the new destination. If it does not exist, a new file will be created.

@format: #optional the format of the new destination, default is to
probe if @mode is 'existing', else the format of the source

@mode: #optional whether and how QEMU should create a new image, default is
'absolute-paths'.

Quoted 7 lines
Quoted 4 lines

@speed: #optional the maximum speed, in bytes per second

Returns: nothing on success
If @device is not a valid block device, DeviceNotFound

This starts a new block job type; I assume the existing block-job-cancel and query-block-jobs can track it.

Right. I will update the documentation to be explicit about the block job that this command creates.

Quoted 4 lines

I'd really love to see us change 'BlockJobInfo' to use an enum for 'type', instead of its open-coded 'str'. Likewise, the block-job related events in QMP/qmp-events.txt should be updated to refer to the enum instead of also being open-coded 'str'.

Since the block job QMP API has been in released I'm not sure changing this is worthwhile. QEMU and libvirt would have to maintain compatibility so the code will just be duplicated.

Quoted 1 line

Will this job be called "backup"?

Yes, it is called "backup".

#16 lore raw
In reply to Wenchao Xia
Quoted 23 lines · Wenchao Xia
Quoted 18 lines · Stefan Hajnoczi

This series adds a new QMP command, block-backup, which takes a point-in-time snapshot of a block device. The snapshot is copied out to a target block device. A simple example is:

block-backup device=virtio0 format=qcow2 target=backup-20130401.qcow2

The original block-backup blockjob was written by Dietmar Maurer <[email protected]>. He is currently busy but I feel the feature is worth pushing into QEMU since there has been interest. This is my version of his patch, plus the QMP command and qemu-iotests test case.

How is this different from block-stream and drive-mirror?
---------------------------------------------------------
Both block-stream and drive-mirror do not provide immediate point-in-time snapshots. Instead they copy data into a new file and then switch to it. In other words, the point at which the "snapshot" is taken cannot be controlled directly.

Quoted 4 lines

block-backup intercepts guest writes and saves data into the target block device before it is overwritten. The target block device can be a raw image file, backing files are not used to implement this feature.

Quoted 6 lines

Besides, compared to snapshot-blkdev, it mainly brings better
performance by avoid merging later, however, other tool may be needed to form an incremental backup, which may be not related to this patch.
No objection to this patch, but perhaps a better way is using
internal snapshot by adding base/delta data export support.

Yes, incremental backups would need to be added later.

Stefan

#7 lore raw

Re: [Qemu-devel] [PATCH 2/3] block: add block_backup QMP command

In reply to Stefan Hajnoczi

Il 29/04/2013 09:21, Stefan Hajnoczi ha scritto:

Quoted 8 lines
Quoted 4 lines

I'd really love to see us change 'BlockJobInfo' to use an enum for 'type', instead of its open-coded 'str'. Likewise, the block-job related events in QMP/qmp-events.txt should be updated to refer to the enum instead of also being open-coded 'str'.

Since the block job QMP API has been in released I'm not sure changing this is worthwhile. QEMU and libvirt would have to maintain compatibility so the code will just be duplicated.

I don't think this would change the actual data on the wire. However, it would let libvirt know the supported block job types by introspecting the enum.

Paolo

#8 lore raw

Re: [Qemu-devel] [PATCH 2/3] block: add block_backup QMP command

Quoted 6 lines · Paolo Bonzini

Il 29/04/2013 09:21, Stefan Hajnoczi ha scritto:

Quoted 5 lines
Quoted 4 lines

I'd really love to see us change 'BlockJobInfo' to use an enum for 'type', instead of its open-coded 'str'. Likewise, the block-job related events in QMP/qmp-events.txt should be updated to refer to the enum instead of also being open-coded 'str'.

Quoted 7 lines
Quoted 3 lines

Since the block job QMP API has been in released I'm not sure changing this is worthwhile. QEMU and libvirt would have to maintain compatibility so the code will just be duplicated.

I don't think this would change the actual data on the wire. However, it would let libvirt know the supported block job types by introspecting the enum.

Until we have introspection, the point is moot. When we have introspection, libvirt would much rather see an enum than a 'str'. I see absolutely no back-compat problem in changing the code to be type-safe prior to the point that introspection is added.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#9 lore raw

Re: [Qemu-devel] [PATCH 2/3] block: add block_backup QMP command

Quoted 11 lines · Eric Blake
Quoted 10 lines · Paolo Bonzini

Il 29/04/2013 09:21, Stefan Hajnoczi ha scritto:

Quoted 8 lines
Quoted 4 lines

I'd really love to see us change 'BlockJobInfo' to use an enum for 'type', instead of its open-coded 'str'. Likewise, the block-job related events in QMP/qmp-events.txt should be updated to refer to the enum instead of also being open-coded 'str'.

Since the block job QMP API has been in released I'm not sure changing this is worthwhile. QEMU and libvirt would have to maintain compatibility so the code will just be duplicated.

Quoted 8 lines
Quoted 3 lines

I don't think this would change the actual data on the wire. However, it would let libvirt know the supported block job types by introspecting the enum.

Until we have introspection, the point is moot. When we have introspection, libvirt would much rather see an enum than a 'str'. I see absolutely no back-compat problem in changing the code to be type-safe prior to the point that introspection is added.

Okay, I haven't looked at how QAPI enums are implemented. If we can move from custom strings to enum without breaking existing libvirt, then great!

Stefan