]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/import/pull-tar.c
pkgconfig: define variables relative to ${prefix}/${rootprefix}/${sysconfdir}
[thirdparty/systemd.git] / src / import / pull-tar.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
56ebfaf1 2
56ebfaf1 3#include <curl/curl.h>
cf0fbc49 4#include <sys/prctl.h>
56ebfaf1 5
7079cfef 6#include "sd-daemon.h"
07630cea 7
b5efdb8a 8#include "alloc-util.h"
56ebfaf1 9#include "btrfs-util.h"
07630cea
LP
10#include "copy.h"
11#include "curl-util.h"
3ffd4af2 12#include "fd-util.h"
0d39fa9c 13#include "fileio.h"
f4f15635 14#include "fs-util.h"
07630cea
LP
15#include "hostname-util.h"
16#include "import-common.h"
17#include "import-util.h"
56ebfaf1
LP
18#include "macro.h"
19#include "mkdir.h"
26166c88 20#include "path-util.h"
25300b5a 21#include "process-util.h"
dc2c282b 22#include "pull-common.h"
07630cea 23#include "pull-job.h"
3ffd4af2 24#include "pull-tar.h"
07630cea
LP
25#include "rm-rf.h"
26#include "string-util.h"
27#include "strv.h"
28#include "utf8.h"
29#include "util.h"
49cf4170 30#include "web-util.h"
56ebfaf1 31
7079cfef
LP
32typedef enum TarProgress {
33 TAR_DOWNLOADING,
34 TAR_VERIFYING,
35 TAR_FINALIZING,
36 TAR_COPYING,
37} TarProgress;
38
dc2c282b 39struct TarPull {
56ebfaf1
LP
40 sd_event *event;
41 CurlGlue *glue;
42
43 char *image_root;
44
dc2c282b 45 PullJob *tar_job;
9854730b 46 PullJob *settings_job;
dc2c282b
LP
47 PullJob *checksum_job;
48 PullJob *signature_job;
56ebfaf1 49
dc2c282b 50 TarPullFinished on_finished;
56ebfaf1
LP
51 void *userdata;
52
56ebfaf1
LP
53 char *local;
54 bool force_local;
26166c88 55 bool grow_machine_directory;
9854730b 56 bool settings;
56ebfaf1
LP
57
58 pid_t tar_pid;
59
56ebfaf1 60 char *final_path;
9854730b
LP
61 char *temp_path;
62
63 char *settings_path;
64 char *settings_temp_path;
0100b6e1
LP
65
66 ImportVerify verify;
56ebfaf1
LP
67};
68
dc2c282b 69TarPull* tar_pull_unref(TarPull *i) {
56ebfaf1
LP
70 if (!i)
71 return NULL;
72
0100b6e1 73 if (i->tar_pid > 1) {
8b71fce8 74 (void) kill_and_sigcont(i->tar_pid, SIGKILL);
0100b6e1 75 (void) wait_for_terminate(i->tar_pid, NULL);
56ebfaf1
LP
76 }
77
dc2c282b 78 pull_job_unref(i->tar_job);
9854730b 79 pull_job_unref(i->settings_job);
dc2c282b
LP
80 pull_job_unref(i->checksum_job);
81 pull_job_unref(i->signature_job);
56ebfaf1
LP
82
83 curl_glue_unref(i->glue);
84 sd_event_unref(i->event);
85
86 if (i->temp_path) {
d9e2daaf 87 (void) rm_rf(i->temp_path, REMOVE_ROOT|REMOVE_PHYSICAL|REMOVE_SUBVOLUME);
0d6e763b 88 free(i->temp_path);
56ebfaf1
LP
89 }
90
9854730b
LP
91 if (i->settings_temp_path) {
92 (void) unlink(i->settings_temp_path);
93 free(i->settings_temp_path);
94 }
95
56ebfaf1 96 free(i->final_path);
9854730b 97 free(i->settings_path);
56ebfaf1
LP
98 free(i->image_root);
99 free(i->local);
e0061812 100
6b430fdb 101 return mfree(i);
56ebfaf1
LP
102}
103
dc2c282b
LP
104int tar_pull_new(
105 TarPull **ret,
8b71fce8
LP
106 sd_event *event,
107 const char *image_root,
dc2c282b 108 TarPullFinished on_finished,
8b71fce8
LP
109 void *userdata) {
110
dc2c282b 111 _cleanup_(tar_pull_unrefp) TarPull *i = NULL;
56ebfaf1
LP
112 int r;
113
114 assert(ret);
56ebfaf1 115
dc2c282b 116 i = new0(TarPull, 1);
56ebfaf1
LP
117 if (!i)
118 return -ENOMEM;
119
120 i->on_finished = on_finished;
121 i->userdata = userdata;
122
123 i->image_root = strdup(image_root ?: "/var/lib/machines");
124 if (!i->image_root)
125 return -ENOMEM;
126
26166c88
LP
127 i->grow_machine_directory = path_startswith(i->image_root, "/var/lib/machines");
128
9854730b
LP
129 if (event)
130 i->event = sd_event_ref(event);
131 else {
132 r = sd_event_default(&i->event);
133 if (r < 0)
134 return r;
135 }
56ebfaf1
LP
136
137 r = curl_glue_new(&i->glue, i->event);
138 if (r < 0)
139 return r;
140
dc2c282b 141 i->glue->on_finished = pull_job_curl_on_finished;
56ebfaf1
LP
142 i->glue->userdata = i;
143
1cc6c93a 144 *ret = TAKE_PTR(i);
56ebfaf1
LP
145
146 return 0;
147}
148
dc2c282b 149static void tar_pull_report_progress(TarPull *i, TarProgress p) {
7079cfef
LP
150 unsigned percent;
151
152 assert(i);
153
154 switch (p) {
155
156 case TAR_DOWNLOADING: {
157 unsigned remain = 85;
158
159 percent = 0;
160
9854730b
LP
161 if (i->settings_job) {
162 percent += i->settings_job->progress_percent * 5 / 100;
163 remain -= 5;
164 }
165
7079cfef
LP
166 if (i->checksum_job) {
167 percent += i->checksum_job->progress_percent * 5 / 100;
168 remain -= 5;
169 }
170
171 if (i->signature_job) {
172 percent += i->signature_job->progress_percent * 5 / 100;
173 remain -= 5;
174 }
175
176 if (i->tar_job)
177 percent += i->tar_job->progress_percent * remain / 100;
178 break;
179 }
180
181 case TAR_VERIFYING:
182 percent = 85;
183 break;
184
185 case TAR_FINALIZING:
186 percent = 90;
187 break;
188
189 case TAR_COPYING:
190 percent = 95;
191 break;
192
193 default:
194 assert_not_reached("Unknown progress state");
195 }
196
197 sd_notifyf(false, "X_IMPORT_PROGRESS=%u", percent);
198 log_debug("Combined progress %u%%", percent);
199}
200
91359193
LP
201static int tar_pull_determine_path(TarPull *i, const char *suffix, char **field) {
202 int r;
203
204 assert(i);
205 assert(field);
206
207 if (*field)
208 return 0;
209
210 assert(i->tar_job);
211
212 r = pull_make_path(i->tar_job->url, i->tar_job->etag, i->image_root, ".tar-", suffix, field);
213 if (r < 0)
214 return log_oom();
215
216 return 1;
217}
218
dc2c282b 219static int tar_pull_make_local_copy(TarPull *i) {
0d6e763b
LP
220 int r;
221
222 assert(i);
223 assert(i->tar_job);
224
225 if (!i->local)
226 return 0;
227
dc2c282b 228 r = pull_make_local_copy(i->final_path, i->image_root, i->local, i->force_local);
0100b6e1
LP
229 if (r < 0)
230 return r;
231
9854730b
LP
232 if (i->settings) {
233 const char *local_settings;
234 assert(i->settings_job);
235
91359193
LP
236 r = tar_pull_determine_path(i, ".nspawn", &i->settings_path);
237 if (r < 0)
238 return r;
9854730b
LP
239
240 local_settings = strjoina(i->image_root, "/", i->local, ".nspawn");
241
1c876927 242 r = copy_file_atomic(i->settings_path, local_settings, 0664, 0, COPY_REFLINK | (i->force_local ? COPY_REPLACE : 0));
9854730b
LP
243 if (r == -EEXIST)
244 log_warning_errno(r, "Settings file %s already exists, not replacing.", local_settings);
33859a6b
LP
245 else if (r == -ENOENT)
246 log_debug_errno(r, "Skipping creation of settings file, since none was found.");
247 else if (r < 0)
79b6198b
LP
248 log_warning_errno(r, "Failed to copy settings files %s, ignoring: %m", local_settings);
249 else
33859a6b 250 log_info("Created new settings file %s.", local_settings);
9854730b
LP
251 }
252
0d6e763b
LP
253 return 0;
254}
255
dc2c282b 256static bool tar_pull_is_done(TarPull *i) {
8b71fce8
LP
257 assert(i);
258 assert(i->tar_job);
259
9854730b 260 if (!PULL_JOB_IS_COMPLETE(i->tar_job))
8b71fce8 261 return false;
9854730b 262 if (i->settings_job && !PULL_JOB_IS_COMPLETE(i->settings_job))
8b71fce8 263 return false;
9854730b
LP
264 if (i->checksum_job && !PULL_JOB_IS_COMPLETE(i->checksum_job))
265 return false;
266 if (i->signature_job && !PULL_JOB_IS_COMPLETE(i->signature_job))
8b71fce8
LP
267 return false;
268
269 return true;
270}
271
dc2c282b
LP
272static void tar_pull_job_on_finished(PullJob *j) {
273 TarPull *i;
56ebfaf1
LP
274 int r;
275
276 assert(j);
277 assert(j->userdata);
278
279 i = j->userdata;
9854730b
LP
280
281 if (j == i->settings_job) {
282 if (j->error != 0)
283 log_info_errno(j->error, "Settings file could not be retrieved, proceeding without.");
697be0be 284 } else if (j->error != 0 && j != i->signature_job) {
0100b6e1
LP
285 if (j == i->checksum_job)
286 log_error_errno(j->error, "Failed to retrieve SHA256 checksum, cannot verify. (Try --verify=no?)");
0100b6e1
LP
287 else
288 log_error_errno(j->error, "Failed to retrieve image file. (Wrong URL?)");
289
56ebfaf1
LP
290 r = j->error;
291 goto finish;
292 }
293
294 /* This is invoked if either the download completed
295 * successfully, or the download was skipped because we
296 * already have the etag. */
297
dc2c282b 298 if (!tar_pull_is_done(i))
0100b6e1
LP
299 return;
300
c9fb8c7c 301 if (i->signature_job && i->checksum_job->style == VERIFICATION_PER_DIRECTORY && i->signature_job->error != 0) {
697be0be
TB
302 log_error_errno(j->error, "Failed to retrieve signature file, cannot verify. (Try --verify=no?)");
303
304 r = i->signature_job->error;
305 goto finish;
306 }
307
9854730b
LP
308 i->tar_job->disk_fd = safe_close(i->tar_job->disk_fd);
309 if (i->settings_job)
310 i->settings_job->disk_fd = safe_close(i->settings_job->disk_fd);
56ebfaf1 311
91359193
LP
312 r = tar_pull_determine_path(i, NULL, &i->final_path);
313 if (r < 0)
314 goto finish;
315
56ebfaf1 316 if (i->tar_pid > 0) {
7d4904fe 317 r = wait_for_terminate_and_check("tar", i->tar_pid, WAIT_LOG);
56ebfaf1
LP
318 i->tar_pid = 0;
319 if (r < 0)
320 goto finish;
b4a34311 321 if (r != EXIT_SUCCESS) {
9854730b
LP
322 r = -EIO;
323 goto finish;
324 }
56ebfaf1
LP
325 }
326
0100b6e1
LP
327 if (!i->tar_job->etag_exists) {
328 /* This is a new download, verify it, and move it into place */
329
dc2c282b 330 tar_pull_report_progress(i, TAR_VERIFYING);
7079cfef 331
91359193 332 r = pull_verify(i->tar_job, NULL, i->settings_job, i->checksum_job, i->signature_job);
0100b6e1
LP
333 if (r < 0)
334 goto finish;
335
dc2c282b 336 tar_pull_report_progress(i, TAR_FINALIZING);
7079cfef 337
b6e676ce 338 r = import_make_read_only(i->temp_path);
56ebfaf1
LP
339 if (r < 0)
340 goto finish;
341
f85ef957
AC
342 r = rename_noreplace(AT_FDCWD, i->temp_path, AT_FDCWD, i->final_path);
343 if (r < 0) {
dc38f65a 344 log_error_errno(r, "Failed to rename to final image name to %s: %m", i->final_path);
56ebfaf1
LP
345 goto finish;
346 }
56ebfaf1 347
9854730b
LP
348 i->temp_path = mfree(i->temp_path);
349
350 if (i->settings_job &&
91359193 351 i->settings_job->error == 0) {
9854730b 352
e0061812 353 /* Also move the settings file into place, if it exists. Note that we do so only if we also
91359193 354 * moved the tar file in place, to keep things strictly in sync. */
e0061812 355 assert(i->settings_temp_path);
91359193 356
e0061812
LP
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 */
91359193 359 i->settings_path = mfree(i->settings_path);
e0061812 360
91359193
LP
361 r = tar_pull_determine_path(i, ".nspawn", &i->settings_path);
362 if (r < 0)
363 goto finish;
9854730b
LP
364
365 r = import_make_read_only(i->settings_temp_path);
366 if (r < 0)
367 goto finish;
368
369 r = rename_noreplace(AT_FDCWD, i->settings_temp_path, AT_FDCWD, i->settings_path);
370 if (r < 0) {
dc38f65a 371 log_error_errno(r, "Failed to rename settings file to %s: %m", i->settings_path);
9854730b
LP
372 goto finish;
373 }
374
375 i->settings_temp_path = mfree(i->settings_temp_path);
376 }
56ebfaf1
LP
377 }
378
dc2c282b 379 tar_pull_report_progress(i, TAR_COPYING);
7079cfef 380
dc2c282b 381 r = tar_pull_make_local_copy(i);
0d6e763b
LP
382 if (r < 0)
383 goto finish;
384
56ebfaf1
LP
385 r = 0;
386
387finish:
56ebfaf1
LP
388 if (i->on_finished)
389 i->on_finished(i, r, i->userdata);
390 else
391 sd_event_exit(i->event, r);
392}
393
9854730b 394static int tar_pull_job_on_open_disk_tar(PullJob *j) {
dc2c282b 395 TarPull *i;
56ebfaf1
LP
396 int r;
397
398 assert(j);
399 assert(j->userdata);
400
401 i = j->userdata;
8b71fce8 402 assert(i->tar_job == j);
8b71fce8 403 assert(i->tar_pid <= 0);
56ebfaf1 404
91359193
LP
405 if (!i->temp_path) {
406 r = tempfn_random_child(i->image_root, "tar", &i->temp_path);
407 if (r < 0)
408 return log_oom();
409 }
56ebfaf1
LP
410
411 mkdir_parents_label(i->temp_path, 0700);
412
413 r = btrfs_subvol_make(i->temp_path);
414 if (r == -ENOTTY) {
415 if (mkdir(i->temp_path, 0755) < 0)
416 return log_error_errno(errno, "Failed to create directory %s: %m", i->temp_path);
417 } else if (r < 0)
709f6e46 418 return log_error_errno(r, "Failed to create subvolume %s: %m", i->temp_path);
8c9cfc28
LP
419 else
420 (void) import_assign_pool_quota_and_warn(i->temp_path);
56ebfaf1 421
587fec42 422 j->disk_fd = import_fork_tar_x(i->temp_path, &i->tar_pid);
2c140ded
LP
423 if (j->disk_fd < 0)
424 return j->disk_fd;
56ebfaf1
LP
425
426 return 0;
427}
428
9854730b
LP
429static int tar_pull_job_on_open_disk_settings(PullJob *j) {
430 TarPull *i;
431 int r;
432
433 assert(j);
434 assert(j->userdata);
435
436 i = j->userdata;
437 assert(i->settings_job == j);
9854730b 438
91359193
LP
439 if (!i->settings_temp_path) {
440 r = tempfn_random_child(i->image_root, "settings", &i->settings_temp_path);
441 if (r < 0)
442 return log_oom();
443 }
9854730b
LP
444
445 mkdir_parents_label(i->settings_temp_path, 0700);
446
447 j->disk_fd = open(i->settings_temp_path, O_RDWR|O_CREAT|O_EXCL|O_NOCTTY|O_CLOEXEC, 0664);
448 if (j->disk_fd < 0)
449 return log_error_errno(errno, "Failed to create %s: %m", i->settings_temp_path);
450
451 return 0;
452}
453
dc2c282b
LP
454static void tar_pull_job_on_progress(PullJob *j) {
455 TarPull *i;
7079cfef
LP
456
457 assert(j);
458 assert(j->userdata);
459
460 i = j->userdata;
461
dc2c282b 462 tar_pull_report_progress(i, TAR_DOWNLOADING);
7079cfef
LP
463}
464
9854730b
LP
465int tar_pull_start(
466 TarPull *i,
467 const char *url,
468 const char *local,
469 bool force_local,
470 ImportVerify verify,
471 bool settings) {
472
56ebfaf1
LP
473 int r;
474
475 assert(i);
9854730b
LP
476 assert(verify < _IMPORT_VERIFY_MAX);
477 assert(verify >= 0);
56ebfaf1 478
56ebfaf1
LP
479 if (!http_url_is_valid(url))
480 return -EINVAL;
481
482 if (local && !machine_name_is_valid(local))
483 return -EINVAL;
484
8b71fce8
LP
485 if (i->tar_job)
486 return -EBUSY;
487
56ebfaf1
LP
488 r = free_and_strdup(&i->local, local);
489 if (r < 0)
490 return r;
9854730b 491
56ebfaf1 492 i->force_local = force_local;
0100b6e1 493 i->verify = verify;
9854730b 494 i->settings = settings;
56ebfaf1 495
9854730b 496 /* Set up download job for TAR file */
dc2c282b 497 r = pull_job_new(&i->tar_job, url, i->glue, i);
56ebfaf1
LP
498 if (r < 0)
499 return r;
500
dc2c282b 501 i->tar_job->on_finished = tar_pull_job_on_finished;
9854730b 502 i->tar_job->on_open_disk = tar_pull_job_on_open_disk_tar;
dc2c282b 503 i->tar_job->on_progress = tar_pull_job_on_progress;
0100b6e1 504 i->tar_job->calc_checksum = verify != IMPORT_VERIFY_NO;
26166c88 505 i->tar_job->grow_machine_directory = i->grow_machine_directory;
56ebfaf1 506
dc2c282b 507 r = pull_find_old_etags(url, i->image_root, DT_DIR, ".tar-", NULL, &i->tar_job->old_etags);
56ebfaf1
LP
508 if (r < 0)
509 return r;
510
9854730b
LP
511 /* Set up download job for the settings file (.nspawn) */
512 if (settings) {
91359193 513 r = pull_make_auxiliary_job(&i->settings_job, url, tar_strip_suffixes, ".nspawn", i->glue, tar_pull_job_on_finished, i);
9854730b
LP
514 if (r < 0)
515 return r;
516
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;
9854730b
LP
520 }
521
522 /* Set up download of checksum/signature files */
dc2c282b 523 r = pull_make_verification_jobs(&i->checksum_job, &i->signature_job, verify, url, i->glue, tar_pull_job_on_finished, i);
0100b6e1
LP
524 if (r < 0)
525 return r;
526
dc2c282b 527 r = pull_job_begin(i->tar_job);
0100b6e1
LP
528 if (r < 0)
529 return r;
530
9854730b
LP
531 if (i->settings_job) {
532 r = pull_job_begin(i->settings_job);
533 if (r < 0)
534 return r;
535 }
536
0100b6e1 537 if (i->checksum_job) {
dc2c282b 538 i->checksum_job->on_progress = tar_pull_job_on_progress;
697be0be 539 i->checksum_job->style = VERIFICATION_PER_FILE;
7079cfef 540
dc2c282b 541 r = pull_job_begin(i->checksum_job);
0100b6e1
LP
542 if (r < 0)
543 return r;
544 }
545
546 if (i->signature_job) {
dc2c282b 547 i->signature_job->on_progress = tar_pull_job_on_progress;
7079cfef 548
dc2c282b 549 r = pull_job_begin(i->signature_job);
0100b6e1
LP
550 if (r < 0)
551 return r;
552 }
553
554 return 0;
56ebfaf1 555}