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