]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/import/import-raw.c
importd: add support for downloading sysext/confext/portable images too
[thirdparty/systemd.git] / src / import / import-raw.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
b6e676ce
LP
2
3#include <linux/fs.h>
4
5#include "sd-daemon.h"
6#include "sd-event.h"
07630cea 7
b5efdb8a 8#include "alloc-util.h"
b6e676ce
LP
9#include "btrfs-util.h"
10#include "copy.h"
3ffd4af2 11#include "fd-util.h"
53e03c96 12#include "format-util.h"
f4f15635 13#include "fs-util.h"
07630cea
LP
14#include "hostname-util.h"
15#include "import-common.h"
16#include "import-compress.h"
3ffd4af2 17#include "import-raw.h"
d32a5841 18#include "install-file.h"
c004493c 19#include "io-util.h"
b6e676ce 20#include "machine-pool.h"
35cd0ba5 21#include "mkdir-label.h"
07630cea 22#include "path-util.h"
b6e676ce 23#include "qcow2-util.h"
07630cea
LP
24#include "ratelimit.h"
25#include "rm-rf.h"
26#include "string-util.h"
e4de7287 27#include "tmpfile-util.h"
b6e676ce
LP
28
29struct RawImport {
30 sd_event *event;
31
32 char *image_root;
33
34 RawImportFinished on_finished;
35 void *userdata;
36
37 char *local;
5183c50a 38 ImportFlags flags;
b6e676ce
LP
39
40 char *temp_path;
41 char *final_path;
42
43 int input_fd;
44 int output_fd;
45
46 ImportCompress compress;
47
b6e676ce
LP
48 sd_event_source *input_event_source;
49
50 uint8_t buffer[16*1024];
51 size_t buffer_size;
52
53 uint64_t written_compressed;
54 uint64_t written_uncompressed;
55
d32a5841
LP
56 struct stat input_stat;
57 struct stat output_stat;
b6e676ce
LP
58
59 unsigned last_percent;
5ac1530e 60 RateLimit progress_ratelimit;
d32a5841
LP
61
62 uint64_t offset;
63 uint64_t size_max;
b6e676ce
LP
64};
65
66RawImport* raw_import_unref(RawImport *i) {
67 if (!i)
68 return NULL;
69
d32a5841 70 sd_event_source_unref(i->input_event_source);
b6e676ce 71
0dfb6503 72 unlink_and_free(i->temp_path);
b6e676ce
LP
73
74 import_compress_free(&i->compress);
75
d32a5841 76 sd_event_unref(i->event);
b6e676ce
LP
77
78 safe_close(i->output_fd);
79
80 free(i->final_path);
81 free(i->image_root);
82 free(i->local);
6b430fdb 83 return mfree(i);
b6e676ce
LP
84}
85
86int raw_import_new(
87 RawImport **ret,
88 sd_event *event,
89 const char *image_root,
90 RawImportFinished on_finished,
91 void *userdata) {
92
93 _cleanup_(raw_import_unrefp) RawImport *i = NULL;
0d94088e 94 _cleanup_free_ char *root = NULL;
b6e676ce
LP
95 int r;
96
97 assert(ret);
7af5785d 98 assert(image_root);
b6e676ce 99
7af5785d 100 root = strdup(image_root);
0d94088e 101 if (!root)
b6e676ce
LP
102 return -ENOMEM;
103
0d94088e
YW
104 i = new(RawImport, 1);
105 if (!i)
b6e676ce
LP
106 return -ENOMEM;
107
0d94088e 108 *i = (RawImport) {
254d1313
ZJS
109 .input_fd = -EBADF,
110 .output_fd = -EBADF,
0d94088e
YW
111 .on_finished = on_finished,
112 .userdata = userdata,
f5fbe71d 113 .last_percent = UINT_MAX,
0d94088e 114 .image_root = TAKE_PTR(root),
5ac1530e 115 .progress_ratelimit = { 100 * USEC_PER_MSEC, 1 },
d32a5841
LP
116 .offset = UINT64_MAX,
117 .size_max = UINT64_MAX,
0d94088e
YW
118 };
119
b6e676ce
LP
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
1cc6c93a 128 *ret = TAKE_PTR(i);
b6e676ce
LP
129 return 0;
130}
131
132static 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 */
d32a5841 137 if (!S_ISREG(i->input_stat.st_mode))
b6e676ce
LP
138 return;
139
d32a5841 140 if (i->written_compressed >= (uint64_t) i->input_stat.st_size)
b6e676ce
LP
141 percent = 100;
142 else
d32a5841 143 percent = (unsigned) ((i->written_compressed * UINT64_C(100)) / (uint64_t) i->input_stat.st_size);
b6e676ce
LP
144
145 if (percent == i->last_percent)
146 return;
147
5ac1530e 148 if (!ratelimit_below(&i->progress_ratelimit))
b6e676ce
LP
149 return;
150
a986de68 151 sd_notifyf(false, "X_IMPORT_PROGRESS=%u%%", percent);
b6e676ce
LP
152 log_info("Imported %u%%.", percent);
153
154 i->last_percent = percent;
155}
156
157static int raw_import_maybe_convert_qcow2(RawImport *i) {
254d1313 158 _cleanup_close_ int converted_fd = -EBADF;
d32a5841
LP
159 _cleanup_(unlink_and_freep) char *t = NULL;
160 _cleanup_free_ char *f = NULL;
b6e676ce
LP
161 int r;
162
163 assert(i);
164
d32a5841
LP
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
b6e676ce
LP
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 */
d32a5841 178 r = tempfn_random(i->final_path, NULL, &f);
b6e676ce
LP
179 if (r < 0)
180 return log_oom();
181
d32a5841 182 converted_fd = open(f, O_RDWR|O_CREAT|O_EXCL|O_NOCTTY|O_CLOEXEC, 0664);
b6e676ce 183 if (converted_fd < 0)
d32a5841
LP
184 return log_error_errno(errno, "Failed to create %s: %m", f);
185
186 t = TAKE_PTR(f);
b6e676ce 187
137c6c6b 188 (void) import_set_nocow_and_log(converted_fd, t);
b6e676ce
LP
189
190 log_info("Unpacking QCOW2 file.");
191
192 r = qcow2_convert(i->output_fd, converted_fd);
d32a5841 193 if (r < 0)
b6e676ce 194 return log_error_errno(r, "Failed to convert qcow2 image: %m");
b6e676ce 195
d32a5841
LP
196 unlink_and_free(i->temp_path);
197 i->temp_path = TAKE_PTR(t);
ee3455cf 198 close_and_replace(i->output_fd, converted_fd);
b6e676ce
LP
199
200 return 1;
201}
202
203static int raw_import_finish(RawImport *i) {
204 int r;
205
206 assert(i);
207 assert(i->output_fd >= 0);
b6e676ce 208
d32a5841
LP
209 /* Nothing of what is below applies to block devices */
210 if (S_ISBLK(i->output_stat.st_mode)) {
b6e676ce 211
d32a5841
LP
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 }
b6e676ce 216
d32a5841 217 return 0;
b6e676ce
LP
218 }
219
d32a5841
LP
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);
b6e676ce
LP
233 if (r < 0)
234 return r;
b6e676ce 235
d32a5841
LP
236 if (S_ISREG(i->input_stat.st_mode)) {
237 (void) copy_times(i->input_fd, i->output_fd, COPY_CRTIME);
c17cfe6e 238 (void) copy_xattr(i->input_fd, NULL, i->output_fd, NULL, 0);
d32a5841
LP
239 }
240 }
b6e676ce 241
d32a5841
LP
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));
f85ef957
AC
247 if (r < 0)
248 return log_error_errno(r, "Failed to move image into place: %m");
b6e676ce 249
a1e58e8e 250 i->temp_path = mfree(i->temp_path);
b6e676ce 251
53e03c96
LP
252 log_info("Wrote %s.", FORMAT_BYTES(i->written_uncompressed));
253
b6e676ce
LP
254 return 0;
255}
256
257static int raw_import_open_disk(RawImport *i) {
258 int r;
259
260 assert(i);
d32a5841 261 assert(i->local);
b6e676ce
LP
262 assert(!i->final_path);
263 assert(!i->temp_path);
264 assert(i->output_fd < 0);
265
d32a5841
LP
266 if (i->flags & IMPORT_DIRECT) {
267 (void) mkdir_parents_label(i->local, 0700);
b6e676ce 268
d32a5841
LP
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();
b6e676ce 289
d32a5841 290 (void) mkdir_parents_label(i->temp_path, 0700);
b6e676ce 291
d32a5841
LP
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) {
86cbbc6d 307 if (lseek(i->output_fd, i->offset, SEEK_SET) < 0)
d32a5841
LP
308 return log_error_errno(errno, "Failed to seek to offset: %m");
309 }
b6e676ce 310
b6e676ce
LP
311 return 0;
312}
313
314static 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
50b9c30c
LP
325 if (i->offset != UINT64_MAX || i->size_max != UINT64_MAX)
326 return 0;
327
d32a5841 328 if (!S_ISREG(i->input_stat.st_mode) || !S_ISREG(i->output_stat.st_mode))
b6e676ce
LP
329 return 0;
330
331 p = lseek(i->input_fd, 0, SEEK_CUR);
86cbbc6d 332 if (p < 0)
b6e676ce
LP
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
b640e274 339 r = reflink(i->input_fd, i->output_fd);
b6e676ce
LP
340 if (r >= 0)
341 return 1;
342
d32a5841 343 log_debug_errno(r, "Couldn't establish reflink, using copy: %m");
b6e676ce
LP
344 return 0;
345}
346
347static int raw_import_write(const void *p, size_t sz, void *userdata) {
99534007 348 RawImport *i = ASSERT_PTR(userdata);
d32a5841
LP
349 bool too_much = false;
350 int r;
b6e676ce 351
d32a5841
LP
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 */
bf284aee 373 if (S_ISREG(i->output_stat.st_mode) && i->offset == UINT64_MAX) {
d32a5841
LP
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 {
e22c60a9 382 r = loop_write(i->output_fd, p, sz);
d32a5841
LP
383 if (r < 0)
384 return log_error_errno(r, "Failed to write file: %m");
385 }
b6e676ce
LP
386
387 i->written_uncompressed += sz;
b6e676ce 388
d32a5841
LP
389finish:
390 if (too_much)
391 return log_error_errno(SYNTHETIC_ERRNO(E2BIG), "File too large");
392
b6e676ce
LP
393 return 0;
394}
395
396static 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) {
587fec42
LP
405 if (errno == EAGAIN)
406 return 0;
407
b6e676ce
LP
408 r = log_error_errno(errno, "Failed to read input file: %m");
409 goto finish;
410 }
b6e676ce
LP
411
412 i->buffer_size += l;
413
414 if (i->compress.type == IMPORT_COMPRESS_UNKNOWN) {
c9b6ebef
LP
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;
b6e676ce 427 }
b6e676ce
LP
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;
c9b6ebef
LP
436 if (r > 0)
437 goto complete;
b6e676ce
LP
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
c9b6ebef
LP
449 if (l == 0) /* EOF */
450 goto complete;
451
b6e676ce
LP
452 raw_import_report_progress(i);
453
454 return 0;
455
c9b6ebef
LP
456complete:
457 r = raw_import_finish(i);
458
b6e676ce
LP
459finish:
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
468static 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
474static int raw_import_on_defer(sd_event_source *s, void *userdata) {
475 RawImport *i = userdata;
476
477 return raw_import_process(i);
478}
479
d32a5841
LP
480int 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) {
b6e676ce
LP
487 int r;
488
489 assert(i);
490 assert(fd >= 0);
491 assert(local);
d32a5841
LP
492 assert(!(flags & ~IMPORT_FLAGS_MASK_RAW));
493 assert(offset == UINT64_MAX || FLAGS_SET(flags, IMPORT_DIRECT));
b6e676ce 494
d32a5841 495 if (!import_validate_local(local, flags))
b6e676ce
LP
496 return -EINVAL;
497
498 if (i->input_fd >= 0)
499 return -EBUSY;
500
587fec42
LP
501 r = fd_nonblock(fd, true);
502 if (r < 0)
503 return r;
504
b6e676ce
LP
505 r = free_and_strdup(&i->local, local);
506 if (r < 0)
507 return r;
5183c50a
LP
508
509 i->flags = flags;
d32a5841
LP
510 i->offset = offset;
511 i->size_max = size_max;
b6e676ce 512
d32a5841 513 if (fstat(fd, &i->input_stat) < 0)
b6e676ce
LP
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;
d32a5841 529 return 0;
b6e676ce 530}