2 ===================================================================
3 --- new.orig/blockdev.c 2014-11-20 07:36:12.000000000 +0100
4 +++ new/blockdev.c 2014-11-20 07:47:31.000000000 +0100
6 #include "qmp-commands.h"
8 #include "sysemu/arch_init.h"
11 static const char *const if_name[IF_COUNT] = {
13 @@ -1954,6 +1955,439 @@
14 bdrv_put_ref_bh_schedule(bs);
17 +/* PVE backup related function */
19 +static struct PVEBackupState {
35 +typedef struct PVEBackupDevInfo {
36 + BlockDriverState *bs;
43 +static void pvebackup_run_next_job(void);
45 +static int pvebackup_dump_cb(void *opaque, BlockDriverState *target,
46 + int64_t sector_num, int n_sectors,
49 + PVEBackupDevInfo *di = opaque;
51 + if (sector_num & 0x7f) {
52 + if (!backup_state.error) {
53 + error_setg(&backup_state.error,
54 + "got unaligned write inside backup dump "
55 + "callback (sector %ld)", sector_num);
57 + return -1; // not aligned to cluster size
60 + int64_t cluster_num = sector_num >> 7;
61 + int size = n_sectors * BDRV_SECTOR_SIZE;
65 + if (backup_state.vmaw) {
66 + size_t zero_bytes = 0;
67 + ret = vma_writer_write(backup_state.vmaw, di->dev_id, cluster_num,
69 + backup_state.zero_bytes += zero_bytes;
73 + backup_state.zero_bytes += size;
77 + backup_state.transferred += size;
82 +static void pvebackup_cleanup(void)
84 + backup_state.end_time = time(NULL);
86 + if (backup_state.vmaw) {
87 + Error *local_err = NULL;
88 + vma_writer_close(backup_state.vmaw, &local_err);
89 + error_propagate(&backup_state.error, local_err);
90 + backup_state.vmaw = NULL;
93 + if (backup_state.di_list) {
94 + GList *l = backup_state.di_list;
96 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
100 + g_list_free(backup_state.di_list);
101 + backup_state.di_list = NULL;
105 +static void pvebackup_complete_cb(void *opaque, int ret)
107 + PVEBackupDevInfo *di = opaque;
109 + assert(backup_state.vmaw);
111 + di->completed = true;
113 + if (ret < 0 && !backup_state.error) {
114 + error_setg(&backup_state.error, "job failed with err %d - %s",
115 + ret, strerror(-ret));
118 + BlockDriverState *bs = di->bs;
122 + vma_writer_close_stream(backup_state.vmaw, di->dev_id);
124 + block_job_cb(bs, ret);
126 + if (!backup_state.cancel) {
127 + pvebackup_run_next_job();
131 +static void pvebackup_cancel(void *opaque)
133 + backup_state.cancel = true;
135 + if (!backup_state.error) {
136 + error_setg(&backup_state.error, "backup cancelled");
139 + /* drain all i/o (awake jobs waiting for aio) */
142 + GList *l = backup_state.di_list;
144 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
145 + l = g_list_next(l);
146 + if (!di->completed && di->bs) {
147 + BlockJob *job = di->bs->job;
149 + if (!di->completed) {
150 + block_job_cancel_sync(job);
156 + pvebackup_cleanup();
159 +void qmp_backup_cancel(Error **errp)
161 + Coroutine *co = qemu_coroutine_create(pvebackup_cancel);
162 + qemu_coroutine_enter(co, NULL);
164 + while (backup_state.vmaw) {
165 + /* vma writer use main aio context */
166 + aio_poll(qemu_get_aio_context(), true);
170 +static void pvebackup_run_next_job(void)
172 + GList *l = backup_state.di_list;
174 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
175 + l = g_list_next(l);
176 + if (!di->completed && di->bs && di->bs->job) {
177 + BlockJob *job = di->bs->job;
178 + if (block_job_is_paused(job)) {
179 + bool cancel = backup_state.error || backup_state.cancel;
181 + block_job_cancel(job);
183 + block_job_resume(job);
190 + pvebackup_cleanup();
193 +char *qmp_backup(const char *backup_file, bool has_format,
194 + BackupFormat format,
195 + bool has_config_file, const char *config_file,
196 + bool has_devlist, const char *devlist,
197 + bool has_speed, int64_t speed, Error **errp)
200 + BlockDriverState *bs = NULL;
201 + Error *local_err = NULL;
203 + VmaWriter *vmaw = NULL;
204 + gchar **devs = NULL;
205 + GList *di_list = NULL;
208 + if (backup_state.di_list) {
209 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
210 + "previous backup not finished");
214 + /* Todo: try to auto-detect format based on file name */
215 + format = has_format ? format : BACKUP_FORMAT_VMA;
217 + if (format != BACKUP_FORMAT_VMA) {
218 + error_set(errp, ERROR_CLASS_GENERIC_ERROR, "unknown backup format");
223 + devs = g_strsplit_set(devlist, ",;:", -1);
227 + blk = blk_by_name(*d);
230 + if (bdrv_is_read_only(bs)) {
231 + error_set(errp, QERR_DEVICE_IS_READ_ONLY, *d);
234 + if (!bdrv_is_inserted(bs)) {
235 + error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, *d);
238 + PVEBackupDevInfo *di = g_new0(PVEBackupDevInfo, 1);
240 + di_list = g_list_append(di_list, di);
242 + error_set(errp, QERR_DEVICE_NOT_FOUND, *d);
251 + while ((bs = bdrv_next(bs))) {
253 + if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
257 + PVEBackupDevInfo *di = g_new0(PVEBackupDevInfo, 1);
259 + di_list = g_list_append(di_list, di);
264 + error_set(errp, ERROR_CLASS_GENERIC_ERROR, "empty device list");
272 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
273 + l = g_list_next(l);
274 + if (bdrv_op_is_blocked(di->bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
278 + ssize_t size = bdrv_getlength(di->bs);
280 + error_setg_errno(errp, -di->size, "bdrv_getlength failed");
287 + uuid_generate(uuid);
289 + vmaw = vma_writer_create(backup_file, uuid, &local_err);
292 + error_propagate(errp, local_err);
297 + /* register all devices for vma writer */
300 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
301 + l = g_list_next(l);
303 + const char *devname = bdrv_get_device_name(di->bs);
304 + di->dev_id = vma_writer_register_stream(vmaw, devname, di->size);
305 + if (di->dev_id <= 0) {
306 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
307 + "register_stream failed");
312 + /* add configuration file to archive */
313 + if (has_config_file) {
314 + char *cdata = NULL;
316 + GError *err = NULL;
317 + if (!g_file_get_contents(config_file, &cdata, &clen, &err)) {
318 + error_setg(errp, "unable to read file '%s'", config_file);
322 + const char *basename = g_path_get_basename(config_file);
323 + if (vma_writer_add_config(vmaw, basename, cdata, clen) != 0) {
324 + error_setg(errp, "unable to add config data to vma archive");
331 + /* initialize global backup_state now */
333 + backup_state.cancel = false;
335 + if (backup_state.error) {
336 + error_free(backup_state.error);
337 + backup_state.error = NULL;
340 + backup_state.speed = (has_speed && speed > 0) ? speed : 0;
342 + backup_state.start_time = time(NULL);
343 + backup_state.end_time = 0;
345 + if (backup_state.backup_file) {
346 + g_free(backup_state.backup_file);
348 + backup_state.backup_file = g_strdup(backup_file);
350 + backup_state.vmaw = vmaw;
352 + uuid_copy(backup_state.uuid, uuid);
353 + uuid_unparse_lower(uuid, backup_state.uuid_str);
355 + backup_state.di_list = di_list;
357 + backup_state.total = total;
358 + backup_state.transferred = 0;
359 + backup_state.zero_bytes = 0;
361 + /* start all jobs (paused state) */
364 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
365 + l = g_list_next(l);
367 + backup_start(di->bs, NULL, speed, MIRROR_SYNC_MODE_FULL,
368 + BLOCKDEV_ON_ERROR_REPORT, BLOCKDEV_ON_ERROR_REPORT,
369 + pvebackup_dump_cb, pvebackup_complete_cb, di,
371 + if (local_err != NULL) {
372 + error_setg(&backup_state.error, "backup_job_create failed");
373 + pvebackup_cancel(NULL);
377 + if (!backup_state.error) {
378 + pvebackup_run_next_job(); // run one job
381 + return g_strdup(backup_state.uuid_str);
388 + l = g_list_next(l);
390 + g_list_free(di_list);
398 + vma_writer_close(vmaw, &err);
399 + unlink(backup_file);
405 +BackupStatus *qmp_query_backup(Error **errp)
407 + BackupStatus *info = g_malloc0(sizeof(*info));
409 + if (!backup_state.start_time) {
410 + /* not started, return {} */
414 + info->has_status = true;
415 + info->has_start_time = true;
416 + info->start_time = backup_state.start_time;
418 + if (backup_state.backup_file) {
419 + info->has_backup_file = true;
420 + info->backup_file = g_strdup(backup_state.backup_file);
423 + info->has_uuid = true;
424 + info->uuid = g_strdup(backup_state.uuid_str);
426 + if (backup_state.end_time) {
427 + if (backup_state.error) {
428 + info->status = g_strdup("error");
429 + info->has_errmsg = true;
430 + info->errmsg = g_strdup(error_get_pretty(backup_state.error));
432 + info->status = g_strdup("done");
434 + info->has_end_time = true;
435 + info->end_time = backup_state.end_time;
437 + info->status = g_strdup("active");
440 + info->has_total = true;
441 + info->total = backup_state.total;
442 + info->has_zero_bytes = true;
443 + info->zero_bytes = backup_state.zero_bytes;
444 + info->has_transferred = true;
445 + info->transferred = backup_state.transferred;
450 void qmp_block_stream(const char *device,
451 bool has_base, const char *base,
452 bool has_backing_file, const char *backing_file,
453 Index: new/hmp-commands.hx
454 ===================================================================
455 --- new.orig/hmp-commands.hx 2014-11-20 06:45:05.000000000 +0100
456 +++ new/hmp-commands.hx 2014-11-20 07:47:31.000000000 +0100
458 Copy data from a backing file into a block device.
463 + .args_type = "backupfile:s,speed:o?,devlist:s?",
464 + .params = "backupfile [speed [devlist]]",
465 + .help = "create a VM Backup.",
466 + .mhandler.cmd = hmp_backup,
476 + .name = "backup_cancel",
479 + .help = "cancel the current VM backup",
480 + .mhandler.cmd = hmp_backup_cancel,
485 +@findex backup_cancel
486 +Cancel the current VM backup.
491 .name = "block_job_set_speed",
492 .args_type = "device:B,speed:o",
493 @@ -1760,6 +1789,8 @@
496 show user network stack connection states
500 show migration status
501 @item info migrate_capabilities
503 ===================================================================
504 --- new.orig/hmp.c 2014-11-20 07:26:23.000000000 +0100
505 +++ new/hmp.c 2014-11-20 07:47:31.000000000 +0100
507 qapi_free_MouseInfoList(mice_list);
510 +void hmp_info_backup(Monitor *mon, const QDict *qdict)
512 + BackupStatus *info;
514 + info = qmp_query_backup(NULL);
515 + if (info->has_status) {
516 + if (info->has_errmsg) {
517 + monitor_printf(mon, "Backup status: %s - %s\n",
518 + info->status, info->errmsg);
520 + monitor_printf(mon, "Backup status: %s\n", info->status);
524 + if (info->has_backup_file) {
525 + monitor_printf(mon, "Start time: %s", ctime(&info->start_time));
526 + if (info->end_time) {
527 + monitor_printf(mon, "End time: %s", ctime(&info->end_time));
530 + int per = (info->has_total && info->total &&
531 + info->has_transferred && info->transferred) ?
532 + (info->transferred * 100)/info->total : 0;
533 + int zero_per = (info->has_total && info->total &&
534 + info->has_zero_bytes && info->zero_bytes) ?
535 + (info->zero_bytes * 100)/info->total : 0;
536 + monitor_printf(mon, "Backup file: %s\n", info->backup_file);
537 + monitor_printf(mon, "Backup uuid: %s\n", info->uuid);
538 + monitor_printf(mon, "Total size: %zd\n", info->total);
539 + monitor_printf(mon, "Transferred bytes: %zd (%d%%)\n",
540 + info->transferred, per);
541 + monitor_printf(mon, "Zero bytes: %zd (%d%%)\n",
542 + info->zero_bytes, zero_per);
545 + qapi_free_BackupStatus(info);
548 void hmp_info_migrate(Monitor *mon, const QDict *qdict)
551 @@ -1212,6 +1250,29 @@
553 hmp_handle_error(mon, &error);
556 +void hmp_backup_cancel(Monitor *mon, const QDict *qdict)
558 + Error *error = NULL;
560 + qmp_backup_cancel(&error);
562 + hmp_handle_error(mon, &error);
565 +void hmp_backup(Monitor *mon, const QDict *qdict)
567 + Error *error = NULL;
569 + const char *backup_file = qdict_get_str(qdict, "backupfile");
570 + const char *devlist = qdict_get_try_str(qdict, "devlist");
571 + int64_t speed = qdict_get_try_int(qdict, "speed", 0);
573 + qmp_backup(backup_file, true, BACKUP_FORMAT_VMA, false, NULL, !!devlist,
574 + devlist, qdict_haskey(qdict, "speed"), speed, &error);
576 + hmp_handle_error(mon, &error);
579 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
582 ===================================================================
583 --- new.orig/hmp.h 2014-11-20 06:45:05.000000000 +0100
584 +++ new/hmp.h 2014-11-20 07:47:31.000000000 +0100
586 void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict);
587 void hmp_info_migrate_parameters(Monitor *mon, const QDict *qdict);
588 void hmp_info_migrate_cache_size(Monitor *mon, const QDict *qdict);
589 +void hmp_info_backup(Monitor *mon, const QDict *qdict);
590 void hmp_info_cpus(Monitor *mon, const QDict *qdict);
591 void hmp_info_block(Monitor *mon, const QDict *qdict);
592 void hmp_info_blockstats(Monitor *mon, const QDict *qdict);
594 void hmp_change(Monitor *mon, const QDict *qdict);
595 void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict);
596 void hmp_block_stream(Monitor *mon, const QDict *qdict);
597 +void hmp_backup(Monitor *mon, const QDict *qdict);
598 +void hmp_backup_cancel(Monitor *mon, const QDict *qdict);
599 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict);
600 void hmp_block_job_cancel(Monitor *mon, const QDict *qdict);
601 void hmp_block_job_pause(Monitor *mon, const QDict *qdict);
603 ===================================================================
604 --- new.orig/monitor.c 2014-11-20 06:45:06.000000000 +0100
605 +++ new/monitor.c 2014-11-20 07:47:31.000000000 +0100
606 @@ -2848,6 +2848,13 @@
613 + .help = "show backup status",
614 + .mhandler.cmd = hmp_info_backup,
620 Index: new/qapi-schema.json
621 ===================================================================
622 --- new.orig/qapi-schema.json 2014-11-20 07:26:43.000000000 +0100
623 +++ new/qapi-schema.json 2014-11-20 07:47:31.000000000 +0100
626 { 'command': 'query-events', 'returns': ['EventInfo'] }
630 +# Detailed backup status.
632 +# @status: #optional string describing the current backup status.
633 +# This can be 'active', 'done', 'error'. If this field is not
634 +# returned, no backup process has been initiated
636 +# @errmsg: #optional error message (only returned if status is 'error')
638 +# @total: #optional total amount of bytes involved in the backup process
640 +# @transferred: #optional amount of bytes already backed up.
642 +# @zero-bytes: #optional amount of 'zero' bytes detected.
644 +# @start-time: #optional time (epoch) when backup job started.
646 +# @end-time: #optional time (epoch) when backup job finished.
648 +# @backupfile: #optional backup file name
650 +# @uuid: #optional uuid for this backup job
653 +{ 'struct': 'BackupStatus',
654 + 'data': {'*status': 'str', '*errmsg': 'str', '*total': 'int',
655 + '*transferred': 'int', '*zero-bytes': 'int',
656 + '*start-time': 'int', '*end-time': 'int',
657 + '*backup-file': 'str', '*uuid': 'str' } }
662 +# An enumeration of supported backup formats.
664 +# @vma: Proxmox vma backup format
666 +{ 'enum': 'BackupFormat',
667 + 'data': [ 'vma' ] }
672 +# Starts a VM backup.
674 +# @backup-file: the backup file name
676 +# @format: format of the backup file
678 +# @config-filename: #optional name of a configuration file to include into
679 +# the backup archive.
681 +# @speed: #optional the maximum speed, in bytes per second
683 +# @devlist: #optional list of block device names (separated by ',', ';'
684 +# or ':'). By default the backup includes all writable block devices.
686 +# Returns: the uuid of the backup job
689 +{ 'command': 'backup', 'data': { 'backup-file': 'str',
690 + '*format': 'BackupFormat',
691 + '*config-file': 'str',
692 + '*devlist': 'str', '*speed': 'int' },
693 + 'returns': 'UuidInfo' }
698 +# Returns information about current/last backup task.
700 +# Returns: @BackupStatus
703 +{ 'command': 'query-backup', 'returns': 'BackupStatus' }
708 +# Cancel the current executing backup process.
710 +# Returns: nothing on success
712 +# Notes: This command succeeds even if there is no backup process running.
715 +{ 'command': 'backup-cancel' }
720 Index: new/qmp-commands.hx
721 ===================================================================
722 --- new.orig/qmp-commands.hx 2014-11-20 07:26:23.000000000 +0100
723 +++ new/qmp-commands.hx 2014-11-20 07:47:31.000000000 +0100
724 @@ -1097,6 +1097,24 @@
729 + .args_type = "backup-file:s,format:s?,config-file:F?,speed:o?,devlist:s?",
730 + .mhandler.cmd_new = qmp_marshal_input_backup,
734 + .name = "backup-cancel",
736 + .mhandler.cmd_new = qmp_marshal_input_backup_cancel,
740 + .name = "query-backup",
742 + .mhandler.cmd_new = qmp_marshal_input_query_backup,
746 .name = "block-job-set-speed",
747 .args_type = "device:B,speed:o",
748 .mhandler.cmd_new = qmp_marshal_input_block_job_set_speed,