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