]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/import/import-raw.c
a0208e505cfcdbef6bf09127d2973d4be4c5edb5
[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 = userdata;
349 bool too_much = false;
350 int r;
351
352 assert(i);
353 assert(p);
354 assert(sz > 0);
355
356 if (i->written_uncompressed >= UINT64_MAX - sz)
357 return log_error_errno(SYNTHETIC_ERRNO(EOVERFLOW), "File too large, overflow");
358
359 if (i->size_max != UINT64_MAX) {
360 if (i->written_uncompressed >= i->size_max) {
361 too_much = true;
362 goto finish;
363 }
364
365 if (i->written_uncompressed + sz > i->size_max) {
366 too_much = true;
367 sz = i->size_max - i->written_uncompressed; /* since we have the data in memory
368 * already, we might as well write it to
369 * disk to the max */
370 }
371 }
372
373 /* Generate sparse file if we created/truncated the file */
374 if (S_ISREG(i->output_stat.st_mode) && i->offset == UINT64_MAX) {
375 ssize_t n;
376
377 n = sparse_write(i->output_fd, p, sz, 64);
378 if (n < 0)
379 return log_error_errno((int) n, "Failed to write file: %m");
380 if ((size_t) n < sz)
381 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Short write");
382 } else {
383 r = loop_write(i->output_fd, p, sz, false);
384 if (r < 0)
385 return log_error_errno(r, "Failed to write file: %m");
386 }
387
388 i->written_uncompressed += sz;
389
390 finish:
391 if (too_much)
392 return log_error_errno(SYNTHETIC_ERRNO(E2BIG), "File too large");
393
394 return 0;
395 }
396
397 static int raw_import_process(RawImport *i) {
398 ssize_t l;
399 int r;
400
401 assert(i);
402 assert(i->buffer_size < sizeof(i->buffer));
403
404 l = read(i->input_fd, i->buffer + i->buffer_size, sizeof(i->buffer) - i->buffer_size);
405 if (l < 0) {
406 if (errno == EAGAIN)
407 return 0;
408
409 r = log_error_errno(errno, "Failed to read input file: %m");
410 goto finish;
411 }
412
413 i->buffer_size += l;
414
415 if (i->compress.type == IMPORT_COMPRESS_UNKNOWN) {
416
417 if (l == 0) { /* EOF */
418 log_debug("File too short to be compressed, as no compression signature fits in, thus assuming uncompressed.");
419 import_uncompress_force_off(&i->compress);
420 } else {
421 r = import_uncompress_detect(&i->compress, i->buffer, i->buffer_size);
422 if (r < 0) {
423 log_error_errno(r, "Failed to detect file compression: %m");
424 goto finish;
425 }
426 if (r == 0) /* Need more data */
427 return 0;
428 }
429
430 r = raw_import_open_disk(i);
431 if (r < 0)
432 goto finish;
433
434 r = raw_import_try_reflink(i);
435 if (r < 0)
436 goto finish;
437 if (r > 0)
438 goto complete;
439 }
440
441 r = import_uncompress(&i->compress, i->buffer, i->buffer_size, raw_import_write, i);
442 if (r < 0) {
443 log_error_errno(r, "Failed to decode and write: %m");
444 goto finish;
445 }
446
447 i->written_compressed += i->buffer_size;
448 i->buffer_size = 0;
449
450 if (l == 0) /* EOF */
451 goto complete;
452
453 raw_import_report_progress(i);
454
455 return 0;
456
457 complete:
458 r = raw_import_finish(i);
459
460 finish:
461 if (i->on_finished)
462 i->on_finished(i, r, i->userdata);
463 else
464 sd_event_exit(i->event, r);
465
466 return 0;
467 }
468
469 static int raw_import_on_input(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
470 RawImport *i = userdata;
471
472 return raw_import_process(i);
473 }
474
475 static int raw_import_on_defer(sd_event_source *s, void *userdata) {
476 RawImport *i = userdata;
477
478 return raw_import_process(i);
479 }
480
481 int raw_import_start(
482 RawImport *i,
483 int fd,
484 const char *local,
485 uint64_t offset,
486 uint64_t size_max,
487 ImportFlags flags) {
488 int r;
489
490 assert(i);
491 assert(fd >= 0);
492 assert(local);
493 assert(!(flags & ~IMPORT_FLAGS_MASK_RAW));
494 assert(offset == UINT64_MAX || FLAGS_SET(flags, IMPORT_DIRECT));
495
496 if (!import_validate_local(local, flags))
497 return -EINVAL;
498
499 if (i->input_fd >= 0)
500 return -EBUSY;
501
502 r = fd_nonblock(fd, true);
503 if (r < 0)
504 return r;
505
506 r = free_and_strdup(&i->local, local);
507 if (r < 0)
508 return r;
509
510 i->flags = flags;
511 i->offset = offset;
512 i->size_max = size_max;
513
514 if (fstat(fd, &i->input_stat) < 0)
515 return -errno;
516
517 r = sd_event_add_io(i->event, &i->input_event_source, fd, EPOLLIN, raw_import_on_input, i);
518 if (r == -EPERM) {
519 /* This fd does not support epoll, for example because it is a regular file. Busy read in that case */
520 r = sd_event_add_defer(i->event, &i->input_event_source, raw_import_on_defer, i);
521 if (r < 0)
522 return r;
523
524 r = sd_event_source_set_enabled(i->input_event_source, SD_EVENT_ON);
525 }
526 if (r < 0)
527 return r;
528
529 i->input_fd = fd;
530 return 0;
531 }