RFC: Efficient VM backup for qemu
PATCH v2 1/6 RFC: Efficient VM backup for qemu
This series provides a way to efficiently backup VMs.
* Backup to a single archive file
* Backup contain all data to restore VM (full backup)
* Do not depend on storage type or image format
* Avoid use of temporary storage
* store sparse images efficiently
The file docs/backup-rfc.txt contains more details.
Changes since v1:
* fix spelling errors
* move BackupInfo from BDS to BackupBlockJob
* introduce BackupDriver to allow more than one backup format
* vma: add suport to store vmstate (size is not known in advance)
* add ability to store VM state
--- docs/backup-rfc.txt | 119 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 119 insertions(+), 0 deletions(-) create mode 100644 docs/backup-rfc.txt
docs/backup-rfc.txt new file +119 -0
@@ -0,0 +1,119 @@ +RFC: Efficient VM backup for qemu + +=Requirements= + +* Backup to a single archive file +* Backup needs to contain all data to restore VM (full backup) +* Do not depend on storage type or image format +* Avoid use of temporary storage +* store sparse images efficiently + +=Introduction= + +Most VM backup solutions use some kind of snapshot to get a consistent +VM view at a specific point in time. For example, we previously used +LVM to create a snapshot of all used VM images, which are then copied +into a tar file. + +That basically means that any data written during backup involve +considerable overhead. For LVM we get the following steps: + +1.) read original data (VM write) +2.) write original data into snapshot (VM write) +3.) write new data (VM write) +4.) read data from snapshot (backup) +5.) write data from snapshot into tar file (backup) + +Another approach to backup VM images is to create a new qcow2 image +which use the old image as base. During backup, writes are redirected +to the new image, so the old image represents a 'snapshot'. After +backup, data need to be copied back from new image into the old +one (commit). So a simple write during backup triggers the following +steps: + +1.) write new data to new image (VM write) +2.) read data from old image (backup) +3.) write data from old image into tar file (backup) + +4.) read data from new image (commit) +5.) write data to old image (commit) + +This is in fact the same overhead as before. Other tools like qemu +livebackup produces similar overhead (2 reads, 3 writes). + +Some storage types/formats supports internal snapshots using some kind +of reference counting (rados, sheepdog, dm-thin, qcow2). It would be possible +to use that for backups, but for now we want to be storage-independent. + +Note: It turned out that taking a qcow2 snapshot can take a very long +time on larger files. + +=Make it more efficient= + +The be more efficient, we simply need to avoid unnecessary steps. The +following steps are always required: + +1.) read old data before it gets overwritten +2.) write that data into the backup archive +3.) write new data (VM write) + +As you can see, this involves only one read, an two writes. + +To make that work, our backup archive need to be able to store image +data 'out of order'. It is important to notice that this will not work +with traditional archive formats like tar. + +During backup we simply intercept writes, then read existing data and +store that directly into the archive. After that we can continue the +write. + +==Advantages== + +* very good performance (1 read, 2 writes) +* works on any storage type and image format. +* avoid usage of temporary storage +* we can define a new and simple archive format, which is able to + store sparse files efficiently. + +Note: Storing sparse files is a mess with existing archive +formats. For example, tar requires information about holes at the +beginning of the archive. + +==Disadvantages== + +* we need to define a new archive format + +Note: Most existing archive formats are optimized to store small files +including file attributes. We simply do not need that for VM archives. + +* archive contains data 'out of order' + +If you want to access image data in sequential order, you need to +re-order archive data. It would be possible to to that on the fly, +using temporary files. + +Fortunately, a normal restore/extract works perfectly with 'out of +order' data, because the target files are seekable. + +* slow backup storage can slow down VM during backup + +It is important to note that we only do sequential writes to the +backup storage. Furthermore one can compress the backup stream. IMHO, +it is better to slow down the VM a bit. All other solutions creates +large amounts of temporary data during backup. + +=Archive format requirements= + +The basic requirement for such new format is that we can store image +date 'out of order'. It is also very likely that we have less than 256 +drives/images per VM, and we want to be able to store VM configuration +files. + +We have defined a very simply format with those properties, see: + +docs/specs/vma_spec.txt + +Please let us know if you know an existing format which provides the +same functionality. + +
Signature · 1 line
1.7.2.5
PATCH v2 2/6 add basic backup support to block driver
Function backup_job_start() creates a block job to backup a block device.
We call backup_do_cow() for each write during backup. That function reads the original data and pass it to backup_dump_cb().
The tracked_request infrastructure is used to serialize access.
Currently backup cluster size is hardcoded to 65536 bytes.
--- Makefile.objs | 1 + backup.c | 302 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ backup.h | 30 ++++++ block.c | 71 ++++++++++++- block.h | 2 + blockjob.h | 10 ++ 6 files changed, 410 insertions(+), 6 deletions(-) create mode 100644 backup.c create mode 100644 backup.h
Makefile.objs +1 -0
@@ -48,6 +48,7 @@ coroutine-obj-$(CONFIG_WIN32) += coroutine-win32.o block-obj-y = iov.o cache-utils.o qemu-option.o module.o async.o block-obj-y += nbd.o block.o blockjob.o aes.o qemu-config.o block-obj-y += thread-pool.o qemu-progress.o qemu-sockets.o uri.o notify.o +block-obj-y += backup.o block-obj-y += $(coroutine-obj-y) $(qobject-obj-y) $(version-obj-y) block-obj-$(CONFIG_POSIX) += event_notifier-posix.o aio-posix.o block-obj-$(CONFIG_WIN32) += event_notifier-win32.o aio-win32.o
backup.c new file +302 -0
302 lines not on this page · raw message
backup.h new file +30 -0
@@ -0,0 +1,30 @@ +/* +* QEMU backup related definitions +* +* Copyright (C) Proxmox Server Solutions +* +* Authors: +* Dietmar Maurer ([email protected]) +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +* +*/ + +#ifndef QEMU_BACKUP_H +#define QEMU_BACKUP_H + +#include <uuid/uuid.h> + +#define BACKUP_CLUSTER_BITS 16 +#define BACKUP_CLUSTER_SIZE (1<<BACKUP_CLUSTER_BITS) +#define BACKUP_BLOCKS_PER_CLUSTER (BACKUP_CLUSTER_SIZE/BDRV_SECTOR_SIZE) + +typedef int BackupDumpFunc (void *opaque,BlockDriverState *bs, +int64_t cluster_num,unsigned char *buf); + +int backup_job_start (BlockDriverState *bs,BackupDumpFunc *backup_dump_cb, +BlockDriverCompletionFunc *backup_complete_cb, +void *opaque); + +#endif /* QEMU_BACKUP_H */
block.c +65 -6
133 lines not on this page · raw message
block.h +2 -0
@@ -172,6 +172,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);/*
blockjob.h +10 -0
@@ -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.7.2.5
PATCH v2 3/6 add backup related monitor commands
In reply to Dietmar MaurerWe use a generic BackupDriver struct to encaplulated all archive format related function.
Another option would be to simply dump <devid,cluster_num,cluster_data> to the output fh (pipe), and an external binary saves the data. That way we could move the whole archive format related code out of qemu.
--- backup.h | 14 +++ blockdev.c | 337 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ hmp-commands.hx | 31 +++++ hmp.c | 63 ++++++++++ hmp.h | 3 + monitor.c | 7 + qapi-schema.json | 81 +++++++++++++ qmp-commands.hx | 27 +++++ 8 files changed, 563 insertions(+), 0 deletions(-)
backup.h +14 -0
@@ -27,4 +27,18 @@ int backup_job_start(BlockDriverState *bs, BackupDumpFunc *backup_dump_cb, BlockDriverCompletionFunc *backup_complete_cb, void *opaque); +typedef struct BackupDriver { +const char *format; +void *(*open_cb)(const char *filename,uuid_t uuid,int64_t speed, +Error **errp); +int (*close_cb)(void *opaque,Error **errp); +int (*register_config_cb)(void *opaque,const char *name,gpointer data, +size_t data_len); +int (*register_stream_cb)(void *opaque,const char *devname,size_t size); +int (*dump_cb)(void *opaque,uint8_t dev_id,int64_t cluster_num, +unsigned char *buf,size_t *zero_bytes); +int (*complete_cb)(void *opaque,uint8_t dev_id,int ret); +void (*cancel_cb)(void *opaque); +}BackupDriver ; +#endif /* QEMU_BACKUP_H */
blockdev.c +337 -0
349 lines not on this page · raw message
hmp-commands.hx +31 -0
@@ -83,6 +83,35 @@ STEXI Copy data from a backing file into a block device. ETEXI + { + .name = "backup", + .args_type = "backupfile:s,speed:o?,devlist:s?", + .params = "backupfile [speed [devlist]]", + .help = "create a VM Backup.", + .mhandler.cmd = hmp_backup, + }, + +STEXI +@item backup +@findex backup +Create a VM backup. +ETEXI + + { + .name = "backup_cancel", + .args_type = "", + .params = "", + .help = "cancel the current VM backup", + .mhandler.cmd = hmp_backup_cancel, + }, + +STEXI +@item backup_cancel +@findex backup_cancel +Cancel the current VM backup. + +ETEXI + { .name = "block_job_set_speed", .args_type = "device:B,speed:o",
@@ -1558,6 +1587,8 @@ show CPU statistics show user network stack connection states @item info migrate show migration status +@item info backup +show backup status @item info migrate_capabilities show current migration capabilities @item info migrate_cache_size
hmp.c +63 -0
75 lines not on this page · raw message
hmp.h +3 -0
@@ -28,6 +28,7 @@ void hmp_info_mice(Monitor *mon);void hmp_info_migrate (Monitor *mon);void hmp_info_migrate_capabilities (Monitor *mon);void hmp_info_migrate_cache_size (Monitor *mon); +void hmp_info_backup (Monitor *mon);void hmp_info_cpus (Monitor *mon);void hmp_info_block (Monitor *mon);void hmp_info_blockstats (Monitor *mon);
@@ -63,6 +64,8 @@ void hmp_eject(Monitor *mon, const QDict *qdict);void hmp_change (Monitor *mon,const QDict *qdict);void hmp_block_set_io_throttle (Monitor *mon,const QDict *qdict);void hmp_block_stream (Monitor *mon,const QDict *qdict); +void hmp_backup (Monitor *mon,const QDict *qdict); +void hmp_backup_cancel (Monitor *mon,const QDict *qdict);void hmp_block_job_set_speed (Monitor *mon,const QDict *qdict);void hmp_block_job_cancel (Monitor *mon,const QDict *qdict);void hmp_block_job_pause (Monitor *mon,const QDict *qdict);
monitor.c +7 -0
@@ -2680,6 +2680,13 @@ static mon_cmd_t info_cmds[] = { },#endif { + .name = "backup", + .args_type = "", + .params = "", + .help = "show backup status", + .mhandler.info = hmp_info_backup, + }, + { .name = "migrate", .args_type = "", .params = "",
qapi-schema.json +81 -0
93 lines not on this page · raw message
qmp-commands.hx +27 -0
@@ -822,6 +822,18 @@ EQMP }, { + .name = "backup", + .args_type = "backupfile:s,format:s?,config-filename:F?,speed:o?,devlist:s?", + .mhandler.cmd_new = qmp_marshal_input_backup, + }, + + { + .name = "backup_cancel", + .args_type = "", + .mhandler.cmd_new = qmp_marshal_input_backup_cancel, + }, + + { .name = "block-job-set-speed", .args_type = "device:B,speed:o", .mhandler.cmd_new = qmp_marshal_input_block_job_set_speed,
@@ -2491,6 +2503,21 @@ EQMP }, SQMP + +query-backup +------------- + +Backup status. + +EQMP + + { + .name = "query-backup", + .args_type = "", + .mhandler.cmd_new = qmp_marshal_input_query_backup, + }, + +SQMP migrate-set-capabilities -------
Signature · 1 line
1.7.2.5
Re: [Qemu-devel] [PATCH v2 3/6] add backup related monitor commands
Quoted 1 line · Dietmar Maurer
We use a generic BackupDriver struct to encaplulated all archive format
s/encaplulated/encapsulated/
Quoted 5 lines
related function.
Another option would be to simply dump <devid,cluster_num,cluster_data> to the output fh (pipe), and an external binary saves the data. That way we could move the whole archive format related code out of qemu.
I'm coming into this review late, so I'm not sure what your series is adding that cannot already be done with external snapshots and migration to file. But any time someone proposes new QMP commands that libvirt might have to interact with, I try to chime in:
Quoted 16 lines
Signed-off-by Dietmar Maurer <[email protected]>--- backup.h | 14 +++ blockdev.c | 337 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ hmp-commands.hx | 31 +++++ hmp.c | 63 ++++++++++ hmp.h | 3 + monitor.c | 7 + qapi-schema.json | 81 +++++++++++++ qmp-commands.hx | 27 +++++ 8 files changed, 563 insertions(+), 0 deletions(-)+++
b/qapi-schema.json
qapi-schema.json line 366
Quoted 9 lines
qapi-schema.json +6
-0@@ -358,6 +358,39 @@ { 'type': 'EventInfo', 'data': {'name': 'str'} } ## +# @BackupStatus: +# +# Detailed backup status. +# +# @status: #optional string describing the current backup status. +# Tthis can be 'active', 'done', 'error'. If this field is not
s/Tthis/This/
qapi-schema.json line 377
Quoted 11 lines
qapi-schema.json +11
-0+# returned, no backup process has been initiated +# +# @errmsg: #optional error message (only returned if status is 'error') +# +# @total: #optional total amount of bytes involved in the backup process +# +# @transferred: #optional amount of bytes already backed up. +# +# @zero-bytes: #optional amount of 'zero' bytes detected. +# +# @start-time: #optional time (epoch) when backup job started.
Should you account for sub-second resolution here
Review line 379
Quoted 2 lines
(quoted diff) +2
-0+# +# @end-time: #optional time (epoch) when backup job finished.
and here?
Quoted 18 lines
qapi-schema.json +14
-0+# +# @backupfile: #optional backup file name +# +# @uuid: #optional uuid for this backup job +# +# Since: 1.4.0 +## +{ 'type': 'BackupStatus', + 'data': {'*status': 'str', '*errmsg': 'str', '*total': 'int', + '*transferred': 'int', '*zero-bytes': 'int', + '*start-time': 'int', '*end-time': 'int', + '*backupfile': 'str', '*uuid': 'str' } } + +## # @query-events: # # Return a list of supported QMP events by this server
qapi-schema.json line 1806
Quoted 10 lines
qapi-schema.json +7
-0@@ -1764,6 +1797,54 @@ 'data': { 'path': 'str' }, 'returns': [ 'ObjectPropertyInfo' ] } + +## +# @backup: +# +# Starts a VM backup. +# +# @backupfile: the backup file name
I guess the fdset mechanism is how libvirt would pass in a pipe fd rather than supplying an actual file name. Does your implementation allow for pipes, or must it be seekable?
Review line 1808
Quoted 2 lines
(quoted diff) +2
-0+# +# @format: format of the backup file
Rather than letting this be a free-form string, it would be nicer as an enum of actually supported formats.
qapi-schema.json line 1815
Quoted 7 lines
qapi-schema.json +7
-0+# +# @config-filename: #optional name of a configuration file to include into +# the backup archive. +# +# @speed: #optional the maximum speed, in bytes per second +# +# Returns: the uuid of the backup job
UUID in raw byte format, or in ASCII format? (Assuming the latter)
qapi-schema.json line 1819
Quoted 4 lines
qapi-schema.json +4
-0+# +# Since: 1.4.0 +## +{ 'command': 'backup', 'data': { 'backupfile': 'str', '*format': 'str',
backupfile sounds like a run-on; is it any better to name it backup-file?
qapi-schema.json line 1820
Quoted 1 line
qapi-schema.json +1
-0+ '*config-filename': 'str',
especially when compared with config-filename
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
Re: [Qemu-devel] [PATCH v2 3/6] add backup related monitor commands
Quoted 2 lines
I'm coming into this review late, so I'm not sure what your series is adding that cannot already be done with external snapshots and migration to file.
This series try to do backup more efficient (see docu in [PATCH v2 1/6]).
Quoted 2 lines
But any time someone proposes new QMP commands that libvirt might have to interact with, I try to chime in:
libvirt does not need to use any of those commands.
Quoted 4 lines
Quoted 4 lines
qapi-schema.json +4
-0+# returned, no backup process has been initiated +# +# @errmsg: #optional error message (only returned if status is +'error') # # @total: #optional total amount of bytes involved in the
Quoted 8 lines
qapi-schema.json line 377
Quoted 6 lines
qapi-schema.json +6
-0+backup process # # @transferred: #optional amount of bytes already +backed up. +# +# @zero-bytes: #optional amount of 'zero' bytes detected. +# +# @start-time: #optional time (epoch) when backup job started.Should you account for sub-second resolution here
Why (I doubt we can backup within a second).
Quoted 5 lines
Quoted 1 line
+# @backupfile: the backup file name
I guess the fdset mechanism is how libvirt would pass in a pipe fd rather than supplying an actual file name. Does your implementation allow for pipes, or must it be seekable?
You can pass /dev/fdset/XXX.
My implementation allows pipes.
(I will post the necessary changes for /dev/fdset/ next time).
Quoted 4 lines
Quoted 1 line
+# @format: format of the backup file
Rather than letting this be a free-form string, it would be nicer as an enum of actually supported formats.
ok
Quoted 7 lines
Review
Quoted 5 lines
(quoted diff) +5
-0+# @config-filename: #optional name of a configuration file to include +into # the backup archive. +# +# @speed: #optional the maximum speed, in bytes per second # # +Returns: the uuid of the backup jobUUID in raw byte format, or in ASCII format? (Assuming the latter)
ASCII
Quoted 11 lines
qapi-schema.json
Quoted 5 lines
qapi-schema.json +5
-0+# +# Since: 1.4.0 +## +{ 'command': 'backup', 'data': { 'backupfile': 'str', '*format': +'str',backupfile sounds like a run-on; is it any better to name it backup-file?
Quoted 1 line
+ '*config-filename': 'str',
especially when compared with config-filename
ok. Many thanks for the review.
PATCH v2 4/6 introduce new vma archive format
In reply to Dietmar MaurerThis is a very simple archive format, see docs/specs/vma_spec.txt
--- Makefile | 3 +- Makefile.objs | 2 +- blockdev.c | 6 +- docs/specs/vma_spec.txt | 24 ++ vma-reader.c | 772 ++++++++++++++++++++++++++++++++++++++++ vma-writer.c | 908 +++++++++++++++++++++++++++++++++++++++++++++++ vma.c | 550 ++++++++++++++++++++++++++++ vma.h | 145 ++++++++ 8 files changed, 2406 insertions(+), 4 deletions(-) create mode 100644 docs/specs/vma_spec.txt create mode 100644 vma-reader.c create mode 100644 vma-writer.c create mode 100644 vma.c create mode 100644 vma.h
Makefile +2 -1
@@ -100,7 +100,7 @@ defconfig: -include config-all-devices.mak -all: $(DOCS) $(TOOLS) $(HELPERS-y) recurse-all +all: $(DOCS) $(TOOLS) vma$(EXESUF) $(HELPERS-y) recurse-all config-host.h: config-host.h-timestamp config-host.h-timestamp: config-host.mak
@@ -194,6 +194,7 @@ tools-obj-$(CONFIG_POSIX) += compatfd.o qemu-img$(EXESUF): qemu-img.o $(tools-obj-y) $(block-obj-y) libqemustub.a qemu-nbd$(EXESUF): qemu-nbd.o $(tools-obj-y) $(block-obj-y) libqemustub.a qemu-io$(EXESUF): qemu-io.o cmd.o $(tools-obj-y) $(block-obj-y) libqemustub.a +vma$(EXESUF): vma.o vma-writer.o vma-reader.o $(tools-obj-y) $(block-obj-y) libqemustub.a qemu-bridge-helper$(EXESUF): qemu-bridge-helper.o
Makefile.objs +1 -1
@@ -48,7 +48,7 @@ coroutine-obj-$(CONFIG_WIN32) += coroutine-win32.o block-obj-y = iov.o cache-utils.o qemu-option.o module.o async.o block-obj-y += nbd.o block.o blockjob.o aes.o qemu-config.o block-obj-y += thread-pool.o qemu-progress.o qemu-sockets.o uri.o notify.o -block-obj-y += backup.o +block-obj-y += vma-writer.o backup.o block-obj-y += $(coroutine-obj-y) $(qobject-obj-y) $(version-obj-y) block-obj-$(CONFIG_POSIX) += event_notifier-posix.o aio-posix.o block-obj-$(CONFIG_WIN32) += event_notifier-win32.o aio-win32.o
blockdev.c +4 -2
@@ -21,6 +21,7 @@#include "trace.h" #include "arch_init.h" #include "backup.h" +#include "vma.h" static QTAILQ_HEAD(drivelist, DriveInfo) drives = QTAILQ_HEAD_INITIALIZER(drives);
@@ -1444,10 +1445,11 @@ char *qmp_backup(const char *backupfile, bool has_format, const char *format,/* Todo: try to auto-detect format based on file name */ format = has_format ? format :"vma" ; -/* fixme: find driver for specifued format */ const BackupDriver *driver =NULL ; -if (!driver) { +if (strcmp (format,"vma" ) ==0 ) { + driver = &backup_vma_driver; + }else {error_set (errp,ERROR_CLASS_GENERIC_ERROR ,"no backup driver for format '%s'" , format);return NULL ;
docs/specs/vma_spec.txt new file +24 -0
@@ -0,0 +1,24 @@ +=Virtual Machine Archive format (VMA)= + +This format contains a header which includes the VM configuration as +binary blobs, and a list of devices (dev_id, name). + +The actual VM image data is stored inside extents. An extent contains +up to 64 clusters, and start with a 512 byte header containing +additional information for those clusters. + +We use a cluster size of 65536, and use 8 bytes for each +cluster in the header to store the following information: + +* 1 byte dev_id (to identity the drive) +* 2 bytes zero indicator (mark zero regions (16x4096)) +* 4 bytes cluster number +* 1 byte not used (reserved) + +We only store non-zero blocks (such block is 4096 bytes). + +Each archive is marked with a uuid. The archive header and all +extent headers includes that uuid and a MD5 checksum (over header +data). + +
vma-reader.c new file +772 -0
772 lines not on this page · raw message
vma-writer.c new file +908 -0
908 lines not on this page · raw message
vma.c new file +550 -0
550 lines not on this page · raw message
vma.h new file +145 -0
145 lines not on this page · raw message
Signature · 1 line
1.7.2.5
PATCH v2 5/6 add regression tests for backup
In reply to Dietmar MaurerAlso added the ratelimit.h fix here, because it is still not upstream.
--- include/qemu/ratelimit.h | 2 +- tests/Makefile | 11 +- tests/backup-test.c | 511 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 521 insertions(+), 3 deletions(-) create mode 100644 tests/backup-test.c
include/qemu/ratelimit.h +1 -1
@@ -42,7 +42,7 @@ static inline void ratelimit_set_speed(RateLimit *limit, uint64_t speed, uint64_t slice_ns) { limit->slice_ns = slice_ns; - limit->slice_quota = ((double )speed *1000000000ULL ) / slice_ns; + limit->slice_quota = ((double )speed * slice_ns)/1000000000ULL ; }#endif
tests/Makefile +9 -2
@@ -20,6 +20,8 @@ check-unit-y += tests/test-thread-pool$(EXESUF) check-block-$(CONFIG_POSIX) += tests/qemu-iotests-quick.sh +check-backup-y = tests/backup-test$(EXESUF) + # All QTests for now are POSIX-only, but the dependencies are # really in libqtest, not in the testcases themselves. check-qtest-i386-y = tests/fdc-test$(EXESUF)
@@ -54,6 +56,7 @@ tests/test-coroutine$(EXESUF): tests/test-coroutine.o $(coroutine-obj-y) $(tools tests/test-aio$(EXESUF): tests/test-aio.o $(coroutine-obj-y) $(tools-obj-y) $(block-obj-y) libqemustub.a tests/test-thread-pool$(EXESUF): tests/test-thread-pool.o $(coroutine-obj-y) $(tools-obj-y) $(block-obj-y) libqemustub.a tests/test-iov$(EXESUF): tests/test-iov.o iov.o +tests/backup-test$(EXESUF): tests/backup-test.o vma-reader.o $(tools-obj-y) $(block-obj-y) libqemustub.a tests/test-qapi-types.c tests/test-qapi-types.h :\ $(SRC_PATH)/qapi-schema-test.json $(SRC_PATH)/scripts/qapi-types.py
@@ -146,10 +149,14 @@ check-tests/qemu-iotests-quick.sh: tests/qemu-iotests-quick.sh qemu-img$(EXESUF) # Consolidated targets -.PHONY: check-qtest check-unit check +.PHONY: check-backup check-qtest check-unit check check-qtest: $(patsubst %,check-qtest-%, $(QTEST_TARGETS)) check-unit: $(patsubst %,check-%, $(check-unit-y)) check-block: $(patsubst %,check-%, $(check-block-y)) -check: check-unit check-qtest + +check-backup: tests/backup-test$(EXESUF) + $< + +check: check-unit check-qtest check-backup -include $(wildcard tests/*.d)
tests/backup-test.c new file +511 -0
511 lines not on this page · raw message
Signature · 1 line
1.7.2.5
PATCH v2 6/6 add vm state to backups
In reply to Dietmar Maurer--- blockdev.c | 196 +++++++++++++++++++++++++++++++++++++++++++++++++++++- hmp.c | 2 +- qapi-schema.json | 6 +- 3 files changed, 199 insertions(+), 5 deletions(-)
blockdev.c +194 -2
@@ -22,6 +22,8 @@#include "arch_init.h" #include "backup.h" #include "vma.h" +#include "qemu-file.h" +#include "migration.h" static QTAILQ_HEAD(drivelist, DriveInfo) drives = QTAILQ_HEAD_INITIALIZER(drives);
@@ -1338,6 +1340,10 @@ static struct GenericBackupState {size_t total;size_t transferred;size_t zero_bytes; +unsigned char buf[BACKUP_CLUSTER_SIZE ]; +int buf_index; +size_t buf_cluster_num; +guint8 vmstate_dev_id; } backup_state; typedef struct BackupCB {
@@ -1430,10 +1436,170 @@ static void backup_start_jobs(void) } } +static int backup_state_close (void *opaque) +{ +if (!backup_state.buf_index) { +return 0 ; + } + +size_t zero_bytes =0 ; +if (backup_state.buf_index <BACKUP_CLUSTER_SIZE ) { +memset (backup_state.buf + backup_state.buf_index,0 , +BACKUP_CLUSTER_SIZE - backup_state.buf_index); + } +int bytes = backup_state.driver->dump_cb ( + backup_state.writer, backup_state.vmstate_dev_id, + backup_state.buf_cluster_num, + backup_state.buf, &zero_bytes); + backup_state.buf_index =0 ; + +return bytes <0 ?-1 :0 ; +} + +static int backup_state_put_buffer (void *opaque,const uint8_t *buf, +int64_t pos,int size) +{ +assert (backup_state.driver); +assert (backup_state.writer); +assert (backup_state.driver->dump_cb); + +/* Note: our backup driver expects to get whole clusters (64KB) */ + +int ret = size; + +while (size >0 ) { +int l =BACKUP_CLUSTER_SIZE - backup_state.buf_index; + l = l > size ? size : l; +memcpy (backup_state.buf + backup_state.buf_index, buf, l); + backup_state.buf_index += l; + buf += l; + size -= l; +if (backup_state.buf_index ==BACKUP_CLUSTER_SIZE ) { +size_t zero_bytes =0 ; +int bytes = backup_state.driver->dump_cb ( + backup_state.writer, backup_state.vmstate_dev_id, + backup_state.buf_cluster_num++, + backup_state.buf, &zero_bytes); + backup_state.buf_index =0 ; +if (bytes <0 ) { +return -1 ; + } + } + } + +return ret; +} + +static const QEMUFileOps backup_file_ops = { + .put_buffer = backup_state_put_buffer, + .close = backup_state_close, +}; + +static void coroutine_fn backup_start_savevm(void *opaque) +{ +assert (backup_state.driver); +assert (backup_state.writer); +int ret; +char *err =NULL ; +uint64_t remaining; +int64_t maxlen; +MigrationParams params = { + .blk =0 , + .shared =0 + }; + +int restart =0 ; + +QEMUFile *file =qemu_fopen_ops (NULL , &backup_file_ops); + + ret =qemu_savevm_state_begin (file, ¶ms); +if (ret <0 ) { +qemu_fclose (file); + err =g_strdup ("qemu_savevm_state_begin failed" ); + goto abort; + } + +while (1 ) { + ret =qemu_savevm_state_iterate (file); + remaining =ram_bytes_remaining (); + +if (ret <0 ) { +qemu_fclose (file); + err =g_strdup_printf ("qemu_savevm_state_iterate error %d" , ret); + goto abort; + } + +/* stop the VM if we use too much space, +* or if remaining is just a few MB +*/ + maxlen =ram_bytes_total (); +size_t cpos = backup_state.buf_cluster_num *BACKUP_CLUSTER_SIZE ; +if ((remaining <100000 ) || ((cpos + remaining) >= maxlen)) { +if (runstate_is_running ()) { + restart =1 ; +vm_stop (RUN_STATE_SAVE_VM ); + } + } + +if (ret ==1 ) {/* finished */ +if (runstate_is_running ()) { + restart =1 ; +vm_stop (RUN_STATE_SAVE_VM ); + } + + ret =qemu_savevm_state_complete (file); +if (ret <0 ) { +qemu_fclose (file); + err =g_strdup ("qemu_savevm_state_complete error" ); + goto abort; + + }else { +if (qemu_fclose (file) <0 ) { +error_setg (&backup_state.error, +"backup_start_savevm: qemu_fclose failed" ); + goto abort; + } +if (backup_state.driver->complete_cb (backup_state.writer, + backup_state.vmstate_dev_id,0 ) <0 ) { + err =g_strdup ("backup_start_savevm: complete_cb failed" ); + goto abort; + } +backup_start_jobs (); + goto out; + } + } + } + +out: +if (restart) { +vm_start (); + } +return ; + +abort: + backup_state.end_time =time (NULL ); + +Error *local_err =NULL ; + backup_state.driver->close_cb (backup_state.writer, &local_err); + backup_state.writer =NULL ; + +error_propagate (&backup_state.error, local_err); + +if (err) { +if (!backup_state.error) { +error_setg (&backup_state.error,"%s" , err); + } +g_free (err); + } + + goto out; +} +char *qmp_backup (const char *backupfile,bool has_format,const char *format,bool has_config_filename,const char *config_filename,bool has_devlist,const char *devlist, -bool has_speed,int64_t speed,Error **errp) +bool has_speed,int64_t speed, +bool has_state,bool state,Error **errp) {BlockDriverState *bs;Error *local_err =NULL ;
@@ -1442,6 +1608,8 @@ char *qmp_backup(const char *backupfile, bool has_format, const char *format,gchar **devs =NULL ;GList *bcblist =NULL ; +bool save_state = has_state ? state : false; +/* Todo: try to auto-detect format based on file name */ format = has_format ? format :"vma" ;
@@ -1523,6 +1691,22 @@ char *qmp_backup(const char *backupfile, bool has_format, const char *format,size_t total =0 ;/* register all devices for vma writer */ + +guint8 vmstate_dev_id =0 ; +if (save_state) { +/* Note: we pass ram_bytes_total() for vmstate size +* The backup driver needs to be aware of the fact +* that the real stream size can be different (we do +* not know that size in advance). +*/ +size_t ramsize =ram_bytes_total (); + vmstate_dev_id = driver->register_stream_cb (writer,"vmstate" , ramsize); +if (vmstate_dev_id <=0 ) { +error_setg (errp,"register vmstate stream failed" ); + goto err; + } + } + l = bcblist;while (l) {BackupCB *bcb = l->data;
@@ -1586,8 +1770,16 @@ char *qmp_backup(const char *backupfile, bool has_format, const char *format, backup_state.total = total; backup_state.transferred =0 ; backup_state.zero_bytes =0 ; + backup_state.buf_index =0 ; + backup_state.buf_cluster_num =0 ; + backup_state.vmstate_dev_id = vmstate_dev_id; -backup_start_jobs (); +if (save_state) { +Coroutine *co =qemu_coroutine_create (backup_start_savevm); +qemu_coroutine_enter (co,NULL ); + }else { +backup_start_jobs (); + }return g_strdup (backup_state.uuid_str);
hmp.c +1 -1
@@ -1031,7 +1031,7 @@ void hmp_backup(Monitor *mon, const QDict *qdict)Error *errp =NULL ;qmp_backup (backupfile, false,NULL , false,NULL , !!devlist, devlist, -qdict_haskey (qdict,"speed" ), speed, &errp); +qdict_haskey (qdict,"speed" ), speed, false, false, &errp);if (error_is_set (&errp)) {monitor_printf (mon,"%s\n" ,error_get_pretty (errp));
qapi-schema.json +4 -2
@@ -1812,13 +1812,15 @@ # # @speed: #optional the maximum speed, in bytes per second # +# @state: #optional flag to include vm state +# # Returns: the uuid of the backup job # # Since: 1.4.0 ## { 'command': 'backup', 'data': { 'backupfile': 'str', '*format': 'str', - '*config-filename': 'str', - '*devlist': 'str', '*speed': 'int' }, + '*config-filename': 'str', '*devlist': 'str', + '*speed': 'int', '*state': 'bool' }, 'returns': 'str' } ##
Signature · 1 line
1.7.2.5
Re: [Qemu-devel] [PATCH v2 6/6] add vm state to backups
Quoted 60 lines
blockdev.c +60
-0+static void coroutine_fn backup_start_savevm(void *opaque) +{ +assert (backup_state.driver); +assert (backup_state.writer); +int ret; +char *err =NULL ; +uint64_t remaining; +int64_t maxlen; +MigrationParams params = { + .blk =0 , + .shared =0 + }; + +int restart =0 ; + +QEMUFile *file =qemu_fopen_ops (NULL , &backup_file_ops); + + ret =qemu_savevm_state_begin (file, ¶ms); +if (ret <0 ) { +qemu_fclose (file); + err =g_strdup ("qemu_savevm_state_begin failed" ); + goto abort; + } + +while (1 ) { + ret =qemu_savevm_state_iterate (file); + remaining =ram_bytes_remaining (); + +if (ret <0 ) { +qemu_fclose (file); + err =g_strdup_printf ("qemu_savevm_state_iterate error %d" , ret); + goto abort; + } + +/* stop the VM if we use too much space, +* or if remaining is just a few MB +*/ + maxlen =ram_bytes_total (); +size_t cpos = backup_state.buf_cluster_num *BACKUP_CLUSTER_SIZE ; +if ((remaining <100000 ) || ((cpos + remaining) >= maxlen)) { +if (runstate_is_running ()) { + restart =1 ; +vm_stop (RUN_STATE_SAVE_VM ); + } + } + +if (ret ==1 ) {/* finished */ +if (runstate_is_running ()) { + restart =1 ; +vm_stop (RUN_STATE_SAVE_VM ); + } + + ret =qemu_savevm_state_complete (file); +if (ret <0 ) { +qemu_fclose (file); + err =g_strdup ("qemu_savevm_state_complete error" ); + goto abort; + + }else { +if (qemu_fclose (file) <0 ) {
blockdev.c static void backup_start_jobs(void) · line 1567
Quoted 10 lines
blockdev.c +10
-0+error_setg (&backup_state.error, +"backup_start_savevm: qemu_fclose failed" ); + goto abort; + } +if (backup_state.driver->complete_cb (backup_state.writer, + backup_state.vmstate_dev_id,0 ) <0 ) { + err =g_strdup ("backup_start_savevm: complete_cb failed" ); + goto abort; + } +backup_start_jobs ();
backup_start_jobs() was called after qemu_savevm_state_complete(),
and then VM got resumed with block backup jobs alone, would this cause saved vm state not sync with the saved block contents, if there is vmstate changing after backup_start_jobs()?
Quoted 4 lines
blockdev.c +4
-0+ goto out; + } + } + }
Signature · 3 lines
Best Regards Wenchao Xia