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 | 424 ++++++++++++++++++++++++++++++++++++++++
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, 1229 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..7151514f94
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-common.h"
78 +#include "qapi/error.h"
79 +#include "qapi/qmp/qerror.h"
80 +#include "qapi/qmp/qstring.h"
81 +#include "qom/object.h"
82 +#include "qom/object_interfaces.h"
83 +#include "block/block_int.h"
85 +/* exported interface */
86 +void vma_object_add_config_file(Object *obj, const char *name,
87 + const char *contents, size_t len,
90 +#define TYPE_VMA_OBJECT "vma"
91 +#define VMA_OBJECT(obj) \
92 + OBJECT_CHECK(VMAObjectState, (obj), TYPE_VMA_OBJECT)
93 +#define VMA_OBJECT_GET_CLASS(obj) \
94 + OBJECT_GET_CLASS(VMAObjectClass, (obj), TYPE_VMA_OBJECT)
96 +typedef struct VMAObjectClass {
97 + ObjectClass parent_class;
100 +typedef struct VMAObjectState {
111 +static VMAObjectState *vma_by_id(const char *name)
116 + container = object_get_objects_root();
117 + obj = object_resolve_path_component(container, name);
119 + return VMA_OBJECT(obj);
122 +static void vma_object_class_complete(UserCreatable *uc, Error **errp)
125 + VMAObjectState *vo = VMA_OBJECT(uc);
126 + VMAObjectClass *voc = VMA_OBJECT_GET_CLASS(uc);
130 + if (!vo->filename) {
131 + error_setg(errp, "Parameter 'filename' is required");
135 + rc = VMAWriter_fopen(vo->filename, &vo->vma);
137 + error_setg_errno(errp, -rc, "failed to create VMA archive");
141 + rc = VMAWriter_set_uuid(vo->vma, vo->uuid.data, sizeof(vo->uuid.data));
143 + error_setg_errno(errp, -rc, "failed to set UUID of VMA archive");
147 + qemu_mutex_init(&vo->mutex);
150 +static bool vma_object_can_be_deleted(UserCreatable *uc, Error **errp)
152 + //VMAObjectState *vo = VMA_OBJECT(uc);
160 +static void vma_object_class_init(ObjectClass *oc, void *data)
162 + UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
164 + ucc->can_be_deleted = vma_object_can_be_deleted;
165 + ucc->complete = vma_object_class_complete;
168 +static char *vma_object_get_filename(Object *obj, Error **errp)
170 + VMAObjectState *vo = VMA_OBJECT(obj);
172 + return g_strdup(vo->filename);
175 +static void vma_object_set_filename(Object *obj, const char *str, Error **errp)
177 + VMAObjectState *vo = VMA_OBJECT(obj);
180 + error_setg(errp, "filename cannot be changed after creation");
184 + g_free(vo->filename);
185 + vo->filename = g_strdup(str);
188 +static char *vma_object_get_uuid(Object *obj, Error **errp)
190 + VMAObjectState *vo = VMA_OBJECT(obj);
192 + return qemu_uuid_unparse_strdup(&vo->uuid);
195 +static void vma_object_set_uuid(Object *obj, const char *str, Error **errp)
197 + VMAObjectState *vo = VMA_OBJECT(obj);
200 + error_setg(errp, "uuid cannot be changed after creation");
204 + qemu_uuid_parse(str, &vo->uuid);
207 +static bool vma_object_get_blocked(Object *obj, Error **errp)
209 + VMAObjectState *vo = VMA_OBJECT(obj);
211 + return vo->blocked;
214 +static void vma_object_set_blocked(Object *obj, bool blocked, Error **errp)
216 + VMAObjectState *vo = VMA_OBJECT(obj);
220 + vo->blocked = blocked;
223 +void vma_object_add_config_file(Object *obj, const char *name,
224 + const char *contents, size_t len,
228 + VMAObjectState *vo = VMA_OBJECT(obj);
230 + if (!vo || !vo->vma) {
231 + error_setg(errp, "not a valid vma object to add config files to");
235 + rc = VMAWriter_addConfigFile(vo->vma, name, contents, len);
237 + error_setg_errno(errp, -rc, "failed to add config file to VMA");
242 +static void vma_object_init(Object *obj)
244 + VMAObjectState *vo = VMA_OBJECT(obj);
247 + object_property_add_str(obj, "filename",
248 + vma_object_get_filename, vma_object_set_filename,
250 + object_property_add_str(obj, "uuid",
251 + vma_object_get_uuid, vma_object_set_uuid,
253 + object_property_add_bool(obj, "blocked",
254 + vma_object_get_blocked, vma_object_set_blocked,
258 +static void vma_object_finalize(Object *obj)
260 + VMAObjectState *vo = VMA_OBJECT(obj);
261 + VMAObjectClass *voc = VMA_OBJECT_GET_CLASS(obj);
264 + qemu_mutex_destroy(&vo->mutex);
266 + VMAWriter_destroy(vo->vma, true);
267 + g_free(vo->filename);
270 +static const TypeInfo vma_object_info = {
271 + .name = TYPE_VMA_OBJECT,
272 + .parent = TYPE_OBJECT,
273 + .class_size = sizeof(VMAObjectClass),
274 + .class_init = vma_object_class_init,
275 + .instance_size = sizeof(VMAObjectState),
276 + .instance_init = vma_object_init,
277 + .instance_finalize = vma_object_finalize,
278 + .interfaces = (InterfaceInfo[]) {
279 + { TYPE_USER_CREATABLE },
284 +static void register_types(void)
286 + type_register_static(&vma_object_info);
289 +type_init(register_types);
292 + VMAObjectState *vma_obj;
295 + uint64_t byte_size;
298 +static void qemu_vma_parse_filename(const char *filename, QDict *options,
303 + sep = strchr(filename, '/');
304 + if (!sep || sep == filename) {
305 + error_setg(errp, "VMA filename should be <vma-object>/<device-name>");
309 + qdict_put(options, "vma", qstring_from_substr(filename, 0, sep-filename-1));
311 + while (*sep && *sep == '/')
314 + error_setg(errp, "missing device name\n");
318 + qdict_put(options, "name", qstring_from_str(sep));
321 +static QemuOptsList runtime_opts = {
322 + .name = "vma-drive",
323 + .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
327 + .type = QEMU_OPT_STRING,
328 + .help = "VMA Object name",
332 + .type = QEMU_OPT_STRING,
333 + .help = "VMA device name",
336 + .name = BLOCK_OPT_SIZE,
337 + .type = QEMU_OPT_SIZE,
338 + .help = "Virtual disk size"
340 + { /* end of list */ }
343 +static int qemu_vma_open(BlockDriverState *bs, QDict *options, int flags,
346 + Error *local_err = NULL;
347 + BDRVVMAState *s = bs->opaque;
349 + const char *vma_id, *device_name;
354 + opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
355 + qemu_opts_absorb_qdict(opts, options, &local_err);
357 + error_propagate(errp, local_err);
362 + bytes = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
365 + vma_id = qemu_opt_get(opts, "vma");
366 + device_name = qemu_opt_get(opts, "name");
368 + VMAObjectState *vma = vma_by_id(vma_id);
371 + error_setg(errp, "no such VMA object: %s", vma_id);
375 + dev_id = VMAWriter_findDevice(vma->vma, device_name);
377 + error_setg(errp, "drive already exists in VMA object");
382 + dev_id = VMAWriter_addDevice(vma->vma, device_name, (uint64_t)bytes);
384 + error_setg_errno(errp, -dev_id, "failed to add VMA device");
389 + object_ref(OBJECT(vma));
391 + s->name = g_strdup(device_name);
392 + s->device_id = (size_t)dev_id;
393 + s->byte_size = bytes;
398 + qemu_opts_del(opts);
402 +static void qemu_vma_close(BlockDriverState *bs)
404 + BDRVVMAState *s = bs->opaque;
406 + (void)VMAWriter_finishDevice(s->vma_obj->vma, s->device_id);
407 + object_unref(OBJECT(s->vma_obj));
412 +static int64_t qemu_vma_getlength(BlockDriverState *bs)
414 + BDRVVMAState *s = bs->opaque;
416 + return s->byte_size;
419 +static coroutine_fn int qemu_vma_co_writev(BlockDriverState *bs,
420 + int64_t sector_num,
422 + QEMUIOVector *qiov)
426 + BDRVVMAState *s = bs->opaque;
427 + VMAObjectState *vo = s->vma_obj;
428 + off_t offset = sector_num * BDRV_SECTOR_SIZE;
430 + qemu_mutex_lock(&vo->mutex);
434 + for (i = 0; i != qiov->niov; ++i) {
435 + const struct iovec *v = &qiov->iov[i];
436 + size_t blocks = v->iov_len / VMA_BLOCK_SIZE;
437 + if (blocks * VMA_BLOCK_SIZE != v->iov_len) {
440 + rc = VMAWriter_writeBlocks(vo->vma, s->device_id,
441 + v->iov_base, blocks, offset);
445 + if (rc != blocks) {
448 + offset += v->iov_len;
450 + qemu_mutex_unlock(&vo->mutex);
454 +static int qemu_vma_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
456 + bdi->cluster_size = VMA_CLUSTER_SIZE;
457 + bdi->unallocated_blocks_are_zero = true;
458 + bdi->can_write_zeroes_with_unmap = false;
462 +static BlockDriver bdrv_vma_drive = {
463 + .format_name = "vma-drive",
464 + .instance_size = sizeof(BDRVVMAState),
467 + .bdrv_create = qemu_vma_create,
468 + .create_opts = &qemu_vma_create_opts,
471 + .bdrv_parse_filename = qemu_vma_parse_filename,
472 + .bdrv_file_open = qemu_vma_open,
474 + .bdrv_close = qemu_vma_close,
475 + .bdrv_has_zero_init = bdrv_has_zero_init_1,
476 + .bdrv_getlength = qemu_vma_getlength,
477 + .bdrv_get_info = qemu_vma_get_info,
479 + .bdrv_co_writev = qemu_vma_co_writev,
482 +static void bdrv_vma_init(void)
484 + bdrv_register(&bdrv_vma_drive);
487 +block_init(bdrv_vma_init);
488 diff --git a/blockdev.c b/blockdev.c
489 index d5eb6b62ca..4f18d3c3d7 100644
495 #include "qemu/osdep.h"
496 +#include "qemu/uuid.h"
497 #include "sysemu/block-backend.h"
498 #include "sysemu/blockdev.h"
499 #include "hw/block/block.h"
500 #include "block/blockjob.h"
501 #include "block/qdict.h"
502 +#include "block/blockjob_int.h"
503 #include "block/throttle-groups.h"
504 #include "monitor/monitor.h"
505 #include "qemu/error-report.h"
507 #include "qapi/qapi-commands-block.h"
508 #include "qapi/qapi-commands-transaction.h"
509 #include "qapi/qapi-visit-block-core.h"
510 +#include "qapi/qapi-types-misc.h"
511 #include "qapi/qmp/qdict.h"
512 #include "qapi/qmp/qnum.h"
513 #include "qapi/qmp/qstring.h"
514 @@ -3220,6 +3223,539 @@ out:
515 aio_context_release(aio_context);
518 +/* PVE backup related function */
520 +static struct PVEBackupState {
533 + size_t transferred;
535 + QemuMutex backup_mutex;
536 + bool backup_mutex_initialized;
539 +typedef struct PVEBackupDevInfo {
540 + BlockDriverState *bs;
544 + char targetfile[PATH_MAX];
545 + BlockDriverState *target;
548 +static void pvebackup_run_next_job(void);
550 +static void pvebackup_cleanup(void)
552 + qemu_mutex_lock(&backup_state.backup_mutex);
553 + // Avoid race between block jobs and backup-cancel command:
554 + if (!backup_state.vmaw) {
555 + qemu_mutex_unlock(&backup_state.backup_mutex);
559 + backup_state.end_time = time(NULL);
561 + if (backup_state.vmaobj) {
562 + object_unparent(backup_state.vmaobj);
563 + backup_state.vmaobj = NULL;
566 + g_list_free(backup_state.di_list);
567 + backup_state.di_list = NULL;
568 + qemu_mutex_unlock(&backup_state.backup_mutex);
571 +static void pvebackup_complete_cb(void *opaque, int ret)
573 + // This always runs in the main loop
575 + PVEBackupDevInfo *di = opaque;
577 + di->completed = true;
579 + if (ret < 0 && !backup_state.error) {
580 + error_setg(&backup_state.error, "job failed with err %d - %s",
581 + ret, strerror(-ret));
587 + if (backup_state.vmaobj) {
588 + object_unparent(backup_state.vmaobj);
589 + backup_state.vmaobj = NULL;
592 + // remove self from job queue
593 + qemu_mutex_lock(&backup_state.backup_mutex);
594 + backup_state.di_list = g_list_remove(backup_state.di_list, di);
596 + qemu_mutex_unlock(&backup_state.backup_mutex);
598 + if (!backup_state.cancel) {
599 + pvebackup_run_next_job();
603 +static void pvebackup_cancel(void *opaque)
605 + backup_state.cancel = true;
606 + qemu_mutex_lock(&backup_state.backup_mutex);
607 + // Avoid race between block jobs and backup-cancel command:
608 + if (!backup_state.vmaw) {
609 + qemu_mutex_unlock(&backup_state.backup_mutex);
613 + if (!backup_state.error) {
614 + error_setg(&backup_state.error, "backup cancelled");
617 + if (backup_state.vmaobj) {
619 + /* make sure vma writer does not block anymore */
620 + if (!object_set_props(backup_state.vmaobj, &err, "blocked", "yes", NULL)) {
622 + error_report_err(err);
627 + GList *l = backup_state.di_list;
629 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
630 + l = g_list_next(l);
631 + if (!di->completed && di->bs) {
632 + BlockJob *job = di->bs->job;
634 + AioContext *aio_context = blk_get_aio_context(job->blk);
635 + aio_context_acquire(aio_context);
636 + if (!di->completed) {
637 + job_cancel(&job->job, false);
639 + aio_context_release(aio_context);
644 + qemu_mutex_unlock(&backup_state.backup_mutex);
645 + pvebackup_cleanup();
648 +void qmp_backup_cancel(Error **errp)
650 + if (!backup_state.backup_mutex_initialized)
652 + Coroutine *co = qemu_coroutine_create(pvebackup_cancel, NULL);
653 + qemu_coroutine_enter(co);
655 + while (backup_state.vmaobj) {
656 + /* FIXME: Find something better for this */
657 + aio_poll(qemu_get_aio_context(), true);
661 +void vma_object_add_config_file(Object *obj, const char *name,
662 + const char *contents, size_t len,
664 +static int config_to_vma(const char *file, BackupFormat format,
666 + const char *backup_dir,
669 + char *cdata = NULL;
671 + GError *err = NULL;
672 + if (!g_file_get_contents(file, &cdata, &clen, &err)) {
673 + error_setg(errp, "unable to read file '%s'", file);
677 + char *basename = g_path_get_basename(file);
679 + if (format == BACKUP_FORMAT_VMA) {
680 + vma_object_add_config_file(vmaobj, basename, cdata, clen, errp);
681 + } else if (format == BACKUP_FORMAT_DIR) {
682 + char config_path[PATH_MAX];
683 + snprintf(config_path, PATH_MAX, "%s/%s", backup_dir, basename);
684 + if (!g_file_set_contents(config_path, cdata, clen, &err)) {
685 + error_setg(errp, "unable to write config file '%s'", config_path);
697 +static void pvebackup_run_next_job(void)
699 + qemu_mutex_lock(&backup_state.backup_mutex);
701 + GList *next = g_list_nth(backup_state.di_list, backup_state.next_job);
703 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)next->data;
704 + backup_state.next_job++;
705 + if (!di->completed && di->bs && di->bs->job) {
706 + BlockJob *job = di->bs->job;
707 + AioContext *aio_context = blk_get_aio_context(job->blk);
708 + aio_context_acquire(aio_context);
709 + qemu_mutex_unlock(&backup_state.backup_mutex);
710 + if (backup_state.error || backup_state.cancel) {
711 + job_cancel_sync(job);
715 + aio_context_release(aio_context);
718 + next = g_list_next(next);
720 + qemu_mutex_unlock(&backup_state.backup_mutex);
722 + // no more jobs, run the cleanup
723 + pvebackup_cleanup();
726 +UuidInfo *qmp_backup(const char *backup_file, bool has_format,
727 + BackupFormat format,
728 + bool has_config_file, const char *config_file,
729 + bool has_firewall_file, const char *firewall_file,
730 + bool has_devlist, const char *devlist,
731 + bool has_speed, int64_t speed, Error **errp)
734 + BlockDriverState *bs = NULL;
735 + const char *backup_dir = NULL;
736 + Error *local_err = NULL;
738 + gchar **devs = NULL;
739 + GList *di_list = NULL;
741 + UuidInfo *uuid_info;
744 + if (!backup_state.backup_mutex_initialized) {
745 + qemu_mutex_init(&backup_state.backup_mutex);
746 + backup_state.backup_mutex_initialized = true;
749 + if (backup_state.di_list || backup_state.vmaobj) {
750 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
751 + "previous backup not finished");
755 + /* Todo: try to auto-detect format based on file name */
756 + format = has_format ? format : BACKUP_FORMAT_VMA;
759 + devs = g_strsplit_set(devlist, ",;:", -1);
763 + blk = blk_by_name(*d);
766 + if (bdrv_is_read_only(bs)) {
767 + error_setg(errp, "Node '%s' is read only", *d);
770 + if (!bdrv_is_inserted(bs)) {
771 + error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, *d);
774 + PVEBackupDevInfo *di = g_new0(PVEBackupDevInfo, 1);
776 + di_list = g_list_append(di_list, di);
778 + error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
779 + "Device '%s' not found", *d);
786 + BdrvNextIterator it;
789 + for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
790 + if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
794 + PVEBackupDevInfo *di = g_new0(PVEBackupDevInfo, 1);
796 + di_list = g_list_append(di_list, di);
801 + error_set(errp, ERROR_CLASS_GENERIC_ERROR, "empty device list");
809 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
810 + l = g_list_next(l);
811 + if (bdrv_op_is_blocked(di->bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
815 + ssize_t size = bdrv_getlength(di->bs);
817 + error_setg_errno(errp, -di->size, "bdrv_getlength failed");
824 + qemu_uuid_generate(&uuid);
826 + if (format == BACKUP_FORMAT_VMA) {
827 + char uuidstr[UUID_FMT_LEN+1];
828 + qemu_uuid_unparse(&uuid, uuidstr);
829 + uuidstr[UUID_FMT_LEN] = 0;
830 + backup_state.vmaobj =
831 + object_new_with_props("vma", object_get_objects_root(),
832 + "vma-backup-obj", &local_err,
833 + "filename", backup_file,
836 + if (!backup_state.vmaobj) {
838 + error_propagate(errp, local_err);
845 + QDict *options = qdict_new();
847 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
848 + l = g_list_next(l);
850 + const char *devname = bdrv_get_device_name(di->bs);
851 + snprintf(di->targetfile, PATH_MAX, "vma-backup-obj/%s.raw", devname);
853 + qdict_put(options, "driver", qstring_from_str("vma-drive"));
854 + qdict_put(options, "size", qint_from_int(di->size));
855 + di->target = bdrv_open(di->targetfile, NULL, options, BDRV_O_RDWR, &local_err);
857 + error_propagate(errp, local_err);
861 + } else if (format == BACKUP_FORMAT_DIR) {
862 + if (mkdir(backup_file, 0640) != 0) {
863 + error_setg_errno(errp, errno, "can't create directory '%s'\n",
867 + backup_dir = backup_file;
871 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
872 + l = g_list_next(l);
874 + const char *devname = bdrv_get_device_name(di->bs);
875 + snprintf(di->targetfile, PATH_MAX, "%s/%s.raw", backup_dir, devname);
877 + int flags = BDRV_O_RDWR;
878 + bdrv_img_create(di->targetfile, "raw", NULL, NULL, NULL,
879 + di->size, flags, false, &local_err);
881 + error_propagate(errp, local_err);
885 + di->target = bdrv_open(di->targetfile, NULL, NULL, flags, &local_err);
887 + error_propagate(errp, local_err);
892 + error_set(errp, ERROR_CLASS_GENERIC_ERROR, "unknown backup format");
896 + /* add configuration file to archive */
897 + if (has_config_file) {
898 + if(config_to_vma(config_file, format, backup_state.vmaobj, backup_dir, errp) != 0) {
903 + /* add firewall file to archive */
904 + if (has_firewall_file) {
905 + if(config_to_vma(firewall_file, format, backup_state.vmaobj, backup_dir, errp) != 0) {
909 + /* initialize global backup_state now */
911 + backup_state.cancel = false;
913 + if (backup_state.error) {
914 + error_free(backup_state.error);
915 + backup_state.error = NULL;
918 + backup_state.speed = (has_speed && speed > 0) ? speed : 0;
920 + backup_state.start_time = time(NULL);
921 + backup_state.end_time = 0;
923 + if (backup_state.backup_file) {
924 + g_free(backup_state.backup_file);
926 + backup_state.backup_file = g_strdup(backup_file);
928 + memcpy(&backup_state.uuid, &uuid, sizeof(uuid));
929 + qemu_uuid_unparse(&uuid, backup_state.uuid_str);
931 + qemu_mutex_lock(&backup_state.backup_mutex);
932 + backup_state.di_list = di_list;
933 + backup_state.next_job = 0;
935 + backup_state.total = total;
936 + backup_state.transferred = 0;
937 + backup_state.zero_bytes = 0;
939 + /* start all jobs (paused state) */
942 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
943 + l = g_list_next(l);
945 + job = backup_job_create(NULL, di->bs, di->target, speed, MIRROR_SYNC_MODE_FULL, NULL,
946 + false, BLOCKDEV_ON_ERROR_REPORT, BLOCKDEV_ON_ERROR_REPORT,
948 + pvebackup_complete_cb, di, 2, NULL, &local_err);
950 + bdrv_unref(di->target);
953 + if (!job || local_err != NULL) {
954 + error_setg(&backup_state.error, "backup_job_create failed");
955 + pvebackup_cancel(NULL);
957 + job_start(&job->job);
961 + qemu_mutex_unlock(&backup_state.backup_mutex);
963 + if (!backup_state.error) {
964 + pvebackup_run_next_job(); // run one job
967 + uuid_info = g_malloc0(sizeof(*uuid_info));
968 + uuid_info->UUID = g_strdup(backup_state.uuid_str);
976 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
977 + l = g_list_next(l);
980 + bdrv_unref(di->target);
983 + if (di->targetfile[0]) {
984 + unlink(di->targetfile);
988 + g_list_free(di_list);
994 + if (backup_state.vmaobj) {
995 + object_unparent(backup_state.vmaobj);
996 + backup_state.vmaobj = NULL;
1000 + rmdir(backup_dir);
1006 +BackupStatus *qmp_query_backup(Error **errp)
1008 + BackupStatus *info = g_malloc0(sizeof(*info));
1010 + if (!backup_state.start_time) {
1011 + /* not started, return {} */
1015 + info->has_status = true;
1016 + info->has_start_time = true;
1017 + info->start_time = backup_state.start_time;
1019 + if (backup_state.backup_file) {
1020 + info->has_backup_file = true;
1021 + info->backup_file = g_strdup(backup_state.backup_file);
1024 + info->has_uuid = true;
1025 + info->uuid = g_strdup(backup_state.uuid_str);
1027 + if (backup_state.end_time) {
1028 + if (backup_state.error) {
1029 + info->status = g_strdup("error");
1030 + info->has_errmsg = true;
1031 + info->errmsg = g_strdup(error_get_pretty(backup_state.error));
1033 + info->status = g_strdup("done");
1035 + info->has_end_time = true;
1036 + info->end_time = backup_state.end_time;
1038 + info->status = g_strdup("active");
1041 + info->has_total = true;
1042 + info->total = backup_state.total;
1043 + info->has_zero_bytes = true;
1044 + info->zero_bytes = backup_state.zero_bytes;
1045 + info->has_transferred = true;
1046 + info->transferred = backup_state.transferred;
1051 void qmp_block_stream(bool has_job_id, const char *job_id, const char *device,
1052 bool has_base, const char *base,
1053 bool has_base_node, const char *base_node,
1054 diff --git a/configure b/configure
1055 index 2a7796ea80..601c1f44f9 100755
1058 @@ -475,6 +475,7 @@ vxhs=""
1064 # cross compilers defaults, can be overridden with --cross-cc-ARCH
1065 cross_cc_aarch64="aarch64-linux-gnu-gcc"
1066 @@ -1435,6 +1436,10 @@ for opt do
1068 --disable-debug-mutex) debug_mutex=no
1070 + --enable-vma) vma=yes
1072 + --disable-vma) vma=no
1075 echo "ERROR: unknown option $opt"
1076 echo "Try '$0 --help' for more information"
1077 @@ -1710,6 +1715,7 @@ disabled with --disable-FEATURE, default is enabled if available:
1078 vhost-user vhost-user support
1079 capstone capstone disassembler support
1080 debug-mutex mutex debugging support
1081 + vma VMA archive backend
1083 NOTE: The object files are built at the place where configure is launched
1085 @@ -4124,6 +4130,22 @@ EOF
1088 ##########################################
1090 +if test "$vma" != "no" ; then
1091 + if $pkg_config --exact-version=0.1.0 vma; then
1093 + vma_cflags=$($pkg_config --cflags vma)
1094 + vma_libs=$($pkg_config --libs vma)
1096 + if test "$vma" = "yes" ; then
1097 + feature_not_found "VMA Archive backend support" \
1098 + "Install libvma devel"
1104 +##########################################
1108 @@ -6010,6 +6032,7 @@ echo "replication support $replication"
1109 echo "VxHS block device $vxhs"
1110 echo "capstone $capstone"
1111 echo "docker $docker"
1112 +echo "VMA support $vma"
1114 if test "$sdl_too_old" = "yes"; then
1115 echo "-> Your SDL version is too old - please upgrade to have SDL support"
1116 @@ -6496,6 +6519,12 @@ if test "$usb_redir" = "yes" ; then
1117 echo "USB_REDIR_LIBS=$usb_redir_libs" >> $config_host_mak
1120 +if test "$vma" = "yes" ; then
1121 + echo "CONFIG_VMA=y" >> $config_host_mak
1122 + echo "VMA_CFLAGS=$vma_cflags" >> $config_host_mak
1123 + echo "VMA_LIBS=$vma_libs" >> $config_host_mak
1126 if test "$opengl" = "yes" ; then
1127 echo "CONFIG_OPENGL=y" >> $config_host_mak
1128 echo "OPENGL_LIBS=$opengl_libs" >> $config_host_mak
1129 diff --git a/hmp-commands-info.hx b/hmp-commands-info.hx
1130 index 42c148fdc9..277e140092 100644
1131 --- a/hmp-commands-info.hx
1132 +++ b/hmp-commands-info.hx
1133 @@ -502,6 +502,19 @@ STEXI
1134 Show CPU statistics.
1141 + .help = "show backup status",
1142 + .cmd = hmp_info_backup,
1150 #if defined(CONFIG_SLIRP)
1153 diff --git a/hmp-commands.hx b/hmp-commands.hx
1154 index a6f0720442..956cbf04b9 100644
1155 --- a/hmp-commands.hx
1156 +++ b/hmp-commands.hx
1157 @@ -107,6 +107,37 @@ STEXI
1158 Copy data from a backing file into a block device.
1163 + .args_type = "directory:-d,backupfile:s,speed:o?,devlist:s?",
1164 + .params = "[-d] backupfile [speed [devlist]]",
1165 + .help = "create a VM Backup."
1166 + "\n\t\t\t Use -d to dump data into a directory instead"
1167 + "\n\t\t\t of using VMA format.",
1168 + .cmd = hmp_backup,
1174 +Create a VM backup.
1178 + .name = "backup_cancel",
1181 + .help = "cancel the current VM backup",
1182 + .cmd = hmp_backup_cancel,
1186 +@item backup_cancel
1187 +@findex backup_cancel
1188 +Cancel the current VM backup.
1193 .name = "block_job_set_speed",
1194 .args_type = "device:B,speed:o",
1195 diff --git a/hmp.c b/hmp.c
1196 index 7c975f3ead..8d659e20f6 100644
1199 @@ -166,6 +166,44 @@ void hmp_info_mice(Monitor *mon, const QDict *qdict)
1200 qapi_free_MouseInfoList(mice_list);
1203 +void hmp_info_backup(Monitor *mon, const QDict *qdict)
1205 + BackupStatus *info;
1207 + info = qmp_query_backup(NULL);
1208 + if (info->has_status) {
1209 + if (info->has_errmsg) {
1210 + monitor_printf(mon, "Backup status: %s - %s\n",
1211 + info->status, info->errmsg);
1213 + monitor_printf(mon, "Backup status: %s\n", info->status);
1217 + if (info->has_backup_file) {
1218 + monitor_printf(mon, "Start time: %s", ctime(&info->start_time));
1219 + if (info->end_time) {
1220 + monitor_printf(mon, "End time: %s", ctime(&info->end_time));
1223 + int per = (info->has_total && info->total &&
1224 + info->has_transferred && info->transferred) ?
1225 + (info->transferred * 100)/info->total : 0;
1226 + int zero_per = (info->has_total && info->total &&
1227 + info->has_zero_bytes && info->zero_bytes) ?
1228 + (info->zero_bytes * 100)/info->total : 0;
1229 + monitor_printf(mon, "Backup file: %s\n", info->backup_file);
1230 + monitor_printf(mon, "Backup uuid: %s\n", info->uuid);
1231 + monitor_printf(mon, "Total size: %zd\n", info->total);
1232 + monitor_printf(mon, "Transferred bytes: %zd (%d%%)\n",
1233 + info->transferred, per);
1234 + monitor_printf(mon, "Zero bytes: %zd (%d%%)\n",
1235 + info->zero_bytes, zero_per);
1238 + qapi_free_BackupStatus(info);
1241 void hmp_info_migrate(Monitor *mon, const QDict *qdict)
1243 MigrationInfo *info;
1244 @@ -1899,6 +1937,31 @@ void hmp_block_stream(Monitor *mon, const QDict *qdict)
1245 hmp_handle_error(mon, &error);
1248 +void hmp_backup_cancel(Monitor *mon, const QDict *qdict)
1250 + Error *error = NULL;
1252 + qmp_backup_cancel(&error);
1254 + hmp_handle_error(mon, &error);
1257 +void hmp_backup(Monitor *mon, const QDict *qdict)
1259 + Error *error = NULL;
1261 + int dir = qdict_get_try_bool(qdict, "directory", 0);
1262 + const char *backup_file = qdict_get_str(qdict, "backupfile");
1263 + const char *devlist = qdict_get_try_str(qdict, "devlist");
1264 + int64_t speed = qdict_get_try_int(qdict, "speed", 0);
1266 + qmp_backup(backup_file, true, dir ? BACKUP_FORMAT_DIR : BACKUP_FORMAT_VMA,
1267 + false, NULL, false, NULL, !!devlist,
1268 + devlist, qdict_haskey(qdict, "speed"), speed, &error);
1270 + hmp_handle_error(mon, &error);
1273 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
1275 Error *error = NULL;
1276 diff --git a/hmp.h b/hmp.h
1277 index 98bb7a44db..853f233195 100644
1280 @@ -29,6 +29,7 @@ void hmp_info_migrate(Monitor *mon, const QDict *qdict);
1281 void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict);
1282 void hmp_info_migrate_parameters(Monitor *mon, const QDict *qdict);
1283 void hmp_info_migrate_cache_size(Monitor *mon, const QDict *qdict);
1284 +void hmp_info_backup(Monitor *mon, const QDict *qdict);
1285 void hmp_info_cpus(Monitor *mon, const QDict *qdict);
1286 void hmp_info_block(Monitor *mon, const QDict *qdict);
1287 void hmp_info_blockstats(Monitor *mon, const QDict *qdict);
1288 @@ -86,6 +87,8 @@ void hmp_eject(Monitor *mon, const QDict *qdict);
1289 void hmp_change(Monitor *mon, const QDict *qdict);
1290 void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict);
1291 void hmp_block_stream(Monitor *mon, const QDict *qdict);
1292 +void hmp_backup(Monitor *mon, const QDict *qdict);
1293 +void hmp_backup_cancel(Monitor *mon, const QDict *qdict);
1294 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict);
1295 void hmp_block_job_cancel(Monitor *mon, const QDict *qdict);
1296 void hmp_block_job_pause(Monitor *mon, const QDict *qdict);
1297 diff --git a/qapi/block-core.json b/qapi/block-core.json
1298 index 5b9084a394..9c3c2d4917 100644
1299 --- a/qapi/block-core.json
1300 +++ b/qapi/block-core.json
1301 @@ -718,6 +718,97 @@
1307 +# Detailed backup status.
1309 +# @status: string describing the current backup status.
1310 +# This can be 'active', 'done', 'error'. If this field is not
1311 +# returned, no backup process has been initiated
1313 +# @errmsg: error message (only returned if status is 'error')
1315 +# @total: total amount of bytes involved in the backup process
1317 +# @transferred: amount of bytes already backed up.
1319 +# @zero-bytes: amount of 'zero' bytes detected.
1321 +# @start-time: time (epoch) when backup job started.
1323 +# @end-time: time (epoch) when backup job finished.
1325 +# @backup-file: backup file name
1327 +# @uuid: uuid for this backup job
1330 +{ 'struct': 'BackupStatus',
1331 + 'data': {'*status': 'str', '*errmsg': 'str', '*total': 'int',
1332 + '*transferred': 'int', '*zero-bytes': 'int',
1333 + '*start-time': 'int', '*end-time': 'int',
1334 + '*backup-file': 'str', '*uuid': 'str' } }
1339 +# An enumeration of supported backup formats.
1341 +# @vma: Proxmox vma backup format
1343 +{ 'enum': 'BackupFormat',
1344 + 'data': [ 'vma', 'dir' ] }
1349 +# Starts a VM backup.
1351 +# @backup-file: the backup file name
1353 +# @format: format of the backup file
1355 +# @config-file: a configuration file to include into
1356 +# the backup archive.
1358 +# @speed: the maximum speed, in bytes per second
1360 +# @devlist: list of block device names (separated by ',', ';'
1361 +# or ':'). By default the backup includes all writable block devices.
1363 +# Returns: the uuid of the backup job
1366 +{ 'command': 'backup', 'data': { 'backup-file': 'str',
1367 + '*format': 'BackupFormat',
1368 + '*config-file': 'str',
1369 + '*firewall-file': 'str',
1370 + '*devlist': 'str', '*speed': 'int' },
1371 + 'returns': 'UuidInfo' }
1376 +# Returns information about current/last backup task.
1378 +# Returns: @BackupStatus
1381 +{ 'command': 'query-backup', 'returns': 'BackupStatus' }
1386 +# Cancel the current executing backup process.
1388 +# Returns: nothing on success
1390 +# Notes: This command succeeds even if there is no backup process running.
1393 +{ 'command': 'backup-cancel' }
1396 # @BlockDeviceTimedStats:
1398 # Statistics of a block device during a given interval of time.
1399 @@ -2549,7 +2640,7 @@
1400 'host_cdrom', 'host_device', 'http', 'https', 'iscsi', 'luks',
1401 'nbd', 'nfs', 'null-aio', 'null-co', 'nvme', 'parallels', 'qcow',
1402 'qcow2', 'qed', 'quorum', 'raw', 'rbd', 'replication', 'sheepdog',
1403 - 'ssh', 'throttle', 'vdi', 'vhdx', 'vmdk', 'vpc', 'vvfat', 'vxhs' ] }
1404 + 'ssh', 'throttle', 'vdi', 'vhdx', 'vma-drive', 'vmdk', 'vpc', 'vvfat', 'vxhs' ] }
1407 # @BlockdevOptionsFile:
1408 @@ -3550,6 +3641,21 @@
1409 '*tls-creds': 'str' } }
1412 +# @BlockdevOptionsVMADrive:
1414 +# Driver specific block device options for VMA Drives
1416 +# @filename: vma-drive path
1418 +# @size: drive size in bytes
1422 +{ 'struct': 'BlockdevOptionsVMADrive',
1423 + 'data': { 'filename': 'str',
1427 # @BlockdevOptionsThrottle:
1429 # Driver specific block device options for the throttle driver
1430 @@ -3633,6 +3739,7 @@
1431 'throttle': 'BlockdevOptionsThrottle',
1432 'vdi': 'BlockdevOptionsGenericFormat',
1433 'vhdx': 'BlockdevOptionsGenericFormat',
1434 + 'vma-drive': 'BlockdevOptionsVMADrive',
1435 'vmdk': 'BlockdevOptionsGenericCOWFormat',
1436 'vpc': 'BlockdevOptionsGenericFormat',
1437 'vvfat': 'BlockdevOptionsVVFAT',
1438 diff --git a/qapi/common.json b/qapi/common.json
1439 index c367adc4b6..070b7b52c8 100644
1440 --- a/qapi/common.json
1441 +++ b/qapi/common.json
1442 @@ -149,3 +149,16 @@
1443 'ppc64', 'ppcemb', 'riscv32', 'riscv64', 's390x', 'sh4',
1444 'sh4eb', 'sparc', 'sparc64', 'tricore', 'unicore32',
1445 'x86_64', 'xtensa', 'xtensaeb' ] }
1450 +# Guest UUID information (Universally Unique Identifier).
1452 +# @UUID: the UUID of the guest
1456 +# Notes: If no UUID was specified for the guest, a null UUID is returned.
1458 +{ 'struct': 'UuidInfo', 'data': {'UUID': 'str'} }
1459 diff --git a/qapi/misc.json b/qapi/misc.json
1460 index b6ad5f028d..3dd5117fc3 100644
1461 --- a/qapi/misc.json
1462 +++ b/qapi/misc.json
1463 @@ -275,19 +275,6 @@
1464 { 'command': 'query-kvm', 'returns': 'KvmInfo' }
1469 -# Guest UUID information (Universally Unique Identifier).
1471 -# @UUID: the UUID of the guest
1475 -# Notes: If no UUID was specified for the guest, a null UUID is returned.
1477 -{ 'struct': 'UuidInfo', 'data': {'UUID': 'str'} }
1482 # Query the guest UUID information.