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