1 /* SPDX-License-Identifier: LGPL-2.1+ */
8 #include "alloc-util.h"
9 #include "btrfs-util.h"
11 #include "curl-util.h"
15 #include "hostname-util.h"
16 #include "import-common.h"
17 #include "import-util.h"
20 #include "path-util.h"
21 #include "process-util.h"
22 #include "pull-common.h"
26 #include "string-util.h"
32 typedef enum TarProgress
{
46 PullJob
*settings_job
;
47 PullJob
*checksum_job
;
48 PullJob
*signature_job
;
50 TarPullFinished on_finished
;
55 bool grow_machine_directory
;
64 char *settings_temp_path
;
69 TarPull
* tar_pull_unref(TarPull
*i
) {
74 (void) kill_and_sigcont(i
->tar_pid
, SIGKILL
);
75 (void) wait_for_terminate(i
->tar_pid
, NULL
);
78 pull_job_unref(i
->tar_job
);
79 pull_job_unref(i
->settings_job
);
80 pull_job_unref(i
->checksum_job
);
81 pull_job_unref(i
->signature_job
);
83 curl_glue_unref(i
->glue
);
84 sd_event_unref(i
->event
);
87 (void) rm_rf(i
->temp_path
, REMOVE_ROOT
|REMOVE_PHYSICAL
|REMOVE_SUBVOLUME
);
91 if (i
->settings_temp_path
) {
92 (void) unlink(i
->settings_temp_path
);
93 free(i
->settings_temp_path
);
97 free(i
->settings_path
);
107 const char *image_root
,
108 TarPullFinished on_finished
,
111 _cleanup_(tar_pull_unrefp
) TarPull
*i
= NULL
;
116 i
= new0(TarPull
, 1);
120 i
->on_finished
= on_finished
;
121 i
->userdata
= userdata
;
123 i
->image_root
= strdup(image_root
?: "/var/lib/machines");
127 i
->grow_machine_directory
= path_startswith(i
->image_root
, "/var/lib/machines");
130 i
->event
= sd_event_ref(event
);
132 r
= sd_event_default(&i
->event
);
137 r
= curl_glue_new(&i
->glue
, i
->event
);
141 i
->glue
->on_finished
= pull_job_curl_on_finished
;
142 i
->glue
->userdata
= i
;
149 static void tar_pull_report_progress(TarPull
*i
, TarProgress p
) {
156 case TAR_DOWNLOADING
: {
157 unsigned remain
= 85;
161 if (i
->settings_job
) {
162 percent
+= i
->settings_job
->progress_percent
* 5 / 100;
166 if (i
->checksum_job
) {
167 percent
+= i
->checksum_job
->progress_percent
* 5 / 100;
171 if (i
->signature_job
) {
172 percent
+= i
->signature_job
->progress_percent
* 5 / 100;
177 percent
+= i
->tar_job
->progress_percent
* remain
/ 100;
194 assert_not_reached("Unknown progress state");
197 sd_notifyf(false, "X_IMPORT_PROGRESS=%u", percent
);
198 log_debug("Combined progress %u%%", percent
);
201 static int tar_pull_determine_path(TarPull
*i
, const char *suffix
, char **field
) {
212 r
= pull_make_path(i
->tar_job
->url
, i
->tar_job
->etag
, i
->image_root
, ".tar-", suffix
, field
);
219 static int tar_pull_make_local_copy(TarPull
*i
) {
228 r
= pull_make_local_copy(i
->final_path
, i
->image_root
, i
->local
, i
->force_local
);
233 const char *local_settings
;
234 assert(i
->settings_job
);
236 r
= tar_pull_determine_path(i
, ".nspawn", &i
->settings_path
);
240 local_settings
= strjoina(i
->image_root
, "/", i
->local
, ".nspawn");
242 r
= copy_file_atomic(i
->settings_path
, local_settings
, 0664, 0, COPY_REFLINK
| (i
->force_local
? COPY_REPLACE
: 0));
244 log_warning_errno(r
, "Settings file %s already exists, not replacing.", local_settings
);
245 else if (r
== -ENOENT
)
246 log_debug_errno(r
, "Skipping creation of settings file, since none was found.");
248 log_warning_errno(r
, "Failed to copy settings files %s, ignoring: %m", local_settings
);
250 log_info("Created new settings file %s.", local_settings
);
256 static bool tar_pull_is_done(TarPull
*i
) {
260 if (!PULL_JOB_IS_COMPLETE(i
->tar_job
))
262 if (i
->settings_job
&& !PULL_JOB_IS_COMPLETE(i
->settings_job
))
264 if (i
->checksum_job
&& !PULL_JOB_IS_COMPLETE(i
->checksum_job
))
266 if (i
->signature_job
&& !PULL_JOB_IS_COMPLETE(i
->signature_job
))
272 static void tar_pull_job_on_finished(PullJob
*j
) {
281 if (j
== i
->settings_job
) {
283 log_info_errno(j
->error
, "Settings file could not be retrieved, proceeding without.");
284 } else if (j
->error
!= 0 && j
!= i
->signature_job
) {
285 if (j
== i
->checksum_job
)
286 log_error_errno(j
->error
, "Failed to retrieve SHA256 checksum, cannot verify. (Try --verify=no?)");
288 log_error_errno(j
->error
, "Failed to retrieve image file. (Wrong URL?)");
294 /* This is invoked if either the download completed
295 * successfully, or the download was skipped because we
296 * already have the etag. */
298 if (!tar_pull_is_done(i
))
301 if (i
->signature_job
&& i
->checksum_job
->style
== VERIFICATION_PER_DIRECTORY
&& i
->signature_job
->error
!= 0) {
302 log_error_errno(j
->error
, "Failed to retrieve signature file, cannot verify. (Try --verify=no?)");
304 r
= i
->signature_job
->error
;
308 i
->tar_job
->disk_fd
= safe_close(i
->tar_job
->disk_fd
);
310 i
->settings_job
->disk_fd
= safe_close(i
->settings_job
->disk_fd
);
312 r
= tar_pull_determine_path(i
, NULL
, &i
->final_path
);
316 if (i
->tar_pid
> 0) {
317 r
= wait_for_terminate_and_check("tar", i
->tar_pid
, WAIT_LOG
);
321 if (r
!= EXIT_SUCCESS
) {
327 if (!i
->tar_job
->etag_exists
) {
328 /* This is a new download, verify it, and move it into place */
330 tar_pull_report_progress(i
, TAR_VERIFYING
);
332 r
= pull_verify(i
->tar_job
, NULL
, i
->settings_job
, i
->checksum_job
, i
->signature_job
);
336 tar_pull_report_progress(i
, TAR_FINALIZING
);
338 r
= import_make_read_only(i
->temp_path
);
342 r
= rename_noreplace(AT_FDCWD
, i
->temp_path
, AT_FDCWD
, i
->final_path
);
344 log_error_errno(r
, "Failed to rename to final image name to %s: %m", i
->final_path
);
348 i
->temp_path
= mfree(i
->temp_path
);
350 if (i
->settings_job
&&
351 i
->settings_job
->error
== 0) {
353 /* Also move the settings file into place, if it exists. Note that we do so only if we also
354 * moved the tar file in place, to keep things strictly in sync. */
355 assert(i
->settings_temp_path
);
357 /* Regenerate final name for this auxiliary file, we might know the etag of the file now, and
358 * we should incorporate it in the file name if we can */
359 i
->settings_path
= mfree(i
->settings_path
);
361 r
= tar_pull_determine_path(i
, ".nspawn", &i
->settings_path
);
365 r
= import_make_read_only(i
->settings_temp_path
);
369 r
= rename_noreplace(AT_FDCWD
, i
->settings_temp_path
, AT_FDCWD
, i
->settings_path
);
371 log_error_errno(r
, "Failed to rename settings file to %s: %m", i
->settings_path
);
375 i
->settings_temp_path
= mfree(i
->settings_temp_path
);
379 tar_pull_report_progress(i
, TAR_COPYING
);
381 r
= tar_pull_make_local_copy(i
);
389 i
->on_finished(i
, r
, i
->userdata
);
391 sd_event_exit(i
->event
, r
);
394 static int tar_pull_job_on_open_disk_tar(PullJob
*j
) {
402 assert(i
->tar_job
== j
);
403 assert(i
->tar_pid
<= 0);
406 r
= tempfn_random_child(i
->image_root
, "tar", &i
->temp_path
);
411 mkdir_parents_label(i
->temp_path
, 0700);
413 r
= btrfs_subvol_make(i
->temp_path
);
415 if (mkdir(i
->temp_path
, 0755) < 0)
416 return log_error_errno(errno
, "Failed to create directory %s: %m", i
->temp_path
);
418 return log_error_errno(r
, "Failed to create subvolume %s: %m", i
->temp_path
);
420 (void) import_assign_pool_quota_and_warn(i
->temp_path
);
422 j
->disk_fd
= import_fork_tar_x(i
->temp_path
, &i
->tar_pid
);
429 static int tar_pull_job_on_open_disk_settings(PullJob
*j
) {
437 assert(i
->settings_job
== j
);
439 if (!i
->settings_temp_path
) {
440 r
= tempfn_random_child(i
->image_root
, "settings", &i
->settings_temp_path
);
445 mkdir_parents_label(i
->settings_temp_path
, 0700);
447 j
->disk_fd
= open(i
->settings_temp_path
, O_RDWR
|O_CREAT
|O_EXCL
|O_NOCTTY
|O_CLOEXEC
, 0664);
449 return log_error_errno(errno
, "Failed to create %s: %m", i
->settings_temp_path
);
454 static void tar_pull_job_on_progress(PullJob
*j
) {
462 tar_pull_report_progress(i
, TAR_DOWNLOADING
);
476 assert(verify
< _IMPORT_VERIFY_MAX
);
479 if (!http_url_is_valid(url
))
482 if (local
&& !machine_name_is_valid(local
))
488 r
= free_and_strdup(&i
->local
, local
);
492 i
->force_local
= force_local
;
494 i
->settings
= settings
;
496 /* Set up download job for TAR file */
497 r
= pull_job_new(&i
->tar_job
, url
, i
->glue
, i
);
501 i
->tar_job
->on_finished
= tar_pull_job_on_finished
;
502 i
->tar_job
->on_open_disk
= tar_pull_job_on_open_disk_tar
;
503 i
->tar_job
->on_progress
= tar_pull_job_on_progress
;
504 i
->tar_job
->calc_checksum
= verify
!= IMPORT_VERIFY_NO
;
505 i
->tar_job
->grow_machine_directory
= i
->grow_machine_directory
;
507 r
= pull_find_old_etags(url
, i
->image_root
, DT_DIR
, ".tar-", NULL
, &i
->tar_job
->old_etags
);
511 /* Set up download job for the settings file (.nspawn) */
513 r
= pull_make_auxiliary_job(&i
->settings_job
, url
, tar_strip_suffixes
, ".nspawn", i
->glue
, tar_pull_job_on_finished
, i
);
517 i
->settings_job
->on_open_disk
= tar_pull_job_on_open_disk_settings
;
518 i
->settings_job
->on_progress
= tar_pull_job_on_progress
;
519 i
->settings_job
->calc_checksum
= verify
!= IMPORT_VERIFY_NO
;
522 /* Set up download of checksum/signature files */
523 r
= pull_make_verification_jobs(&i
->checksum_job
, &i
->signature_job
, verify
, url
, i
->glue
, tar_pull_job_on_finished
, i
);
527 r
= pull_job_begin(i
->tar_job
);
531 if (i
->settings_job
) {
532 r
= pull_job_begin(i
->settings_job
);
537 if (i
->checksum_job
) {
538 i
->checksum_job
->on_progress
= tar_pull_job_on_progress
;
539 i
->checksum_job
->style
= VERIFICATION_PER_FILE
;
541 r
= pull_job_begin(i
->checksum_job
);
546 if (i
->signature_job
) {
547 i
->signature_job
->on_progress
= tar_pull_job_on_progress
;
549 r
= pull_job_begin(i
->signature_job
);