1 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2 From: Wolfgang Bumiller <w.bumiller@proxmox.com>
3 Date: Wed, 2 Aug 2017 13:51:02 +0200
4 Subject: [PATCH] PVE: backup: introduce vma archive format
6 TODO: Move to a libvma block backend.
9 block/Makefile.objs | 3 +
10 block/vma.c | 503 +++++++++++++++++++++++++++++++++++++++++++++++
11 blockdev.c | 536 +++++++++++++++++++++++++++++++++++++++++++++++++++
13 hmp-commands-info.hx | 13 ++
14 hmp-commands.hx | 31 +++
17 qapi/block-core.json | 109 ++++++++++-
18 qapi/common.json | 13 ++
19 qapi/misc.json | 13 --
20 12 files changed, 1308 insertions(+), 14 deletions(-)
21 create mode 100644 block/vma.c
23 diff --git a/MAINTAINERS b/MAINTAINERS
24 index 666e936812..299a73cd86 100644
27 @@ -2140,6 +2140,12 @@ L: qemu-block@nongnu.org
32 +M: Wolfgang Bumiller <w.bumiller@proxmox.com>.
33 +L: pve-devel@proxmox.com
38 M: Stefan Hajnoczi <stefanha@redhat.com>
39 L: qemu-block@nongnu.org
40 diff --git a/block/Makefile.objs b/block/Makefile.objs
41 index c00f0b32d6..abfd0f69d7 100644
42 --- a/block/Makefile.objs
43 +++ b/block/Makefile.objs
44 @@ -24,6 +24,7 @@ block-obj-$(CONFIG_RBD) += rbd.o
45 block-obj-$(CONFIG_GLUSTERFS) += gluster.o
46 block-obj-$(CONFIG_VXHS) += vxhs.o
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
52 @@ -52,3 +53,5 @@ qcow.o-libs := -lz
53 linux-aio.o-libs := -laio
54 parallels.o-cflags := $(LIBXML2_CFLAGS)
55 parallels.o-libs := $(LIBXML2_LIBS)
56 +vma.o-cflags := $(VMA_CFLAGS)
57 +vma.o-libs := $(VMA_LIBS)
58 diff --git a/block/vma.c b/block/vma.c
60 index 0000000000..b911b198dc
65 + * VMA archive backend for QEMU, container object
67 + * Copyright (C) 2017 Proxmox Server Solutions GmbH
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.
75 +#include "qemu/osdep.h"
76 +#include "qemu/uuid.h"
77 +#include "qemu/option.h"
78 +#include "qemu-common.h"
79 +#include "qapi/error.h"
80 +#include "qapi/qmp/qerror.h"
81 +#include "qapi/qmp/qstring.h"
82 +#include "qapi/qmp/qdict.h"
83 +#include "qom/object.h"
84 +#include "qom/object_interfaces.h"
85 +#include "block/block_int.h"
87 +/* exported interface */
88 +void vma_object_add_config_file(Object *obj, const char *name,
89 + const char *contents, size_t len,
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)
98 +typedef struct VMAObjectClass {
99 + ObjectClass parent_class;
102 +typedef struct VMAObjectState {
113 +static VMAObjectState *vma_by_id(const char *name)
118 + container = object_get_objects_root();
119 + obj = object_resolve_path_component(container, name);
121 + return VMA_OBJECT(obj);
124 +static void vma_object_class_complete(UserCreatable *uc, Error **errp)
127 + VMAObjectState *vo = VMA_OBJECT(uc);
128 + VMAObjectClass *voc = VMA_OBJECT_GET_CLASS(uc);
132 + if (!vo->filename) {
133 + error_setg(errp, "Parameter 'filename' is required");
137 + vo->vma = VMAWriter_fopen(vo->filename);
139 + error_setg_errno(errp, errno, "failed to create VMA archive");
143 + rc = VMAWriter_set_uuid(vo->vma, vo->uuid.data, sizeof(vo->uuid.data));
145 + error_setg_errno(errp, -rc, "failed to set UUID of VMA archive");
149 + qemu_mutex_init(&vo->mutex);
152 +static bool vma_object_can_be_deleted(UserCreatable *uc)
154 + //VMAObjectState *vo = VMA_OBJECT(uc);
162 +static void vma_object_class_init(ObjectClass *oc, void *data)
164 + UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
166 + ucc->can_be_deleted = vma_object_can_be_deleted;
167 + ucc->complete = vma_object_class_complete;
170 +static char *vma_object_get_filename(Object *obj, Error **errp)
172 + VMAObjectState *vo = VMA_OBJECT(obj);
174 + return g_strdup(vo->filename);
177 +static void vma_object_set_filename(Object *obj, const char *str, Error **errp)
179 + VMAObjectState *vo = VMA_OBJECT(obj);
182 + error_setg(errp, "filename cannot be changed after creation");
186 + g_free(vo->filename);
187 + vo->filename = g_strdup(str);
190 +static char *vma_object_get_uuid(Object *obj, Error **errp)
192 + VMAObjectState *vo = VMA_OBJECT(obj);
194 + return qemu_uuid_unparse_strdup(&vo->uuid);
197 +static void vma_object_set_uuid(Object *obj, const char *str, Error **errp)
199 + VMAObjectState *vo = VMA_OBJECT(obj);
202 + error_setg(errp, "uuid cannot be changed after creation");
206 + qemu_uuid_parse(str, &vo->uuid);
209 +static bool vma_object_get_blocked(Object *obj, Error **errp)
211 + VMAObjectState *vo = VMA_OBJECT(obj);
213 + return vo->blocked;
216 +static void vma_object_set_blocked(Object *obj, bool blocked, Error **errp)
218 + VMAObjectState *vo = VMA_OBJECT(obj);
222 + vo->blocked = blocked;
225 +void vma_object_add_config_file(Object *obj, const char *name,
226 + const char *contents, size_t len,
230 + VMAObjectState *vo = VMA_OBJECT(obj);
232 + if (!vo || !vo->vma) {
233 + error_setg(errp, "not a valid vma object to add config files to");
237 + rc = VMAWriter_addConfigFile(vo->vma, name, contents, len);
239 + error_setg_errno(errp, -rc, "failed to add config file to VMA");
244 +static void vma_object_init(Object *obj)
246 + VMAObjectState *vo = VMA_OBJECT(obj);
249 + object_property_add_str(obj, "filename",
250 + vma_object_get_filename, vma_object_set_filename,
252 + object_property_add_str(obj, "uuid",
253 + vma_object_get_uuid, vma_object_set_uuid,
255 + object_property_add_bool(obj, "blocked",
256 + vma_object_get_blocked, vma_object_set_blocked,
260 +static void vma_object_finalize(Object *obj)
262 + VMAObjectState *vo = VMA_OBJECT(obj);
263 + VMAObjectClass *voc = VMA_OBJECT_GET_CLASS(obj);
266 + qemu_mutex_destroy(&vo->mutex);
268 + VMAWriter_destroy(vo->vma, true);
269 + g_free(vo->filename);
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 },
286 +static void register_types(void)
288 + type_register_static(&vma_object_info);
291 +type_init(register_types);
294 + VMAObjectState *vma_obj;
297 + uint64_t byte_size;
300 +static void qemu_vma_parse_filename(const char *filename, QDict *options,
305 + if (strncmp(filename, "vma:", sizeof("vma:")-1) == 0) {
306 + filename += sizeof("vma:")-1;
309 + sep = strchr(filename, '/');
310 + if (!sep || sep == filename) {
311 + error_setg(errp, "VMA file should be <vma-obj>/<name>/<size>");
315 + qdict_put(options, "vma", qstring_from_substr(filename, 0, sep-filename));
317 + while (*sep && *sep == '/')
320 + error_setg(errp, "missing device name\n");
325 + sep = strchr(filename, '/');
326 + if (!sep || sep == filename) {
327 + error_setg(errp, "VMA file should be <vma-obj>/<name>/<size>");
331 + qdict_put(options, "name", qstring_from_substr(filename, 0, sep-filename));
333 + while (*sep && *sep == '/')
336 + error_setg(errp, "missing device size\n");
341 + qdict_put_str(options, "size", filename);
344 +static QemuOptsList runtime_opts = {
345 + .name = "vma-drive",
346 + .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
350 + .type = QEMU_OPT_STRING,
351 + .help = "VMA Object name",
355 + .type = QEMU_OPT_STRING,
356 + .help = "VMA device name",
359 + .name = BLOCK_OPT_SIZE,
360 + .type = QEMU_OPT_SIZE,
361 + .help = "Virtual disk size"
363 + { /* end of list */ }
366 +static int qemu_vma_open(BlockDriverState *bs, QDict *options, int flags,
369 + Error *local_err = NULL;
370 + BDRVVMAState *s = bs->opaque;
372 + const char *vma_id, *device_name;
377 + opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
378 + qemu_opts_absorb_qdict(opts, options, &local_err);
380 + error_propagate(errp, local_err);
385 + bytes = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
388 + vma_id = qemu_opt_get(opts, "vma");
391 + error_setg(errp, "missing 'vma' property");
395 + device_name = qemu_opt_get(opts, "name");
396 + if (!device_name) {
398 + error_setg(errp, "missing 'name' property");
402 + VMAObjectState *vma = vma_by_id(vma_id);
405 + error_setg(errp, "no such VMA object: %s", vma_id);
409 + dev_id = VMAWriter_findDevice(vma->vma, device_name);
411 + error_setg(errp, "drive already exists in VMA object");
416 + dev_id = VMAWriter_addDevice(vma->vma, device_name, (uint64_t)bytes);
418 + error_setg_errno(errp, -dev_id, "failed to add VMA device");
423 + object_ref(OBJECT(vma));
425 + s->name = g_strdup(device_name);
426 + s->device_id = (size_t)dev_id;
427 + s->byte_size = bytes;
432 + qemu_opts_del(opts);
436 +static void qemu_vma_close(BlockDriverState *bs)
438 + BDRVVMAState *s = bs->opaque;
440 + (void)VMAWriter_finishDevice(s->vma_obj->vma, s->device_id);
441 + object_unref(OBJECT(s->vma_obj));
446 +static int64_t qemu_vma_getlength(BlockDriverState *bs)
448 + BDRVVMAState *s = bs->opaque;
450 + return s->byte_size;
453 +static coroutine_fn int qemu_vma_co_writev(BlockDriverState *bs,
454 + int64_t sector_num,
456 + QEMUIOVector *qiov,
461 + BDRVVMAState *s = bs->opaque;
462 + VMAObjectState *vo = s->vma_obj;
463 + off_t offset = sector_num * BDRV_SECTOR_SIZE;
464 + /* flags can be only values we set in supported_write_flags */
465 + assert(flags == 0);
467 + qemu_mutex_lock(&vo->mutex);
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) {
477 + rc = VMAWriter_writeBlocks(vo->vma, s->device_id,
478 + v->iov_base, blocks, offset);
482 + if (rc != blocks) {
485 + offset += v->iov_len;
487 + qemu_mutex_unlock(&vo->mutex);
491 +static int qemu_vma_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
493 + bdi->cluster_size = VMA_CLUSTER_SIZE;
494 + bdi->unallocated_blocks_are_zero = true;
498 +static int qemu_vma_check_perm(BlockDriverState *bs,
503 + /* Nothing to do. */
507 +static void qemu_vma_set_perm(BlockDriverState *bs,
511 + /* Nothing to do. */
514 +static void qemu_vma_abort_perm_update(BlockDriverState *bs)
516 + /* Nothing to do. */
519 +static void qemu_vma_refresh_limits(BlockDriverState *bs, Error **errp)
521 + bs->bl.request_alignment = BDRV_SECTOR_SIZE; /* No sub-sector I/O */
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)
529 + *nperm = BLK_PERM_ALL;
530 + *nshared = BLK_PERM_ALL;
533 +static BlockDriver bdrv_vma_drive = {
534 + .format_name = "vma-drive",
535 + .protocol_name = "vma",
536 + .instance_size = sizeof(BDRVVMAState),
539 + .bdrv_create = qemu_vma_create,
540 + .create_opts = &qemu_vma_create_opts,
543 + .bdrv_parse_filename = qemu_vma_parse_filename,
544 + .bdrv_file_open = qemu_vma_open,
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,
551 + //.bdrv_co_preadv = qemu_vma_co_preadv,
552 + .bdrv_co_writev = qemu_vma_co_writev,
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,
561 +static void bdrv_vma_init(void)
563 + bdrv_register(&bdrv_vma_drive);
566 +block_init(bdrv_vma_init);
567 diff --git a/blockdev.c b/blockdev.c
568 index d5eb6b62ca..4f18d3c3d7 100644
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"
580 #include "block/qdict.h"
581 +#include "block/blockjob_int.h"
582 #include "block/throttle-groups.h"
583 #include "monitor/monitor.h"
584 #include "qemu/error-report.h"
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:
594 aio_context_release(aio_context);
597 +/* PVE backup related function */
599 +static struct PVEBackupState {
612 + size_t transferred;
614 + QemuMutex backup_mutex;
615 + bool backup_mutex_initialized;
618 +typedef struct PVEBackupDevInfo {
619 + BlockDriverState *bs;
623 + char targetfile[PATH_MAX];
624 + BlockDriverState *target;
627 +static void pvebackup_run_next_job(void);
629 +static void pvebackup_cleanup(void)
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);
638 + backup_state.end_time = time(NULL);
640 + if (backup_state.vmaobj) {
641 + object_unparent(backup_state.vmaobj);
642 + backup_state.vmaobj = NULL;
645 + g_list_free(backup_state.di_list);
646 + backup_state.di_list = NULL;
647 + qemu_mutex_unlock(&backup_state.backup_mutex);
650 +static void pvebackup_complete_cb(void *opaque, int ret)
652 + // This always runs in the main loop
654 + PVEBackupDevInfo *di = opaque;
656 + di->completed = true;
658 + if (ret < 0 && !backup_state.error) {
659 + error_setg(&backup_state.error, "job failed with err %d - %s",
660 + ret, strerror(-ret));
666 + if (backup_state.vmaobj) {
667 + object_unparent(backup_state.vmaobj);
668 + backup_state.vmaobj = NULL;
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);
675 + qemu_mutex_unlock(&backup_state.backup_mutex);
677 + if (!backup_state.cancel) {
678 + pvebackup_run_next_job();
682 +static void pvebackup_cancel(void *opaque)
684 + backup_state.cancel = true;
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);
692 + if (!backup_state.error) {
693 + error_setg(&backup_state.error, "backup cancelled");
696 + if (backup_state.vmaobj) {
698 + /* make sure vma writer does not block anymore */
699 + if (!object_set_props(backup_state.vmaobj, &err, "blocked", "yes", NULL)) {
701 + error_report_err(err);
706 + GList *l = backup_state.di_list;
708 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
709 + l = g_list_next(l);
710 + if (!di->completed && di->bs) {
711 + BlockJob *job = di->bs->job;
713 + AioContext *aio_context = blk_get_aio_context(job->blk);
714 + aio_context_acquire(aio_context);
715 + if (!di->completed) {
716 + job_cancel(&job->job, false);
718 + aio_context_release(aio_context);
723 + qemu_mutex_unlock(&backup_state.backup_mutex);
724 + pvebackup_cleanup();
727 +void qmp_backup_cancel(Error **errp)
729 + if (!backup_state.backup_mutex_initialized)
731 + Coroutine *co = qemu_coroutine_create(pvebackup_cancel, NULL);
732 + qemu_coroutine_enter(co);
734 + while (backup_state.vmaobj) {
735 + /* FIXME: Find something better for this */
736 + aio_poll(qemu_get_aio_context(), true);
740 +void vma_object_add_config_file(Object *obj, const char *name,
741 + const char *contents, size_t len,
743 +static int config_to_vma(const char *file, BackupFormat format,
745 + const char *backup_dir,
748 + char *cdata = NULL;
750 + GError *err = NULL;
751 + if (!g_file_get_contents(file, &cdata, &clen, &err)) {
752 + error_setg(errp, "unable to read file '%s'", file);
756 + char *basename = g_path_get_basename(file);
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);
776 +static void pvebackup_run_next_job(void)
778 + qemu_mutex_lock(&backup_state.backup_mutex);
780 + GList *next = g_list_nth(backup_state.di_list, backup_state.next_job);
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;
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) {
790 + job_cancel_sync(job);
794 + aio_context_release(aio_context);
797 + next = g_list_next(next);
799 + qemu_mutex_unlock(&backup_state.backup_mutex);
801 + // no more jobs, run the cleanup
802 + pvebackup_cleanup();
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)
813 + BlockDriverState *bs = NULL;
814 + const char *backup_dir = NULL;
815 + Error *local_err = NULL;
817 + gchar **devs = NULL;
818 + GList *di_list = NULL;
820 + UuidInfo *uuid_info;
823 + if (!backup_state.backup_mutex_initialized) {
824 + qemu_mutex_init(&backup_state.backup_mutex);
825 + backup_state.backup_mutex_initialized = true;
828 + if (backup_state.di_list || backup_state.vmaobj) {
829 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
830 + "previous backup not finished");
834 + /* Todo: try to auto-detect format based on file name */
835 + format = has_format ? format : BACKUP_FORMAT_VMA;
838 + devs = g_strsplit_set(devlist, ",;:", -1);
842 + blk = blk_by_name(*d);
845 + if (bdrv_is_read_only(bs)) {
846 + error_setg(errp, "Node '%s' is read only", *d);
849 + if (!bdrv_is_inserted(bs)) {
850 + error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, *d);
853 + PVEBackupDevInfo *di = g_new0(PVEBackupDevInfo, 1);
855 + di_list = g_list_append(di_list, di);
857 + error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
858 + "Device '%s' not found", *d);
865 + BdrvNextIterator it;
868 + for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
869 + if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
873 + PVEBackupDevInfo *di = g_new0(PVEBackupDevInfo, 1);
875 + di_list = g_list_append(di_list, di);
880 + error_set(errp, ERROR_CLASS_GENERIC_ERROR, "empty device list");
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)) {
894 + ssize_t size = bdrv_getlength(di->bs);
896 + error_setg_errno(errp, -di->size, "bdrv_getlength failed");
903 + qemu_uuid_generate(&uuid);
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,
915 + if (!backup_state.vmaobj) {
917 + error_propagate(errp, local_err);
924 + QDict *options = qdict_new();
926 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
927 + l = g_list_next(l);
929 + const char *devname = bdrv_get_device_name(di->bs);
930 + snprintf(di->targetfile, PATH_MAX, "vma-backup-obj/%s.raw", devname);
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);
936 + error_propagate(errp, local_err);
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",
946 + backup_dir = backup_file;
950 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
951 + l = g_list_next(l);
953 + const char *devname = bdrv_get_device_name(di->bs);
954 + snprintf(di->targetfile, PATH_MAX, "%s/%s.raw", backup_dir, devname);
956 + int flags = BDRV_O_RDWR;
957 + bdrv_img_create(di->targetfile, "raw", NULL, NULL, NULL,
958 + di->size, flags, false, &local_err);
960 + error_propagate(errp, local_err);
964 + di->target = bdrv_open(di->targetfile, NULL, NULL, flags, &local_err);
966 + error_propagate(errp, local_err);
971 + error_set(errp, ERROR_CLASS_GENERIC_ERROR, "unknown backup format");
975 + /* add configuration file to archive */
976 + if (has_config_file) {
977 + if(config_to_vma(config_file, format, backup_state.vmaobj, backup_dir, errp) != 0) {
982 + /* add firewall file to archive */
983 + if (has_firewall_file) {
984 + if(config_to_vma(firewall_file, format, backup_state.vmaobj, backup_dir, errp) != 0) {
988 + /* initialize global backup_state now */
990 + backup_state.cancel = false;
992 + if (backup_state.error) {
993 + error_free(backup_state.error);
994 + backup_state.error = NULL;
997 + backup_state.speed = (has_speed && speed > 0) ? speed : 0;
999 + backup_state.start_time = time(NULL);
1000 + backup_state.end_time = 0;
1002 + if (backup_state.backup_file) {
1003 + g_free(backup_state.backup_file);
1005 + backup_state.backup_file = g_strdup(backup_file);
1007 + memcpy(&backup_state.uuid, &uuid, sizeof(uuid));
1008 + qemu_uuid_unparse(&uuid, backup_state.uuid_str);
1010 + qemu_mutex_lock(&backup_state.backup_mutex);
1011 + backup_state.di_list = di_list;
1012 + backup_state.next_job = 0;
1014 + backup_state.total = total;
1015 + backup_state.transferred = 0;
1016 + backup_state.zero_bytes = 0;
1018 + /* start all jobs (paused state) */
1021 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
1022 + l = g_list_next(l);
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,
1027 + pvebackup_complete_cb, di, 2, NULL, &local_err);
1029 + bdrv_unref(di->target);
1030 + di->target = NULL;
1032 + if (!job || local_err != NULL) {
1033 + error_setg(&backup_state.error, "backup_job_create failed");
1034 + pvebackup_cancel(NULL);
1036 + job_start(&job->job);
1040 + qemu_mutex_unlock(&backup_state.backup_mutex);
1042 + if (!backup_state.error) {
1043 + pvebackup_run_next_job(); // run one job
1046 + uuid_info = g_malloc0(sizeof(*uuid_info));
1047 + uuid_info->UUID = g_strdup(backup_state.uuid_str);
1055 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
1056 + l = g_list_next(l);
1059 + bdrv_unref(di->target);
1062 + if (di->targetfile[0]) {
1063 + unlink(di->targetfile);
1067 + g_list_free(di_list);
1073 + if (backup_state.vmaobj) {
1074 + object_unparent(backup_state.vmaobj);
1075 + backup_state.vmaobj = NULL;
1079 + rmdir(backup_dir);
1085 +BackupStatus *qmp_query_backup(Error **errp)
1087 + BackupStatus *info = g_malloc0(sizeof(*info));
1089 + if (!backup_state.start_time) {
1090 + /* not started, return {} */
1094 + info->has_status = true;
1095 + info->has_start_time = true;
1096 + info->start_time = backup_state.start_time;
1098 + if (backup_state.backup_file) {
1099 + info->has_backup_file = true;
1100 + info->backup_file = g_strdup(backup_state.backup_file);
1103 + info->has_uuid = true;
1104 + info->uuid = g_strdup(backup_state.uuid_str);
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));
1112 + info->status = g_strdup("done");
1114 + info->has_end_time = true;
1115 + info->end_time = backup_state.end_time;
1117 + info->status = g_strdup("active");
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;
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,
1133 diff --git a/configure b/configure
1134 index 7b3f80a49c..d2cc11cdbb 100755
1137 @@ -475,6 +475,7 @@ vxhs=""
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
1147 --disable-debug-mutex) debug_mutex=no
1149 + --enable-vma) vma=yes
1151 + --disable-vma) vma=no
1154 echo "ERROR: unknown option $opt"
1155 echo "Try '$0 --help' for more information"
1156 @@ -1710,6 +1715,7 @@ disabled with --disable-FEATURE, default is enabled if available:
1157 vhost-user vhost-user support
1158 capstone capstone disassembler support
1159 debug-mutex mutex debugging support
1160 + vma VMA archive backend
1162 NOTE: The object files are built at the place where configure is launched
1164 @@ -4121,6 +4127,22 @@ EOF
1167 ##########################################
1169 +if test "$vma" != "no" ; then
1170 + if $pkg_config --exact-version=0.1.0 vma; then
1172 + vma_cflags=$($pkg_config --cflags vma)
1173 + vma_libs=$($pkg_config --libs vma)
1175 + if test "$vma" = "yes" ; then
1176 + feature_not_found "VMA Archive backend support" \
1177 + "Install libvma devel"
1183 +##########################################
1187 @@ -6007,6 +6029,7 @@ echo "replication support $replication"
1188 echo "VxHS block device $vxhs"
1189 echo "capstone $capstone"
1190 echo "docker $docker"
1191 +echo "VMA support $vma"
1193 if test "$sdl_too_old" = "yes"; then
1194 echo "-> Your SDL version is too old - please upgrade to have SDL support"
1195 @@ -6493,6 +6516,12 @@ if test "$usb_redir" = "yes" ; then
1196 echo "USB_REDIR_LIBS=$usb_redir_libs" >> $config_host_mak
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
1205 if test "$opengl" = "yes" ; then
1206 echo "CONFIG_OPENGL=y" >> $config_host_mak
1207 echo "OPENGL_LIBS=$opengl_libs" >> $config_host_mak
1208 diff --git a/hmp-commands-info.hx b/hmp-commands-info.hx
1209 index 42c148fdc9..277e140092 100644
1210 --- a/hmp-commands-info.hx
1211 +++ b/hmp-commands-info.hx
1212 @@ -502,6 +502,19 @@ STEXI
1213 Show CPU statistics.
1220 + .help = "show backup status",
1221 + .cmd = hmp_info_backup,
1229 #if defined(CONFIG_SLIRP)
1232 diff --git a/hmp-commands.hx b/hmp-commands.hx
1233 index a6f0720442..956cbf04b9 100644
1234 --- a/hmp-commands.hx
1235 +++ b/hmp-commands.hx
1236 @@ -107,6 +107,37 @@ STEXI
1237 Copy data from a backing file into a block device.
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,
1253 +Create a VM backup.
1257 + .name = "backup_cancel",
1260 + .help = "cancel the current VM backup",
1261 + .cmd = hmp_backup_cancel,
1265 +@item backup_cancel
1266 +@findex backup_cancel
1267 +Cancel the current VM backup.
1272 .name = "block_job_set_speed",
1273 .args_type = "device:B,speed:o",
1274 diff --git a/hmp.c b/hmp.c
1275 index 7c975f3ead..8d659e20f6 100644
1278 @@ -166,6 +166,44 @@ void hmp_info_mice(Monitor *mon, const QDict *qdict)
1279 qapi_free_MouseInfoList(mice_list);
1282 +void hmp_info_backup(Monitor *mon, const QDict *qdict)
1284 + BackupStatus *info;
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);
1292 + monitor_printf(mon, "Backup status: %s\n", info->status);
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));
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);
1317 + qapi_free_BackupStatus(info);
1320 void hmp_info_migrate(Monitor *mon, const QDict *qdict)
1322 MigrationInfo *info;
1323 @@ -1899,6 +1937,31 @@ void hmp_block_stream(Monitor *mon, const QDict *qdict)
1324 hmp_handle_error(mon, &error);
1327 +void hmp_backup_cancel(Monitor *mon, const QDict *qdict)
1329 + Error *error = NULL;
1331 + qmp_backup_cancel(&error);
1333 + hmp_handle_error(mon, &error);
1336 +void hmp_backup(Monitor *mon, const QDict *qdict)
1338 + Error *error = NULL;
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);
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);
1349 + hmp_handle_error(mon, &error);
1352 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
1354 Error *error = NULL;
1355 diff --git a/hmp.h b/hmp.h
1356 index 98bb7a44db..853f233195 100644
1359 @@ -29,6 +29,7 @@ void hmp_info_migrate(Monitor *mon, const QDict *qdict);
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);
1367 @@ -86,6 +87,8 @@ void hmp_eject(Monitor *mon, const QDict *qdict);
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);
1376 diff --git a/qapi/block-core.json b/qapi/block-core.json
1377 index 5b9084a394..9c3c2d4917 100644
1378 --- a/qapi/block-core.json
1379 +++ b/qapi/block-core.json
1380 @@ -718,6 +718,97 @@
1386 +# Detailed backup status.
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
1392 +# @errmsg: error message (only returned if status is 'error')
1394 +# @total: total amount of bytes involved in the backup process
1396 +# @transferred: amount of bytes already backed up.
1398 +# @zero-bytes: amount of 'zero' bytes detected.
1400 +# @start-time: time (epoch) when backup job started.
1402 +# @end-time: time (epoch) when backup job finished.
1404 +# @backup-file: backup file name
1406 +# @uuid: uuid for this backup job
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' } }
1418 +# An enumeration of supported backup formats.
1420 +# @vma: Proxmox vma backup format
1422 +{ 'enum': 'BackupFormat',
1423 + 'data': [ 'vma', 'dir' ] }
1428 +# Starts a VM backup.
1430 +# @backup-file: the backup file name
1432 +# @format: format of the backup file
1434 +# @config-file: a configuration file to include into
1435 +# the backup archive.
1437 +# @speed: the maximum speed, in bytes per second
1439 +# @devlist: list of block device names (separated by ',', ';'
1440 +# or ':'). By default the backup includes all writable block devices.
1442 +# Returns: the uuid of the backup job
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' }
1455 +# Returns information about current/last backup task.
1457 +# Returns: @BackupStatus
1460 +{ 'command': 'query-backup', 'returns': 'BackupStatus' }
1465 +# Cancel the current executing backup process.
1467 +# Returns: nothing on success
1469 +# Notes: This command succeeds even if there is no backup process running.
1472 +{ 'command': 'backup-cancel' }
1475 # @BlockDeviceTimedStats:
1477 # Statistics of a block device during a given interval of time.
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' ] }
1486 # @BlockdevOptionsFile:
1487 @@ -3550,6 +3641,21 @@
1488 '*tls-creds': 'str' } }
1491 +# @BlockdevOptionsVMADrive:
1493 +# Driver specific block device options for VMA Drives
1495 +# @filename: vma-drive path
1497 +# @size: drive size in bytes
1501 +{ 'struct': 'BlockdevOptionsVMADrive',
1502 + 'data': { 'filename': 'str',
1506 # @BlockdevOptionsThrottle:
1508 # Driver specific block device options for the throttle driver
1509 @@ -3633,6 +3739,7 @@
1510 'throttle': 'BlockdevOptionsThrottle',
1511 'vdi': 'BlockdevOptionsGenericFormat',
1512 'vhdx': 'BlockdevOptionsGenericFormat',
1513 + 'vma-drive': 'BlockdevOptionsVMADrive',
1514 'vmdk': 'BlockdevOptionsGenericCOWFormat',
1515 'vpc': 'BlockdevOptionsGenericFormat',
1516 'vvfat': 'BlockdevOptionsVVFAT',
1517 diff --git a/qapi/common.json b/qapi/common.json
1518 index 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' ] }
1529 +# Guest UUID information (Universally Unique Identifier).
1531 +# @UUID: the UUID of the guest
1535 +# Notes: If no UUID was specified for the guest, a null UUID is returned.
1537 +{ 'struct': 'UuidInfo', 'data': {'UUID': 'str'} }
1538 diff --git a/qapi/misc.json b/qapi/misc.json
1539 index b6ad5f028d..3dd5117fc3 100644
1540 --- a/qapi/misc.json
1541 +++ b/qapi/misc.json
1542 @@ -275,19 +275,6 @@
1543 { 'command': 'query-kvm', 'returns': 'KvmInfo' }
1548 -# Guest UUID information (Universally Unique Identifier).
1550 -# @UUID: the UUID of the guest
1554 -# Notes: If no UUID was specified for the guest, a null UUID is returned.
1556 -{ 'struct': 'UuidInfo', 'data': {'UUID': 'str'} }
1561 # Query the guest UUID information.