various CVE fixes
[pve-qemu-kvm.git] / debian / patches / pve / 0017-backup-vma-add-dir-format.patch
blobf1bf20c93e387febddfbe798db25140e24e0082d
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
6 ---
7 blockdev.c | 126 +++++++++++++++++++++++++++++++++++++++++--------------
8 hmp-commands.hx | 8 ++--
9 hmp.c | 4 +-
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
15 --- a/blockdev.c
16 +++ b/blockdev.c
17 @@ -3014,6 +3014,8 @@ typedef struct PVEBackupDevInfo {
18 uint8_t dev_id;
19 //bool started;
20 bool completed;
21 + char targetfile[PATH_MAX];
22 + BlockDriverState *target;
23 } PVEBackupDevInfo;
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);
32 di->completed = true;
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;
38 di->bs = NULL;
39 + di->target = NULL;
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);
44 + }
46 block_job_cb(bs, ret);
48 @@ -3174,6 +3177,7 @@ UuidInfo *qmp_backup(const char *backup_file, bool has_format,
50 BlockBackend *blk;
51 BlockDriverState *bs = NULL;
52 + const char *backup_dir = NULL;
53 Error *local_err = NULL;
54 uuid_t uuid;
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");
62 - return NULL;
63 - }
65 if (has_devlist) {
66 devs = g_strsplit_set(devlist, ",;:", -1);
68 @@ -3264,27 +3263,63 @@ UuidInfo *qmp_backup(const char *backup_file, bool has_format,
70 uuid_generate(uuid);
72 - vmaw = vma_writer_create(backup_file, uuid, &local_err);
73 - if (!vmaw) {
74 - if (local_err) {
75 - error_propagate(errp, local_err);
76 + if (format == BACKUP_FORMAT_VMA) {
77 + vmaw = vma_writer_create(backup_file, uuid, &local_err);
78 + if (!vmaw) {
79 + if (local_err) {
80 + error_propagate(errp, local_err);
81 + }
82 + goto err;
84 - goto err;
85 - }
87 - /* register all devices for vma writer */
88 - l = di_list;
89 - while (l) {
90 - PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
91 - l = g_list_next(l);
92 + /* register all devices for vma writer */
93 + l = di_list;
94 + while (l) {
95 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
96 + l = g_list_next(l);
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");
108 + goto err;
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",
114 + backup_file);
115 goto err;
117 + backup_dir = backup_file;
119 + l = di_list;
120 + while (l) {
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);
130 + if (local_err) {
131 + error_propagate(errp, local_err);
132 + goto 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);
139 + goto err;
142 + } else {
143 + error_set(errp, ERROR_CLASS_GENERIC_ERROR, "unknown backup format");
144 + goto err;
147 /* add configuration file to archive */
148 @@ -3297,12 +3332,27 @@ UuidInfo *qmp_backup(const char *backup_file, bool has_format,
149 goto err;
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");
155 - g_free(cdata);
156 - goto err;
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");
162 + g_free(cdata);
163 + g_free(basename);
164 + goto err;
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);
171 + g_free(cdata);
172 + g_free(basename);
173 + goto err;
177 + g_free(basename);
178 g_free(cdata);
181 @@ -3342,10 +3392,11 @@ UuidInfo *qmp_backup(const char *backup_file, bool has_format,
182 PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
183 l = g_list_next(l);
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,
189 1, &local_err);
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:
196 l = di_list;
197 while (l) {
198 - g_free(l->data);
199 + PVEBackupDevInfo *di = (PVEBackupDevInfo *)l->data;
200 l = g_list_next(l);
202 + if (di->target) {
203 + bdrv_unref(di->target);
206 + if (di->targetfile[0]) {
207 + unlink(di->targetfile);
209 + g_free(di);
211 g_list_free(di_list);
213 @@ -3379,6 +3439,10 @@ err:
214 unlink(backup_file);
217 + if (backup_dir) {
218 + rmdir(backup_dir);
221 return NULL;
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
231 .name = "backup",
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
245 --- a/hmp.c
246 +++ b/hmp.c
247 @@ -1527,11 +1527,13 @@ void hmp_backup(Monitor *mon, const QDict *qdict)
249 Error *error = NULL;
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
266 @@ -395,7 +395,7 @@
267 # @vma: Proxmox vma backup format
269 { 'enum': 'BackupFormat',
270 - 'data': [ 'vma' ] }
271 + 'data': [ 'vma', 'dir' ] }
274 # @backup:
276 2.1.4