1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2014 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 #include <curl/curl.h>
23 #include <sys/xattr.h>
25 #include "sd-daemon.h"
27 #include "alloc-util.h"
28 #include "btrfs-util.h"
29 #include "chattr-util.h"
31 #include "curl-util.h"
35 #include "hostname-util.h"
36 #include "import-common.h"
37 #include "import-util.h"
40 #include "path-util.h"
41 #include "pull-common.h"
44 #include "qcow2-util.h"
46 #include "string-util.h"
52 typedef enum RawProgress
{
67 PullJob
*roothash_job
;
68 PullJob
*settings_job
;
69 PullJob
*checksum_job
;
70 PullJob
*signature_job
;
72 RawPullFinished on_finished
;
77 bool grow_machine_directory
;
85 char *settings_temp_path
;
88 char *roothash_temp_path
;
93 RawPull
* raw_pull_unref(RawPull
*i
) {
97 pull_job_unref(i
->raw_job
);
98 pull_job_unref(i
->settings_job
);
99 pull_job_unref(i
->roothash_job
);
100 pull_job_unref(i
->checksum_job
);
101 pull_job_unref(i
->signature_job
);
103 curl_glue_unref(i
->glue
);
104 sd_event_unref(i
->event
);
107 (void) unlink(i
->temp_path
);
111 if (i
->roothash_temp_path
) {
112 (void) unlink(i
->roothash_temp_path
);
113 free(i
->roothash_temp_path
);
116 if (i
->settings_temp_path
) {
117 (void) unlink(i
->settings_temp_path
);
118 free(i
->settings_temp_path
);
122 free(i
->roothash_path
);
123 free(i
->settings_path
);
132 const char *image_root
,
133 RawPullFinished on_finished
,
136 _cleanup_(raw_pull_unrefp
) RawPull
*i
= NULL
;
141 i
= new0(RawPull
, 1);
145 i
->on_finished
= on_finished
;
146 i
->userdata
= userdata
;
148 i
->image_root
= strdup(image_root
?: "/var/lib/machines");
152 i
->grow_machine_directory
= path_startswith(i
->image_root
, "/var/lib/machines");
155 i
->event
= sd_event_ref(event
);
157 r
= sd_event_default(&i
->event
);
162 r
= curl_glue_new(&i
->glue
, i
->event
);
166 i
->glue
->on_finished
= pull_job_curl_on_finished
;
167 i
->glue
->userdata
= i
;
175 static void raw_pull_report_progress(RawPull
*i
, RawProgress p
) {
182 case RAW_DOWNLOADING
: {
183 unsigned remain
= 80;
187 if (i
->settings_job
) {
188 percent
+= i
->settings_job
->progress_percent
* 5 / 100;
192 if (i
->roothash_job
) {
193 percent
+= i
->roothash_job
->progress_percent
* 5 / 100;
197 if (i
->checksum_job
) {
198 percent
+= i
->checksum_job
->progress_percent
* 5 / 100;
202 if (i
->signature_job
) {
203 percent
+= i
->signature_job
->progress_percent
* 5 / 100;
208 percent
+= i
->raw_job
->progress_percent
* remain
/ 100;
229 assert_not_reached("Unknown progress state");
232 sd_notifyf(false, "X_IMPORT_PROGRESS=%u", percent
);
233 log_debug("Combined progress %u%%", percent
);
236 static int raw_pull_maybe_convert_qcow2(RawPull
*i
) {
237 _cleanup_close_
int converted_fd
= -1;
238 _cleanup_free_
char *t
= NULL
;
244 r
= qcow2_detect(i
->raw_job
->disk_fd
);
246 return log_error_errno(r
, "Failed to detect whether this is a QCOW2 image: %m");
250 /* This is a QCOW2 image, let's convert it */
251 r
= tempfn_random(i
->final_path
, NULL
, &t
);
255 converted_fd
= open(t
, O_RDWR
|O_CREAT
|O_EXCL
|O_NOCTTY
|O_CLOEXEC
, 0664);
256 if (converted_fd
< 0)
257 return log_error_errno(errno
, "Failed to create %s: %m", t
);
259 r
= chattr_fd(converted_fd
, FS_NOCOW_FL
, FS_NOCOW_FL
);
261 log_warning_errno(r
, "Failed to set file attributes on %s: %m", t
);
263 log_info("Unpacking QCOW2 file.");
265 r
= qcow2_convert(i
->raw_job
->disk_fd
, converted_fd
);
268 return log_error_errno(r
, "Failed to convert qcow2 image: %m");
271 (void) unlink(i
->temp_path
);
276 safe_close(i
->raw_job
->disk_fd
);
277 i
->raw_job
->disk_fd
= converted_fd
;
283 static int raw_pull_determine_path(RawPull
*i
, const char *suffix
, char **field
) {
294 r
= pull_make_path(i
->raw_job
->url
, i
->raw_job
->etag
, i
->image_root
, ".raw-", suffix
, field
);
301 static int raw_pull_copy_auxiliary_file(
313 r
= raw_pull_determine_path(i
, suffix
, path
);
317 local
= strjoina(i
->image_root
, "/", i
->local
, suffix
);
319 r
= copy_file_atomic(*path
, local
, 0644, 0, COPY_REFLINK
| (i
->force_local
? COPY_REPLACE
: 0));
321 log_warning_errno(r
, "File %s already exists, not replacing.", local
);
322 else if (r
== -ENOENT
)
323 log_debug_errno(r
, "Skipping creation of auxiliary file, since none was found.");
325 log_warning_errno(r
, "Failed to copy file %s, ignoring: %m", local
);
327 log_info("Created new file %s.", local
);
332 static int raw_pull_make_local_copy(RawPull
*i
) {
333 _cleanup_free_
char *tp
= NULL
;
334 _cleanup_close_
int dfd
= -1;
344 if (i
->raw_job
->etag_exists
) {
345 /* We have downloaded this one previously, reopen it */
347 assert(i
->raw_job
->disk_fd
< 0);
349 i
->raw_job
->disk_fd
= open(i
->final_path
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
);
350 if (i
->raw_job
->disk_fd
< 0)
351 return log_error_errno(errno
, "Failed to open vendor image: %m");
353 /* We freshly downloaded the image, use it */
355 assert(i
->raw_job
->disk_fd
>= 0);
357 if (lseek(i
->raw_job
->disk_fd
, SEEK_SET
, 0) == (off_t
) -1)
358 return log_error_errno(errno
, "Failed to seek to beginning of vendor image: %m");
361 p
= strjoina(i
->image_root
, "/", i
->local
, ".raw");
364 (void) rm_rf(p
, REMOVE_ROOT
|REMOVE_PHYSICAL
|REMOVE_SUBVOLUME
);
366 r
= tempfn_random(p
, NULL
, &tp
);
370 dfd
= open(tp
, O_WRONLY
|O_CREAT
|O_EXCL
|O_NOCTTY
|O_CLOEXEC
, 0664);
372 return log_error_errno(errno
, "Failed to create writable copy of image: %m");
374 /* Turn off COW writing. This should greatly improve
375 * performance on COW file systems like btrfs, since it
376 * reduces fragmentation caused by not allowing in-place
378 r
= chattr_fd(dfd
, FS_NOCOW_FL
, FS_NOCOW_FL
);
380 log_warning_errno(r
, "Failed to set file attributes on %s: %m", tp
);
382 r
= copy_bytes(i
->raw_job
->disk_fd
, dfd
, (uint64_t) -1, COPY_REFLINK
);
385 return log_error_errno(r
, "Failed to make writable copy of image: %m");
388 (void) copy_times(i
->raw_job
->disk_fd
, dfd
);
389 (void) copy_xattr(i
->raw_job
->disk_fd
, dfd
);
391 dfd
= safe_close(dfd
);
395 r
= log_error_errno(errno
, "Failed to move writable image into place: %m");
400 log_info("Created new local image '%s'.", i
->local
);
403 r
= raw_pull_copy_auxiliary_file(i
, ".roothash", &i
->roothash_path
);
409 r
= raw_pull_copy_auxiliary_file(i
, ".nspawn", &i
->settings_path
);
417 static bool raw_pull_is_done(RawPull
*i
) {
421 if (!PULL_JOB_IS_COMPLETE(i
->raw_job
))
423 if (i
->roothash_job
&& !PULL_JOB_IS_COMPLETE(i
->roothash_job
))
425 if (i
->settings_job
&& !PULL_JOB_IS_COMPLETE(i
->settings_job
))
427 if (i
->checksum_job
&& !PULL_JOB_IS_COMPLETE(i
->checksum_job
))
429 if (i
->signature_job
&& !PULL_JOB_IS_COMPLETE(i
->signature_job
))
435 static int raw_pull_rename_auxiliary_file(
448 /* Regenerate final name for this auxiliary file, we might know the etag of the file now, and we should
449 * incorporate it in the file name if we can */
450 *path
= mfree(*path
);
451 r
= raw_pull_determine_path(i
, suffix
, path
);
455 r
= import_make_read_only(*temp_path
);
459 r
= rename_noreplace(AT_FDCWD
, *temp_path
, AT_FDCWD
, *path
);
461 return log_error_errno(r
, "Failed to rename file %s to %s: %m", *temp_path
, *path
);
463 *temp_path
= mfree(*temp_path
);
468 static void raw_pull_job_on_finished(PullJob
*j
) {
476 if (j
== i
->roothash_job
) {
478 log_info_errno(j
->error
, "Root hash file could not be retrieved, proceeding without.");
479 } else if (j
== i
->settings_job
) {
481 log_info_errno(j
->error
, "Settings file could not be retrieved, proceeding without.");
482 } else if (j
->error
!= 0 && j
!= i
->signature_job
) {
483 if (j
== i
->checksum_job
)
484 log_error_errno(j
->error
, "Failed to retrieve SHA256 checksum, cannot verify. (Try --verify=no?)");
486 log_error_errno(j
->error
, "Failed to retrieve image file. (Wrong URL?)");
492 /* This is invoked if either the download completed
493 * successfully, or the download was skipped because we
494 * already have the etag. In this case ->etag_exists is
497 * We only do something when we got all three files */
499 if (!raw_pull_is_done(i
))
502 if (i
->signature_job
&& i
->checksum_job
->style
== VERIFICATION_PER_DIRECTORY
&& i
->signature_job
->error
!= 0) {
503 log_error_errno(j
->error
, "Failed to retrieve signature file, cannot verify. (Try --verify=no?)");
505 r
= i
->signature_job
->error
;
510 i
->roothash_job
->disk_fd
= safe_close(i
->roothash_job
->disk_fd
);
512 i
->settings_job
->disk_fd
= safe_close(i
->settings_job
->disk_fd
);
514 r
= raw_pull_determine_path(i
, ".raw", &i
->final_path
);
518 if (!i
->raw_job
->etag_exists
) {
519 /* This is a new download, verify it, and move it into place */
520 assert(i
->raw_job
->disk_fd
>= 0);
522 raw_pull_report_progress(i
, RAW_VERIFYING
);
524 r
= pull_verify(i
->raw_job
, i
->roothash_job
, i
->settings_job
, i
->checksum_job
, i
->signature_job
);
528 raw_pull_report_progress(i
, RAW_UNPACKING
);
530 r
= raw_pull_maybe_convert_qcow2(i
);
534 raw_pull_report_progress(i
, RAW_FINALIZING
);
536 r
= import_make_read_only_fd(i
->raw_job
->disk_fd
);
540 r
= rename_noreplace(AT_FDCWD
, i
->temp_path
, AT_FDCWD
, i
->final_path
);
542 log_error_errno(r
, "Failed to rename raw file to %s: %m", i
->final_path
);
546 i
->temp_path
= mfree(i
->temp_path
);
548 if (i
->roothash_job
&&
549 i
->roothash_job
->error
== 0) {
550 r
= raw_pull_rename_auxiliary_file(i
, ".roothash", &i
->roothash_temp_path
, &i
->roothash_path
);
555 if (i
->settings_job
&&
556 i
->settings_job
->error
== 0) {
557 r
= raw_pull_rename_auxiliary_file(i
, ".nspawn", &i
->settings_temp_path
, &i
->settings_path
);
563 raw_pull_report_progress(i
, RAW_COPYING
);
565 r
= raw_pull_make_local_copy(i
);
573 i
->on_finished(i
, r
, i
->userdata
);
575 sd_event_exit(i
->event
, r
);
578 static int raw_pull_job_on_open_disk_generic(
592 r
= tempfn_random_child(i
->image_root
, extra
, temp_path
);
597 (void) mkdir_parents_label(*temp_path
, 0700);
599 j
->disk_fd
= open(*temp_path
, O_RDWR
|O_CREAT
|O_EXCL
|O_NOCTTY
|O_CLOEXEC
, 0664);
601 return log_error_errno(errno
, "Failed to create %s: %m", *temp_path
);
606 static int raw_pull_job_on_open_disk_raw(PullJob
*j
) {
614 assert(i
->raw_job
== j
);
616 r
= raw_pull_job_on_open_disk_generic(i
, j
, "raw", &i
->temp_path
);
620 r
= chattr_fd(j
->disk_fd
, FS_NOCOW_FL
, FS_NOCOW_FL
);
622 log_warning_errno(r
, "Failed to set file attributes on %s, ignoring: %m", i
->temp_path
);
627 static int raw_pull_job_on_open_disk_roothash(PullJob
*j
) {
634 assert(i
->roothash_job
== j
);
636 return raw_pull_job_on_open_disk_generic(i
, j
, "roothash", &i
->roothash_temp_path
);
639 static int raw_pull_job_on_open_disk_settings(PullJob
*j
) {
646 assert(i
->settings_job
== j
);
648 return raw_pull_job_on_open_disk_generic(i
, j
, "settings", &i
->settings_temp_path
);
651 static void raw_pull_job_on_progress(PullJob
*j
) {
659 raw_pull_report_progress(i
, RAW_DOWNLOADING
);
674 assert(verify
< _IMPORT_VERIFY_MAX
);
677 if (!http_url_is_valid(url
))
680 if (local
&& !machine_name_is_valid(local
))
686 r
= free_and_strdup(&i
->local
, local
);
690 i
->force_local
= force_local
;
692 i
->settings
= settings
;
693 i
->roothash
= roothash
;
695 /* Queue job for the image itself */
696 r
= pull_job_new(&i
->raw_job
, url
, i
->glue
, i
);
700 i
->raw_job
->on_finished
= raw_pull_job_on_finished
;
701 i
->raw_job
->on_open_disk
= raw_pull_job_on_open_disk_raw
;
702 i
->raw_job
->on_progress
= raw_pull_job_on_progress
;
703 i
->raw_job
->calc_checksum
= verify
!= IMPORT_VERIFY_NO
;
704 i
->raw_job
->grow_machine_directory
= i
->grow_machine_directory
;
706 r
= pull_find_old_etags(url
, i
->image_root
, DT_REG
, ".raw-", ".raw", &i
->raw_job
->old_etags
);
711 r
= pull_make_auxiliary_job(&i
->roothash_job
, url
, raw_strip_suffixes
, ".roothash", i
->glue
, raw_pull_job_on_finished
, i
);
715 i
->roothash_job
->on_open_disk
= raw_pull_job_on_open_disk_roothash
;
716 i
->roothash_job
->on_progress
= raw_pull_job_on_progress
;
717 i
->roothash_job
->calc_checksum
= verify
!= IMPORT_VERIFY_NO
;
721 r
= pull_make_auxiliary_job(&i
->settings_job
, url
, raw_strip_suffixes
, ".nspawn", i
->glue
, raw_pull_job_on_finished
, i
);
725 i
->settings_job
->on_open_disk
= raw_pull_job_on_open_disk_settings
;
726 i
->settings_job
->on_progress
= raw_pull_job_on_progress
;
727 i
->settings_job
->calc_checksum
= verify
!= IMPORT_VERIFY_NO
;
730 r
= pull_make_verification_jobs(&i
->checksum_job
, &i
->signature_job
, verify
, url
, i
->glue
, raw_pull_job_on_finished
, i
);
734 r
= pull_job_begin(i
->raw_job
);
738 if (i
->roothash_job
) {
739 r
= pull_job_begin(i
->roothash_job
);
744 if (i
->settings_job
) {
745 r
= pull_job_begin(i
->settings_job
);
750 if (i
->checksum_job
) {
751 i
->checksum_job
->on_progress
= raw_pull_job_on_progress
;
752 i
->checksum_job
->style
= VERIFICATION_PER_FILE
;
754 r
= pull_job_begin(i
->checksum_job
);
759 if (i
->signature_job
) {
760 i
->signature_job
->on_progress
= raw_pull_job_on_progress
;
762 r
= pull_job_begin(i
->signature_job
);