1 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2 From: Dietmar Maurer <dietmar@proxmox.com>
3 Date: Mon, 6 Apr 2020 12:16:59 +0200
4 Subject: [PATCH] PVE-Backup: proxmox backup patches for qemu
6 Signed-off-by: Dietmar Maurer <dietmar@proxmox.com>
7 [PVE-Backup: avoid coroutines to fix AIO freeze, cleanups]
8 Signed-off-by: Stefan Reiter <s.reiter@proxmox.com>
9 Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
11 block/meson.build | 5 +
12 block/monitor/block-hmp-cmds.c | 33 ++
14 hmp-commands-info.hx | 14 +
15 hmp-commands.hx | 29 +
16 include/block/block_int.h | 2 +-
17 include/monitor/hmp.h | 3 +
19 monitor/hmp-cmds.c | 44 ++
20 proxmox-backup-client.c | 176 ++++++
21 proxmox-backup-client.h | 59 ++
22 pve-backup.c | 959 +++++++++++++++++++++++++++++++++
23 qapi/block-core.json | 109 ++++
24 qapi/common.json | 13 +
25 qapi/machine.json | 15 +-
26 15 files changed, 1449 insertions(+), 14 deletions(-)
27 create mode 100644 proxmox-backup-client.c
28 create mode 100644 proxmox-backup-client.h
29 create mode 100644 pve-backup.c
31 diff --git a/block/meson.build b/block/meson.build
32 index 19bc2b7cbb..9e433daf2e 100644
33 --- a/block/meson.build
34 +++ b/block/meson.build
35 @@ -46,6 +46,11 @@ block_ss.add(files(
36 ), zstd, zlib, gnutls)
38 block_ss.add(files('../vma-writer.c'), libuuid)
40 + '../proxmox-backup-client.c',
42 +), libproxmox_backup_qemu)
45 softmmu_ss.add(when: 'CONFIG_TCG', if_true: files('blkreplay.c'))
47 diff --git a/block/monitor/block-hmp-cmds.c b/block/monitor/block-hmp-cmds.c
48 index 3e6670c963..1e29681d30 100644
49 --- a/block/monitor/block-hmp-cmds.c
50 +++ b/block/monitor/block-hmp-cmds.c
51 @@ -1015,3 +1015,36 @@ void hmp_info_snapshots(Monitor *mon, const QDict *qdict)
53 g_free(global_snapshots);
56 +void hmp_backup_cancel(Monitor *mon, const QDict *qdict)
58 + Error *error = NULL;
60 + qmp_backup_cancel(&error);
62 + hmp_handle_error(mon, error);
65 +void hmp_backup(Monitor *mon, const QDict *qdict)
67 + Error *error = NULL;
69 + int dir = qdict_get_try_bool(qdict, "directory", 0);
70 + const char *backup_file = qdict_get_str(qdict, "backupfile");
71 + const char *devlist = qdict_get_try_str(qdict, "devlist");
72 + int64_t speed = qdict_get_try_int(qdict, "speed", 0);
76 + false, NULL, // PBS password
77 + false, NULL, // PBS keyfile
78 + false, NULL, // PBS key_password
79 + false, NULL, // PBS fingerprint
80 + false, NULL, // PBS backup-id
81 + false, 0, // PBS backup-time
82 + true, dir ? BACKUP_FORMAT_DIR : BACKUP_FORMAT_VMA,
83 + false, NULL, false, NULL, !!devlist,
84 + devlist, qdict_haskey(qdict, "speed"), speed, &error);
86 + hmp_handle_error(mon, error);
88 diff --git a/blockdev.c b/blockdev.c
89 index b6f797b41f..84e9b898be 100644
93 #include "hw/block/block.h"
94 #include "block/blockjob.h"
95 #include "block/qdict.h"
96 +#include "block/blockjob_int.h"
97 #include "block/throttle-groups.h"
98 #include "monitor/monitor.h"
99 #include "qemu/error-report.h"
100 diff --git a/hmp-commands-info.hx b/hmp-commands-info.hx
101 index e6dd3be07a..15ddecada1 100644
102 --- a/hmp-commands-info.hx
103 +++ b/hmp-commands-info.hx
104 @@ -497,6 +497,20 @@ SRST
105 Show the current VM UUID.
113 + .help = "show backup status",
114 + .cmd = hmp_info_backup,
119 + Show backup status.
122 #if defined(CONFIG_SLIRP)
125 diff --git a/hmp-commands.hx b/hmp-commands.hx
126 index 42203dbe92..7faba36b39 100644
127 --- a/hmp-commands.hx
128 +++ b/hmp-commands.hx
129 @@ -99,6 +99,35 @@ ERST
132 Copy data from a backing file into a block device.
137 + .args_type = "directory:-d,backupfile:s,speed:o?,devlist:s?",
138 + .params = "[-d] backupfile [speed [devlist]]",
139 + .help = "create a VM Backup."
140 + "\n\t\t\t Use -d to dump data into a directory instead"
141 + "\n\t\t\t of using VMA format.",
147 + Create a VM backup.
151 + .name = "backup_cancel",
154 + .help = "cancel the current VM backup",
155 + .cmd = hmp_backup_cancel,
160 + Cancel the current VM backup.
165 diff --git a/include/block/block_int.h b/include/block/block_int.h
166 index 8f6135e6a5..4a572a2e34 100644
167 --- a/include/block/block_int.h
168 +++ b/include/block/block_int.h
171 typedef int BackupDumpFunc(void *opaque, uint64_t offset, uint64_t bytes, const void *buf);
173 -BlockDriverState *bdrv_backuo_dump_create(
174 +BlockDriverState *bdrv_backup_dump_create(
175 int dump_cb_block_size,
177 BackupDumpFunc *dump_cb,
178 diff --git a/include/monitor/hmp.h b/include/monitor/hmp.h
179 index 1247d7362a..8d3df46c93 100644
180 --- a/include/monitor/hmp.h
181 +++ b/include/monitor/hmp.h
182 @@ -29,6 +29,7 @@ void hmp_info_savevm(Monitor *mon, const QDict *qdict);
183 void hmp_info_migrate(Monitor *mon, const QDict *qdict);
184 void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict);
185 void hmp_info_migrate_parameters(Monitor *mon, const QDict *qdict);
186 +void hmp_info_backup(Monitor *mon, const QDict *qdict);
187 void hmp_info_cpus(Monitor *mon, const QDict *qdict);
188 void hmp_info_vnc(Monitor *mon, const QDict *qdict);
189 void hmp_info_spice(Monitor *mon, const QDict *qdict);
190 @@ -72,6 +73,8 @@ void hmp_x_colo_lost_heartbeat(Monitor *mon, const QDict *qdict);
191 void hmp_set_password(Monitor *mon, const QDict *qdict);
192 void hmp_expire_password(Monitor *mon, const QDict *qdict);
193 void hmp_change(Monitor *mon, const QDict *qdict);
194 +void hmp_backup(Monitor *mon, const QDict *qdict);
195 +void hmp_backup_cancel(Monitor *mon, const QDict *qdict);
196 void hmp_migrate(Monitor *mon, const QDict *qdict);
197 void hmp_device_add(Monitor *mon, const QDict *qdict);
198 void hmp_device_del(Monitor *mon, const QDict *qdict);
199 diff --git a/meson.build b/meson.build
200 index cc46eabb42..7d7e474313 100644
203 @@ -1065,6 +1065,7 @@ keyutils = dependency('libkeyutils', required: false,
204 has_gettid = cc.has_function('gettid')
206 libuuid = cc.find_library('uuid', required: true)
207 +libproxmox_backup_qemu = cc.find_library('proxmox_backup_qemu', required: true)
211 diff --git a/monitor/hmp-cmds.c b/monitor/hmp-cmds.c
212 index 64a84cf4ee..7efcd2d641 100644
213 --- a/monitor/hmp-cmds.c
214 +++ b/monitor/hmp-cmds.c
215 @@ -195,6 +195,50 @@ void hmp_info_mice(Monitor *mon, const QDict *qdict)
216 qapi_free_MouseInfoList(mice_list);
219 +void hmp_info_backup(Monitor *mon, const QDict *qdict)
221 + BackupStatus *info;
223 + info = qmp_query_backup(NULL);
226 + monitor_printf(mon, "Backup status: not initialized\n");
230 + if (info->has_status) {
231 + if (info->has_errmsg) {
232 + monitor_printf(mon, "Backup status: %s - %s\n",
233 + info->status, info->errmsg);
235 + monitor_printf(mon, "Backup status: %s\n", info->status);
239 + if (info->has_backup_file) {
240 + monitor_printf(mon, "Start time: %s", ctime(&info->start_time));
241 + if (info->end_time) {
242 + monitor_printf(mon, "End time: %s", ctime(&info->end_time));
245 + int per = (info->has_total && info->total &&
246 + info->has_transferred && info->transferred) ?
247 + (info->transferred * 100)/info->total : 0;
248 + int zero_per = (info->has_total && info->total &&
249 + info->has_zero_bytes && info->zero_bytes) ?
250 + (info->zero_bytes * 100)/info->total : 0;
251 + monitor_printf(mon, "Backup file: %s\n", info->backup_file);
252 + monitor_printf(mon, "Backup uuid: %s\n", info->uuid);
253 + monitor_printf(mon, "Total size: %zd\n", info->total);
254 + monitor_printf(mon, "Transferred bytes: %zd (%d%%)\n",
255 + info->transferred, per);
256 + monitor_printf(mon, "Zero bytes: %zd (%d%%)\n",
257 + info->zero_bytes, zero_per);
260 + qapi_free_BackupStatus(info);
263 static char *SocketAddress_to_str(SocketAddress *addr)
265 switch (addr->type) {
266 diff --git a/proxmox-backup-client.c b/proxmox-backup-client.c
268 index 0000000000..a8f6653a81
270 +++ b/proxmox-backup-client.c
272 +#include "proxmox-backup-client.h"
273 +#include "qemu/main-loop.h"
274 +#include "block/aio-wait.h"
275 +#include "qapi/error.h"
277 +/* Proxmox Backup Server client bindings using coroutines */
279 +typedef struct BlockOnCoroutineWrapper {
281 + CoroutineEntry *entry;
284 +} BlockOnCoroutineWrapper;
286 +static void coroutine_fn block_on_coroutine_wrapper(void *opaque)
288 + BlockOnCoroutineWrapper *wrapper = opaque;
289 + wrapper->entry(wrapper->entry_arg);
290 + wrapper->finished = true;
294 +void block_on_coroutine_fn(CoroutineEntry *entry, void *entry_arg)
296 + assert(!qemu_in_coroutine());
298 + AioContext *ctx = qemu_get_current_aio_context();
299 + BlockOnCoroutineWrapper wrapper = {
302 + .entry_arg = entry_arg,
305 + Coroutine *wrapper_co = qemu_coroutine_create(block_on_coroutine_wrapper, &wrapper);
306 + aio_co_enter(ctx, wrapper_co);
307 + AIO_WAIT_WHILE(ctx, !wrapper.finished);
310 +// This is called from another thread, so we use aio_co_schedule()
311 +static void proxmox_backup_schedule_wake(void *data) {
312 + CoCtxData *waker = (CoCtxData *)data;
313 + aio_co_schedule(waker->ctx, waker->co);
317 +proxmox_backup_co_connect(ProxmoxBackupHandle *pbs, Error **errp)
319 + Coroutine *co = qemu_coroutine_self();
320 + AioContext *ctx = qemu_get_current_aio_context();
321 + CoCtxData waker = { .co = co, .ctx = ctx };
322 + char *pbs_err = NULL;
325 + proxmox_backup_connect_async(pbs, proxmox_backup_schedule_wake, &waker, &pbs_res, &pbs_err);
326 + qemu_coroutine_yield();
328 + if (errp) error_setg(errp, "backup connect failed: %s", pbs_err ? pbs_err : "unknown error");
329 + if (pbs_err) proxmox_backup_free_error(pbs_err);
335 +proxmox_backup_co_add_config(
336 + ProxmoxBackupHandle *pbs,
338 + const uint8_t *data,
342 + Coroutine *co = qemu_coroutine_self();
343 + AioContext *ctx = qemu_get_current_aio_context();
344 + CoCtxData waker = { .co = co, .ctx = ctx };
345 + char *pbs_err = NULL;
348 + proxmox_backup_add_config_async(
349 + pbs, name, data, size ,proxmox_backup_schedule_wake, &waker, &pbs_res, &pbs_err);
350 + qemu_coroutine_yield();
352 + if (errp) error_setg(errp, "backup add_config %s failed: %s", name, pbs_err ? pbs_err : "unknown error");
353 + if (pbs_err) proxmox_backup_free_error(pbs_err);
359 +proxmox_backup_co_register_image(
360 + ProxmoxBackupHandle *pbs,
361 + const char *device_name,
365 + Coroutine *co = qemu_coroutine_self();
366 + AioContext *ctx = qemu_get_current_aio_context();
367 + CoCtxData waker = { .co = co, .ctx = ctx };
368 + char *pbs_err = NULL;
371 + proxmox_backup_register_image_async(
372 + pbs, device_name, size ,proxmox_backup_schedule_wake, &waker, &pbs_res, &pbs_err);
373 + qemu_coroutine_yield();
375 + if (errp) error_setg(errp, "backup register image failed: %s", pbs_err ? pbs_err : "unknown error");
376 + if (pbs_err) proxmox_backup_free_error(pbs_err);
382 +proxmox_backup_co_finish(
383 + ProxmoxBackupHandle *pbs,
386 + Coroutine *co = qemu_coroutine_self();
387 + AioContext *ctx = qemu_get_current_aio_context();
388 + CoCtxData waker = { .co = co, .ctx = ctx };
389 + char *pbs_err = NULL;
392 + proxmox_backup_finish_async(
393 + pbs, proxmox_backup_schedule_wake, &waker, &pbs_res, &pbs_err);
394 + qemu_coroutine_yield();
396 + if (errp) error_setg(errp, "backup finish failed: %s", pbs_err ? pbs_err : "unknown error");
397 + if (pbs_err) proxmox_backup_free_error(pbs_err);
403 +proxmox_backup_co_close_image(
404 + ProxmoxBackupHandle *pbs,
408 + Coroutine *co = qemu_coroutine_self();
409 + AioContext *ctx = qemu_get_current_aio_context();
410 + CoCtxData waker = { .co = co, .ctx = ctx };
411 + char *pbs_err = NULL;
414 + proxmox_backup_close_image_async(
415 + pbs, dev_id, proxmox_backup_schedule_wake, &waker, &pbs_res, &pbs_err);
416 + qemu_coroutine_yield();
418 + if (errp) error_setg(errp, "backup close image failed: %s", pbs_err ? pbs_err : "unknown error");
419 + if (pbs_err) proxmox_backup_free_error(pbs_err);
425 +proxmox_backup_co_write_data(
426 + ProxmoxBackupHandle *pbs,
428 + const uint8_t *data,
433 + Coroutine *co = qemu_coroutine_self();
434 + AioContext *ctx = qemu_get_current_aio_context();
435 + CoCtxData waker = { .co = co, .ctx = ctx };
436 + char *pbs_err = NULL;
439 + proxmox_backup_write_data_async(
440 + pbs, dev_id, data, offset, size, proxmox_backup_schedule_wake, &waker, &pbs_res, &pbs_err);
441 + qemu_coroutine_yield();
443 + if (errp) error_setg(errp, "backup write data failed: %s", pbs_err ? pbs_err : "unknown error");
444 + if (pbs_err) proxmox_backup_free_error(pbs_err);
448 diff --git a/proxmox-backup-client.h b/proxmox-backup-client.h
450 index 0000000000..1dda8b7d8f
452 +++ b/proxmox-backup-client.h
454 +#ifndef PROXMOX_BACKUP_CLIENT_H
455 +#define PROXMOX_BACKUP_CLIENT_H
457 +#include "qemu/osdep.h"
458 +#include "qemu/coroutine.h"
459 +#include "proxmox-backup-qemu.h"
461 +typedef struct CoCtxData {
467 +// FIXME: Remove once coroutines are supported for QMP
468 +void block_on_coroutine_fn(CoroutineEntry *entry, void *entry_arg);
471 +proxmox_backup_co_connect(
472 + ProxmoxBackupHandle *pbs,
476 +proxmox_backup_co_add_config(
477 + ProxmoxBackupHandle *pbs,
479 + const uint8_t *data,
484 +proxmox_backup_co_register_image(
485 + ProxmoxBackupHandle *pbs,
486 + const char *device_name,
492 +proxmox_backup_co_finish(
493 + ProxmoxBackupHandle *pbs,
497 +proxmox_backup_co_close_image(
498 + ProxmoxBackupHandle *pbs,
503 +proxmox_backup_co_write_data(
504 + ProxmoxBackupHandle *pbs,
506 + const uint8_t *data,
512 +#endif /* PROXMOX_BACKUP_CLIENT_H */
513 diff --git a/pve-backup.c b/pve-backup.c
515 index 0000000000..66868dec14
519 +#include "proxmox-backup-client.h"
522 +#include "qemu/osdep.h"
523 +#include "qemu/module.h"
524 +#include "sysemu/block-backend.h"
525 +#include "sysemu/blockdev.h"
526 +#include "block/blockjob.h"
527 +#include "qapi/qapi-commands-block.h"
528 +#include "qapi/qmp/qerror.h"
530 +/* PVE backup state and related function */
533 + * Note: A resume from a qemu_coroutine_yield can happen in a different thread,
534 + * so you may not use normal mutexes within coroutines:
536 + * ---bad-example---
537 + * qemu_rec_mutex_lock(lock)
539 + * qemu_coroutine_yield() // wait for something
540 + * // we are now inside a different thread
541 + * qemu_rec_mutex_unlock(lock) // Crash - wrong thread!!
542 + * ---end-bad-example--
544 + * ==> Always use CoMutext inside coroutines.
545 + * ==> Never acquire/release AioContext withing coroutines (because that use QemuRecMutex)
549 +static struct PVEBackupState {
551 + // Everithing accessed from qmp_backup_query command is protected using lock
560 + size_t transferred;
565 + ProxmoxBackupHandle *pbs;
567 + QemuMutex backup_mutex;
568 + CoMutex dump_callback_mutex;
571 +static void pvebackup_init(void)
573 + qemu_mutex_init(&backup_state.stat.lock);
574 + qemu_mutex_init(&backup_state.backup_mutex);
575 + qemu_co_mutex_init(&backup_state.dump_callback_mutex);
578 +// initialize PVEBackupState at startup
579 +opts_init(pvebackup_init);
581 +typedef struct PVEBackupDevInfo {
582 + BlockDriverState *bs;
586 + char targetfile[PATH_MAX];
587 + BlockDriverState *target;
590 +static void pvebackup_run_next_job(void);
593 +lookup_active_block_job(PVEBackupDevInfo *di)
595 + if (!di->completed && di->bs) {
596 + for (BlockJob *job = block_job_next(NULL); job; job = block_job_next(job)) {
597 + if (job->job.driver->job_type != JOB_TYPE_BACKUP) {
601 + BackupBlockJob *bjob = container_of(job, BackupBlockJob, common);
602 + if (bjob && bjob->source_bs == di->bs) {
610 +static void pvebackup_propagate_error(Error *err)
612 + qemu_mutex_lock(&backup_state.stat.lock);
613 + error_propagate(&backup_state.stat.error, err);
614 + qemu_mutex_unlock(&backup_state.stat.lock);
617 +static bool pvebackup_error_or_canceled(void)
619 + qemu_mutex_lock(&backup_state.stat.lock);
620 + bool error_or_canceled = !!backup_state.stat.error;
621 + qemu_mutex_unlock(&backup_state.stat.lock);
623 + return error_or_canceled;
626 +static void pvebackup_add_transfered_bytes(size_t transferred, size_t zero_bytes)
628 + qemu_mutex_lock(&backup_state.stat.lock);
629 + backup_state.stat.zero_bytes += zero_bytes;
630 + backup_state.stat.transferred += transferred;
631 + qemu_mutex_unlock(&backup_state.stat.lock);
634 +// This may get called from multiple coroutines in multiple io-threads
635 +// Note1: this may get called after job_cancel()
636 +static int coroutine_fn
637 +pvebackup_co_dump_pbs_cb(
643 + assert(qemu_in_coroutine());
645 + const uint64_t size = bytes;
646 + const unsigned char *buf = pbuf;
647 + PVEBackupDevInfo *di = opaque;
649 + assert(backup_state.pbs);
651 + Error *local_err = NULL;
654 + qemu_co_mutex_lock(&backup_state.dump_callback_mutex);
656 + // avoid deadlock if job is cancelled
657 + if (pvebackup_error_or_canceled()) {
658 + qemu_co_mutex_unlock(&backup_state.dump_callback_mutex);
662 + pbs_res = proxmox_backup_co_write_data(backup_state.pbs, di->dev_id, buf, start, size, &local_err);
663 + qemu_co_mutex_unlock(&backup_state.dump_callback_mutex);
666 + pvebackup_propagate_error(local_err);
669 + pvebackup_add_transfered_bytes(size, !buf ? size : 0);
675 +// This may get called from multiple coroutines in multiple io-threads
676 +static int coroutine_fn
677 +pvebackup_co_dump_vma_cb(
683 + assert(qemu_in_coroutine());
685 + const uint64_t size = bytes;
686 + const unsigned char *buf = pbuf;
687 + PVEBackupDevInfo *di = opaque;
691 + assert(backup_state.vmaw);
693 + uint64_t remaining = size;
695 + uint64_t cluster_num = start / VMA_CLUSTER_SIZE;
696 + if ((cluster_num * VMA_CLUSTER_SIZE) != start) {
697 + Error *local_err = NULL;
698 + error_setg(&local_err,
699 + "got unaligned write inside backup dump "
700 + "callback (sector %ld)", start);
701 + pvebackup_propagate_error(local_err);
702 + return -1; // not aligned to cluster size
705 + while (remaining > 0) {
706 + qemu_co_mutex_lock(&backup_state.dump_callback_mutex);
707 + // avoid deadlock if job is cancelled
708 + if (pvebackup_error_or_canceled()) {
709 + qemu_co_mutex_unlock(&backup_state.dump_callback_mutex);
713 + size_t zero_bytes = 0;
714 + ret = vma_writer_write(backup_state.vmaw, di->dev_id, cluster_num, buf, &zero_bytes);
715 + qemu_co_mutex_unlock(&backup_state.dump_callback_mutex);
719 + buf += VMA_CLUSTER_SIZE;
722 + Error *local_err = NULL;
723 + vma_writer_error_propagate(backup_state.vmaw, &local_err);
724 + pvebackup_propagate_error(local_err);
727 + if (remaining >= VMA_CLUSTER_SIZE) {
728 + assert(ret == VMA_CLUSTER_SIZE);
729 + pvebackup_add_transfered_bytes(VMA_CLUSTER_SIZE, zero_bytes);
730 + remaining -= VMA_CLUSTER_SIZE;
732 + assert(ret == remaining);
733 + pvebackup_add_transfered_bytes(remaining, zero_bytes);
742 +// assumes the caller holds backup_mutex
743 +static void coroutine_fn pvebackup_co_cleanup(void *unused)
745 + assert(qemu_in_coroutine());
747 + qemu_mutex_lock(&backup_state.stat.lock);
748 + backup_state.stat.end_time = time(NULL);
749 + qemu_mutex_unlock(&backup_state.stat.lock);
751 + if (backup_state.vmaw) {
752 + Error *local_err = NULL;
753 + vma_writer_close(backup_state.vmaw, &local_err);
755 + if (local_err != NULL) {
756 + pvebackup_propagate_error(local_err);
759 + backup_state.vmaw = NULL;
762 + if (backup_state.pbs) {
763 + if (!pvebackup_error_or_canceled()) {
764 + Error *local_err = NULL;
765 + proxmox_backup_co_finish(backup_state.pbs, &local_err);
766 + if (local_err != NULL) {
767 + pvebackup_propagate_error(local_err);
771 + proxmox_backup_disconnect(backup_state.pbs);
772 + backup_state.pbs = NULL;
775 + g_list_free(backup_state.di_list);
776 + backup_state.di_list = NULL;
779 +// assumes the caller holds backup_mutex
780 +static void coroutine_fn pvebackup_complete_stream(void *opaque)
782 + PVEBackupDevInfo *di = opaque;
784 + bool error_or_canceled = pvebackup_error_or_canceled();
786 + if (backup_state.vmaw) {
787 + vma_writer_close_stream(backup_state.vmaw, di->dev_id);
790 + if (backup_state.pbs && !error_or_canceled) {
791 + Error *local_err = NULL;
792 + proxmox_backup_co_close_image(backup_state.pbs, di->dev_id, &local_err);
793 + if (local_err != NULL) {
794 + pvebackup_propagate_error(local_err);
799 +static void pvebackup_complete_cb(void *opaque, int ret)
801 + assert(!qemu_in_coroutine());
803 + PVEBackupDevInfo *di = opaque;
805 + qemu_mutex_lock(&backup_state.backup_mutex);
807 + di->completed = true;
810 + Error *local_err = NULL;
811 + error_setg(&local_err, "job failed with err %d - %s", ret, strerror(-ret));
812 + pvebackup_propagate_error(local_err);
817 + assert(di->target == NULL);
819 + block_on_coroutine_fn(pvebackup_complete_stream, di);
821 + // remove self from job queue
822 + backup_state.di_list = g_list_remove(backup_state.di_list, di);
826 + qemu_mutex_unlock(&backup_state.backup_mutex);
828 + pvebackup_run_next_job();
831 +static void pvebackup_cancel(void)
833 + assert(!qemu_in_coroutine());
835 + Error *cancel_err = NULL;
836 + error_setg(&cancel_err, "backup canceled");
837 + pvebackup_propagate_error(cancel_err);
839 + qemu_mutex_lock(&backup_state.backup_mutex);
841 + if (backup_state.vmaw) {
842 + /* make sure vma writer does not block anymore */
843 + vma_writer_set_error(backup_state.vmaw, "backup canceled");
846 + if (backup_state.pbs) {
847 + proxmox_backup_abort(backup_state.pbs, "backup canceled");
850 + qemu_mutex_unlock(&backup_state.backup_mutex);
854 + BlockJob *next_job = NULL;
856 + qemu_mutex_lock(&backup_state.backup_mutex);
858 + GList *l = backup_state.di_list;
860 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
861 + l = g_list_next(l);
863 + BlockJob *job = lookup_active_block_job(di);
870 + qemu_mutex_unlock(&backup_state.backup_mutex);
873 + AioContext *aio_context = next_job->job.aio_context;
874 + aio_context_acquire(aio_context);
875 + job_cancel_sync(&next_job->job);
876 + aio_context_release(aio_context);
883 +void qmp_backup_cancel(Error **errp)
885 + pvebackup_cancel();
888 +// assumes the caller holds backup_mutex
889 +static int coroutine_fn pvebackup_co_add_config(
892 + BackupFormat format,
893 + const char *backup_dir,
895 + ProxmoxBackupHandle *pbs,
900 + char *cdata = NULL;
902 + GError *err = NULL;
903 + if (!g_file_get_contents(file, &cdata, &clen, &err)) {
904 + error_setg(errp, "unable to read file '%s'", file);
908 + char *basename = g_path_get_basename(file);
909 + if (name == NULL) name = basename;
911 + if (format == BACKUP_FORMAT_VMA) {
912 + if (vma_writer_add_config(vmaw, name, cdata, clen) != 0) {
913 + error_setg(errp, "unable to add %s config data to vma archive", file);
916 + } else if (format == BACKUP_FORMAT_PBS) {
917 + if (proxmox_backup_co_add_config(pbs, name, (unsigned char *)cdata, clen, errp) < 0)
919 + } else if (format == BACKUP_FORMAT_DIR) {
920 + char config_path[PATH_MAX];
921 + snprintf(config_path, PATH_MAX, "%s/%s", backup_dir, name);
922 + if (!g_file_set_contents(config_path, cdata, clen, &err)) {
923 + error_setg(errp, "unable to write config file '%s'", config_path);
938 +bool job_should_pause(Job *job);
940 +static void pvebackup_run_next_job(void)
942 + assert(!qemu_in_coroutine());
944 + qemu_mutex_lock(&backup_state.backup_mutex);
946 + GList *l = backup_state.di_list;
948 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
949 + l = g_list_next(l);
951 + BlockJob *job = lookup_active_block_job(di);
954 + qemu_mutex_unlock(&backup_state.backup_mutex);
956 + AioContext *aio_context = job->job.aio_context;
957 + aio_context_acquire(aio_context);
959 + if (job_should_pause(&job->job)) {
960 + bool error_or_canceled = pvebackup_error_or_canceled();
961 + if (error_or_canceled) {
962 + job_cancel_sync(&job->job);
964 + job_resume(&job->job);
967 + aio_context_release(aio_context);
972 + block_on_coroutine_fn(pvebackup_co_cleanup, NULL); // no more jobs, run cleanup
974 + qemu_mutex_unlock(&backup_state.backup_mutex);
977 +static bool create_backup_jobs(void) {
979 + assert(!qemu_in_coroutine());
981 + Error *local_err = NULL;
983 + BackupPerf perf = { .max_workers = 16 };
985 + /* create and start all jobs (paused state) */
986 + GList *l = backup_state.di_list;
988 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
989 + l = g_list_next(l);
991 + assert(di->target != NULL);
993 + AioContext *aio_context = bdrv_get_aio_context(di->bs);
994 + aio_context_acquire(aio_context);
996 + BlockJob *job = backup_job_create(
997 + NULL, di->bs, di->target, backup_state.speed, MIRROR_SYNC_MODE_FULL, NULL,
998 + BITMAP_SYNC_MODE_NEVER, false, NULL, &perf, BLOCKDEV_ON_ERROR_REPORT, BLOCKDEV_ON_ERROR_REPORT,
999 + JOB_DEFAULT, pvebackup_complete_cb, di, NULL, &local_err);
1001 + aio_context_release(aio_context);
1003 + if (!job || local_err != NULL) {
1004 + Error *create_job_err = NULL;
1005 + error_setg(&create_job_err, "backup_job_create failed: %s",
1006 + local_err ? error_get_pretty(local_err) : "null");
1008 + pvebackup_propagate_error(create_job_err);
1011 + job_start(&job->job);
1013 + bdrv_unref(di->target);
1014 + di->target = NULL;
1017 + bool errors = pvebackup_error_or_canceled();
1020 + l = backup_state.di_list;
1022 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
1023 + l = g_list_next(l);
1026 + bdrv_unref(di->target);
1027 + di->target = NULL;
1035 +typedef struct QmpBackupTask {
1036 + const char *backup_file;
1037 + bool has_password;
1038 + const char *password;
1040 + const char *keyfile;
1041 + bool has_key_password;
1042 + const char *key_password;
1043 + bool has_backup_id;
1044 + const char *backup_id;
1045 + bool has_backup_time;
1046 + const char *fingerprint;
1047 + bool has_fingerprint;
1048 + int64_t backup_time;
1050 + BackupFormat format;
1051 + bool has_config_file;
1052 + const char *config_file;
1053 + bool has_firewall_file;
1054 + const char *firewall_file;
1056 + const char *devlist;
1063 +// assumes the caller holds backup_mutex
1064 +static void coroutine_fn pvebackup_co_prepare(void *opaque)
1066 + assert(qemu_in_coroutine());
1068 + QmpBackupTask *task = opaque;
1070 + task->result = NULL; // just to be sure
1072 + BlockBackend *blk;
1073 + BlockDriverState *bs = NULL;
1074 + const char *backup_dir = NULL;
1075 + Error *local_err = NULL;
1077 + VmaWriter *vmaw = NULL;
1078 + ProxmoxBackupHandle *pbs = NULL;
1079 + gchar **devs = NULL;
1080 + GList *di_list = NULL;
1082 + UuidInfo *uuid_info;
1084 + const char *config_name = "qemu-server.conf";
1085 + const char *firewall_name = "qemu-server.fw";
1087 + if (backup_state.di_list) {
1088 + error_set(task->errp, ERROR_CLASS_GENERIC_ERROR,
1089 + "previous backup not finished");
1093 + /* Todo: try to auto-detect format based on file name */
1094 + BackupFormat format = task->has_format ? task->format : BACKUP_FORMAT_VMA;
1096 + if (task->has_devlist) {
1097 + devs = g_strsplit_set(task->devlist, ",;:", -1);
1101 + blk = blk_by_name(*d);
1104 + if (!bdrv_is_inserted(bs)) {
1105 + error_setg(task->errp, QERR_DEVICE_HAS_NO_MEDIUM, *d);
1108 + PVEBackupDevInfo *di = g_new0(PVEBackupDevInfo, 1);
1110 + di_list = g_list_append(di_list, di);
1112 + error_set(task->errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1113 + "Device '%s' not found", *d);
1120 + BdrvNextIterator it;
1123 + for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
1124 + if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
1128 + PVEBackupDevInfo *di = g_new0(PVEBackupDevInfo, 1);
1130 + di_list = g_list_append(di_list, di);
1135 + error_set(task->errp, ERROR_CLASS_GENERIC_ERROR, "empty device list");
1143 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
1144 + l = g_list_next(l);
1145 + if (bdrv_op_is_blocked(di->bs, BLOCK_OP_TYPE_BACKUP_SOURCE, task->errp)) {
1149 + ssize_t size = bdrv_getlength(di->bs);
1151 + error_setg_errno(task->errp, -di->size, "bdrv_getlength failed");
1158 + uuid_generate(uuid);
1160 + if (format == BACKUP_FORMAT_PBS) {
1161 + if (!task->has_password) {
1162 + error_set(task->errp, ERROR_CLASS_GENERIC_ERROR, "missing parameter 'password'");
1165 + if (!task->has_backup_id) {
1166 + error_set(task->errp, ERROR_CLASS_GENERIC_ERROR, "missing parameter 'backup-id'");
1169 + if (!task->has_backup_time) {
1170 + error_set(task->errp, ERROR_CLASS_GENERIC_ERROR, "missing parameter 'backup-time'");
1174 + int dump_cb_block_size = PROXMOX_BACKUP_DEFAULT_CHUNK_SIZE; // Hardcoded (4M)
1175 + firewall_name = "fw.conf";
1177 + char *pbs_err = NULL;
1178 + pbs = proxmox_backup_new(
1179 + task->backup_file,
1181 + task->backup_time,
1182 + dump_cb_block_size,
1183 + task->has_password ? task->password : NULL,
1184 + task->has_keyfile ? task->keyfile : NULL,
1185 + task->has_key_password ? task->key_password : NULL,
1186 + task->has_fingerprint ? task->fingerprint : NULL,
1190 + error_set(task->errp, ERROR_CLASS_GENERIC_ERROR,
1191 + "proxmox_backup_new failed: %s", pbs_err);
1192 + proxmox_backup_free_error(pbs_err);
1196 + if (proxmox_backup_co_connect(pbs, task->errp) < 0)
1199 + /* register all devices */
1202 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
1203 + l = g_list_next(l);
1205 + const char *devname = bdrv_get_device_name(di->bs);
1207 + int dev_id = proxmox_backup_co_register_image(pbs, devname, di->size, task->errp);
1211 + if (!(di->target = bdrv_backup_dump_create(dump_cb_block_size, di->size, pvebackup_co_dump_pbs_cb, di, task->errp))) {
1215 + di->dev_id = dev_id;
1217 + } else if (format == BACKUP_FORMAT_VMA) {
1218 + vmaw = vma_writer_create(task->backup_file, uuid, &local_err);
1221 + error_propagate(task->errp, local_err);
1226 + /* register all devices for vma writer */
1229 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
1230 + l = g_list_next(l);
1232 + if (!(di->target = bdrv_backup_dump_create(VMA_CLUSTER_SIZE, di->size, pvebackup_co_dump_vma_cb, di, task->errp))) {
1236 + const char *devname = bdrv_get_device_name(di->bs);
1237 + di->dev_id = vma_writer_register_stream(vmaw, devname, di->size);
1238 + if (di->dev_id <= 0) {
1239 + error_set(task->errp, ERROR_CLASS_GENERIC_ERROR,
1240 + "register_stream failed");
1244 + } else if (format == BACKUP_FORMAT_DIR) {
1245 + if (mkdir(task->backup_file, 0640) != 0) {
1246 + error_setg_errno(task->errp, errno, "can't create directory '%s'\n",
1247 + task->backup_file);
1250 + backup_dir = task->backup_file;
1254 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
1255 + l = g_list_next(l);
1257 + const char *devname = bdrv_get_device_name(di->bs);
1258 + snprintf(di->targetfile, PATH_MAX, "%s/%s.raw", backup_dir, devname);
1260 + int flags = BDRV_O_RDWR;
1261 + bdrv_img_create(di->targetfile, "raw", NULL, NULL, NULL,
1262 + di->size, flags, false, &local_err);
1264 + error_propagate(task->errp, local_err);
1268 + di->target = bdrv_open(di->targetfile, NULL, NULL, flags, &local_err);
1269 + if (!di->target) {
1270 + error_propagate(task->errp, local_err);
1275 + error_set(task->errp, ERROR_CLASS_GENERIC_ERROR, "unknown backup format");
1280 + /* add configuration file to archive */
1281 + if (task->has_config_file) {
1282 + if (pvebackup_co_add_config(task->config_file, config_name, format, backup_dir,
1283 + vmaw, pbs, task->errp) != 0) {
1288 + /* add firewall file to archive */
1289 + if (task->has_firewall_file) {
1290 + if (pvebackup_co_add_config(task->firewall_file, firewall_name, format, backup_dir,
1291 + vmaw, pbs, task->errp) != 0) {
1295 + /* initialize global backup_state now */
1297 + qemu_mutex_lock(&backup_state.stat.lock);
1299 + if (backup_state.stat.error) {
1300 + error_free(backup_state.stat.error);
1301 + backup_state.stat.error = NULL;
1304 + backup_state.stat.start_time = time(NULL);
1305 + backup_state.stat.end_time = 0;
1307 + if (backup_state.stat.backup_file) {
1308 + g_free(backup_state.stat.backup_file);
1310 + backup_state.stat.backup_file = g_strdup(task->backup_file);
1312 + uuid_copy(backup_state.stat.uuid, uuid);
1313 + uuid_unparse_lower(uuid, backup_state.stat.uuid_str);
1314 + char *uuid_str = g_strdup(backup_state.stat.uuid_str);
1316 + backup_state.stat.total = total;
1317 + backup_state.stat.transferred = 0;
1318 + backup_state.stat.zero_bytes = 0;
1320 + qemu_mutex_unlock(&backup_state.stat.lock);
1322 + backup_state.speed = (task->has_speed && task->speed > 0) ? task->speed : 0;
1324 + backup_state.vmaw = vmaw;
1325 + backup_state.pbs = pbs;
1327 + backup_state.di_list = di_list;
1329 + uuid_info = g_malloc0(sizeof(*uuid_info));
1330 + uuid_info->UUID = uuid_str;
1332 + task->result = uuid_info;
1339 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
1340 + l = g_list_next(l);
1343 + bdrv_unref(di->target);
1346 + if (di->targetfile[0]) {
1347 + unlink(di->targetfile);
1351 + g_list_free(di_list);
1358 + Error *err = NULL;
1359 + vma_writer_close(vmaw, &err);
1360 + unlink(task->backup_file);
1364 + proxmox_backup_disconnect(pbs);
1368 + rmdir(backup_dir);
1371 + task->result = NULL;
1375 +UuidInfo *qmp_backup(
1376 + const char *backup_file,
1377 + bool has_password, const char *password,
1378 + bool has_keyfile, const char *keyfile,
1379 + bool has_key_password, const char *key_password,
1380 + bool has_fingerprint, const char *fingerprint,
1381 + bool has_backup_id, const char *backup_id,
1382 + bool has_backup_time, int64_t backup_time,
1383 + bool has_format, BackupFormat format,
1384 + bool has_config_file, const char *config_file,
1385 + bool has_firewall_file, const char *firewall_file,
1386 + bool has_devlist, const char *devlist,
1387 + bool has_speed, int64_t speed, Error **errp)
1389 + QmpBackupTask task = {
1390 + .backup_file = backup_file,
1391 + .has_password = has_password,
1392 + .password = password,
1393 + .has_key_password = has_key_password,
1394 + .key_password = key_password,
1395 + .has_fingerprint = has_fingerprint,
1396 + .fingerprint = fingerprint,
1397 + .has_backup_id = has_backup_id,
1398 + .backup_id = backup_id,
1399 + .has_backup_time = has_backup_time,
1400 + .backup_time = backup_time,
1401 + .has_format = has_format,
1403 + .has_config_file = has_config_file,
1404 + .config_file = config_file,
1405 + .has_firewall_file = has_firewall_file,
1406 + .firewall_file = firewall_file,
1407 + .has_devlist = has_devlist,
1408 + .devlist = devlist,
1409 + .has_speed = has_speed,
1414 + qemu_mutex_lock(&backup_state.backup_mutex);
1416 + block_on_coroutine_fn(pvebackup_co_prepare, &task);
1418 + if (*errp == NULL) {
1419 + create_backup_jobs();
1420 + qemu_mutex_unlock(&backup_state.backup_mutex);
1421 + pvebackup_run_next_job();
1423 + qemu_mutex_unlock(&backup_state.backup_mutex);
1426 + return task.result;
1429 +BackupStatus *qmp_query_backup(Error **errp)
1431 + BackupStatus *info = g_malloc0(sizeof(*info));
1433 + qemu_mutex_lock(&backup_state.stat.lock);
1435 + if (!backup_state.stat.start_time) {
1436 + /* not started, return {} */
1437 + qemu_mutex_unlock(&backup_state.stat.lock);
1441 + info->has_status = true;
1442 + info->has_start_time = true;
1443 + info->start_time = backup_state.stat.start_time;
1445 + if (backup_state.stat.backup_file) {
1446 + info->has_backup_file = true;
1447 + info->backup_file = g_strdup(backup_state.stat.backup_file);
1450 + info->has_uuid = true;
1451 + info->uuid = g_strdup(backup_state.stat.uuid_str);
1453 + if (backup_state.stat.end_time) {
1454 + if (backup_state.stat.error) {
1455 + info->status = g_strdup("error");
1456 + info->has_errmsg = true;
1457 + info->errmsg = g_strdup(error_get_pretty(backup_state.stat.error));
1459 + info->status = g_strdup("done");
1461 + info->has_end_time = true;
1462 + info->end_time = backup_state.stat.end_time;
1464 + info->status = g_strdup("active");
1467 + info->has_total = true;
1468 + info->total = backup_state.stat.total;
1469 + info->has_zero_bytes = true;
1470 + info->zero_bytes = backup_state.stat.zero_bytes;
1471 + info->has_transferred = true;
1472 + info->transferred = backup_state.stat.transferred;
1474 + qemu_mutex_unlock(&backup_state.stat.lock);
1478 diff --git a/qapi/block-core.json b/qapi/block-core.json
1479 index fdfa579d00..c5d604693f 100644
1480 --- a/qapi/block-core.json
1481 +++ b/qapi/block-core.json
1482 @@ -699,6 +699,115 @@
1483 { 'command': 'query-block', 'returns': ['BlockInfo'] }
1489 +# Detailed backup status.
1491 +# @status: string describing the current backup status.
1492 +# This can be 'active', 'done', 'error'. If this field is not
1493 +# returned, no backup process has been initiated
1495 +# @errmsg: error message (only returned if status is 'error')
1497 +# @total: total amount of bytes involved in the backup process
1499 +# @transferred: amount of bytes already backed up.
1501 +# @zero-bytes: amount of 'zero' bytes detected.
1503 +# @start-time: time (epoch) when backup job started.
1505 +# @end-time: time (epoch) when backup job finished.
1507 +# @backup-file: backup file name
1509 +# @uuid: uuid for this backup job
1512 +{ 'struct': 'BackupStatus',
1513 + 'data': {'*status': 'str', '*errmsg': 'str', '*total': 'int',
1514 + '*transferred': 'int', '*zero-bytes': 'int',
1515 + '*start-time': 'int', '*end-time': 'int',
1516 + '*backup-file': 'str', '*uuid': 'str' } }
1521 +# An enumeration of supported backup formats.
1523 +# @vma: Proxmox vma backup format
1525 +{ 'enum': 'BackupFormat',
1526 + 'data': [ 'vma', 'dir', 'pbs' ] }
1531 +# Starts a VM backup.
1533 +# @backup-file: the backup file name
1535 +# @format: format of the backup file
1537 +# @config-file: a configuration file to include into
1538 +# the backup archive.
1540 +# @speed: the maximum speed, in bytes per second
1542 +# @devlist: list of block device names (separated by ',', ';'
1543 +# or ':'). By default the backup includes all writable block devices.
1545 +# @password: backup server passsword (required for format 'pbs')
1547 +# @keyfile: keyfile used for encryption (optional for format 'pbs')
1549 +# @key-password: password for keyfile (optional for format 'pbs')
1551 +# @fingerprint: server cert fingerprint (optional for format 'pbs')
1553 +# @backup-id: backup ID (required for format 'pbs')
1555 +# @backup-time: backup timestamp (Unix epoch, required for format 'pbs')
1557 +# Returns: the uuid of the backup job
1560 +{ 'command': 'backup', 'data': { 'backup-file': 'str',
1561 + '*password': 'str',
1562 + '*keyfile': 'str',
1563 + '*key-password': 'str',
1564 + '*fingerprint': 'str',
1565 + '*backup-id': 'str',
1566 + '*backup-time': 'int',
1567 + '*format': 'BackupFormat',
1568 + '*config-file': 'str',
1569 + '*firewall-file': 'str',
1570 + '*devlist': 'str', '*speed': 'int' },
1571 + 'returns': 'UuidInfo' }
1576 +# Returns information about current/last backup task.
1578 +# Returns: @BackupStatus
1581 +{ 'command': 'query-backup', 'returns': 'BackupStatus' }
1586 +# Cancel the current executing backup process.
1588 +# Returns: nothing on success
1590 +# Notes: This command succeeds even if there is no backup process running.
1593 +{ 'command': 'backup-cancel' }
1596 # @BlockDeviceTimedStats:
1598 diff --git a/qapi/common.json b/qapi/common.json
1599 index 7c976296f0..0690b07903 100644
1600 --- a/qapi/common.json
1601 +++ b/qapi/common.json
1602 @@ -197,3 +197,16 @@
1603 { 'enum': 'GrabToggleKeys',
1604 'data': [ 'ctrl-ctrl', 'alt-alt', 'shift-shift','meta-meta', 'scrolllock',
1605 'ctrl-scrolllock' ] }
1610 +# Guest UUID information (Universally Unique Identifier).
1612 +# @UUID: the UUID of the guest
1616 +# Notes: If no UUID was specified for the guest, a null UUID is returned.
1618 +{ 'struct': 'UuidInfo', 'data': {'UUID': 'str'} }
1619 diff --git a/qapi/machine.json b/qapi/machine.json
1620 index a6f483af4f..6effa7ad30 100644
1621 --- a/qapi/machine.json
1622 +++ b/qapi/machine.json
1624 # This work is licensed under the terms of the GNU GPL, version 2 or later.
1625 # See the COPYING file in the top-level directory.
1627 +{ 'include': 'common.json' }
1632 @@ -229,19 +231,6 @@
1634 { 'command': 'query-target', 'returns': 'TargetInfo' }
1639 -# Guest UUID information (Universally Unique Identifier).
1641 -# @UUID: the UUID of the guest
1645 -# Notes: If no UUID was specified for the guest, a null UUID is returned.
1647 -{ 'struct': 'UuidInfo', 'data': {'UUID': 'str'} }