]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/import/pull-raw.c
curl-util: fix error code check from curl_multi_socket_action
[thirdparty/systemd.git] / src / import / pull-raw.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <curl/curl.h>
4 #include <linux/fs.h>
5 #include <sys/xattr.h>
6
7 #include "sd-daemon.h"
8
9 #include "alloc-util.h"
10 #include "btrfs-util.h"
11 #include "chattr-util.h"
12 #include "copy.h"
13 #include "curl-util.h"
14 #include "fd-util.h"
15 #include "fileio.h"
16 #include "fs-util.h"
17 #include "hostname-util.h"
18 #include "import-common.h"
19 #include "import-util.h"
20 #include "macro.h"
21 #include "mkdir.h"
22 #include "path-util.h"
23 #include "pull-common.h"
24 #include "pull-job.h"
25 #include "pull-raw.h"
26 #include "qcow2-util.h"
27 #include "rm-rf.h"
28 #include "string-util.h"
29 #include "strv.h"
30 #include "utf8.h"
31 #include "util.h"
32 #include "web-util.h"
33
34 typedef enum RawProgress {
35 RAW_DOWNLOADING,
36 RAW_VERIFYING,
37 RAW_UNPACKING,
38 RAW_FINALIZING,
39 RAW_COPYING,
40 } RawProgress;
41
42 struct RawPull {
43 sd_event *event;
44 CurlGlue *glue;
45
46 char *image_root;
47
48 PullJob *raw_job;
49 PullJob *roothash_job;
50 PullJob *settings_job;
51 PullJob *checksum_job;
52 PullJob *signature_job;
53
54 RawPullFinished on_finished;
55 void *userdata;
56
57 char *local;
58 bool force_local;
59 bool grow_machine_directory;
60 bool settings;
61 bool roothash;
62
63 char *final_path;
64 char *temp_path;
65
66 char *settings_path;
67 char *settings_temp_path;
68
69 char *roothash_path;
70 char *roothash_temp_path;
71
72 ImportVerify verify;
73 };
74
75 RawPull* raw_pull_unref(RawPull *i) {
76 if (!i)
77 return NULL;
78
79 pull_job_unref(i->raw_job);
80 pull_job_unref(i->settings_job);
81 pull_job_unref(i->roothash_job);
82 pull_job_unref(i->checksum_job);
83 pull_job_unref(i->signature_job);
84
85 curl_glue_unref(i->glue);
86 sd_event_unref(i->event);
87
88 if (i->temp_path) {
89 (void) unlink(i->temp_path);
90 free(i->temp_path);
91 }
92
93 if (i->roothash_temp_path) {
94 (void) unlink(i->roothash_temp_path);
95 free(i->roothash_temp_path);
96 }
97
98 if (i->settings_temp_path) {
99 (void) unlink(i->settings_temp_path);
100 free(i->settings_temp_path);
101 }
102
103 free(i->final_path);
104 free(i->roothash_path);
105 free(i->settings_path);
106 free(i->image_root);
107 free(i->local);
108 return mfree(i);
109 }
110
111 int raw_pull_new(
112 RawPull **ret,
113 sd_event *event,
114 const char *image_root,
115 RawPullFinished on_finished,
116 void *userdata) {
117
118 _cleanup_(raw_pull_unrefp) RawPull *i = NULL;
119 int r;
120
121 assert(ret);
122
123 i = new0(RawPull, 1);
124 if (!i)
125 return -ENOMEM;
126
127 i->on_finished = on_finished;
128 i->userdata = userdata;
129
130 i->image_root = strdup(image_root ?: "/var/lib/machines");
131 if (!i->image_root)
132 return -ENOMEM;
133
134 i->grow_machine_directory = path_startswith(i->image_root, "/var/lib/machines");
135
136 if (event)
137 i->event = sd_event_ref(event);
138 else {
139 r = sd_event_default(&i->event);
140 if (r < 0)
141 return r;
142 }
143
144 r = curl_glue_new(&i->glue, i->event);
145 if (r < 0)
146 return r;
147
148 i->glue->on_finished = pull_job_curl_on_finished;
149 i->glue->userdata = i;
150
151 *ret = TAKE_PTR(i);
152
153 return 0;
154 }
155
156 static void raw_pull_report_progress(RawPull *i, RawProgress p) {
157 unsigned percent;
158
159 assert(i);
160
161 switch (p) {
162
163 case RAW_DOWNLOADING: {
164 unsigned remain = 80;
165
166 percent = 0;
167
168 if (i->settings_job) {
169 percent += i->settings_job->progress_percent * 5 / 100;
170 remain -= 5;
171 }
172
173 if (i->roothash_job) {
174 percent += i->roothash_job->progress_percent * 5 / 100;
175 remain -= 5;
176 }
177
178 if (i->checksum_job) {
179 percent += i->checksum_job->progress_percent * 5 / 100;
180 remain -= 5;
181 }
182
183 if (i->signature_job) {
184 percent += i->signature_job->progress_percent * 5 / 100;
185 remain -= 5;
186 }
187
188 if (i->raw_job)
189 percent += i->raw_job->progress_percent * remain / 100;
190 break;
191 }
192
193 case RAW_VERIFYING:
194 percent = 80;
195 break;
196
197 case RAW_UNPACKING:
198 percent = 85;
199 break;
200
201 case RAW_FINALIZING:
202 percent = 90;
203 break;
204
205 case RAW_COPYING:
206 percent = 95;
207 break;
208
209 default:
210 assert_not_reached("Unknown progress state");
211 }
212
213 sd_notifyf(false, "X_IMPORT_PROGRESS=%u", percent);
214 log_debug("Combined progress %u%%", percent);
215 }
216
217 static int raw_pull_maybe_convert_qcow2(RawPull *i) {
218 _cleanup_close_ int converted_fd = -1;
219 _cleanup_free_ char *t = NULL;
220 int r;
221
222 assert(i);
223 assert(i->raw_job);
224
225 r = qcow2_detect(i->raw_job->disk_fd);
226 if (r < 0)
227 return log_error_errno(r, "Failed to detect whether this is a QCOW2 image: %m");
228 if (r == 0)
229 return 0;
230
231 /* This is a QCOW2 image, let's convert it */
232 r = tempfn_random(i->final_path, NULL, &t);
233 if (r < 0)
234 return log_oom();
235
236 converted_fd = open(t, O_RDWR|O_CREAT|O_EXCL|O_NOCTTY|O_CLOEXEC, 0664);
237 if (converted_fd < 0)
238 return log_error_errno(errno, "Failed to create %s: %m", t);
239
240 r = chattr_fd(converted_fd, FS_NOCOW_FL, FS_NOCOW_FL, NULL);
241 if (r < 0)
242 log_warning_errno(r, "Failed to set file attributes on %s: %m", t);
243
244 log_info("Unpacking QCOW2 file.");
245
246 r = qcow2_convert(i->raw_job->disk_fd, converted_fd);
247 if (r < 0) {
248 unlink(t);
249 return log_error_errno(r, "Failed to convert qcow2 image: %m");
250 }
251
252 (void) unlink(i->temp_path);
253 free_and_replace(i->temp_path, t);
254
255 safe_close(i->raw_job->disk_fd);
256 i->raw_job->disk_fd = TAKE_FD(converted_fd);
257
258 return 1;
259 }
260
261 static int raw_pull_determine_path(RawPull *i, const char *suffix, char **field) {
262 int r;
263
264 assert(i);
265 assert(field);
266
267 if (*field)
268 return 0;
269
270 assert(i->raw_job);
271
272 r = pull_make_path(i->raw_job->url, i->raw_job->etag, i->image_root, ".raw-", suffix, field);
273 if (r < 0)
274 return log_oom();
275
276 return 1;
277 }
278
279 static int raw_pull_copy_auxiliary_file(
280 RawPull *i,
281 const char *suffix,
282 char **path) {
283
284 const char *local;
285 int r;
286
287 assert(i);
288 assert(suffix);
289 assert(path);
290
291 r = raw_pull_determine_path(i, suffix, path);
292 if (r < 0)
293 return r;
294
295 local = strjoina(i->image_root, "/", i->local, suffix);
296
297 r = copy_file_atomic(*path, local, 0644, 0, COPY_REFLINK | (i->force_local ? COPY_REPLACE : 0));
298 if (r == -EEXIST)
299 log_warning_errno(r, "File %s already exists, not replacing.", local);
300 else if (r == -ENOENT)
301 log_debug_errno(r, "Skipping creation of auxiliary file, since none was found.");
302 else if (r < 0)
303 log_warning_errno(r, "Failed to copy file %s, ignoring: %m", local);
304 else
305 log_info("Created new file %s.", local);
306
307 return 0;
308 }
309
310 static int raw_pull_make_local_copy(RawPull *i) {
311 _cleanup_free_ char *tp = NULL;
312 _cleanup_close_ int dfd = -1;
313 const char *p;
314 int r;
315
316 assert(i);
317 assert(i->raw_job);
318
319 if (!i->local)
320 return 0;
321
322 if (i->raw_job->etag_exists) {
323 /* We have downloaded this one previously, reopen it */
324
325 assert(i->raw_job->disk_fd < 0);
326
327 i->raw_job->disk_fd = open(i->final_path, O_RDONLY|O_NOCTTY|O_CLOEXEC);
328 if (i->raw_job->disk_fd < 0)
329 return log_error_errno(errno, "Failed to open vendor image: %m");
330 } else {
331 /* We freshly downloaded the image, use it */
332
333 assert(i->raw_job->disk_fd >= 0);
334
335 if (lseek(i->raw_job->disk_fd, SEEK_SET, 0) == (off_t) -1)
336 return log_error_errno(errno, "Failed to seek to beginning of vendor image: %m");
337 }
338
339 p = strjoina(i->image_root, "/", i->local, ".raw");
340
341 if (i->force_local)
342 (void) rm_rf(p, REMOVE_ROOT|REMOVE_PHYSICAL|REMOVE_SUBVOLUME);
343
344 r = tempfn_random(p, NULL, &tp);
345 if (r < 0)
346 return log_oom();
347
348 dfd = open(tp, O_WRONLY|O_CREAT|O_EXCL|O_NOCTTY|O_CLOEXEC, 0664);
349 if (dfd < 0)
350 return log_error_errno(errno, "Failed to create writable copy of image: %m");
351
352 /* Turn off COW writing. This should greatly improve
353 * performance on COW file systems like btrfs, since it
354 * reduces fragmentation caused by not allowing in-place
355 * writes. */
356 r = chattr_fd(dfd, FS_NOCOW_FL, FS_NOCOW_FL, NULL);
357 if (r < 0)
358 log_warning_errno(r, "Failed to set file attributes on %s: %m", tp);
359
360 r = copy_bytes(i->raw_job->disk_fd, dfd, (uint64_t) -1, COPY_REFLINK);
361 if (r < 0) {
362 unlink(tp);
363 return log_error_errno(r, "Failed to make writable copy of image: %m");
364 }
365
366 (void) copy_times(i->raw_job->disk_fd, dfd);
367 (void) copy_xattr(i->raw_job->disk_fd, dfd);
368
369 dfd = safe_close(dfd);
370
371 r = rename(tp, p);
372 if (r < 0) {
373 r = log_error_errno(errno, "Failed to move writable image into place: %m");
374 unlink(tp);
375 return r;
376 }
377
378 log_info("Created new local image '%s'.", i->local);
379
380 if (i->roothash) {
381 r = raw_pull_copy_auxiliary_file(i, ".roothash", &i->roothash_path);
382 if (r < 0)
383 return r;
384 }
385
386 if (i->settings) {
387 r = raw_pull_copy_auxiliary_file(i, ".nspawn", &i->settings_path);
388 if (r < 0)
389 return r;
390 }
391
392 return 0;
393 }
394
395 static bool raw_pull_is_done(RawPull *i) {
396 assert(i);
397 assert(i->raw_job);
398
399 if (!PULL_JOB_IS_COMPLETE(i->raw_job))
400 return false;
401 if (i->roothash_job && !PULL_JOB_IS_COMPLETE(i->roothash_job))
402 return false;
403 if (i->settings_job && !PULL_JOB_IS_COMPLETE(i->settings_job))
404 return false;
405 if (i->checksum_job && !PULL_JOB_IS_COMPLETE(i->checksum_job))
406 return false;
407 if (i->signature_job && !PULL_JOB_IS_COMPLETE(i->signature_job))
408 return false;
409
410 return true;
411 }
412
413 static int raw_pull_rename_auxiliary_file(
414 RawPull *i,
415 const char *suffix,
416 char **temp_path,
417 char **path) {
418
419 int r;
420
421 assert(i);
422 assert(temp_path);
423 assert(suffix);
424 assert(path);
425
426 /* Regenerate final name for this auxiliary file, we might know the etag of the file now, and we should
427 * incorporate it in the file name if we can */
428 *path = mfree(*path);
429 r = raw_pull_determine_path(i, suffix, path);
430 if (r < 0)
431 return r;
432
433 r = import_make_read_only(*temp_path);
434 if (r < 0)
435 return r;
436
437 r = rename_noreplace(AT_FDCWD, *temp_path, AT_FDCWD, *path);
438 if (r < 0)
439 return log_error_errno(r, "Failed to rename file %s to %s: %m", *temp_path, *path);
440
441 *temp_path = mfree(*temp_path);
442
443 return 1;
444 }
445
446 static void raw_pull_job_on_finished(PullJob *j) {
447 RawPull *i;
448 int r;
449
450 assert(j);
451 assert(j->userdata);
452
453 i = j->userdata;
454 if (j == i->roothash_job) {
455 if (j->error != 0)
456 log_info_errno(j->error, "Root hash file could not be retrieved, proceeding without.");
457 } else if (j == i->settings_job) {
458 if (j->error != 0)
459 log_info_errno(j->error, "Settings file could not be retrieved, proceeding without.");
460 } else if (j->error != 0 && j != i->signature_job) {
461 if (j == i->checksum_job)
462 log_error_errno(j->error, "Failed to retrieve SHA256 checksum, cannot verify. (Try --verify=no?)");
463 else
464 log_error_errno(j->error, "Failed to retrieve image file. (Wrong URL?)");
465
466 r = j->error;
467 goto finish;
468 }
469
470 /* This is invoked if either the download completed
471 * successfully, or the download was skipped because we
472 * already have the etag. In this case ->etag_exists is
473 * true.
474 *
475 * We only do something when we got all three files */
476
477 if (!raw_pull_is_done(i))
478 return;
479
480 if (i->signature_job && i->checksum_job->style == VERIFICATION_PER_DIRECTORY && i->signature_job->error != 0) {
481 log_error_errno(j->error, "Failed to retrieve signature file, cannot verify. (Try --verify=no?)");
482
483 r = i->signature_job->error;
484 goto finish;
485 }
486
487 if (i->roothash_job)
488 i->roothash_job->disk_fd = safe_close(i->roothash_job->disk_fd);
489 if (i->settings_job)
490 i->settings_job->disk_fd = safe_close(i->settings_job->disk_fd);
491
492 r = raw_pull_determine_path(i, ".raw", &i->final_path);
493 if (r < 0)
494 goto finish;
495
496 if (!i->raw_job->etag_exists) {
497 /* This is a new download, verify it, and move it into place */
498 assert(i->raw_job->disk_fd >= 0);
499
500 raw_pull_report_progress(i, RAW_VERIFYING);
501
502 r = pull_verify(i->raw_job, i->roothash_job, i->settings_job, i->checksum_job, i->signature_job);
503 if (r < 0)
504 goto finish;
505
506 raw_pull_report_progress(i, RAW_UNPACKING);
507
508 r = raw_pull_maybe_convert_qcow2(i);
509 if (r < 0)
510 goto finish;
511
512 raw_pull_report_progress(i, RAW_FINALIZING);
513
514 r = import_make_read_only_fd(i->raw_job->disk_fd);
515 if (r < 0)
516 goto finish;
517
518 r = rename_noreplace(AT_FDCWD, i->temp_path, AT_FDCWD, i->final_path);
519 if (r < 0) {
520 log_error_errno(r, "Failed to rename raw file to %s: %m", i->final_path);
521 goto finish;
522 }
523
524 i->temp_path = mfree(i->temp_path);
525
526 if (i->roothash_job &&
527 i->roothash_job->error == 0) {
528 r = raw_pull_rename_auxiliary_file(i, ".roothash", &i->roothash_temp_path, &i->roothash_path);
529 if (r < 0)
530 goto finish;
531 }
532
533 if (i->settings_job &&
534 i->settings_job->error == 0) {
535 r = raw_pull_rename_auxiliary_file(i, ".nspawn", &i->settings_temp_path, &i->settings_path);
536 if (r < 0)
537 goto finish;
538 }
539 }
540
541 raw_pull_report_progress(i, RAW_COPYING);
542
543 r = raw_pull_make_local_copy(i);
544 if (r < 0)
545 goto finish;
546
547 r = 0;
548
549 finish:
550 if (i->on_finished)
551 i->on_finished(i, r, i->userdata);
552 else
553 sd_event_exit(i->event, r);
554 }
555
556 static int raw_pull_job_on_open_disk_generic(
557 RawPull *i,
558 PullJob *j,
559 const char *extra,
560 char **temp_path) {
561
562 int r;
563
564 assert(i);
565 assert(j);
566 assert(extra);
567 assert(temp_path);
568
569 if (!*temp_path) {
570 r = tempfn_random_child(i->image_root, extra, temp_path);
571 if (r < 0)
572 return log_oom();
573 }
574
575 (void) mkdir_parents_label(*temp_path, 0700);
576
577 j->disk_fd = open(*temp_path, O_RDWR|O_CREAT|O_EXCL|O_NOCTTY|O_CLOEXEC, 0664);
578 if (j->disk_fd < 0)
579 return log_error_errno(errno, "Failed to create %s: %m", *temp_path);
580
581 return 0;
582 }
583
584 static int raw_pull_job_on_open_disk_raw(PullJob *j) {
585 RawPull *i;
586 int r;
587
588 assert(j);
589 assert(j->userdata);
590
591 i = j->userdata;
592 assert(i->raw_job == j);
593
594 r = raw_pull_job_on_open_disk_generic(i, j, "raw", &i->temp_path);
595 if (r < 0)
596 return r;
597
598 r = chattr_fd(j->disk_fd, FS_NOCOW_FL, FS_NOCOW_FL, NULL);
599 if (r < 0)
600 log_warning_errno(r, "Failed to set file attributes on %s, ignoring: %m", i->temp_path);
601
602 return 0;
603 }
604
605 static int raw_pull_job_on_open_disk_roothash(PullJob *j) {
606 RawPull *i;
607
608 assert(j);
609 assert(j->userdata);
610
611 i = j->userdata;
612 assert(i->roothash_job == j);
613
614 return raw_pull_job_on_open_disk_generic(i, j, "roothash", &i->roothash_temp_path);
615 }
616
617 static int raw_pull_job_on_open_disk_settings(PullJob *j) {
618 RawPull *i;
619
620 assert(j);
621 assert(j->userdata);
622
623 i = j->userdata;
624 assert(i->settings_job == j);
625
626 return raw_pull_job_on_open_disk_generic(i, j, "settings", &i->settings_temp_path);
627 }
628
629 static void raw_pull_job_on_progress(PullJob *j) {
630 RawPull *i;
631
632 assert(j);
633 assert(j->userdata);
634
635 i = j->userdata;
636
637 raw_pull_report_progress(i, RAW_DOWNLOADING);
638 }
639
640 int raw_pull_start(
641 RawPull *i,
642 const char *url,
643 const char *local,
644 bool force_local,
645 ImportVerify verify,
646 bool settings,
647 bool roothash) {
648
649 int r;
650
651 assert(i);
652 assert(verify < _IMPORT_VERIFY_MAX);
653 assert(verify >= 0);
654
655 if (!http_url_is_valid(url))
656 return -EINVAL;
657
658 if (local && !machine_name_is_valid(local))
659 return -EINVAL;
660
661 if (i->raw_job)
662 return -EBUSY;
663
664 r = free_and_strdup(&i->local, local);
665 if (r < 0)
666 return r;
667
668 i->force_local = force_local;
669 i->verify = verify;
670 i->settings = settings;
671 i->roothash = roothash;
672
673 /* Queue job for the image itself */
674 r = pull_job_new(&i->raw_job, url, i->glue, i);
675 if (r < 0)
676 return r;
677
678 i->raw_job->on_finished = raw_pull_job_on_finished;
679 i->raw_job->on_open_disk = raw_pull_job_on_open_disk_raw;
680 i->raw_job->on_progress = raw_pull_job_on_progress;
681 i->raw_job->calc_checksum = verify != IMPORT_VERIFY_NO;
682 i->raw_job->grow_machine_directory = i->grow_machine_directory;
683
684 r = pull_find_old_etags(url, i->image_root, DT_REG, ".raw-", ".raw", &i->raw_job->old_etags);
685 if (r < 0)
686 return r;
687
688 if (roothash) {
689 r = pull_make_auxiliary_job(&i->roothash_job, url, raw_strip_suffixes, ".roothash", i->glue, raw_pull_job_on_finished, i);
690 if (r < 0)
691 return r;
692
693 i->roothash_job->on_open_disk = raw_pull_job_on_open_disk_roothash;
694 i->roothash_job->on_progress = raw_pull_job_on_progress;
695 i->roothash_job->calc_checksum = verify != IMPORT_VERIFY_NO;
696 }
697
698 if (settings) {
699 r = pull_make_auxiliary_job(&i->settings_job, url, raw_strip_suffixes, ".nspawn", i->glue, raw_pull_job_on_finished, i);
700 if (r < 0)
701 return r;
702
703 i->settings_job->on_open_disk = raw_pull_job_on_open_disk_settings;
704 i->settings_job->on_progress = raw_pull_job_on_progress;
705 i->settings_job->calc_checksum = verify != IMPORT_VERIFY_NO;
706 }
707
708 r = pull_make_verification_jobs(&i->checksum_job, &i->signature_job, verify, url, i->glue, raw_pull_job_on_finished, i);
709 if (r < 0)
710 return r;
711
712 r = pull_job_begin(i->raw_job);
713 if (r < 0)
714 return r;
715
716 if (i->roothash_job) {
717 r = pull_job_begin(i->roothash_job);
718 if (r < 0)
719 return r;
720 }
721
722 if (i->settings_job) {
723 r = pull_job_begin(i->settings_job);
724 if (r < 0)
725 return r;
726 }
727
728 if (i->checksum_job) {
729 i->checksum_job->on_progress = raw_pull_job_on_progress;
730 i->checksum_job->style = VERIFICATION_PER_FILE;
731
732 r = pull_job_begin(i->checksum_job);
733 if (r < 0)
734 return r;
735 }
736
737 if (i->signature_job) {
738 i->signature_job->on_progress = raw_pull_job_on_progress;
739
740 r = pull_job_begin(i->signature_job);
741 if (r < 0)
742 return r;
743 }
744
745 return 0;
746 }