1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
8 #include "alloc-util.h"
9 #include "btrfs-util.h"
11 #include "curl-util.h"
14 #include "hostname-util.h"
15 #include "import-common.h"
16 #include "import-util.h"
17 #include "install-file.h"
19 #include "mkdir-label.h"
20 #include "path-util.h"
21 #include "process-util.h"
22 #include "pull-common.h"
26 #include "string-util.h"
28 #include "tmpfile-util.h"
29 #include "user-util.h"
33 typedef enum TarProgress
{
49 PullJob
*checksum_job
;
50 PullJob
*signature_job
;
51 PullJob
*settings_job
;
53 TarPullFinished on_finished
;
64 char *settings_temp_path
;
69 TarPull
* tar_pull_unref(TarPull
*i
) {
74 sigkill_wait(i
->tar_pid
);
76 pull_job_unref(i
->tar_job
);
77 pull_job_unref(i
->checksum_job
);
78 pull_job_unref(i
->signature_job
);
79 pull_job_unref(i
->settings_job
);
81 curl_glue_unref(i
->glue
);
82 sd_event_unref(i
->event
);
84 rm_rf_subvolume_and_free(i
->temp_path
);
85 unlink_and_free(i
->settings_temp_path
);
88 free(i
->settings_path
);
99 const char *image_root
,
100 TarPullFinished on_finished
,
103 _cleanup_(curl_glue_unrefp
) CurlGlue
*g
= NULL
;
104 _cleanup_(sd_event_unrefp
) sd_event
*e
= NULL
;
105 _cleanup_(tar_pull_unrefp
) TarPull
*i
= NULL
;
106 _cleanup_free_
char *root
= NULL
;
112 root
= strdup(image_root
);
117 e
= sd_event_ref(event
);
119 r
= sd_event_default(&e
);
124 r
= curl_glue_new(&g
, e
);
133 .on_finished
= on_finished
,
134 .userdata
= userdata
,
135 .image_root
= TAKE_PTR(root
),
136 .event
= TAKE_PTR(e
),
140 i
->glue
->on_finished
= pull_job_curl_on_finished
;
141 i
->glue
->userdata
= i
;
148 static void tar_pull_report_progress(TarPull
*i
, TarProgress p
) {
155 case TAR_DOWNLOADING
: {
156 unsigned remain
= 85;
160 if (i
->checksum_job
) {
161 percent
+= i
->checksum_job
->progress_percent
* 5 / 100;
165 if (i
->signature_job
) {
166 percent
+= i
->signature_job
->progress_percent
* 5 / 100;
170 if (i
->settings_job
) {
171 percent
+= i
->settings_job
->progress_percent
* 5 / 100;
176 percent
+= i
->tar_job
->progress_percent
* remain
/ 100;
193 assert_not_reached();
196 sd_notifyf(false, "X_IMPORT_PROGRESS=%u%%", percent
);
197 log_debug("Combined progress %u%%", percent
);
200 static int tar_pull_determine_path(
203 char **field
/* input + output (!) */) {
214 r
= pull_make_path(i
->tar_job
->url
, i
->tar_job
->etag
, i
->image_root
, ".tar-", suffix
, field
);
221 static int tar_pull_make_local_copy(TarPull
*i
) {
222 _cleanup_(rm_rf_subvolume_and_freep
) char *t
= NULL
;
223 _cleanup_free_
char *p
= NULL
;
232 assert(i
->final_path
);
234 p
= path_join(i
->image_root
, i
->local
);
238 r
= tempfn_random(p
, NULL
, &t
);
240 return log_error_errno(r
, "Failed to generate temporary filename for %s: %m", p
);
242 if (i
->flags
& IMPORT_BTRFS_SUBVOL
)
243 r
= btrfs_subvol_snapshot_at(
244 AT_FDCWD
, i
->final_path
,
246 (i
->flags
& IMPORT_BTRFS_QUOTA
? BTRFS_SNAPSHOT_QUOTA
: 0)|
247 BTRFS_SNAPSHOT_FALLBACK_COPY
|
248 BTRFS_SNAPSHOT_FALLBACK_DIRECTORY
|
249 BTRFS_SNAPSHOT_RECURSIVE
);
251 r
= copy_tree(i
->final_path
, t
, UID_INVALID
, GID_INVALID
, COPY_REFLINK
|COPY_HARDLINKS
, NULL
, NULL
);
253 return log_error_errno(r
, "Failed to create local image: %m");
255 r
= install_file(AT_FDCWD
, t
,
257 (i
->flags
& IMPORT_FORCE
? INSTALL_REPLACE
: 0) |
258 (i
->flags
& IMPORT_READ_ONLY
? INSTALL_READ_ONLY
: 0) |
259 (i
->flags
& IMPORT_SYNC
? INSTALL_SYNCFS
: 0));
261 return log_error_errno(r
, "Failed to install local image '%s': %m", p
);
265 log_info("Created new local image '%s'.", i
->local
);
267 if (FLAGS_SET(i
->flags
, IMPORT_PULL_SETTINGS
)) {
268 const char *local_settings
;
269 assert(i
->settings_job
);
271 r
= tar_pull_determine_path(i
, ".nspawn", &i
->settings_path
);
275 local_settings
= strjoina(i
->image_root
, "/", i
->local
, ".nspawn");
277 r
= copy_file_atomic(
282 (FLAGS_SET(i
->flags
, IMPORT_FORCE
) ? COPY_REPLACE
: 0) |
283 (FLAGS_SET(i
->flags
, IMPORT_SYNC
) ? COPY_FSYNC_FULL
: 0));
285 log_warning_errno(r
, "Settings file %s already exists, not replacing.", local_settings
);
286 else if (r
== -ENOENT
)
287 log_debug_errno(r
, "Skipping creation of settings file, since none was found.");
289 log_warning_errno(r
, "Failed to copy settings files %s, ignoring: %m", local_settings
);
291 log_info("Created new settings file %s.", local_settings
);
297 static bool tar_pull_is_done(TarPull
*i
) {
301 if (!PULL_JOB_IS_COMPLETE(i
->tar_job
))
303 if (i
->checksum_job
&& !PULL_JOB_IS_COMPLETE(i
->checksum_job
))
305 if (i
->signature_job
&& !PULL_JOB_IS_COMPLETE(i
->signature_job
))
307 if (i
->settings_job
&& !PULL_JOB_IS_COMPLETE(i
->settings_job
))
313 static void tar_pull_job_on_finished(PullJob
*j
) {
323 if (j
== i
->tar_job
) {
324 if (j
->error
== ENOMEDIUM
) /* HTTP 404 */
325 r
= log_error_errno(j
->error
, "Failed to retrieve image file. (Wrong URL?)");
327 r
= log_error_errno(j
->error
, "Failed to retrieve image file.");
329 } else if (j
== i
->checksum_job
) {
330 r
= log_error_errno(j
->error
, "Failed to retrieve SHA256 checksum, cannot verify. (Try --verify=no?)");
332 } else if (j
== i
->signature_job
)
333 log_debug_errno(j
->error
, "Signature job for %s failed, proceeding for now.", j
->url
);
334 else if (j
== i
->settings_job
)
335 log_info_errno(j
->error
, "Settings file could not be retrieved, proceeding without.");
337 assert("unexpected job");
340 /* This is invoked if either the download completed successfully, or the download was skipped because
341 * we already have the etag. */
343 if (!tar_pull_is_done(i
))
346 if (i
->signature_job
&& i
->signature_job
->error
!= 0) {
347 VerificationStyle style
;
349 assert(i
->checksum_job
);
351 r
= verification_style_from_url(i
->checksum_job
->url
, &style
);
353 log_error_errno(r
, "Failed to determine verification style from checksum URL: %m");
357 if (style
== VERIFICATION_PER_DIRECTORY
) { /* A failed signature file download only matters
358 * in per-directory verification mode, since only
359 * then the signature is detached, and thus a file
361 r
= log_error_errno(i
->signature_job
->error
,
362 "Failed to retrieve signature file, cannot verify. (Try --verify=no?)");
367 pull_job_close_disk_fd(i
->tar_job
);
368 pull_job_close_disk_fd(i
->settings_job
);
370 if (i
->tar_pid
> 0) {
371 r
= wait_for_terminate_and_check("tar", TAKE_PID(i
->tar_pid
), WAIT_LOG
);
374 if (r
!= EXIT_SUCCESS
) {
380 if (!i
->tar_job
->etag_exists
) {
381 /* This is a new download, verify it, and move it into place */
383 tar_pull_report_progress(i
, TAR_VERIFYING
);
385 r
= pull_verify(i
->verify
,
391 /* roothash_job = */ NULL
,
392 /* roothash_signature_job = */ NULL
,
393 /* verity_job = */ NULL
);
398 if (i
->flags
& IMPORT_DIRECT
) {
399 assert(!i
->settings_job
);
401 assert(!i
->temp_path
);
403 tar_pull_report_progress(i
, TAR_FINALIZING
);
405 r
= import_mangle_os_tree(i
->local
);
412 (i
->flags
& IMPORT_READ_ONLY
) ? INSTALL_READ_ONLY
: 0 |
413 (i
->flags
& IMPORT_SYNC
? INSTALL_SYNCFS
: 0));
415 log_error_errno(r
, "Failed to finalize '%s': %m", i
->local
);
419 r
= tar_pull_determine_path(i
, NULL
, &i
->final_path
);
423 if (!i
->tar_job
->etag_exists
) {
424 /* This is a new download, verify it, and move it into place */
426 assert(i
->temp_path
);
427 assert(i
->final_path
);
429 tar_pull_report_progress(i
, TAR_FINALIZING
);
431 r
= import_mangle_os_tree(i
->temp_path
);
436 AT_FDCWD
, i
->temp_path
,
437 AT_FDCWD
, i
->final_path
,
439 (i
->flags
& IMPORT_SYNC
? INSTALL_SYNCFS
: 0));
441 log_error_errno(r
, "Failed to rename to final image name to %s: %m", i
->final_path
);
445 i
->temp_path
= mfree(i
->temp_path
);
447 if (i
->settings_job
&&
448 i
->settings_job
->error
== 0) {
450 /* Also move the settings file into place, if it exists. Note that we do so only if we also
451 * moved the tar file in place, to keep things strictly in sync. */
452 assert(i
->settings_temp_path
);
454 /* Regenerate final name for this auxiliary file, we might know the etag of the file now, and
455 * we should incorporate it in the file name if we can */
456 i
->settings_path
= mfree(i
->settings_path
);
458 r
= tar_pull_determine_path(i
, ".nspawn", &i
->settings_path
);
463 AT_FDCWD
, i
->settings_temp_path
,
464 AT_FDCWD
, i
->settings_path
,
466 (i
->flags
& IMPORT_SYNC
? INSTALL_FSYNC_FULL
: 0));
468 log_error_errno(r
, "Failed to rename settings file to %s: %m", i
->settings_path
);
472 i
->settings_temp_path
= mfree(i
->settings_temp_path
);
476 tar_pull_report_progress(i
, TAR_COPYING
);
478 r
= tar_pull_make_local_copy(i
);
487 i
->on_finished(i
, r
, i
->userdata
);
489 sd_event_exit(i
->event
, r
);
492 static int tar_pull_job_on_open_disk_tar(PullJob
*j
) {
501 assert(i
->tar_job
== j
);
502 assert(i
->tar_pid
<= 0);
504 if (i
->flags
& IMPORT_DIRECT
)
508 r
= tempfn_random_child(i
->image_root
, "tar", &i
->temp_path
);
513 where
= i
->temp_path
;
516 (void) mkdir_parents_label(where
, 0700);
518 if (FLAGS_SET(i
->flags
, IMPORT_DIRECT
|IMPORT_FORCE
))
519 (void) rm_rf(where
, REMOVE_ROOT
|REMOVE_PHYSICAL
|REMOVE_SUBVOLUME
);
521 if (i
->flags
& IMPORT_BTRFS_SUBVOL
)
522 r
= btrfs_subvol_make_fallback(AT_FDCWD
, where
, 0755);
524 r
= RET_NERRNO(mkdir(where
, 0755));
525 if (r
== -EEXIST
&& (i
->flags
& IMPORT_DIRECT
)) /* EEXIST is OK if in direct mode, but not otherwise,
526 * because in that case our temporary path collided */
529 return log_error_errno(r
, "Failed to create directory/subvolume %s: %m", where
);
530 if (r
> 0 && (i
->flags
& IMPORT_BTRFS_QUOTA
)) { /* actually btrfs subvol */
531 if (!(i
->flags
& IMPORT_DIRECT
))
532 (void) import_assign_pool_quota_and_warn(i
->image_root
);
533 (void) import_assign_pool_quota_and_warn(where
);
536 j
->disk_fd
= import_fork_tar_x(where
, &i
->tar_pid
);
543 static int tar_pull_job_on_open_disk_settings(PullJob
*j
) {
551 assert(i
->settings_job
== j
);
553 if (!i
->settings_temp_path
) {
554 r
= tempfn_random_child(i
->image_root
, "settings", &i
->settings_temp_path
);
559 (void) mkdir_parents_label(i
->settings_temp_path
, 0700);
561 j
->disk_fd
= open(i
->settings_temp_path
, O_RDWR
|O_CREAT
|O_EXCL
|O_NOCTTY
|O_CLOEXEC
, 0664);
563 return log_error_errno(errno
, "Failed to create %s: %m", i
->settings_temp_path
);
568 static void tar_pull_job_on_progress(PullJob
*j
) {
576 tar_pull_report_progress(i
, TAR_DOWNLOADING
);
585 const char *checksum
) {
590 assert(verify
== _IMPORT_VERIFY_INVALID
|| verify
< _IMPORT_VERIFY_MAX
);
591 assert(verify
== _IMPORT_VERIFY_INVALID
|| verify
>= 0);
592 assert((verify
< 0) || !checksum
);
593 assert(!(flags
& ~IMPORT_PULL_FLAGS_MASK_TAR
));
594 assert(!(flags
& IMPORT_PULL_SETTINGS
) || !(flags
& IMPORT_DIRECT
));
595 assert(!(flags
& IMPORT_PULL_SETTINGS
) || !checksum
);
597 if (!http_url_is_valid(url
) && !file_url_is_valid(url
))
600 if (local
&& !pull_validate_local(local
, flags
))
606 r
= free_and_strdup(&i
->local
, local
);
610 r
= free_and_strdup(&i
->checksum
, checksum
);
617 /* Set up download job for TAR file */
618 r
= pull_job_new(&i
->tar_job
, url
, i
->glue
, i
);
622 i
->tar_job
->on_finished
= tar_pull_job_on_finished
;
623 i
->tar_job
->on_open_disk
= tar_pull_job_on_open_disk_tar
;
624 i
->tar_job
->calc_checksum
= checksum
|| IN_SET(verify
, IMPORT_VERIFY_CHECKSUM
, IMPORT_VERIFY_SIGNATURE
);
626 if (!FLAGS_SET(flags
, IMPORT_DIRECT
)) {
627 r
= pull_find_old_etags(url
, i
->image_root
, DT_DIR
, ".tar-", NULL
, &i
->tar_job
->old_etags
);
632 /* Set up download of checksum/signature files */
633 r
= pull_make_verification_jobs(
640 tar_pull_job_on_finished
,
645 /* Set up download job for the settings file (.nspawn) */
646 if (FLAGS_SET(flags
, IMPORT_PULL_SETTINGS
)) {
647 r
= pull_make_auxiliary_job(
654 tar_pull_job_on_open_disk_settings
,
655 tar_pull_job_on_finished
,
671 j
->on_progress
= tar_pull_job_on_progress
;
672 j
->sync
= FLAGS_SET(flags
, IMPORT_SYNC
);
674 r
= pull_job_begin(j
);