1 From d97630338d62186229192d0724fae489fc500acc Mon Sep 17 00:00:00 2001
2 From: Dietmar Maurer <dietmar@proxmox.com>
3 Date: Tue, 13 Nov 2012 11:27:56 +0100
4 Subject: [PATCH v3 3/6] add backup related monitor commands
6 We use a generic BackupDriver struct to encapsulate all archive format
9 Another option would be to simply dump <devid,cluster_num,cluster_data> to
10 the output fh (pipe), and an external binary saves the data. That way we
11 could move the whole archive format related code out of qemu.
13 Signed-off-by: Dietmar Maurer <dietmar@proxmox.com>
16 blockdev.c | 375 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
17 hmp-commands.hx | 31 +++++
21 qapi-schema.json | 91 +++++++++++++
22 qmp-commands.hx | 27 ++++
23 8 files changed, 610 insertions(+), 0 deletions(-)
25 diff --git a/backup.h b/backup.h
26 index e1f0290..ae4aa8c 100644
29 @@ -27,4 +27,17 @@ int backup_job_start(BlockDriverState *bs, BackupDumpFunc *backup_dump_cb,
30 BlockDriverCompletionFunc *backup_complete_cb,
33 +typedef struct BackupDriver {
35 + void *(*open_cb)(const char *filename, uuid_t uuid, int64_t speed,
37 + int (*close_cb)(void *opaque, Error **errp);
38 + int (*register_config_cb)(void *opaque, const char *name, gpointer data,
40 + int (*register_stream_cb)(void *opaque, const char *devname, size_t size);
41 + int (*dump_cb)(void *opaque, uint8_t dev_id, int64_t cluster_num,
42 + unsigned char *buf, size_t *zero_bytes);
43 + int (*complete_cb)(void *opaque, uint8_t dev_id, int ret);
46 #endif /* QEMU_BACKUP_H */
47 diff --git a/blockdev.c b/blockdev.c
48 index e73fd6e..08ac7ad 100644
52 #include "qmp-commands.h"
54 #include "arch_init.h"
57 static QTAILQ_HEAD(drivelist, DriveInfo) drives = QTAILQ_HEAD_INITIALIZER(drives);
59 @@ -1321,6 +1322,380 @@ void qmp_drive_mirror(const char *device, const char *target,
60 drive_get_ref(drive_get_by_blockdev(bs));
63 +/* Backup related function */
65 +static struct GenericBackupState {
72 + const BackupDriver *driver;
80 +typedef struct BackupCB {
81 + BlockDriverState *bs;
89 +static int backup_dump_cb(void *opaque, BlockDriverState *bs,
90 + int64_t cluster_num, unsigned char *buf)
92 + BackupCB *bcb = opaque;
94 + assert(backup_state.driver);
95 + assert(backup_state.writer);
96 + assert(backup_state.driver->dump_cb);
98 + size_t zero_bytes = 0;
99 + int bytes = backup_state.driver->dump_cb(backup_state.writer,
100 + bcb->dev_id, cluster_num,
104 + bcb->transferred += bytes;
105 + backup_state.transferred += bytes;
107 + bcb->zero_bytes += bytes;
108 + backup_state.zero_bytes += zero_bytes;
115 +static void backup_cleanup(void)
117 + if (backup_state.writer && backup_state.driver) {
118 + backup_state.end_time = time(NULL);
119 + Error *local_err = NULL;
120 + backup_state.driver->close_cb(backup_state.writer, &local_err);
121 + error_propagate(&backup_state.error, local_err);
122 + backup_state.writer = NULL;
124 + GList *l = backup_state.bcb_list;
127 + l = g_list_next(l);
129 + g_list_free(backup_state.bcb_list);
130 + backup_state.bcb_list = NULL;
134 +static void backup_complete_cb(void *opaque, int ret)
136 + BackupCB *bcb = opaque;
138 + assert(backup_state.driver);
139 + assert(backup_state.writer);
140 + assert(backup_state.driver->complete_cb);
141 + assert(backup_state.driver->close_cb);
143 + drive_put_ref_bh_schedule(drive_get_by_blockdev(bcb->bs));
145 + backup_state.bcb_list = g_list_remove(backup_state.bcb_list, bcb);
147 + backup_state.driver->complete_cb(backup_state.writer, bcb->dev_id, ret);
149 + if (g_list_length(backup_state.bcb_list) == 0) {
156 +static void backup_cancel(void)
158 + GList *l = backup_state.bcb_list;
162 + BackupCB *bcb = l->data;
163 + l = g_list_next(l);
164 + if (bcb->bs->job && bcb->job_started) {
165 + block_job_cancel(bcb->bs->job);
175 +void qmp_backup_cancel(Error **errp)
177 + if (!backup_state.error) {
178 + error_setg(&backup_state.error, "backup cancelled");
184 +static void backup_start_jobs(void)
186 + /* start all jobs (one for each device) */
187 + GList *l = backup_state.bcb_list;
189 + BackupCB *bcb = l->data;
190 + l = g_list_next(l);
192 + if (backup_job_start(bcb->bs, backup_dump_cb, backup_complete_cb,
194 + bcb->job_started = true;
195 + /* Grab a reference so hotplug does not delete the
196 + * BlockDriverState from underneath us.
198 + drive_get_ref(drive_get_by_blockdev(bcb->bs));
200 + if (!backup_state.error) {
201 + error_setg(&backup_state.error, "backup_job_start failed");
209 +char *qmp_backup(const char *backupfile, bool has_format, BackupFormat format,
210 + bool has_config_filename, const char *config_filename,
211 + bool has_devlist, const char *devlist,
212 + bool has_speed, int64_t speed, Error **errp)
214 + BlockDriverState *bs;
215 + Error *local_err = NULL;
217 + void *writer = NULL;
218 + gchar **devs = NULL;
219 + GList *bcblist = NULL;
221 + /* Todo: try to auto-detect format based on file name */
222 + format = has_format ? format : BACKUP_FORMAT_VMA;
224 + /* fixme: find driver for specifued format */
225 + const BackupDriver *driver = NULL;
228 + error_set(errp, ERROR_CLASS_GENERIC_ERROR, "unknown backup format");
233 + devs = g_strsplit_set(devlist, ",;:", -1);
237 + bs = bdrv_find(*d);
239 + if (bdrv_is_read_only(bs)) {
240 + error_set(errp, QERR_DEVICE_IS_READ_ONLY, *d);
243 + if (!bdrv_is_inserted(bs)) {
244 + error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, *d);
247 + BackupCB *bcb = g_new0(BackupCB, 1);
249 + bcblist = g_list_append(bcblist, bcb);
251 + error_set(errp, QERR_DEVICE_NOT_FOUND, *d);
260 + while ((bs = bdrv_next(bs))) {
262 + if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
266 + BackupCB *bcb = g_new0(BackupCB, 1);
268 + bcblist = g_list_append(bcblist, bcb);
273 + error_set(errp, ERROR_CLASS_GENERIC_ERROR, "empty device list");
277 + GList *l = bcblist;
279 + BackupCB *bcb = l->data;
280 + l = g_list_next(l);
281 + if (bcb->bs->job) {
282 + error_set(errp, QERR_DEVICE_IN_USE, bdrv_get_device_name(bcb->bs));
287 + uuid_generate(uuid);
289 + writer = driver->open_cb(backupfile, uuid, speed, &local_err);
291 + if (error_is_set(&local_err)) {
292 + error_propagate(errp, local_err);
299 + /* register all devices for vma writer */
302 + BackupCB *bcb = l->data;
303 + l = g_list_next(l);
305 + int64_t size = bdrv_getlength(bcb->bs);
306 + const char *devname = bdrv_get_device_name(bcb->bs);
307 + bcb->dev_id = driver->register_stream_cb(writer, devname, size);
308 + if (bcb->dev_id <= 0) {
309 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
310 + "register_stream failed");
317 + /* add configuration file to archive */
318 + if (has_config_filename) {
319 + char *cdata = NULL;
321 + GError *err = NULL;
322 + if (!g_file_get_contents(config_filename, &cdata, &clen, &err)) {
323 + error_setg(errp, "unable to read file '%s'", config_filename);
327 + const char *basename = g_path_get_basename(config_filename);
328 + if (driver->register_config_cb(writer, basename, cdata, clen) < 0) {
329 + error_setg(errp, "register_config failed");
336 + /* initialize global backup_state now */
338 + if (backup_state.error) {
339 + error_free(backup_state.error);
340 + backup_state.error = NULL;
343 + backup_state.driver = driver;
345 + backup_state.start_time = time(NULL);
346 + backup_state.end_time = 0;
348 + if (backup_state.backupfile) {
349 + g_free(backup_state.backupfile);
351 + backup_state.backupfile = g_strdup(backupfile);
353 + backup_state.writer = writer;
355 + uuid_copy(backup_state.uuid, uuid);
356 + uuid_unparse_lower(uuid, backup_state.uuid_str);
358 + backup_state.bcb_list = bcblist;
360 + backup_state.total = total;
361 + backup_state.transferred = 0;
362 + backup_state.zero_bytes = 0;
364 + backup_start_jobs();
366 + return g_strdup(backup_state.uuid_str);
373 + l = g_list_next(l);
375 + g_list_free(bcblist);
382 + unlink(backupfile);
385 + driver->close_cb(writer, &err);
392 +BackupStatus *qmp_query_backup(Error **errp)
394 + BackupStatus *info = g_malloc0(sizeof(*info));
396 + if (!backup_state.start_time) {
397 + /* not started, return {} */
401 + info->has_status = true;
402 + info->has_start_time = true;
403 + info->start_time = backup_state.start_time;
405 + if (backup_state.backupfile) {
406 + info->has_backupfile = true;
407 + info->backupfile = g_strdup(backup_state.backupfile);
410 + info->has_uuid = true;
411 + info->uuid = g_strdup(backup_state.uuid_str);
413 + if (backup_state.end_time) {
414 + if (backup_state.error) {
415 + info->status = g_strdup("error");
416 + info->has_errmsg = true;
417 + info->errmsg = g_strdup(error_get_pretty(backup_state.error));
419 + info->status = g_strdup("done");
421 + info->has_end_time = true;
422 + info->end_time = backup_state.end_time;
424 + info->status = g_strdup("active");
427 + info->has_total = true;
428 + info->total = backup_state.total;
429 + info->has_zero_bytes = true;
430 + info->zero_bytes = backup_state.zero_bytes;
431 + info->has_transferred = true;
432 + info->transferred = backup_state.transferred;
437 static BlockJob *find_block_job(const char *device)
439 BlockDriverState *bs;
440 diff --git a/hmp-commands.hx b/hmp-commands.hx
441 index 010b8c9..57be357 100644
442 --- a/hmp-commands.hx
443 +++ b/hmp-commands.hx
444 @@ -83,6 +83,35 @@ STEXI
445 Copy data from a backing file into a block device.
450 + .args_type = "backupfile:s,speed:o?,devlist:s?",
451 + .params = "backupfile [speed [devlist]]",
452 + .help = "create a VM Backup.",
453 + .mhandler.cmd = hmp_backup,
463 + .name = "backup_cancel",
466 + .help = "cancel the current VM backup",
467 + .mhandler.cmd = hmp_backup_cancel,
472 +@findex backup_cancel
473 +Cancel the current VM backup.
478 .name = "block_job_set_speed",
479 .args_type = "device:B,speed:o",
480 @@ -1558,6 +1587,8 @@ show CPU statistics
481 show user network stack connection states
483 show migration status
486 @item info migrate_capabilities
487 show current migration capabilities
488 @item info migrate_cache_size
489 diff --git a/hmp.c b/hmp.c
490 index 180ba2b..77076fa 100644
493 @@ -130,6 +130,38 @@ void hmp_info_mice(Monitor *mon)
494 qapi_free_MouseInfoList(mice_list);
497 +void hmp_info_backup(Monitor *mon)
499 + BackupStatus *info;
501 + info = qmp_query_backup(NULL);
502 + if (info->has_status) {
503 + if (info->has_errmsg) {
504 + monitor_printf(mon, "Backup status: %s - %s\n",
505 + info->status, info->errmsg);
507 + monitor_printf(mon, "Backup status: %s\n", info->status);
510 + if (info->has_backupfile) {
511 + int per = (info->has_total && info->total &&
512 + info->has_transferred && info->transferred) ?
513 + (info->transferred * 100)/info->total : 0;
514 + int zero_per = (info->has_total && info->total &&
515 + info->has_zero_bytes && info->zero_bytes) ?
516 + (info->zero_bytes * 100)/info->total : 0;
517 + monitor_printf(mon, "Backup file: %s\n", info->backupfile);
518 + monitor_printf(mon, "Backup uuid: %s\n", info->uuid);
519 + monitor_printf(mon, "Total size: %zd\n", info->total);
520 + monitor_printf(mon, "Transferred bytes: %zd (%d%%)\n",
521 + info->transferred, per);
522 + monitor_printf(mon, "Zero bytes: %zd (%d%%)\n",
523 + info->zero_bytes, zero_per);
526 + qapi_free_BackupStatus(info);
529 void hmp_info_migrate(Monitor *mon)
532 @@ -977,6 +1009,37 @@ void hmp_block_stream(Monitor *mon, const QDict *qdict)
533 hmp_handle_error(mon, &error);
536 +void hmp_backup_cancel(Monitor *mon, const QDict *qdict)
538 + Error *errp = NULL;
540 + qmp_backup_cancel(&errp);
542 + if (error_is_set(&errp)) {
543 + monitor_printf(mon, "%s\n", error_get_pretty(errp));
549 +void hmp_backup(Monitor *mon, const QDict *qdict)
551 + const char *backupfile = qdict_get_str(qdict, "backupfile");
552 + const char *devlist = qdict_get_try_str(qdict, "devlist");
553 + int64_t speed = qdict_get_try_int(qdict, "speed", 0);
555 + Error *errp = NULL;
557 + qmp_backup(backupfile, true, BACKUP_FORMAT_VMA, false, NULL, !!devlist,
558 + devlist, qdict_haskey(qdict, "speed"), speed, &errp);
560 + if (error_is_set(&errp)) {
561 + monitor_printf(mon, "%s\n", error_get_pretty(errp));
567 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
570 diff --git a/hmp.h b/hmp.h
571 index 0ab03be..20c9a62 100644
574 @@ -28,6 +28,7 @@ void hmp_info_mice(Monitor *mon);
575 void hmp_info_migrate(Monitor *mon);
576 void hmp_info_migrate_capabilities(Monitor *mon);
577 void hmp_info_migrate_cache_size(Monitor *mon);
578 +void hmp_info_backup(Monitor *mon);
579 void hmp_info_cpus(Monitor *mon);
580 void hmp_info_block(Monitor *mon);
581 void hmp_info_blockstats(Monitor *mon);
582 @@ -63,6 +64,8 @@ void hmp_eject(Monitor *mon, const QDict *qdict);
583 void hmp_change(Monitor *mon, const QDict *qdict);
584 void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict);
585 void hmp_block_stream(Monitor *mon, const QDict *qdict);
586 +void hmp_backup(Monitor *mon, const QDict *qdict);
587 +void hmp_backup_cancel(Monitor *mon, const QDict *qdict);
588 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict);
589 void hmp_block_job_cancel(Monitor *mon, const QDict *qdict);
590 void hmp_block_job_pause(Monitor *mon, const QDict *qdict);
591 diff --git a/monitor.c b/monitor.c
592 index c0e32d6..85cf47e 100644
595 @@ -2680,6 +2680,13 @@ static mon_cmd_t info_cmds[] = {
602 + .help = "show backup status",
603 + .mhandler.info = hmp_info_backup,
609 diff --git a/qapi-schema.json b/qapi-schema.json
610 index 5dfa052..2d3699b 100644
611 --- a/qapi-schema.json
612 +++ b/qapi-schema.json
614 { 'type': 'EventInfo', 'data': {'name': 'str'} }
619 +# Detailed backup status.
621 +# @status: #optional string describing the current backup status.
622 +# This can be 'active', 'done', 'error'. If this field is not
623 +# returned, no backup process has been initiated
625 +# @errmsg: #optional error message (only returned if status is 'error')
627 +# @total: #optional total amount of bytes involved in the backup process
629 +# @transferred: #optional amount of bytes already backed up.
631 +# @zero-bytes: #optional amount of 'zero' bytes detected.
633 +# @start-time: #optional time (epoch) when backup job started.
635 +# @end-time: #optional time (epoch) when backup job finished.
637 +# @backupfile: #optional backup file name
639 +# @uuid: #optional uuid for this backup job
643 +{ 'type': 'BackupStatus',
644 + 'data': {'*status': 'str', '*errmsg': 'str', '*total': 'int',
645 + '*transferred': 'int', '*zero-bytes': 'int',
646 + '*start-time': 'int', '*end-time': 'int',
647 + '*backupfile': 'str', '*uuid': 'str' } }
652 # Return a list of supported QMP events by this server
653 @@ -1764,6 +1797,64 @@
654 'data': { 'path': 'str' },
655 'returns': [ 'ObjectPropertyInfo' ] }
661 +# An enumeration of supported backup formats.
663 +# @vma: Proxmox vma backup format
665 +{ 'enum': 'BackupFormat',
666 + 'data': [ 'vma' ] }
671 +# Starts a VM backup.
673 +# @backupfile: the backup file name
675 +# @format: format of the backup file
677 +# @config-filename: #optional name of a configuration file to include into
678 +# the backup archive.
680 +# @speed: #optional the maximum speed, in bytes per second
682 +# Returns: the uuid of the backup job
686 +{ 'command': 'backup', 'data': { 'backupfile': 'str', '*format': 'BackupFormat',
687 + '*config-filename': 'str',
688 + '*devlist': 'str', '*speed': 'int' },
694 +# Returns information about current/last backup task.
696 +# Returns: @BackupStatus
700 +{ 'command': 'query-backup', 'returns': 'BackupStatus' }
705 +# Cancel the current executing backup process.
707 +# Returns: nothing on success
709 +# Notes: This command succeeds even if there is no backup process running.
713 +{ 'command': 'backup-cancel' }
718 diff --git a/qmp-commands.hx b/qmp-commands.hx
719 index 5c692d0..c46fdc4 100644
720 --- a/qmp-commands.hx
721 +++ b/qmp-commands.hx
722 @@ -822,6 +822,18 @@ EQMP
727 + .args_type = "backupfile:s,format:s?,config-filename:F?,speed:o?,devlist:s?",
728 + .mhandler.cmd_new = qmp_marshal_input_backup,
732 + .name = "backup_cancel",
734 + .mhandler.cmd_new = qmp_marshal_input_backup_cancel,
738 .name = "block-job-set-speed",
739 .args_type = "device:B,speed:o",
740 .mhandler.cmd_new = qmp_marshal_input_block_job_set_speed,
741 @@ -2491,6 +2503,21 @@ EQMP
754 + .name = "query-backup",
756 + .mhandler.cmd_new = qmp_marshal_input_query_backup,
760 migrate-set-capabilities