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