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] backup: introduce vma archive format
8 block/Makefile.objs | 3 +
9 block/vma.c | 424 +++++++++++++++++++++++++++++++++++++++++++
10 blockdev.c | 498 +++++++++++++++++++++++++++++++++++++++++++++++++++
12 hmp-commands-info.hx | 13 ++
13 hmp-commands.hx | 31 ++++
16 qapi-schema.json | 91 ++++++++++
17 qapi/block-core.json | 20 ++-
18 11 files changed, 1180 insertions(+), 2 deletions(-)
19 create mode 100644 block/vma.c
21 diff --git a/MAINTAINERS b/MAINTAINERS
22 index 430efb0ab7..6a7d338aad 100644
25 @@ -1811,6 +1811,12 @@ L: qemu-block@nongnu.org
30 +M: Wolfgang Bumiller <w.bumiller@proxmox.com>.
31 +L: pve-devel@proxmox.com
36 M: Stefan Hajnoczi <stefanha@redhat.com>
37 L: qemu-block@nongnu.org
38 diff --git a/block/Makefile.objs b/block/Makefile.objs
39 index 8cdac08db5..6df5567dd3 100644
40 --- a/block/Makefile.objs
41 +++ b/block/Makefile.objs
42 @@ -21,6 +21,7 @@ block-obj-$(CONFIG_CURL) += curl.o
43 block-obj-$(CONFIG_RBD) += rbd.o
44 block-obj-$(CONFIG_GLUSTERFS) += gluster.o
45 block-obj-$(CONFIG_LIBSSH2) += ssh.o
46 +block-obj-$(CONFIG_VMA) += vma.o
47 block-obj-y += accounting.o dirty-bitmap.o
48 block-obj-y += write-threshold.o
49 block-obj-y += backup.o
50 @@ -45,3 +46,5 @@ block-obj-$(if $(CONFIG_BZIP2),m,n) += dmg-bz2.o
51 dmg-bz2.o-libs := $(BZIP2_LIBS)
53 linux-aio.o-libs := -laio
54 +vma.o-cflags := $(VMA_CFLAGS)
55 +vma.o-libs := $(VMA_LIBS)
56 diff --git a/block/vma.c b/block/vma.c
58 index 0000000000..7151514f94
63 + * VMA archive backend for QEMU, container object
65 + * Copyright (C) 2017 Proxmox Server Solutions GmbH
67 + * This work is licensed under the terms of the GNU GPL, version 2 or later.
68 + * See the COPYING file in the top-level directory.
73 +#include "qemu/osdep.h"
74 +#include "qemu/uuid.h"
75 +#include "qemu-common.h"
76 +#include "qapi/error.h"
77 +#include "qapi/qmp/qerror.h"
78 +#include "qapi/qmp/qstring.h"
79 +#include "qom/object.h"
80 +#include "qom/object_interfaces.h"
81 +#include "block/block_int.h"
83 +/* exported interface */
84 +void vma_object_add_config_file(Object *obj, const char *name,
85 + const char *contents, size_t len,
88 +#define TYPE_VMA_OBJECT "vma"
89 +#define VMA_OBJECT(obj) \
90 + OBJECT_CHECK(VMAObjectState, (obj), TYPE_VMA_OBJECT)
91 +#define VMA_OBJECT_GET_CLASS(obj) \
92 + OBJECT_GET_CLASS(VMAObjectClass, (obj), TYPE_VMA_OBJECT)
94 +typedef struct VMAObjectClass {
95 + ObjectClass parent_class;
98 +typedef struct VMAObjectState {
109 +static VMAObjectState *vma_by_id(const char *name)
114 + container = object_get_objects_root();
115 + obj = object_resolve_path_component(container, name);
117 + return VMA_OBJECT(obj);
120 +static void vma_object_class_complete(UserCreatable *uc, Error **errp)
123 + VMAObjectState *vo = VMA_OBJECT(uc);
124 + VMAObjectClass *voc = VMA_OBJECT_GET_CLASS(uc);
128 + if (!vo->filename) {
129 + error_setg(errp, "Parameter 'filename' is required");
133 + rc = VMAWriter_fopen(vo->filename, &vo->vma);
135 + error_setg_errno(errp, -rc, "failed to create VMA archive");
139 + rc = VMAWriter_set_uuid(vo->vma, vo->uuid.data, sizeof(vo->uuid.data));
141 + error_setg_errno(errp, -rc, "failed to set UUID of VMA archive");
145 + qemu_mutex_init(&vo->mutex);
148 +static bool vma_object_can_be_deleted(UserCreatable *uc, Error **errp)
150 + //VMAObjectState *vo = VMA_OBJECT(uc);
158 +static void vma_object_class_init(ObjectClass *oc, void *data)
160 + UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
162 + ucc->can_be_deleted = vma_object_can_be_deleted;
163 + ucc->complete = vma_object_class_complete;
166 +static char *vma_object_get_filename(Object *obj, Error **errp)
168 + VMAObjectState *vo = VMA_OBJECT(obj);
170 + return g_strdup(vo->filename);
173 +static void vma_object_set_filename(Object *obj, const char *str, Error **errp)
175 + VMAObjectState *vo = VMA_OBJECT(obj);
178 + error_setg(errp, "filename cannot be changed after creation");
182 + g_free(vo->filename);
183 + vo->filename = g_strdup(str);
186 +static char *vma_object_get_uuid(Object *obj, Error **errp)
188 + VMAObjectState *vo = VMA_OBJECT(obj);
190 + return qemu_uuid_unparse_strdup(&vo->uuid);
193 +static void vma_object_set_uuid(Object *obj, const char *str, Error **errp)
195 + VMAObjectState *vo = VMA_OBJECT(obj);
198 + error_setg(errp, "uuid cannot be changed after creation");
202 + qemu_uuid_parse(str, &vo->uuid);
205 +static bool vma_object_get_blocked(Object *obj, Error **errp)
207 + VMAObjectState *vo = VMA_OBJECT(obj);
209 + return vo->blocked;
212 +static void vma_object_set_blocked(Object *obj, bool blocked, Error **errp)
214 + VMAObjectState *vo = VMA_OBJECT(obj);
218 + vo->blocked = blocked;
221 +void vma_object_add_config_file(Object *obj, const char *name,
222 + const char *contents, size_t len,
226 + VMAObjectState *vo = VMA_OBJECT(obj);
228 + if (!vo || !vo->vma) {
229 + error_setg(errp, "not a valid vma object to add config files to");
233 + rc = VMAWriter_addConfigFile(vo->vma, name, contents, len);
235 + error_setg_errno(errp, -rc, "failed to add config file to VMA");
240 +static void vma_object_init(Object *obj)
242 + VMAObjectState *vo = VMA_OBJECT(obj);
245 + object_property_add_str(obj, "filename",
246 + vma_object_get_filename, vma_object_set_filename,
248 + object_property_add_str(obj, "uuid",
249 + vma_object_get_uuid, vma_object_set_uuid,
251 + object_property_add_bool(obj, "blocked",
252 + vma_object_get_blocked, vma_object_set_blocked,
256 +static void vma_object_finalize(Object *obj)
258 + VMAObjectState *vo = VMA_OBJECT(obj);
259 + VMAObjectClass *voc = VMA_OBJECT_GET_CLASS(obj);
262 + qemu_mutex_destroy(&vo->mutex);
264 + VMAWriter_destroy(vo->vma, true);
265 + g_free(vo->filename);
268 +static const TypeInfo vma_object_info = {
269 + .name = TYPE_VMA_OBJECT,
270 + .parent = TYPE_OBJECT,
271 + .class_size = sizeof(VMAObjectClass),
272 + .class_init = vma_object_class_init,
273 + .instance_size = sizeof(VMAObjectState),
274 + .instance_init = vma_object_init,
275 + .instance_finalize = vma_object_finalize,
276 + .interfaces = (InterfaceInfo[]) {
277 + { TYPE_USER_CREATABLE },
282 +static void register_types(void)
284 + type_register_static(&vma_object_info);
287 +type_init(register_types);
290 + VMAObjectState *vma_obj;
293 + uint64_t byte_size;
296 +static void qemu_vma_parse_filename(const char *filename, QDict *options,
301 + sep = strchr(filename, '/');
302 + if (!sep || sep == filename) {
303 + error_setg(errp, "VMA filename should be <vma-object>/<device-name>");
307 + qdict_put(options, "vma", qstring_from_substr(filename, 0, sep-filename-1));
309 + while (*sep && *sep == '/')
312 + error_setg(errp, "missing device name\n");
316 + qdict_put(options, "name", qstring_from_str(sep));
319 +static QemuOptsList runtime_opts = {
320 + .name = "vma-drive",
321 + .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
325 + .type = QEMU_OPT_STRING,
326 + .help = "VMA Object name",
330 + .type = QEMU_OPT_STRING,
331 + .help = "VMA device name",
334 + .name = BLOCK_OPT_SIZE,
335 + .type = QEMU_OPT_SIZE,
336 + .help = "Virtual disk size"
338 + { /* end of list */ }
341 +static int qemu_vma_open(BlockDriverState *bs, QDict *options, int flags,
344 + Error *local_err = NULL;
345 + BDRVVMAState *s = bs->opaque;
347 + const char *vma_id, *device_name;
352 + opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
353 + qemu_opts_absorb_qdict(opts, options, &local_err);
355 + error_propagate(errp, local_err);
360 + bytes = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
363 + vma_id = qemu_opt_get(opts, "vma");
364 + device_name = qemu_opt_get(opts, "name");
366 + VMAObjectState *vma = vma_by_id(vma_id);
369 + error_setg(errp, "no such VMA object: %s", vma_id);
373 + dev_id = VMAWriter_findDevice(vma->vma, device_name);
375 + error_setg(errp, "drive already exists in VMA object");
380 + dev_id = VMAWriter_addDevice(vma->vma, device_name, (uint64_t)bytes);
382 + error_setg_errno(errp, -dev_id, "failed to add VMA device");
387 + object_ref(OBJECT(vma));
389 + s->name = g_strdup(device_name);
390 + s->device_id = (size_t)dev_id;
391 + s->byte_size = bytes;
396 + qemu_opts_del(opts);
400 +static void qemu_vma_close(BlockDriverState *bs)
402 + BDRVVMAState *s = bs->opaque;
404 + (void)VMAWriter_finishDevice(s->vma_obj->vma, s->device_id);
405 + object_unref(OBJECT(s->vma_obj));
410 +static int64_t qemu_vma_getlength(BlockDriverState *bs)
412 + BDRVVMAState *s = bs->opaque;
414 + return s->byte_size;
417 +static coroutine_fn int qemu_vma_co_writev(BlockDriverState *bs,
418 + int64_t sector_num,
420 + QEMUIOVector *qiov)
424 + BDRVVMAState *s = bs->opaque;
425 + VMAObjectState *vo = s->vma_obj;
426 + off_t offset = sector_num * BDRV_SECTOR_SIZE;
428 + qemu_mutex_lock(&vo->mutex);
432 + for (i = 0; i != qiov->niov; ++i) {
433 + const struct iovec *v = &qiov->iov[i];
434 + size_t blocks = v->iov_len / VMA_BLOCK_SIZE;
435 + if (blocks * VMA_BLOCK_SIZE != v->iov_len) {
438 + rc = VMAWriter_writeBlocks(vo->vma, s->device_id,
439 + v->iov_base, blocks, offset);
443 + if (rc != blocks) {
446 + offset += v->iov_len;
448 + qemu_mutex_unlock(&vo->mutex);
452 +static int qemu_vma_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
454 + bdi->cluster_size = VMA_CLUSTER_SIZE;
455 + bdi->unallocated_blocks_are_zero = true;
456 + bdi->can_write_zeroes_with_unmap = false;
460 +static BlockDriver bdrv_vma_drive = {
461 + .format_name = "vma-drive",
462 + .instance_size = sizeof(BDRVVMAState),
465 + .bdrv_create = qemu_vma_create,
466 + .create_opts = &qemu_vma_create_opts,
469 + .bdrv_parse_filename = qemu_vma_parse_filename,
470 + .bdrv_file_open = qemu_vma_open,
472 + .bdrv_close = qemu_vma_close,
473 + .bdrv_has_zero_init = bdrv_has_zero_init_1,
474 + .bdrv_getlength = qemu_vma_getlength,
475 + .bdrv_get_info = qemu_vma_get_info,
477 + .bdrv_co_writev = qemu_vma_co_writev,
480 +static void bdrv_vma_init(void)
482 + bdrv_register(&bdrv_vma_drive);
485 +block_init(bdrv_vma_init);
486 diff --git a/blockdev.c b/blockdev.c
487 index 9b6cfafd33..534c00f5da 100644
493 #include "qemu/osdep.h"
494 +#include "qemu/uuid.h"
495 #include "sysemu/block-backend.h"
496 #include "sysemu/blockdev.h"
497 #include "hw/block/block.h"
498 #include "block/blockjob.h"
499 +#include "block/blockjob_int.h"
500 #include "block/throttle-groups.h"
501 #include "monitor/monitor.h"
502 #include "qemu/error-report.h"
503 @@ -2932,6 +2934,502 @@ out:
504 aio_context_release(aio_context);
507 +/* PVE backup related function */
509 +static struct PVEBackupState {
522 + size_t transferred;
526 +typedef struct PVEBackupDevInfo {
527 + BlockDriverState *bs;
531 + char targetfile[PATH_MAX];
532 + BlockDriverState *target;
535 +static void pvebackup_run_next_job(void);
537 +static void pvebackup_cleanup(void)
539 + backup_state.end_time = time(NULL);
541 + if (backup_state.vmaobj) {
542 + object_unparent(backup_state.vmaobj);
543 + backup_state.vmaobj = NULL;
546 + if (backup_state.di_list) {
547 + GList *l = backup_state.di_list;
549 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
550 + l = g_list_next(l);
553 + g_list_free(backup_state.di_list);
554 + backup_state.di_list = NULL;
558 +static void pvebackup_complete_cb(void *opaque, int ret)
560 + PVEBackupDevInfo *di = opaque;
562 + di->completed = true;
564 + if (ret < 0 && !backup_state.error) {
565 + error_setg(&backup_state.error, "job failed with err %d - %s",
566 + ret, strerror(-ret));
572 + if (backup_state.vmaobj) {
573 + object_unparent(backup_state.vmaobj);
574 + backup_state.vmaobj = NULL;
577 + if (!backup_state.cancel) {
578 + pvebackup_run_next_job();
582 +static void pvebackup_cancel(void *opaque)
584 + backup_state.cancel = true;
586 + if (!backup_state.error) {
587 + error_setg(&backup_state.error, "backup cancelled");
590 + if (backup_state.vmaobj) {
592 + /* make sure vma writer does not block anymore */
593 + if (!object_set_props(backup_state.vmaobj, &err, "blocked", "yes", NULL)) {
595 + error_report_err(err);
600 + GList *l = backup_state.di_list;
602 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
603 + l = g_list_next(l);
604 + if (!di->completed && di->bs) {
605 + BlockJob *job = di->bs->job;
607 + if (!di->completed) {
608 + block_job_cancel_sync(job);
614 + pvebackup_cleanup();
617 +void qmp_backup_cancel(Error **errp)
619 + Coroutine *co = qemu_coroutine_create(pvebackup_cancel, NULL);
620 + qemu_coroutine_enter(co);
622 + while (backup_state.vmaobj) {
623 + /* FIXME: Find something better for this */
624 + aio_poll(qemu_get_aio_context(), true);
628 +void vma_object_add_config_file(Object *obj, const char *name,
629 + const char *contents, size_t len,
631 +static int config_to_vma(const char *file, BackupFormat format,
633 + const char *backup_dir,
636 + char *cdata = NULL;
638 + GError *err = NULL;
639 + if (!g_file_get_contents(file, &cdata, &clen, &err)) {
640 + error_setg(errp, "unable to read file '%s'", file);
644 + char *basename = g_path_get_basename(file);
646 + if (format == BACKUP_FORMAT_VMA) {
647 + vma_object_add_config_file(vmaobj, basename, cdata, clen, errp);
648 + } else if (format == BACKUP_FORMAT_DIR) {
649 + char config_path[PATH_MAX];
650 + snprintf(config_path, PATH_MAX, "%s/%s", backup_dir, basename);
651 + if (!g_file_set_contents(config_path, cdata, clen, &err)) {
652 + error_setg(errp, "unable to write config file '%s'", config_path);
665 +static void pvebackup_run_next_job(void)
667 + bool cancel = backup_state.error || backup_state.cancel;
668 + GList *next = g_list_nth(backup_state.di_list, backup_state.next_job);
670 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)next->data;
671 + backup_state.next_job++;
672 + if (!di->completed && di->bs && di->bs->job) {
673 + BlockJob *job = di->bs->job;
675 + block_job_cancel(job);
677 + block_job_resume(job);
681 + next = g_list_next(next);
683 + pvebackup_cleanup();
686 +UuidInfo *qmp_backup(const char *backup_file, bool has_format,
687 + BackupFormat format,
688 + bool has_config_file, const char *config_file,
689 + bool has_firewall_file, const char *firewall_file,
690 + bool has_devlist, const char *devlist,
691 + bool has_speed, int64_t speed, Error **errp)
694 + BlockDriverState *bs = NULL;
695 + const char *backup_dir = NULL;
696 + Error *local_err = NULL;
698 + gchar **devs = NULL;
699 + GList *di_list = NULL;
701 + UuidInfo *uuid_info;
704 + if (backup_state.di_list || backup_state.vmaobj) {
705 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
706 + "previous backup not finished");
710 + /* Todo: try to auto-detect format based on file name */
711 + format = has_format ? format : BACKUP_FORMAT_VMA;
714 + devs = g_strsplit_set(devlist, ",;:", -1);
718 + blk = blk_by_name(*d);
721 + if (bdrv_is_read_only(bs)) {
722 + error_setg(errp, "Node '%s' is read only", *d);
725 + if (!bdrv_is_inserted(bs)) {
726 + error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, *d);
729 + PVEBackupDevInfo *di = g_new0(PVEBackupDevInfo, 1);
731 + di_list = g_list_append(di_list, di);
733 + error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
734 + "Device '%s' not found", *d);
741 + BdrvNextIterator it;
744 + for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
745 + if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
749 + PVEBackupDevInfo *di = g_new0(PVEBackupDevInfo, 1);
751 + di_list = g_list_append(di_list, di);
756 + error_set(errp, ERROR_CLASS_GENERIC_ERROR, "empty device list");
764 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
765 + l = g_list_next(l);
766 + if (bdrv_op_is_blocked(di->bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
770 + ssize_t size = bdrv_getlength(di->bs);
772 + error_setg_errno(errp, -di->size, "bdrv_getlength failed");
779 + qemu_uuid_generate(&uuid);
781 + if (format == BACKUP_FORMAT_VMA) {
782 + char uuidstr[UUID_FMT_LEN+1];
783 + qemu_uuid_unparse(&uuid, uuidstr);
784 + uuidstr[UUID_FMT_LEN] = 0;
785 + backup_state.vmaobj =
786 + object_new_with_props("vma", object_get_objects_root(),
787 + "vma-backup-obj", &local_err,
788 + "filename", backup_file,
791 + if (!backup_state.vmaobj) {
793 + error_propagate(errp, local_err);
800 + QDict *options = qdict_new();
802 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
803 + l = g_list_next(l);
805 + const char *devname = bdrv_get_device_name(di->bs);
806 + snprintf(di->targetfile, PATH_MAX, "vma-backup-obj/%s.raw", devname);
808 + qdict_put(options, "driver", qstring_from_str("vma-drive"));
809 + qdict_put(options, "size", qint_from_int(di->size));
810 + di->target = bdrv_open(di->targetfile, NULL, options, BDRV_O_RDWR, &local_err);
812 + error_propagate(errp, local_err);
816 + } else if (format == BACKUP_FORMAT_DIR) {
817 + if (mkdir(backup_file, 0640) != 0) {
818 + error_setg_errno(errp, errno, "can't create directory '%s'\n",
822 + backup_dir = backup_file;
826 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
827 + l = g_list_next(l);
829 + const char *devname = bdrv_get_device_name(di->bs);
830 + snprintf(di->targetfile, PATH_MAX, "%s/%s.raw", backup_dir, devname);
832 + int flags = BDRV_O_RDWR;
833 + bdrv_img_create(di->targetfile, "raw", NULL, NULL, NULL,
834 + di->size, flags, &local_err, false);
836 + error_propagate(errp, local_err);
840 + di->target = bdrv_open(di->targetfile, NULL, NULL, flags, &local_err);
842 + error_propagate(errp, local_err);
847 + error_set(errp, ERROR_CLASS_GENERIC_ERROR, "unknown backup format");
851 + /* add configuration file to archive */
852 + if (has_config_file) {
853 + if(config_to_vma(config_file, format, backup_state.vmaobj, backup_dir, errp) != 0) {
858 + /* add firewall file to archive */
859 + if (has_firewall_file) {
860 + if(config_to_vma(firewall_file, format, backup_state.vmaobj, backup_dir, errp) != 0) {
864 + /* initialize global backup_state now */
866 + backup_state.cancel = false;
868 + if (backup_state.error) {
869 + error_free(backup_state.error);
870 + backup_state.error = NULL;
873 + backup_state.speed = (has_speed && speed > 0) ? speed : 0;
875 + backup_state.start_time = time(NULL);
876 + backup_state.end_time = 0;
878 + if (backup_state.backup_file) {
879 + g_free(backup_state.backup_file);
881 + backup_state.backup_file = g_strdup(backup_file);
883 + memcpy(&backup_state.uuid, &uuid, sizeof(uuid));
884 + qemu_uuid_unparse(&uuid, backup_state.uuid_str);
886 + backup_state.di_list = di_list;
887 + backup_state.next_job = 0;
889 + backup_state.total = total;
890 + backup_state.transferred = 0;
891 + backup_state.zero_bytes = 0;
893 + /* start all jobs (paused state) */
896 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
897 + l = g_list_next(l);
899 + job = backup_job_create(NULL, di->bs, di->target, speed, MIRROR_SYNC_MODE_FULL, NULL,
900 + false, BLOCKDEV_ON_ERROR_REPORT, BLOCKDEV_ON_ERROR_REPORT,
902 + pvebackup_complete_cb, di, 2, NULL, &local_err);
904 + bdrv_unref(di->target);
907 + if (!job || local_err != NULL) {
908 + error_setg(&backup_state.error, "backup_job_create failed");
909 + pvebackup_cancel(NULL);
911 + block_job_start(job);
915 + if (!backup_state.error) {
916 + pvebackup_run_next_job(); // run one job
919 + uuid_info = g_malloc0(sizeof(*uuid_info));
920 + uuid_info->UUID = g_strdup(backup_state.uuid_str);
928 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
929 + l = g_list_next(l);
932 + bdrv_unref(di->target);
935 + if (di->targetfile[0]) {
936 + unlink(di->targetfile);
940 + g_list_free(di_list);
946 + if (backup_state.vmaobj) {
947 + object_unparent(backup_state.vmaobj);
948 + backup_state.vmaobj = NULL;
958 +BackupStatus *qmp_query_backup(Error **errp)
960 + BackupStatus *info = g_malloc0(sizeof(*info));
962 + if (!backup_state.start_time) {
963 + /* not started, return {} */
967 + info->has_status = true;
968 + info->has_start_time = true;
969 + info->start_time = backup_state.start_time;
971 + if (backup_state.backup_file) {
972 + info->has_backup_file = true;
973 + info->backup_file = g_strdup(backup_state.backup_file);
976 + info->has_uuid = true;
977 + info->uuid = g_strdup(backup_state.uuid_str);
979 + if (backup_state.end_time) {
980 + if (backup_state.error) {
981 + info->status = g_strdup("error");
982 + info->has_errmsg = true;
983 + info->errmsg = g_strdup(error_get_pretty(backup_state.error));
985 + info->status = g_strdup("done");
987 + info->has_end_time = true;
988 + info->end_time = backup_state.end_time;
990 + info->status = g_strdup("active");
993 + info->has_total = true;
994 + info->total = backup_state.total;
995 + info->has_zero_bytes = true;
996 + info->zero_bytes = backup_state.zero_bytes;
997 + info->has_transferred = true;
998 + info->transferred = backup_state.transferred;
1003 void qmp_block_stream(bool has_job_id, const char *job_id, const char *device,
1004 bool has_base, const char *base,
1005 bool has_base_node, const char *base_node,
1006 diff --git a/configure b/configure
1007 index be4d326ae0..841f7a8fae 100755
1010 @@ -320,6 +320,7 @@ numa=""
1018 @@ -1183,6 +1184,10 @@ for opt do
1020 --enable-replication) replication="yes"
1022 + --disable-vma) vma="no"
1024 + --enable-vma) vma="yes"
1027 echo "ERROR: unknown option $opt"
1028 echo "Try '$0 --help' for more information"
1029 @@ -1427,6 +1432,7 @@ disabled with --disable-FEATURE, default is enabled if available:
1030 xfsctl xfsctl support
1031 qom-cast-debug cast debugging support
1032 tools build qemu-io, qemu-nbd and qemu-image tools
1033 + vma VMA archive backend
1035 NOTE: The object files are built at the place where configure is launched
1037 @@ -3705,6 +3711,23 @@ EOF
1040 ##########################################
1042 +if test "$vma" != "no" ; then
1043 + if $pkg_config --exact-version=0.1.0 vma; then
1045 + vma_cflags=$($pkg_config --cflags vma)
1046 + vma_libs=$($pkg_config --libs vma)
1048 + if test "$vma" = "yes" ; then
1049 + feature_not_found "VMA Archive backend support" \
1050 + "Install libvma devel"
1057 +##########################################
1061 @@ -5146,6 +5169,7 @@ echo "tcmalloc support $tcmalloc"
1062 echo "jemalloc support $jemalloc"
1063 echo "avx2 optimization $avx2_opt"
1064 echo "replication support $replication"
1065 +echo "VMA support $vma"
1067 if test "$sdl_too_old" = "yes"; then
1068 echo "-> Your SDL version is too old - please upgrade to have SDL support"
1069 @@ -5703,6 +5727,12 @@ if test "$libssh2" = "yes" ; then
1070 echo "LIBSSH2_LIBS=$libssh2_libs" >> $config_host_mak
1073 +if test "$vma" = "yes" ; then
1074 + echo "CONFIG_VMA=y" >> $config_host_mak
1075 + echo "VMA_CFLAGS=$vma_cflags" >> $config_host_mak
1076 + echo "VMA_LIBS=$vma_libs" >> $config_host_mak
1080 if test "$libusb" = "yes"; then
1081 echo "HOST_USB=libusb legacy" >> $config_host_mak
1082 diff --git a/hmp-commands-info.hx b/hmp-commands-info.hx
1083 index 5fc57a2210..3b5a0f95e4 100644
1084 --- a/hmp-commands-info.hx
1085 +++ b/hmp-commands-info.hx
1086 @@ -487,6 +487,19 @@ STEXI
1087 Show CPU statistics.
1094 + .help = "show backup status",
1095 + .cmd = hmp_info_backup,
1103 #if defined(CONFIG_SLIRP)
1106 diff --git a/hmp-commands.hx b/hmp-commands.hx
1107 index 58940a762b..a2867b56f2 100644
1108 --- a/hmp-commands.hx
1109 +++ b/hmp-commands.hx
1110 @@ -87,6 +87,37 @@ STEXI
1111 Copy data from a backing file into a block device.
1116 + .args_type = "directory:-d,backupfile:s,speed:o?,devlist:s?",
1117 + .params = "[-d] backupfile [speed [devlist]]",
1118 + .help = "create a VM Backup."
1119 + "\n\t\t\t Use -d to dump data into a directory instead"
1120 + "\n\t\t\t of using VMA format.",
1121 + .cmd = hmp_backup,
1127 +Create a VM backup.
1131 + .name = "backup_cancel",
1134 + .help = "cancel the current VM backup",
1135 + .cmd = hmp_backup_cancel,
1139 +@item backup_cancel
1140 +@findex backup_cancel
1141 +Cancel the current VM backup.
1146 .name = "block_job_set_speed",
1147 .args_type = "device:B,speed:o",
1148 diff --git a/hmp.c b/hmp.c
1149 index f725d061e6..12f1f46125 100644
1152 @@ -151,6 +151,44 @@ void hmp_info_mice(Monitor *mon, const QDict *qdict)
1153 qapi_free_MouseInfoList(mice_list);
1156 +void hmp_info_backup(Monitor *mon, const QDict *qdict)
1158 + BackupStatus *info;
1160 + info = qmp_query_backup(NULL);
1161 + if (info->has_status) {
1162 + if (info->has_errmsg) {
1163 + monitor_printf(mon, "Backup status: %s - %s\n",
1164 + info->status, info->errmsg);
1166 + monitor_printf(mon, "Backup status: %s\n", info->status);
1170 + if (info->has_backup_file) {
1171 + monitor_printf(mon, "Start time: %s", ctime(&info->start_time));
1172 + if (info->end_time) {
1173 + monitor_printf(mon, "End time: %s", ctime(&info->end_time));
1176 + int per = (info->has_total && info->total &&
1177 + info->has_transferred && info->transferred) ?
1178 + (info->transferred * 100)/info->total : 0;
1179 + int zero_per = (info->has_total && info->total &&
1180 + info->has_zero_bytes && info->zero_bytes) ?
1181 + (info->zero_bytes * 100)/info->total : 0;
1182 + monitor_printf(mon, "Backup file: %s\n", info->backup_file);
1183 + monitor_printf(mon, "Backup uuid: %s\n", info->uuid);
1184 + monitor_printf(mon, "Total size: %zd\n", info->total);
1185 + monitor_printf(mon, "Transferred bytes: %zd (%d%%)\n",
1186 + info->transferred, per);
1187 + monitor_printf(mon, "Zero bytes: %zd (%d%%)\n",
1188 + info->zero_bytes, zero_per);
1191 + qapi_free_BackupStatus(info);
1194 void hmp_info_migrate(Monitor *mon, const QDict *qdict)
1196 MigrationInfo *info;
1197 @@ -1613,6 +1651,31 @@ void hmp_block_stream(Monitor *mon, const QDict *qdict)
1198 hmp_handle_error(mon, &error);
1201 +void hmp_backup_cancel(Monitor *mon, const QDict *qdict)
1203 + Error *error = NULL;
1205 + qmp_backup_cancel(&error);
1207 + hmp_handle_error(mon, &error);
1210 +void hmp_backup(Monitor *mon, const QDict *qdict)
1212 + Error *error = NULL;
1214 + int dir = qdict_get_try_bool(qdict, "directory", 0);
1215 + const char *backup_file = qdict_get_str(qdict, "backupfile");
1216 + const char *devlist = qdict_get_try_str(qdict, "devlist");
1217 + int64_t speed = qdict_get_try_int(qdict, "speed", 0);
1219 + qmp_backup(backup_file, true, dir ? BACKUP_FORMAT_DIR : BACKUP_FORMAT_VMA,
1220 + false, NULL, false, NULL, !!devlist,
1221 + devlist, qdict_haskey(qdict, "speed"), speed, &error);
1223 + hmp_handle_error(mon, &error);
1226 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
1228 Error *error = NULL;
1229 diff --git a/hmp.h b/hmp.h
1230 index 0497afbf65..8c1b4846b3 100644
1233 @@ -31,6 +31,7 @@ void hmp_info_migrate(Monitor *mon, const QDict *qdict);
1234 void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict);
1235 void hmp_info_migrate_parameters(Monitor *mon, const QDict *qdict);
1236 void hmp_info_migrate_cache_size(Monitor *mon, const QDict *qdict);
1237 +void hmp_info_backup(Monitor *mon, const QDict *qdict);
1238 void hmp_info_cpus(Monitor *mon, const QDict *qdict);
1239 void hmp_info_block(Monitor *mon, const QDict *qdict);
1240 void hmp_info_blockstats(Monitor *mon, const QDict *qdict);
1241 @@ -80,6 +81,8 @@ void hmp_eject(Monitor *mon, const QDict *qdict);
1242 void hmp_change(Monitor *mon, const QDict *qdict);
1243 void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict);
1244 void hmp_block_stream(Monitor *mon, const QDict *qdict);
1245 +void hmp_backup(Monitor *mon, const QDict *qdict);
1246 +void hmp_backup_cancel(Monitor *mon, const QDict *qdict);
1247 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict);
1248 void hmp_block_job_cancel(Monitor *mon, const QDict *qdict);
1249 void hmp_block_job_pause(Monitor *mon, const QDict *qdict);
1250 diff --git a/qapi-schema.json b/qapi-schema.json
1251 index 5e82933ca1..b20020a054 100644
1252 --- a/qapi-schema.json
1253 +++ b/qapi-schema.json
1254 @@ -571,6 +571,97 @@
1255 { 'command': 'query-events', 'returns': ['EventInfo'] }
1260 +# Detailed backup status.
1262 +# @status: string describing the current backup status.
1263 +# This can be 'active', 'done', 'error'. If this field is not
1264 +# returned, no backup process has been initiated
1266 +# @errmsg: error message (only returned if status is 'error')
1268 +# @total: total amount of bytes involved in the backup process
1270 +# @transferred: amount of bytes already backed up.
1272 +# @zero-bytes: amount of 'zero' bytes detected.
1274 +# @start-time: time (epoch) when backup job started.
1276 +# @end-time: time (epoch) when backup job finished.
1278 +# @backup-file: backup file name
1280 +# @uuid: uuid for this backup job
1283 +{ 'struct': 'BackupStatus',
1284 + 'data': {'*status': 'str', '*errmsg': 'str', '*total': 'int',
1285 + '*transferred': 'int', '*zero-bytes': 'int',
1286 + '*start-time': 'int', '*end-time': 'int',
1287 + '*backup-file': 'str', '*uuid': 'str' } }
1292 +# An enumeration of supported backup formats.
1294 +# @vma: Proxmox vma backup format
1296 +{ 'enum': 'BackupFormat',
1297 + 'data': [ 'vma', 'dir' ] }
1302 +# Starts a VM backup.
1304 +# @backup-file: the backup file name
1306 +# @format: format of the backup file
1308 +# @config-file: a configuration file to include into
1309 +# the backup archive.
1311 +# @speed: the maximum speed, in bytes per second
1313 +# @devlist: list of block device names (separated by ',', ';'
1314 +# or ':'). By default the backup includes all writable block devices.
1316 +# Returns: the uuid of the backup job
1319 +{ 'command': 'backup', 'data': { 'backup-file': 'str',
1320 + '*format': 'BackupFormat',
1321 + '*config-file': 'str',
1322 + '*firewall-file': 'str',
1323 + '*devlist': 'str', '*speed': 'int' },
1324 + 'returns': 'UuidInfo' }
1329 +# Returns information about current/last backup task.
1331 +# Returns: @BackupStatus
1334 +{ 'command': 'query-backup', 'returns': 'BackupStatus' }
1339 +# Cancel the current executing backup process.
1341 +# Returns: nothing on success
1343 +# Notes: This command succeeds even if there is no backup process running.
1346 +{ 'command': 'backup-cancel' }
1351 # Detailed migration status.
1352 diff --git a/qapi/block-core.json b/qapi/block-core.json
1353 index 7ce90ec940..b0ffd3de4d 100644
1354 --- a/qapi/block-core.json
1355 +++ b/qapi/block-core.json
1356 @@ -2118,7 +2118,7 @@
1357 'host_device', 'http', 'https', 'iscsi', 'luks', 'nbd', 'nfs',
1358 'null-aio', 'null-co', 'parallels', 'qcow', 'qcow2', 'qed',
1359 'quorum', 'raw', 'rbd', 'replication', 'sheepdog', 'ssh',
1360 - 'vdi', 'vhdx', 'vmdk', 'vpc', 'vvfat' ] }
1361 + 'vdi', 'vhdx', 'vmdk', 'vpc', 'vvfat', 'vma-drive' ] }
1364 # @BlockdevOptionsFile:
1365 @@ -2895,6 +2895,21 @@
1366 'data': { '*offset': 'int', '*size': 'int' } }
1369 +# @BlockdevOptionsVMADrive:
1371 +# Driver specific block device options for VMA Drives
1373 +# @filename: vma-drive path
1375 +# @size: drive size in bytes
1379 +{ 'struct': 'BlockdevOptionsVMADrive',
1380 + 'data': { 'filename': 'str',
1386 # Options for creating a block device. Many options are available for all
1387 @@ -2956,7 +2971,8 @@
1388 'vhdx': 'BlockdevOptionsGenericFormat',
1389 'vmdk': 'BlockdevOptionsGenericCOWFormat',
1390 'vpc': 'BlockdevOptionsGenericFormat',
1391 - 'vvfat': 'BlockdevOptionsVVFAT'
1392 + 'vvfat': 'BlockdevOptionsVVFAT',
1393 + 'vma-drive': 'BlockdevOptionsVMADrive'