]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/import/import-raw.c
Merge pull request #1783 from vcaputo/still_make_progress_when_throttling
[thirdparty/systemd.git] / src / import / import-raw.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2015 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <linux/fs.h>
23
24 #include "sd-daemon.h"
25 #include "sd-event.h"
26
27 #include "alloc-util.h"
28 #include "btrfs-util.h"
29 #include "chattr-util.h"
30 #include "copy.h"
31 #include "fd-util.h"
32 #include "fileio.h"
33 #include "fs-util.h"
34 #include "hostname-util.h"
35 #include "import-common.h"
36 #include "import-compress.h"
37 #include "import-raw.h"
38 #include "io-util.h"
39 #include "machine-pool.h"
40 #include "mkdir.h"
41 #include "path-util.h"
42 #include "qcow2-util.h"
43 #include "ratelimit.h"
44 #include "rm-rf.h"
45 #include "string-util.h"
46 #include "util.h"
47
48 struct RawImport {
49 sd_event *event;
50
51 char *image_root;
52
53 RawImportFinished on_finished;
54 void *userdata;
55
56 char *local;
57 bool force_local;
58 bool read_only;
59 bool grow_machine_directory;
60
61 char *temp_path;
62 char *final_path;
63
64 int input_fd;
65 int output_fd;
66
67 ImportCompress compress;
68
69 uint64_t written_since_last_grow;
70
71 sd_event_source *input_event_source;
72
73 uint8_t buffer[16*1024];
74 size_t buffer_size;
75
76 uint64_t written_compressed;
77 uint64_t written_uncompressed;
78
79 struct stat st;
80
81 unsigned last_percent;
82 RateLimit progress_rate_limit;
83 };
84
85 RawImport* raw_import_unref(RawImport *i) {
86 if (!i)
87 return NULL;
88
89 sd_event_unref(i->event);
90
91 if (i->temp_path) {
92 (void) unlink(i->temp_path);
93 free(i->temp_path);
94 }
95
96 import_compress_free(&i->compress);
97
98 sd_event_source_unref(i->input_event_source);
99
100 safe_close(i->output_fd);
101
102 free(i->final_path);
103 free(i->image_root);
104 free(i->local);
105 free(i);
106
107 return NULL;
108 }
109
110 int raw_import_new(
111 RawImport **ret,
112 sd_event *event,
113 const char *image_root,
114 RawImportFinished on_finished,
115 void *userdata) {
116
117 _cleanup_(raw_import_unrefp) RawImport *i = NULL;
118 int r;
119
120 assert(ret);
121
122 i = new0(RawImport, 1);
123 if (!i)
124 return -ENOMEM;
125
126 i->input_fd = i->output_fd = -1;
127 i->on_finished = on_finished;
128 i->userdata = userdata;
129
130 RATELIMIT_INIT(i->progress_rate_limit, 100 * USEC_PER_MSEC, 1);
131 i->last_percent = (unsigned) -1;
132
133 i->image_root = strdup(image_root ?: "/var/lib/machines");
134 if (!i->image_root)
135 return -ENOMEM;
136
137 i->grow_machine_directory = path_startswith(i->image_root, "/var/lib/machines");
138
139 if (event)
140 i->event = sd_event_ref(event);
141 else {
142 r = sd_event_default(&i->event);
143 if (r < 0)
144 return r;
145 }
146
147 *ret = i;
148 i = NULL;
149
150 return 0;
151 }
152
153 static void raw_import_report_progress(RawImport *i) {
154 unsigned percent;
155 assert(i);
156
157 /* We have no size information, unless the source is a regular file */
158 if (!S_ISREG(i->st.st_mode))
159 return;
160
161 if (i->written_compressed >= (uint64_t) i->st.st_size)
162 percent = 100;
163 else
164 percent = (unsigned) ((i->written_compressed * UINT64_C(100)) / (uint64_t) i->st.st_size);
165
166 if (percent == i->last_percent)
167 return;
168
169 if (!ratelimit_test(&i->progress_rate_limit))
170 return;
171
172 sd_notifyf(false, "X_IMPORT_PROGRESS=%u", percent);
173 log_info("Imported %u%%.", percent);
174
175 i->last_percent = percent;
176 }
177
178 static int raw_import_maybe_convert_qcow2(RawImport *i) {
179 _cleanup_close_ int converted_fd = -1;
180 _cleanup_free_ char *t = NULL;
181 int r;
182
183 assert(i);
184
185 r = qcow2_detect(i->output_fd);
186 if (r < 0)
187 return log_error_errno(r, "Failed to detect whether this is a QCOW2 image: %m");
188 if (r == 0)
189 return 0;
190
191 /* This is a QCOW2 image, let's convert it */
192 r = tempfn_random(i->final_path, NULL, &t);
193 if (r < 0)
194 return log_oom();
195
196 converted_fd = open(t, O_RDWR|O_CREAT|O_EXCL|O_NOCTTY|O_CLOEXEC, 0664);
197 if (converted_fd < 0)
198 return log_error_errno(errno, "Failed to create %s: %m", t);
199
200 r = chattr_fd(converted_fd, FS_NOCOW_FL, FS_NOCOW_FL);
201 if (r < 0)
202 log_warning_errno(r, "Failed to set file attributes on %s: %m", t);
203
204 log_info("Unpacking QCOW2 file.");
205
206 r = qcow2_convert(i->output_fd, converted_fd);
207 if (r < 0) {
208 unlink(t);
209 return log_error_errno(r, "Failed to convert qcow2 image: %m");
210 }
211
212 (void) unlink(i->temp_path);
213 free(i->temp_path);
214 i->temp_path = t;
215 t = NULL;
216
217 safe_close(i->output_fd);
218 i->output_fd = converted_fd;
219 converted_fd = -1;
220
221 return 1;
222 }
223
224 static int raw_import_finish(RawImport *i) {
225 int r;
226
227 assert(i);
228 assert(i->output_fd >= 0);
229 assert(i->temp_path);
230 assert(i->final_path);
231
232 /* In case this was a sparse file, make sure the file system is right */
233 if (i->written_uncompressed > 0) {
234 if (ftruncate(i->output_fd, i->written_uncompressed) < 0)
235 return log_error_errno(errno, "Failed to truncate file: %m");
236 }
237
238 r = raw_import_maybe_convert_qcow2(i);
239 if (r < 0)
240 return r;
241
242 if (S_ISREG(i->st.st_mode)) {
243 (void) copy_times(i->input_fd, i->output_fd);
244 (void) copy_xattr(i->input_fd, i->output_fd);
245 }
246
247 if (i->read_only) {
248 r = import_make_read_only_fd(i->output_fd);
249 if (r < 0)
250 return r;
251 }
252
253 if (i->force_local)
254 (void) rm_rf(i->final_path, REMOVE_ROOT|REMOVE_PHYSICAL|REMOVE_SUBVOLUME);
255
256 r = rename_noreplace(AT_FDCWD, i->temp_path, AT_FDCWD, i->final_path);
257 if (r < 0)
258 return log_error_errno(r, "Failed to move image into place: %m");
259
260 i->temp_path = mfree(i->temp_path);
261
262 return 0;
263 }
264
265 static int raw_import_open_disk(RawImport *i) {
266 int r;
267
268 assert(i);
269
270 assert(!i->final_path);
271 assert(!i->temp_path);
272 assert(i->output_fd < 0);
273
274 i->final_path = strjoin(i->image_root, "/", i->local, ".raw", NULL);
275 if (!i->final_path)
276 return log_oom();
277
278 r = tempfn_random(i->final_path, NULL, &i->temp_path);
279 if (r < 0)
280 return log_oom();
281
282 (void) mkdir_parents_label(i->temp_path, 0700);
283
284 i->output_fd = open(i->temp_path, O_RDWR|O_CREAT|O_EXCL|O_NOCTTY|O_CLOEXEC, 0664);
285 if (i->output_fd < 0)
286 return log_error_errno(errno, "Failed to open destination %s: %m", i->temp_path);
287
288 r = chattr_fd(i->output_fd, FS_NOCOW_FL, FS_NOCOW_FL);
289 if (r < 0)
290 log_warning_errno(r, "Failed to set file attributes on %s: %m", i->temp_path);
291
292 return 0;
293 }
294
295 static int raw_import_try_reflink(RawImport *i) {
296 off_t p;
297 int r;
298
299 assert(i);
300 assert(i->input_fd >= 0);
301 assert(i->output_fd >= 0);
302
303 if (i->compress.type != IMPORT_COMPRESS_UNCOMPRESSED)
304 return 0;
305
306 if (!S_ISREG(i->st.st_mode))
307 return 0;
308
309 p = lseek(i->input_fd, 0, SEEK_CUR);
310 if (p == (off_t) -1)
311 return log_error_errno(errno, "Failed to read file offset of input file: %m");
312
313 /* Let's only try a btrfs reflink, if we are reading from the beginning of the file */
314 if ((uint64_t) p != (uint64_t) i->buffer_size)
315 return 0;
316
317 r = btrfs_reflink(i->input_fd, i->output_fd);
318 if (r >= 0)
319 return 1;
320
321 return 0;
322 }
323
324 static int raw_import_write(const void *p, size_t sz, void *userdata) {
325 RawImport *i = userdata;
326 ssize_t n;
327
328 if (i->grow_machine_directory && i->written_since_last_grow >= GROW_INTERVAL_BYTES) {
329 i->written_since_last_grow = 0;
330 grow_machine_directory();
331 }
332
333 n = sparse_write(i->output_fd, p, sz, 64);
334 if (n < 0)
335 return -errno;
336 if ((size_t) n < sz)
337 return -EIO;
338
339 i->written_uncompressed += sz;
340 i->written_since_last_grow += sz;
341
342 return 0;
343 }
344
345 static int raw_import_process(RawImport *i) {
346 ssize_t l;
347 int r;
348
349 assert(i);
350 assert(i->buffer_size < sizeof(i->buffer));
351
352 l = read(i->input_fd, i->buffer + i->buffer_size, sizeof(i->buffer) - i->buffer_size);
353 if (l < 0) {
354 if (errno == EAGAIN)
355 return 0;
356
357 r = log_error_errno(errno, "Failed to read input file: %m");
358 goto finish;
359 }
360 if (l == 0) {
361 if (i->compress.type == IMPORT_COMPRESS_UNKNOWN) {
362 log_error("Premature end of file: %m");
363 r = -EIO;
364 goto finish;
365 }
366
367 r = raw_import_finish(i);
368 goto finish;
369 }
370
371 i->buffer_size += l;
372
373 if (i->compress.type == IMPORT_COMPRESS_UNKNOWN) {
374 r = import_uncompress_detect(&i->compress, i->buffer, i->buffer_size);
375 if (r < 0) {
376 log_error("Failed to detect file compression: %m");
377 goto finish;
378 }
379 if (r == 0) /* Need more data */
380 return 0;
381
382 r = raw_import_open_disk(i);
383 if (r < 0)
384 goto finish;
385
386 r = raw_import_try_reflink(i);
387 if (r < 0)
388 goto finish;
389 if (r > 0) {
390 r = raw_import_finish(i);
391 goto finish;
392 }
393 }
394
395 r = import_uncompress(&i->compress, i->buffer, i->buffer_size, raw_import_write, i);
396 if (r < 0) {
397 log_error_errno(r, "Failed to decode and write: %m");
398 goto finish;
399 }
400
401 i->written_compressed += i->buffer_size;
402 i->buffer_size = 0;
403
404 raw_import_report_progress(i);
405
406 return 0;
407
408 finish:
409 if (i->on_finished)
410 i->on_finished(i, r, i->userdata);
411 else
412 sd_event_exit(i->event, r);
413
414 return 0;
415 }
416
417 static int raw_import_on_input(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
418 RawImport *i = userdata;
419
420 return raw_import_process(i);
421 }
422
423 static int raw_import_on_defer(sd_event_source *s, void *userdata) {
424 RawImport *i = userdata;
425
426 return raw_import_process(i);
427 }
428
429 int raw_import_start(RawImport *i, int fd, const char *local, bool force_local, bool read_only) {
430 int r;
431
432 assert(i);
433 assert(fd >= 0);
434 assert(local);
435
436 if (!machine_name_is_valid(local))
437 return -EINVAL;
438
439 if (i->input_fd >= 0)
440 return -EBUSY;
441
442 r = fd_nonblock(fd, true);
443 if (r < 0)
444 return r;
445
446 r = free_and_strdup(&i->local, local);
447 if (r < 0)
448 return r;
449 i->force_local = force_local;
450 i->read_only = read_only;
451
452 if (fstat(fd, &i->st) < 0)
453 return -errno;
454
455 r = sd_event_add_io(i->event, &i->input_event_source, fd, EPOLLIN, raw_import_on_input, i);
456 if (r == -EPERM) {
457 /* This fd does not support epoll, for example because it is a regular file. Busy read in that case */
458 r = sd_event_add_defer(i->event, &i->input_event_source, raw_import_on_defer, i);
459 if (r < 0)
460 return r;
461
462 r = sd_event_source_set_enabled(i->input_event_source, SD_EVENT_ON);
463 }
464 if (r < 0)
465 return r;
466
467 i->input_fd = fd;
468 return r;
469 }