]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/import/import-raw.c
tree-wide: use ASSERT_PTR more
[thirdparty/systemd.git] / src / import / import-raw.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <linux/fs.h>
4
5 #include "sd-daemon.h"
6 #include "sd-event.h"
7
8 #include "alloc-util.h"
9 #include "btrfs-util.h"
10 #include "copy.h"
11 #include "fd-util.h"
12 #include "format-util.h"
13 #include "fs-util.h"
14 #include "hostname-util.h"
15 #include "import-common.h"
16 #include "import-compress.h"
17 #include "import-raw.h"
18 #include "install-file.h"
19 #include "io-util.h"
20 #include "machine-pool.h"
21 #include "mkdir-label.h"
22 #include "path-util.h"
23 #include "qcow2-util.h"
24 #include "ratelimit.h"
25 #include "rm-rf.h"
26 #include "string-util.h"
27 #include "tmpfile-util.h"
28 #include "util.h"
29
30 struct RawImport {
31 sd_event *event;
32
33 char *image_root;
34
35 RawImportFinished on_finished;
36 void *userdata;
37
38 char *local;
39 ImportFlags flags;
40
41 char *temp_path;
42 char *final_path;
43
44 int input_fd;
45 int output_fd;
46
47 ImportCompress compress;
48
49 sd_event_source *input_event_source;
50
51 uint8_t buffer[16*1024];
52 size_t buffer_size;
53
54 uint64_t written_compressed;
55 uint64_t written_uncompressed;
56
57 struct stat input_stat;
58 struct stat output_stat;
59
60 unsigned last_percent;
61 RateLimit progress_ratelimit;
62
63 uint64_t offset;
64 uint64_t size_max;
65 };
66
67 RawImport* raw_import_unref(RawImport *i) {
68 if (!i)
69 return NULL;
70
71 sd_event_source_unref(i->input_event_source);
72
73 unlink_and_free(i->temp_path);
74
75 import_compress_free(&i->compress);
76
77 sd_event_unref(i->event);
78
79 safe_close(i->output_fd);
80
81 free(i->final_path);
82 free(i->image_root);
83 free(i->local);
84 return mfree(i);
85 }
86
87 int raw_import_new(
88 RawImport **ret,
89 sd_event *event,
90 const char *image_root,
91 RawImportFinished on_finished,
92 void *userdata) {
93
94 _cleanup_(raw_import_unrefp) RawImport *i = NULL;
95 _cleanup_free_ char *root = NULL;
96 int r;
97
98 assert(ret);
99
100 root = strdup(image_root ?: "/var/lib/machines");
101 if (!root)
102 return -ENOMEM;
103
104 i = new(RawImport, 1);
105 if (!i)
106 return -ENOMEM;
107
108 *i = (RawImport) {
109 .input_fd = -1,
110 .output_fd = -1,
111 .on_finished = on_finished,
112 .userdata = userdata,
113 .last_percent = UINT_MAX,
114 .image_root = TAKE_PTR(root),
115 .progress_ratelimit = { 100 * USEC_PER_MSEC, 1 },
116 .offset = UINT64_MAX,
117 .size_max = UINT64_MAX,
118 };
119
120 if (event)
121 i->event = sd_event_ref(event);
122 else {
123 r = sd_event_default(&i->event);
124 if (r < 0)
125 return r;
126 }
127
128 *ret = TAKE_PTR(i);
129 return 0;
130 }
131
132 static void raw_import_report_progress(RawImport *i) {
133 unsigned percent;
134 assert(i);
135
136 /* We have no size information, unless the source is a regular file */
137 if (!S_ISREG(i->input_stat.st_mode))
138 return;
139
140 if (i->written_compressed >= (uint64_t) i->input_stat.st_size)
141 percent = 100;
142 else
143 percent = (unsigned) ((i->written_compressed * UINT64_C(100)) / (uint64_t) i->input_stat.st_size);
144
145 if (percent == i->last_percent)
146 return;
147
148 if (!ratelimit_below(&i->progress_ratelimit))
149 return;
150
151 sd_notifyf(false, "X_IMPORT_PROGRESS=%u", percent);
152 log_info("Imported %u%%.", percent);
153
154 i->last_percent = percent;
155 }
156
157 static int raw_import_maybe_convert_qcow2(RawImport *i) {
158 _cleanup_close_ int converted_fd = -1;
159 _cleanup_(unlink_and_freep) char *t = NULL;
160 _cleanup_free_ char *f = NULL;
161 int r;
162
163 assert(i);
164
165 /* Do QCOW2 conversion if enabled and not in direct mode */
166 if ((i->flags & (IMPORT_CONVERT_QCOW2|IMPORT_DIRECT)) != IMPORT_CONVERT_QCOW2)
167 return 0;
168
169 assert(i->final_path);
170
171 r = qcow2_detect(i->output_fd);
172 if (r < 0)
173 return log_error_errno(r, "Failed to detect whether this is a QCOW2 image: %m");
174 if (r == 0)
175 return 0;
176
177 /* This is a QCOW2 image, let's convert it */
178 r = tempfn_random(i->final_path, NULL, &f);
179 if (r < 0)
180 return log_oom();
181
182 converted_fd = open(f, O_RDWR|O_CREAT|O_EXCL|O_NOCTTY|O_CLOEXEC, 0664);
183 if (converted_fd < 0)
184 return log_error_errno(errno, "Failed to create %s: %m", f);
185
186 t = TAKE_PTR(f);
187
188 (void) import_set_nocow_and_log(converted_fd, t);
189
190 log_info("Unpacking QCOW2 file.");
191
192 r = qcow2_convert(i->output_fd, converted_fd);
193 if (r < 0)
194 return log_error_errno(r, "Failed to convert qcow2 image: %m");
195
196 unlink_and_free(i->temp_path);
197 i->temp_path = TAKE_PTR(t);
198 CLOSE_AND_REPLACE(i->output_fd, converted_fd);
199
200 return 1;
201 }
202
203 static int raw_import_finish(RawImport *i) {
204 int r;
205
206 assert(i);
207 assert(i->output_fd >= 0);
208
209 /* Nothing of what is below applies to block devices */
210 if (S_ISBLK(i->output_stat.st_mode)) {
211
212 if (i->flags & IMPORT_SYNC) {
213 if (fsync(i->output_fd) < 0)
214 return log_error_errno(errno, "Failed to synchronize block device: %m");
215 }
216
217 return 0;
218 }
219
220 assert(S_ISREG(i->output_stat.st_mode));
221
222 /* If an offset is specified we only are supposed to affect part of an existing output file or block
223 * device, thus don't manipulate file properties in that case */
224
225 if (i->offset == UINT64_MAX) {
226 /* In case this was a sparse file, make sure the file size is right */
227 if (i->written_uncompressed > 0) {
228 if (ftruncate(i->output_fd, i->written_uncompressed) < 0)
229 return log_error_errno(errno, "Failed to truncate file: %m");
230 }
231
232 r = raw_import_maybe_convert_qcow2(i);
233 if (r < 0)
234 return r;
235
236 if (S_ISREG(i->input_stat.st_mode)) {
237 (void) copy_times(i->input_fd, i->output_fd, COPY_CRTIME);
238 (void) copy_xattr(i->input_fd, i->output_fd, 0);
239 }
240 }
241
242 r = install_file(AT_FDCWD, i->temp_path ?: i->local,
243 AT_FDCWD, i->final_path,
244 (i->flags & IMPORT_FORCE ? INSTALL_REPLACE : 0) |
245 (i->flags & IMPORT_READ_ONLY ? INSTALL_READ_ONLY : 0) |
246 (i->flags & IMPORT_SYNC ? INSTALL_FSYNC_FULL : 0));
247 if (r < 0)
248 return log_error_errno(r, "Failed to move image into place: %m");
249
250 i->temp_path = mfree(i->temp_path);
251
252 log_info("Wrote %s.", FORMAT_BYTES(i->written_uncompressed));
253
254 return 0;
255 }
256
257 static int raw_import_open_disk(RawImport *i) {
258 int r;
259
260 assert(i);
261 assert(i->local);
262 assert(!i->final_path);
263 assert(!i->temp_path);
264 assert(i->output_fd < 0);
265
266 if (i->flags & IMPORT_DIRECT) {
267 (void) mkdir_parents_label(i->local, 0700);
268
269 /* In direct mode we just open/create the local path and truncate it (like shell >
270 * redirection would do it) — except if an offset was passed, in which case we are supposed
271 * to operate on a section of the file only, in which case we apparently work on an some
272 * existing thing (i.e. are not the sole thing stored in the file), in which case we will
273 * neither truncate nor create. */
274
275 i->output_fd = open(i->local, O_RDWR|O_NOCTTY|O_CLOEXEC|(i->offset == UINT64_MAX ? O_TRUNC|O_CREAT : 0), 0664);
276 if (i->output_fd < 0)
277 return log_error_errno(errno, "Failed to open destination '%s': %m", i->local);
278
279 if (i->offset == UINT64_MAX)
280 (void) import_set_nocow_and_log(i->output_fd, i->local);
281 } else {
282 i->final_path = strjoin(i->image_root, "/", i->local, ".raw");
283 if (!i->final_path)
284 return log_oom();
285
286 r = tempfn_random(i->final_path, NULL, &i->temp_path);
287 if (r < 0)
288 return log_oom();
289
290 (void) mkdir_parents_label(i->temp_path, 0700);
291
292 i->output_fd = open(i->temp_path, O_RDWR|O_CREAT|O_EXCL|O_NOCTTY|O_CLOEXEC, 0664);
293 if (i->output_fd < 0)
294 return log_error_errno(errno, "Failed to open destination '%s': %m", i->temp_path);
295
296 (void) import_set_nocow_and_log(i->output_fd, i->temp_path);
297 }
298
299 if (fstat(i->output_fd, &i->output_stat) < 0)
300 return log_error_errno(errno, "Failed to stat() output file: %m");
301
302 if (!S_ISREG(i->output_stat.st_mode) && !S_ISBLK(i->output_stat.st_mode))
303 return log_error_errno(SYNTHETIC_ERRNO(EBADFD),
304 "Target file is not a regular file or block device");
305
306 if (i->offset != UINT64_MAX) {
307 if (lseek(i->output_fd, i->offset, SEEK_SET) == (off_t) -1)
308 return log_error_errno(errno, "Failed to seek to offset: %m");
309 }
310
311 return 0;
312 }
313
314 static int raw_import_try_reflink(RawImport *i) {
315 off_t p;
316 int r;
317
318 assert(i);
319 assert(i->input_fd >= 0);
320 assert(i->output_fd >= 0);
321
322 if (i->compress.type != IMPORT_COMPRESS_UNCOMPRESSED)
323 return 0;
324
325 if (i->offset != UINT64_MAX || i->size_max != UINT64_MAX)
326 return 0;
327
328 if (!S_ISREG(i->input_stat.st_mode) || !S_ISREG(i->output_stat.st_mode))
329 return 0;
330
331 p = lseek(i->input_fd, 0, SEEK_CUR);
332 if (p == (off_t) -1)
333 return log_error_errno(errno, "Failed to read file offset of input file: %m");
334
335 /* Let's only try a btrfs reflink, if we are reading from the beginning of the file */
336 if ((uint64_t) p != (uint64_t) i->buffer_size)
337 return 0;
338
339 r = btrfs_reflink(i->input_fd, i->output_fd);
340 if (r >= 0)
341 return 1;
342
343 log_debug_errno(r, "Couldn't establish reflink, using copy: %m");
344 return 0;
345 }
346
347 static int raw_import_write(const void *p, size_t sz, void *userdata) {
348 RawImport *i = ASSERT_PTR(userdata);
349 bool too_much = false;
350 int r;
351
352 assert(p);
353 assert(sz > 0);
354
355 if (i->written_uncompressed >= UINT64_MAX - sz)
356 return log_error_errno(SYNTHETIC_ERRNO(EOVERFLOW), "File too large, overflow");
357
358 if (i->size_max != UINT64_MAX) {
359 if (i->written_uncompressed >= i->size_max) {
360 too_much = true;
361 goto finish;
362 }
363
364 if (i->written_uncompressed + sz > i->size_max) {
365 too_much = true;
366 sz = i->size_max - i->written_uncompressed; /* since we have the data in memory
367 * already, we might as well write it to
368 * disk to the max */
369 }
370 }
371
372 /* Generate sparse file if we created/truncated the file */
373 if (S_ISREG(i->output_stat.st_mode) && i->offset == UINT64_MAX) {
374 ssize_t n;
375
376 n = sparse_write(i->output_fd, p, sz, 64);
377 if (n < 0)
378 return log_error_errno((int) n, "Failed to write file: %m");
379 if ((size_t) n < sz)
380 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Short write");
381 } else {
382 r = loop_write(i->output_fd, p, sz, false);
383 if (r < 0)
384 return log_error_errno(r, "Failed to write file: %m");
385 }
386
387 i->written_uncompressed += sz;
388
389 finish:
390 if (too_much)
391 return log_error_errno(SYNTHETIC_ERRNO(E2BIG), "File too large");
392
393 return 0;
394 }
395
396 static int raw_import_process(RawImport *i) {
397 ssize_t l;
398 int r;
399
400 assert(i);
401 assert(i->buffer_size < sizeof(i->buffer));
402
403 l = read(i->input_fd, i->buffer + i->buffer_size, sizeof(i->buffer) - i->buffer_size);
404 if (l < 0) {
405 if (errno == EAGAIN)
406 return 0;
407
408 r = log_error_errno(errno, "Failed to read input file: %m");
409 goto finish;
410 }
411
412 i->buffer_size += l;
413
414 if (i->compress.type == IMPORT_COMPRESS_UNKNOWN) {
415
416 if (l == 0) { /* EOF */
417 log_debug("File too short to be compressed, as no compression signature fits in, thus assuming uncompressed.");
418 import_uncompress_force_off(&i->compress);
419 } else {
420 r = import_uncompress_detect(&i->compress, i->buffer, i->buffer_size);
421 if (r < 0) {
422 log_error_errno(r, "Failed to detect file compression: %m");
423 goto finish;
424 }
425 if (r == 0) /* Need more data */
426 return 0;
427 }
428
429 r = raw_import_open_disk(i);
430 if (r < 0)
431 goto finish;
432
433 r = raw_import_try_reflink(i);
434 if (r < 0)
435 goto finish;
436 if (r > 0)
437 goto complete;
438 }
439
440 r = import_uncompress(&i->compress, i->buffer, i->buffer_size, raw_import_write, i);
441 if (r < 0) {
442 log_error_errno(r, "Failed to decode and write: %m");
443 goto finish;
444 }
445
446 i->written_compressed += i->buffer_size;
447 i->buffer_size = 0;
448
449 if (l == 0) /* EOF */
450 goto complete;
451
452 raw_import_report_progress(i);
453
454 return 0;
455
456 complete:
457 r = raw_import_finish(i);
458
459 finish:
460 if (i->on_finished)
461 i->on_finished(i, r, i->userdata);
462 else
463 sd_event_exit(i->event, r);
464
465 return 0;
466 }
467
468 static int raw_import_on_input(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
469 RawImport *i = userdata;
470
471 return raw_import_process(i);
472 }
473
474 static int raw_import_on_defer(sd_event_source *s, void *userdata) {
475 RawImport *i = userdata;
476
477 return raw_import_process(i);
478 }
479
480 int raw_import_start(
481 RawImport *i,
482 int fd,
483 const char *local,
484 uint64_t offset,
485 uint64_t size_max,
486 ImportFlags flags) {
487 int r;
488
489 assert(i);
490 assert(fd >= 0);
491 assert(local);
492 assert(!(flags & ~IMPORT_FLAGS_MASK_RAW));
493 assert(offset == UINT64_MAX || FLAGS_SET(flags, IMPORT_DIRECT));
494
495 if (!import_validate_local(local, flags))
496 return -EINVAL;
497
498 if (i->input_fd >= 0)
499 return -EBUSY;
500
501 r = fd_nonblock(fd, true);
502 if (r < 0)
503 return r;
504
505 r = free_and_strdup(&i->local, local);
506 if (r < 0)
507 return r;
508
509 i->flags = flags;
510 i->offset = offset;
511 i->size_max = size_max;
512
513 if (fstat(fd, &i->input_stat) < 0)
514 return -errno;
515
516 r = sd_event_add_io(i->event, &i->input_event_source, fd, EPOLLIN, raw_import_on_input, i);
517 if (r == -EPERM) {
518 /* This fd does not support epoll, for example because it is a regular file. Busy read in that case */
519 r = sd_event_add_defer(i->event, &i->input_event_source, raw_import_on_defer, i);
520 if (r < 0)
521 return r;
522
523 r = sd_event_source_set_enabled(i->input_event_source, SD_EVENT_ON);
524 }
525 if (r < 0)
526 return r;
527
528 i->input_fd = fd;
529 return 0;
530 }