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>
10 [FE: add new force parameter to job_cancel_sync calls
11 adapt for new job lock mechanism replacing AioContext locks]
12 Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
14 block/meson.build | 5 +
15 block/monitor/block-hmp-cmds.c | 33 ++
17 hmp-commands-info.hx | 14 +
18 hmp-commands.hx | 29 +
19 include/monitor/hmp.h | 3 +
21 monitor/hmp-cmds.c | 44 ++
22 proxmox-backup-client.c | 176 ++++++
23 proxmox-backup-client.h | 59 ++
24 pve-backup.c | 956 +++++++++++++++++++++++++++++++++
25 qapi/block-core.json | 109 ++++
26 qapi/common.json | 13 +
27 qapi/machine.json | 15 +-
28 14 files changed, 1445 insertions(+), 13 deletions(-)
29 create mode 100644 proxmox-backup-client.c
30 create mode 100644 proxmox-backup-client.h
31 create mode 100644 pve-backup.c
33 diff --git a/block/meson.build b/block/meson.build
34 index 0d7023fc82..e995ae72b9 100644
35 --- a/block/meson.build
36 +++ b/block/meson.build
37 @@ -48,6 +48,11 @@ block_ss.add(files(
38 ), zstd, zlib, gnutls)
40 block_ss.add(files('../vma-writer.c'), libuuid)
42 + '../proxmox-backup-client.c',
44 +), libproxmox_backup_qemu)
47 softmmu_ss.add(when: 'CONFIG_TCG', if_true: files('blkreplay.c'))
48 softmmu_ss.add(files('block-ram-registrar.c'))
49 diff --git a/block/monitor/block-hmp-cmds.c b/block/monitor/block-hmp-cmds.c
50 index b6135e9bfe..477044c54a 100644
51 --- a/block/monitor/block-hmp-cmds.c
52 +++ b/block/monitor/block-hmp-cmds.c
53 @@ -1015,3 +1015,36 @@ void hmp_info_snapshots(Monitor *mon, const QDict *qdict)
55 g_free(global_snapshots);
58 +void hmp_backup_cancel(Monitor *mon, const QDict *qdict)
60 + Error *error = NULL;
62 + qmp_backup_cancel(&error);
64 + hmp_handle_error(mon, error);
67 +void hmp_backup(Monitor *mon, const QDict *qdict)
69 + Error *error = NULL;
71 + int dir = qdict_get_try_bool(qdict, "directory", 0);
72 + const char *backup_file = qdict_get_str(qdict, "backupfile");
73 + const char *devlist = qdict_get_try_str(qdict, "devlist");
74 + int64_t speed = qdict_get_try_int(qdict, "speed", 0);
78 + false, NULL, // PBS password
79 + false, NULL, // PBS keyfile
80 + false, NULL, // PBS key_password
81 + false, NULL, // PBS fingerprint
82 + false, NULL, // PBS backup-id
83 + false, 0, // PBS backup-time
84 + true, dir ? BACKUP_FORMAT_DIR : BACKUP_FORMAT_VMA,
85 + false, NULL, false, NULL, !!devlist,
86 + devlist, qdict_haskey(qdict, "speed"), speed, &error);
88 + hmp_handle_error(mon, error);
90 diff --git a/blockdev.c b/blockdev.c
91 index 756e980889..bc8d67b290 100644
95 #include "hw/block/block.h"
96 #include "block/blockjob.h"
97 #include "block/qdict.h"
98 +#include "block/blockjob_int.h"
99 #include "block/throttle-groups.h"
100 #include "monitor/monitor.h"
101 #include "qemu/error-report.h"
102 diff --git a/hmp-commands-info.hx b/hmp-commands-info.hx
103 index 489c524e9e..bc1d46d845 100644
104 --- a/hmp-commands-info.hx
105 +++ b/hmp-commands-info.hx
106 @@ -486,6 +486,20 @@ SRST
107 Show the current VM UUID.
115 + .help = "show backup status",
116 + .cmd = hmp_info_backup,
121 + Show backup status.
124 #if defined(CONFIG_SLIRP)
127 diff --git a/hmp-commands.hx b/hmp-commands.hx
128 index 039be0033d..fcf9461295 100644
129 --- a/hmp-commands.hx
130 +++ b/hmp-commands.hx
131 @@ -101,6 +101,35 @@ ERST
134 Copy data from a backing file into a block device.
139 + .args_type = "directory:-d,backupfile:s,speed:o?,devlist:s?",
140 + .params = "[-d] backupfile [speed [devlist]]",
141 + .help = "create a VM Backup."
142 + "\n\t\t\t Use -d to dump data into a directory instead"
143 + "\n\t\t\t of using VMA format.",
149 + Create a VM backup.
153 + .name = "backup_cancel",
156 + .help = "cancel the current VM backup",
157 + .cmd = hmp_backup_cancel,
162 + Cancel the current VM backup.
167 diff --git a/include/monitor/hmp.h b/include/monitor/hmp.h
168 index 440f86aba8..350527e599 100644
169 --- a/include/monitor/hmp.h
170 +++ b/include/monitor/hmp.h
171 @@ -31,6 +31,7 @@ void hmp_info_savevm(Monitor *mon, const QDict *qdict);
172 void hmp_info_migrate(Monitor *mon, const QDict *qdict);
173 void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict);
174 void hmp_info_migrate_parameters(Monitor *mon, const QDict *qdict);
175 +void hmp_info_backup(Monitor *mon, const QDict *qdict);
176 void hmp_info_cpus(Monitor *mon, const QDict *qdict);
177 void hmp_info_vnc(Monitor *mon, const QDict *qdict);
178 void hmp_info_spice(Monitor *mon, const QDict *qdict);
179 @@ -74,6 +75,8 @@ void hmp_x_colo_lost_heartbeat(Monitor *mon, const QDict *qdict);
180 void hmp_set_password(Monitor *mon, const QDict *qdict);
181 void hmp_expire_password(Monitor *mon, const QDict *qdict);
182 void hmp_change(Monitor *mon, const QDict *qdict);
183 +void hmp_backup(Monitor *mon, const QDict *qdict);
184 +void hmp_backup_cancel(Monitor *mon, const QDict *qdict);
185 void hmp_migrate(Monitor *mon, const QDict *qdict);
186 void hmp_device_add(Monitor *mon, const QDict *qdict);
187 void hmp_device_del(Monitor *mon, const QDict *qdict);
188 diff --git a/meson.build b/meson.build
189 index e8cf7e3d78..782756162c 100644
192 @@ -1526,6 +1526,7 @@ keyutils = dependency('libkeyutils', required: false,
193 has_gettid = cc.has_function('gettid')
195 libuuid = cc.find_library('uuid', required: true)
196 +libproxmox_backup_qemu = cc.find_library('proxmox_backup_qemu', required: true)
199 selinux = dependency('libselinux',
200 diff --git a/monitor/hmp-cmds.c b/monitor/hmp-cmds.c
201 index cfebfd1db5..a40b25e906 100644
202 --- a/monitor/hmp-cmds.c
203 +++ b/monitor/hmp-cmds.c
204 @@ -199,6 +199,50 @@ void hmp_info_mice(Monitor *mon, const QDict *qdict)
205 qapi_free_MouseInfoList(mice_list);
208 +void hmp_info_backup(Monitor *mon, const QDict *qdict)
210 + BackupStatus *info;
212 + info = qmp_query_backup(NULL);
215 + monitor_printf(mon, "Backup status: not initialized\n");
219 + if (info->has_status) {
220 + if (info->has_errmsg) {
221 + monitor_printf(mon, "Backup status: %s - %s\n",
222 + info->status, info->errmsg);
224 + monitor_printf(mon, "Backup status: %s\n", info->status);
228 + if (info->has_backup_file) {
229 + monitor_printf(mon, "Start time: %s", ctime(&info->start_time));
230 + if (info->end_time) {
231 + monitor_printf(mon, "End time: %s", ctime(&info->end_time));
234 + int per = (info->has_total && info->total &&
235 + info->has_transferred && info->transferred) ?
236 + (info->transferred * 100)/info->total : 0;
237 + int zero_per = (info->has_total && info->total &&
238 + info->has_zero_bytes && info->zero_bytes) ?
239 + (info->zero_bytes * 100)/info->total : 0;
240 + monitor_printf(mon, "Backup file: %s\n", info->backup_file);
241 + monitor_printf(mon, "Backup uuid: %s\n", info->uuid);
242 + monitor_printf(mon, "Total size: %zd\n", info->total);
243 + monitor_printf(mon, "Transferred bytes: %zd (%d%%)\n",
244 + info->transferred, per);
245 + monitor_printf(mon, "Zero bytes: %zd (%d%%)\n",
246 + info->zero_bytes, zero_per);
249 + qapi_free_BackupStatus(info);
252 void hmp_info_migrate(Monitor *mon, const QDict *qdict)
255 diff --git a/proxmox-backup-client.c b/proxmox-backup-client.c
257 index 0000000000..a8f6653a81
259 +++ b/proxmox-backup-client.c
261 +#include "proxmox-backup-client.h"
262 +#include "qemu/main-loop.h"
263 +#include "block/aio-wait.h"
264 +#include "qapi/error.h"
266 +/* Proxmox Backup Server client bindings using coroutines */
268 +typedef struct BlockOnCoroutineWrapper {
270 + CoroutineEntry *entry;
273 +} BlockOnCoroutineWrapper;
275 +static void coroutine_fn block_on_coroutine_wrapper(void *opaque)
277 + BlockOnCoroutineWrapper *wrapper = opaque;
278 + wrapper->entry(wrapper->entry_arg);
279 + wrapper->finished = true;
283 +void block_on_coroutine_fn(CoroutineEntry *entry, void *entry_arg)
285 + assert(!qemu_in_coroutine());
287 + AioContext *ctx = qemu_get_current_aio_context();
288 + BlockOnCoroutineWrapper wrapper = {
291 + .entry_arg = entry_arg,
294 + Coroutine *wrapper_co = qemu_coroutine_create(block_on_coroutine_wrapper, &wrapper);
295 + aio_co_enter(ctx, wrapper_co);
296 + AIO_WAIT_WHILE(ctx, !wrapper.finished);
299 +// This is called from another thread, so we use aio_co_schedule()
300 +static void proxmox_backup_schedule_wake(void *data) {
301 + CoCtxData *waker = (CoCtxData *)data;
302 + aio_co_schedule(waker->ctx, waker->co);
306 +proxmox_backup_co_connect(ProxmoxBackupHandle *pbs, Error **errp)
308 + Coroutine *co = qemu_coroutine_self();
309 + AioContext *ctx = qemu_get_current_aio_context();
310 + CoCtxData waker = { .co = co, .ctx = ctx };
311 + char *pbs_err = NULL;
314 + proxmox_backup_connect_async(pbs, proxmox_backup_schedule_wake, &waker, &pbs_res, &pbs_err);
315 + qemu_coroutine_yield();
317 + if (errp) error_setg(errp, "backup connect failed: %s", pbs_err ? pbs_err : "unknown error");
318 + if (pbs_err) proxmox_backup_free_error(pbs_err);
324 +proxmox_backup_co_add_config(
325 + ProxmoxBackupHandle *pbs,
327 + const uint8_t *data,
331 + Coroutine *co = qemu_coroutine_self();
332 + AioContext *ctx = qemu_get_current_aio_context();
333 + CoCtxData waker = { .co = co, .ctx = ctx };
334 + char *pbs_err = NULL;
337 + proxmox_backup_add_config_async(
338 + pbs, name, data, size ,proxmox_backup_schedule_wake, &waker, &pbs_res, &pbs_err);
339 + qemu_coroutine_yield();
341 + if (errp) error_setg(errp, "backup add_config %s failed: %s", name, pbs_err ? pbs_err : "unknown error");
342 + if (pbs_err) proxmox_backup_free_error(pbs_err);
348 +proxmox_backup_co_register_image(
349 + ProxmoxBackupHandle *pbs,
350 + const char *device_name,
354 + Coroutine *co = qemu_coroutine_self();
355 + AioContext *ctx = qemu_get_current_aio_context();
356 + CoCtxData waker = { .co = co, .ctx = ctx };
357 + char *pbs_err = NULL;
360 + proxmox_backup_register_image_async(
361 + pbs, device_name, size ,proxmox_backup_schedule_wake, &waker, &pbs_res, &pbs_err);
362 + qemu_coroutine_yield();
364 + if (errp) error_setg(errp, "backup register image failed: %s", pbs_err ? pbs_err : "unknown error");
365 + if (pbs_err) proxmox_backup_free_error(pbs_err);
371 +proxmox_backup_co_finish(
372 + ProxmoxBackupHandle *pbs,
375 + Coroutine *co = qemu_coroutine_self();
376 + AioContext *ctx = qemu_get_current_aio_context();
377 + CoCtxData waker = { .co = co, .ctx = ctx };
378 + char *pbs_err = NULL;
381 + proxmox_backup_finish_async(
382 + pbs, proxmox_backup_schedule_wake, &waker, &pbs_res, &pbs_err);
383 + qemu_coroutine_yield();
385 + if (errp) error_setg(errp, "backup finish failed: %s", pbs_err ? pbs_err : "unknown error");
386 + if (pbs_err) proxmox_backup_free_error(pbs_err);
392 +proxmox_backup_co_close_image(
393 + ProxmoxBackupHandle *pbs,
397 + Coroutine *co = qemu_coroutine_self();
398 + AioContext *ctx = qemu_get_current_aio_context();
399 + CoCtxData waker = { .co = co, .ctx = ctx };
400 + char *pbs_err = NULL;
403 + proxmox_backup_close_image_async(
404 + pbs, dev_id, proxmox_backup_schedule_wake, &waker, &pbs_res, &pbs_err);
405 + qemu_coroutine_yield();
407 + if (errp) error_setg(errp, "backup close image failed: %s", pbs_err ? pbs_err : "unknown error");
408 + if (pbs_err) proxmox_backup_free_error(pbs_err);
414 +proxmox_backup_co_write_data(
415 + ProxmoxBackupHandle *pbs,
417 + const uint8_t *data,
422 + Coroutine *co = qemu_coroutine_self();
423 + AioContext *ctx = qemu_get_current_aio_context();
424 + CoCtxData waker = { .co = co, .ctx = ctx };
425 + char *pbs_err = NULL;
428 + proxmox_backup_write_data_async(
429 + pbs, dev_id, data, offset, size, proxmox_backup_schedule_wake, &waker, &pbs_res, &pbs_err);
430 + qemu_coroutine_yield();
432 + if (errp) error_setg(errp, "backup write data failed: %s", pbs_err ? pbs_err : "unknown error");
433 + if (pbs_err) proxmox_backup_free_error(pbs_err);
437 diff --git a/proxmox-backup-client.h b/proxmox-backup-client.h
439 index 0000000000..1dda8b7d8f
441 +++ b/proxmox-backup-client.h
443 +#ifndef PROXMOX_BACKUP_CLIENT_H
444 +#define PROXMOX_BACKUP_CLIENT_H
446 +#include "qemu/osdep.h"
447 +#include "qemu/coroutine.h"
448 +#include "proxmox-backup-qemu.h"
450 +typedef struct CoCtxData {
456 +// FIXME: Remove once coroutines are supported for QMP
457 +void block_on_coroutine_fn(CoroutineEntry *entry, void *entry_arg);
460 +proxmox_backup_co_connect(
461 + ProxmoxBackupHandle *pbs,
465 +proxmox_backup_co_add_config(
466 + ProxmoxBackupHandle *pbs,
468 + const uint8_t *data,
473 +proxmox_backup_co_register_image(
474 + ProxmoxBackupHandle *pbs,
475 + const char *device_name,
481 +proxmox_backup_co_finish(
482 + ProxmoxBackupHandle *pbs,
486 +proxmox_backup_co_close_image(
487 + ProxmoxBackupHandle *pbs,
492 +proxmox_backup_co_write_data(
493 + ProxmoxBackupHandle *pbs,
495 + const uint8_t *data,
501 +#endif /* PROXMOX_BACKUP_CLIENT_H */
502 diff --git a/pve-backup.c b/pve-backup.c
504 index 0000000000..3d28975eaa
508 +#include "proxmox-backup-client.h"
511 +#include "qemu/osdep.h"
512 +#include "qemu/module.h"
513 +#include "sysemu/block-backend.h"
514 +#include "sysemu/blockdev.h"
515 +#include "block/blockjob.h"
516 +#include "qapi/qapi-commands-block.h"
517 +#include "qapi/qmp/qerror.h"
519 +/* PVE backup state and related function */
522 + * Note: A resume from a qemu_coroutine_yield can happen in a different thread,
523 + * so you may not use normal mutexes within coroutines:
525 + * ---bad-example---
526 + * qemu_rec_mutex_lock(lock)
528 + * qemu_coroutine_yield() // wait for something
529 + * // we are now inside a different thread
530 + * qemu_rec_mutex_unlock(lock) // Crash - wrong thread!!
531 + * ---end-bad-example--
533 + * ==> Always use CoMutext inside coroutines.
534 + * ==> Never acquire/release AioContext withing coroutines (because that use QemuRecMutex)
538 +static struct PVEBackupState {
540 + // Everithing accessed from qmp_backup_query command is protected using lock
549 + size_t transferred;
554 + ProxmoxBackupHandle *pbs;
556 + QemuMutex backup_mutex;
557 + CoMutex dump_callback_mutex;
560 +static void pvebackup_init(void)
562 + qemu_mutex_init(&backup_state.stat.lock);
563 + qemu_mutex_init(&backup_state.backup_mutex);
564 + qemu_co_mutex_init(&backup_state.dump_callback_mutex);
567 +// initialize PVEBackupState at startup
568 +opts_init(pvebackup_init);
570 +typedef struct PVEBackupDevInfo {
571 + BlockDriverState *bs;
575 + char targetfile[PATH_MAX];
576 + BlockDriverState *target;
579 +static void pvebackup_run_next_job(void);
582 +lookup_active_block_job(PVEBackupDevInfo *di)
584 + if (!di->completed && di->bs) {
585 + WITH_JOB_LOCK_GUARD() {
586 + for (BlockJob *job = block_job_next_locked(NULL); job; job = block_job_next_locked(job)) {
587 + if (job->job.driver->job_type != JOB_TYPE_BACKUP) {
591 + BackupBlockJob *bjob = container_of(job, BackupBlockJob, common);
592 + if (bjob && bjob->source_bs == di->bs) {
601 +static void pvebackup_propagate_error(Error *err)
603 + qemu_mutex_lock(&backup_state.stat.lock);
604 + error_propagate(&backup_state.stat.error, err);
605 + qemu_mutex_unlock(&backup_state.stat.lock);
608 +static bool pvebackup_error_or_canceled(void)
610 + qemu_mutex_lock(&backup_state.stat.lock);
611 + bool error_or_canceled = !!backup_state.stat.error;
612 + qemu_mutex_unlock(&backup_state.stat.lock);
614 + return error_or_canceled;
617 +static void pvebackup_add_transfered_bytes(size_t transferred, size_t zero_bytes)
619 + qemu_mutex_lock(&backup_state.stat.lock);
620 + backup_state.stat.zero_bytes += zero_bytes;
621 + backup_state.stat.transferred += transferred;
622 + qemu_mutex_unlock(&backup_state.stat.lock);
625 +// This may get called from multiple coroutines in multiple io-threads
626 +// Note1: this may get called after job_cancel()
627 +static int coroutine_fn
628 +pvebackup_co_dump_pbs_cb(
634 + assert(qemu_in_coroutine());
636 + const uint64_t size = bytes;
637 + const unsigned char *buf = pbuf;
638 + PVEBackupDevInfo *di = opaque;
640 + assert(backup_state.pbs);
642 + Error *local_err = NULL;
645 + qemu_co_mutex_lock(&backup_state.dump_callback_mutex);
647 + // avoid deadlock if job is cancelled
648 + if (pvebackup_error_or_canceled()) {
649 + qemu_co_mutex_unlock(&backup_state.dump_callback_mutex);
653 + pbs_res = proxmox_backup_co_write_data(backup_state.pbs, di->dev_id, buf, start, size, &local_err);
654 + qemu_co_mutex_unlock(&backup_state.dump_callback_mutex);
657 + pvebackup_propagate_error(local_err);
660 + pvebackup_add_transfered_bytes(size, !buf ? size : 0);
666 +// This may get called from multiple coroutines in multiple io-threads
667 +static int coroutine_fn
668 +pvebackup_co_dump_vma_cb(
674 + assert(qemu_in_coroutine());
676 + const uint64_t size = bytes;
677 + const unsigned char *buf = pbuf;
678 + PVEBackupDevInfo *di = opaque;
682 + assert(backup_state.vmaw);
684 + uint64_t remaining = size;
686 + uint64_t cluster_num = start / VMA_CLUSTER_SIZE;
687 + if ((cluster_num * VMA_CLUSTER_SIZE) != start) {
688 + Error *local_err = NULL;
689 + error_setg(&local_err,
690 + "got unaligned write inside backup dump "
691 + "callback (sector %ld)", start);
692 + pvebackup_propagate_error(local_err);
693 + return -1; // not aligned to cluster size
696 + while (remaining > 0) {
697 + qemu_co_mutex_lock(&backup_state.dump_callback_mutex);
698 + // avoid deadlock if job is cancelled
699 + if (pvebackup_error_or_canceled()) {
700 + qemu_co_mutex_unlock(&backup_state.dump_callback_mutex);
704 + size_t zero_bytes = 0;
705 + ret = vma_writer_write(backup_state.vmaw, di->dev_id, cluster_num, buf, &zero_bytes);
706 + qemu_co_mutex_unlock(&backup_state.dump_callback_mutex);
710 + buf += VMA_CLUSTER_SIZE;
713 + Error *local_err = NULL;
714 + vma_writer_error_propagate(backup_state.vmaw, &local_err);
715 + pvebackup_propagate_error(local_err);
718 + if (remaining >= VMA_CLUSTER_SIZE) {
719 + assert(ret == VMA_CLUSTER_SIZE);
720 + pvebackup_add_transfered_bytes(VMA_CLUSTER_SIZE, zero_bytes);
721 + remaining -= VMA_CLUSTER_SIZE;
723 + assert(ret == remaining);
724 + pvebackup_add_transfered_bytes(remaining, zero_bytes);
733 +// assumes the caller holds backup_mutex
734 +static void coroutine_fn pvebackup_co_cleanup(void *unused)
736 + assert(qemu_in_coroutine());
738 + qemu_mutex_lock(&backup_state.stat.lock);
739 + backup_state.stat.end_time = time(NULL);
740 + qemu_mutex_unlock(&backup_state.stat.lock);
742 + if (backup_state.vmaw) {
743 + Error *local_err = NULL;
744 + vma_writer_close(backup_state.vmaw, &local_err);
746 + if (local_err != NULL) {
747 + pvebackup_propagate_error(local_err);
750 + backup_state.vmaw = NULL;
753 + if (backup_state.pbs) {
754 + if (!pvebackup_error_or_canceled()) {
755 + Error *local_err = NULL;
756 + proxmox_backup_co_finish(backup_state.pbs, &local_err);
757 + if (local_err != NULL) {
758 + pvebackup_propagate_error(local_err);
762 + proxmox_backup_disconnect(backup_state.pbs);
763 + backup_state.pbs = NULL;
766 + g_list_free(backup_state.di_list);
767 + backup_state.di_list = NULL;
770 +// assumes the caller holds backup_mutex
771 +static void coroutine_fn pvebackup_complete_stream(void *opaque)
773 + PVEBackupDevInfo *di = opaque;
775 + bool error_or_canceled = pvebackup_error_or_canceled();
777 + if (backup_state.vmaw) {
778 + vma_writer_close_stream(backup_state.vmaw, di->dev_id);
781 + if (backup_state.pbs && !error_or_canceled) {
782 + Error *local_err = NULL;
783 + proxmox_backup_co_close_image(backup_state.pbs, di->dev_id, &local_err);
784 + if (local_err != NULL) {
785 + pvebackup_propagate_error(local_err);
790 +static void pvebackup_complete_cb(void *opaque, int ret)
792 + assert(!qemu_in_coroutine());
794 + PVEBackupDevInfo *di = opaque;
796 + qemu_mutex_lock(&backup_state.backup_mutex);
798 + di->completed = true;
801 + Error *local_err = NULL;
802 + error_setg(&local_err, "job failed with err %d - %s", ret, strerror(-ret));
803 + pvebackup_propagate_error(local_err);
808 + assert(di->target == NULL);
810 + block_on_coroutine_fn(pvebackup_complete_stream, di);
812 + // remove self from job queue
813 + backup_state.di_list = g_list_remove(backup_state.di_list, di);
817 + qemu_mutex_unlock(&backup_state.backup_mutex);
819 + pvebackup_run_next_job();
822 +static void pvebackup_cancel(void)
824 + assert(!qemu_in_coroutine());
826 + Error *cancel_err = NULL;
827 + error_setg(&cancel_err, "backup canceled");
828 + pvebackup_propagate_error(cancel_err);
830 + qemu_mutex_lock(&backup_state.backup_mutex);
832 + if (backup_state.vmaw) {
833 + /* make sure vma writer does not block anymore */
834 + vma_writer_set_error(backup_state.vmaw, "backup canceled");
837 + if (backup_state.pbs) {
838 + proxmox_backup_abort(backup_state.pbs, "backup canceled");
841 + qemu_mutex_unlock(&backup_state.backup_mutex);
845 + BlockJob *next_job = NULL;
847 + qemu_mutex_lock(&backup_state.backup_mutex);
849 + GList *l = backup_state.di_list;
851 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
852 + l = g_list_next(l);
854 + BlockJob *job = lookup_active_block_job(di);
861 + qemu_mutex_unlock(&backup_state.backup_mutex);
864 + job_cancel_sync(&next_job->job, true);
871 +void qmp_backup_cancel(Error **errp)
873 + pvebackup_cancel();
876 +// assumes the caller holds backup_mutex
877 +static int coroutine_fn pvebackup_co_add_config(
880 + BackupFormat format,
881 + const char *backup_dir,
883 + ProxmoxBackupHandle *pbs,
888 + char *cdata = NULL;
890 + GError *err = NULL;
891 + if (!g_file_get_contents(file, &cdata, &clen, &err)) {
892 + error_setg(errp, "unable to read file '%s'", file);
896 + char *basename = g_path_get_basename(file);
897 + if (name == NULL) name = basename;
899 + if (format == BACKUP_FORMAT_VMA) {
900 + if (vma_writer_add_config(vmaw, name, cdata, clen) != 0) {
901 + error_setg(errp, "unable to add %s config data to vma archive", file);
904 + } else if (format == BACKUP_FORMAT_PBS) {
905 + if (proxmox_backup_co_add_config(pbs, name, (unsigned char *)cdata, clen, errp) < 0)
907 + } else if (format == BACKUP_FORMAT_DIR) {
908 + char config_path[PATH_MAX];
909 + snprintf(config_path, PATH_MAX, "%s/%s", backup_dir, name);
910 + if (!g_file_set_contents(config_path, cdata, clen, &err)) {
911 + error_setg(errp, "unable to write config file '%s'", config_path);
926 +bool job_should_pause_locked(Job *job);
928 +static void pvebackup_run_next_job(void)
930 + assert(!qemu_in_coroutine());
932 + qemu_mutex_lock(&backup_state.backup_mutex);
934 + GList *l = backup_state.di_list;
936 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
937 + l = g_list_next(l);
939 + BlockJob *job = lookup_active_block_job(di);
942 + qemu_mutex_unlock(&backup_state.backup_mutex);
944 + WITH_JOB_LOCK_GUARD() {
945 + if (job_should_pause_locked(&job->job)) {
946 + bool error_or_canceled = pvebackup_error_or_canceled();
947 + if (error_or_canceled) {
948 + job_cancel_sync_locked(&job->job, true);
950 + job_resume_locked(&job->job);
958 + block_on_coroutine_fn(pvebackup_co_cleanup, NULL); // no more jobs, run cleanup
960 + qemu_mutex_unlock(&backup_state.backup_mutex);
963 +static bool create_backup_jobs(void) {
965 + assert(!qemu_in_coroutine());
967 + Error *local_err = NULL;
969 + BackupPerf perf = { .max_workers = 16 };
971 + /* create and start all jobs (paused state) */
972 + GList *l = backup_state.di_list;
974 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
975 + l = g_list_next(l);
977 + assert(di->target != NULL);
979 + AioContext *aio_context = bdrv_get_aio_context(di->bs);
980 + aio_context_acquire(aio_context);
982 + BlockJob *job = backup_job_create(
983 + NULL, di->bs, di->target, backup_state.speed, MIRROR_SYNC_MODE_FULL, NULL,
984 + BITMAP_SYNC_MODE_NEVER, false, NULL, &perf, BLOCKDEV_ON_ERROR_REPORT, BLOCKDEV_ON_ERROR_REPORT,
985 + JOB_DEFAULT, pvebackup_complete_cb, di, NULL, &local_err);
987 + aio_context_release(aio_context);
989 + if (!job || local_err != NULL) {
990 + Error *create_job_err = NULL;
991 + error_setg(&create_job_err, "backup_job_create failed: %s",
992 + local_err ? error_get_pretty(local_err) : "null");
994 + pvebackup_propagate_error(create_job_err);
997 + job_start(&job->job);
999 + bdrv_unref(di->target);
1000 + di->target = NULL;
1003 + bool errors = pvebackup_error_or_canceled();
1006 + l = backup_state.di_list;
1008 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
1009 + l = g_list_next(l);
1012 + bdrv_unref(di->target);
1013 + di->target = NULL;
1021 +typedef struct QmpBackupTask {
1022 + const char *backup_file;
1023 + bool has_password;
1024 + const char *password;
1026 + const char *keyfile;
1027 + bool has_key_password;
1028 + const char *key_password;
1029 + bool has_backup_id;
1030 + const char *backup_id;
1031 + bool has_backup_time;
1032 + const char *fingerprint;
1033 + bool has_fingerprint;
1034 + int64_t backup_time;
1036 + BackupFormat format;
1037 + bool has_config_file;
1038 + const char *config_file;
1039 + bool has_firewall_file;
1040 + const char *firewall_file;
1042 + const char *devlist;
1049 +// assumes the caller holds backup_mutex
1050 +static void coroutine_fn pvebackup_co_prepare(void *opaque)
1052 + assert(qemu_in_coroutine());
1054 + QmpBackupTask *task = opaque;
1056 + task->result = NULL; // just to be sure
1058 + BlockBackend *blk;
1059 + BlockDriverState *bs = NULL;
1060 + const char *backup_dir = NULL;
1061 + Error *local_err = NULL;
1063 + VmaWriter *vmaw = NULL;
1064 + ProxmoxBackupHandle *pbs = NULL;
1065 + gchar **devs = NULL;
1066 + GList *di_list = NULL;
1068 + UuidInfo *uuid_info;
1070 + const char *config_name = "qemu-server.conf";
1071 + const char *firewall_name = "qemu-server.fw";
1073 + if (backup_state.di_list) {
1074 + error_set(task->errp, ERROR_CLASS_GENERIC_ERROR,
1075 + "previous backup not finished");
1079 + /* Todo: try to auto-detect format based on file name */
1080 + BackupFormat format = task->has_format ? task->format : BACKUP_FORMAT_VMA;
1082 + if (task->has_devlist) {
1083 + devs = g_strsplit_set(task->devlist, ",;:", -1);
1087 + blk = blk_by_name(*d);
1090 + if (!bdrv_is_inserted(bs)) {
1091 + error_setg(task->errp, QERR_DEVICE_HAS_NO_MEDIUM, *d);
1094 + PVEBackupDevInfo *di = g_new0(PVEBackupDevInfo, 1);
1096 + di_list = g_list_append(di_list, di);
1098 + error_set(task->errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1099 + "Device '%s' not found", *d);
1106 + BdrvNextIterator it;
1109 + for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
1110 + if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
1114 + PVEBackupDevInfo *di = g_new0(PVEBackupDevInfo, 1);
1116 + di_list = g_list_append(di_list, di);
1121 + error_set(task->errp, ERROR_CLASS_GENERIC_ERROR, "empty device list");
1129 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
1130 + l = g_list_next(l);
1131 + if (bdrv_op_is_blocked(di->bs, BLOCK_OP_TYPE_BACKUP_SOURCE, task->errp)) {
1135 + ssize_t size = bdrv_getlength(di->bs);
1137 + error_setg_errno(task->errp, -di->size, "bdrv_getlength failed");
1144 + uuid_generate(uuid);
1146 + if (format == BACKUP_FORMAT_PBS) {
1147 + if (!task->has_password) {
1148 + error_set(task->errp, ERROR_CLASS_GENERIC_ERROR, "missing parameter 'password'");
1151 + if (!task->has_backup_id) {
1152 + error_set(task->errp, ERROR_CLASS_GENERIC_ERROR, "missing parameter 'backup-id'");
1155 + if (!task->has_backup_time) {
1156 + error_set(task->errp, ERROR_CLASS_GENERIC_ERROR, "missing parameter 'backup-time'");
1160 + int dump_cb_block_size = PROXMOX_BACKUP_DEFAULT_CHUNK_SIZE; // Hardcoded (4M)
1161 + firewall_name = "fw.conf";
1163 + char *pbs_err = NULL;
1164 + pbs = proxmox_backup_new(
1165 + task->backup_file,
1167 + task->backup_time,
1168 + dump_cb_block_size,
1169 + task->has_password ? task->password : NULL,
1170 + task->has_keyfile ? task->keyfile : NULL,
1171 + task->has_key_password ? task->key_password : NULL,
1172 + task->has_fingerprint ? task->fingerprint : NULL,
1176 + error_set(task->errp, ERROR_CLASS_GENERIC_ERROR,
1177 + "proxmox_backup_new failed: %s", pbs_err);
1178 + proxmox_backup_free_error(pbs_err);
1182 + if (proxmox_backup_co_connect(pbs, task->errp) < 0)
1185 + /* register all devices */
1188 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
1189 + l = g_list_next(l);
1191 + const char *devname = bdrv_get_device_name(di->bs);
1193 + int dev_id = proxmox_backup_co_register_image(pbs, devname, di->size, task->errp);
1197 + if (!(di->target = bdrv_backup_dump_create(dump_cb_block_size, di->size, pvebackup_co_dump_pbs_cb, di, task->errp))) {
1201 + di->dev_id = dev_id;
1203 + } else if (format == BACKUP_FORMAT_VMA) {
1204 + vmaw = vma_writer_create(task->backup_file, uuid, &local_err);
1207 + error_propagate(task->errp, local_err);
1212 + /* register all devices for vma writer */
1215 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
1216 + l = g_list_next(l);
1218 + if (!(di->target = bdrv_backup_dump_create(VMA_CLUSTER_SIZE, di->size, pvebackup_co_dump_vma_cb, di, task->errp))) {
1222 + const char *devname = bdrv_get_device_name(di->bs);
1223 + di->dev_id = vma_writer_register_stream(vmaw, devname, di->size);
1224 + if (di->dev_id <= 0) {
1225 + error_set(task->errp, ERROR_CLASS_GENERIC_ERROR,
1226 + "register_stream failed");
1230 + } else if (format == BACKUP_FORMAT_DIR) {
1231 + if (mkdir(task->backup_file, 0640) != 0) {
1232 + error_setg_errno(task->errp, errno, "can't create directory '%s'\n",
1233 + task->backup_file);
1236 + backup_dir = task->backup_file;
1240 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
1241 + l = g_list_next(l);
1243 + const char *devname = bdrv_get_device_name(di->bs);
1244 + snprintf(di->targetfile, PATH_MAX, "%s/%s.raw", backup_dir, devname);
1246 + int flags = BDRV_O_RDWR;
1247 + bdrv_img_create(di->targetfile, "raw", NULL, NULL, NULL,
1248 + di->size, flags, false, &local_err);
1250 + error_propagate(task->errp, local_err);
1254 + di->target = bdrv_open(di->targetfile, NULL, NULL, flags, &local_err);
1255 + if (!di->target) {
1256 + error_propagate(task->errp, local_err);
1261 + error_set(task->errp, ERROR_CLASS_GENERIC_ERROR, "unknown backup format");
1266 + /* add configuration file to archive */
1267 + if (task->has_config_file) {
1268 + if (pvebackup_co_add_config(task->config_file, config_name, format, backup_dir,
1269 + vmaw, pbs, task->errp) != 0) {
1274 + /* add firewall file to archive */
1275 + if (task->has_firewall_file) {
1276 + if (pvebackup_co_add_config(task->firewall_file, firewall_name, format, backup_dir,
1277 + vmaw, pbs, task->errp) != 0) {
1281 + /* initialize global backup_state now */
1283 + qemu_mutex_lock(&backup_state.stat.lock);
1285 + if (backup_state.stat.error) {
1286 + error_free(backup_state.stat.error);
1287 + backup_state.stat.error = NULL;
1290 + backup_state.stat.start_time = time(NULL);
1291 + backup_state.stat.end_time = 0;
1293 + if (backup_state.stat.backup_file) {
1294 + g_free(backup_state.stat.backup_file);
1296 + backup_state.stat.backup_file = g_strdup(task->backup_file);
1298 + uuid_copy(backup_state.stat.uuid, uuid);
1299 + uuid_unparse_lower(uuid, backup_state.stat.uuid_str);
1300 + char *uuid_str = g_strdup(backup_state.stat.uuid_str);
1302 + backup_state.stat.total = total;
1303 + backup_state.stat.transferred = 0;
1304 + backup_state.stat.zero_bytes = 0;
1306 + qemu_mutex_unlock(&backup_state.stat.lock);
1308 + backup_state.speed = (task->has_speed && task->speed > 0) ? task->speed : 0;
1310 + backup_state.vmaw = vmaw;
1311 + backup_state.pbs = pbs;
1313 + backup_state.di_list = di_list;
1315 + uuid_info = g_malloc0(sizeof(*uuid_info));
1316 + uuid_info->UUID = uuid_str;
1318 + task->result = uuid_info;
1325 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
1326 + l = g_list_next(l);
1329 + bdrv_unref(di->target);
1332 + if (di->targetfile[0]) {
1333 + unlink(di->targetfile);
1337 + g_list_free(di_list);
1344 + Error *err = NULL;
1345 + vma_writer_close(vmaw, &err);
1346 + unlink(task->backup_file);
1350 + proxmox_backup_disconnect(pbs);
1354 + rmdir(backup_dir);
1357 + task->result = NULL;
1361 +UuidInfo *qmp_backup(
1362 + const char *backup_file,
1363 + bool has_password, const char *password,
1364 + bool has_keyfile, const char *keyfile,
1365 + bool has_key_password, const char *key_password,
1366 + bool has_fingerprint, const char *fingerprint,
1367 + bool has_backup_id, const char *backup_id,
1368 + bool has_backup_time, int64_t backup_time,
1369 + bool has_format, BackupFormat format,
1370 + bool has_config_file, const char *config_file,
1371 + bool has_firewall_file, const char *firewall_file,
1372 + bool has_devlist, const char *devlist,
1373 + bool has_speed, int64_t speed, Error **errp)
1375 + QmpBackupTask task = {
1376 + .backup_file = backup_file,
1377 + .has_password = has_password,
1378 + .password = password,
1379 + .has_key_password = has_key_password,
1380 + .key_password = key_password,
1381 + .has_fingerprint = has_fingerprint,
1382 + .fingerprint = fingerprint,
1383 + .has_backup_id = has_backup_id,
1384 + .backup_id = backup_id,
1385 + .has_backup_time = has_backup_time,
1386 + .backup_time = backup_time,
1387 + .has_format = has_format,
1389 + .has_config_file = has_config_file,
1390 + .config_file = config_file,
1391 + .has_firewall_file = has_firewall_file,
1392 + .firewall_file = firewall_file,
1393 + .has_devlist = has_devlist,
1394 + .devlist = devlist,
1395 + .has_speed = has_speed,
1400 + qemu_mutex_lock(&backup_state.backup_mutex);
1402 + block_on_coroutine_fn(pvebackup_co_prepare, &task);
1404 + if (*errp == NULL) {
1405 + create_backup_jobs();
1406 + qemu_mutex_unlock(&backup_state.backup_mutex);
1407 + pvebackup_run_next_job();
1409 + qemu_mutex_unlock(&backup_state.backup_mutex);
1412 + return task.result;
1415 +BackupStatus *qmp_query_backup(Error **errp)
1417 + BackupStatus *info = g_malloc0(sizeof(*info));
1419 + qemu_mutex_lock(&backup_state.stat.lock);
1421 + if (!backup_state.stat.start_time) {
1422 + /* not started, return {} */
1423 + qemu_mutex_unlock(&backup_state.stat.lock);
1427 + info->has_status = true;
1428 + info->has_start_time = true;
1429 + info->start_time = backup_state.stat.start_time;
1431 + if (backup_state.stat.backup_file) {
1432 + info->has_backup_file = true;
1433 + info->backup_file = g_strdup(backup_state.stat.backup_file);
1436 + info->has_uuid = true;
1437 + info->uuid = g_strdup(backup_state.stat.uuid_str);
1439 + if (backup_state.stat.end_time) {
1440 + if (backup_state.stat.error) {
1441 + info->status = g_strdup("error");
1442 + info->has_errmsg = true;
1443 + info->errmsg = g_strdup(error_get_pretty(backup_state.stat.error));
1445 + info->status = g_strdup("done");
1447 + info->has_end_time = true;
1448 + info->end_time = backup_state.stat.end_time;
1450 + info->status = g_strdup("active");
1453 + info->has_total = true;
1454 + info->total = backup_state.stat.total;
1455 + info->has_zero_bytes = true;
1456 + info->zero_bytes = backup_state.stat.zero_bytes;
1457 + info->has_transferred = true;
1458 + info->transferred = backup_state.stat.transferred;
1460 + qemu_mutex_unlock(&backup_state.stat.lock);
1464 diff --git a/qapi/block-core.json b/qapi/block-core.json
1465 index 9e902b96bb..c3b6b93472 100644
1466 --- a/qapi/block-core.json
1467 +++ b/qapi/block-core.json
1468 @@ -740,6 +740,115 @@
1469 { 'command': 'query-block', 'returns': ['BlockInfo'],
1470 'allow-preconfig': true }
1475 +# Detailed backup status.
1477 +# @status: string describing the current backup status.
1478 +# This can be 'active', 'done', 'error'. If this field is not
1479 +# returned, no backup process has been initiated
1481 +# @errmsg: error message (only returned if status is 'error')
1483 +# @total: total amount of bytes involved in the backup process
1485 +# @transferred: amount of bytes already backed up.
1487 +# @zero-bytes: amount of 'zero' bytes detected.
1489 +# @start-time: time (epoch) when backup job started.
1491 +# @end-time: time (epoch) when backup job finished.
1493 +# @backup-file: backup file name
1495 +# @uuid: uuid for this backup job
1498 +{ 'struct': 'BackupStatus',
1499 + 'data': {'*status': 'str', '*errmsg': 'str', '*total': 'int',
1500 + '*transferred': 'int', '*zero-bytes': 'int',
1501 + '*start-time': 'int', '*end-time': 'int',
1502 + '*backup-file': 'str', '*uuid': 'str' } }
1507 +# An enumeration of supported backup formats.
1509 +# @vma: Proxmox vma backup format
1511 +{ 'enum': 'BackupFormat',
1512 + 'data': [ 'vma', 'dir', 'pbs' ] }
1517 +# Starts a VM backup.
1519 +# @backup-file: the backup file name
1521 +# @format: format of the backup file
1523 +# @config-file: a configuration file to include into
1524 +# the backup archive.
1526 +# @speed: the maximum speed, in bytes per second
1528 +# @devlist: list of block device names (separated by ',', ';'
1529 +# or ':'). By default the backup includes all writable block devices.
1531 +# @password: backup server passsword (required for format 'pbs')
1533 +# @keyfile: keyfile used for encryption (optional for format 'pbs')
1535 +# @key-password: password for keyfile (optional for format 'pbs')
1537 +# @fingerprint: server cert fingerprint (optional for format 'pbs')
1539 +# @backup-id: backup ID (required for format 'pbs')
1541 +# @backup-time: backup timestamp (Unix epoch, required for format 'pbs')
1543 +# Returns: the uuid of the backup job
1546 +{ 'command': 'backup', 'data': { 'backup-file': 'str',
1547 + '*password': 'str',
1548 + '*keyfile': 'str',
1549 + '*key-password': 'str',
1550 + '*fingerprint': 'str',
1551 + '*backup-id': 'str',
1552 + '*backup-time': 'int',
1553 + '*format': 'BackupFormat',
1554 + '*config-file': 'str',
1555 + '*firewall-file': 'str',
1556 + '*devlist': 'str', '*speed': 'int' },
1557 + 'returns': 'UuidInfo' }
1562 +# Returns information about current/last backup task.
1564 +# Returns: @BackupStatus
1567 +{ 'command': 'query-backup', 'returns': 'BackupStatus' }
1572 +# Cancel the current executing backup process.
1574 +# Returns: nothing on success
1576 +# Notes: This command succeeds even if there is no backup process running.
1579 +{ 'command': 'backup-cancel' }
1582 # @BlockDeviceTimedStats:
1584 diff --git a/qapi/common.json b/qapi/common.json
1585 index 356db3f670..aae8a3b682 100644
1586 --- a/qapi/common.json
1587 +++ b/qapi/common.json
1588 @@ -206,3 +206,16 @@
1590 { 'struct': 'HumanReadableText',
1591 'data': { 'human-readable-text': 'str' } }
1596 +# Guest UUID information (Universally Unique Identifier).
1598 +# @UUID: the UUID of the guest
1602 +# Notes: If no UUID was specified for the guest, a null UUID is returned.
1604 +{ 'struct': 'UuidInfo', 'data': {'UUID': 'str'} }
1605 diff --git a/qapi/machine.json b/qapi/machine.json
1606 index f4fb1b2c9c..0d6ee836ed 100644
1607 --- a/qapi/machine.json
1608 +++ b/qapi/machine.json
1610 # This work is licensed under the terms of the GNU GPL, version 2 or later.
1611 # See the COPYING file in the top-level directory.
1613 +{ 'include': 'common.json' }
1618 @@ -226,19 +228,6 @@
1620 { 'command': 'query-target', 'returns': 'TargetInfo' }
1625 -# Guest UUID information (Universally Unique Identifier).
1627 -# @UUID: the UUID of the guest
1631 -# Notes: If no UUID was specified for the guest, a null UUID is returned.
1633 -{ 'struct': 'UuidInfo', 'data': {'UUID': 'str'} }