2 ===================================================================
3 --- new.orig/blockdev.c 2013-12-05 13:39:59.000000000 +0100
4 +++ new/blockdev.c 2013-12-06 06:59:11.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,425 @@
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 + if (backup_state.vmaw) {
85 + backup_state.end_time = time(NULL);
86 + Error *local_err = NULL;
87 + vma_writer_close(backup_state.vmaw, &local_err);
88 + error_propagate(&backup_state.error, local_err);
89 + backup_state.vmaw = NULL;
92 + if (backup_state.di_list) {
93 + GList *l = backup_state.di_list;
95 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
99 + g_list_free(backup_state.di_list);
100 + backup_state.di_list = NULL;
104 +static void pvebackup_complete_cb(void *opaque, int ret)
106 + PVEBackupDevInfo *di = opaque;
108 + assert(backup_state.vmaw);
110 + 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) */
131 + GList *l = backup_state.di_list;
133 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
134 + l = g_list_next(l);
135 + if (!di->completed && di->bs) {
136 + BlockJob *job = di->bs->job;
138 + if (!di->completed) {
139 + 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 + if (!di->completed && di->bs && di->bs->job) {
165 + BlockJob *job = di->bs->job;
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");
258 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
259 + l = g_list_next(l);
260 + if (bdrv_in_use(di->bs)) {
261 + error_set(errp, QERR_DEVICE_IN_USE, bdrv_get_device_name(di->bs));
265 + ssize_t size = bdrv_getlength(di->bs);
267 + error_setg_errno(errp, -di->size, "bdrv_getlength failed");
274 + uuid_generate(uuid);
276 + vmaw = vma_writer_create(backup_file, uuid, &local_err);
278 + if (error_is_set(&local_err)) {
279 + error_propagate(errp, local_err);
284 + /* register all devices for vma writer */
287 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
288 + l = g_list_next(l);
290 + const char *devname = bdrv_get_device_name(di->bs);
291 + di->dev_id = vma_writer_register_stream(vmaw, devname, di->size);
292 + if (di->dev_id <= 0) {
293 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
294 + "register_stream failed");
299 + /* add configuration file to archive */
300 + if (has_config_file) {
301 + char *cdata = NULL;
303 + GError *err = NULL;
304 + if (!g_file_get_contents(config_file, &cdata, &clen, &err)) {
305 + error_setg(errp, "unable to read file '%s'", config_file);
309 + const char *basename = g_path_get_basename(config_file);
310 + if (vma_writer_add_config(vmaw, basename, cdata, clen) != 0) {
311 + error_setg(errp, "unable to add config data to vma archive");
318 + /* initialize global backup_state now */
320 + backup_state.cancel = false;
322 + if (backup_state.error) {
323 + error_free(backup_state.error);
324 + backup_state.error = NULL;
327 + backup_state.speed = (has_speed && speed > 0) ? speed : 0;
329 + backup_state.start_time = time(NULL);
330 + backup_state.end_time = 0;
332 + if (backup_state.backup_file) {
333 + g_free(backup_state.backup_file);
335 + backup_state.backup_file = g_strdup(backup_file);
337 + backup_state.vmaw = vmaw;
339 + uuid_copy(backup_state.uuid, uuid);
340 + uuid_unparse_lower(uuid, backup_state.uuid_str);
342 + backup_state.di_list = di_list;
344 + backup_state.total = total;
345 + backup_state.transferred = 0;
346 + backup_state.zero_bytes = 0;
348 + /* start all jobs (paused state) */
351 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
352 + l = g_list_next(l);
354 + backup_start(di->bs, NULL, speed, MIRROR_SYNC_MODE_FULL,
355 + BLOCKDEV_ON_ERROR_REPORT, BLOCKDEV_ON_ERROR_REPORT,
356 + pvebackup_dump_cb, pvebackup_complete_cb, di,
358 + if (local_err != NULL) {
359 + error_setg(&backup_state.error, "backup_job_create failed");
360 + pvebackup_cancel(NULL);
364 + if (!backup_state.error) {
365 + pvebackup_run_next_job(); // run one job
368 + return g_strdup(backup_state.uuid_str);
375 + l = g_list_next(l);
377 + g_list_free(di_list);
385 + vma_writer_close(vmaw, &err);
386 + unlink(backup_file);
392 +BackupStatus *qmp_query_backup(Error **errp)
394 + BackupStatus *info = g_malloc0(sizeof(*info));
396 + if (!backup_state.start_time) {
397 + /* not started, return {} */
401 + info->has_status = true;
402 + info->has_start_time = true;
403 + info->start_time = backup_state.start_time;
405 + if (backup_state.backup_file) {
406 + info->has_backup_file = true;
407 + info->backup_file = g_strdup(backup_state.backup_file);
410 + info->has_uuid = true;
411 + info->uuid = g_strdup(backup_state.uuid_str);
413 + if (backup_state.end_time) {
414 + if (backup_state.error) {
415 + info->status = g_strdup("error");
416 + info->has_errmsg = true;
417 + info->errmsg = g_strdup(error_get_pretty(backup_state.error));
419 + info->status = g_strdup("done");
421 + info->has_end_time = true;
422 + info->end_time = backup_state.end_time;
424 + info->status = g_strdup("active");
427 + info->has_total = true;
428 + info->total = backup_state.total;
429 + info->has_zero_bytes = true;
430 + info->zero_bytes = backup_state.zero_bytes;
431 + info->has_transferred = true;
432 + info->transferred = backup_state.transferred;
437 static void eject_device(BlockDriverState *bs, int force, Error **errp)
439 Index: new/hmp-commands.hx
440 ===================================================================
441 --- new.orig/hmp-commands.hx 2013-12-05 13:39:59.000000000 +0100
442 +++ new/hmp-commands.hx 2013-12-05 13:40:07.000000000 +0100
444 Copy data from a backing file into a block device.
449 + .args_type = "backupfile:s,speed:o?,devlist:s?",
450 + .params = "backupfile [speed [devlist]]",
451 + .help = "create a VM Backup.",
452 + .mhandler.cmd = hmp_backup,
462 + .name = "backup_cancel",
465 + .help = "cancel the current VM backup",
466 + .mhandler.cmd = hmp_backup_cancel,
471 +@findex backup_cancel
472 +Cancel the current VM backup.
477 .name = "block_job_set_speed",
478 .args_type = "device:B,speed:o",
479 @@ -1692,6 +1721,8 @@
482 show user network stack connection states
486 show migration status
487 @item info migrate_capabilities
489 ===================================================================
490 --- new.orig/hmp.c 2013-12-05 13:39:59.000000000 +0100
491 +++ new/hmp.c 2013-12-06 07:25:28.000000000 +0100
493 qapi_free_MouseInfoList(mice_list);
496 +void hmp_info_backup(Monitor *mon, const QDict *qdict)
498 + BackupStatus *info;
500 + info = qmp_query_backup(NULL);
501 + if (info->has_status) {
502 + if (info->has_errmsg) {
503 + monitor_printf(mon, "Backup status: %s - %s\n",
504 + info->status, info->errmsg);
506 + monitor_printf(mon, "Backup status: %s\n", info->status);
510 + if (info->has_backup_file) {
511 + monitor_printf(mon, "Start time: %s", ctime(&info->start_time));
512 + if (info->end_time) {
513 + monitor_printf(mon, "End time: %s", ctime(&info->end_time));
516 + int per = (info->has_total && info->total &&
517 + info->has_transferred && info->transferred) ?
518 + (info->transferred * 100)/info->total : 0;
519 + int zero_per = (info->has_total && info->total &&
520 + info->has_zero_bytes && info->zero_bytes) ?
521 + (info->zero_bytes * 100)/info->total : 0;
522 + monitor_printf(mon, "Backup file: %s\n", info->backup_file);
523 + monitor_printf(mon, "Backup uuid: %s\n", info->uuid);
524 + monitor_printf(mon, "Total size: %zd\n", info->total);
525 + monitor_printf(mon, "Transferred bytes: %zd (%d%%)\n",
526 + info->transferred, per);
527 + monitor_printf(mon, "Zero bytes: %zd (%d%%)\n",
528 + info->zero_bytes, zero_per);
531 + qapi_free_BackupStatus(info);
534 void hmp_info_migrate(Monitor *mon, const QDict *qdict)
537 @@ -1193,6 +1231,29 @@
539 hmp_handle_error(mon, &error);
542 +void hmp_backup_cancel(Monitor *mon, const QDict *qdict)
544 + Error *error = NULL;
546 + qmp_backup_cancel(&error);
548 + hmp_handle_error(mon, &error);
551 +void hmp_backup(Monitor *mon, const QDict *qdict)
553 + Error *error = NULL;
555 + const char *backup_file = qdict_get_str(qdict, "backupfile");
556 + const char *devlist = qdict_get_try_str(qdict, "devlist");
557 + int64_t speed = qdict_get_try_int(qdict, "speed", 0);
559 + qmp_backup(backup_file, true, BACKUP_FORMAT_VMA, false, NULL, !!devlist,
560 + devlist, qdict_haskey(qdict, "speed"), speed, &error);
562 + hmp_handle_error(mon, &error);
565 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
568 ===================================================================
569 --- new.orig/hmp.h 2013-12-05 13:39:59.000000000 +0100
570 +++ new/hmp.h 2013-12-05 13:40:07.000000000 +0100
572 void hmp_info_migrate(Monitor *mon, const QDict *qdict);
573 void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict);
574 void hmp_info_migrate_cache_size(Monitor *mon, const QDict *qdict);
575 +void hmp_info_backup(Monitor *mon, const QDict *qdict);
576 void hmp_info_cpus(Monitor *mon, const QDict *qdict);
577 void hmp_info_block(Monitor *mon, const QDict *qdict);
578 void hmp_info_blockstats(Monitor *mon, const QDict *qdict);
580 void hmp_change(Monitor *mon, const QDict *qdict);
581 void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict);
582 void hmp_block_stream(Monitor *mon, const QDict *qdict);
583 +void hmp_backup(Monitor *mon, const QDict *qdict);
584 +void hmp_backup_cancel(Monitor *mon, const QDict *qdict);
585 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict);
586 void hmp_block_job_cancel(Monitor *mon, const QDict *qdict);
587 void hmp_block_job_pause(Monitor *mon, const QDict *qdict);
589 ===================================================================
590 --- new.orig/monitor.c 2013-12-05 13:39:59.000000000 +0100
591 +++ new/monitor.c 2013-12-05 13:40:07.000000000 +0100
592 @@ -2880,6 +2880,13 @@
599 + .help = "show backup status",
600 + .mhandler.cmd = hmp_info_backup,
606 Index: new/qapi-schema.json
607 ===================================================================
608 --- new.orig/qapi-schema.json 2013-12-05 13:39:59.000000000 +0100
609 +++ new/qapi-schema.json 2013-12-06 06:59:11.000000000 +0100
612 { 'command': 'query-events', 'returns': ['EventInfo'] }
616 +# Detailed backup status.
618 +# @status: #optional string describing the current backup status.
619 +# This can be 'active', 'done', 'error'. If this field is not
620 +# returned, no backup process has been initiated
622 +# @errmsg: #optional error message (only returned if status is 'error')
624 +# @total: #optional total amount of bytes involved in the backup process
626 +# @transferred: #optional amount of bytes already backed up.
628 +# @zero-bytes: #optional amount of 'zero' bytes detected.
630 +# @start-time: #optional time (epoch) when backup job started.
632 +# @end-time: #optional time (epoch) when backup job finished.
634 +# @backupfile: #optional backup file name
636 +# @uuid: #optional uuid for this backup job
639 +{ 'type': 'BackupStatus',
640 + 'data': {'*status': 'str', '*errmsg': 'str', '*total': 'int',
641 + '*transferred': 'int', '*zero-bytes': 'int',
642 + '*start-time': 'int', '*end-time': 'int',
643 + '*backup-file': 'str', '*uuid': 'str' } }
648 +# An enumeration of supported backup formats.
650 +# @vma: Proxmox vma backup format
652 +{ 'enum': 'BackupFormat',
653 + 'data': [ 'vma' ] }
658 +# Starts a VM backup.
660 +# @backup-file: the backup file name
662 +# @format: format of the backup file
664 +# @config-filename: #optional name of a configuration file to include into
665 +# the backup archive.
667 +# @speed: #optional the maximum speed, in bytes per second
669 +# @devlist: #optional list of block device names (separated by ',', ';'
670 +# or ':'). By default the backup includes all writable block devices.
672 +# Returns: the uuid of the backup job
675 +{ 'command': 'backup', 'data': { 'backup-file': 'str',
676 + '*format': 'BackupFormat',
677 + '*config-file': 'str',
678 + '*devlist': 'str', '*speed': 'int' },
684 +# Returns information about current/last backup task.
686 +# Returns: @BackupStatus
689 +{ 'command': 'query-backup', 'returns': 'BackupStatus' }
694 +# Cancel the current executing backup process.
696 +# Returns: nothing on success
698 +# Notes: This command succeeds even if there is no backup process running.
701 +{ 'command': 'backup-cancel' }
706 Index: new/qmp-commands.hx
707 ===================================================================
708 --- new.orig/qmp-commands.hx 2013-12-05 13:39:59.000000000 +0100
709 +++ new/qmp-commands.hx 2013-12-05 13:40:07.000000000 +0100
715 + .args_type = "backup-file:s,format:s?,config-file:F?,speed:o?,devlist:s?",
716 + .mhandler.cmd_new = qmp_marshal_input_backup,
720 + .name = "backup-cancel",
722 + .mhandler.cmd_new = qmp_marshal_input_backup_cancel,
726 + .name = "query-backup",
728 + .mhandler.cmd_new = qmp_marshal_input_query_backup,
732 .name = "block-job-set-speed",
733 .args_type = "device:B,speed:o",
734 .mhandler.cmd_new = qmp_marshal_input_block_job_set_speed,