]> git.proxmox.com Git - pve-qemu.git/blame - debian/patches/pve/0021-PVE-backup-introduce-vma-archive-format.patch
merge: make file locking optional also on creation
[pve-qemu.git] / debian / patches / pve / 0021-PVE-backup-introduce-vma-archive-format.patch
CommitLineData
23102ed6 1From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
67af0fa4
WB
2From: Wolfgang Bumiller <w.bumiller@proxmox.com>
3Date: Wed, 2 Aug 2017 13:51:02 +0200
53e83913 4Subject: [PATCH] PVE: backup: introduce vma archive format
67af0fa4 5
53e83913 6TODO: Move to a libvma block backend.
67af0fa4
WB
7---
8 MAINTAINERS | 6 +
9 block/Makefile.objs | 3 +
0775f12b 10 block/vma.c | 503 +++++++++++++++++++++++++++++++++++++++++++++++
6838f038 11 blockdev.c | 536 +++++++++++++++++++++++++++++++++++++++++++++++++++
53e83913 12 configure | 29 +++
67af0fa4 13 hmp-commands-info.hx | 13 ++
6838f038
WB
14 hmp-commands.hx | 31 +++
15 hmp.c | 63 ++++++
67af0fa4 16 hmp.h | 3 +
6838f038 17 qapi/block-core.json | 109 ++++++++++-
53e83913
WB
18 qapi/common.json | 13 ++
19 qapi/misc.json | 13 --
0775f12b 20 12 files changed, 1308 insertions(+), 14 deletions(-)
67af0fa4
WB
21 create mode 100644 block/vma.c
22
23diff --git a/MAINTAINERS b/MAINTAINERS
53e83913 24index 666e936812..299a73cd86 100644
67af0fa4
WB
25--- a/MAINTAINERS
26+++ b/MAINTAINERS
53e83913 27@@ -2140,6 +2140,12 @@ L: qemu-block@nongnu.org
67af0fa4
WB
28 S: Supported
29 F: block/vvfat.c
30
31+VMA
32+M: Wolfgang Bumiller <w.bumiller@proxmox.com>.
33+L: pve-devel@proxmox.com
34+S: Supported
35+F: block/vma.c
36+
37 Image format fuzzer
38 M: Stefan Hajnoczi <stefanha@redhat.com>
39 L: qemu-block@nongnu.org
40diff --git a/block/Makefile.objs b/block/Makefile.objs
53e83913 41index c00f0b32d6..abfd0f69d7 100644
67af0fa4
WB
42--- a/block/Makefile.objs
43+++ b/block/Makefile.objs
53e83913 44@@ -24,6 +24,7 @@ block-obj-$(CONFIG_RBD) += rbd.o
67af0fa4 45 block-obj-$(CONFIG_GLUSTERFS) += gluster.o
6838f038 46 block-obj-$(CONFIG_VXHS) += vxhs.o
67af0fa4
WB
47 block-obj-$(CONFIG_LIBSSH2) += ssh.o
48+block-obj-$(CONFIG_VMA) += vma.o
49 block-obj-y += accounting.o dirty-bitmap.o
50 block-obj-y += write-threshold.o
51 block-obj-y += backup.o
53e83913 52@@ -52,3 +53,5 @@ qcow.o-libs := -lz
67af0fa4 53 linux-aio.o-libs := -laio
53e83913
WB
54 parallels.o-cflags := $(LIBXML2_CFLAGS)
55 parallels.o-libs := $(LIBXML2_LIBS)
67af0fa4
WB
56+vma.o-cflags := $(VMA_CFLAGS)
57+vma.o-libs := $(VMA_LIBS)
58diff --git a/block/vma.c b/block/vma.c
59new file mode 100644
0775f12b 60index 0000000000..b911b198dc
67af0fa4
WB
61--- /dev/null
62+++ b/block/vma.c
0775f12b 63@@ -0,0 +1,503 @@
67af0fa4
WB
64+/*
65+ * VMA archive backend for QEMU, container object
66+ *
67+ * Copyright (C) 2017 Proxmox Server Solutions GmbH
68+ *
69+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
70+ * See the COPYING file in the top-level directory.
71+ *
72+ */
73+#include <vma/vma.h>
74+
75+#include "qemu/osdep.h"
76+#include "qemu/uuid.h"
0775f12b 77+#include "qemu/option.h"
67af0fa4
WB
78+#include "qemu-common.h"
79+#include "qapi/error.h"
80+#include "qapi/qmp/qerror.h"
81+#include "qapi/qmp/qstring.h"
0775f12b 82+#include "qapi/qmp/qdict.h"
67af0fa4
WB
83+#include "qom/object.h"
84+#include "qom/object_interfaces.h"
85+#include "block/block_int.h"
86+
87+/* exported interface */
88+void vma_object_add_config_file(Object *obj, const char *name,
89+ const char *contents, size_t len,
90+ Error **errp);
91+
92+#define TYPE_VMA_OBJECT "vma"
93+#define VMA_OBJECT(obj) \
94+ OBJECT_CHECK(VMAObjectState, (obj), TYPE_VMA_OBJECT)
95+#define VMA_OBJECT_GET_CLASS(obj) \
96+ OBJECT_GET_CLASS(VMAObjectClass, (obj), TYPE_VMA_OBJECT)
97+
98+typedef struct VMAObjectClass {
99+ ObjectClass parent_class;
100+} VMAObjectClass;
101+
102+typedef struct VMAObjectState {
103+ Object parent;
104+
105+ char *filename;
106+
107+ QemuUUID uuid;
108+ bool blocked;
109+ VMAWriter *vma;
110+ QemuMutex mutex;
111+} VMAObjectState;
112+
113+static VMAObjectState *vma_by_id(const char *name)
114+{
115+ Object *container;
116+ Object *obj;
117+
118+ container = object_get_objects_root();
119+ obj = object_resolve_path_component(container, name);
120+
121+ return VMA_OBJECT(obj);
122+}
123+
124+static void vma_object_class_complete(UserCreatable *uc, Error **errp)
125+{
126+ int rc;
127+ VMAObjectState *vo = VMA_OBJECT(uc);
128+ VMAObjectClass *voc = VMA_OBJECT_GET_CLASS(uc);
129+ (void)!vo;
130+ (void)!voc;
131+
132+ if (!vo->filename) {
133+ error_setg(errp, "Parameter 'filename' is required");
134+ return;
135+ }
136+
0775f12b
WB
137+ vo->vma = VMAWriter_fopen(vo->filename);
138+ if (!vo->vma) {
139+ error_setg_errno(errp, errno, "failed to create VMA archive");
67af0fa4
WB
140+ return;
141+ }
142+
143+ rc = VMAWriter_set_uuid(vo->vma, vo->uuid.data, sizeof(vo->uuid.data));
144+ if (rc < 0) {
145+ error_setg_errno(errp, -rc, "failed to set UUID of VMA archive");
146+ return;
147+ }
148+
149+ qemu_mutex_init(&vo->mutex);
150+}
151+
0775f12b 152+static bool vma_object_can_be_deleted(UserCreatable *uc)
67af0fa4
WB
153+{
154+ //VMAObjectState *vo = VMA_OBJECT(uc);
155+ //if (!vo->vma) {
156+ // return true;
157+ //}
158+ //return false;
159+ return true;
160+}
161+
162+static void vma_object_class_init(ObjectClass *oc, void *data)
163+{
164+ UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
165+
166+ ucc->can_be_deleted = vma_object_can_be_deleted;
167+ ucc->complete = vma_object_class_complete;
168+}
169+
170+static char *vma_object_get_filename(Object *obj, Error **errp)
171+{
172+ VMAObjectState *vo = VMA_OBJECT(obj);
173+
174+ return g_strdup(vo->filename);
175+}
176+
177+static void vma_object_set_filename(Object *obj, const char *str, Error **errp)
178+{
179+ VMAObjectState *vo = VMA_OBJECT(obj);
180+
181+ if (vo->vma) {
182+ error_setg(errp, "filename cannot be changed after creation");
183+ return;
184+ }
185+
186+ g_free(vo->filename);
187+ vo->filename = g_strdup(str);
188+}
189+
190+static char *vma_object_get_uuid(Object *obj, Error **errp)
191+{
192+ VMAObjectState *vo = VMA_OBJECT(obj);
193+
194+ return qemu_uuid_unparse_strdup(&vo->uuid);
195+}
196+
197+static void vma_object_set_uuid(Object *obj, const char *str, Error **errp)
198+{
199+ VMAObjectState *vo = VMA_OBJECT(obj);
200+
201+ if (vo->vma) {
202+ error_setg(errp, "uuid cannot be changed after creation");
203+ return;
204+ }
205+
206+ qemu_uuid_parse(str, &vo->uuid);
207+}
208+
209+static bool vma_object_get_blocked(Object *obj, Error **errp)
210+{
211+ VMAObjectState *vo = VMA_OBJECT(obj);
212+
213+ return vo->blocked;
214+}
215+
216+static void vma_object_set_blocked(Object *obj, bool blocked, Error **errp)
217+{
218+ VMAObjectState *vo = VMA_OBJECT(obj);
219+
220+ (void)errp;
221+
222+ vo->blocked = blocked;
223+}
224+
225+void vma_object_add_config_file(Object *obj, const char *name,
226+ const char *contents, size_t len,
227+ Error **errp)
228+{
229+ int rc;
230+ VMAObjectState *vo = VMA_OBJECT(obj);
231+
232+ if (!vo || !vo->vma) {
233+ error_setg(errp, "not a valid vma object to add config files to");
234+ return;
235+ }
236+
237+ rc = VMAWriter_addConfigFile(vo->vma, name, contents, len);
238+ if (rc < 0) {
239+ error_setg_errno(errp, -rc, "failed to add config file to VMA");
240+ return;
241+ }
242+}
243+
244+static void vma_object_init(Object *obj)
245+{
246+ VMAObjectState *vo = VMA_OBJECT(obj);
247+ (void)!vo;
248+
249+ object_property_add_str(obj, "filename",
250+ vma_object_get_filename, vma_object_set_filename,
251+ NULL);
252+ object_property_add_str(obj, "uuid",
253+ vma_object_get_uuid, vma_object_set_uuid,
254+ NULL);
255+ object_property_add_bool(obj, "blocked",
256+ vma_object_get_blocked, vma_object_set_blocked,
257+ NULL);
258+}
259+
260+static void vma_object_finalize(Object *obj)
261+{
262+ VMAObjectState *vo = VMA_OBJECT(obj);
263+ VMAObjectClass *voc = VMA_OBJECT_GET_CLASS(obj);
264+ (void)!voc;
265+
266+ qemu_mutex_destroy(&vo->mutex);
267+
268+ VMAWriter_destroy(vo->vma, true);
269+ g_free(vo->filename);
270+}
271+
272+static const TypeInfo vma_object_info = {
273+ .name = TYPE_VMA_OBJECT,
274+ .parent = TYPE_OBJECT,
275+ .class_size = sizeof(VMAObjectClass),
276+ .class_init = vma_object_class_init,
277+ .instance_size = sizeof(VMAObjectState),
278+ .instance_init = vma_object_init,
279+ .instance_finalize = vma_object_finalize,
280+ .interfaces = (InterfaceInfo[]) {
281+ { TYPE_USER_CREATABLE },
282+ { }
283+ }
284+};
285+
286+static void register_types(void)
287+{
288+ type_register_static(&vma_object_info);
289+}
290+
291+type_init(register_types);
292+
293+typedef struct {
294+ VMAObjectState *vma_obj;
295+ char *name;
296+ size_t device_id;
297+ uint64_t byte_size;
298+} BDRVVMAState;
299+
300+static void qemu_vma_parse_filename(const char *filename, QDict *options,
301+ Error **errp)
302+{
303+ char *sep;
304+
0775f12b
WB
305+ if (strncmp(filename, "vma:", sizeof("vma:")-1) == 0) {
306+ filename += sizeof("vma:")-1;
307+ }
308+
67af0fa4
WB
309+ sep = strchr(filename, '/');
310+ if (!sep || sep == filename) {
0775f12b 311+ error_setg(errp, "VMA file should be <vma-obj>/<name>/<size>");
67af0fa4
WB
312+ return;
313+ }
314+
0775f12b 315+ qdict_put(options, "vma", qstring_from_substr(filename, 0, sep-filename));
67af0fa4
WB
316+
317+ while (*sep && *sep == '/')
318+ ++sep;
319+ if (!*sep) {
320+ error_setg(errp, "missing device name\n");
321+ return;
322+ }
323+
0775f12b
WB
324+ filename = sep;
325+ sep = strchr(filename, '/');
326+ if (!sep || sep == filename) {
327+ error_setg(errp, "VMA file should be <vma-obj>/<name>/<size>");
328+ return;
329+ }
330+
331+ qdict_put(options, "name", qstring_from_substr(filename, 0, sep-filename));
332+
333+ while (*sep && *sep == '/')
334+ ++sep;
335+ if (!*sep) {
336+ error_setg(errp, "missing device size\n");
337+ return;
338+ }
339+
340+ filename = sep;
341+ qdict_put_str(options, "size", filename);
67af0fa4
WB
342+}
343+
344+static QemuOptsList runtime_opts = {
345+ .name = "vma-drive",
346+ .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
347+ .desc = {
348+ {
349+ .name = "vma",
350+ .type = QEMU_OPT_STRING,
351+ .help = "VMA Object name",
352+ },
353+ {
354+ .name = "name",
355+ .type = QEMU_OPT_STRING,
356+ .help = "VMA device name",
357+ },
358+ {
359+ .name = BLOCK_OPT_SIZE,
360+ .type = QEMU_OPT_SIZE,
361+ .help = "Virtual disk size"
362+ },
363+ { /* end of list */ }
364+ },
365+};
366+static int qemu_vma_open(BlockDriverState *bs, QDict *options, int flags,
367+ Error **errp)
368+{
369+ Error *local_err = NULL;
370+ BDRVVMAState *s = bs->opaque;
371+ QemuOpts *opts;
372+ const char *vma_id, *device_name;
373+ ssize_t dev_id;
374+ int64_t bytes = 0;
375+ int ret;
376+
377+ opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
378+ qemu_opts_absorb_qdict(opts, options, &local_err);
379+ if (local_err) {
380+ error_propagate(errp, local_err);
381+ ret = -EINVAL;
382+ goto failed_opts;
383+ }
384+
385+ bytes = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
386+ BDRV_SECTOR_SIZE);
387+
388+ vma_id = qemu_opt_get(opts, "vma");
0775f12b
WB
389+ if (!vma_id) {
390+ ret = -EINVAL;
391+ error_setg(errp, "missing 'vma' property");
392+ goto failed_opts;
393+ }
394+
67af0fa4 395+ device_name = qemu_opt_get(opts, "name");
0775f12b
WB
396+ if (!device_name) {
397+ ret = -EINVAL;
398+ error_setg(errp, "missing 'name' property");
399+ goto failed_opts;
400+ }
67af0fa4
WB
401+
402+ VMAObjectState *vma = vma_by_id(vma_id);
403+ if (!vma) {
404+ ret = -EINVAL;
405+ error_setg(errp, "no such VMA object: %s", vma_id);
406+ goto failed_opts;
407+ }
408+
409+ dev_id = VMAWriter_findDevice(vma->vma, device_name);
410+ if (dev_id >= 0) {
411+ error_setg(errp, "drive already exists in VMA object");
412+ ret = -EIO;
413+ goto failed_opts;
414+ }
415+
416+ dev_id = VMAWriter_addDevice(vma->vma, device_name, (uint64_t)bytes);
417+ if (dev_id < 0) {
418+ error_setg_errno(errp, -dev_id, "failed to add VMA device");
419+ ret = -EIO;
420+ goto failed_opts;
421+ }
422+
423+ object_ref(OBJECT(vma));
424+ s->vma_obj = vma;
425+ s->name = g_strdup(device_name);
426+ s->device_id = (size_t)dev_id;
427+ s->byte_size = bytes;
428+
429+ ret = 0;
430+
431+failed_opts:
432+ qemu_opts_del(opts);
433+ return ret;
434+}
435+
436+static void qemu_vma_close(BlockDriverState *bs)
437+{
438+ BDRVVMAState *s = bs->opaque;
439+
440+ (void)VMAWriter_finishDevice(s->vma_obj->vma, s->device_id);
441+ object_unref(OBJECT(s->vma_obj));
442+
443+ g_free(s->name);
444+}
445+
446+static int64_t qemu_vma_getlength(BlockDriverState *bs)
447+{
448+ BDRVVMAState *s = bs->opaque;
449+
450+ return s->byte_size;
451+}
452+
453+static coroutine_fn int qemu_vma_co_writev(BlockDriverState *bs,
454+ int64_t sector_num,
455+ int nb_sectors,
0775f12b
WB
456+ QEMUIOVector *qiov,
457+ int flags)
67af0fa4
WB
458+{
459+ size_t i;
460+ ssize_t rc;
461+ BDRVVMAState *s = bs->opaque;
462+ VMAObjectState *vo = s->vma_obj;
463+ off_t offset = sector_num * BDRV_SECTOR_SIZE;
0775f12b
WB
464+ /* flags can be only values we set in supported_write_flags */
465+ assert(flags == 0);
67af0fa4
WB
466+
467+ qemu_mutex_lock(&vo->mutex);
468+ if (vo->blocked) {
469+ return -EPERM;
470+ }
471+ for (i = 0; i != qiov->niov; ++i) {
472+ const struct iovec *v = &qiov->iov[i];
473+ size_t blocks = v->iov_len / VMA_BLOCK_SIZE;
474+ if (blocks * VMA_BLOCK_SIZE != v->iov_len) {
475+ return -EIO;
476+ }
477+ rc = VMAWriter_writeBlocks(vo->vma, s->device_id,
478+ v->iov_base, blocks, offset);
479+ if (errno) {
480+ return -errno;
481+ }
482+ if (rc != blocks) {
483+ return -EIO;
484+ }
485+ offset += v->iov_len;
486+ }
487+ qemu_mutex_unlock(&vo->mutex);
488+ return 0;
489+}
490+
491+static int qemu_vma_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
492+{
493+ bdi->cluster_size = VMA_CLUSTER_SIZE;
494+ bdi->unallocated_blocks_are_zero = true;
67af0fa4
WB
495+ return 0;
496+}
497+
0775f12b
WB
498+static int qemu_vma_check_perm(BlockDriverState *bs,
499+ uint64_t perm,
500+ uint64_t shared,
501+ Error **errp)
502+{
503+ /* Nothing to do. */
504+ return 0;
505+}
506+
507+static void qemu_vma_set_perm(BlockDriverState *bs,
508+ uint64_t perm,
509+ uint64_t shared)
510+{
511+ /* Nothing to do. */
512+}
513+
514+static void qemu_vma_abort_perm_update(BlockDriverState *bs)
515+{
516+ /* Nothing to do. */
517+}
518+
519+static void qemu_vma_refresh_limits(BlockDriverState *bs, Error **errp)
520+{
521+ bs->bl.request_alignment = BDRV_SECTOR_SIZE; /* No sub-sector I/O */
522+}
523+static void qemu_vma_child_perm(BlockDriverState *bs, BdrvChild *c,
524+ const BdrvChildRole *role,
525+ BlockReopenQueue *reopen_queue,
526+ uint64_t perm, uint64_t shared,
527+ uint64_t *nperm, uint64_t *nshared)
528+{
529+ *nperm = BLK_PERM_ALL;
530+ *nshared = BLK_PERM_ALL;
531+}
532+
67af0fa4
WB
533+static BlockDriver bdrv_vma_drive = {
534+ .format_name = "vma-drive",
0775f12b 535+ .protocol_name = "vma",
67af0fa4
WB
536+ .instance_size = sizeof(BDRVVMAState),
537+
538+#if 0
539+ .bdrv_create = qemu_vma_create,
540+ .create_opts = &qemu_vma_create_opts,
541+#endif
542+
543+ .bdrv_parse_filename = qemu_vma_parse_filename,
544+ .bdrv_file_open = qemu_vma_open,
545+
546+ .bdrv_close = qemu_vma_close,
547+ .bdrv_has_zero_init = bdrv_has_zero_init_1,
548+ .bdrv_getlength = qemu_vma_getlength,
549+ .bdrv_get_info = qemu_vma_get_info,
550+
0775f12b 551+ //.bdrv_co_preadv = qemu_vma_co_preadv,
67af0fa4 552+ .bdrv_co_writev = qemu_vma_co_writev,
0775f12b
WB
553+
554+ .bdrv_refresh_limits = qemu_vma_refresh_limits,
555+ .bdrv_check_perm = qemu_vma_check_perm,
556+ .bdrv_set_perm = qemu_vma_set_perm,
557+ .bdrv_abort_perm_update = qemu_vma_abort_perm_update,
558+ .bdrv_child_perm = qemu_vma_child_perm,
67af0fa4
WB
559+};
560+
561+static void bdrv_vma_init(void)
562+{
563+ bdrv_register(&bdrv_vma_drive);
564+}
565+
566+block_init(bdrv_vma_init);
567diff --git a/blockdev.c b/blockdev.c
53e83913 568index d5eb6b62ca..4f18d3c3d7 100644
67af0fa4
WB
569--- a/blockdev.c
570+++ b/blockdev.c
53e83913 571@@ -31,11 +31,13 @@
67af0fa4
WB
572 */
573
574 #include "qemu/osdep.h"
575+#include "qemu/uuid.h"
576 #include "sysemu/block-backend.h"
577 #include "sysemu/blockdev.h"
578 #include "hw/block/block.h"
579 #include "block/blockjob.h"
53e83913 580 #include "block/qdict.h"
67af0fa4
WB
581+#include "block/blockjob_int.h"
582 #include "block/throttle-groups.h"
583 #include "monitor/monitor.h"
584 #include "qemu/error-report.h"
53e83913
WB
585@@ -44,6 +46,7 @@
586 #include "qapi/qapi-commands-block.h"
587 #include "qapi/qapi-commands-transaction.h"
588 #include "qapi/qapi-visit-block-core.h"
589+#include "qapi/qapi-types-misc.h"
590 #include "qapi/qmp/qdict.h"
591 #include "qapi/qmp/qnum.h"
592 #include "qapi/qmp/qstring.h"
593@@ -3220,6 +3223,539 @@ out:
67af0fa4
WB
594 aio_context_release(aio_context);
595 }
596
597+/* PVE backup related function */
598+
599+static struct PVEBackupState {
600+ Error *error;
601+ bool cancel;
602+ QemuUUID uuid;
603+ char uuid_str[37];
604+ int64_t speed;
605+ time_t start_time;
606+ time_t end_time;
607+ char *backup_file;
608+ Object *vmaobj;
609+ GList *di_list;
610+ size_t next_job;
611+ size_t total;
612+ size_t transferred;
613+ size_t zero_bytes;
6838f038
WB
614+ QemuMutex backup_mutex;
615+ bool backup_mutex_initialized;
67af0fa4
WB
616+} backup_state;
617+
618+typedef struct PVEBackupDevInfo {
619+ BlockDriverState *bs;
620+ size_t size;
621+ uint8_t dev_id;
622+ bool completed;
623+ char targetfile[PATH_MAX];
624+ BlockDriverState *target;
625+} PVEBackupDevInfo;
626+
627+static void pvebackup_run_next_job(void);
628+
629+static void pvebackup_cleanup(void)
630+{
6838f038
WB
631+ qemu_mutex_lock(&backup_state.backup_mutex);
632+ // Avoid race between block jobs and backup-cancel command:
633+ if (!backup_state.vmaw) {
634+ qemu_mutex_unlock(&backup_state.backup_mutex);
635+ return;
636+ }
637+
67af0fa4
WB
638+ backup_state.end_time = time(NULL);
639+
640+ if (backup_state.vmaobj) {
641+ object_unparent(backup_state.vmaobj);
642+ backup_state.vmaobj = NULL;
643+ }
644+
6838f038
WB
645+ g_list_free(backup_state.di_list);
646+ backup_state.di_list = NULL;
647+ qemu_mutex_unlock(&backup_state.backup_mutex);
67af0fa4
WB
648+}
649+
650+static void pvebackup_complete_cb(void *opaque, int ret)
651+{
6838f038
WB
652+ // This always runs in the main loop
653+
67af0fa4
WB
654+ PVEBackupDevInfo *di = opaque;
655+
656+ di->completed = true;
657+
658+ if (ret < 0 && !backup_state.error) {
659+ error_setg(&backup_state.error, "job failed with err %d - %s",
660+ ret, strerror(-ret));
661+ }
662+
663+ di->bs = NULL;
664+ di->target = NULL;
665+
666+ if (backup_state.vmaobj) {
667+ object_unparent(backup_state.vmaobj);
668+ backup_state.vmaobj = NULL;
669+ }
670+
6838f038
WB
671+ // remove self from job queue
672+ qemu_mutex_lock(&backup_state.backup_mutex);
673+ backup_state.di_list = g_list_remove(backup_state.di_list, di);
674+ g_free(di);
675+ qemu_mutex_unlock(&backup_state.backup_mutex);
676+
67af0fa4
WB
677+ if (!backup_state.cancel) {
678+ pvebackup_run_next_job();
679+ }
680+}
681+
682+static void pvebackup_cancel(void *opaque)
683+{
684+ backup_state.cancel = true;
6838f038
WB
685+ qemu_mutex_lock(&backup_state.backup_mutex);
686+ // Avoid race between block jobs and backup-cancel command:
687+ if (!backup_state.vmaw) {
688+ qemu_mutex_unlock(&backup_state.backup_mutex);
689+ return;
690+ }
67af0fa4
WB
691+
692+ if (!backup_state.error) {
693+ error_setg(&backup_state.error, "backup cancelled");
694+ }
695+
696+ if (backup_state.vmaobj) {
697+ Error *err;
698+ /* make sure vma writer does not block anymore */
699+ if (!object_set_props(backup_state.vmaobj, &err, "blocked", "yes", NULL)) {
700+ if (err) {
701+ error_report_err(err);
702+ }
703+ }
704+ }
705+
706+ GList *l = backup_state.di_list;
707+ while (l) {
708+ PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
709+ l = g_list_next(l);
710+ if (!di->completed && di->bs) {
711+ BlockJob *job = di->bs->job;
712+ if (job) {
6838f038
WB
713+ AioContext *aio_context = blk_get_aio_context(job->blk);
714+ aio_context_acquire(aio_context);
67af0fa4 715+ if (!di->completed) {
53e83913 716+ job_cancel(&job->job, false);
67af0fa4 717+ }
6838f038 718+ aio_context_release(aio_context);
67af0fa4
WB
719+ }
720+ }
721+ }
722+
6838f038 723+ qemu_mutex_unlock(&backup_state.backup_mutex);
67af0fa4
WB
724+ pvebackup_cleanup();
725+}
726+
727+void qmp_backup_cancel(Error **errp)
728+{
6838f038
WB
729+ if (!backup_state.backup_mutex_initialized)
730+ return;
67af0fa4
WB
731+ Coroutine *co = qemu_coroutine_create(pvebackup_cancel, NULL);
732+ qemu_coroutine_enter(co);
733+
734+ while (backup_state.vmaobj) {
735+ /* FIXME: Find something better for this */
736+ aio_poll(qemu_get_aio_context(), true);
737+ }
738+}
739+
740+void vma_object_add_config_file(Object *obj, const char *name,
741+ const char *contents, size_t len,
742+ Error **errp);
743+static int config_to_vma(const char *file, BackupFormat format,
744+ Object *vmaobj,
745+ const char *backup_dir,
746+ Error **errp)
747+{
6838f038
WB
748+ char *cdata = NULL;
749+ gsize clen = 0;
750+ GError *err = NULL;
751+ if (!g_file_get_contents(file, &cdata, &clen, &err)) {
752+ error_setg(errp, "unable to read file '%s'", file);
753+ return 1;
754+ }
67af0fa4 755+
6838f038 756+ char *basename = g_path_get_basename(file);
67af0fa4 757+
6838f038
WB
758+ if (format == BACKUP_FORMAT_VMA) {
759+ vma_object_add_config_file(vmaobj, basename, cdata, clen, errp);
760+ } else if (format == BACKUP_FORMAT_DIR) {
761+ char config_path[PATH_MAX];
762+ snprintf(config_path, PATH_MAX, "%s/%s", backup_dir, basename);
763+ if (!g_file_set_contents(config_path, cdata, clen, &err)) {
764+ error_setg(errp, "unable to write config file '%s'", config_path);
765+ g_free(cdata);
766+ g_free(basename);
767+ return 1;
768+ }
769+ }
67af0fa4 770+
6838f038
WB
771+ g_free(basename);
772+ g_free(cdata);
773+ return 0;
67af0fa4
WB
774+}
775+
776+static void pvebackup_run_next_job(void)
777+{
6838f038
WB
778+ qemu_mutex_lock(&backup_state.backup_mutex);
779+
67af0fa4
WB
780+ GList *next = g_list_nth(backup_state.di_list, backup_state.next_job);
781+ while (next) {
782+ PVEBackupDevInfo *di = (PVEBackupDevInfo *)next->data;
783+ backup_state.next_job++;
784+ if (!di->completed && di->bs && di->bs->job) {
785+ BlockJob *job = di->bs->job;
6838f038
WB
786+ AioContext *aio_context = blk_get_aio_context(job->blk);
787+ aio_context_acquire(aio_context);
788+ qemu_mutex_unlock(&backup_state.backup_mutex);
789+ if (backup_state.error || backup_state.cancel) {
53e83913 790+ job_cancel_sync(job);
67af0fa4 791+ } else {
53e83913 792+ job_resume(job);
67af0fa4 793+ }
6838f038 794+ aio_context_release(aio_context);
67af0fa4
WB
795+ return;
796+ }
797+ next = g_list_next(next);
798+ }
6838f038
WB
799+ qemu_mutex_unlock(&backup_state.backup_mutex);
800+
801+ // no more jobs, run the cleanup
67af0fa4
WB
802+ pvebackup_cleanup();
803+}
804+
805+UuidInfo *qmp_backup(const char *backup_file, bool has_format,
806+ BackupFormat format,
807+ bool has_config_file, const char *config_file,
808+ bool has_firewall_file, const char *firewall_file,
809+ bool has_devlist, const char *devlist,
810+ bool has_speed, int64_t speed, Error **errp)
811+{
812+ BlockBackend *blk;
813+ BlockDriverState *bs = NULL;
814+ const char *backup_dir = NULL;
815+ Error *local_err = NULL;
816+ QemuUUID uuid;
817+ gchar **devs = NULL;
818+ GList *di_list = NULL;
819+ GList *l;
820+ UuidInfo *uuid_info;
821+ BlockJob *job;
822+
6838f038
WB
823+ if (!backup_state.backup_mutex_initialized) {
824+ qemu_mutex_init(&backup_state.backup_mutex);
825+ backup_state.backup_mutex_initialized = true;
826+ }
827+
67af0fa4
WB
828+ if (backup_state.di_list || backup_state.vmaobj) {
829+ error_set(errp, ERROR_CLASS_GENERIC_ERROR,
830+ "previous backup not finished");
831+ return NULL;
832+ }
833+
834+ /* Todo: try to auto-detect format based on file name */
835+ format = has_format ? format : BACKUP_FORMAT_VMA;
836+
837+ if (has_devlist) {
838+ devs = g_strsplit_set(devlist, ",;:", -1);
839+
840+ gchar **d = devs;
841+ while (d && *d) {
842+ blk = blk_by_name(*d);
843+ if (blk) {
844+ bs = blk_bs(blk);
845+ if (bdrv_is_read_only(bs)) {
846+ error_setg(errp, "Node '%s' is read only", *d);
847+ goto err;
848+ }
849+ if (!bdrv_is_inserted(bs)) {
850+ error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, *d);
851+ goto err;
852+ }
853+ PVEBackupDevInfo *di = g_new0(PVEBackupDevInfo, 1);
854+ di->bs = bs;
855+ di_list = g_list_append(di_list, di);
856+ } else {
857+ error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
858+ "Device '%s' not found", *d);
859+ goto err;
860+ }
861+ d++;
862+ }
863+
864+ } else {
865+ BdrvNextIterator it;
866+
867+ bs = NULL;
868+ for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
869+ if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
870+ continue;
871+ }
872+
873+ PVEBackupDevInfo *di = g_new0(PVEBackupDevInfo, 1);
874+ di->bs = bs;
875+ di_list = g_list_append(di_list, di);
876+ }
877+ }
878+
879+ if (!di_list) {
880+ error_set(errp, ERROR_CLASS_GENERIC_ERROR, "empty device list");
881+ goto err;
882+ }
883+
884+ size_t total = 0;
885+
886+ l = di_list;
887+ while (l) {
888+ PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
889+ l = g_list_next(l);
890+ if (bdrv_op_is_blocked(di->bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
891+ goto err;
892+ }
893+
894+ ssize_t size = bdrv_getlength(di->bs);
895+ if (size < 0) {
896+ error_setg_errno(errp, -di->size, "bdrv_getlength failed");
897+ goto err;
898+ }
899+ di->size = size;
900+ total += size;
901+ }
902+
903+ qemu_uuid_generate(&uuid);
904+
905+ if (format == BACKUP_FORMAT_VMA) {
906+ char uuidstr[UUID_FMT_LEN+1];
907+ qemu_uuid_unparse(&uuid, uuidstr);
908+ uuidstr[UUID_FMT_LEN] = 0;
909+ backup_state.vmaobj =
910+ object_new_with_props("vma", object_get_objects_root(),
911+ "vma-backup-obj", &local_err,
912+ "filename", backup_file,
913+ "uuid", uuidstr,
914+ NULL);
915+ if (!backup_state.vmaobj) {
916+ if (local_err) {
917+ error_propagate(errp, local_err);
918+ }
919+ goto err;
920+ }
921+
922+ l = di_list;
923+ while (l) {
924+ QDict *options = qdict_new();
925+
926+ PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
927+ l = g_list_next(l);
928+
929+ const char *devname = bdrv_get_device_name(di->bs);
930+ snprintf(di->targetfile, PATH_MAX, "vma-backup-obj/%s.raw", devname);
931+
932+ qdict_put(options, "driver", qstring_from_str("vma-drive"));
933+ qdict_put(options, "size", qint_from_int(di->size));
934+ di->target = bdrv_open(di->targetfile, NULL, options, BDRV_O_RDWR, &local_err);
935+ if (!di->target) {
936+ error_propagate(errp, local_err);
937+ goto err;
938+ }
939+ }
940+ } else if (format == BACKUP_FORMAT_DIR) {
941+ if (mkdir(backup_file, 0640) != 0) {
942+ error_setg_errno(errp, errno, "can't create directory '%s'\n",
943+ backup_file);
944+ goto err;
945+ }
946+ backup_dir = backup_file;
947+
948+ l = di_list;
949+ while (l) {
950+ PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
951+ l = g_list_next(l);
952+
953+ const char *devname = bdrv_get_device_name(di->bs);
954+ snprintf(di->targetfile, PATH_MAX, "%s/%s.raw", backup_dir, devname);
955+
956+ int flags = BDRV_O_RDWR;
957+ bdrv_img_create(di->targetfile, "raw", NULL, NULL, NULL,
6838f038 958+ di->size, flags, false, &local_err);
67af0fa4
WB
959+ if (local_err) {
960+ error_propagate(errp, local_err);
961+ goto err;
962+ }
963+
964+ di->target = bdrv_open(di->targetfile, NULL, NULL, flags, &local_err);
965+ if (!di->target) {
966+ error_propagate(errp, local_err);
967+ goto err;
968+ }
969+ }
970+ } else {
6838f038
WB
971+ error_set(errp, ERROR_CLASS_GENERIC_ERROR, "unknown backup format");
972+ goto err;
67af0fa4
WB
973+ }
974+
975+ /* add configuration file to archive */
976+ if (has_config_file) {
6838f038
WB
977+ if(config_to_vma(config_file, format, backup_state.vmaobj, backup_dir, errp) != 0) {
978+ goto err;
979+ }
67af0fa4
WB
980+ }
981+
982+ /* add firewall file to archive */
983+ if (has_firewall_file) {
6838f038
WB
984+ if(config_to_vma(firewall_file, format, backup_state.vmaobj, backup_dir, errp) != 0) {
985+ goto err;
986+ }
67af0fa4
WB
987+ }
988+ /* initialize global backup_state now */
989+
990+ backup_state.cancel = false;
991+
992+ if (backup_state.error) {
993+ error_free(backup_state.error);
994+ backup_state.error = NULL;
995+ }
996+
997+ backup_state.speed = (has_speed && speed > 0) ? speed : 0;
998+
999+ backup_state.start_time = time(NULL);
1000+ backup_state.end_time = 0;
1001+
1002+ if (backup_state.backup_file) {
1003+ g_free(backup_state.backup_file);
1004+ }
1005+ backup_state.backup_file = g_strdup(backup_file);
1006+
1007+ memcpy(&backup_state.uuid, &uuid, sizeof(uuid));
1008+ qemu_uuid_unparse(&uuid, backup_state.uuid_str);
1009+
6838f038 1010+ qemu_mutex_lock(&backup_state.backup_mutex);
67af0fa4
WB
1011+ backup_state.di_list = di_list;
1012+ backup_state.next_job = 0;
1013+
1014+ backup_state.total = total;
1015+ backup_state.transferred = 0;
1016+ backup_state.zero_bytes = 0;
1017+
1018+ /* start all jobs (paused state) */
1019+ l = di_list;
1020+ while (l) {
1021+ PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
1022+ l = g_list_next(l);
1023+
1024+ job = backup_job_create(NULL, di->bs, di->target, speed, MIRROR_SYNC_MODE_FULL, NULL,
1025+ false, BLOCKDEV_ON_ERROR_REPORT, BLOCKDEV_ON_ERROR_REPORT,
53e83913 1026+ JOB_DEFAULT,
67af0fa4
WB
1027+ pvebackup_complete_cb, di, 2, NULL, &local_err);
1028+ if (di->target) {
1029+ bdrv_unref(di->target);
1030+ di->target = NULL;
1031+ }
1032+ if (!job || local_err != NULL) {
1033+ error_setg(&backup_state.error, "backup_job_create failed");
1034+ pvebackup_cancel(NULL);
1035+ } else {
53e83913 1036+ job_start(&job->job);
67af0fa4
WB
1037+ }
1038+ }
1039+
6838f038
WB
1040+ qemu_mutex_unlock(&backup_state.backup_mutex);
1041+
67af0fa4
WB
1042+ if (!backup_state.error) {
1043+ pvebackup_run_next_job(); // run one job
1044+ }
1045+
1046+ uuid_info = g_malloc0(sizeof(*uuid_info));
1047+ uuid_info->UUID = g_strdup(backup_state.uuid_str);
1048+
1049+ return uuid_info;
1050+
1051+err:
1052+
1053+ l = di_list;
1054+ while (l) {
1055+ PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
1056+ l = g_list_next(l);
1057+
1058+ if (di->target) {
1059+ bdrv_unref(di->target);
1060+ }
1061+
1062+ if (di->targetfile[0]) {
1063+ unlink(di->targetfile);
1064+ }
1065+ g_free(di);
1066+ }
1067+ g_list_free(di_list);
1068+
1069+ if (devs) {
1070+ g_strfreev(devs);
1071+ }
1072+
1073+ if (backup_state.vmaobj) {
1074+ object_unparent(backup_state.vmaobj);
1075+ backup_state.vmaobj = NULL;
1076+ }
1077+
1078+ if (backup_dir) {
1079+ rmdir(backup_dir);
1080+ }
1081+
1082+ return NULL;
1083+}
1084+
1085+BackupStatus *qmp_query_backup(Error **errp)
1086+{
1087+ BackupStatus *info = g_malloc0(sizeof(*info));
1088+
1089+ if (!backup_state.start_time) {
1090+ /* not started, return {} */
1091+ return info;
1092+ }
1093+
1094+ info->has_status = true;
1095+ info->has_start_time = true;
1096+ info->start_time = backup_state.start_time;
1097+
1098+ if (backup_state.backup_file) {
1099+ info->has_backup_file = true;
1100+ info->backup_file = g_strdup(backup_state.backup_file);
1101+ }
1102+
1103+ info->has_uuid = true;
1104+ info->uuid = g_strdup(backup_state.uuid_str);
1105+
1106+ if (backup_state.end_time) {
1107+ if (backup_state.error) {
1108+ info->status = g_strdup("error");
1109+ info->has_errmsg = true;
1110+ info->errmsg = g_strdup(error_get_pretty(backup_state.error));
1111+ } else {
1112+ info->status = g_strdup("done");
1113+ }
1114+ info->has_end_time = true;
1115+ info->end_time = backup_state.end_time;
1116+ } else {
1117+ info->status = g_strdup("active");
1118+ }
1119+
1120+ info->has_total = true;
1121+ info->total = backup_state.total;
1122+ info->has_zero_bytes = true;
1123+ info->zero_bytes = backup_state.zero_bytes;
1124+ info->has_transferred = true;
1125+ info->transferred = backup_state.transferred;
1126+
1127+ return info;
1128+}
1129+
1130 void qmp_block_stream(bool has_job_id, const char *job_id, const char *device,
1131 bool has_base, const char *base,
1132 bool has_base_node, const char *base_node,
1133diff --git a/configure b/configure
0775f12b 1134index 7b3f80a49c..d2cc11cdbb 100755
67af0fa4
WB
1135--- a/configure
1136+++ b/configure
53e83913
WB
1137@@ -475,6 +475,7 @@ vxhs=""
1138 libxml2=""
1139 docker="no"
1140 debug_mutex="no"
67af0fa4
WB
1141+vma=""
1142
53e83913
WB
1143 # cross compilers defaults, can be overridden with --cross-cc-ARCH
1144 cross_cc_aarch64="aarch64-linux-gnu-gcc"
1145@@ -1435,6 +1436,10 @@ for opt do
67af0fa4 1146 ;;
53e83913 1147 --disable-debug-mutex) debug_mutex=no
67af0fa4 1148 ;;
53e83913 1149+ --enable-vma) vma=yes
67af0fa4 1150+ ;;
53e83913 1151+ --disable-vma) vma=no
67af0fa4
WB
1152+ ;;
1153 *)
1154 echo "ERROR: unknown option $opt"
1155 echo "Try '$0 --help' for more information"
53e83913 1156@@ -1710,6 +1715,7 @@ disabled with --disable-FEATURE, default is enabled if available:
6838f038
WB
1157 vhost-user vhost-user support
1158 capstone capstone disassembler support
53e83913 1159 debug-mutex mutex debugging support
67af0fa4
WB
1160+ vma VMA archive backend
1161
1162 NOTE: The object files are built at the place where configure is launched
1163 EOF
0775f12b 1164@@ -4121,6 +4127,22 @@ EOF
67af0fa4
WB
1165 fi
1166
1167 ##########################################
1168+# vma probe
1169+if test "$vma" != "no" ; then
1170+ if $pkg_config --exact-version=0.1.0 vma; then
1171+ vma="yes"
1172+ vma_cflags=$($pkg_config --cflags vma)
1173+ vma_libs=$($pkg_config --libs vma)
1174+ else
1175+ if test "$vma" = "yes" ; then
1176+ feature_not_found "VMA Archive backend support" \
1177+ "Install libvma devel"
1178+ fi
1179+ vma="no"
1180+ fi
1181+fi
1182+
67af0fa4
WB
1183+##########################################
1184 # signalfd probe
1185 signalfd="no"
1186 cat > $TMPC << EOF
0775f12b 1187@@ -6007,6 +6029,7 @@ echo "replication support $replication"
6838f038
WB
1188 echo "VxHS block device $vxhs"
1189 echo "capstone $capstone"
53e83913 1190 echo "docker $docker"
67af0fa4
WB
1191+echo "VMA support $vma"
1192
1193 if test "$sdl_too_old" = "yes"; then
1194 echo "-> Your SDL version is too old - please upgrade to have SDL support"
0775f12b 1195@@ -6493,6 +6516,12 @@ if test "$usb_redir" = "yes" ; then
53e83913 1196 echo "USB_REDIR_LIBS=$usb_redir_libs" >> $config_host_mak
67af0fa4
WB
1197 fi
1198
1199+if test "$vma" = "yes" ; then
1200+ echo "CONFIG_VMA=y" >> $config_host_mak
1201+ echo "VMA_CFLAGS=$vma_cflags" >> $config_host_mak
1202+ echo "VMA_LIBS=$vma_libs" >> $config_host_mak
1203+fi
1204+
53e83913
WB
1205 if test "$opengl" = "yes" ; then
1206 echo "CONFIG_OPENGL=y" >> $config_host_mak
1207 echo "OPENGL_LIBS=$opengl_libs" >> $config_host_mak
67af0fa4 1208diff --git a/hmp-commands-info.hx b/hmp-commands-info.hx
53e83913 1209index 42c148fdc9..277e140092 100644
67af0fa4
WB
1210--- a/hmp-commands-info.hx
1211+++ b/hmp-commands-info.hx
53e83913 1212@@ -502,6 +502,19 @@ STEXI
67af0fa4
WB
1213 Show CPU statistics.
1214 ETEXI
1215
1216+ {
1217+ .name = "backup",
1218+ .args_type = "",
1219+ .params = "",
1220+ .help = "show backup status",
1221+ .cmd = hmp_info_backup,
1222+ },
1223+
1224+STEXI
1225+@item info backup
1226+show backup status
1227+ETEXI
1228+
1229 #if defined(CONFIG_SLIRP)
1230 {
1231 .name = "usernet",
1232diff --git a/hmp-commands.hx b/hmp-commands.hx
53e83913 1233index a6f0720442..956cbf04b9 100644
67af0fa4
WB
1234--- a/hmp-commands.hx
1235+++ b/hmp-commands.hx
53e83913 1236@@ -107,6 +107,37 @@ STEXI
67af0fa4
WB
1237 Copy data from a backing file into a block device.
1238 ETEXI
1239
1240+ {
1241+ .name = "backup",
1242+ .args_type = "directory:-d,backupfile:s,speed:o?,devlist:s?",
1243+ .params = "[-d] backupfile [speed [devlist]]",
1244+ .help = "create a VM Backup."
1245+ "\n\t\t\t Use -d to dump data into a directory instead"
1246+ "\n\t\t\t of using VMA format.",
1247+ .cmd = hmp_backup,
1248+ },
1249+
1250+STEXI
1251+@item backup
1252+@findex backup
1253+Create a VM backup.
1254+ETEXI
1255+
1256+ {
1257+ .name = "backup_cancel",
1258+ .args_type = "",
1259+ .params = "",
1260+ .help = "cancel the current VM backup",
1261+ .cmd = hmp_backup_cancel,
1262+ },
1263+
1264+STEXI
1265+@item backup_cancel
1266+@findex backup_cancel
1267+Cancel the current VM backup.
1268+
1269+ETEXI
1270+
1271 {
1272 .name = "block_job_set_speed",
1273 .args_type = "device:B,speed:o",
1274diff --git a/hmp.c b/hmp.c
53e83913 1275index 7c975f3ead..8d659e20f6 100644
67af0fa4
WB
1276--- a/hmp.c
1277+++ b/hmp.c
53e83913 1278@@ -166,6 +166,44 @@ void hmp_info_mice(Monitor *mon, const QDict *qdict)
67af0fa4
WB
1279 qapi_free_MouseInfoList(mice_list);
1280 }
1281
1282+void hmp_info_backup(Monitor *mon, const QDict *qdict)
1283+{
1284+ BackupStatus *info;
1285+
1286+ info = qmp_query_backup(NULL);
1287+ if (info->has_status) {
1288+ if (info->has_errmsg) {
1289+ monitor_printf(mon, "Backup status: %s - %s\n",
1290+ info->status, info->errmsg);
1291+ } else {
1292+ monitor_printf(mon, "Backup status: %s\n", info->status);
1293+ }
1294+ }
1295+
1296+ if (info->has_backup_file) {
1297+ monitor_printf(mon, "Start time: %s", ctime(&info->start_time));
1298+ if (info->end_time) {
1299+ monitor_printf(mon, "End time: %s", ctime(&info->end_time));
1300+ }
1301+
1302+ int per = (info->has_total && info->total &&
1303+ info->has_transferred && info->transferred) ?
1304+ (info->transferred * 100)/info->total : 0;
1305+ int zero_per = (info->has_total && info->total &&
1306+ info->has_zero_bytes && info->zero_bytes) ?
1307+ (info->zero_bytes * 100)/info->total : 0;
1308+ monitor_printf(mon, "Backup file: %s\n", info->backup_file);
1309+ monitor_printf(mon, "Backup uuid: %s\n", info->uuid);
1310+ monitor_printf(mon, "Total size: %zd\n", info->total);
1311+ monitor_printf(mon, "Transferred bytes: %zd (%d%%)\n",
1312+ info->transferred, per);
1313+ monitor_printf(mon, "Zero bytes: %zd (%d%%)\n",
1314+ info->zero_bytes, zero_per);
1315+ }
1316+
1317+ qapi_free_BackupStatus(info);
1318+}
1319+
1320 void hmp_info_migrate(Monitor *mon, const QDict *qdict)
1321 {
1322 MigrationInfo *info;
53e83913 1323@@ -1899,6 +1937,31 @@ void hmp_block_stream(Monitor *mon, const QDict *qdict)
67af0fa4
WB
1324 hmp_handle_error(mon, &error);
1325 }
1326
1327+void hmp_backup_cancel(Monitor *mon, const QDict *qdict)
1328+{
1329+ Error *error = NULL;
1330+
1331+ qmp_backup_cancel(&error);
1332+
1333+ hmp_handle_error(mon, &error);
1334+}
1335+
1336+void hmp_backup(Monitor *mon, const QDict *qdict)
1337+{
1338+ Error *error = NULL;
1339+
1340+ int dir = qdict_get_try_bool(qdict, "directory", 0);
1341+ const char *backup_file = qdict_get_str(qdict, "backupfile");
1342+ const char *devlist = qdict_get_try_str(qdict, "devlist");
1343+ int64_t speed = qdict_get_try_int(qdict, "speed", 0);
1344+
1345+ qmp_backup(backup_file, true, dir ? BACKUP_FORMAT_DIR : BACKUP_FORMAT_VMA,
1346+ false, NULL, false, NULL, !!devlist,
1347+ devlist, qdict_haskey(qdict, "speed"), speed, &error);
1348+
1349+ hmp_handle_error(mon, &error);
1350+}
1351+
1352 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
1353 {
1354 Error *error = NULL;
1355diff --git a/hmp.h b/hmp.h
53e83913 1356index 98bb7a44db..853f233195 100644
67af0fa4
WB
1357--- a/hmp.h
1358+++ b/hmp.h
53e83913 1359@@ -29,6 +29,7 @@ void hmp_info_migrate(Monitor *mon, const QDict *qdict);
67af0fa4
WB
1360 void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict);
1361 void hmp_info_migrate_parameters(Monitor *mon, const QDict *qdict);
1362 void hmp_info_migrate_cache_size(Monitor *mon, const QDict *qdict);
1363+void hmp_info_backup(Monitor *mon, const QDict *qdict);
1364 void hmp_info_cpus(Monitor *mon, const QDict *qdict);
1365 void hmp_info_block(Monitor *mon, const QDict *qdict);
1366 void hmp_info_blockstats(Monitor *mon, const QDict *qdict);
53e83913 1367@@ -86,6 +87,8 @@ void hmp_eject(Monitor *mon, const QDict *qdict);
67af0fa4
WB
1368 void hmp_change(Monitor *mon, const QDict *qdict);
1369 void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict);
1370 void hmp_block_stream(Monitor *mon, const QDict *qdict);
1371+void hmp_backup(Monitor *mon, const QDict *qdict);
1372+void hmp_backup_cancel(Monitor *mon, const QDict *qdict);
1373 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict);
1374 void hmp_block_job_cancel(Monitor *mon, const QDict *qdict);
1375 void hmp_block_job_pause(Monitor *mon, const QDict *qdict);
6838f038 1376diff --git a/qapi/block-core.json b/qapi/block-core.json
53e83913 1377index 5b9084a394..9c3c2d4917 100644
6838f038
WB
1378--- a/qapi/block-core.json
1379+++ b/qapi/block-core.json
53e83913 1380@@ -718,6 +718,97 @@
6838f038 1381
67af0fa4
WB
1382
1383 ##
1384+# @BackupStatus:
1385+#
1386+# Detailed backup status.
1387+#
1388+# @status: string describing the current backup status.
1389+# This can be 'active', 'done', 'error'. If this field is not
1390+# returned, no backup process has been initiated
1391+#
1392+# @errmsg: error message (only returned if status is 'error')
1393+#
1394+# @total: total amount of bytes involved in the backup process
1395+#
1396+# @transferred: amount of bytes already backed up.
1397+#
1398+# @zero-bytes: amount of 'zero' bytes detected.
1399+#
1400+# @start-time: time (epoch) when backup job started.
1401+#
1402+# @end-time: time (epoch) when backup job finished.
1403+#
1404+# @backup-file: backup file name
1405+#
1406+# @uuid: uuid for this backup job
1407+#
1408+##
1409+{ 'struct': 'BackupStatus',
1410+ 'data': {'*status': 'str', '*errmsg': 'str', '*total': 'int',
1411+ '*transferred': 'int', '*zero-bytes': 'int',
1412+ '*start-time': 'int', '*end-time': 'int',
1413+ '*backup-file': 'str', '*uuid': 'str' } }
1414+
1415+##
1416+# @BackupFormat:
1417+#
1418+# An enumeration of supported backup formats.
1419+#
1420+# @vma: Proxmox vma backup format
1421+##
1422+{ 'enum': 'BackupFormat',
1423+ 'data': [ 'vma', 'dir' ] }
1424+
1425+##
1426+# @backup:
1427+#
1428+# Starts a VM backup.
1429+#
1430+# @backup-file: the backup file name
1431+#
1432+# @format: format of the backup file
1433+#
1434+# @config-file: a configuration file to include into
1435+# the backup archive.
1436+#
1437+# @speed: the maximum speed, in bytes per second
1438+#
1439+# @devlist: list of block device names (separated by ',', ';'
1440+# or ':'). By default the backup includes all writable block devices.
1441+#
1442+# Returns: the uuid of the backup job
1443+#
1444+##
1445+{ 'command': 'backup', 'data': { 'backup-file': 'str',
1446+ '*format': 'BackupFormat',
1447+ '*config-file': 'str',
1448+ '*firewall-file': 'str',
1449+ '*devlist': 'str', '*speed': 'int' },
1450+ 'returns': 'UuidInfo' }
1451+
1452+##
1453+# @query-backup:
1454+#
1455+# Returns information about current/last backup task.
1456+#
1457+# Returns: @BackupStatus
1458+#
1459+##
1460+{ 'command': 'query-backup', 'returns': 'BackupStatus' }
1461+
1462+##
1463+# @backup-cancel:
1464+#
1465+# Cancel the current executing backup process.
1466+#
1467+# Returns: nothing on success
1468+#
1469+# Notes: This command succeeds even if there is no backup process running.
1470+#
1471+##
1472+{ 'command': 'backup-cancel' }
1473+
1474+##
6838f038 1475 # @BlockDeviceTimedStats:
67af0fa4 1476 #
6838f038 1477 # Statistics of a block device during a given interval of time.
53e83913
WB
1478@@ -2549,7 +2640,7 @@
1479 'host_cdrom', 'host_device', 'http', 'https', 'iscsi', 'luks',
1480 'nbd', 'nfs', 'null-aio', 'null-co', 'nvme', 'parallels', 'qcow',
1481 'qcow2', 'qed', 'quorum', 'raw', 'rbd', 'replication', 'sheepdog',
1482- 'ssh', 'throttle', 'vdi', 'vhdx', 'vmdk', 'vpc', 'vvfat', 'vxhs' ] }
1483+ 'ssh', 'throttle', 'vdi', 'vhdx', 'vma-drive', 'vmdk', 'vpc', 'vvfat', 'vxhs' ] }
67af0fa4
WB
1484
1485 ##
1486 # @BlockdevOptionsFile:
53e83913 1487@@ -3550,6 +3641,21 @@
6838f038 1488 '*tls-creds': 'str' } }
67af0fa4
WB
1489
1490 ##
1491+# @BlockdevOptionsVMADrive:
1492+#
1493+# Driver specific block device options for VMA Drives
1494+#
1495+# @filename: vma-drive path
1496+#
1497+# @size: drive size in bytes
1498+#
1499+# Since: 2.9
1500+##
1501+{ 'struct': 'BlockdevOptionsVMADrive',
1502+ 'data': { 'filename': 'str',
1503+ 'size': 'int' } }
1504+
1505+##
6838f038 1506 # @BlockdevOptionsThrottle:
67af0fa4 1507 #
6838f038 1508 # Driver specific block device options for the throttle driver
53e83913 1509@@ -3633,6 +3739,7 @@
6838f038
WB
1510 'throttle': 'BlockdevOptionsThrottle',
1511 'vdi': 'BlockdevOptionsGenericFormat',
67af0fa4 1512 'vhdx': 'BlockdevOptionsGenericFormat',
6838f038 1513+ 'vma-drive': 'BlockdevOptionsVMADrive',
67af0fa4
WB
1514 'vmdk': 'BlockdevOptionsGenericCOWFormat',
1515 'vpc': 'BlockdevOptionsGenericFormat',
6838f038 1516 'vvfat': 'BlockdevOptionsVVFAT',
53e83913
WB
1517diff --git a/qapi/common.json b/qapi/common.json
1518index c367adc4b6..070b7b52c8 100644
1519--- a/qapi/common.json
1520+++ b/qapi/common.json
1521@@ -149,3 +149,16 @@
1522 'ppc64', 'ppcemb', 'riscv32', 'riscv64', 's390x', 'sh4',
1523 'sh4eb', 'sparc', 'sparc64', 'tricore', 'unicore32',
1524 'x86_64', 'xtensa', 'xtensaeb' ] }
1525+
1526+##
1527+# @UuidInfo:
1528+#
1529+# Guest UUID information (Universally Unique Identifier).
1530+#
1531+# @UUID: the UUID of the guest
1532+#
1533+# Since: 0.14.0
1534+#
1535+# Notes: If no UUID was specified for the guest, a null UUID is returned.
1536+##
1537+{ 'struct': 'UuidInfo', 'data': {'UUID': 'str'} }
1538diff --git a/qapi/misc.json b/qapi/misc.json
1539index b6ad5f028d..3dd5117fc3 100644
1540--- a/qapi/misc.json
1541+++ b/qapi/misc.json
1542@@ -275,19 +275,6 @@
1543 { 'command': 'query-kvm', 'returns': 'KvmInfo' }
1544
1545 ##
1546-# @UuidInfo:
1547-#
1548-# Guest UUID information (Universally Unique Identifier).
1549-#
1550-# @UUID: the UUID of the guest
1551-#
1552-# Since: 0.14.0
1553-#
1554-# Notes: If no UUID was specified for the guest, a null UUID is returned.
1555-##
1556-{ 'struct': 'UuidInfo', 'data': {'UUID': 'str'} }
1557-
1558-##
1559 # @query-uuid:
1560 #
1561 # Query the guest UUID information.
67af0fa4
WB
1562--
15632.11.0
1564