1 From af106cee7626a76c7d24c838d07b437f48c2da2c 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 16/41] backup: add pve monitor commands
7 blockdev.c | 438 +++++++++++++++++++++++++++++++++++++++++++++++++++
8 hmp-commands-info.hx | 13 ++
9 hmp-commands.hx | 29 ++++
12 qapi-schema.json | 89 +++++++++++
13 qmp-commands.hx | 18 +++
14 7 files changed, 651 insertions(+)
16 diff --git a/blockdev.c b/blockdev.c
17 index 1796eaf..a346c55 100644
21 #include "qmp-commands.h"
23 #include "sysemu/arch_init.h"
26 static const char *const if_name[IF_COUNT] = {
28 @@ -2875,6 +2876,443 @@ static void block_job_cb(void *opaque, int ret)
32 +/* PVE backup related function */
34 +static struct PVEBackupState {
50 +typedef struct PVEBackupDevInfo {
51 + BlockDriverState *bs;
58 +static void pvebackup_run_next_job(void);
60 +static int pvebackup_dump_cb(void *opaque, BlockDriverState *target,
61 + int64_t sector_num, int n_sectors,
64 + PVEBackupDevInfo *di = opaque;
66 + if (sector_num & 0x7f) {
67 + if (!backup_state.error) {
68 + error_setg(&backup_state.error,
69 + "got unaligned write inside backup dump "
70 + "callback (sector %ld)", sector_num);
72 + return -1; // not aligned to cluster size
75 + int64_t cluster_num = sector_num >> 7;
76 + int size = n_sectors * BDRV_SECTOR_SIZE;
80 + if (backup_state.vmaw) {
81 + size_t zero_bytes = 0;
82 + ret = vma_writer_write(backup_state.vmaw, di->dev_id, cluster_num,
84 + backup_state.zero_bytes += zero_bytes;
88 + backup_state.zero_bytes += size;
92 + backup_state.transferred += size;
97 +static void pvebackup_cleanup(void)
99 + backup_state.end_time = time(NULL);
101 + if (backup_state.vmaw) {
102 + Error *local_err = NULL;
103 + vma_writer_close(backup_state.vmaw, &local_err);
104 + error_propagate(&backup_state.error, local_err);
105 + backup_state.vmaw = NULL;
108 + if (backup_state.di_list) {
109 + GList *l = backup_state.di_list;
111 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
112 + l = g_list_next(l);
115 + g_list_free(backup_state.di_list);
116 + backup_state.di_list = NULL;
120 +static void pvebackup_complete_cb(void *opaque, int ret)
122 + PVEBackupDevInfo *di = opaque;
124 + assert(backup_state.vmaw);
126 + di->completed = true;
128 + if (ret < 0 && !backup_state.error) {
129 + error_setg(&backup_state.error, "job failed with err %d - %s",
130 + ret, strerror(-ret));
133 + BlockDriverState *bs = di->bs;
137 + vma_writer_close_stream(backup_state.vmaw, di->dev_id);
139 + block_job_cb(bs, ret);
141 + if (!backup_state.cancel) {
142 + pvebackup_run_next_job();
146 +static void pvebackup_cancel(void *opaque)
148 + backup_state.cancel = true;
150 + if (!backup_state.error) {
151 + error_setg(&backup_state.error, "backup cancelled");
154 + /* drain all i/o (awake jobs waiting for aio) */
157 + GList *l = backup_state.di_list;
159 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
160 + l = g_list_next(l);
161 + if (!di->completed && di->bs) {
162 + BlockJob *job = di->bs->job;
164 + if (!di->completed) {
165 + block_job_cancel_sync(job);
171 + pvebackup_cleanup();
174 +void qmp_backup_cancel(Error **errp)
176 + Coroutine *co = qemu_coroutine_create(pvebackup_cancel);
177 + qemu_coroutine_enter(co, NULL);
179 + while (backup_state.vmaw) {
180 + /* vma writer use main aio context */
181 + aio_poll(qemu_get_aio_context(), true);
185 +static void pvebackup_run_next_job(void)
187 + GList *l = backup_state.di_list;
189 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
190 + l = g_list_next(l);
191 + if (!di->completed && di->bs && di->bs->job) {
192 + BlockJob *job = di->bs->job;
193 + if (block_job_is_paused(job)) {
194 + bool cancel = backup_state.error || backup_state.cancel;
196 + block_job_cancel(job);
198 + block_job_resume(job);
205 + pvebackup_cleanup();
208 +UuidInfo *qmp_backup(const char *backup_file, bool has_format,
209 + BackupFormat format,
210 + bool has_config_file, const char *config_file,
211 + bool has_devlist, const char *devlist,
212 + bool has_speed, int64_t speed, Error **errp)
215 + BlockDriverState *bs = NULL;
216 + Error *local_err = NULL;
218 + VmaWriter *vmaw = NULL;
219 + gchar **devs = NULL;
220 + GList *di_list = NULL;
222 + UuidInfo *uuid_info;
224 + if (backup_state.di_list) {
225 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
226 + "previous backup not finished");
230 + /* Todo: try to auto-detect format based on file name */
231 + format = has_format ? format : BACKUP_FORMAT_VMA;
233 + if (format != BACKUP_FORMAT_VMA) {
234 + error_set(errp, ERROR_CLASS_GENERIC_ERROR, "unknown backup format");
239 + devs = g_strsplit_set(devlist, ",;:", -1);
243 + blk = blk_by_name(*d);
246 + if (bdrv_is_read_only(bs)) {
247 + error_setg(errp, "Node '%s' is read only", *d);
250 + if (!bdrv_is_inserted(bs)) {
251 + error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, *d);
254 + PVEBackupDevInfo *di = g_new0(PVEBackupDevInfo, 1);
256 + di_list = g_list_append(di_list, di);
258 + error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
259 + "Device '%s' not found", *d);
268 + while ((bs = bdrv_next(bs))) {
270 + if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
274 + PVEBackupDevInfo *di = g_new0(PVEBackupDevInfo, 1);
276 + di_list = g_list_append(di_list, di);
281 + error_set(errp, ERROR_CLASS_GENERIC_ERROR, "empty device list");
289 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
290 + l = g_list_next(l);
291 + if (bdrv_op_is_blocked(di->bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
295 + ssize_t size = bdrv_getlength(di->bs);
297 + error_setg_errno(errp, -di->size, "bdrv_getlength failed");
304 + uuid_generate(uuid);
306 + vmaw = vma_writer_create(backup_file, uuid, &local_err);
309 + error_propagate(errp, local_err);
314 + /* register all devices for vma writer */
317 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
318 + l = g_list_next(l);
320 + const char *devname = bdrv_get_device_name(di->bs);
321 + di->dev_id = vma_writer_register_stream(vmaw, devname, di->size);
322 + if (di->dev_id <= 0) {
323 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
324 + "register_stream failed");
329 + /* add configuration file to archive */
330 + if (has_config_file) {
331 + char *cdata = NULL;
333 + GError *err = NULL;
334 + if (!g_file_get_contents(config_file, &cdata, &clen, &err)) {
335 + error_setg(errp, "unable to read file '%s'", config_file);
339 + const char *basename = g_path_get_basename(config_file);
340 + if (vma_writer_add_config(vmaw, basename, cdata, clen) != 0) {
341 + error_setg(errp, "unable to add config data to vma archive");
348 + /* initialize global backup_state now */
350 + backup_state.cancel = false;
352 + if (backup_state.error) {
353 + error_free(backup_state.error);
354 + backup_state.error = NULL;
357 + backup_state.speed = (has_speed && speed > 0) ? speed : 0;
359 + backup_state.start_time = time(NULL);
360 + backup_state.end_time = 0;
362 + if (backup_state.backup_file) {
363 + g_free(backup_state.backup_file);
365 + backup_state.backup_file = g_strdup(backup_file);
367 + backup_state.vmaw = vmaw;
369 + uuid_copy(backup_state.uuid, uuid);
370 + uuid_unparse_lower(uuid, backup_state.uuid_str);
372 + backup_state.di_list = di_list;
374 + backup_state.total = total;
375 + backup_state.transferred = 0;
376 + backup_state.zero_bytes = 0;
378 + /* start all jobs (paused state) */
381 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
382 + l = g_list_next(l);
384 + backup_start(di->bs, NULL, speed, MIRROR_SYNC_MODE_FULL, NULL,
385 + BLOCKDEV_ON_ERROR_REPORT, BLOCKDEV_ON_ERROR_REPORT,
386 + pvebackup_dump_cb, pvebackup_complete_cb, di,
388 + if (local_err != NULL) {
389 + error_setg(&backup_state.error, "backup_job_create failed");
390 + pvebackup_cancel(NULL);
394 + if (!backup_state.error) {
395 + pvebackup_run_next_job(); // run one job
398 + uuid_info = g_malloc0(sizeof(*uuid_info));
399 + uuid_info->UUID = g_strdup(backup_state.uuid_str);
407 + l = g_list_next(l);
409 + g_list_free(di_list);
417 + vma_writer_close(vmaw, &err);
418 + unlink(backup_file);
424 +BackupStatus *qmp_query_backup(Error **errp)
426 + BackupStatus *info = g_malloc0(sizeof(*info));
428 + if (!backup_state.start_time) {
429 + /* not started, return {} */
433 + info->has_status = true;
434 + info->has_start_time = true;
435 + info->start_time = backup_state.start_time;
437 + if (backup_state.backup_file) {
438 + info->has_backup_file = true;
439 + info->backup_file = g_strdup(backup_state.backup_file);
442 + info->has_uuid = true;
443 + info->uuid = g_strdup(backup_state.uuid_str);
445 + if (backup_state.end_time) {
446 + if (backup_state.error) {
447 + info->status = g_strdup("error");
448 + info->has_errmsg = true;
449 + info->errmsg = g_strdup(error_get_pretty(backup_state.error));
451 + info->status = g_strdup("done");
453 + info->has_end_time = true;
454 + info->end_time = backup_state.end_time;
456 + info->status = g_strdup("active");
459 + info->has_total = true;
460 + info->total = backup_state.total;
461 + info->has_zero_bytes = true;
462 + info->zero_bytes = backup_state.zero_bytes;
463 + info->has_transferred = true;
464 + info->transferred = backup_state.transferred;
469 void qmp_block_stream(const char *device,
470 bool has_base, const char *base,
471 bool has_backing_file, const char *backing_file,
472 diff --git a/hmp-commands-info.hx b/hmp-commands-info.hx
473 index 9b71351..355289a 100644
474 --- a/hmp-commands-info.hx
475 +++ b/hmp-commands-info.hx
476 @@ -502,6 +502,19 @@ STEXI
484 + .help = "show backup status",
485 + .mhandler.cmd = hmp_info_backup,
493 #if defined(CONFIG_SLIRP)
496 diff --git a/hmp-commands.hx b/hmp-commands.hx
497 index bb52e4d..324125f 100644
498 --- a/hmp-commands.hx
499 +++ b/hmp-commands.hx
500 @@ -87,6 +87,35 @@ STEXI
501 Copy data from a backing file into a block device.
506 + .args_type = "backupfile:s,speed:o?,devlist:s?",
507 + .params = "backupfile [speed [devlist]]",
508 + .help = "create a VM Backup.",
509 + .mhandler.cmd = hmp_backup,
519 + .name = "backup_cancel",
522 + .help = "cancel the current VM backup",
523 + .mhandler.cmd = hmp_backup_cancel,
528 +@findex backup_cancel
529 +Cancel the current VM backup.
534 .name = "block_job_set_speed",
535 .args_type = "device:B,speed:o",
536 diff --git a/hmp.c b/hmp.c
537 index 0e63ea8..ebb16c1 100644
540 @@ -146,6 +146,44 @@ void hmp_info_mice(Monitor *mon, const QDict *qdict)
541 qapi_free_MouseInfoList(mice_list);
544 +void hmp_info_backup(Monitor *mon, const QDict *qdict)
546 + BackupStatus *info;
548 + info = qmp_query_backup(NULL);
549 + if (info->has_status) {
550 + if (info->has_errmsg) {
551 + monitor_printf(mon, "Backup status: %s - %s\n",
552 + info->status, info->errmsg);
554 + monitor_printf(mon, "Backup status: %s\n", info->status);
558 + if (info->has_backup_file) {
559 + monitor_printf(mon, "Start time: %s", ctime(&info->start_time));
560 + if (info->end_time) {
561 + monitor_printf(mon, "End time: %s", ctime(&info->end_time));
564 + int per = (info->has_total && info->total &&
565 + info->has_transferred && info->transferred) ?
566 + (info->transferred * 100)/info->total : 0;
567 + int zero_per = (info->has_total && info->total &&
568 + info->has_zero_bytes && info->zero_bytes) ?
569 + (info->zero_bytes * 100)/info->total : 0;
570 + monitor_printf(mon, "Backup file: %s\n", info->backup_file);
571 + monitor_printf(mon, "Backup uuid: %s\n", info->uuid);
572 + monitor_printf(mon, "Total size: %zd\n", info->total);
573 + monitor_printf(mon, "Transferred bytes: %zd (%d%%)\n",
574 + info->transferred, per);
575 + monitor_printf(mon, "Zero bytes: %zd (%d%%)\n",
576 + info->zero_bytes, zero_per);
579 + qapi_free_BackupStatus(info);
582 void hmp_info_migrate(Monitor *mon, const QDict *qdict)
585 @@ -1461,6 +1499,29 @@ void hmp_block_stream(Monitor *mon, const QDict *qdict)
586 hmp_handle_error(mon, &error);
589 +void hmp_backup_cancel(Monitor *mon, const QDict *qdict)
591 + Error *error = NULL;
593 + qmp_backup_cancel(&error);
595 + hmp_handle_error(mon, &error);
598 +void hmp_backup(Monitor *mon, const QDict *qdict)
600 + Error *error = NULL;
602 + const char *backup_file = qdict_get_str(qdict, "backupfile");
603 + const char *devlist = qdict_get_try_str(qdict, "devlist");
604 + int64_t speed = qdict_get_try_int(qdict, "speed", 0);
606 + qmp_backup(backup_file, true, BACKUP_FORMAT_VMA, false, NULL, !!devlist,
607 + devlist, qdict_haskey(qdict, "speed"), speed, &error);
609 + hmp_handle_error(mon, &error);
612 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
615 diff --git a/hmp.h b/hmp.h
616 index a8c5b5a..0b39c25 100644
619 @@ -30,6 +30,7 @@ void hmp_info_migrate(Monitor *mon, const QDict *qdict);
620 void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict);
621 void hmp_info_migrate_parameters(Monitor *mon, const QDict *qdict);
622 void hmp_info_migrate_cache_size(Monitor *mon, const QDict *qdict);
623 +void hmp_info_backup(Monitor *mon, const QDict *qdict);
624 void hmp_info_cpus(Monitor *mon, const QDict *qdict);
625 void hmp_info_block(Monitor *mon, const QDict *qdict);
626 void hmp_info_blockstats(Monitor *mon, const QDict *qdict);
627 @@ -76,6 +77,8 @@ void hmp_eject(Monitor *mon, const QDict *qdict);
628 void hmp_change(Monitor *mon, const QDict *qdict);
629 void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict);
630 void hmp_block_stream(Monitor *mon, const QDict *qdict);
631 +void hmp_backup(Monitor *mon, const QDict *qdict);
632 +void hmp_backup_cancel(Monitor *mon, const QDict *qdict);
633 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict);
634 void hmp_block_job_cancel(Monitor *mon, const QDict *qdict);
635 void hmp_block_job_pause(Monitor *mon, const QDict *qdict);
636 diff --git a/qapi-schema.json b/qapi-schema.json
637 index 1aa434d..edfb3a4 100644
638 --- a/qapi-schema.json
639 +++ b/qapi-schema.json
642 { 'command': 'query-events', 'returns': ['EventInfo'] }
646 +# Detailed backup status.
648 +# @status: #optional string describing the current backup status.
649 +# This can be 'active', 'done', 'error'. If this field is not
650 +# returned, no backup process has been initiated
652 +# @errmsg: #optional error message (only returned if status is 'error')
654 +# @total: #optional total amount of bytes involved in the backup process
656 +# @transferred: #optional amount of bytes already backed up.
658 +# @zero-bytes: #optional amount of 'zero' bytes detected.
660 +# @start-time: #optional time (epoch) when backup job started.
662 +# @end-time: #optional time (epoch) when backup job finished.
664 +# @backupfile: #optional backup file name
666 +# @uuid: #optional uuid for this backup job
669 +{ 'struct': 'BackupStatus',
670 + 'data': {'*status': 'str', '*errmsg': 'str', '*total': 'int',
671 + '*transferred': 'int', '*zero-bytes': 'int',
672 + '*start-time': 'int', '*end-time': 'int',
673 + '*backup-file': 'str', '*uuid': 'str' } }
678 +# An enumeration of supported backup formats.
680 +# @vma: Proxmox vma backup format
682 +{ 'enum': 'BackupFormat',
683 + 'data': [ 'vma' ] }
688 +# Starts a VM backup.
690 +# @backup-file: the backup file name
692 +# @format: format of the backup file
694 +# @config-filename: #optional name of a configuration file to include into
695 +# the backup archive.
697 +# @speed: #optional the maximum speed, in bytes per second
699 +# @devlist: #optional list of block device names (separated by ',', ';'
700 +# or ':'). By default the backup includes all writable block devices.
702 +# Returns: the uuid of the backup job
705 +{ 'command': 'backup', 'data': { 'backup-file': 'str',
706 + '*format': 'BackupFormat',
707 + '*config-file': 'str',
708 + '*devlist': 'str', '*speed': 'int' },
709 + 'returns': 'UuidInfo' }
714 +# Returns information about current/last backup task.
716 +# Returns: @BackupStatus
719 +{ 'command': 'query-backup', 'returns': 'BackupStatus' }
724 +# Cancel the current executing backup process.
726 +# Returns: nothing on success
728 +# Notes: This command succeeds even if there is no backup process running.
731 +{ 'command': 'backup-cancel' }
736 diff --git a/qmp-commands.hx b/qmp-commands.hx
737 index 5c23d91..3dea794 100644
738 --- a/qmp-commands.hx
739 +++ b/qmp-commands.hx
740 @@ -1254,6 +1254,24 @@ Example:
745 + .args_type = "backup-file:s,format:s?,config-file:F?,speed:o?,devlist:s?",
746 + .mhandler.cmd_new = qmp_marshal_input_backup,
750 + .name = "backup-cancel",
752 + .mhandler.cmd_new = qmp_marshal_input_backup_cancel,
756 + .name = "query-backup",
758 + .mhandler.cmd_new = qmp_marshal_input_query_backup,
762 .name = "block-job-set-speed",
763 .args_type = "device:B,speed:o",
764 .mhandler.cmd_new = qmp_marshal_block_job_set_speed,