2 ===================================================================
3 --- new.orig/blockdev.c 2013-12-02 11:40:36.000000000 +0100
4 +++ new/blockdev.c 2013-12-02 12:07:54.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,417 @@
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);
165 + if (di->bs && di->bs->job && !di->completed) {
166 + if (block_job_is_paused(di->bs->job)) {
167 + bool cancel = backup_state.error || backup_state.cancel;
169 + block_job_cancel(di->bs->job);
171 + block_job_resume(di->bs->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, &local_err);
346 + if (local_err != NULL) {
347 + error_setg(&backup_state.error, "backup_job_create failed");
348 + pvebackup_cancel(NULL);
352 + if (!backup_state.error) {
353 + pvebackup_run_next_job(); // run one job
356 + return g_strdup(backup_state.uuid_str);
363 + l = g_list_next(l);
365 + g_list_free(di_list);
373 + vma_writer_close(vmaw, &err);
374 + unlink(backup_file);
380 +BackupStatus *qmp_query_backup(Error **errp)
382 + BackupStatus *info = g_malloc0(sizeof(*info));
384 + if (!backup_state.start_time) {
385 + /* not started, return {} */
389 + info->has_status = true;
390 + info->has_start_time = true;
391 + info->start_time = backup_state.start_time;
393 + if (backup_state.backup_file) {
394 + info->has_backup_file = true;
395 + info->backup_file = g_strdup(backup_state.backup_file);
398 + info->has_uuid = true;
399 + info->uuid = g_strdup(backup_state.uuid_str);
401 + if (backup_state.end_time) {
402 + if (backup_state.error) {
403 + info->status = g_strdup("error");
404 + info->has_errmsg = true;
405 + info->errmsg = g_strdup(error_get_pretty(backup_state.error));
407 + info->status = g_strdup("done");
409 + info->has_end_time = true;
410 + info->end_time = backup_state.end_time;
412 + info->status = g_strdup("active");
415 + if (backup_state.vmaw) {
416 + pvebackup_update_status();
419 + info->has_total = true;
420 + info->total = backup_state.total;
421 + info->has_zero_bytes = true;
422 + info->zero_bytes = backup_state.zero_bytes;
423 + info->has_transferred = true;
424 + info->transferred = backup_state.transferred;
429 static void eject_device(BlockDriverState *bs, int force, Error **errp)
431 Index: new/hmp-commands.hx
432 ===================================================================
433 --- new.orig/hmp-commands.hx 2013-11-27 10:51:33.000000000 +0100
434 +++ new/hmp-commands.hx 2013-12-02 11:40:36.000000000 +0100
436 Copy data from a backing file into a block device.
441 + .args_type = "backupfile:s,speed:o?,devlist:s?",
442 + .params = "backupfile [speed [devlist]]",
443 + .help = "create a VM Backup.",
444 + .mhandler.cmd = hmp_backup,
454 + .name = "backup_cancel",
457 + .help = "cancel the current VM backup",
458 + .mhandler.cmd = hmp_backup_cancel,
463 +@findex backup_cancel
464 +Cancel the current VM backup.
469 .name = "block_job_set_speed",
470 .args_type = "device:B,speed:o",
471 @@ -1692,6 +1721,8 @@
474 show user network stack connection states
478 show migration status
479 @item info migrate_capabilities
481 ===================================================================
482 --- new.orig/hmp.c 2013-12-02 11:40:35.000000000 +0100
483 +++ new/hmp.c 2013-12-02 11:40:36.000000000 +0100
485 qapi_free_MouseInfoList(mice_list);
488 +void hmp_info_backup(Monitor *mon, const QDict *qdict)
490 + BackupStatus *info;
492 + info = qmp_query_backup(NULL);
493 + if (info->has_status) {
494 + if (info->has_errmsg) {
495 + monitor_printf(mon, "Backup status: %s - %s\n",
496 + info->status, info->errmsg);
498 + monitor_printf(mon, "Backup status: %s\n", info->status);
501 + if (info->has_backup_file) {
502 + int per = (info->has_total && info->total &&
503 + info->has_transferred && info->transferred) ?
504 + (info->transferred * 100)/info->total : 0;
505 + int zero_per = (info->has_total && info->total &&
506 + info->has_zero_bytes && info->zero_bytes) ?
507 + (info->zero_bytes * 100)/info->total : 0;
508 + monitor_printf(mon, "Backup file: %s\n", info->backup_file);
509 + monitor_printf(mon, "Backup uuid: %s\n", info->uuid);
510 + monitor_printf(mon, "Total size: %zd\n", info->total);
511 + monitor_printf(mon, "Transferred bytes: %zd (%d%%)\n",
512 + info->transferred, per);
513 + monitor_printf(mon, "Zero bytes: %zd (%d%%)\n",
514 + info->zero_bytes, zero_per);
517 + qapi_free_BackupStatus(info);
520 void hmp_info_migrate(Monitor *mon, const QDict *qdict)
523 @@ -1194,6 +1226,37 @@
524 hmp_handle_error(mon, &error);
527 +void hmp_backup_cancel(Monitor *mon, const QDict *qdict)
529 + Error *errp = NULL;
531 + qmp_backup_cancel(&errp);
533 + if (error_is_set(&errp)) {
534 + monitor_printf(mon, "%s\n", error_get_pretty(errp));
540 +void hmp_backup(Monitor *mon, const QDict *qdict)
542 + const char *backup_file = qdict_get_str(qdict, "backup-file");
543 + const char *devlist = qdict_get_try_str(qdict, "devlist");
544 + int64_t speed = qdict_get_try_int(qdict, "speed", 0);
546 + Error *errp = NULL;
548 + qmp_backup(backup_file, true, BACKUP_FORMAT_VMA, false, NULL, !!devlist,
549 + devlist, qdict_haskey(qdict, "speed"), speed, &errp);
551 + if (error_is_set(&errp)) {
552 + monitor_printf(mon, "%s\n", error_get_pretty(errp));
558 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
562 ===================================================================
563 --- new.orig/hmp.h 2013-11-27 10:51:33.000000000 +0100
564 +++ new/hmp.h 2013-12-02 11:40:36.000000000 +0100
566 void hmp_info_migrate(Monitor *mon, const QDict *qdict);
567 void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict);
568 void hmp_info_migrate_cache_size(Monitor *mon, const QDict *qdict);
569 +void hmp_info_backup(Monitor *mon, const QDict *qdict);
570 void hmp_info_cpus(Monitor *mon, const QDict *qdict);
571 void hmp_info_block(Monitor *mon, const QDict *qdict);
572 void hmp_info_blockstats(Monitor *mon, const QDict *qdict);
574 void hmp_change(Monitor *mon, const QDict *qdict);
575 void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict);
576 void hmp_block_stream(Monitor *mon, const QDict *qdict);
577 +void hmp_backup(Monitor *mon, const QDict *qdict);
578 +void hmp_backup_cancel(Monitor *mon, const QDict *qdict);
579 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict);
580 void hmp_block_job_cancel(Monitor *mon, const QDict *qdict);
581 void hmp_block_job_pause(Monitor *mon, const QDict *qdict);
583 ===================================================================
584 --- new.orig/monitor.c 2013-11-27 10:51:33.000000000 +0100
585 +++ new/monitor.c 2013-12-02 11:40:36.000000000 +0100
586 @@ -2880,6 +2880,13 @@
593 + .help = "show backup status",
594 + .mhandler.cmd = hmp_info_backup,
600 Index: new/qapi-schema.json
601 ===================================================================
602 --- new.orig/qapi-schema.json 2013-12-02 11:40:35.000000000 +0100
603 +++ new/qapi-schema.json 2013-12-02 11:40:36.000000000 +0100
606 { 'command': 'query-events', 'returns': ['EventInfo'] }
610 +# Detailed backup status.
612 +# @status: #optional string describing the current backup status.
613 +# This can be 'active', 'done', 'error'. If this field is not
614 +# returned, no backup process has been initiated
616 +# @errmsg: #optional error message (only returned if status is 'error')
618 +# @total: #optional total amount of bytes involved in the backup process
620 +# @transferred: #optional amount of bytes already backed up.
622 +# @zero-bytes: #optional amount of 'zero' bytes detected.
624 +# @start-time: #optional time (epoch) when backup job started.
626 +# @end-time: #optional time (epoch) when backup job finished.
628 +# @backupfile: #optional backup file name
630 +# @uuid: #optional uuid for this backup job
633 +{ 'type': 'BackupStatus',
634 + 'data': {'*status': 'str', '*errmsg': 'str', '*total': 'int',
635 + '*transferred': 'int', '*zero-bytes': 'int',
636 + '*start-time': 'int', '*end-time': 'int',
637 + '*backup-file': 'str', '*uuid': 'str' } }
642 +# An enumeration of supported backup formats.
644 +# @vma: Proxmox vma backup format
646 +{ 'enum': 'BackupFormat',
647 + 'data': [ 'vma' ] }
652 +# Starts a VM backup.
654 +# @backup-file: the backup file name
656 +# @format: format of the backup file
658 +# @config-filename: #optional name of a configuration file to include into
659 +# the backup archive.
661 +# @speed: #optional the maximum speed, in bytes per second
663 +# @devlist: #optional list of block device names (separated by ',', ';'
664 +# or ':'). By default the backup includes all writable block devices.
666 +# Returns: the uuid of the backup job
669 +{ 'command': 'backup', 'data': { 'backup-file': 'str',
670 + '*format': 'BackupFormat',
671 + '*config-file': 'str',
672 + '*devlist': 'str', '*speed': 'int' },
678 +# Returns information about current/last backup task.
680 +# Returns: @BackupStatus
683 +{ 'command': 'query-backup', 'returns': 'BackupStatus' }
688 +# Cancel the current executing backup process.
690 +# Returns: nothing on success
692 +# Notes: This command succeeds even if there is no backup process running.
695 +{ 'command': 'backup-cancel' }
700 Index: new/qmp-commands.hx
701 ===================================================================
702 --- new.orig/qmp-commands.hx 2013-12-02 11:40:35.000000000 +0100
703 +++ new/qmp-commands.hx 2013-12-02 11:40:36.000000000 +0100
709 + .args_type = "backup-file:s,format:s?,config-file:F?,speed:o?,devlist:s?",
710 + .mhandler.cmd_new = qmp_marshal_input_backup,
714 + .name = "backup-cancel",
716 + .mhandler.cmd_new = qmp_marshal_input_backup_cancel,
720 + .name = "query-backup",
722 + .mhandler.cmd_new = qmp_marshal_input_query_backup,
726 .name = "block-job-set-speed",
727 .args_type = "device:B,speed:o",
728 .mhandler.cmd_new = qmp_marshal_input_block_job_set_speed,