2 ===================================================================
3 --- new.orig/blockdev.c 2013-12-02 12:54:24.000000000 +0100
4 +++ new/blockdev.c 2013-12-02 14:10:24.000000000 +0100
6 #include "qmp-commands.h"
8 #include "sysemu/arch_init.h"
11 static QTAILQ_HEAD(drivelist, DriveInfo) drives = QTAILQ_HEAD_INITIALIZER(drives);
13 @@ -1438,6 +1439,418 @@
17 +/* PVE backup related function */
19 +static struct PVEBackupState {
35 +typedef struct PVEBackupDevInfo {
36 + BlockDriverState *bs;
42 +static void pvebackup_run_next_job(void);
44 +static int pvebackup_dump_cb(void *opaque, BlockDriverState *bs,
45 + int64_t cluster_num, unsigned char *buf)
47 + PVEBackupDevInfo *di = opaque;
49 + assert(backup_state.vmaw);
51 + size_t zero_bytes = 0;
52 + return vma_writer_write(backup_state.vmaw, di->dev_id, cluster_num,
56 +static void pvebackup_update_status(void)
58 + g_assert(backup_state.vmaw);
62 + vma_writer_get_status(backup_state.vmaw, &vmastat);
65 + uint64_t transferred = 0;
66 + uint64_t zero_bytes = 0;
69 + for (i = 0; i < 256; i++) {
70 + if (vmastat.stream_info[i].size) {
71 + total += vmastat.stream_info[i].size;
72 + transferred += vmastat.stream_info[i].transferred;
73 + zero_bytes += vmastat.stream_info[i].zero_bytes;
77 + backup_state.total = total;
78 + backup_state.transferred = transferred;
79 + backup_state.zero_bytes = zero_bytes;
82 +static void pvebackup_cleanup(void)
84 + if (backup_state.vmaw) {
85 + backup_state.end_time = time(NULL);
86 + Error *local_err = NULL;
87 + pvebackup_update_status();
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 + vma_writer_close_stream(backup_state.vmaw, di->dev_id);
115 + if (!backup_state.cancel) {
116 + pvebackup_run_next_job();
120 +static void pvebackup_cancel(void *opaque)
122 + backup_state.cancel = true;
124 + if (!backup_state.error) {
125 + error_setg(&backup_state.error, "backup cancelled");
128 + /* drain all i/o (awake jobs waiting for aio) */
132 + GList *l = backup_state.di_list;
134 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
135 + l = g_list_next(l);
136 + BlockJob *job = di->bs->job;
139 + if (!di->completed) {
140 + block_job_cancel_sync(job);
145 + pvebackup_cleanup();
148 +void qmp_backup_cancel(Error **errp)
150 + Coroutine *co = qemu_coroutine_create(pvebackup_cancel);
151 + qemu_coroutine_enter(co, NULL);
153 + while (backup_state.vmaw) {
158 +static void pvebackup_run_next_job(void)
160 + GList *l = backup_state.di_list;
162 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
163 + l = g_list_next(l);
164 + BlockJob *job = di->bs->job;
165 + if (di->bs && job && !di->completed) {
166 + if (block_job_is_paused(job)) {
167 + bool cancel = backup_state.error || backup_state.cancel;
169 + block_job_cancel(job);
171 + block_job_resume(job);
178 + pvebackup_cleanup();
181 +char *qmp_backup(const char *backup_file, bool has_format,
182 + BackupFormat format,
183 + bool has_config_file, const char *config_file,
184 + bool has_devlist, const char *devlist,
185 + bool has_speed, int64_t speed, Error **errp)
187 + BlockDriverState *bs;
188 + Error *local_err = NULL;
190 + VmaWriter *vmaw = NULL;
191 + gchar **devs = NULL;
192 + GList *di_list = NULL;
195 + if (backup_state.di_list) {
196 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
197 + "previous backup not finished");
201 + /* Todo: try to auto-detect format based on file name */
202 + format = has_format ? format : BACKUP_FORMAT_VMA;
204 + if (format != BACKUP_FORMAT_VMA) {
205 + error_set(errp, ERROR_CLASS_GENERIC_ERROR, "unknown backup format");
210 + devs = g_strsplit_set(devlist, ",;:", -1);
214 + bs = bdrv_find(*d);
216 + if (bdrv_is_read_only(bs)) {
217 + error_set(errp, QERR_DEVICE_IS_READ_ONLY, *d);
220 + if (!bdrv_is_inserted(bs)) {
221 + error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, *d);
224 + PVEBackupDevInfo *di = g_new0(PVEBackupDevInfo, 1);
226 + di_list = g_list_append(di_list, di);
228 + error_set(errp, QERR_DEVICE_NOT_FOUND, *d);
237 + while ((bs = bdrv_next(bs))) {
239 + if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
243 + PVEBackupDevInfo *di = g_new0(PVEBackupDevInfo, 1);
245 + di_list = g_list_append(di_list, di);
250 + error_set(errp, ERROR_CLASS_GENERIC_ERROR, "empty device list");
256 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
257 + l = g_list_next(l);
259 + error_set(errp, QERR_DEVICE_IN_USE, bdrv_get_device_name(di->bs));
264 + uuid_generate(uuid);
266 + vmaw = vma_writer_create(backup_file, uuid, &local_err);
268 + if (error_is_set(&local_err)) {
269 + error_propagate(errp, local_err);
274 + /* register all devices for vma writer */
277 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
278 + l = g_list_next(l);
280 + int64_t size = bdrv_getlength(di->bs);
281 + const char *devname = bdrv_get_device_name(di->bs);
282 + di->dev_id = vma_writer_register_stream(vmaw, devname, size);
283 + if (di->dev_id <= 0) {
284 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
285 + "register_stream failed");
290 + /* add configuration file to archive */
291 + if (has_config_file) {
292 + char *cdata = NULL;
294 + GError *err = NULL;
295 + if (!g_file_get_contents(config_file, &cdata, &clen, &err)) {
296 + error_setg(errp, "unable to read file '%s'", config_file);
300 + const char *basename = g_path_get_basename(config_file);
301 + if (vma_writer_add_config(vmaw, basename, cdata, clen) != 0) {
302 + error_setg(errp, "unable to add config data to vma archive");
309 + /* initialize global backup_state now */
311 + backup_state.cancel = false;
313 + if (backup_state.error) {
314 + error_free(backup_state.error);
315 + backup_state.error = NULL;
318 + backup_state.speed = (has_speed && speed > 0) ? speed : 0;
320 + backup_state.start_time = time(NULL);
321 + backup_state.end_time = 0;
323 + if (backup_state.backup_file) {
324 + g_free(backup_state.backup_file);
326 + backup_state.backup_file = g_strdup(backup_file);
328 + backup_state.vmaw = vmaw;
330 + uuid_copy(backup_state.uuid, uuid);
331 + uuid_unparse_lower(uuid, backup_state.uuid_str);
333 + backup_state.di_list = di_list;
335 + pvebackup_update_status();
337 + /* start all jobs (paused state) */
340 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
341 + l = g_list_next(l);
343 + backup_start(di->bs, NULL, speed, MIRROR_SYNC_MODE_FULL,
344 + BLOCKDEV_ON_ERROR_REPORT, BLOCKDEV_ON_ERROR_REPORT,
345 + pvebackup_dump_cb, pvebackup_complete_cb, di,
347 + if (local_err != NULL) {
348 + error_setg(&backup_state.error, "backup_job_create failed");
349 + pvebackup_cancel(NULL);
353 + if (!backup_state.error) {
354 + pvebackup_run_next_job(); // run one job
357 + return g_strdup(backup_state.uuid_str);
364 + l = g_list_next(l);
366 + g_list_free(di_list);
374 + vma_writer_close(vmaw, &err);
375 + unlink(backup_file);
381 +BackupStatus *qmp_query_backup(Error **errp)
383 + BackupStatus *info = g_malloc0(sizeof(*info));
385 + if (!backup_state.start_time) {
386 + /* not started, return {} */
390 + info->has_status = true;
391 + info->has_start_time = true;
392 + info->start_time = backup_state.start_time;
394 + if (backup_state.backup_file) {
395 + info->has_backup_file = true;
396 + info->backup_file = g_strdup(backup_state.backup_file);
399 + info->has_uuid = true;
400 + info->uuid = g_strdup(backup_state.uuid_str);
402 + if (backup_state.end_time) {
403 + if (backup_state.error) {
404 + info->status = g_strdup("error");
405 + info->has_errmsg = true;
406 + info->errmsg = g_strdup(error_get_pretty(backup_state.error));
408 + info->status = g_strdup("done");
410 + info->has_end_time = true;
411 + info->end_time = backup_state.end_time;
413 + info->status = g_strdup("active");
416 + if (backup_state.vmaw) {
417 + pvebackup_update_status();
420 + info->has_total = true;
421 + info->total = backup_state.total;
422 + info->has_zero_bytes = true;
423 + info->zero_bytes = backup_state.zero_bytes;
424 + info->has_transferred = true;
425 + info->transferred = backup_state.transferred;
430 static void eject_device(BlockDriverState *bs, int force, Error **errp)
432 Index: new/hmp-commands.hx
433 ===================================================================
434 --- new.orig/hmp-commands.hx 2013-12-02 12:49:36.000000000 +0100
435 +++ new/hmp-commands.hx 2013-12-02 12:58:31.000000000 +0100
437 Copy data from a backing file into a block device.
442 + .args_type = "backupfile:s,speed:o?,devlist:s?",
443 + .params = "backupfile [speed [devlist]]",
444 + .help = "create a VM Backup.",
445 + .mhandler.cmd = hmp_backup,
455 + .name = "backup_cancel",
458 + .help = "cancel the current VM backup",
459 + .mhandler.cmd = hmp_backup_cancel,
464 +@findex backup_cancel
465 +Cancel the current VM backup.
470 .name = "block_job_set_speed",
471 .args_type = "device:B,speed:o",
472 @@ -1692,6 +1721,8 @@
475 show user network stack connection states
479 show migration status
480 @item info migrate_capabilities
482 ===================================================================
483 --- new.orig/hmp.c 2013-12-02 12:49:36.000000000 +0100
484 +++ new/hmp.c 2013-12-02 12:58:31.000000000 +0100
486 qapi_free_MouseInfoList(mice_list);
489 +void hmp_info_backup(Monitor *mon, const QDict *qdict)
491 + BackupStatus *info;
493 + info = qmp_query_backup(NULL);
494 + if (info->has_status) {
495 + if (info->has_errmsg) {
496 + monitor_printf(mon, "Backup status: %s - %s\n",
497 + info->status, info->errmsg);
499 + monitor_printf(mon, "Backup status: %s\n", info->status);
502 + if (info->has_backup_file) {
503 + int per = (info->has_total && info->total &&
504 + info->has_transferred && info->transferred) ?
505 + (info->transferred * 100)/info->total : 0;
506 + int zero_per = (info->has_total && info->total &&
507 + info->has_zero_bytes && info->zero_bytes) ?
508 + (info->zero_bytes * 100)/info->total : 0;
509 + monitor_printf(mon, "Backup file: %s\n", info->backup_file);
510 + monitor_printf(mon, "Backup uuid: %s\n", info->uuid);
511 + monitor_printf(mon, "Total size: %zd\n", info->total);
512 + monitor_printf(mon, "Transferred bytes: %zd (%d%%)\n",
513 + info->transferred, per);
514 + monitor_printf(mon, "Zero bytes: %zd (%d%%)\n",
515 + info->zero_bytes, zero_per);
518 + qapi_free_BackupStatus(info);
521 void hmp_info_migrate(Monitor *mon, const QDict *qdict)
524 @@ -1194,6 +1226,37 @@
525 hmp_handle_error(mon, &error);
528 +void hmp_backup_cancel(Monitor *mon, const QDict *qdict)
530 + Error *errp = NULL;
532 + qmp_backup_cancel(&errp);
534 + if (error_is_set(&errp)) {
535 + monitor_printf(mon, "%s\n", error_get_pretty(errp));
541 +void hmp_backup(Monitor *mon, const QDict *qdict)
543 + const char *backup_file = qdict_get_str(qdict, "backup-file");
544 + const char *devlist = qdict_get_try_str(qdict, "devlist");
545 + int64_t speed = qdict_get_try_int(qdict, "speed", 0);
547 + Error *errp = NULL;
549 + qmp_backup(backup_file, true, BACKUP_FORMAT_VMA, false, NULL, !!devlist,
550 + devlist, qdict_haskey(qdict, "speed"), speed, &errp);
552 + if (error_is_set(&errp)) {
553 + monitor_printf(mon, "%s\n", error_get_pretty(errp));
559 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
563 ===================================================================
564 --- new.orig/hmp.h 2013-12-02 12:49:36.000000000 +0100
565 +++ new/hmp.h 2013-12-02 12:58:31.000000000 +0100
567 void hmp_info_migrate(Monitor *mon, const QDict *qdict);
568 void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict);
569 void hmp_info_migrate_cache_size(Monitor *mon, const QDict *qdict);
570 +void hmp_info_backup(Monitor *mon, const QDict *qdict);
571 void hmp_info_cpus(Monitor *mon, const QDict *qdict);
572 void hmp_info_block(Monitor *mon, const QDict *qdict);
573 void hmp_info_blockstats(Monitor *mon, const QDict *qdict);
575 void hmp_change(Monitor *mon, const QDict *qdict);
576 void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict);
577 void hmp_block_stream(Monitor *mon, const QDict *qdict);
578 +void hmp_backup(Monitor *mon, const QDict *qdict);
579 +void hmp_backup_cancel(Monitor *mon, const QDict *qdict);
580 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict);
581 void hmp_block_job_cancel(Monitor *mon, const QDict *qdict);
582 void hmp_block_job_pause(Monitor *mon, const QDict *qdict);
584 ===================================================================
585 --- new.orig/monitor.c 2013-12-02 12:49:36.000000000 +0100
586 +++ new/monitor.c 2013-12-02 12:58:31.000000000 +0100
587 @@ -2880,6 +2880,13 @@
594 + .help = "show backup status",
595 + .mhandler.cmd = hmp_info_backup,
601 Index: new/qapi-schema.json
602 ===================================================================
603 --- new.orig/qapi-schema.json 2013-12-02 12:49:36.000000000 +0100
604 +++ new/qapi-schema.json 2013-12-02 12:58:31.000000000 +0100
607 { 'command': 'query-events', 'returns': ['EventInfo'] }
611 +# Detailed backup status.
613 +# @status: #optional string describing the current backup status.
614 +# This can be 'active', 'done', 'error'. If this field is not
615 +# returned, no backup process has been initiated
617 +# @errmsg: #optional error message (only returned if status is 'error')
619 +# @total: #optional total amount of bytes involved in the backup process
621 +# @transferred: #optional amount of bytes already backed up.
623 +# @zero-bytes: #optional amount of 'zero' bytes detected.
625 +# @start-time: #optional time (epoch) when backup job started.
627 +# @end-time: #optional time (epoch) when backup job finished.
629 +# @backupfile: #optional backup file name
631 +# @uuid: #optional uuid for this backup job
634 +{ 'type': 'BackupStatus',
635 + 'data': {'*status': 'str', '*errmsg': 'str', '*total': 'int',
636 + '*transferred': 'int', '*zero-bytes': 'int',
637 + '*start-time': 'int', '*end-time': 'int',
638 + '*backup-file': 'str', '*uuid': 'str' } }
643 +# An enumeration of supported backup formats.
645 +# @vma: Proxmox vma backup format
647 +{ 'enum': 'BackupFormat',
648 + 'data': [ 'vma' ] }
653 +# Starts a VM backup.
655 +# @backup-file: the backup file name
657 +# @format: format of the backup file
659 +# @config-filename: #optional name of a configuration file to include into
660 +# the backup archive.
662 +# @speed: #optional the maximum speed, in bytes per second
664 +# @devlist: #optional list of block device names (separated by ',', ';'
665 +# or ':'). By default the backup includes all writable block devices.
667 +# Returns: the uuid of the backup job
670 +{ 'command': 'backup', 'data': { 'backup-file': 'str',
671 + '*format': 'BackupFormat',
672 + '*config-file': 'str',
673 + '*devlist': 'str', '*speed': 'int' },
679 +# Returns information about current/last backup task.
681 +# Returns: @BackupStatus
684 +{ 'command': 'query-backup', 'returns': 'BackupStatus' }
689 +# Cancel the current executing backup process.
691 +# Returns: nothing on success
693 +# Notes: This command succeeds even if there is no backup process running.
696 +{ 'command': 'backup-cancel' }
701 Index: new/qmp-commands.hx
702 ===================================================================
703 --- new.orig/qmp-commands.hx 2013-12-02 12:49:36.000000000 +0100
704 +++ new/qmp-commands.hx 2013-12-02 12:58:31.000000000 +0100
710 + .args_type = "backup-file:s,format:s?,config-file:F?,speed:o?,devlist:s?",
711 + .mhandler.cmd_new = qmp_marshal_input_backup,
715 + .name = "backup-cancel",
717 + .mhandler.cmd_new = qmp_marshal_input_backup_cancel,
721 + .name = "query-backup",
723 + .mhandler.cmd_new = qmp_marshal_input_query_backup,
727 .name = "block-job-set-speed",
728 .args_type = "device:B,speed:o",
729 .mhandler.cmd_new = qmp_marshal_input_block_job_set_speed,