adding 2.5 pve patches and left-over extra fixes
[pve-qemu-kvm.git] / debian / patches / pve / 0016-backup-add-pve-monitor-commands.patch
blob7105e6334df9a29d62898816320bac000a5892bd
1 From af106cee7626a76c7d24c838d07b437f48c2da2c Mon Sep 17 00:00:00 2001
2 From: Wolfgang Bumiller <w.bumiller@proxmox.com>
3 Date: Wed, 9 Dec 2015 15:20:56 +0100
4 Subject: [PATCH 16/41] backup: add pve monitor commands
6 ---
7 blockdev.c | 438 +++++++++++++++++++++++++++++++++++++++++++++++++++
8 hmp-commands-info.hx | 13 ++
9 hmp-commands.hx | 29 ++++
10 hmp.c | 61 +++++++
11 hmp.h | 3 +
12 qapi-schema.json | 89 +++++++++++
13 qmp-commands.hx | 18 +++
14 7 files changed, 651 insertions(+)
16 diff --git a/blockdev.c b/blockdev.c
17 index 1796eaf..a346c55 100644
18 --- a/blockdev.c
19 +++ b/blockdev.c
20 @@ -49,6 +49,7 @@
21 #include "qmp-commands.h"
22 #include "trace.h"
23 #include "sysemu/arch_init.h"
24 +#include "vma.h"
26 static const char *const if_name[IF_COUNT] = {
27 [IF_NONE] = "none",
28 @@ -2875,6 +2876,443 @@ static void block_job_cb(void *opaque, int ret)
32 +/* PVE backup related function */
34 +static struct PVEBackupState {
35 + Error *error;
36 + bool cancel;
37 + uuid_t uuid;
38 + char uuid_str[37];
39 + int64_t speed;
40 + time_t start_time;
41 + time_t end_time;
42 + char *backup_file;
43 + VmaWriter *vmaw;
44 + GList *di_list;
45 + size_t total;
46 + size_t transferred;
47 + size_t zero_bytes;
48 +} backup_state;
50 +typedef struct PVEBackupDevInfo {
51 + BlockDriverState *bs;
52 + size_t size;
53 + uint8_t dev_id;
54 + //bool started;
55 + bool completed;
56 +} PVEBackupDevInfo;
58 +static void pvebackup_run_next_job(void);
60 +static int pvebackup_dump_cb(void *opaque, BlockDriverState *target,
61 + int64_t sector_num, int n_sectors,
62 + unsigned char *buf)
64 + PVEBackupDevInfo *di = opaque;
66 + if (sector_num & 0x7f) {
67 + if (!backup_state.error) {
68 + error_setg(&backup_state.error,
69 + "got unaligned write inside backup dump "
70 + "callback (sector %ld)", sector_num);
71 + }
72 + return -1; // not aligned to cluster size
73 + }
75 + int64_t cluster_num = sector_num >> 7;
76 + int size = n_sectors * BDRV_SECTOR_SIZE;
78 + int ret = -1;
80 + if (backup_state.vmaw) {
81 + size_t zero_bytes = 0;
82 + ret = vma_writer_write(backup_state.vmaw, di->dev_id, cluster_num,
83 + buf, &zero_bytes);
84 + backup_state.zero_bytes += zero_bytes;
85 + } else {
86 + ret = size;
87 + if (!buf) {
88 + backup_state.zero_bytes += size;
89 + }
90 + }
92 + backup_state.transferred += size;
94 + return ret;
97 +static void pvebackup_cleanup(void)
99 + backup_state.end_time = time(NULL);
101 + if (backup_state.vmaw) {
102 + Error *local_err = NULL;
103 + vma_writer_close(backup_state.vmaw, &local_err);
104 + error_propagate(&backup_state.error, local_err);
105 + backup_state.vmaw = NULL;
108 + if (backup_state.di_list) {
109 + GList *l = backup_state.di_list;
110 + while (l) {
111 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
112 + l = g_list_next(l);
113 + g_free(di);
115 + g_list_free(backup_state.di_list);
116 + backup_state.di_list = NULL;
120 +static void pvebackup_complete_cb(void *opaque, int ret)
122 + PVEBackupDevInfo *di = opaque;
124 + assert(backup_state.vmaw);
126 + di->completed = true;
128 + if (ret < 0 && !backup_state.error) {
129 + error_setg(&backup_state.error, "job failed with err %d - %s",
130 + ret, strerror(-ret));
133 + BlockDriverState *bs = di->bs;
135 + di->bs = NULL;
137 + vma_writer_close_stream(backup_state.vmaw, di->dev_id);
139 + block_job_cb(bs, ret);
141 + if (!backup_state.cancel) {
142 + pvebackup_run_next_job();
146 +static void pvebackup_cancel(void *opaque)
148 + backup_state.cancel = true;
150 + if (!backup_state.error) {
151 + error_setg(&backup_state.error, "backup cancelled");
154 + /* drain all i/o (awake jobs waiting for aio) */
155 + bdrv_drain_all();
157 + GList *l = backup_state.di_list;
158 + while (l) {
159 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
160 + l = g_list_next(l);
161 + if (!di->completed && di->bs) {
162 + BlockJob *job = di->bs->job;
163 + if (job) {
164 + if (!di->completed) {
165 + block_job_cancel_sync(job);
171 + pvebackup_cleanup();
174 +void qmp_backup_cancel(Error **errp)
176 + Coroutine *co = qemu_coroutine_create(pvebackup_cancel);
177 + qemu_coroutine_enter(co, NULL);
179 + while (backup_state.vmaw) {
180 + /* vma writer use main aio context */
181 + aio_poll(qemu_get_aio_context(), true);
185 +static void pvebackup_run_next_job(void)
187 + GList *l = backup_state.di_list;
188 + while (l) {
189 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
190 + l = g_list_next(l);
191 + if (!di->completed && di->bs && di->bs->job) {
192 + BlockJob *job = di->bs->job;
193 + if (block_job_is_paused(job)) {
194 + bool cancel = backup_state.error || backup_state.cancel;
195 + if (cancel) {
196 + block_job_cancel(job);
197 + } else {
198 + block_job_resume(job);
201 + return;
205 + pvebackup_cleanup();
208 +UuidInfo *qmp_backup(const char *backup_file, bool has_format,
209 + BackupFormat format,
210 + bool has_config_file, const char *config_file,
211 + bool has_devlist, const char *devlist,
212 + bool has_speed, int64_t speed, Error **errp)
214 + BlockBackend *blk;
215 + BlockDriverState *bs = NULL;
216 + Error *local_err = NULL;
217 + uuid_t uuid;
218 + VmaWriter *vmaw = NULL;
219 + gchar **devs = NULL;
220 + GList *di_list = NULL;
221 + GList *l;
222 + UuidInfo *uuid_info;
224 + if (backup_state.di_list) {
225 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
226 + "previous backup not finished");
227 + return NULL;
230 + /* Todo: try to auto-detect format based on file name */
231 + format = has_format ? format : BACKUP_FORMAT_VMA;
233 + if (format != BACKUP_FORMAT_VMA) {
234 + error_set(errp, ERROR_CLASS_GENERIC_ERROR, "unknown backup format");
235 + return NULL;
238 + if (has_devlist) {
239 + devs = g_strsplit_set(devlist, ",;:", -1);
241 + gchar **d = devs;
242 + while (d && *d) {
243 + blk = blk_by_name(*d);
244 + if (blk) {
245 + bs = blk_bs(blk);
246 + if (bdrv_is_read_only(bs)) {
247 + error_setg(errp, "Node '%s' is read only", *d);
248 + goto err;
250 + if (!bdrv_is_inserted(bs)) {
251 + error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, *d);
252 + goto err;
254 + PVEBackupDevInfo *di = g_new0(PVEBackupDevInfo, 1);
255 + di->bs = bs;
256 + di_list = g_list_append(di_list, di);
257 + } else {
258 + error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
259 + "Device '%s' not found", *d);
260 + goto err;
262 + d++;
265 + } else {
267 + bs = NULL;
268 + while ((bs = bdrv_next(bs))) {
270 + if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
271 + continue;
274 + PVEBackupDevInfo *di = g_new0(PVEBackupDevInfo, 1);
275 + di->bs = bs;
276 + di_list = g_list_append(di_list, di);
280 + if (!di_list) {
281 + error_set(errp, ERROR_CLASS_GENERIC_ERROR, "empty device list");
282 + goto err;
285 + size_t total = 0;
287 + l = di_list;
288 + while (l) {
289 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
290 + l = g_list_next(l);
291 + if (bdrv_op_is_blocked(di->bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
292 + goto err;
295 + ssize_t size = bdrv_getlength(di->bs);
296 + if (size < 0) {
297 + error_setg_errno(errp, -di->size, "bdrv_getlength failed");
298 + goto err;
300 + di->size = size;
301 + total += size;
304 + uuid_generate(uuid);
306 + vmaw = vma_writer_create(backup_file, uuid, &local_err);
307 + if (!vmaw) {
308 + if (local_err) {
309 + error_propagate(errp, local_err);
311 + goto err;
314 + /* register all devices for vma writer */
315 + l = di_list;
316 + while (l) {
317 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
318 + l = g_list_next(l);
320 + const char *devname = bdrv_get_device_name(di->bs);
321 + di->dev_id = vma_writer_register_stream(vmaw, devname, di->size);
322 + if (di->dev_id <= 0) {
323 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
324 + "register_stream failed");
325 + goto err;
329 + /* add configuration file to archive */
330 + if (has_config_file) {
331 + char *cdata = NULL;
332 + gsize clen = 0;
333 + GError *err = NULL;
334 + if (!g_file_get_contents(config_file, &cdata, &clen, &err)) {
335 + error_setg(errp, "unable to read file '%s'", config_file);
336 + goto err;
339 + const char *basename = g_path_get_basename(config_file);
340 + if (vma_writer_add_config(vmaw, basename, cdata, clen) != 0) {
341 + error_setg(errp, "unable to add config data to vma archive");
342 + g_free(cdata);
343 + goto err;
345 + g_free(cdata);
348 + /* initialize global backup_state now */
350 + backup_state.cancel = false;
352 + if (backup_state.error) {
353 + error_free(backup_state.error);
354 + backup_state.error = NULL;
357 + backup_state.speed = (has_speed && speed > 0) ? speed : 0;
359 + backup_state.start_time = time(NULL);
360 + backup_state.end_time = 0;
362 + if (backup_state.backup_file) {
363 + g_free(backup_state.backup_file);
365 + backup_state.backup_file = g_strdup(backup_file);
367 + backup_state.vmaw = vmaw;
369 + uuid_copy(backup_state.uuid, uuid);
370 + uuid_unparse_lower(uuid, backup_state.uuid_str);
372 + backup_state.di_list = di_list;
374 + backup_state.total = total;
375 + backup_state.transferred = 0;
376 + backup_state.zero_bytes = 0;
378 + /* start all jobs (paused state) */
379 + l = di_list;
380 + while (l) {
381 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
382 + l = g_list_next(l);
384 + backup_start(di->bs, NULL, speed, MIRROR_SYNC_MODE_FULL, NULL,
385 + BLOCKDEV_ON_ERROR_REPORT, BLOCKDEV_ON_ERROR_REPORT,
386 + pvebackup_dump_cb, pvebackup_complete_cb, di,
387 + 1, &local_err);
388 + if (local_err != NULL) {
389 + error_setg(&backup_state.error, "backup_job_create failed");
390 + pvebackup_cancel(NULL);
394 + if (!backup_state.error) {
395 + pvebackup_run_next_job(); // run one job
398 + uuid_info = g_malloc0(sizeof(*uuid_info));
399 + uuid_info->UUID = g_strdup(backup_state.uuid_str);
400 + return uuid_info;
402 +err:
404 + l = di_list;
405 + while (l) {
406 + g_free(l->data);
407 + l = g_list_next(l);
409 + g_list_free(di_list);
411 + if (devs) {
412 + g_strfreev(devs);
415 + if (vmaw) {
416 + Error *err = NULL;
417 + vma_writer_close(vmaw, &err);
418 + unlink(backup_file);
421 + return NULL;
424 +BackupStatus *qmp_query_backup(Error **errp)
426 + BackupStatus *info = g_malloc0(sizeof(*info));
428 + if (!backup_state.start_time) {
429 + /* not started, return {} */
430 + return info;
433 + info->has_status = true;
434 + info->has_start_time = true;
435 + info->start_time = backup_state.start_time;
437 + if (backup_state.backup_file) {
438 + info->has_backup_file = true;
439 + info->backup_file = g_strdup(backup_state.backup_file);
442 + info->has_uuid = true;
443 + info->uuid = g_strdup(backup_state.uuid_str);
445 + if (backup_state.end_time) {
446 + if (backup_state.error) {
447 + info->status = g_strdup("error");
448 + info->has_errmsg = true;
449 + info->errmsg = g_strdup(error_get_pretty(backup_state.error));
450 + } else {
451 + info->status = g_strdup("done");
453 + info->has_end_time = true;
454 + info->end_time = backup_state.end_time;
455 + } else {
456 + info->status = g_strdup("active");
459 + info->has_total = true;
460 + info->total = backup_state.total;
461 + info->has_zero_bytes = true;
462 + info->zero_bytes = backup_state.zero_bytes;
463 + info->has_transferred = true;
464 + info->transferred = backup_state.transferred;
466 + return info;
469 void qmp_block_stream(const char *device,
470 bool has_base, const char *base,
471 bool has_backing_file, const char *backing_file,
472 diff --git a/hmp-commands-info.hx b/hmp-commands-info.hx
473 index 9b71351..355289a 100644
474 --- a/hmp-commands-info.hx
475 +++ b/hmp-commands-info.hx
476 @@ -502,6 +502,19 @@ STEXI
477 Show CPU statistics.
478 ETEXI
481 + .name = "backup",
482 + .args_type = "",
483 + .params = "",
484 + .help = "show backup status",
485 + .mhandler.cmd = hmp_info_backup,
486 + },
488 +STEXI
489 +@item info backup
490 +show backup status
491 +ETEXI
493 #if defined(CONFIG_SLIRP)
495 .name = "usernet",
496 diff --git a/hmp-commands.hx b/hmp-commands.hx
497 index bb52e4d..324125f 100644
498 --- a/hmp-commands.hx
499 +++ b/hmp-commands.hx
500 @@ -87,6 +87,35 @@ STEXI
501 Copy data from a backing file into a block device.
502 ETEXI
505 + .name = "backup",
506 + .args_type = "backupfile:s,speed:o?,devlist:s?",
507 + .params = "backupfile [speed [devlist]]",
508 + .help = "create a VM Backup.",
509 + .mhandler.cmd = hmp_backup,
510 + },
512 +STEXI
513 +@item backup
514 +@findex backup
515 +Create a VM backup.
516 +ETEXI
519 + .name = "backup_cancel",
520 + .args_type = "",
521 + .params = "",
522 + .help = "cancel the current VM backup",
523 + .mhandler.cmd = hmp_backup_cancel,
524 + },
526 +STEXI
527 +@item backup_cancel
528 +@findex backup_cancel
529 +Cancel the current VM backup.
531 +ETEXI
534 .name = "block_job_set_speed",
535 .args_type = "device:B,speed:o",
536 diff --git a/hmp.c b/hmp.c
537 index 0e63ea8..ebb16c1 100644
538 --- a/hmp.c
539 +++ b/hmp.c
540 @@ -146,6 +146,44 @@ void hmp_info_mice(Monitor *mon, const QDict *qdict)
541 qapi_free_MouseInfoList(mice_list);
544 +void hmp_info_backup(Monitor *mon, const QDict *qdict)
546 + BackupStatus *info;
548 + info = qmp_query_backup(NULL);
549 + if (info->has_status) {
550 + if (info->has_errmsg) {
551 + monitor_printf(mon, "Backup status: %s - %s\n",
552 + info->status, info->errmsg);
553 + } else {
554 + monitor_printf(mon, "Backup status: %s\n", info->status);
558 + if (info->has_backup_file) {
559 + monitor_printf(mon, "Start time: %s", ctime(&info->start_time));
560 + if (info->end_time) {
561 + monitor_printf(mon, "End time: %s", ctime(&info->end_time));
564 + int per = (info->has_total && info->total &&
565 + info->has_transferred && info->transferred) ?
566 + (info->transferred * 100)/info->total : 0;
567 + int zero_per = (info->has_total && info->total &&
568 + info->has_zero_bytes && info->zero_bytes) ?
569 + (info->zero_bytes * 100)/info->total : 0;
570 + monitor_printf(mon, "Backup file: %s\n", info->backup_file);
571 + monitor_printf(mon, "Backup uuid: %s\n", info->uuid);
572 + monitor_printf(mon, "Total size: %zd\n", info->total);
573 + monitor_printf(mon, "Transferred bytes: %zd (%d%%)\n",
574 + info->transferred, per);
575 + monitor_printf(mon, "Zero bytes: %zd (%d%%)\n",
576 + info->zero_bytes, zero_per);
579 + qapi_free_BackupStatus(info);
582 void hmp_info_migrate(Monitor *mon, const QDict *qdict)
584 MigrationInfo *info;
585 @@ -1461,6 +1499,29 @@ void hmp_block_stream(Monitor *mon, const QDict *qdict)
586 hmp_handle_error(mon, &error);
589 +void hmp_backup_cancel(Monitor *mon, const QDict *qdict)
591 + Error *error = NULL;
593 + qmp_backup_cancel(&error);
595 + hmp_handle_error(mon, &error);
598 +void hmp_backup(Monitor *mon, const QDict *qdict)
600 + Error *error = NULL;
602 + const char *backup_file = qdict_get_str(qdict, "backupfile");
603 + const char *devlist = qdict_get_try_str(qdict, "devlist");
604 + int64_t speed = qdict_get_try_int(qdict, "speed", 0);
606 + qmp_backup(backup_file, true, BACKUP_FORMAT_VMA, false, NULL, !!devlist,
607 + devlist, qdict_haskey(qdict, "speed"), speed, &error);
609 + hmp_handle_error(mon, &error);
612 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
614 Error *error = NULL;
615 diff --git a/hmp.h b/hmp.h
616 index a8c5b5a..0b39c25 100644
617 --- a/hmp.h
618 +++ b/hmp.h
619 @@ -30,6 +30,7 @@ void hmp_info_migrate(Monitor *mon, const QDict *qdict);
620 void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict);
621 void hmp_info_migrate_parameters(Monitor *mon, const QDict *qdict);
622 void hmp_info_migrate_cache_size(Monitor *mon, const QDict *qdict);
623 +void hmp_info_backup(Monitor *mon, const QDict *qdict);
624 void hmp_info_cpus(Monitor *mon, const QDict *qdict);
625 void hmp_info_block(Monitor *mon, const QDict *qdict);
626 void hmp_info_blockstats(Monitor *mon, const QDict *qdict);
627 @@ -76,6 +77,8 @@ void hmp_eject(Monitor *mon, const QDict *qdict);
628 void hmp_change(Monitor *mon, const QDict *qdict);
629 void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict);
630 void hmp_block_stream(Monitor *mon, const QDict *qdict);
631 +void hmp_backup(Monitor *mon, const QDict *qdict);
632 +void hmp_backup_cancel(Monitor *mon, const QDict *qdict);
633 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict);
634 void hmp_block_job_cancel(Monitor *mon, const QDict *qdict);
635 void hmp_block_job_pause(Monitor *mon, const QDict *qdict);
636 diff --git a/qapi-schema.json b/qapi-schema.json
637 index 1aa434d..edfb3a4 100644
638 --- a/qapi-schema.json
639 +++ b/qapi-schema.json
640 @@ -358,6 +358,95 @@
642 { 'command': 'query-events', 'returns': ['EventInfo'] }
644 +# @BackupStatus:
646 +# Detailed backup status.
648 +# @status: #optional string describing the current backup status.
649 +# This can be 'active', 'done', 'error'. If this field is not
650 +# returned, no backup process has been initiated
652 +# @errmsg: #optional error message (only returned if status is 'error')
654 +# @total: #optional total amount of bytes involved in the backup process
656 +# @transferred: #optional amount of bytes already backed up.
658 +# @zero-bytes: #optional amount of 'zero' bytes detected.
660 +# @start-time: #optional time (epoch) when backup job started.
662 +# @end-time: #optional time (epoch) when backup job finished.
664 +# @backupfile: #optional backup file name
666 +# @uuid: #optional uuid for this backup job
669 +{ 'struct': 'BackupStatus',
670 + 'data': {'*status': 'str', '*errmsg': 'str', '*total': 'int',
671 + '*transferred': 'int', '*zero-bytes': 'int',
672 + '*start-time': 'int', '*end-time': 'int',
673 + '*backup-file': 'str', '*uuid': 'str' } }
676 +# @BackupFormat
678 +# An enumeration of supported backup formats.
680 +# @vma: Proxmox vma backup format
682 +{ 'enum': 'BackupFormat',
683 + 'data': [ 'vma' ] }
686 +# @backup:
688 +# Starts a VM backup.
690 +# @backup-file: the backup file name
692 +# @format: format of the backup file
694 +# @config-filename: #optional name of a configuration file to include into
695 +# the backup archive.
697 +# @speed: #optional the maximum speed, in bytes per second
699 +# @devlist: #optional list of block device names (separated by ',', ';'
700 +# or ':'). By default the backup includes all writable block devices.
702 +# Returns: the uuid of the backup job
705 +{ 'command': 'backup', 'data': { 'backup-file': 'str',
706 + '*format': 'BackupFormat',
707 + '*config-file': 'str',
708 + '*devlist': 'str', '*speed': 'int' },
709 + 'returns': 'UuidInfo' }
712 +# @query-backup
714 +# Returns information about current/last backup task.
716 +# Returns: @BackupStatus
719 +{ 'command': 'query-backup', 'returns': 'BackupStatus' }
722 +# @backup-cancel
724 +# Cancel the current executing backup process.
726 +# Returns: nothing on success
728 +# Notes: This command succeeds even if there is no backup process running.
731 +{ 'command': 'backup-cancel' }
734 # @MigrationStats
736 diff --git a/qmp-commands.hx b/qmp-commands.hx
737 index 5c23d91..3dea794 100644
738 --- a/qmp-commands.hx
739 +++ b/qmp-commands.hx
740 @@ -1254,6 +1254,24 @@ Example:
741 EQMP
744 + .name = "backup",
745 + .args_type = "backup-file:s,format:s?,config-file:F?,speed:o?,devlist:s?",
746 + .mhandler.cmd_new = qmp_marshal_input_backup,
747 + },
750 + .name = "backup-cancel",
751 + .args_type = "",
752 + .mhandler.cmd_new = qmp_marshal_input_backup_cancel,
753 + },
756 + .name = "query-backup",
757 + .args_type = "",
758 + .mhandler.cmd_new = qmp_marshal_input_query_backup,
759 + },
762 .name = "block-job-set-speed",
763 .args_type = "device:B,speed:o",
764 .mhandler.cmd_new = qmp_marshal_block_job_set_speed,
766 2.1.4