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 | 536 +++++++++++++++++++++++++++++++++++++++++++++++++++
13 hmp-commands-info.hx | 13 ++
14 hmp-commands.hx | 31 +++
17 qapi/block-core.json | 109 ++++++++++-
18 11 files changed, 1219 insertions(+), 2 deletions(-)
19 create mode 100644 block/vma.c
21 diff --git a/MAINTAINERS b/MAINTAINERS
22 index 0255113470..581d80d144 100644
25 @@ -1950,6 +1950,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 823e60cda6..d74140e413 100644
40 --- a/block/Makefile.objs
41 +++ b/block/Makefile.objs
42 @@ -22,6 +22,7 @@ block-obj-$(CONFIG_RBD) += rbd.o
43 block-obj-$(CONFIG_GLUSTERFS) += gluster.o
44 block-obj-$(CONFIG_VXHS) += vxhs.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 @@ -48,3 +49,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 a9ed9034b5..3ffd064c48 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 @@ -2963,6 +2965,540 @@ out:
504 aio_context_release(aio_context);
507 +/* PVE backup related function */
509 +static struct PVEBackupState {
522 + size_t transferred;
524 + QemuMutex backup_mutex;
525 + bool backup_mutex_initialized;
528 +typedef struct PVEBackupDevInfo {
529 + BlockDriverState *bs;
533 + char targetfile[PATH_MAX];
534 + BlockDriverState *target;
537 +static void pvebackup_run_next_job(void);
539 +static void pvebackup_cleanup(void)
541 + qemu_mutex_lock(&backup_state.backup_mutex);
542 + // Avoid race between block jobs and backup-cancel command:
543 + if (!backup_state.vmaw) {
544 + qemu_mutex_unlock(&backup_state.backup_mutex);
548 + backup_state.end_time = time(NULL);
550 + if (backup_state.vmaobj) {
551 + object_unparent(backup_state.vmaobj);
552 + backup_state.vmaobj = NULL;
555 + g_list_free(backup_state.di_list);
556 + backup_state.di_list = NULL;
557 + qemu_mutex_unlock(&backup_state.backup_mutex);
560 +static void pvebackup_complete_cb(void *opaque, int ret)
562 + // This always runs in the main loop
564 + PVEBackupDevInfo *di = opaque;
566 + di->completed = true;
568 + if (ret < 0 && !backup_state.error) {
569 + error_setg(&backup_state.error, "job failed with err %d - %s",
570 + ret, strerror(-ret));
576 + if (backup_state.vmaobj) {
577 + object_unparent(backup_state.vmaobj);
578 + backup_state.vmaobj = NULL;
581 + // remove self from job queue
582 + qemu_mutex_lock(&backup_state.backup_mutex);
583 + backup_state.di_list = g_list_remove(backup_state.di_list, di);
585 + qemu_mutex_unlock(&backup_state.backup_mutex);
587 + if (!backup_state.cancel) {
588 + pvebackup_run_next_job();
592 +static void pvebackup_cancel(void *opaque)
594 + backup_state.cancel = true;
595 + qemu_mutex_lock(&backup_state.backup_mutex);
596 + // Avoid race between block jobs and backup-cancel command:
597 + if (!backup_state.vmaw) {
598 + qemu_mutex_unlock(&backup_state.backup_mutex);
602 + if (!backup_state.error) {
603 + error_setg(&backup_state.error, "backup cancelled");
606 + if (backup_state.vmaobj) {
608 + /* make sure vma writer does not block anymore */
609 + if (!object_set_props(backup_state.vmaobj, &err, "blocked", "yes", NULL)) {
611 + error_report_err(err);
616 + GList *l = backup_state.di_list;
618 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
619 + l = g_list_next(l);
620 + if (!di->completed && di->bs) {
621 + BlockJob *job = di->bs->job;
623 + AioContext *aio_context = blk_get_aio_context(job->blk);
624 + aio_context_acquire(aio_context);
625 + if (!di->completed) {
626 + block_job_cancel(job);
628 + aio_context_release(aio_context);
633 + qemu_mutex_unlock(&backup_state.backup_mutex);
634 + pvebackup_cleanup();
637 +void qmp_backup_cancel(Error **errp)
639 + if (!backup_state.backup_mutex_initialized)
641 + Coroutine *co = qemu_coroutine_create(pvebackup_cancel, NULL);
642 + qemu_coroutine_enter(co);
644 + while (backup_state.vmaobj) {
645 + /* FIXME: Find something better for this */
646 + aio_poll(qemu_get_aio_context(), true);
650 +void vma_object_add_config_file(Object *obj, const char *name,
651 + const char *contents, size_t len,
653 +static int config_to_vma(const char *file, BackupFormat format,
655 + const char *backup_dir,
658 + char *cdata = NULL;
660 + GError *err = NULL;
661 + if (!g_file_get_contents(file, &cdata, &clen, &err)) {
662 + error_setg(errp, "unable to read file '%s'", file);
666 + char *basename = g_path_get_basename(file);
668 + if (format == BACKUP_FORMAT_VMA) {
669 + vma_object_add_config_file(vmaobj, basename, cdata, clen, errp);
670 + } else if (format == BACKUP_FORMAT_DIR) {
671 + char config_path[PATH_MAX];
672 + snprintf(config_path, PATH_MAX, "%s/%s", backup_dir, basename);
673 + if (!g_file_set_contents(config_path, cdata, clen, &err)) {
674 + error_setg(errp, "unable to write config file '%s'", config_path);
686 +void block_job_resume(BlockJob *job);
687 +static void pvebackup_run_next_job(void)
689 + qemu_mutex_lock(&backup_state.backup_mutex);
691 + GList *next = g_list_nth(backup_state.di_list, backup_state.next_job);
693 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)next->data;
694 + backup_state.next_job++;
695 + if (!di->completed && di->bs && di->bs->job) {
696 + BlockJob *job = di->bs->job;
697 + AioContext *aio_context = blk_get_aio_context(job->blk);
698 + aio_context_acquire(aio_context);
699 + qemu_mutex_unlock(&backup_state.backup_mutex);
700 + if (backup_state.error || backup_state.cancel) {
701 + block_job_cancel_sync(job);
703 + block_job_resume(job);
705 + aio_context_release(aio_context);
708 + next = g_list_next(next);
710 + qemu_mutex_unlock(&backup_state.backup_mutex);
712 + // no more jobs, run the cleanup
713 + pvebackup_cleanup();
716 +UuidInfo *qmp_backup(const char *backup_file, bool has_format,
717 + BackupFormat format,
718 + bool has_config_file, const char *config_file,
719 + bool has_firewall_file, const char *firewall_file,
720 + bool has_devlist, const char *devlist,
721 + bool has_speed, int64_t speed, Error **errp)
724 + BlockDriverState *bs = NULL;
725 + const char *backup_dir = NULL;
726 + Error *local_err = NULL;
728 + gchar **devs = NULL;
729 + GList *di_list = NULL;
731 + UuidInfo *uuid_info;
734 + if (!backup_state.backup_mutex_initialized) {
735 + qemu_mutex_init(&backup_state.backup_mutex);
736 + backup_state.backup_mutex_initialized = true;
739 + if (backup_state.di_list || backup_state.vmaobj) {
740 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
741 + "previous backup not finished");
745 + /* Todo: try to auto-detect format based on file name */
746 + format = has_format ? format : BACKUP_FORMAT_VMA;
749 + devs = g_strsplit_set(devlist, ",;:", -1);
753 + blk = blk_by_name(*d);
756 + if (bdrv_is_read_only(bs)) {
757 + error_setg(errp, "Node '%s' is read only", *d);
760 + if (!bdrv_is_inserted(bs)) {
761 + error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, *d);
764 + PVEBackupDevInfo *di = g_new0(PVEBackupDevInfo, 1);
766 + di_list = g_list_append(di_list, di);
768 + error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
769 + "Device '%s' not found", *d);
776 + BdrvNextIterator it;
779 + for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
780 + if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
784 + PVEBackupDevInfo *di = g_new0(PVEBackupDevInfo, 1);
786 + di_list = g_list_append(di_list, di);
791 + error_set(errp, ERROR_CLASS_GENERIC_ERROR, "empty device list");
799 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
800 + l = g_list_next(l);
801 + if (bdrv_op_is_blocked(di->bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
805 + ssize_t size = bdrv_getlength(di->bs);
807 + error_setg_errno(errp, -di->size, "bdrv_getlength failed");
814 + qemu_uuid_generate(&uuid);
816 + if (format == BACKUP_FORMAT_VMA) {
817 + char uuidstr[UUID_FMT_LEN+1];
818 + qemu_uuid_unparse(&uuid, uuidstr);
819 + uuidstr[UUID_FMT_LEN] = 0;
820 + backup_state.vmaobj =
821 + object_new_with_props("vma", object_get_objects_root(),
822 + "vma-backup-obj", &local_err,
823 + "filename", backup_file,
826 + if (!backup_state.vmaobj) {
828 + error_propagate(errp, local_err);
835 + QDict *options = qdict_new();
837 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
838 + l = g_list_next(l);
840 + const char *devname = bdrv_get_device_name(di->bs);
841 + snprintf(di->targetfile, PATH_MAX, "vma-backup-obj/%s.raw", devname);
843 + qdict_put(options, "driver", qstring_from_str("vma-drive"));
844 + qdict_put(options, "size", qint_from_int(di->size));
845 + di->target = bdrv_open(di->targetfile, NULL, options, BDRV_O_RDWR, &local_err);
847 + error_propagate(errp, local_err);
851 + } else if (format == BACKUP_FORMAT_DIR) {
852 + if (mkdir(backup_file, 0640) != 0) {
853 + error_setg_errno(errp, errno, "can't create directory '%s'\n",
857 + backup_dir = backup_file;
861 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
862 + l = g_list_next(l);
864 + const char *devname = bdrv_get_device_name(di->bs);
865 + snprintf(di->targetfile, PATH_MAX, "%s/%s.raw", backup_dir, devname);
867 + int flags = BDRV_O_RDWR;
868 + bdrv_img_create(di->targetfile, "raw", NULL, NULL, NULL,
869 + di->size, flags, false, &local_err);
871 + error_propagate(errp, local_err);
875 + di->target = bdrv_open(di->targetfile, NULL, NULL, flags, &local_err);
877 + error_propagate(errp, local_err);
882 + error_set(errp, ERROR_CLASS_GENERIC_ERROR, "unknown backup format");
886 + /* add configuration file to archive */
887 + if (has_config_file) {
888 + if(config_to_vma(config_file, format, backup_state.vmaobj, backup_dir, errp) != 0) {
893 + /* add firewall file to archive */
894 + if (has_firewall_file) {
895 + if(config_to_vma(firewall_file, format, backup_state.vmaobj, backup_dir, errp) != 0) {
899 + /* initialize global backup_state now */
901 + backup_state.cancel = false;
903 + if (backup_state.error) {
904 + error_free(backup_state.error);
905 + backup_state.error = NULL;
908 + backup_state.speed = (has_speed && speed > 0) ? speed : 0;
910 + backup_state.start_time = time(NULL);
911 + backup_state.end_time = 0;
913 + if (backup_state.backup_file) {
914 + g_free(backup_state.backup_file);
916 + backup_state.backup_file = g_strdup(backup_file);
918 + memcpy(&backup_state.uuid, &uuid, sizeof(uuid));
919 + qemu_uuid_unparse(&uuid, backup_state.uuid_str);
921 + qemu_mutex_lock(&backup_state.backup_mutex);
922 + backup_state.di_list = di_list;
923 + backup_state.next_job = 0;
925 + backup_state.total = total;
926 + backup_state.transferred = 0;
927 + backup_state.zero_bytes = 0;
929 + /* start all jobs (paused state) */
932 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
933 + l = g_list_next(l);
935 + job = backup_job_create(NULL, di->bs, di->target, speed, MIRROR_SYNC_MODE_FULL, NULL,
936 + false, BLOCKDEV_ON_ERROR_REPORT, BLOCKDEV_ON_ERROR_REPORT,
938 + pvebackup_complete_cb, di, 2, NULL, &local_err);
940 + bdrv_unref(di->target);
943 + if (!job || local_err != NULL) {
944 + error_setg(&backup_state.error, "backup_job_create failed");
945 + pvebackup_cancel(NULL);
947 + block_job_start(job);
951 + qemu_mutex_unlock(&backup_state.backup_mutex);
953 + if (!backup_state.error) {
954 + pvebackup_run_next_job(); // run one job
957 + uuid_info = g_malloc0(sizeof(*uuid_info));
958 + uuid_info->UUID = g_strdup(backup_state.uuid_str);
966 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
967 + l = g_list_next(l);
970 + bdrv_unref(di->target);
973 + if (di->targetfile[0]) {
974 + unlink(di->targetfile);
978 + g_list_free(di_list);
984 + if (backup_state.vmaobj) {
985 + object_unparent(backup_state.vmaobj);
986 + backup_state.vmaobj = NULL;
996 +BackupStatus *qmp_query_backup(Error **errp)
998 + BackupStatus *info = g_malloc0(sizeof(*info));
1000 + if (!backup_state.start_time) {
1001 + /* not started, return {} */
1005 + info->has_status = true;
1006 + info->has_start_time = true;
1007 + info->start_time = backup_state.start_time;
1009 + if (backup_state.backup_file) {
1010 + info->has_backup_file = true;
1011 + info->backup_file = g_strdup(backup_state.backup_file);
1014 + info->has_uuid = true;
1015 + info->uuid = g_strdup(backup_state.uuid_str);
1017 + if (backup_state.end_time) {
1018 + if (backup_state.error) {
1019 + info->status = g_strdup("error");
1020 + info->has_errmsg = true;
1021 + info->errmsg = g_strdup(error_get_pretty(backup_state.error));
1023 + info->status = g_strdup("done");
1025 + info->has_end_time = true;
1026 + info->end_time = backup_state.end_time;
1028 + info->status = g_strdup("active");
1031 + info->has_total = true;
1032 + info->total = backup_state.total;
1033 + info->has_zero_bytes = true;
1034 + info->zero_bytes = backup_state.zero_bytes;
1035 + info->has_transferred = true;
1036 + info->transferred = backup_state.transferred;
1041 void qmp_block_stream(bool has_job_id, const char *job_id, const char *device,
1042 bool has_base, const char *base,
1043 bool has_base_node, const char *base_node,
1044 diff --git a/blockjob.c b/blockjob.c
1045 index c1b6b6a810..2de9f8f4dd 100644
1048 @@ -149,7 +149,8 @@ static void block_job_pause(BlockJob *job)
1052 -static void block_job_resume(BlockJob *job)
1053 +void block_job_resume(BlockJob *job);
1054 +void block_job_resume(BlockJob *job)
1056 assert(job->pause_count > 0);
1058 diff --git a/configure b/configure
1059 index 0c6e7572db..3a28a0a092 100755
1062 @@ -422,6 +422,7 @@ tcmalloc="no"
1070 @@ -1313,6 +1314,10 @@ for opt do
1072 --disable-git-update) git_update=no
1074 + --disable-vma) vma="no"
1076 + --enable-vma) vma="yes"
1079 echo "ERROR: unknown option $opt"
1080 echo "Try '$0 --help' for more information"
1081 @@ -1561,6 +1566,7 @@ disabled with --disable-FEATURE, default is enabled if available:
1082 crypto-afalg Linux AF_ALG crypto backend driver
1083 vhost-user vhost-user support
1084 capstone capstone disassembler support
1085 + vma VMA archive backend
1087 NOTE: The object files are built at the place where configure is launched
1089 @@ -3890,6 +3896,23 @@ EOF
1092 ##########################################
1094 +if test "$vma" != "no" ; then
1095 + if $pkg_config --exact-version=0.1.0 vma; then
1097 + vma_cflags=$($pkg_config --cflags vma)
1098 + vma_libs=$($pkg_config --libs vma)
1100 + if test "$vma" = "yes" ; then
1101 + feature_not_found "VMA Archive backend support" \
1102 + "Install libvma devel"
1109 +##########################################
1113 @@ -5555,6 +5578,7 @@ echo "avx2 optimization $avx2_opt"
1114 echo "replication support $replication"
1115 echo "VxHS block device $vxhs"
1116 echo "capstone $capstone"
1117 +echo "VMA support $vma"
1119 if test "$sdl_too_old" = "yes"; then
1120 echo "-> Your SDL version is too old - please upgrade to have SDL support"
1121 @@ -5998,6 +6022,12 @@ if test "$libusb" = "yes" ; then
1122 echo "LIBUSB_LIBS=$libusb_libs" >> $config_host_mak
1125 +if test "$vma" = "yes" ; then
1126 + echo "CONFIG_VMA=y" >> $config_host_mak
1127 + echo "VMA_CFLAGS=$vma_cflags" >> $config_host_mak
1128 + echo "VMA_LIBS=$vma_libs" >> $config_host_mak
1131 if test "$usb_redir" = "yes" ; then
1132 echo "CONFIG_USB_REDIR=y" >> $config_host_mak
1133 echo "USB_REDIR_CFLAGS=$usb_redir_cflags" >> $config_host_mak
1134 diff --git a/hmp-commands-info.hx b/hmp-commands-info.hx
1135 index 3bf69a193c..7beeb29e99 100644
1136 --- a/hmp-commands-info.hx
1137 +++ b/hmp-commands-info.hx
1138 @@ -493,6 +493,19 @@ STEXI
1139 Show CPU statistics.
1146 + .help = "show backup status",
1147 + .cmd = hmp_info_backup,
1155 #if defined(CONFIG_SLIRP)
1158 diff --git a/hmp-commands.hx b/hmp-commands.hx
1159 index b35bc6ab6c..9e50947845 100644
1160 --- a/hmp-commands.hx
1161 +++ b/hmp-commands.hx
1162 @@ -87,6 +87,37 @@ STEXI
1163 Copy data from a backing file into a block device.
1168 + .args_type = "directory:-d,backupfile:s,speed:o?,devlist:s?",
1169 + .params = "[-d] backupfile [speed [devlist]]",
1170 + .help = "create a VM Backup."
1171 + "\n\t\t\t Use -d to dump data into a directory instead"
1172 + "\n\t\t\t of using VMA format.",
1173 + .cmd = hmp_backup,
1179 +Create a VM backup.
1183 + .name = "backup_cancel",
1186 + .help = "cancel the current VM backup",
1187 + .cmd = hmp_backup_cancel,
1191 +@item backup_cancel
1192 +@findex backup_cancel
1193 +Cancel the current VM backup.
1198 .name = "block_job_set_speed",
1199 .args_type = "device:B,speed:o",
1200 diff --git a/hmp.c b/hmp.c
1201 index b9ade681f0..241c2543ec 100644
1204 @@ -156,6 +156,44 @@ void hmp_info_mice(Monitor *mon, const QDict *qdict)
1205 qapi_free_MouseInfoList(mice_list);
1208 +void hmp_info_backup(Monitor *mon, const QDict *qdict)
1210 + BackupStatus *info;
1212 + info = qmp_query_backup(NULL);
1213 + if (info->has_status) {
1214 + if (info->has_errmsg) {
1215 + monitor_printf(mon, "Backup status: %s - %s\n",
1216 + info->status, info->errmsg);
1218 + monitor_printf(mon, "Backup status: %s\n", info->status);
1222 + if (info->has_backup_file) {
1223 + monitor_printf(mon, "Start time: %s", ctime(&info->start_time));
1224 + if (info->end_time) {
1225 + monitor_printf(mon, "End time: %s", ctime(&info->end_time));
1228 + int per = (info->has_total && info->total &&
1229 + info->has_transferred && info->transferred) ?
1230 + (info->transferred * 100)/info->total : 0;
1231 + int zero_per = (info->has_total && info->total &&
1232 + info->has_zero_bytes && info->zero_bytes) ?
1233 + (info->zero_bytes * 100)/info->total : 0;
1234 + monitor_printf(mon, "Backup file: %s\n", info->backup_file);
1235 + monitor_printf(mon, "Backup uuid: %s\n", info->uuid);
1236 + monitor_printf(mon, "Total size: %zd\n", info->total);
1237 + monitor_printf(mon, "Transferred bytes: %zd (%d%%)\n",
1238 + info->transferred, per);
1239 + monitor_printf(mon, "Zero bytes: %zd (%d%%)\n",
1240 + info->zero_bytes, zero_per);
1243 + qapi_free_BackupStatus(info);
1246 void hmp_info_migrate(Monitor *mon, const QDict *qdict)
1248 MigrationInfo *info;
1249 @@ -1848,6 +1886,31 @@ void hmp_block_stream(Monitor *mon, const QDict *qdict)
1250 hmp_handle_error(mon, &error);
1253 +void hmp_backup_cancel(Monitor *mon, const QDict *qdict)
1255 + Error *error = NULL;
1257 + qmp_backup_cancel(&error);
1259 + hmp_handle_error(mon, &error);
1262 +void hmp_backup(Monitor *mon, const QDict *qdict)
1264 + Error *error = NULL;
1266 + int dir = qdict_get_try_bool(qdict, "directory", 0);
1267 + const char *backup_file = qdict_get_str(qdict, "backupfile");
1268 + const char *devlist = qdict_get_try_str(qdict, "devlist");
1269 + int64_t speed = qdict_get_try_int(qdict, "speed", 0);
1271 + qmp_backup(backup_file, true, dir ? BACKUP_FORMAT_DIR : BACKUP_FORMAT_VMA,
1272 + false, NULL, false, NULL, !!devlist,
1273 + devlist, qdict_haskey(qdict, "speed"), speed, &error);
1275 + hmp_handle_error(mon, &error);
1278 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
1280 Error *error = NULL;
1281 diff --git a/hmp.h b/hmp.h
1282 index 45ada581b6..635b9b4218 100644
1285 @@ -31,6 +31,7 @@ void hmp_info_migrate(Monitor *mon, const QDict *qdict);
1286 void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict);
1287 void hmp_info_migrate_parameters(Monitor *mon, const QDict *qdict);
1288 void hmp_info_migrate_cache_size(Monitor *mon, const QDict *qdict);
1289 +void hmp_info_backup(Monitor *mon, const QDict *qdict);
1290 void hmp_info_cpus(Monitor *mon, const QDict *qdict);
1291 void hmp_info_block(Monitor *mon, const QDict *qdict);
1292 void hmp_info_blockstats(Monitor *mon, const QDict *qdict);
1293 @@ -85,6 +86,8 @@ void hmp_eject(Monitor *mon, const QDict *qdict);
1294 void hmp_change(Monitor *mon, const QDict *qdict);
1295 void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict);
1296 void hmp_block_stream(Monitor *mon, const QDict *qdict);
1297 +void hmp_backup(Monitor *mon, const QDict *qdict);
1298 +void hmp_backup_cancel(Monitor *mon, const QDict *qdict);
1299 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict);
1300 void hmp_block_job_cancel(Monitor *mon, const QDict *qdict);
1301 void hmp_block_job_pause(Monitor *mon, const QDict *qdict);
1302 diff --git a/qapi/block-core.json b/qapi/block-core.json
1303 index dd763dcf87..461fca9a3d 100644
1304 --- a/qapi/block-core.json
1305 +++ b/qapi/block-core.json
1306 @@ -615,6 +615,97 @@
1312 +# Detailed backup status.
1314 +# @status: string describing the current backup status.
1315 +# This can be 'active', 'done', 'error'. If this field is not
1316 +# returned, no backup process has been initiated
1318 +# @errmsg: error message (only returned if status is 'error')
1320 +# @total: total amount of bytes involved in the backup process
1322 +# @transferred: amount of bytes already backed up.
1324 +# @zero-bytes: amount of 'zero' bytes detected.
1326 +# @start-time: time (epoch) when backup job started.
1328 +# @end-time: time (epoch) when backup job finished.
1330 +# @backup-file: backup file name
1332 +# @uuid: uuid for this backup job
1335 +{ 'struct': 'BackupStatus',
1336 + 'data': {'*status': 'str', '*errmsg': 'str', '*total': 'int',
1337 + '*transferred': 'int', '*zero-bytes': 'int',
1338 + '*start-time': 'int', '*end-time': 'int',
1339 + '*backup-file': 'str', '*uuid': 'str' } }
1344 +# An enumeration of supported backup formats.
1346 +# @vma: Proxmox vma backup format
1348 +{ 'enum': 'BackupFormat',
1349 + 'data': [ 'vma', 'dir' ] }
1354 +# Starts a VM backup.
1356 +# @backup-file: the backup file name
1358 +# @format: format of the backup file
1360 +# @config-file: a configuration file to include into
1361 +# the backup archive.
1363 +# @speed: the maximum speed, in bytes per second
1365 +# @devlist: list of block device names (separated by ',', ';'
1366 +# or ':'). By default the backup includes all writable block devices.
1368 +# Returns: the uuid of the backup job
1371 +{ 'command': 'backup', 'data': { 'backup-file': 'str',
1372 + '*format': 'BackupFormat',
1373 + '*config-file': 'str',
1374 + '*firewall-file': 'str',
1375 + '*devlist': 'str', '*speed': 'int' },
1376 + 'returns': 'UuidInfo' }
1381 +# Returns information about current/last backup task.
1383 +# Returns: @BackupStatus
1386 +{ 'command': 'query-backup', 'returns': 'BackupStatus' }
1391 +# Cancel the current executing backup process.
1393 +# Returns: nothing on success
1395 +# Notes: This command succeeds even if there is no backup process running.
1398 +{ 'command': 'backup-cancel' }
1401 # @BlockDeviceTimedStats:
1403 # Statistics of a block device during a given interval of time.
1404 @@ -2239,7 +2330,7 @@
1405 'host_device', 'http', 'https', 'iscsi', 'luks', 'nbd', 'nfs',
1406 'null-aio', 'null-co', 'parallels', 'qcow', 'qcow2', 'qed',
1407 'quorum', 'raw', 'rbd', 'replication', 'sheepdog', 'ssh',
1408 - 'throttle', 'vdi', 'vhdx', 'vmdk', 'vpc', 'vvfat', 'vxhs' ] }
1409 + 'throttle', 'vdi', 'vhdx', 'vma-drive', 'vmdk', 'vpc', 'vvfat', 'vxhs' ] }
1412 # @BlockdevOptionsFile:
1413 @@ -3116,6 +3207,21 @@
1414 '*tls-creds': 'str' } }
1417 +# @BlockdevOptionsVMADrive:
1419 +# Driver specific block device options for VMA Drives
1421 +# @filename: vma-drive path
1423 +# @size: drive size in bytes
1427 +{ 'struct': 'BlockdevOptionsVMADrive',
1428 + 'data': { 'filename': 'str',
1432 # @BlockdevOptionsThrottle:
1434 # Driver specific block device options for the throttle driver
1435 @@ -3196,6 +3302,7 @@
1436 'throttle': 'BlockdevOptionsThrottle',
1437 'vdi': 'BlockdevOptionsGenericFormat',
1438 'vhdx': 'BlockdevOptionsGenericFormat',
1439 + 'vma-drive': 'BlockdevOptionsVMADrive',
1440 'vmdk': 'BlockdevOptionsGenericCOWFormat',
1441 'vpc': 'BlockdevOptionsGenericFormat',
1442 'vvfat': 'BlockdevOptionsVVFAT',