1 From 0ea55080ddb9b9503dae8c35671a76fa5578572f Mon Sep 17 00:00:00 2001
2 From: Wolfgang Bumiller <w.bumiller@proxmox.com>
3 Date: Wed, 9 Dec 2015 15:21:54 +0100
4 Subject: [PATCH 17/55] backup: vma: add dir format
7 blockdev.c | 126 +++++++++++++++++++++++++++++++++++++++++--------------
8 hmp-commands.hx | 8 ++--
10 qapi-schema.json | 2 +-
11 4 files changed, 104 insertions(+), 36 deletions(-)
13 diff --git a/blockdev.c b/blockdev.c
14 index 115524f..0537a53 100644
17 @@ -3014,6 +3014,8 @@ typedef struct PVEBackupDevInfo {
21 + char targetfile[PATH_MAX];
22 + BlockDriverState *target;
25 static void pvebackup_run_next_job(void);
26 @@ -3082,8 +3084,6 @@ static void pvebackup_complete_cb(void *opaque, int ret)
28 PVEBackupDevInfo *di = opaque;
30 - assert(backup_state.vmaw);
34 if (ret < 0 && !backup_state.error) {
35 @@ -3094,8 +3094,11 @@ static void pvebackup_complete_cb(void *opaque, int ret)
36 BlockDriverState *bs = di->bs;
41 - vma_writer_close_stream(backup_state.vmaw, di->dev_id);
42 + if (backup_state.vmaw) {
43 + vma_writer_close_stream(backup_state.vmaw, di->dev_id);
46 block_job_cb(bs, ret);
48 @@ -3174,6 +3177,7 @@ UuidInfo *qmp_backup(const char *backup_file, bool has_format,
51 BlockDriverState *bs = NULL;
52 + const char *backup_dir = NULL;
53 Error *local_err = NULL;
55 VmaWriter *vmaw = NULL;
56 @@ -3191,11 +3195,6 @@ UuidInfo *qmp_backup(const char *backup_file, bool has_format,
57 /* Todo: try to auto-detect format based on file name */
58 format = has_format ? format : BACKUP_FORMAT_VMA;
60 - if (format != BACKUP_FORMAT_VMA) {
61 - error_set(errp, ERROR_CLASS_GENERIC_ERROR, "unknown backup format");
66 devs = g_strsplit_set(devlist, ",;:", -1);
68 @@ -3264,27 +3263,63 @@ UuidInfo *qmp_backup(const char *backup_file, bool has_format,
72 - vmaw = vma_writer_create(backup_file, uuid, &local_err);
75 - error_propagate(errp, local_err);
76 + if (format == BACKUP_FORMAT_VMA) {
77 + vmaw = vma_writer_create(backup_file, uuid, &local_err);
80 + error_propagate(errp, local_err);
87 - /* register all devices for vma writer */
90 - PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
92 + /* register all devices for vma writer */
95 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
98 - const char *devname = bdrv_get_device_name(di->bs);
99 - di->dev_id = vma_writer_register_stream(vmaw, devname, di->size);
100 - if (di->dev_id <= 0) {
101 - error_set(errp, ERROR_CLASS_GENERIC_ERROR,
102 - "register_stream failed");
103 + const char *devname = bdrv_get_device_name(di->bs);
104 + di->dev_id = vma_writer_register_stream(vmaw, devname, di->size);
105 + if (di->dev_id <= 0) {
106 + error_set(errp, ERROR_CLASS_GENERIC_ERROR,
107 + "register_stream failed");
111 + } else if (format == BACKUP_FORMAT_DIR) {
112 + if (mkdir(backup_file, 0640) != 0) {
113 + error_setg_errno(errp, errno, "can't create directory '%s'\n",
117 + backup_dir = backup_file;
121 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
122 + l = g_list_next(l);
124 + const char *devname = bdrv_get_device_name(di->bs);
125 + snprintf(di->targetfile, PATH_MAX, "%s/%s.raw", backup_dir, devname);
127 + int flags = BDRV_O_RDWR|BDRV_O_CACHE_WB;
128 + bdrv_img_create(di->targetfile, "raw", NULL, NULL, NULL,
129 + di->size, flags, &local_err, false);
131 + error_propagate(errp, local_err);
135 + di->target = bdrv_new();
136 + if (bdrv_open(&di->target, di->targetfile, NULL, NULL, flags, NULL, &local_err) < 0) {
137 + bdrv_unref(di->target);
138 + error_propagate(errp, local_err);
143 + error_set(errp, ERROR_CLASS_GENERIC_ERROR, "unknown backup format");
147 /* add configuration file to archive */
148 @@ -3297,12 +3332,27 @@ UuidInfo *qmp_backup(const char *backup_file, bool has_format,
152 - const char *basename = g_path_get_basename(config_file);
153 - if (vma_writer_add_config(vmaw, basename, cdata, clen) != 0) {
154 - error_setg(errp, "unable to add config data to vma archive");
157 + char *basename = g_path_get_basename(config_file);
159 + if (format == BACKUP_FORMAT_VMA) {
160 + if (vma_writer_add_config(vmaw, basename, cdata, clen) != 0) {
161 + error_setg(errp, "unable to add config data to vma archive");
166 + } else if (format == BACKUP_FORMAT_DIR) {
167 + char config_path[PATH_MAX];
168 + snprintf(config_path, PATH_MAX, "%s/%s", backup_dir, basename);
169 + if (!g_file_set_contents(config_path, cdata, clen, &err)) {
170 + error_setg(errp, "unable to write config file '%s'", config_path);
181 @@ -3342,10 +3392,11 @@ UuidInfo *qmp_backup(const char *backup_file, bool has_format,
182 PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
185 - backup_start(di->bs, NULL, speed, MIRROR_SYNC_MODE_FULL, NULL,
186 + backup_start(di->bs, di->target, speed, MIRROR_SYNC_MODE_FULL, NULL,
187 BLOCKDEV_ON_ERROR_REPORT, BLOCKDEV_ON_ERROR_REPORT,
188 pvebackup_dump_cb, pvebackup_complete_cb, di,
191 if (local_err != NULL) {
192 error_setg(&backup_state.error, "backup_job_create failed");
193 pvebackup_cancel(NULL);
194 @@ -3364,8 +3415,17 @@ err:
199 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
203 + bdrv_unref(di->target);
206 + if (di->targetfile[0]) {
207 + unlink(di->targetfile);
211 g_list_free(di_list);
213 @@ -3379,6 +3439,10 @@ err:
224 diff --git a/hmp-commands.hx b/hmp-commands.hx
225 index 63c44f5..8a94e36 100644
226 --- a/hmp-commands.hx
227 +++ b/hmp-commands.hx
228 @@ -89,9 +89,11 @@ ETEXI
232 - .args_type = "backupfile:s,speed:o?,devlist:s?",
233 - .params = "backupfile [speed [devlist]]",
234 - .help = "create a VM Backup.",
235 + .args_type = "directory:-d,backupfile:s,speed:o?,devlist:s?",
236 + .params = "[-d] backupfile [speed [devlist]]",
237 + .help = "create a VM Backup."
238 + "\n\t\t\t Use -d to dump data into a directory instead"
239 + "\n\t\t\t of using VMA format.",
240 .mhandler.cmd = hmp_backup,
243 diff --git a/hmp.c b/hmp.c
244 index 2d4bf97..040909e 100644
247 @@ -1527,11 +1527,13 @@ void hmp_backup(Monitor *mon, const QDict *qdict)
251 + int dir = qdict_get_try_bool(qdict, "directory", 0);
252 const char *backup_file = qdict_get_str(qdict, "backupfile");
253 const char *devlist = qdict_get_try_str(qdict, "devlist");
254 int64_t speed = qdict_get_try_int(qdict, "speed", 0);
256 - qmp_backup(backup_file, true, BACKUP_FORMAT_VMA, false, NULL, !!devlist,
257 + qmp_backup(backup_file, true, dir ? BACKUP_FORMAT_DIR : BACKUP_FORMAT_VMA,
258 + false, NULL, !!devlist,
259 devlist, qdict_haskey(qdict, "speed"), speed, &error);
261 hmp_handle_error(mon, &error);
262 diff --git a/qapi-schema.json b/qapi-schema.json
263 index 4f673d4..3427d50 100644
264 --- a/qapi-schema.json
265 +++ b/qapi-schema.json
267 # @vma: Proxmox vma backup format
269 { 'enum': 'BackupFormat',
270 - 'data': [ 'vma' ] }
271 + 'data': [ 'vma', 'dir' ] }