2 This file is part of systemd.
4 Copyright 2015 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 #include <curl/curl.h>
21 #include <sys/prctl.h>
23 #include "sd-daemon.h"
25 #include "alloc-util.h"
26 #include "btrfs-util.h"
28 #include "curl-util.h"
32 #include "hostname-util.h"
33 #include "import-common.h"
34 #include "import-util.h"
37 #include "path-util.h"
38 #include "process-util.h"
39 #include "pull-common.h"
43 #include "string-util.h"
49 typedef enum TarProgress
{
63 PullJob
*settings_job
;
64 PullJob
*checksum_job
;
65 PullJob
*signature_job
;
67 TarPullFinished on_finished
;
72 bool grow_machine_directory
;
81 char *settings_temp_path
;
86 TarPull
* tar_pull_unref(TarPull
*i
) {
91 (void) kill_and_sigcont(i
->tar_pid
, SIGKILL
);
92 (void) wait_for_terminate(i
->tar_pid
, NULL
);
95 pull_job_unref(i
->tar_job
);
96 pull_job_unref(i
->settings_job
);
97 pull_job_unref(i
->checksum_job
);
98 pull_job_unref(i
->signature_job
);
100 curl_glue_unref(i
->glue
);
101 sd_event_unref(i
->event
);
104 (void) rm_rf(i
->temp_path
, REMOVE_ROOT
|REMOVE_PHYSICAL
|REMOVE_SUBVOLUME
);
108 if (i
->settings_temp_path
) {
109 (void) unlink(i
->settings_temp_path
);
110 free(i
->settings_temp_path
);
114 free(i
->settings_path
);
123 const char *image_root
,
124 TarPullFinished on_finished
,
127 _cleanup_(tar_pull_unrefp
) TarPull
*i
= NULL
;
132 i
= new0(TarPull
, 1);
136 i
->on_finished
= on_finished
;
137 i
->userdata
= userdata
;
139 i
->image_root
= strdup(image_root
?: "/var/lib/machines");
143 i
->grow_machine_directory
= path_startswith(i
->image_root
, "/var/lib/machines");
146 i
->event
= sd_event_ref(event
);
148 r
= sd_event_default(&i
->event
);
153 r
= curl_glue_new(&i
->glue
, i
->event
);
157 i
->glue
->on_finished
= pull_job_curl_on_finished
;
158 i
->glue
->userdata
= i
;
166 static void tar_pull_report_progress(TarPull
*i
, TarProgress p
) {
173 case TAR_DOWNLOADING
: {
174 unsigned remain
= 85;
178 if (i
->settings_job
) {
179 percent
+= i
->settings_job
->progress_percent
* 5 / 100;
183 if (i
->checksum_job
) {
184 percent
+= i
->checksum_job
->progress_percent
* 5 / 100;
188 if (i
->signature_job
) {
189 percent
+= i
->signature_job
->progress_percent
* 5 / 100;
194 percent
+= i
->tar_job
->progress_percent
* remain
/ 100;
211 assert_not_reached("Unknown progress state");
214 sd_notifyf(false, "X_IMPORT_PROGRESS=%u", percent
);
215 log_debug("Combined progress %u%%", percent
);
218 static int tar_pull_determine_path(TarPull
*i
, const char *suffix
, char **field
) {
229 r
= pull_make_path(i
->tar_job
->url
, i
->tar_job
->etag
, i
->image_root
, ".tar-", suffix
, field
);
236 static int tar_pull_make_local_copy(TarPull
*i
) {
245 r
= pull_make_local_copy(i
->final_path
, i
->image_root
, i
->local
, i
->force_local
);
250 const char *local_settings
;
251 assert(i
->settings_job
);
253 r
= tar_pull_determine_path(i
, ".nspawn", &i
->settings_path
);
257 local_settings
= strjoina(i
->image_root
, "/", i
->local
, ".nspawn");
259 r
= copy_file_atomic(i
->settings_path
, local_settings
, 0664, 0, COPY_REFLINK
| (i
->force_local
? COPY_REPLACE
: 0));
261 log_warning_errno(r
, "Settings file %s already exists, not replacing.", local_settings
);
262 else if (r
== -ENOENT
)
263 log_debug_errno(r
, "Skipping creation of settings file, since none was found.");
265 log_warning_errno(r
, "Failed to copy settings files %s, ignoring: %m", local_settings
);
267 log_info("Created new settings file %s.", local_settings
);
273 static bool tar_pull_is_done(TarPull
*i
) {
277 if (!PULL_JOB_IS_COMPLETE(i
->tar_job
))
279 if (i
->settings_job
&& !PULL_JOB_IS_COMPLETE(i
->settings_job
))
281 if (i
->checksum_job
&& !PULL_JOB_IS_COMPLETE(i
->checksum_job
))
283 if (i
->signature_job
&& !PULL_JOB_IS_COMPLETE(i
->signature_job
))
289 static void tar_pull_job_on_finished(PullJob
*j
) {
298 if (j
== i
->settings_job
) {
300 log_info_errno(j
->error
, "Settings file could not be retrieved, proceeding without.");
301 } else if (j
->error
!= 0 && j
!= i
->signature_job
) {
302 if (j
== i
->checksum_job
)
303 log_error_errno(j
->error
, "Failed to retrieve SHA256 checksum, cannot verify. (Try --verify=no?)");
305 log_error_errno(j
->error
, "Failed to retrieve image file. (Wrong URL?)");
311 /* This is invoked if either the download completed
312 * successfully, or the download was skipped because we
313 * already have the etag. */
315 if (!tar_pull_is_done(i
))
318 if (i
->checksum_job
->style
== VERIFICATION_PER_DIRECTORY
&& i
->signature_job
->error
!= 0) {
319 log_error_errno(j
->error
, "Failed to retrieve signature file, cannot verify. (Try --verify=no?)");
321 r
= i
->signature_job
->error
;
325 i
->tar_job
->disk_fd
= safe_close(i
->tar_job
->disk_fd
);
327 i
->settings_job
->disk_fd
= safe_close(i
->settings_job
->disk_fd
);
329 r
= tar_pull_determine_path(i
, NULL
, &i
->final_path
);
333 if (i
->tar_pid
> 0) {
334 r
= wait_for_terminate_and_warn("tar", i
->tar_pid
, true);
344 if (!i
->tar_job
->etag_exists
) {
345 /* This is a new download, verify it, and move it into place */
347 tar_pull_report_progress(i
, TAR_VERIFYING
);
349 r
= pull_verify(i
->tar_job
, NULL
, i
->settings_job
, i
->checksum_job
, i
->signature_job
);
353 tar_pull_report_progress(i
, TAR_FINALIZING
);
355 r
= import_make_read_only(i
->temp_path
);
359 r
= rename_noreplace(AT_FDCWD
, i
->temp_path
, AT_FDCWD
, i
->final_path
);
361 log_error_errno(r
, "Failed to rename to final image name: %m");
365 i
->temp_path
= mfree(i
->temp_path
);
367 if (i
->settings_job
&&
368 i
->settings_job
->error
== 0) {
370 assert(i
->settings_temp_path
);
371 assert(i
->settings_path
);
373 /* Also move the settings file into place, if it exist. Note that we do so only if we also
374 * moved the tar file in place, to keep things strictly in sync. */
376 i
->settings_path
= mfree(i
->settings_path
);
377 r
= tar_pull_determine_path(i
, ".nspawn", &i
->settings_path
);
381 r
= import_make_read_only(i
->settings_temp_path
);
385 r
= rename_noreplace(AT_FDCWD
, i
->settings_temp_path
, AT_FDCWD
, i
->settings_path
);
387 log_error_errno(r
, "Failed to rename settings file: %m");
391 i
->settings_temp_path
= mfree(i
->settings_temp_path
);
395 tar_pull_report_progress(i
, TAR_COPYING
);
397 r
= tar_pull_make_local_copy(i
);
405 i
->on_finished(i
, r
, i
->userdata
);
407 sd_event_exit(i
->event
, r
);
410 static int tar_pull_job_on_open_disk_tar(PullJob
*j
) {
418 assert(i
->tar_job
== j
);
419 assert(i
->tar_pid
<= 0);
422 r
= tempfn_random_child(i
->image_root
, "tar", &i
->temp_path
);
427 mkdir_parents_label(i
->temp_path
, 0700);
429 r
= btrfs_subvol_make(i
->temp_path
);
431 if (mkdir(i
->temp_path
, 0755) < 0)
432 return log_error_errno(errno
, "Failed to create directory %s: %m", i
->temp_path
);
434 return log_error_errno(r
, "Failed to create subvolume %s: %m", i
->temp_path
);
436 (void) import_assign_pool_quota_and_warn(i
->temp_path
);
438 j
->disk_fd
= import_fork_tar_x(i
->temp_path
, &i
->tar_pid
);
445 static int tar_pull_job_on_open_disk_settings(PullJob
*j
) {
453 assert(i
->settings_job
== j
);
455 if (!i
->settings_temp_path
) {
456 r
= tempfn_random_child(i
->image_root
, "settings", &i
->settings_temp_path
);
461 mkdir_parents_label(i
->settings_temp_path
, 0700);
463 j
->disk_fd
= open(i
->settings_temp_path
, O_RDWR
|O_CREAT
|O_EXCL
|O_NOCTTY
|O_CLOEXEC
, 0664);
465 return log_error_errno(errno
, "Failed to create %s: %m", i
->settings_temp_path
);
470 static void tar_pull_job_on_progress(PullJob
*j
) {
478 tar_pull_report_progress(i
, TAR_DOWNLOADING
);
492 assert(verify
< _IMPORT_VERIFY_MAX
);
495 if (!http_url_is_valid(url
))
498 if (local
&& !machine_name_is_valid(local
))
504 r
= free_and_strdup(&i
->local
, local
);
508 i
->force_local
= force_local
;
510 i
->settings
= settings
;
512 /* Set up download job for TAR file */
513 r
= pull_job_new(&i
->tar_job
, url
, i
->glue
, i
);
517 i
->tar_job
->on_finished
= tar_pull_job_on_finished
;
518 i
->tar_job
->on_open_disk
= tar_pull_job_on_open_disk_tar
;
519 i
->tar_job
->on_progress
= tar_pull_job_on_progress
;
520 i
->tar_job
->calc_checksum
= verify
!= IMPORT_VERIFY_NO
;
521 i
->tar_job
->grow_machine_directory
= i
->grow_machine_directory
;
523 r
= pull_find_old_etags(url
, i
->image_root
, DT_DIR
, ".tar-", NULL
, &i
->tar_job
->old_etags
);
527 /* Set up download job for the settings file (.nspawn) */
529 r
= pull_make_auxiliary_job(&i
->settings_job
, url
, tar_strip_suffixes
, ".nspawn", i
->glue
, tar_pull_job_on_finished
, i
);
533 i
->settings_job
->on_open_disk
= tar_pull_job_on_open_disk_settings
;
534 i
->settings_job
->on_progress
= tar_pull_job_on_progress
;
535 i
->settings_job
->calc_checksum
= verify
!= IMPORT_VERIFY_NO
;
538 /* Set up download of checksum/signature files */
539 r
= pull_make_verification_jobs(&i
->checksum_job
, &i
->signature_job
, verify
, url
, i
->glue
, tar_pull_job_on_finished
, i
);
543 r
= pull_job_begin(i
->tar_job
);
547 if (i
->settings_job
) {
548 r
= pull_job_begin(i
->settings_job
);
553 if (i
->checksum_job
) {
554 i
->checksum_job
->on_progress
= tar_pull_job_on_progress
;
555 i
->checksum_job
->style
= VERIFICATION_PER_FILE
;
557 r
= pull_job_begin(i
->checksum_job
);
562 if (i
->signature_job
) {
563 i
->signature_job
->on_progress
= tar_pull_job_on_progress
;
565 r
= pull_job_begin(i
->signature_job
);