]> git.proxmox.com Git - pve-qemu.git/blame - debian/patches/pve/0027-PVE-Backup-proxmox-backup-patches-for-qemu.patch
bump version to 6.1.0-3
[pve-qemu.git] / debian / patches / pve / 0027-PVE-Backup-proxmox-backup-patches-for-qemu.patch
CommitLineData
6402d961
TL
1From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2From: Dietmar Maurer <dietmar@proxmox.com>
83faa3fe
TL
3Date: Mon, 6 Apr 2020 12:16:59 +0200
4Subject: [PATCH] PVE-Backup: proxmox backup patches for qemu
6402d961 5
0c893fd8
SR
6Signed-off-by: Dietmar Maurer <dietmar@proxmox.com>
7[PVE-Backup: avoid coroutines to fix AIO freeze, cleanups]
8Signed-off-by: Stefan Reiter <s.reiter@proxmox.com>
6402d961 9---
817b7667 10 block/meson.build | 5 +
83faa3fe
TL
11 block/monitor/block-hmp-cmds.c | 33 ++
12 blockdev.c | 1 +
f376b2b9 13 hmp-commands-info.hx | 14 +
83faa3fe
TL
14 hmp-commands.hx | 29 +
15 include/block/block_int.h | 2 +-
16 include/monitor/hmp.h | 3 +
817b7667 17 meson.build | 1 +
83faa3fe 18 monitor/hmp-cmds.c | 44 ++
72ae34ec
SR
19 proxmox-backup-client.c | 176 ++++++
20 proxmox-backup-client.h | 59 ++
8dca018b 21 pve-backup.c | 959 +++++++++++++++++++++++++++++++++
83faa3fe
TL
22 qapi/block-core.json | 109 ++++
23 qapi/common.json | 13 +
817b7667 24 qapi/machine.json | 15 +-
f376b2b9 25 15 files changed, 1449 insertions(+), 14 deletions(-)
6402d961
TL
26 create mode 100644 proxmox-backup-client.c
27 create mode 100644 proxmox-backup-client.h
28 create mode 100644 pve-backup.c
29
817b7667 30diff --git a/block/meson.build b/block/meson.build
f376b2b9 31index 19bc2b7cbb..9e433daf2e 100644
817b7667
SR
32--- a/block/meson.build
33+++ b/block/meson.build
f376b2b9 34@@ -46,6 +46,11 @@ block_ss.add(files(
8dca018b 35 ), zstd, zlib, gnutls)
83faa3fe 36
817b7667
SR
37 block_ss.add(files('../vma-writer.c'), libuuid)
38+block_ss.add(files(
39+ '../proxmox-backup-client.c',
40+ '../pve-backup.c',
41+), libproxmox_backup_qemu)
42+
83faa3fe 43
817b7667 44 softmmu_ss.add(when: 'CONFIG_TCG', if_true: files('blkreplay.c'))
6402d961 45
83faa3fe 46diff --git a/block/monitor/block-hmp-cmds.c b/block/monitor/block-hmp-cmds.c
f376b2b9 47index 3e6670c963..1e29681d30 100644
83faa3fe
TL
48--- a/block/monitor/block-hmp-cmds.c
49+++ b/block/monitor/block-hmp-cmds.c
f376b2b9 50@@ -1015,3 +1015,36 @@ void hmp_info_snapshots(Monitor *mon, const QDict *qdict)
83faa3fe
TL
51 g_free(sn_tab);
52 g_free(global_snapshots);
53 }
54+
55+void hmp_backup_cancel(Monitor *mon, const QDict *qdict)
56+{
57+ Error *error = NULL;
58+
59+ qmp_backup_cancel(&error);
60+
61+ hmp_handle_error(mon, error);
62+}
63+
64+void hmp_backup(Monitor *mon, const QDict *qdict)
65+{
66+ Error *error = NULL;
67+
68+ int dir = qdict_get_try_bool(qdict, "directory", 0);
69+ const char *backup_file = qdict_get_str(qdict, "backupfile");
70+ const char *devlist = qdict_get_try_str(qdict, "devlist");
71+ int64_t speed = qdict_get_try_int(qdict, "speed", 0);
72+
73+ qmp_backup(
74+ backup_file,
c96a4a38
DM
75+ false, NULL, // PBS password
76+ false, NULL, // PBS keyfile
77+ false, NULL, // PBS key_password
78+ false, NULL, // PBS fingerprint
79+ false, NULL, // PBS backup-id
80+ false, 0, // PBS backup-time
83faa3fe
TL
81+ true, dir ? BACKUP_FORMAT_DIR : BACKUP_FORMAT_VMA,
82+ false, NULL, false, NULL, !!devlist,
83+ devlist, qdict_haskey(qdict, "speed"), speed, &error);
84+
85+ hmp_handle_error(mon, error);
86+}
6402d961 87diff --git a/blockdev.c b/blockdev.c
f376b2b9 88index b6f797b41f..84e9b898be 100644
6402d961
TL
89--- a/blockdev.c
90+++ b/blockdev.c
91@@ -36,6 +36,7 @@
92 #include "hw/block/block.h"
93 #include "block/blockjob.h"
94 #include "block/qdict.h"
95+#include "block/blockjob_int.h"
96 #include "block/throttle-groups.h"
97 #include "monitor/monitor.h"
98 #include "qemu/error-report.h"
99diff --git a/hmp-commands-info.hx b/hmp-commands-info.hx
f376b2b9 100index e6dd3be07a..15ddecada1 100644
6402d961
TL
101--- a/hmp-commands-info.hx
102+++ b/hmp-commands-info.hx
f376b2b9
SR
103@@ -497,6 +497,20 @@ SRST
104 Show the current VM UUID.
83faa3fe
TL
105 ERST
106
f376b2b9 107+
6402d961
TL
108+ {
109+ .name = "backup",
110+ .args_type = "",
111+ .params = "",
112+ .help = "show backup status",
83faa3fe 113+ .cmd = hmp_info_backup,
6402d961
TL
114+ },
115+
83faa3fe
TL
116+SRST
117+ ``info backup``
118+ Show backup status.
119+ERST
120+
6402d961 121 #if defined(CONFIG_SLIRP)
83faa3fe
TL
122 {
123 .name = "usernet",
6402d961 124diff --git a/hmp-commands.hx b/hmp-commands.hx
f376b2b9 125index 96bd7e00bd..b18063ce19 100644
6402d961
TL
126--- a/hmp-commands.hx
127+++ b/hmp-commands.hx
8dca018b 128@@ -99,6 +99,35 @@ ERST
83faa3fe
TL
129 SRST
130 ``block_stream``
131 Copy data from a backing file into a block device.
132+ERST
6402d961
TL
133+
134+ {
135+ .name = "backup",
136+ .args_type = "directory:-d,backupfile:s,speed:o?,devlist:s?",
137+ .params = "[-d] backupfile [speed [devlist]]",
138+ .help = "create a VM Backup."
139+ "\n\t\t\t Use -d to dump data into a directory instead"
140+ "\n\t\t\t of using VMA format.",
141+ .cmd = hmp_backup,
142+ },
143+
83faa3fe
TL
144+SRST
145+``backup``
146+ Create a VM backup.
147+ERST
6402d961
TL
148+
149+ {
150+ .name = "backup_cancel",
151+ .args_type = "",
152+ .params = "",
153+ .help = "cancel the current VM backup",
83faa3fe 154+ .cmd = hmp_backup_cancel,
6402d961
TL
155+ },
156+
83faa3fe
TL
157+SRST
158+``backup_cancel``
159+ Cancel the current VM backup.
6402d961 160+
83faa3fe 161 ERST
6402d961
TL
162
163 {
164diff --git a/include/block/block_int.h b/include/block/block_int.h
f376b2b9 165index 4bd5fdb191..10452dd50b 100644
6402d961
TL
166--- a/include/block/block_int.h
167+++ b/include/block/block_int.h
8dca018b 168@@ -66,7 +66,7 @@
6402d961
TL
169
170 typedef int BackupDumpFunc(void *opaque, uint64_t offset, uint64_t bytes, const void *buf);
171
172-BlockDriverState *bdrv_backuo_dump_create(
173+BlockDriverState *bdrv_backup_dump_create(
174 int dump_cb_block_size,
175 uint64_t byte_size,
176 BackupDumpFunc *dump_cb,
177diff --git a/include/monitor/hmp.h b/include/monitor/hmp.h
f376b2b9 178index 1247d7362a..8d3df46c93 100644
6402d961
TL
179--- a/include/monitor/hmp.h
180+++ b/include/monitor/hmp.h
8dca018b
SR
181@@ -29,6 +29,7 @@ void hmp_info_savevm(Monitor *mon, const QDict *qdict);
182 void hmp_info_migrate(Monitor *mon, const QDict *qdict);
6402d961
TL
183 void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict);
184 void hmp_info_migrate_parameters(Monitor *mon, const QDict *qdict);
6402d961
TL
185+void hmp_info_backup(Monitor *mon, const QDict *qdict);
186 void hmp_info_cpus(Monitor *mon, const QDict *qdict);
83faa3fe
TL
187 void hmp_info_vnc(Monitor *mon, const QDict *qdict);
188 void hmp_info_spice(Monitor *mon, const QDict *qdict);
8dca018b 189@@ -72,6 +73,8 @@ void hmp_x_colo_lost_heartbeat(Monitor *mon, const QDict *qdict);
83faa3fe
TL
190 void hmp_set_password(Monitor *mon, const QDict *qdict);
191 void hmp_expire_password(Monitor *mon, const QDict *qdict);
6402d961 192 void hmp_change(Monitor *mon, const QDict *qdict);
6402d961
TL
193+void hmp_backup(Monitor *mon, const QDict *qdict);
194+void hmp_backup_cancel(Monitor *mon, const QDict *qdict);
83faa3fe
TL
195 void hmp_migrate(Monitor *mon, const QDict *qdict);
196 void hmp_device_add(Monitor *mon, const QDict *qdict);
197 void hmp_device_del(Monitor *mon, const QDict *qdict);
817b7667 198diff --git a/meson.build b/meson.build
f376b2b9 199index b84f62f882..c05c926cc3 100644
817b7667
SR
200--- a/meson.build
201+++ b/meson.build
f376b2b9 202@@ -1065,6 +1065,7 @@ keyutils = dependency('libkeyutils', required: false,
817b7667
SR
203 has_gettid = cc.has_function('gettid')
204
205 libuuid = cc.find_library('uuid', required: true)
206+libproxmox_backup_qemu = cc.find_library('proxmox_backup_qemu', required: true)
207
208 # Malloc tests
209
6402d961 210diff --git a/monitor/hmp-cmds.c b/monitor/hmp-cmds.c
f376b2b9 211index 42a1eaf370..55033530fb 100644
6402d961
TL
212--- a/monitor/hmp-cmds.c
213+++ b/monitor/hmp-cmds.c
817b7667 214@@ -195,6 +195,50 @@ void hmp_info_mice(Monitor *mon, const QDict *qdict)
6402d961
TL
215 qapi_free_MouseInfoList(mice_list);
216 }
217
218+void hmp_info_backup(Monitor *mon, const QDict *qdict)
219+{
220+ BackupStatus *info;
221+
222+ info = qmp_query_backup(NULL);
223+
224+ if (!info) {
225+ monitor_printf(mon, "Backup status: not initialized\n");
226+ return;
227+ }
228+
229+ if (info->has_status) {
230+ if (info->has_errmsg) {
231+ monitor_printf(mon, "Backup status: %s - %s\n",
232+ info->status, info->errmsg);
233+ } else {
234+ monitor_printf(mon, "Backup status: %s\n", info->status);
235+ }
236+ }
237+
238+ if (info->has_backup_file) {
239+ monitor_printf(mon, "Start time: %s", ctime(&info->start_time));
240+ if (info->end_time) {
241+ monitor_printf(mon, "End time: %s", ctime(&info->end_time));
242+ }
243+
244+ int per = (info->has_total && info->total &&
245+ info->has_transferred && info->transferred) ?
246+ (info->transferred * 100)/info->total : 0;
247+ int zero_per = (info->has_total && info->total &&
248+ info->has_zero_bytes && info->zero_bytes) ?
249+ (info->zero_bytes * 100)/info->total : 0;
250+ monitor_printf(mon, "Backup file: %s\n", info->backup_file);
251+ monitor_printf(mon, "Backup uuid: %s\n", info->uuid);
252+ monitor_printf(mon, "Total size: %zd\n", info->total);
253+ monitor_printf(mon, "Transferred bytes: %zd (%d%%)\n",
254+ info->transferred, per);
255+ monitor_printf(mon, "Zero bytes: %zd (%d%%)\n",
256+ info->zero_bytes, zero_per);
257+ }
258+
259+ qapi_free_BackupStatus(info);
260+}
261+
262 static char *SocketAddress_to_str(SocketAddress *addr)
263 {
264 switch (addr->type) {
6402d961
TL
265diff --git a/proxmox-backup-client.c b/proxmox-backup-client.c
266new file mode 100644
72ae34ec 267index 0000000000..a8f6653a81
6402d961
TL
268--- /dev/null
269+++ b/proxmox-backup-client.c
72ae34ec 270@@ -0,0 +1,176 @@
6402d961
TL
271+#include "proxmox-backup-client.h"
272+#include "qemu/main-loop.h"
273+#include "block/aio-wait.h"
274+#include "qapi/error.h"
275+
276+/* Proxmox Backup Server client bindings using coroutines */
277+
278+typedef struct BlockOnCoroutineWrapper {
279+ AioContext *ctx;
280+ CoroutineEntry *entry;
281+ void *entry_arg;
282+ bool finished;
283+} BlockOnCoroutineWrapper;
284+
6402d961
TL
285+static void coroutine_fn block_on_coroutine_wrapper(void *opaque)
286+{
287+ BlockOnCoroutineWrapper *wrapper = opaque;
288+ wrapper->entry(wrapper->entry_arg);
289+ wrapper->finished = true;
290+ aio_wait_kick();
291+}
292+
293+void block_on_coroutine_fn(CoroutineEntry *entry, void *entry_arg)
294+{
295+ assert(!qemu_in_coroutine());
296+
297+ AioContext *ctx = qemu_get_current_aio_context();
298+ BlockOnCoroutineWrapper wrapper = {
299+ .finished = false,
300+ .entry = entry,
301+ .entry_arg = entry_arg,
302+ .ctx = ctx,
303+ };
304+ Coroutine *wrapper_co = qemu_coroutine_create(block_on_coroutine_wrapper, &wrapper);
305+ aio_co_enter(ctx, wrapper_co);
306+ AIO_WAIT_WHILE(ctx, !wrapper.finished);
307+}
308+
309+// This is called from another thread, so we use aio_co_schedule()
310+static void proxmox_backup_schedule_wake(void *data) {
72ae34ec 311+ CoCtxData *waker = (CoCtxData *)data;
6402d961
TL
312+ aio_co_schedule(waker->ctx, waker->co);
313+}
314+
315+int coroutine_fn
316+proxmox_backup_co_connect(ProxmoxBackupHandle *pbs, Error **errp)
317+{
318+ Coroutine *co = qemu_coroutine_self();
319+ AioContext *ctx = qemu_get_current_aio_context();
72ae34ec 320+ CoCtxData waker = { .co = co, .ctx = ctx };
6402d961
TL
321+ char *pbs_err = NULL;
322+ int pbs_res = -1;
323+
324+ proxmox_backup_connect_async(pbs, proxmox_backup_schedule_wake, &waker, &pbs_res, &pbs_err);
325+ qemu_coroutine_yield();
326+ if (pbs_res < 0) {
327+ if (errp) error_setg(errp, "backup connect failed: %s", pbs_err ? pbs_err : "unknown error");
328+ if (pbs_err) proxmox_backup_free_error(pbs_err);
329+ }
330+ return pbs_res;
331+}
332+
333+int coroutine_fn
334+proxmox_backup_co_add_config(
335+ ProxmoxBackupHandle *pbs,
336+ const char *name,
337+ const uint8_t *data,
338+ uint64_t size,
339+ Error **errp)
340+{
341+ Coroutine *co = qemu_coroutine_self();
342+ AioContext *ctx = qemu_get_current_aio_context();
72ae34ec 343+ CoCtxData waker = { .co = co, .ctx = ctx };
6402d961
TL
344+ char *pbs_err = NULL;
345+ int pbs_res = -1;
346+
347+ proxmox_backup_add_config_async(
348+ pbs, name, data, size ,proxmox_backup_schedule_wake, &waker, &pbs_res, &pbs_err);
349+ qemu_coroutine_yield();
350+ if (pbs_res < 0) {
351+ if (errp) error_setg(errp, "backup add_config %s failed: %s", name, pbs_err ? pbs_err : "unknown error");
352+ if (pbs_err) proxmox_backup_free_error(pbs_err);
353+ }
354+ return pbs_res;
355+}
356+
357+int coroutine_fn
358+proxmox_backup_co_register_image(
359+ ProxmoxBackupHandle *pbs,
360+ const char *device_name,
361+ uint64_t size,
362+ Error **errp)
363+{
364+ Coroutine *co = qemu_coroutine_self();
365+ AioContext *ctx = qemu_get_current_aio_context();
72ae34ec 366+ CoCtxData waker = { .co = co, .ctx = ctx };
6402d961
TL
367+ char *pbs_err = NULL;
368+ int pbs_res = -1;
369+
370+ proxmox_backup_register_image_async(
371+ pbs, device_name, size ,proxmox_backup_schedule_wake, &waker, &pbs_res, &pbs_err);
372+ qemu_coroutine_yield();
373+ if (pbs_res < 0) {
374+ if (errp) error_setg(errp, "backup register image failed: %s", pbs_err ? pbs_err : "unknown error");
375+ if (pbs_err) proxmox_backup_free_error(pbs_err);
376+ }
377+ return pbs_res;
378+}
379+
380+int coroutine_fn
381+proxmox_backup_co_finish(
382+ ProxmoxBackupHandle *pbs,
383+ Error **errp)
384+{
385+ Coroutine *co = qemu_coroutine_self();
386+ AioContext *ctx = qemu_get_current_aio_context();
72ae34ec 387+ CoCtxData waker = { .co = co, .ctx = ctx };
6402d961
TL
388+ char *pbs_err = NULL;
389+ int pbs_res = -1;
390+
391+ proxmox_backup_finish_async(
392+ pbs, proxmox_backup_schedule_wake, &waker, &pbs_res, &pbs_err);
393+ qemu_coroutine_yield();
394+ if (pbs_res < 0) {
395+ if (errp) error_setg(errp, "backup finish failed: %s", pbs_err ? pbs_err : "unknown error");
396+ if (pbs_err) proxmox_backup_free_error(pbs_err);
397+ }
398+ return pbs_res;
399+}
400+
401+int coroutine_fn
402+proxmox_backup_co_close_image(
403+ ProxmoxBackupHandle *pbs,
404+ uint8_t dev_id,
405+ Error **errp)
406+{
407+ Coroutine *co = qemu_coroutine_self();
408+ AioContext *ctx = qemu_get_current_aio_context();
72ae34ec 409+ CoCtxData waker = { .co = co, .ctx = ctx };
6402d961
TL
410+ char *pbs_err = NULL;
411+ int pbs_res = -1;
412+
413+ proxmox_backup_close_image_async(
414+ pbs, dev_id, proxmox_backup_schedule_wake, &waker, &pbs_res, &pbs_err);
415+ qemu_coroutine_yield();
416+ if (pbs_res < 0) {
417+ if (errp) error_setg(errp, "backup close image failed: %s", pbs_err ? pbs_err : "unknown error");
418+ if (pbs_err) proxmox_backup_free_error(pbs_err);
419+ }
420+ return pbs_res;
421+}
422+
423+int coroutine_fn
424+proxmox_backup_co_write_data(
425+ ProxmoxBackupHandle *pbs,
426+ uint8_t dev_id,
427+ const uint8_t *data,
428+ uint64_t offset,
429+ uint64_t size,
430+ Error **errp)
431+{
432+ Coroutine *co = qemu_coroutine_self();
433+ AioContext *ctx = qemu_get_current_aio_context();
72ae34ec 434+ CoCtxData waker = { .co = co, .ctx = ctx };
6402d961
TL
435+ char *pbs_err = NULL;
436+ int pbs_res = -1;
437+
438+ proxmox_backup_write_data_async(
439+ pbs, dev_id, data, offset, size, proxmox_backup_schedule_wake, &waker, &pbs_res, &pbs_err);
440+ qemu_coroutine_yield();
441+ if (pbs_res < 0) {
442+ if (errp) error_setg(errp, "backup write data failed: %s", pbs_err ? pbs_err : "unknown error");
443+ if (pbs_err) proxmox_backup_free_error(pbs_err);
444+ }
445+ return pbs_res;
446+}
447diff --git a/proxmox-backup-client.h b/proxmox-backup-client.h
448new file mode 100644
72ae34ec 449index 0000000000..1dda8b7d8f
6402d961
TL
450--- /dev/null
451+++ b/proxmox-backup-client.h
72ae34ec 452@@ -0,0 +1,59 @@
6402d961
TL
453+#ifndef PROXMOX_BACKUP_CLIENT_H
454+#define PROXMOX_BACKUP_CLIENT_H
455+
456+#include "qemu/osdep.h"
457+#include "qemu/coroutine.h"
458+#include "proxmox-backup-qemu.h"
459+
72ae34ec
SR
460+typedef struct CoCtxData {
461+ Coroutine *co;
462+ AioContext *ctx;
463+ void *data;
464+} CoCtxData;
465+
466+// FIXME: Remove once coroutines are supported for QMP
6402d961
TL
467+void block_on_coroutine_fn(CoroutineEntry *entry, void *entry_arg);
468+
469+int coroutine_fn
470+proxmox_backup_co_connect(
471+ ProxmoxBackupHandle *pbs,
472+ Error **errp);
473+
474+int coroutine_fn
475+proxmox_backup_co_add_config(
476+ ProxmoxBackupHandle *pbs,
477+ const char *name,
478+ const uint8_t *data,
479+ uint64_t size,
480+ Error **errp);
481+
482+int coroutine_fn
483+proxmox_backup_co_register_image(
484+ ProxmoxBackupHandle *pbs,
485+ const char *device_name,
486+ uint64_t size,
487+ Error **errp);
488+
489+
490+int coroutine_fn
491+proxmox_backup_co_finish(
492+ ProxmoxBackupHandle *pbs,
493+ Error **errp);
494+
495+int coroutine_fn
496+proxmox_backup_co_close_image(
497+ ProxmoxBackupHandle *pbs,
498+ uint8_t dev_id,
499+ Error **errp);
500+
501+int coroutine_fn
502+proxmox_backup_co_write_data(
503+ ProxmoxBackupHandle *pbs,
504+ uint8_t dev_id,
505+ const uint8_t *data,
506+ uint64_t offset,
507+ uint64_t size,
508+ Error **errp);
509+
510+
511+#endif /* PROXMOX_BACKUP_CLIENT_H */
512diff --git a/pve-backup.c b/pve-backup.c
513new file mode 100644
277d3345 514index 0000000000..66868dec14
6402d961
TL
515--- /dev/null
516+++ b/pve-backup.c
8dca018b 517@@ -0,0 +1,959 @@
6402d961
TL
518+#include "proxmox-backup-client.h"
519+#include "vma.h"
520+
521+#include "qemu/osdep.h"
522+#include "qemu/module.h"
523+#include "sysemu/block-backend.h"
524+#include "sysemu/blockdev.h"
525+#include "block/blockjob.h"
526+#include "qapi/qapi-commands-block.h"
527+#include "qapi/qmp/qerror.h"
528+
529+/* PVE backup state and related function */
530+
0c893fd8
SR
531+/*
532+ * Note: A resume from a qemu_coroutine_yield can happen in a different thread,
533+ * so you may not use normal mutexes within coroutines:
534+ *
535+ * ---bad-example---
536+ * qemu_rec_mutex_lock(lock)
537+ * ...
538+ * qemu_coroutine_yield() // wait for something
539+ * // we are now inside a different thread
540+ * qemu_rec_mutex_unlock(lock) // Crash - wrong thread!!
541+ * ---end-bad-example--
542+ *
543+ * ==> Always use CoMutext inside coroutines.
544+ * ==> Never acquire/release AioContext withing coroutines (because that use QemuRecMutex)
545+ *
546+ */
6402d961
TL
547+
548+static struct PVEBackupState {
549+ struct {
0c893fd8
SR
550+ // Everithing accessed from qmp_backup_query command is protected using lock
551+ QemuMutex lock;
6402d961
TL
552+ Error *error;
553+ time_t start_time;
554+ time_t end_time;
555+ char *backup_file;
556+ uuid_t uuid;
557+ char uuid_str[37];
558+ size_t total;
559+ size_t transferred;
560+ size_t zero_bytes;
6402d961
TL
561+ } stat;
562+ int64_t speed;
563+ VmaWriter *vmaw;
564+ ProxmoxBackupHandle *pbs;
565+ GList *di_list;
0c893fd8
SR
566+ QemuMutex backup_mutex;
567+ CoMutex dump_callback_mutex;
6402d961
TL
568+} backup_state;
569+
570+static void pvebackup_init(void)
571+{
0c893fd8
SR
572+ qemu_mutex_init(&backup_state.stat.lock);
573+ qemu_mutex_init(&backup_state.backup_mutex);
574+ qemu_co_mutex_init(&backup_state.dump_callback_mutex);
6402d961
TL
575+}
576+
577+// initialize PVEBackupState at startup
578+opts_init(pvebackup_init);
579+
580+typedef struct PVEBackupDevInfo {
581+ BlockDriverState *bs;
582+ size_t size;
583+ uint8_t dev_id;
584+ bool completed;
585+ char targetfile[PATH_MAX];
586+ BlockDriverState *target;
587+} PVEBackupDevInfo;
588+
0c893fd8 589+static void pvebackup_run_next_job(void);
6402d961 590+
0c893fd8
SR
591+static BlockJob *
592+lookup_active_block_job(PVEBackupDevInfo *di)
593+{
594+ if (!di->completed && di->bs) {
595+ for (BlockJob *job = block_job_next(NULL); job; job = block_job_next(job)) {
596+ if (job->job.driver->job_type != JOB_TYPE_BACKUP) {
597+ continue;
598+ }
599+
600+ BackupBlockJob *bjob = container_of(job, BackupBlockJob, common);
601+ if (bjob && bjob->source_bs == di->bs) {
602+ return job;
603+ }
604+ }
605+ }
606+ return NULL;
607+}
608+
609+static void pvebackup_propagate_error(Error *err)
610+{
611+ qemu_mutex_lock(&backup_state.stat.lock);
612+ error_propagate(&backup_state.stat.error, err);
613+ qemu_mutex_unlock(&backup_state.stat.lock);
614+}
615+
616+static bool pvebackup_error_or_canceled(void)
617+{
618+ qemu_mutex_lock(&backup_state.stat.lock);
619+ bool error_or_canceled = !!backup_state.stat.error;
620+ qemu_mutex_unlock(&backup_state.stat.lock);
621+
622+ return error_or_canceled;
623+}
624+
625+static void pvebackup_add_transfered_bytes(size_t transferred, size_t zero_bytes)
626+{
627+ qemu_mutex_lock(&backup_state.stat.lock);
628+ backup_state.stat.zero_bytes += zero_bytes;
629+ backup_state.stat.transferred += transferred;
630+ qemu_mutex_unlock(&backup_state.stat.lock);
631+}
632+
633+// This may get called from multiple coroutines in multiple io-threads
634+// Note1: this may get called after job_cancel()
6402d961 635+static int coroutine_fn
0c893fd8 636+pvebackup_co_dump_pbs_cb(
6402d961
TL
637+ void *opaque,
638+ uint64_t start,
639+ uint64_t bytes,
640+ const void *pbuf)
641+{
642+ assert(qemu_in_coroutine());
643+
644+ const uint64_t size = bytes;
645+ const unsigned char *buf = pbuf;
646+ PVEBackupDevInfo *di = opaque;
647+
0c893fd8
SR
648+ assert(backup_state.pbs);
649+
650+ Error *local_err = NULL;
651+ int pbs_res = -1;
652+
653+ qemu_co_mutex_lock(&backup_state.dump_callback_mutex);
6402d961 654+
0c893fd8
SR
655+ // avoid deadlock if job is cancelled
656+ if (pvebackup_error_or_canceled()) {
657+ qemu_co_mutex_unlock(&backup_state.dump_callback_mutex);
658+ return -1;
6402d961
TL
659+ }
660+
0c893fd8
SR
661+ pbs_res = proxmox_backup_co_write_data(backup_state.pbs, di->dev_id, buf, start, size, &local_err);
662+ qemu_co_mutex_unlock(&backup_state.dump_callback_mutex);
663+
664+ if (pbs_res < 0) {
665+ pvebackup_propagate_error(local_err);
666+ return pbs_res;
667+ } else {
668+ pvebackup_add_transfered_bytes(size, !buf ? size : 0);
669+ }
670+
671+ return size;
672+}
673+
674+// This may get called from multiple coroutines in multiple io-threads
675+static int coroutine_fn
676+pvebackup_co_dump_vma_cb(
677+ void *opaque,
678+ uint64_t start,
679+ uint64_t bytes,
680+ const void *pbuf)
681+{
682+ assert(qemu_in_coroutine());
683+
684+ const uint64_t size = bytes;
685+ const unsigned char *buf = pbuf;
686+ PVEBackupDevInfo *di = opaque;
6402d961
TL
687+
688+ int ret = -1;
689+
0c893fd8 690+ assert(backup_state.vmaw);
6402d961 691+
0c893fd8 692+ uint64_t remaining = size;
6402d961 693+
0c893fd8
SR
694+ uint64_t cluster_num = start / VMA_CLUSTER_SIZE;
695+ if ((cluster_num * VMA_CLUSTER_SIZE) != start) {
6402d961 696+ Error *local_err = NULL;
0c893fd8
SR
697+ error_setg(&local_err,
698+ "got unaligned write inside backup dump "
699+ "callback (sector %ld)", start);
700+ pvebackup_propagate_error(local_err);
701+ return -1; // not aligned to cluster size
702+ }
6402d961 703+
0c893fd8
SR
704+ while (remaining > 0) {
705+ qemu_co_mutex_lock(&backup_state.dump_callback_mutex);
706+ // avoid deadlock if job is cancelled
707+ if (pvebackup_error_or_canceled()) {
708+ qemu_co_mutex_unlock(&backup_state.dump_callback_mutex);
709+ return -1;
710+ }
6402d961 711+
0c893fd8
SR
712+ size_t zero_bytes = 0;
713+ ret = vma_writer_write(backup_state.vmaw, di->dev_id, cluster_num, buf, &zero_bytes);
714+ qemu_co_mutex_unlock(&backup_state.dump_callback_mutex);
6402d961 715+
0c893fd8
SR
716+ ++cluster_num;
717+ if (buf) {
718+ buf += VMA_CLUSTER_SIZE;
719+ }
720+ if (ret < 0) {
721+ Error *local_err = NULL;
722+ vma_writer_error_propagate(backup_state.vmaw, &local_err);
723+ pvebackup_propagate_error(local_err);
724+ return ret;
6402d961 725+ } else {
0c893fd8
SR
726+ if (remaining >= VMA_CLUSTER_SIZE) {
727+ assert(ret == VMA_CLUSTER_SIZE);
728+ pvebackup_add_transfered_bytes(VMA_CLUSTER_SIZE, zero_bytes);
729+ remaining -= VMA_CLUSTER_SIZE;
730+ } else {
731+ assert(ret == remaining);
732+ pvebackup_add_transfered_bytes(remaining, zero_bytes);
733+ remaining = 0;
6402d961 734+ }
6402d961 735+ }
6402d961
TL
736+ }
737+
6402d961
TL
738+ return size;
739+}
740+
0c893fd8
SR
741+// assumes the caller holds backup_mutex
742+static void coroutine_fn pvebackup_co_cleanup(void *unused)
6402d961
TL
743+{
744+ assert(qemu_in_coroutine());
745+
0c893fd8 746+ qemu_mutex_lock(&backup_state.stat.lock);
6402d961 747+ backup_state.stat.end_time = time(NULL);
0c893fd8 748+ qemu_mutex_unlock(&backup_state.stat.lock);
6402d961
TL
749+
750+ if (backup_state.vmaw) {
751+ Error *local_err = NULL;
752+ vma_writer_close(backup_state.vmaw, &local_err);
753+
754+ if (local_err != NULL) {
0c893fd8
SR
755+ pvebackup_propagate_error(local_err);
756+ }
6402d961
TL
757+
758+ backup_state.vmaw = NULL;
759+ }
760+
761+ if (backup_state.pbs) {
0c893fd8 762+ if (!pvebackup_error_or_canceled()) {
6402d961
TL
763+ Error *local_err = NULL;
764+ proxmox_backup_co_finish(backup_state.pbs, &local_err);
765+ if (local_err != NULL) {
0c893fd8
SR
766+ pvebackup_propagate_error(local_err);
767+ }
6402d961 768+ }
6402d961
TL
769+
770+ proxmox_backup_disconnect(backup_state.pbs);
771+ backup_state.pbs = NULL;
772+ }
773+
774+ g_list_free(backup_state.di_list);
775+ backup_state.di_list = NULL;
6402d961
TL
776+}
777+
0c893fd8
SR
778+// assumes the caller holds backup_mutex
779+static void coroutine_fn pvebackup_complete_stream(void *opaque)
6402d961 780+{
0c893fd8 781+ PVEBackupDevInfo *di = opaque;
6402d961 782+
0c893fd8 783+ bool error_or_canceled = pvebackup_error_or_canceled();
6402d961
TL
784+
785+ if (backup_state.vmaw) {
786+ vma_writer_close_stream(backup_state.vmaw, di->dev_id);
787+ }
788+
789+ if (backup_state.pbs && !error_or_canceled) {
790+ Error *local_err = NULL;
791+ proxmox_backup_co_close_image(backup_state.pbs, di->dev_id, &local_err);
792+ if (local_err != NULL) {
0c893fd8 793+ pvebackup_propagate_error(local_err);
6402d961
TL
794+ }
795+ }
0c893fd8 796+}
6402d961 797+
0c893fd8
SR
798+static void pvebackup_complete_cb(void *opaque, int ret)
799+{
800+ assert(!qemu_in_coroutine());
6402d961 801+
0c893fd8 802+ PVEBackupDevInfo *di = opaque;
6402d961 803+
0c893fd8 804+ qemu_mutex_lock(&backup_state.backup_mutex);
6402d961 805+
0c893fd8
SR
806+ di->completed = true;
807+
808+ if (ret < 0) {
809+ Error *local_err = NULL;
810+ error_setg(&local_err, "job failed with err %d - %s", ret, strerror(-ret));
811+ pvebackup_propagate_error(local_err);
6402d961 812+ }
6402d961 813+
0c893fd8 814+ di->bs = NULL;
6402d961 815+
0c893fd8 816+ assert(di->target == NULL);
6402d961 817+
0c893fd8 818+ block_on_coroutine_fn(pvebackup_complete_stream, di);
6402d961 819+
0c893fd8
SR
820+ // remove self from job queue
821+ backup_state.di_list = g_list_remove(backup_state.di_list, di);
6402d961 822+
0c893fd8 823+ g_free(di);
6402d961 824+
0c893fd8 825+ qemu_mutex_unlock(&backup_state.backup_mutex);
6402d961 826+
0c893fd8
SR
827+ pvebackup_run_next_job();
828+}
829+
830+static void pvebackup_cancel(void)
831+{
832+ assert(!qemu_in_coroutine());
833+
834+ Error *cancel_err = NULL;
835+ error_setg(&cancel_err, "backup canceled");
836+ pvebackup_propagate_error(cancel_err);
837+
838+ qemu_mutex_lock(&backup_state.backup_mutex);
6402d961
TL
839+
840+ if (backup_state.vmaw) {
841+ /* make sure vma writer does not block anymore */
0c893fd8 842+ vma_writer_set_error(backup_state.vmaw, "backup canceled");
6402d961
TL
843+ }
844+
845+ if (backup_state.pbs) {
0c893fd8 846+ proxmox_backup_abort(backup_state.pbs, "backup canceled");
6402d961
TL
847+ }
848+
0c893fd8 849+ qemu_mutex_unlock(&backup_state.backup_mutex);
6402d961 850+
0c893fd8 851+ for(;;) {
6402d961 852+
0c893fd8
SR
853+ BlockJob *next_job = NULL;
854+
855+ qemu_mutex_lock(&backup_state.backup_mutex);
856+
857+ GList *l = backup_state.di_list;
858+ while (l) {
859+ PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
860+ l = g_list_next(l);
861+
862+ BlockJob *job = lookup_active_block_job(di);
863+ if (job != NULL) {
864+ next_job = job;
865+ break;
6402d961
TL
866+ }
867+ }
6402d961 868+
0c893fd8 869+ qemu_mutex_unlock(&backup_state.backup_mutex);
6402d961 870+
0c893fd8
SR
871+ if (next_job) {
872+ AioContext *aio_context = next_job->job.aio_context;
873+ aio_context_acquire(aio_context);
874+ job_cancel_sync(&next_job->job);
875+ aio_context_release(aio_context);
876+ } else {
877+ break;
878+ }
879+ }
6402d961
TL
880+}
881+
882+void qmp_backup_cancel(Error **errp)
883+{
0c893fd8 884+ pvebackup_cancel();
6402d961
TL
885+}
886+
0c893fd8 887+// assumes the caller holds backup_mutex
6402d961
TL
888+static int coroutine_fn pvebackup_co_add_config(
889+ const char *file,
890+ const char *name,
891+ BackupFormat format,
892+ const char *backup_dir,
893+ VmaWriter *vmaw,
894+ ProxmoxBackupHandle *pbs,
895+ Error **errp)
896+{
897+ int res = 0;
898+
899+ char *cdata = NULL;
900+ gsize clen = 0;
901+ GError *err = NULL;
902+ if (!g_file_get_contents(file, &cdata, &clen, &err)) {
903+ error_setg(errp, "unable to read file '%s'", file);
904+ return 1;
905+ }
906+
907+ char *basename = g_path_get_basename(file);
908+ if (name == NULL) name = basename;
909+
910+ if (format == BACKUP_FORMAT_VMA) {
911+ if (vma_writer_add_config(vmaw, name, cdata, clen) != 0) {
912+ error_setg(errp, "unable to add %s config data to vma archive", file);
913+ goto err;
914+ }
915+ } else if (format == BACKUP_FORMAT_PBS) {
916+ if (proxmox_backup_co_add_config(pbs, name, (unsigned char *)cdata, clen, errp) < 0)
917+ goto err;
918+ } else if (format == BACKUP_FORMAT_DIR) {
919+ char config_path[PATH_MAX];
920+ snprintf(config_path, PATH_MAX, "%s/%s", backup_dir, name);
921+ if (!g_file_set_contents(config_path, cdata, clen, &err)) {
922+ error_setg(errp, "unable to write config file '%s'", config_path);
923+ goto err;
924+ }
925+ }
926+
927+ out:
928+ g_free(basename);
929+ g_free(cdata);
930+ return res;
931+
932+ err:
933+ res = -1;
934+ goto out;
935+}
936+
937+bool job_should_pause(Job *job);
938+
0c893fd8 939+static void pvebackup_run_next_job(void)
6402d961 940+{
0c893fd8 941+ assert(!qemu_in_coroutine());
6402d961 942+
0c893fd8 943+ qemu_mutex_lock(&backup_state.backup_mutex);
6402d961
TL
944+
945+ GList *l = backup_state.di_list;
946+ while (l) {
947+ PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
948+ l = g_list_next(l);
6402d961 949+
0c893fd8
SR
950+ BlockJob *job = lookup_active_block_job(di);
951+
952+ if (job) {
953+ qemu_mutex_unlock(&backup_state.backup_mutex);
954+
955+ AioContext *aio_context = job->job.aio_context;
956+ aio_context_acquire(aio_context);
957+
958+ if (job_should_pause(&job->job)) {
959+ bool error_or_canceled = pvebackup_error_or_canceled();
960+ if (error_or_canceled) {
961+ job_cancel_sync(&job->job);
962+ } else {
963+ job_resume(&job->job);
6402d961
TL
964+ }
965+ }
0c893fd8
SR
966+ aio_context_release(aio_context);
967+ return;
6402d961
TL
968+ }
969+ }
0c893fd8
SR
970+
971+ block_on_coroutine_fn(pvebackup_co_cleanup, NULL); // no more jobs, run cleanup
972+
973+ qemu_mutex_unlock(&backup_state.backup_mutex);
974+}
975+
976+static bool create_backup_jobs(void) {
977+
978+ assert(!qemu_in_coroutine());
979+
980+ Error *local_err = NULL;
981+
8dca018b
SR
982+ BackupPerf perf = { .max_workers = 16 };
983+
0c893fd8
SR
984+ /* create and start all jobs (paused state) */
985+ GList *l = backup_state.di_list;
986+ while (l) {
987+ PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
988+ l = g_list_next(l);
989+
990+ assert(di->target != NULL);
991+
992+ AioContext *aio_context = bdrv_get_aio_context(di->bs);
993+ aio_context_acquire(aio_context);
994+
995+ BlockJob *job = backup_job_create(
996+ NULL, di->bs, di->target, backup_state.speed, MIRROR_SYNC_MODE_FULL, NULL,
8dca018b
SR
997+ BITMAP_SYNC_MODE_NEVER, false, NULL, &perf, BLOCKDEV_ON_ERROR_REPORT, BLOCKDEV_ON_ERROR_REPORT,
998+ JOB_DEFAULT, pvebackup_complete_cb, di, NULL, &local_err);
0c893fd8
SR
999+
1000+ aio_context_release(aio_context);
1001+
1002+ if (!job || local_err != NULL) {
1003+ Error *create_job_err = NULL;
1004+ error_setg(&create_job_err, "backup_job_create failed: %s",
1005+ local_err ? error_get_pretty(local_err) : "null");
1006+
1007+ pvebackup_propagate_error(create_job_err);
1008+ break;
1009+ }
1010+ job_start(&job->job);
1011+
1012+ bdrv_unref(di->target);
1013+ di->target = NULL;
1014+ }
1015+
1016+ bool errors = pvebackup_error_or_canceled();
1017+
1018+ if (errors) {
1019+ l = backup_state.di_list;
1020+ while (l) {
1021+ PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
1022+ l = g_list_next(l);
1023+
1024+ if (di->target) {
1025+ bdrv_unref(di->target);
1026+ di->target = NULL;
1027+ }
1028+ }
1029+ }
1030+
1031+ return errors;
6402d961
TL
1032+}
1033+
1034+typedef struct QmpBackupTask {
1035+ const char *backup_file;
1036+ bool has_password;
1037+ const char *password;
1038+ bool has_keyfile;
1039+ const char *keyfile;
1040+ bool has_key_password;
1041+ const char *key_password;
1042+ bool has_backup_id;
1043+ const char *backup_id;
1044+ bool has_backup_time;
1045+ const char *fingerprint;
1046+ bool has_fingerprint;
1047+ int64_t backup_time;
1048+ bool has_format;
1049+ BackupFormat format;
1050+ bool has_config_file;
1051+ const char *config_file;
1052+ bool has_firewall_file;
1053+ const char *firewall_file;
1054+ bool has_devlist;
1055+ const char *devlist;
1056+ bool has_speed;
1057+ int64_t speed;
1058+ Error **errp;
1059+ UuidInfo *result;
1060+} QmpBackupTask;
1061+
0c893fd8
SR
1062+// assumes the caller holds backup_mutex
1063+static void coroutine_fn pvebackup_co_prepare(void *opaque)
6402d961
TL
1064+{
1065+ assert(qemu_in_coroutine());
1066+
1067+ QmpBackupTask *task = opaque;
1068+
1069+ task->result = NULL; // just to be sure
1070+
1071+ BlockBackend *blk;
1072+ BlockDriverState *bs = NULL;
1073+ const char *backup_dir = NULL;
1074+ Error *local_err = NULL;
1075+ uuid_t uuid;
1076+ VmaWriter *vmaw = NULL;
1077+ ProxmoxBackupHandle *pbs = NULL;
1078+ gchar **devs = NULL;
1079+ GList *di_list = NULL;
1080+ GList *l;
1081+ UuidInfo *uuid_info;
6402d961
TL
1082+
1083+ const char *config_name = "qemu-server.conf";
1084+ const char *firewall_name = "qemu-server.fw";
1085+
6402d961 1086+ if (backup_state.di_list) {
0c893fd8 1087+ error_set(task->errp, ERROR_CLASS_GENERIC_ERROR,
6402d961
TL
1088+ "previous backup not finished");
1089+ return;
1090+ }
1091+
1092+ /* Todo: try to auto-detect format based on file name */
1093+ BackupFormat format = task->has_format ? task->format : BACKUP_FORMAT_VMA;
1094+
1095+ if (task->has_devlist) {
1096+ devs = g_strsplit_set(task->devlist, ",;:", -1);
1097+
1098+ gchar **d = devs;
1099+ while (d && *d) {
1100+ blk = blk_by_name(*d);
1101+ if (blk) {
1102+ bs = blk_bs(blk);
6402d961
TL
1103+ if (!bdrv_is_inserted(bs)) {
1104+ error_setg(task->errp, QERR_DEVICE_HAS_NO_MEDIUM, *d);
1105+ goto err;
1106+ }
1107+ PVEBackupDevInfo *di = g_new0(PVEBackupDevInfo, 1);
1108+ di->bs = bs;
1109+ di_list = g_list_append(di_list, di);
1110+ } else {
1111+ error_set(task->errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1112+ "Device '%s' not found", *d);
1113+ goto err;
1114+ }
1115+ d++;
1116+ }
1117+
1118+ } else {
1119+ BdrvNextIterator it;
1120+
1121+ bs = NULL;
1122+ for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
1123+ if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
1124+ continue;
1125+ }
1126+
1127+ PVEBackupDevInfo *di = g_new0(PVEBackupDevInfo, 1);
1128+ di->bs = bs;
1129+ di_list = g_list_append(di_list, di);
1130+ }
1131+ }
1132+
1133+ if (!di_list) {
1134+ error_set(task->errp, ERROR_CLASS_GENERIC_ERROR, "empty device list");
1135+ goto err;
1136+ }
1137+
1138+ size_t total = 0;
1139+
1140+ l = di_list;
1141+ while (l) {
1142+ PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
1143+ l = g_list_next(l);
1144+ if (bdrv_op_is_blocked(di->bs, BLOCK_OP_TYPE_BACKUP_SOURCE, task->errp)) {
1145+ goto err;
1146+ }
1147+
1148+ ssize_t size = bdrv_getlength(di->bs);
1149+ if (size < 0) {
1150+ error_setg_errno(task->errp, -di->size, "bdrv_getlength failed");
1151+ goto err;
1152+ }
1153+ di->size = size;
1154+ total += size;
1155+ }
1156+
1157+ uuid_generate(uuid);
1158+
1159+ if (format == BACKUP_FORMAT_PBS) {
1160+ if (!task->has_password) {
1161+ error_set(task->errp, ERROR_CLASS_GENERIC_ERROR, "missing parameter 'password'");
1162+ goto err;
1163+ }
1164+ if (!task->has_backup_id) {
1165+ error_set(task->errp, ERROR_CLASS_GENERIC_ERROR, "missing parameter 'backup-id'");
1166+ goto err;
1167+ }
1168+ if (!task->has_backup_time) {
1169+ error_set(task->errp, ERROR_CLASS_GENERIC_ERROR, "missing parameter 'backup-time'");
1170+ goto err;
1171+ }
1172+
1173+ int dump_cb_block_size = PROXMOX_BACKUP_DEFAULT_CHUNK_SIZE; // Hardcoded (4M)
1174+ firewall_name = "fw.conf";
1175+
1176+ char *pbs_err = NULL;
1177+ pbs = proxmox_backup_new(
1178+ task->backup_file,
1179+ task->backup_id,
1180+ task->backup_time,
1181+ dump_cb_block_size,
1182+ task->has_password ? task->password : NULL,
1183+ task->has_keyfile ? task->keyfile : NULL,
1184+ task->has_key_password ? task->key_password : NULL,
1185+ task->has_fingerprint ? task->fingerprint : NULL,
1186+ &pbs_err);
1187+
1188+ if (!pbs) {
1189+ error_set(task->errp, ERROR_CLASS_GENERIC_ERROR,
1190+ "proxmox_backup_new failed: %s", pbs_err);
1191+ proxmox_backup_free_error(pbs_err);
1192+ goto err;
1193+ }
1194+
1195+ if (proxmox_backup_co_connect(pbs, task->errp) < 0)
1196+ goto err;
1197+
1198+ /* register all devices */
1199+ l = di_list;
1200+ while (l) {
1201+ PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
1202+ l = g_list_next(l);
1203+
1204+ const char *devname = bdrv_get_device_name(di->bs);
1205+
1206+ int dev_id = proxmox_backup_co_register_image(pbs, devname, di->size, task->errp);
1207+ if (dev_id < 0)
1208+ goto err;
1209+
0c893fd8 1210+ if (!(di->target = bdrv_backup_dump_create(dump_cb_block_size, di->size, pvebackup_co_dump_pbs_cb, di, task->errp))) {
6402d961
TL
1211+ goto err;
1212+ }
1213+
1214+ di->dev_id = dev_id;
1215+ }
1216+ } else if (format == BACKUP_FORMAT_VMA) {
1217+ vmaw = vma_writer_create(task->backup_file, uuid, &local_err);
1218+ if (!vmaw) {
1219+ if (local_err) {
1220+ error_propagate(task->errp, local_err);
1221+ }
1222+ goto err;
1223+ }
1224+
1225+ /* register all devices for vma writer */
1226+ l = di_list;
1227+ while (l) {
1228+ PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
1229+ l = g_list_next(l);
1230+
0c893fd8 1231+ if (!(di->target = bdrv_backup_dump_create(VMA_CLUSTER_SIZE, di->size, pvebackup_co_dump_vma_cb, di, task->errp))) {
6402d961
TL
1232+ goto err;
1233+ }
1234+
1235+ const char *devname = bdrv_get_device_name(di->bs);
1236+ di->dev_id = vma_writer_register_stream(vmaw, devname, di->size);
1237+ if (di->dev_id <= 0) {
1238+ error_set(task->errp, ERROR_CLASS_GENERIC_ERROR,
1239+ "register_stream failed");
1240+ goto err;
1241+ }
1242+ }
1243+ } else if (format == BACKUP_FORMAT_DIR) {
1244+ if (mkdir(task->backup_file, 0640) != 0) {
1245+ error_setg_errno(task->errp, errno, "can't create directory '%s'\n",
1246+ task->backup_file);
1247+ goto err;
1248+ }
1249+ backup_dir = task->backup_file;
1250+
1251+ l = di_list;
1252+ while (l) {
1253+ PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
1254+ l = g_list_next(l);
1255+
1256+ const char *devname = bdrv_get_device_name(di->bs);
1257+ snprintf(di->targetfile, PATH_MAX, "%s/%s.raw", backup_dir, devname);
1258+
1259+ int flags = BDRV_O_RDWR;
1260+ bdrv_img_create(di->targetfile, "raw", NULL, NULL, NULL,
1261+ di->size, flags, false, &local_err);
1262+ if (local_err) {
1263+ error_propagate(task->errp, local_err);
1264+ goto err;
1265+ }
1266+
1267+ di->target = bdrv_open(di->targetfile, NULL, NULL, flags, &local_err);
1268+ if (!di->target) {
1269+ error_propagate(task->errp, local_err);
1270+ goto err;
1271+ }
1272+ }
1273+ } else {
1274+ error_set(task->errp, ERROR_CLASS_GENERIC_ERROR, "unknown backup format");
1275+ goto err;
1276+ }
1277+
1278+
1279+ /* add configuration file to archive */
1280+ if (task->has_config_file) {
1281+ if (pvebackup_co_add_config(task->config_file, config_name, format, backup_dir,
1282+ vmaw, pbs, task->errp) != 0) {
1283+ goto err;
1284+ }
1285+ }
1286+
1287+ /* add firewall file to archive */
1288+ if (task->has_firewall_file) {
1289+ if (pvebackup_co_add_config(task->firewall_file, firewall_name, format, backup_dir,
1290+ vmaw, pbs, task->errp) != 0) {
1291+ goto err;
1292+ }
1293+ }
1294+ /* initialize global backup_state now */
1295+
0c893fd8 1296+ qemu_mutex_lock(&backup_state.stat.lock);
6402d961
TL
1297+
1298+ if (backup_state.stat.error) {
1299+ error_free(backup_state.stat.error);
1300+ backup_state.stat.error = NULL;
1301+ }
1302+
1303+ backup_state.stat.start_time = time(NULL);
1304+ backup_state.stat.end_time = 0;
1305+
1306+ if (backup_state.stat.backup_file) {
1307+ g_free(backup_state.stat.backup_file);
1308+ }
1309+ backup_state.stat.backup_file = g_strdup(task->backup_file);
1310+
1311+ uuid_copy(backup_state.stat.uuid, uuid);
1312+ uuid_unparse_lower(uuid, backup_state.stat.uuid_str);
1313+ char *uuid_str = g_strdup(backup_state.stat.uuid_str);
1314+
1315+ backup_state.stat.total = total;
1316+ backup_state.stat.transferred = 0;
1317+ backup_state.stat.zero_bytes = 0;
1318+
0c893fd8 1319+ qemu_mutex_unlock(&backup_state.stat.lock);
6402d961
TL
1320+
1321+ backup_state.speed = (task->has_speed && task->speed > 0) ? task->speed : 0;
1322+
1323+ backup_state.vmaw = vmaw;
1324+ backup_state.pbs = pbs;
1325+
1326+ backup_state.di_list = di_list;
1327+
6402d961
TL
1328+ uuid_info = g_malloc0(sizeof(*uuid_info));
1329+ uuid_info->UUID = uuid_str;
1330+
1331+ task->result = uuid_info;
1332+ return;
1333+
1334+err:
1335+
1336+ l = di_list;
1337+ while (l) {
1338+ PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
1339+ l = g_list_next(l);
1340+
1341+ if (di->target) {
1342+ bdrv_unref(di->target);
1343+ }
1344+
1345+ if (di->targetfile[0]) {
1346+ unlink(di->targetfile);
1347+ }
1348+ g_free(di);
1349+ }
1350+ g_list_free(di_list);
1351+
1352+ if (devs) {
1353+ g_strfreev(devs);
1354+ }
1355+
1356+ if (vmaw) {
1357+ Error *err = NULL;
1358+ vma_writer_close(vmaw, &err);
1359+ unlink(task->backup_file);
1360+ }
1361+
1362+ if (pbs) {
1363+ proxmox_backup_disconnect(pbs);
1364+ }
1365+
1366+ if (backup_dir) {
1367+ rmdir(backup_dir);
1368+ }
1369+
6402d961
TL
1370+ task->result = NULL;
1371+ return;
1372+}
1373+
1374+UuidInfo *qmp_backup(
1375+ const char *backup_file,
1376+ bool has_password, const char *password,
1377+ bool has_keyfile, const char *keyfile,
1378+ bool has_key_password, const char *key_password,
1379+ bool has_fingerprint, const char *fingerprint,
1380+ bool has_backup_id, const char *backup_id,
1381+ bool has_backup_time, int64_t backup_time,
1382+ bool has_format, BackupFormat format,
1383+ bool has_config_file, const char *config_file,
1384+ bool has_firewall_file, const char *firewall_file,
1385+ bool has_devlist, const char *devlist,
1386+ bool has_speed, int64_t speed, Error **errp)
1387+{
1388+ QmpBackupTask task = {
1389+ .backup_file = backup_file,
1390+ .has_password = has_password,
1391+ .password = password,
1392+ .has_key_password = has_key_password,
1393+ .key_password = key_password,
1394+ .has_fingerprint = has_fingerprint,
1395+ .fingerprint = fingerprint,
1396+ .has_backup_id = has_backup_id,
1397+ .backup_id = backup_id,
1398+ .has_backup_time = has_backup_time,
1399+ .backup_time = backup_time,
1400+ .has_format = has_format,
1401+ .format = format,
1402+ .has_config_file = has_config_file,
1403+ .config_file = config_file,
1404+ .has_firewall_file = has_firewall_file,
1405+ .firewall_file = firewall_file,
1406+ .has_devlist = has_devlist,
1407+ .devlist = devlist,
1408+ .has_speed = has_speed,
1409+ .speed = speed,
1410+ .errp = errp,
1411+ };
1412+
0c893fd8 1413+ qemu_mutex_lock(&backup_state.backup_mutex);
6402d961 1414+
0c893fd8 1415+ block_on_coroutine_fn(pvebackup_co_prepare, &task);
6402d961 1416+
0c893fd8
SR
1417+ if (*errp == NULL) {
1418+ create_backup_jobs();
1419+ qemu_mutex_unlock(&backup_state.backup_mutex);
1420+ pvebackup_run_next_job();
1421+ } else {
1422+ qemu_mutex_unlock(&backup_state.backup_mutex);
1423+ }
6402d961 1424+
0c893fd8
SR
1425+ return task.result;
1426+}
6402d961 1427+
0c893fd8 1428+BackupStatus *qmp_query_backup(Error **errp)
6402d961 1429+{
6402d961
TL
1430+ BackupStatus *info = g_malloc0(sizeof(*info));
1431+
0c893fd8 1432+ qemu_mutex_lock(&backup_state.stat.lock);
6402d961
TL
1433+
1434+ if (!backup_state.stat.start_time) {
1435+ /* not started, return {} */
0c893fd8
SR
1436+ qemu_mutex_unlock(&backup_state.stat.lock);
1437+ return info;
6402d961
TL
1438+ }
1439+
1440+ info->has_status = true;
1441+ info->has_start_time = true;
1442+ info->start_time = backup_state.stat.start_time;
1443+
1444+ if (backup_state.stat.backup_file) {
1445+ info->has_backup_file = true;
1446+ info->backup_file = g_strdup(backup_state.stat.backup_file);
1447+ }
1448+
1449+ info->has_uuid = true;
1450+ info->uuid = g_strdup(backup_state.stat.uuid_str);
1451+
1452+ if (backup_state.stat.end_time) {
1453+ if (backup_state.stat.error) {
1454+ info->status = g_strdup("error");
1455+ info->has_errmsg = true;
1456+ info->errmsg = g_strdup(error_get_pretty(backup_state.stat.error));
1457+ } else {
1458+ info->status = g_strdup("done");
1459+ }
1460+ info->has_end_time = true;
1461+ info->end_time = backup_state.stat.end_time;
1462+ } else {
1463+ info->status = g_strdup("active");
1464+ }
1465+
1466+ info->has_total = true;
1467+ info->total = backup_state.stat.total;
1468+ info->has_zero_bytes = true;
1469+ info->zero_bytes = backup_state.stat.zero_bytes;
1470+ info->has_transferred = true;
1471+ info->transferred = backup_state.stat.transferred;
1472+
0c893fd8 1473+ qemu_mutex_unlock(&backup_state.stat.lock);
6402d961 1474+
0c893fd8 1475+ return info;
6402d961
TL
1476+}
1477diff --git a/qapi/block-core.json b/qapi/block-core.json
f376b2b9 1478index fdfa579d00..c5d604693f 100644
6402d961
TL
1479--- a/qapi/block-core.json
1480+++ b/qapi/block-core.json
f376b2b9 1481@@ -699,6 +699,115 @@
6402d961
TL
1482 { 'command': 'query-block', 'returns': ['BlockInfo'] }
1483
1484
1485+##
1486+# @BackupStatus:
1487+#
1488+# Detailed backup status.
1489+#
1490+# @status: string describing the current backup status.
1491+# This can be 'active', 'done', 'error'. If this field is not
1492+# returned, no backup process has been initiated
1493+#
1494+# @errmsg: error message (only returned if status is 'error')
1495+#
1496+# @total: total amount of bytes involved in the backup process
1497+#
1498+# @transferred: amount of bytes already backed up.
1499+#
1500+# @zero-bytes: amount of 'zero' bytes detected.
1501+#
1502+# @start-time: time (epoch) when backup job started.
1503+#
1504+# @end-time: time (epoch) when backup job finished.
1505+#
1506+# @backup-file: backup file name
1507+#
1508+# @uuid: uuid for this backup job
1509+#
1510+##
1511+{ 'struct': 'BackupStatus',
1512+ 'data': {'*status': 'str', '*errmsg': 'str', '*total': 'int',
1513+ '*transferred': 'int', '*zero-bytes': 'int',
1514+ '*start-time': 'int', '*end-time': 'int',
1515+ '*backup-file': 'str', '*uuid': 'str' } }
1516+
1517+##
1518+# @BackupFormat:
1519+#
1520+# An enumeration of supported backup formats.
1521+#
1522+# @vma: Proxmox vma backup format
1523+##
1524+{ 'enum': 'BackupFormat',
1525+ 'data': [ 'vma', 'dir', 'pbs' ] }
1526+
1527+##
1528+# @backup:
1529+#
1530+# Starts a VM backup.
1531+#
1532+# @backup-file: the backup file name
1533+#
1534+# @format: format of the backup file
1535+#
1536+# @config-file: a configuration file to include into
817b7667 1537+# the backup archive.
6402d961
TL
1538+#
1539+# @speed: the maximum speed, in bytes per second
1540+#
1541+# @devlist: list of block device names (separated by ',', ';'
817b7667 1542+# or ':'). By default the backup includes all writable block devices.
6402d961
TL
1543+#
1544+# @password: backup server passsword (required for format 'pbs')
1545+#
1546+# @keyfile: keyfile used for encryption (optional for format 'pbs')
1547+#
1548+# @key-password: password for keyfile (optional for format 'pbs')
1549+#
1550+# @fingerprint: server cert fingerprint (optional for format 'pbs')
1551+#
1552+# @backup-id: backup ID (required for format 'pbs')
1553+#
1554+# @backup-time: backup timestamp (Unix epoch, required for format 'pbs')
1555+#
1556+# Returns: the uuid of the backup job
1557+#
1558+##
1559+{ 'command': 'backup', 'data': { 'backup-file': 'str',
1560+ '*password': 'str',
1561+ '*keyfile': 'str',
1562+ '*key-password': 'str',
1563+ '*fingerprint': 'str',
1564+ '*backup-id': 'str',
1565+ '*backup-time': 'int',
1566+ '*format': 'BackupFormat',
1567+ '*config-file': 'str',
1568+ '*firewall-file': 'str',
1569+ '*devlist': 'str', '*speed': 'int' },
1570+ 'returns': 'UuidInfo' }
1571+
1572+##
1573+# @query-backup:
1574+#
1575+# Returns information about current/last backup task.
1576+#
1577+# Returns: @BackupStatus
1578+#
1579+##
1580+{ 'command': 'query-backup', 'returns': 'BackupStatus' }
1581+
1582+##
1583+# @backup-cancel:
1584+#
1585+# Cancel the current executing backup process.
1586+#
1587+# Returns: nothing on success
1588+#
1589+# Notes: This command succeeds even if there is no backup process running.
1590+#
1591+##
1592+{ 'command': 'backup-cancel' }
1593+
1594 ##
1595 # @BlockDeviceTimedStats:
1596 #
1597diff --git a/qapi/common.json b/qapi/common.json
8dca018b 1598index 7c976296f0..0690b07903 100644
6402d961
TL
1599--- a/qapi/common.json
1600+++ b/qapi/common.json
8dca018b
SR
1601@@ -197,3 +197,16 @@
1602 { 'enum': 'GrabToggleKeys',
1603 'data': [ 'ctrl-ctrl', 'alt-alt', 'shift-shift','meta-meta', 'scrolllock',
1604 'ctrl-scrolllock' ] }
6402d961
TL
1605+
1606+##
1607+# @UuidInfo:
1608+#
1609+# Guest UUID information (Universally Unique Identifier).
1610+#
1611+# @UUID: the UUID of the guest
1612+#
1613+# Since: 0.14.0
1614+#
1615+# Notes: If no UUID was specified for the guest, a null UUID is returned.
1616+##
1617+{ 'struct': 'UuidInfo', 'data': {'UUID': 'str'} }
817b7667 1618diff --git a/qapi/machine.json b/qapi/machine.json
f376b2b9 1619index a6f483af4f..6effa7ad30 100644
817b7667
SR
1620--- a/qapi/machine.json
1621+++ b/qapi/machine.json
1622@@ -4,6 +4,8 @@
1623 # This work is licensed under the terms of the GNU GPL, version 2 or later.
1624 # See the COPYING file in the top-level directory.
1625
1626+{ 'include': 'common.json' }
1627+
1628 ##
1629 # = Machines
1630 ##
8dca018b 1631@@ -229,19 +231,6 @@
6402d961 1632 ##
817b7667 1633 { 'command': 'query-target', 'returns': 'TargetInfo' }
6402d961
TL
1634
1635-##
1636-# @UuidInfo:
1637-#
1638-# Guest UUID information (Universally Unique Identifier).
1639-#
1640-# @UUID: the UUID of the guest
1641-#
8dca018b 1642-# Since: 0.14
6402d961
TL
1643-#
1644-# Notes: If no UUID was specified for the guest, a null UUID is returned.
1645-##
1646-{ 'struct': 'UuidInfo', 'data': {'UUID': 'str'} }
1647-
1648 ##
1649 # @query-uuid:
1650 #