1 From 83fc57d04020d8d9aebd7b9676bee0be099421d8 Mon Sep 17 00:00:00 2001
2 From: Wolfgang Bumiller <w.bumiller@proxmox.com>
3 Date: Wed, 9 Dec 2015 15:20:56 +0100
4 Subject: [PATCH 15/41] backup: add pve monitor commands
7 blockdev.c | 439 ++++++++++++++++++++++++++++++++++++++++++++++
9 hmp-commands-info.hx | 13 ++
10 hmp-commands.hx | 29 +++
13 include/block/block_int.h | 2 +-
14 qapi-schema.json | 89 ++++++++++
15 qmp-commands.hx | 18 ++
16 9 files changed, 655 insertions(+), 2 deletions(-)
18 diff --git a/blockdev.c b/blockdev.c
19 index 5e3707d..5417bb0 100644
23 #include "sysemu/arch_init.h"
24 #include "qemu/cutils.h"
25 #include "qemu/help_option.h"
28 static QTAILQ_HEAD(, BlockDriverState) monitor_bdrv_states =
29 QTAILQ_HEAD_INITIALIZER(monitor_bdrv_states);
30 @@ -2976,6 +2977,444 @@ static void block_job_cb(void *opaque, int ret)
34 +/* PVE backup related function */
36 +static struct PVEBackupState {
52 +typedef struct PVEBackupDevInfo {
53 + BlockDriverState *bs;
60 +static void pvebackup_run_next_job(void);
62 +static int pvebackup_dump_cb(void *opaque, BlockBackend *target,
63 + int64_t sector_num, int n_sectors,
66 + PVEBackupDevInfo *di = opaque;
68 + if (sector_num & 0x7f) {
69 + if (!backup_state.error) {
70 + error_setg(&backup_state.error,
71 + "got unaligned write inside backup dump "
72 + "callback (sector %ld)", sector_num);
74 + return -1; // not aligned to cluster size
77 + int64_t cluster_num = sector_num >> 7;
78 + int size = n_sectors * BDRV_SECTOR_SIZE;
82 + if (backup_state.vmaw) {
83 + size_t zero_bytes = 0;
84 + ret = vma_writer_write(backup_state.vmaw, di->dev_id, cluster_num,
86 + backup_state.zero_bytes += zero_bytes;
90 + backup_state.zero_bytes += size;
94 + backup_state.transferred += size;
99 +static void pvebackup_cleanup(void)
101 + backup_state.end_time = time(NULL);
103 + if (backup_state.vmaw) {
104 + Error *local_err = NULL;
105 + vma_writer_close(backup_state.vmaw, &local_err);
106 + error_propagate(&backup_state.error, local_err);
107 + backup_state.vmaw = NULL;
110 + if (backup_state.di_list) {
111 + GList *l = backup_state.di_list;
113 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
114 + l = g_list_next(l);
117 + g_list_free(backup_state.di_list);
118 + backup_state.di_list = NULL;
122 +static void pvebackup_complete_cb(void *opaque, int ret)
124 + PVEBackupDevInfo *di = opaque;
126 + assert(backup_state.vmaw);
128 + di->completed = true;
130 + if (ret < 0 && !backup_state.error) {
131 + error_setg(&backup_state.error, "job failed with err %d - %s",
132 + ret, strerror(-ret));
135 + BlockDriverState *bs = di->bs;
139 + vma_writer_close_stream(backup_state.vmaw, di->dev_id);
141 + block_job_cb(bs, ret);
143 + if (!backup_state.cancel) {
144 + pvebackup_run_next_job();
148 +static void pvebackup_cancel(void *opaque)
150 + backup_state.cancel = true;
152 + if (!backup_state.error) {
153 + error_setg(&backup_state.error, "backup cancelled");
156 + /* drain all i/o (awake jobs waiting for aio) */
159 + GList *l = backup_state.di_list;
161 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
162 + l = g_list_next(l);
163 + if (!di->completed && di->bs) {
164 + BlockJob *job = di->bs->job;
166 + if (!di->completed) {
167 + block_job_cancel_sync(job);
173 + pvebackup_cleanup();
176 +void qmp_backup_cancel(Error **errp)
178 + Coroutine *co = qemu_coroutine_create(pvebackup_cancel, NULL);
179 + qemu_coroutine_enter(co);
181 + while (backup_state.vmaw) {
182 + /* vma writer use main aio context */
183 + aio_poll(qemu_get_aio_context(), true);
187 +bool block_job_should_pause(BlockJob *job);
188 +static void pvebackup_run_next_job(void)
190 + GList *l = backup_state.di_list;
192 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
193 + l = g_list_next(l);
194 + if (!di->completed && di->bs && di->bs->job) {
195 + BlockJob *job = di->bs->job;
196 + if (block_job_should_pause(job)) {
197 + bool cancel = backup_state.error || backup_state.cancel;
199 + block_job_cancel(job);
201 + block_job_resume(job);
208 + pvebackup_cleanup();
211 +UuidInfo *qmp_backup(const char *backup_file, bool has_format,
212 + BackupFormat format,
213 + bool has_config_file, const char *config_file,
214 + bool has_devlist, const char *devlist,
215 + bool has_speed, int64_t speed, Error **errp)
218 + BlockDriverState *bs = NULL;
219 + Error *local_err = NULL;
221 + VmaWriter *vmaw = NULL;
222 + gchar **devs = NULL;
223 + GList *di_list = NULL;
225 + UuidInfo *uuid_info;
227 + if (backup_state.di_list) {
228 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
229 + "previous backup not finished");
233 + /* Todo: try to auto-detect format based on file name */
234 + format = has_format ? format : BACKUP_FORMAT_VMA;
236 + if (format != BACKUP_FORMAT_VMA) {
237 + error_set(errp, ERROR_CLASS_GENERIC_ERROR, "unknown backup format");
242 + devs = g_strsplit_set(devlist, ",;:", -1);
246 + blk = blk_by_name(*d);
249 + if (bdrv_is_read_only(bs)) {
250 + error_setg(errp, "Node '%s' is read only", *d);
253 + if (!bdrv_is_inserted(bs)) {
254 + error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, *d);
257 + PVEBackupDevInfo *di = g_new0(PVEBackupDevInfo, 1);
259 + di_list = g_list_append(di_list, di);
261 + error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
262 + "Device '%s' not found", *d);
269 + BdrvNextIterator it;
272 + for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
273 + if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
277 + PVEBackupDevInfo *di = g_new0(PVEBackupDevInfo, 1);
279 + di_list = g_list_append(di_list, di);
284 + error_set(errp, ERROR_CLASS_GENERIC_ERROR, "empty device list");
292 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
293 + l = g_list_next(l);
294 + if (bdrv_op_is_blocked(di->bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
298 + ssize_t size = bdrv_getlength(di->bs);
300 + error_setg_errno(errp, -di->size, "bdrv_getlength failed");
307 + uuid_generate(uuid);
309 + vmaw = vma_writer_create(backup_file, uuid, &local_err);
312 + error_propagate(errp, local_err);
317 + /* register all devices for vma writer */
320 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
321 + l = g_list_next(l);
323 + const char *devname = bdrv_get_device_name(di->bs);
324 + di->dev_id = vma_writer_register_stream(vmaw, devname, di->size);
325 + if (di->dev_id <= 0) {
326 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
327 + "register_stream failed");
332 + /* add configuration file to archive */
333 + if (has_config_file) {
334 + char *cdata = NULL;
336 + GError *err = NULL;
337 + if (!g_file_get_contents(config_file, &cdata, &clen, &err)) {
338 + error_setg(errp, "unable to read file '%s'", config_file);
342 + const char *basename = g_path_get_basename(config_file);
343 + if (vma_writer_add_config(vmaw, basename, cdata, clen) != 0) {
344 + error_setg(errp, "unable to add config data to vma archive");
351 + /* initialize global backup_state now */
353 + backup_state.cancel = false;
355 + if (backup_state.error) {
356 + error_free(backup_state.error);
357 + backup_state.error = NULL;
360 + backup_state.speed = (has_speed && speed > 0) ? speed : 0;
362 + backup_state.start_time = time(NULL);
363 + backup_state.end_time = 0;
365 + if (backup_state.backup_file) {
366 + g_free(backup_state.backup_file);
368 + backup_state.backup_file = g_strdup(backup_file);
370 + backup_state.vmaw = vmaw;
372 + uuid_copy(backup_state.uuid, uuid);
373 + uuid_unparse_lower(uuid, backup_state.uuid_str);
375 + backup_state.di_list = di_list;
377 + backup_state.total = total;
378 + backup_state.transferred = 0;
379 + backup_state.zero_bytes = 0;
381 + /* start all jobs (paused state) */
384 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
385 + l = g_list_next(l);
387 + backup_start(NULL, di->bs, NULL, speed, MIRROR_SYNC_MODE_FULL, NULL,
388 + BLOCKDEV_ON_ERROR_REPORT, BLOCKDEV_ON_ERROR_REPORT,
389 + pvebackup_dump_cb, pvebackup_complete_cb, di,
390 + 1, NULL, &local_err);
391 + if (local_err != NULL) {
392 + error_setg(&backup_state.error, "backup_job_create failed");
393 + pvebackup_cancel(NULL);
397 + if (!backup_state.error) {
398 + pvebackup_run_next_job(); // run one job
401 + uuid_info = g_malloc0(sizeof(*uuid_info));
402 + uuid_info->UUID = g_strdup(backup_state.uuid_str);
410 + l = g_list_next(l);
412 + g_list_free(di_list);
420 + vma_writer_close(vmaw, &err);
421 + unlink(backup_file);
427 +BackupStatus *qmp_query_backup(Error **errp)
429 + BackupStatus *info = g_malloc0(sizeof(*info));
431 + if (!backup_state.start_time) {
432 + /* not started, return {} */
436 + info->has_status = true;
437 + info->has_start_time = true;
438 + info->start_time = backup_state.start_time;
440 + if (backup_state.backup_file) {
441 + info->has_backup_file = true;
442 + info->backup_file = g_strdup(backup_state.backup_file);
445 + info->has_uuid = true;
446 + info->uuid = g_strdup(backup_state.uuid_str);
448 + if (backup_state.end_time) {
449 + if (backup_state.error) {
450 + info->status = g_strdup("error");
451 + info->has_errmsg = true;
452 + info->errmsg = g_strdup(error_get_pretty(backup_state.error));
454 + info->status = g_strdup("done");
456 + info->has_end_time = true;
457 + info->end_time = backup_state.end_time;
459 + info->status = g_strdup("active");
462 + info->has_total = true;
463 + info->total = backup_state.total;
464 + info->has_zero_bytes = true;
465 + info->zero_bytes = backup_state.zero_bytes;
466 + info->has_transferred = true;
467 + info->transferred = backup_state.transferred;
472 void qmp_block_stream(bool has_job_id, const char *job_id, const char *device,
473 bool has_base, const char *base,
474 bool has_backing_file, const char *backing_file,
475 diff --git a/blockjob.c b/blockjob.c
476 index a5ba3be..a550458 100644
479 @@ -331,7 +331,8 @@ void block_job_pause(BlockJob *job)
483 -static bool block_job_should_pause(BlockJob *job)
484 +bool block_job_should_pause(BlockJob *job);
485 +bool block_job_should_pause(BlockJob *job)
487 return job->pause_count > 0;
489 diff --git a/hmp-commands-info.hx b/hmp-commands-info.hx
490 index 74446c6..7616fe2 100644
491 --- a/hmp-commands-info.hx
492 +++ b/hmp-commands-info.hx
493 @@ -502,6 +502,19 @@ STEXI
501 + .help = "show backup status",
502 + .mhandler.cmd = hmp_info_backup,
510 #if defined(CONFIG_SLIRP)
513 diff --git a/hmp-commands.hx b/hmp-commands.hx
514 index 848efee..8f2f3e0 100644
515 --- a/hmp-commands.hx
516 +++ b/hmp-commands.hx
517 @@ -87,6 +87,35 @@ STEXI
518 Copy data from a backing file into a block device.
523 + .args_type = "backupfile:s,speed:o?,devlist:s?",
524 + .params = "backupfile [speed [devlist]]",
525 + .help = "create a VM Backup.",
526 + .mhandler.cmd = hmp_backup,
536 + .name = "backup_cancel",
539 + .help = "cancel the current VM backup",
540 + .mhandler.cmd = hmp_backup_cancel,
545 +@findex backup_cancel
546 +Cancel the current VM backup.
551 .name = "block_job_set_speed",
552 .args_type = "device:B,speed:o",
553 diff --git a/hmp.c b/hmp.c
554 index aa1395d..88cb4e5 100644
557 @@ -149,6 +149,44 @@ void hmp_info_mice(Monitor *mon, const QDict *qdict)
558 qapi_free_MouseInfoList(mice_list);
561 +void hmp_info_backup(Monitor *mon, const QDict *qdict)
563 + BackupStatus *info;
565 + info = qmp_query_backup(NULL);
566 + if (info->has_status) {
567 + if (info->has_errmsg) {
568 + monitor_printf(mon, "Backup status: %s - %s\n",
569 + info->status, info->errmsg);
571 + monitor_printf(mon, "Backup status: %s\n", info->status);
575 + if (info->has_backup_file) {
576 + monitor_printf(mon, "Start time: %s", ctime(&info->start_time));
577 + if (info->end_time) {
578 + monitor_printf(mon, "End time: %s", ctime(&info->end_time));
581 + int per = (info->has_total && info->total &&
582 + info->has_transferred && info->transferred) ?
583 + (info->transferred * 100)/info->total : 0;
584 + int zero_per = (info->has_total && info->total &&
585 + info->has_zero_bytes && info->zero_bytes) ?
586 + (info->zero_bytes * 100)/info->total : 0;
587 + monitor_printf(mon, "Backup file: %s\n", info->backup_file);
588 + monitor_printf(mon, "Backup uuid: %s\n", info->uuid);
589 + monitor_printf(mon, "Total size: %zd\n", info->total);
590 + monitor_printf(mon, "Transferred bytes: %zd (%d%%)\n",
591 + info->transferred, per);
592 + monitor_printf(mon, "Zero bytes: %zd (%d%%)\n",
593 + info->zero_bytes, zero_per);
596 + qapi_free_BackupStatus(info);
599 void hmp_info_migrate(Monitor *mon, const QDict *qdict)
602 @@ -1492,6 +1530,29 @@ void hmp_block_stream(Monitor *mon, const QDict *qdict)
603 hmp_handle_error(mon, &error);
606 +void hmp_backup_cancel(Monitor *mon, const QDict *qdict)
608 + Error *error = NULL;
610 + qmp_backup_cancel(&error);
612 + hmp_handle_error(mon, &error);
615 +void hmp_backup(Monitor *mon, const QDict *qdict)
617 + Error *error = NULL;
619 + const char *backup_file = qdict_get_str(qdict, "backupfile");
620 + const char *devlist = qdict_get_try_str(qdict, "devlist");
621 + int64_t speed = qdict_get_try_int(qdict, "speed", 0);
623 + qmp_backup(backup_file, true, BACKUP_FORMAT_VMA, false, NULL, !!devlist,
624 + devlist, qdict_haskey(qdict, "speed"), speed, &error);
626 + hmp_handle_error(mon, &error);
629 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
632 diff --git a/hmp.h b/hmp.h
633 index 0876ec0..9a4c1f6 100644
636 @@ -30,6 +30,7 @@ void hmp_info_migrate(Monitor *mon, const QDict *qdict);
637 void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict);
638 void hmp_info_migrate_parameters(Monitor *mon, const QDict *qdict);
639 void hmp_info_migrate_cache_size(Monitor *mon, const QDict *qdict);
640 +void hmp_info_backup(Monitor *mon, const QDict *qdict);
641 void hmp_info_cpus(Monitor *mon, const QDict *qdict);
642 void hmp_info_block(Monitor *mon, const QDict *qdict);
643 void hmp_info_blockstats(Monitor *mon, const QDict *qdict);
644 @@ -76,6 +77,8 @@ void hmp_eject(Monitor *mon, const QDict *qdict);
645 void hmp_change(Monitor *mon, const QDict *qdict);
646 void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict);
647 void hmp_block_stream(Monitor *mon, const QDict *qdict);
648 +void hmp_backup(Monitor *mon, const QDict *qdict);
649 +void hmp_backup_cancel(Monitor *mon, const QDict *qdict);
650 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict);
651 void hmp_block_job_cancel(Monitor *mon, const QDict *qdict);
652 void hmp_block_job_pause(Monitor *mon, const QDict *qdict);
653 diff --git a/include/block/block_int.h b/include/block/block_int.h
654 index db4650e..0f79b51 100644
655 --- a/include/block/block_int.h
656 +++ b/include/block/block_int.h
659 #define BLOCK_PROBE_BUF_SIZE 512
661 -typedef int BackupDumpFunc(void *opaque, BlockDriverState *bs,
662 +typedef int BackupDumpFunc(void *opaque, BlockBackend *be,
663 int64_t sector_num, int n_sectors, unsigned char *buf);
665 enum BdrvTrackedRequestType {
666 diff --git a/qapi-schema.json b/qapi-schema.json
667 index 518c2ea..89d9ea6 100644
668 --- a/qapi-schema.json
669 +++ b/qapi-schema.json
672 { 'command': 'query-events', 'returns': ['EventInfo'] }
676 +# Detailed backup status.
678 +# @status: #optional string describing the current backup status.
679 +# This can be 'active', 'done', 'error'. If this field is not
680 +# returned, no backup process has been initiated
682 +# @errmsg: #optional error message (only returned if status is 'error')
684 +# @total: #optional total amount of bytes involved in the backup process
686 +# @transferred: #optional amount of bytes already backed up.
688 +# @zero-bytes: #optional amount of 'zero' bytes detected.
690 +# @start-time: #optional time (epoch) when backup job started.
692 +# @end-time: #optional time (epoch) when backup job finished.
694 +# @backupfile: #optional backup file name
696 +# @uuid: #optional uuid for this backup job
699 +{ 'struct': 'BackupStatus',
700 + 'data': {'*status': 'str', '*errmsg': 'str', '*total': 'int',
701 + '*transferred': 'int', '*zero-bytes': 'int',
702 + '*start-time': 'int', '*end-time': 'int',
703 + '*backup-file': 'str', '*uuid': 'str' } }
708 +# An enumeration of supported backup formats.
710 +# @vma: Proxmox vma backup format
712 +{ 'enum': 'BackupFormat',
713 + 'data': [ 'vma' ] }
718 +# Starts a VM backup.
720 +# @backup-file: the backup file name
722 +# @format: format of the backup file
724 +# @config-filename: #optional name of a configuration file to include into
725 +# the backup archive.
727 +# @speed: #optional the maximum speed, in bytes per second
729 +# @devlist: #optional list of block device names (separated by ',', ';'
730 +# or ':'). By default the backup includes all writable block devices.
732 +# Returns: the uuid of the backup job
735 +{ 'command': 'backup', 'data': { 'backup-file': 'str',
736 + '*format': 'BackupFormat',
737 + '*config-file': 'str',
738 + '*devlist': 'str', '*speed': 'int' },
739 + 'returns': 'UuidInfo' }
744 +# Returns information about current/last backup task.
746 +# Returns: @BackupStatus
749 +{ 'command': 'query-backup', 'returns': 'BackupStatus' }
754 +# Cancel the current executing backup process.
756 +# Returns: nothing on success
758 +# Notes: This command succeeds even if there is no backup process running.
761 +{ 'command': 'backup-cancel' }
766 diff --git a/qmp-commands.hx b/qmp-commands.hx
767 index 6de28d4..a8e8522 100644
768 --- a/qmp-commands.hx
769 +++ b/qmp-commands.hx
770 @@ -1314,6 +1314,24 @@ Example:
775 + .args_type = "backup-file:s,format:s?,config-file:F?,speed:o?,devlist:s?",
776 + .mhandler.cmd_new = qmp_marshal_backup,
780 + .name = "backup-cancel",
782 + .mhandler.cmd_new = qmp_marshal_backup_cancel,
786 + .name = "query-backup",
788 + .mhandler.cmd_new = qmp_marshal_query_backup,
792 .name = "block-job-set-speed",
793 .args_type = "device:B,speed:o",
794 .mhandler.cmd_new = qmp_marshal_block_job_set_speed,