1 From 8c04a78d763014aa9efb179a451ea332cf7d5454 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/47] backup: add pve monitor commands
7 blockdev.c | 465 ++++++++++++++++++++++++++++++++++++++++++++++
9 hmp-commands-info.hx | 13 ++
10 hmp-commands.hx | 29 +++
13 include/block/block_int.h | 2 +-
14 qapi-schema.json | 90 +++++++++
15 8 files changed, 668 insertions(+), 6 deletions(-)
17 diff --git a/blockdev.c b/blockdev.c
18 index bb3fc5b..3e5c9ce 100644
22 #include "sysemu/blockdev.h"
23 #include "hw/block/block.h"
24 #include "block/blockjob.h"
25 +#include "block/blockjob_int.h"
26 #include "block/throttle-groups.h"
27 #include "monitor/monitor.h"
28 #include "qemu/error-report.h"
30 #include "qemu/cutils.h"
31 #include "qemu/help_option.h"
32 #include "qemu/throttle-options.h"
35 static QTAILQ_HEAD(, BlockDriverState) monitor_bdrv_states =
36 QTAILQ_HEAD_INITIALIZER(monitor_bdrv_states);
37 @@ -2956,6 +2958,469 @@ out:
38 aio_context_release(aio_context);
41 +void block_job_event_cancelled(BlockJob *job);
42 +void block_job_event_completed(BlockJob *job, const char *msg);
43 +static void block_job_cb(void *opaque, int ret)
45 + /* Note that this function may be executed from another AioContext besides
46 + * the QEMU main loop. If you need to access anything that assumes the
47 + * QEMU global mutex, use a BH or introduce a mutex.
50 + BlockDriverState *bs = opaque;
51 + const char *msg = NULL;
56 + msg = strerror(-ret);
59 + if (block_job_is_cancelled(bs->job)) {
60 + block_job_event_cancelled(bs->job);
62 + block_job_event_completed(bs->job, msg);
66 +/* PVE backup related function */
68 +static struct PVEBackupState {
84 +typedef struct PVEBackupDevInfo {
85 + BlockDriverState *bs;
92 +static void pvebackup_run_next_job(void);
94 +static int pvebackup_dump_cb(void *opaque, BlockBackend *target,
95 + int64_t sector_num, int n_sectors,
98 + PVEBackupDevInfo *di = opaque;
100 + if (sector_num & 0x7f) {
101 + if (!backup_state.error) {
102 + error_setg(&backup_state.error,
103 + "got unaligned write inside backup dump "
104 + "callback (sector %ld)", sector_num);
106 + return -1; // not aligned to cluster size
109 + int64_t cluster_num = sector_num >> 7;
110 + int size = n_sectors * BDRV_SECTOR_SIZE;
114 + if (backup_state.vmaw) {
115 + size_t zero_bytes = 0;
116 + ret = vma_writer_write(backup_state.vmaw, di->dev_id, cluster_num,
118 + backup_state.zero_bytes += zero_bytes;
122 + backup_state.zero_bytes += size;
126 + backup_state.transferred += size;
131 +static void pvebackup_cleanup(void)
133 + backup_state.end_time = time(NULL);
135 + if (backup_state.vmaw) {
136 + Error *local_err = NULL;
137 + vma_writer_close(backup_state.vmaw, &local_err);
138 + error_propagate(&backup_state.error, local_err);
139 + backup_state.vmaw = NULL;
142 + if (backup_state.di_list) {
143 + GList *l = backup_state.di_list;
145 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
146 + l = g_list_next(l);
149 + g_list_free(backup_state.di_list);
150 + backup_state.di_list = NULL;
154 +static void pvebackup_complete_cb(void *opaque, int ret)
156 + PVEBackupDevInfo *di = opaque;
158 + assert(backup_state.vmaw);
160 + di->completed = true;
162 + if (ret < 0 && !backup_state.error) {
163 + error_setg(&backup_state.error, "job failed with err %d - %s",
164 + ret, strerror(-ret));
167 + BlockDriverState *bs = di->bs;
171 + vma_writer_close_stream(backup_state.vmaw, di->dev_id);
173 + block_job_cb(bs, ret);
175 + if (!backup_state.cancel) {
176 + pvebackup_run_next_job();
180 +static void pvebackup_cancel(void *opaque)
182 + backup_state.cancel = true;
184 + if (!backup_state.error) {
185 + error_setg(&backup_state.error, "backup cancelled");
188 + /* drain all i/o (awake jobs waiting for aio) */
191 + GList *l = backup_state.di_list;
193 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
194 + l = g_list_next(l);
195 + if (!di->completed && di->bs) {
196 + BlockJob *job = di->bs->job;
198 + if (!di->completed) {
199 + block_job_cancel_sync(job);
205 + pvebackup_cleanup();
208 +void qmp_backup_cancel(Error **errp)
210 + Coroutine *co = qemu_coroutine_create(pvebackup_cancel, NULL);
211 + qemu_coroutine_enter(co);
213 + while (backup_state.vmaw) {
214 + /* vma writer use main aio context */
215 + aio_poll(qemu_get_aio_context(), true);
219 +bool block_job_should_pause(BlockJob *job);
220 +static void pvebackup_run_next_job(void)
222 + GList *l = backup_state.di_list;
224 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
225 + l = g_list_next(l);
226 + if (!di->completed && di->bs && di->bs->job) {
227 + BlockJob *job = di->bs->job;
228 + if (block_job_should_pause(job)) {
229 + bool cancel = backup_state.error || backup_state.cancel;
231 + block_job_cancel(job);
233 + block_job_resume(job);
240 + pvebackup_cleanup();
243 +UuidInfo *qmp_backup(const char *backup_file, bool has_format,
244 + BackupFormat format,
245 + bool has_config_file, const char *config_file,
246 + bool has_devlist, const char *devlist,
247 + bool has_speed, int64_t speed, Error **errp)
250 + BlockDriverState *bs = NULL;
251 + Error *local_err = NULL;
253 + VmaWriter *vmaw = NULL;
254 + gchar **devs = NULL;
255 + GList *di_list = NULL;
257 + UuidInfo *uuid_info;
259 + if (backup_state.di_list) {
260 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
261 + "previous backup not finished");
265 + /* Todo: try to auto-detect format based on file name */
266 + format = has_format ? format : BACKUP_FORMAT_VMA;
268 + if (format != BACKUP_FORMAT_VMA) {
269 + error_set(errp, ERROR_CLASS_GENERIC_ERROR, "unknown backup format");
274 + devs = g_strsplit_set(devlist, ",;:", -1);
278 + blk = blk_by_name(*d);
281 + if (bdrv_is_read_only(bs)) {
282 + error_setg(errp, "Node '%s' is read only", *d);
285 + if (!bdrv_is_inserted(bs)) {
286 + error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, *d);
289 + PVEBackupDevInfo *di = g_new0(PVEBackupDevInfo, 1);
291 + di_list = g_list_append(di_list, di);
293 + error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
294 + "Device '%s' not found", *d);
301 + BdrvNextIterator it;
304 + for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
305 + if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
309 + PVEBackupDevInfo *di = g_new0(PVEBackupDevInfo, 1);
311 + di_list = g_list_append(di_list, di);
316 + error_set(errp, ERROR_CLASS_GENERIC_ERROR, "empty device list");
324 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
325 + l = g_list_next(l);
326 + if (bdrv_op_is_blocked(di->bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
330 + ssize_t size = bdrv_getlength(di->bs);
332 + error_setg_errno(errp, -di->size, "bdrv_getlength failed");
339 + uuid_generate(uuid);
341 + vmaw = vma_writer_create(backup_file, uuid, &local_err);
344 + error_propagate(errp, local_err);
349 + /* register all devices for vma writer */
352 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
353 + l = g_list_next(l);
355 + const char *devname = bdrv_get_device_name(di->bs);
356 + di->dev_id = vma_writer_register_stream(vmaw, devname, di->size);
357 + if (di->dev_id <= 0) {
358 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
359 + "register_stream failed");
364 + /* add configuration file to archive */
365 + if (has_config_file) {
366 + char *cdata = NULL;
368 + GError *err = NULL;
369 + if (!g_file_get_contents(config_file, &cdata, &clen, &err)) {
370 + error_setg(errp, "unable to read file '%s'", config_file);
374 + const char *basename = g_path_get_basename(config_file);
375 + if (vma_writer_add_config(vmaw, basename, cdata, clen) != 0) {
376 + error_setg(errp, "unable to add config data to vma archive");
383 + /* initialize global backup_state now */
385 + backup_state.cancel = false;
387 + if (backup_state.error) {
388 + error_free(backup_state.error);
389 + backup_state.error = NULL;
392 + backup_state.speed = (has_speed && speed > 0) ? speed : 0;
394 + backup_state.start_time = time(NULL);
395 + backup_state.end_time = 0;
397 + if (backup_state.backup_file) {
398 + g_free(backup_state.backup_file);
400 + backup_state.backup_file = g_strdup(backup_file);
402 + backup_state.vmaw = vmaw;
404 + uuid_copy(backup_state.uuid, uuid);
405 + uuid_unparse_lower(uuid, backup_state.uuid_str);
407 + backup_state.di_list = di_list;
409 + backup_state.total = total;
410 + backup_state.transferred = 0;
411 + backup_state.zero_bytes = 0;
413 + /* start all jobs (paused state) */
416 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
417 + l = g_list_next(l);
419 + backup_job_create(NULL, di->bs, NULL, speed, MIRROR_SYNC_MODE_FULL, NULL,
420 + BLOCKDEV_ON_ERROR_REPORT, BLOCKDEV_ON_ERROR_REPORT,
421 + pvebackup_dump_cb, pvebackup_complete_cb, di,
422 + 1, NULL, &local_err);
423 + if (local_err != NULL) {
424 + error_setg(&backup_state.error, "backup_job_create failed");
425 + pvebackup_cancel(NULL);
429 + if (!backup_state.error) {
430 + pvebackup_run_next_job(); // run one job
433 + uuid_info = g_malloc0(sizeof(*uuid_info));
434 + uuid_info->UUID = g_strdup(backup_state.uuid_str);
442 + l = g_list_next(l);
444 + g_list_free(di_list);
452 + vma_writer_close(vmaw, &err);
453 + unlink(backup_file);
459 +BackupStatus *qmp_query_backup(Error **errp)
461 + BackupStatus *info = g_malloc0(sizeof(*info));
463 + if (!backup_state.start_time) {
464 + /* not started, return {} */
468 + info->has_status = true;
469 + info->has_start_time = true;
470 + info->start_time = backup_state.start_time;
472 + if (backup_state.backup_file) {
473 + info->has_backup_file = true;
474 + info->backup_file = g_strdup(backup_state.backup_file);
477 + info->has_uuid = true;
478 + info->uuid = g_strdup(backup_state.uuid_str);
480 + if (backup_state.end_time) {
481 + if (backup_state.error) {
482 + info->status = g_strdup("error");
483 + info->has_errmsg = true;
484 + info->errmsg = g_strdup(error_get_pretty(backup_state.error));
486 + info->status = g_strdup("done");
488 + info->has_end_time = true;
489 + info->end_time = backup_state.end_time;
491 + info->status = g_strdup("active");
494 + info->has_total = true;
495 + info->total = backup_state.total;
496 + info->has_zero_bytes = true;
497 + info->zero_bytes = backup_state.zero_bytes;
498 + info->has_transferred = true;
499 + info->transferred = backup_state.transferred;
504 void qmp_block_stream(bool has_job_id, const char *job_id, const char *device,
505 bool has_base, const char *base,
506 bool has_base_node, const char *base_node,
507 diff --git a/blockjob.c b/blockjob.c
508 index 9b619f385..54bd34a 100644
512 #include "qemu/timer.h"
513 #include "qapi-event.h"
515 -static void block_job_event_cancelled(BlockJob *job);
516 -static void block_job_event_completed(BlockJob *job, const char *msg);
517 +void block_job_event_cancelled(BlockJob *job);
518 +void block_job_event_completed(BlockJob *job, const char *msg);
520 /* Transactional group of block jobs */
522 @@ -473,7 +473,8 @@ void block_job_user_pause(BlockJob *job)
523 block_job_pause(job);
526 -static bool block_job_should_pause(BlockJob *job)
527 +bool block_job_should_pause(BlockJob *job);
528 +bool block_job_should_pause(BlockJob *job)
530 return job->pause_count > 0;
532 @@ -687,7 +688,7 @@ static void block_job_iostatus_set_err(BlockJob *job, int error)
536 -static void block_job_event_cancelled(BlockJob *job)
537 +void block_job_event_cancelled(BlockJob *job)
539 if (block_job_is_internal(job)) {
541 @@ -701,7 +702,7 @@ static void block_job_event_cancelled(BlockJob *job)
545 -static void block_job_event_completed(BlockJob *job, const char *msg)
546 +void block_job_event_completed(BlockJob *job, const char *msg)
548 if (block_job_is_internal(job)) {
550 diff --git a/hmp-commands-info.hx b/hmp-commands-info.hx
551 index a53f105..1a18380 100644
552 --- a/hmp-commands-info.hx
553 +++ b/hmp-commands-info.hx
554 @@ -487,6 +487,19 @@ STEXI
562 + .help = "show backup status",
563 + .cmd = hmp_info_backup,
571 #if defined(CONFIG_SLIRP)
574 diff --git a/hmp-commands.hx b/hmp-commands.hx
575 index 8819281..aea39d0 100644
576 --- a/hmp-commands.hx
577 +++ b/hmp-commands.hx
578 @@ -87,6 +87,35 @@ STEXI
579 Copy data from a backing file into a block device.
584 + .args_type = "backupfile:s,speed:o?,devlist:s?",
585 + .params = "backupfile [speed [devlist]]",
586 + .help = "create a VM Backup.",
597 + .name = "backup_cancel",
600 + .help = "cancel the current VM backup",
601 + .cmd = hmp_backup_cancel,
606 +@findex backup_cancel
607 +Cancel the current VM backup.
612 .name = "block_job_set_speed",
613 .args_type = "device:B,speed:o",
614 diff --git a/hmp.c b/hmp.c
615 index 904542d..c685ba5 100644
618 @@ -151,6 +151,44 @@ void hmp_info_mice(Monitor *mon, const QDict *qdict)
619 qapi_free_MouseInfoList(mice_list);
622 +void hmp_info_backup(Monitor *mon, const QDict *qdict)
624 + BackupStatus *info;
626 + info = qmp_query_backup(NULL);
627 + if (info->has_status) {
628 + if (info->has_errmsg) {
629 + monitor_printf(mon, "Backup status: %s - %s\n",
630 + info->status, info->errmsg);
632 + monitor_printf(mon, "Backup status: %s\n", info->status);
636 + if (info->has_backup_file) {
637 + monitor_printf(mon, "Start time: %s", ctime(&info->start_time));
638 + if (info->end_time) {
639 + monitor_printf(mon, "End time: %s", ctime(&info->end_time));
642 + int per = (info->has_total && info->total &&
643 + info->has_transferred && info->transferred) ?
644 + (info->transferred * 100)/info->total : 0;
645 + int zero_per = (info->has_total && info->total &&
646 + info->has_zero_bytes && info->zero_bytes) ?
647 + (info->zero_bytes * 100)/info->total : 0;
648 + monitor_printf(mon, "Backup file: %s\n", info->backup_file);
649 + monitor_printf(mon, "Backup uuid: %s\n", info->uuid);
650 + monitor_printf(mon, "Total size: %zd\n", info->total);
651 + monitor_printf(mon, "Transferred bytes: %zd (%d%%)\n",
652 + info->transferred, per);
653 + monitor_printf(mon, "Zero bytes: %zd (%d%%)\n",
654 + info->zero_bytes, zero_per);
657 + qapi_free_BackupStatus(info);
660 void hmp_info_migrate(Monitor *mon, const QDict *qdict)
663 @@ -1613,6 +1651,29 @@ void hmp_block_stream(Monitor *mon, const QDict *qdict)
664 hmp_handle_error(mon, &error);
667 +void hmp_backup_cancel(Monitor *mon, const QDict *qdict)
669 + Error *error = NULL;
671 + qmp_backup_cancel(&error);
673 + hmp_handle_error(mon, &error);
676 +void hmp_backup(Monitor *mon, const QDict *qdict)
678 + Error *error = NULL;
680 + const char *backup_file = qdict_get_str(qdict, "backupfile");
681 + const char *devlist = qdict_get_try_str(qdict, "devlist");
682 + int64_t speed = qdict_get_try_int(qdict, "speed", 0);
684 + qmp_backup(backup_file, true, BACKUP_FORMAT_VMA, false, NULL, !!devlist,
685 + devlist, qdict_haskey(qdict, "speed"), speed, &error);
687 + hmp_handle_error(mon, &error);
690 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
693 diff --git a/hmp.h b/hmp.h
694 index 799fd37..17a65b2 100644
697 @@ -30,6 +30,7 @@ void hmp_info_migrate(Monitor *mon, const QDict *qdict);
698 void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict);
699 void hmp_info_migrate_parameters(Monitor *mon, const QDict *qdict);
700 void hmp_info_migrate_cache_size(Monitor *mon, const QDict *qdict);
701 +void hmp_info_backup(Monitor *mon, const QDict *qdict);
702 void hmp_info_cpus(Monitor *mon, const QDict *qdict);
703 void hmp_info_block(Monitor *mon, const QDict *qdict);
704 void hmp_info_blockstats(Monitor *mon, const QDict *qdict);
705 @@ -79,6 +80,8 @@ void hmp_eject(Monitor *mon, const QDict *qdict);
706 void hmp_change(Monitor *mon, const QDict *qdict);
707 void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict);
708 void hmp_block_stream(Monitor *mon, const QDict *qdict);
709 +void hmp_backup(Monitor *mon, const QDict *qdict);
710 +void hmp_backup_cancel(Monitor *mon, const QDict *qdict);
711 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict);
712 void hmp_block_job_cancel(Monitor *mon, const QDict *qdict);
713 void hmp_block_job_pause(Monitor *mon, const QDict *qdict);
714 diff --git a/include/block/block_int.h b/include/block/block_int.h
715 index ec65581..278da16 100644
716 --- a/include/block/block_int.h
717 +++ b/include/block/block_int.h
720 #define BLOCK_PROBE_BUF_SIZE 512
722 -typedef int BackupDumpFunc(void *opaque, BlockDriverState *bs,
723 +typedef int BackupDumpFunc(void *opaque, BlockBackend *be,
724 int64_t sector_num, int n_sectors, unsigned char *buf);
726 enum BdrvTrackedRequestType {
727 diff --git a/qapi-schema.json b/qapi-schema.json
728 index ca534cc..059cbfc 100644
729 --- a/qapi-schema.json
730 +++ b/qapi-schema.json
732 { 'command': 'query-events', 'returns': ['EventInfo'] }
737 +# Detailed backup status.
739 +# @status: string describing the current backup status.
740 +# This can be 'active', 'done', 'error'. If this field is not
741 +# returned, no backup process has been initiated
743 +# @errmsg: error message (only returned if status is 'error')
745 +# @total: total amount of bytes involved in the backup process
747 +# @transferred: amount of bytes already backed up.
749 +# @zero-bytes: amount of 'zero' bytes detected.
751 +# @start-time: time (epoch) when backup job started.
753 +# @end-time: time (epoch) when backup job finished.
755 +# @backup-file: backup file name
757 +# @uuid: uuid for this backup job
760 +{ 'struct': 'BackupStatus',
761 + 'data': {'*status': 'str', '*errmsg': 'str', '*total': 'int',
762 + '*transferred': 'int', '*zero-bytes': 'int',
763 + '*start-time': 'int', '*end-time': 'int',
764 + '*backup-file': 'str', '*uuid': 'str' } }
769 +# An enumeration of supported backup formats.
771 +# @vma: Proxmox vma backup format
773 +{ 'enum': 'BackupFormat',
774 + 'data': [ 'vma' ] }
779 +# Starts a VM backup.
781 +# @backup-file: the backup file name
783 +# @format: format of the backup file
785 +# @config-file: a configuration file to include into
786 +# the backup archive.
788 +# @speed: the maximum speed, in bytes per second
790 +# @devlist: list of block device names (separated by ',', ';'
791 +# or ':'). By default the backup includes all writable block devices.
793 +# Returns: the uuid of the backup job
796 +{ 'command': 'backup', 'data': { 'backup-file': 'str',
797 + '*format': 'BackupFormat',
798 + '*config-file': 'str',
799 + '*devlist': 'str', '*speed': 'int' },
800 + 'returns': 'UuidInfo' }
805 +# Returns information about current/last backup task.
807 +# Returns: @BackupStatus
810 +{ 'command': 'query-backup', 'returns': 'BackupStatus' }
815 +# Cancel the current executing backup process.
817 +# Returns: nothing on success
819 +# Notes: This command succeeds even if there is no backup process running.
822 +{ 'command': 'backup-cancel' }
827 # Detailed migration status.