2 ===================================================================
3 --- new.orig/blockdev.c 2013-12-02 07:42:33.000000000 +0100
4 +++ new/blockdev.c 2013-12-02 08:40:19.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,412 @@
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)
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 + pvebackup_cancel();
153 +static void pvebackup_run_next_job(void)
155 + GList *l = backup_state.di_list;
157 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
158 + l = g_list_next(l);
160 + if (di->bs && di->bs->job && !di->completed) {
161 + if (block_job_is_paused(di->bs->job)) {
162 + bool cancel = backup_state.error || backup_state.cancel;
164 + block_job_cancel(di->bs->job);
166 + block_job_resume(di->bs->job);
173 + pvebackup_cleanup();
176 +char *qmp_backup(const char *backup_file, bool has_format,
177 + BackupFormat format,
178 + bool has_config_file, const char *config_file,
179 + bool has_devlist, const char *devlist,
180 + bool has_speed, int64_t speed, Error **errp)
182 + BlockDriverState *bs;
183 + Error *local_err = NULL;
185 + VmaWriter *vmaw = NULL;
186 + gchar **devs = NULL;
187 + GList *di_list = NULL;
190 + if (backup_state.di_list) {
191 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
192 + "previous backup not finished");
196 + /* Todo: try to auto-detect format based on file name */
197 + format = has_format ? format : BACKUP_FORMAT_VMA;
199 + if (format != BACKUP_FORMAT_VMA) {
200 + error_set(errp, ERROR_CLASS_GENERIC_ERROR, "unknown backup format");
205 + devs = g_strsplit_set(devlist, ",;:", -1);
209 + bs = bdrv_find(*d);
211 + if (bdrv_is_read_only(bs)) {
212 + error_set(errp, QERR_DEVICE_IS_READ_ONLY, *d);
215 + if (!bdrv_is_inserted(bs)) {
216 + error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, *d);
219 + PVEBackupDevInfo *di = g_new0(PVEBackupDevInfo, 1);
221 + di_list = g_list_append(di_list, di);
223 + error_set(errp, QERR_DEVICE_NOT_FOUND, *d);
232 + while ((bs = bdrv_next(bs))) {
234 + if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
238 + PVEBackupDevInfo *di = g_new0(PVEBackupDevInfo, 1);
240 + di_list = g_list_append(di_list, di);
245 + error_set(errp, ERROR_CLASS_GENERIC_ERROR, "empty device list");
251 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
252 + l = g_list_next(l);
254 + error_set(errp, QERR_DEVICE_IN_USE, bdrv_get_device_name(di->bs));
259 + uuid_generate(uuid);
261 + vmaw = vma_writer_create(backup_file, uuid, &local_err);
263 + if (error_is_set(&local_err)) {
264 + error_propagate(errp, local_err);
269 + /* register all devices for vma writer */
272 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
273 + l = g_list_next(l);
275 + int64_t size = bdrv_getlength(di->bs);
276 + const char *devname = bdrv_get_device_name(di->bs);
277 + di->dev_id = vma_writer_register_stream(vmaw, devname, size);
278 + if (di->dev_id <= 0) {
279 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
280 + "register_stream failed");
285 + /* add configuration file to archive */
286 + if (has_config_file) {
287 + char *cdata = NULL;
289 + GError *err = NULL;
290 + if (!g_file_get_contents(config_file, &cdata, &clen, &err)) {
291 + error_setg(errp, "unable to read file '%s'", config_file);
295 + const char *basename = g_path_get_basename(config_file);
296 + if (vma_writer_add_config(vmaw, basename, cdata, clen) != 0) {
297 + error_setg(errp, "unable to add config data to vma archive");
304 + /* initialize global backup_state now */
306 + backup_state.cancel = false;
308 + if (backup_state.error) {
309 + error_free(backup_state.error);
310 + backup_state.error = NULL;
313 + backup_state.speed = (has_speed && speed > 0) ? speed : 0;
315 + backup_state.start_time = time(NULL);
316 + backup_state.end_time = 0;
318 + if (backup_state.backup_file) {
319 + g_free(backup_state.backup_file);
321 + backup_state.backup_file = g_strdup(backup_file);
323 + backup_state.vmaw = vmaw;
325 + uuid_copy(backup_state.uuid, uuid);
326 + uuid_unparse_lower(uuid, backup_state.uuid_str);
328 + backup_state.di_list = di_list;
330 + pvebackup_update_status();
332 + /* start all jobs (paused state) */
335 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
336 + l = g_list_next(l);
338 + backup_start(di->bs, NULL, speed, MIRROR_SYNC_MODE_FULL,
339 + BLOCKDEV_ON_ERROR_REPORT, BLOCKDEV_ON_ERROR_REPORT,
340 + pvebackup_dump_cb, pvebackup_complete_cb, di, &local_err);
341 + if (local_err != NULL) {
342 + error_setg(&backup_state.error, "backup_job_create failed");
343 + pvebackup_cancel();
347 + if (!backup_state.error) {
348 + pvebackup_run_next_job(); // run one job
351 + return g_strdup(backup_state.uuid_str);
358 + l = g_list_next(l);
360 + g_list_free(di_list);
368 + vma_writer_close(vmaw, &err);
369 + unlink(backup_file);
375 +BackupStatus *qmp_query_backup(Error **errp)
377 + BackupStatus *info = g_malloc0(sizeof(*info));
379 + if (!backup_state.start_time) {
380 + /* not started, return {} */
384 + info->has_status = true;
385 + info->has_start_time = true;
386 + info->start_time = backup_state.start_time;
388 + if (backup_state.backup_file) {
389 + info->has_backup_file = true;
390 + info->backup_file = g_strdup(backup_state.backup_file);
393 + info->has_uuid = true;
394 + info->uuid = g_strdup(backup_state.uuid_str);
396 + if (backup_state.end_time) {
397 + if (backup_state.error) {
398 + info->status = g_strdup("error");
399 + info->has_errmsg = true;
400 + info->errmsg = g_strdup(error_get_pretty(backup_state.error));
402 + info->status = g_strdup("done");
404 + info->has_end_time = true;
405 + info->end_time = backup_state.end_time;
407 + info->status = g_strdup("active");
410 + if (backup_state.vmaw) {
411 + pvebackup_update_status();
414 + info->has_total = true;
415 + info->total = backup_state.total;
416 + info->has_zero_bytes = true;
417 + info->zero_bytes = backup_state.zero_bytes;
418 + info->has_transferred = true;
419 + info->transferred = backup_state.transferred;
424 static void eject_device(BlockDriverState *bs, int force, Error **errp)
426 Index: new/hmp-commands.hx
427 ===================================================================
428 --- new.orig/hmp-commands.hx 2013-12-02 07:39:56.000000000 +0100
429 +++ new/hmp-commands.hx 2013-12-02 08:19:27.000000000 +0100
431 Copy data from a backing file into a block device.
436 + .args_type = "backupfile:s,speed:o?,devlist:s?",
437 + .params = "backupfile [speed [devlist]]",
438 + .help = "create a VM Backup.",
439 + .mhandler.cmd = hmp_backup,
449 + .name = "backup_cancel",
452 + .help = "cancel the current VM backup",
453 + .mhandler.cmd = hmp_backup_cancel,
458 +@findex backup_cancel
459 +Cancel the current VM backup.
464 .name = "block_job_set_speed",
465 .args_type = "device:B,speed:o",
466 @@ -1692,6 +1721,8 @@
469 show user network stack connection states
473 show migration status
474 @item info migrate_capabilities
476 ===================================================================
477 --- new.orig/hmp.c 2013-12-02 07:39:56.000000000 +0100
478 +++ new/hmp.c 2013-12-02 08:21:00.000000000 +0100
480 qapi_free_MouseInfoList(mice_list);
483 +void hmp_info_backup(Monitor *mon, const QDict *qdict)
485 + BackupStatus *info;
487 + info = qmp_query_backup(NULL);
488 + if (info->has_status) {
489 + if (info->has_errmsg) {
490 + monitor_printf(mon, "Backup status: %s - %s\n",
491 + info->status, info->errmsg);
493 + monitor_printf(mon, "Backup status: %s\n", info->status);
496 + if (info->has_backup_file) {
497 + int per = (info->has_total && info->total &&
498 + info->has_transferred && info->transferred) ?
499 + (info->transferred * 100)/info->total : 0;
500 + int zero_per = (info->has_total && info->total &&
501 + info->has_zero_bytes && info->zero_bytes) ?
502 + (info->zero_bytes * 100)/info->total : 0;
503 + monitor_printf(mon, "Backup file: %s\n", info->backup_file);
504 + monitor_printf(mon, "Backup uuid: %s\n", info->uuid);
505 + monitor_printf(mon, "Total size: %zd\n", info->total);
506 + monitor_printf(mon, "Transferred bytes: %zd (%d%%)\n",
507 + info->transferred, per);
508 + monitor_printf(mon, "Zero bytes: %zd (%d%%)\n",
509 + info->zero_bytes, zero_per);
512 + qapi_free_BackupStatus(info);
515 void hmp_info_migrate(Monitor *mon, const QDict *qdict)
518 @@ -1194,6 +1226,37 @@
519 hmp_handle_error(mon, &error);
522 +void hmp_backup_cancel(Monitor *mon, const QDict *qdict)
524 + Error *errp = NULL;
526 + qmp_backup_cancel(&errp);
528 + if (error_is_set(&errp)) {
529 + monitor_printf(mon, "%s\n", error_get_pretty(errp));
535 +void hmp_backup(Monitor *mon, const QDict *qdict)
537 + const char *backup_file = qdict_get_str(qdict, "backup-file");
538 + const char *devlist = qdict_get_try_str(qdict, "devlist");
539 + int64_t speed = qdict_get_try_int(qdict, "speed", 0);
541 + Error *errp = NULL;
543 + qmp_backup(backup_file, true, BACKUP_FORMAT_VMA, false, NULL, !!devlist,
544 + devlist, qdict_haskey(qdict, "speed"), speed, &errp);
546 + if (error_is_set(&errp)) {
547 + monitor_printf(mon, "%s\n", error_get_pretty(errp));
553 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
557 ===================================================================
558 --- new.orig/hmp.h 2013-12-02 07:39:56.000000000 +0100
559 +++ new/hmp.h 2013-12-02 08:22:00.000000000 +0100
561 void hmp_info_migrate(Monitor *mon, const QDict *qdict);
562 void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict);
563 void hmp_info_migrate_cache_size(Monitor *mon, const QDict *qdict);
564 +void hmp_info_backup(Monitor *mon, const QDict *qdict);
565 void hmp_info_cpus(Monitor *mon, const QDict *qdict);
566 void hmp_info_block(Monitor *mon, const QDict *qdict);
567 void hmp_info_blockstats(Monitor *mon, const QDict *qdict);
569 void hmp_change(Monitor *mon, const QDict *qdict);
570 void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict);
571 void hmp_block_stream(Monitor *mon, const QDict *qdict);
572 +void hmp_backup(Monitor *mon, const QDict *qdict);
573 +void hmp_backup_cancel(Monitor *mon, const QDict *qdict);
574 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict);
575 void hmp_block_job_cancel(Monitor *mon, const QDict *qdict);
576 void hmp_block_job_pause(Monitor *mon, const QDict *qdict);
578 ===================================================================
579 --- new.orig/monitor.c 2013-12-02 07:39:56.000000000 +0100
580 +++ new/monitor.c 2013-12-02 08:22:29.000000000 +0100
581 @@ -2880,6 +2880,13 @@
588 + .help = "show backup status",
589 + .mhandler.cmd = hmp_info_backup,
595 Index: new/qapi-schema.json
596 ===================================================================
597 --- new.orig/qapi-schema.json 2013-12-02 07:39:56.000000000 +0100
598 +++ new/qapi-schema.json 2013-12-02 08:16:27.000000000 +0100
601 { 'command': 'query-events', 'returns': ['EventInfo'] }
605 +# Detailed backup status.
607 +# @status: #optional string describing the current backup status.
608 +# This can be 'active', 'done', 'error'. If this field is not
609 +# returned, no backup process has been initiated
611 +# @errmsg: #optional error message (only returned if status is 'error')
613 +# @total: #optional total amount of bytes involved in the backup process
615 +# @transferred: #optional amount of bytes already backed up.
617 +# @zero-bytes: #optional amount of 'zero' bytes detected.
619 +# @start-time: #optional time (epoch) when backup job started.
621 +# @end-time: #optional time (epoch) when backup job finished.
623 +# @backupfile: #optional backup file name
625 +# @uuid: #optional uuid for this backup job
628 +{ 'type': 'BackupStatus',
629 + 'data': {'*status': 'str', '*errmsg': 'str', '*total': 'int',
630 + '*transferred': 'int', '*zero-bytes': 'int',
631 + '*start-time': 'int', '*end-time': 'int',
632 + '*backup-file': 'str', '*uuid': 'str' } }
637 +# An enumeration of supported backup formats.
639 +# @vma: Proxmox vma backup format
641 +{ 'enum': 'BackupFormat',
642 + 'data': [ 'vma' ] }
647 +# Starts a VM backup.
649 +# @backup-file: the backup file name
651 +# @format: format of the backup file
653 +# @config-filename: #optional name of a configuration file to include into
654 +# the backup archive.
656 +# @speed: #optional the maximum speed, in bytes per second
658 +# @devlist: #optional list of block device names (separated by ',', ';'
659 +# or ':'). By default the backup includes all writable block devices.
661 +# Returns: the uuid of the backup job
664 +{ 'command': 'backup', 'data': { 'backup-file': 'str',
665 + '*format': 'BackupFormat',
666 + '*config-file': 'str',
667 + '*devlist': 'str', '*speed': 'int' },
673 +# Returns information about current/last backup task.
675 +# Returns: @BackupStatus
678 +{ 'command': 'query-backup', 'returns': 'BackupStatus' }
683 +# Cancel the current executing backup process.
685 +# Returns: nothing on success
687 +# Notes: This command succeeds even if there is no backup process running.
690 +{ 'command': 'backup-cancel' }
695 Index: new/qmp-commands.hx
696 ===================================================================
697 --- new.orig/qmp-commands.hx 2013-12-02 07:39:56.000000000 +0100
698 +++ new/qmp-commands.hx 2013-12-02 08:19:45.000000000 +0100
704 + .args_type = "backup-file:s,format:s?,config-file:F?,speed:o?,devlist:s?",
705 + .mhandler.cmd_new = qmp_marshal_input_backup,
709 + .name = "backup-cancel",
711 + .mhandler.cmd_new = qmp_marshal_input_backup_cancel,
715 + .name = "query-backup",
717 + .mhandler.cmd_new = qmp_marshal_input_query_backup,
721 .name = "block-job-set-speed",
722 .args_type = "device:B,speed:o",
723 .mhandler.cmd_new = qmp_marshal_input_block_job_set_speed,