]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/import/pull-tar.c
machinectl: tweak address output in "machinectl status"
[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);
6b430fdb 117 return mfree(i);
56ebfaf1
LP
118}
119
dc2c282b
LP
120int tar_pull_new(
121 TarPull **ret,
8b71fce8
LP
122 sd_event *event,
123 const char *image_root,
dc2c282b 124 TarPullFinished on_finished,
8b71fce8
LP
125 void *userdata) {
126
dc2c282b 127 _cleanup_(tar_pull_unrefp) TarPull *i = NULL;
56ebfaf1
LP
128 int r;
129
130 assert(ret);
56ebfaf1 131
dc2c282b 132 i = new0(TarPull, 1);
56ebfaf1
LP
133 if (!i)
134 return -ENOMEM;
135
136 i->on_finished = on_finished;
137 i->userdata = userdata;
138
139 i->image_root = strdup(image_root ?: "/var/lib/machines");
140 if (!i->image_root)
141 return -ENOMEM;
142
26166c88
LP
143 i->grow_machine_directory = path_startswith(i->image_root, "/var/lib/machines");
144
9854730b
LP
145 if (event)
146 i->event = sd_event_ref(event);
147 else {
148 r = sd_event_default(&i->event);
149 if (r < 0)
150 return r;
151 }
56ebfaf1
LP
152
153 r = curl_glue_new(&i->glue, i->event);
154 if (r < 0)
155 return r;
156
dc2c282b 157 i->glue->on_finished = pull_job_curl_on_finished;
56ebfaf1
LP
158 i->glue->userdata = i;
159
160 *ret = i;
161 i = NULL;
162
163 return 0;
164}
165
dc2c282b 166static void tar_pull_report_progress(TarPull *i, TarProgress p) {
7079cfef
LP
167 unsigned percent;
168
169 assert(i);
170
171 switch (p) {
172
173 case TAR_DOWNLOADING: {
174 unsigned remain = 85;
175
176 percent = 0;
177
9854730b
LP
178 if (i->settings_job) {
179 percent += i->settings_job->progress_percent * 5 / 100;
180 remain -= 5;
181 }
182
7079cfef
LP
183 if (i->checksum_job) {
184 percent += i->checksum_job->progress_percent * 5 / 100;
185 remain -= 5;
186 }
187
188 if (i->signature_job) {
189 percent += i->signature_job->progress_percent * 5 / 100;
190 remain -= 5;
191 }
192
193 if (i->tar_job)
194 percent += i->tar_job->progress_percent * remain / 100;
195 break;
196 }
197
198 case TAR_VERIFYING:
199 percent = 85;
200 break;
201
202 case TAR_FINALIZING:
203 percent = 90;
204 break;
205
206 case TAR_COPYING:
207 percent = 95;
208 break;
209
210 default:
211 assert_not_reached("Unknown progress state");
212 }
213
214 sd_notifyf(false, "X_IMPORT_PROGRESS=%u", percent);
215 log_debug("Combined progress %u%%", percent);
216}
217
91359193
LP
218static int tar_pull_determine_path(TarPull *i, const char *suffix, char **field) {
219 int r;
220
221 assert(i);
222 assert(field);
223
224 if (*field)
225 return 0;
226
227 assert(i->tar_job);
228
229 r = pull_make_path(i->tar_job->url, i->tar_job->etag, i->image_root, ".tar-", suffix, field);
230 if (r < 0)
231 return log_oom();
232
233 return 1;
234}
235
dc2c282b 236static int tar_pull_make_local_copy(TarPull *i) {
0d6e763b
LP
237 int r;
238
239 assert(i);
240 assert(i->tar_job);
241
242 if (!i->local)
243 return 0;
244
dc2c282b 245 r = pull_make_local_copy(i->final_path, i->image_root, i->local, i->force_local);
0100b6e1
LP
246 if (r < 0)
247 return r;
248
9854730b
LP
249 if (i->settings) {
250 const char *local_settings;
251 assert(i->settings_job);
252
91359193
LP
253 r = tar_pull_determine_path(i, ".nspawn", &i->settings_path);
254 if (r < 0)
255 return r;
9854730b
LP
256
257 local_settings = strjoina(i->image_root, "/", i->local, ".nspawn");
258
259 r = copy_file_atomic(i->settings_path, local_settings, 0664, i->force_local, 0);
260 if (r == -EEXIST)
261 log_warning_errno(r, "Settings file %s already exists, not replacing.", local_settings);
33859a6b
LP
262 else if (r == -ENOENT)
263 log_debug_errno(r, "Skipping creation of settings file, since none was found.");
264 else if (r < 0)
79b6198b
LP
265 log_warning_errno(r, "Failed to copy settings files %s, ignoring: %m", local_settings);
266 else
33859a6b 267 log_info("Created new settings file %s.", local_settings);
9854730b
LP
268 }
269
0d6e763b
LP
270 return 0;
271}
272
dc2c282b 273static bool tar_pull_is_done(TarPull *i) {
8b71fce8
LP
274 assert(i);
275 assert(i->tar_job);
276
9854730b 277 if (!PULL_JOB_IS_COMPLETE(i->tar_job))
8b71fce8 278 return false;
9854730b 279 if (i->settings_job && !PULL_JOB_IS_COMPLETE(i->settings_job))
8b71fce8 280 return false;
9854730b
LP
281 if (i->checksum_job && !PULL_JOB_IS_COMPLETE(i->checksum_job))
282 return false;
283 if (i->signature_job && !PULL_JOB_IS_COMPLETE(i->signature_job))
8b71fce8
LP
284 return false;
285
286 return true;
287}
288
dc2c282b
LP
289static void tar_pull_job_on_finished(PullJob *j) {
290 TarPull *i;
56ebfaf1
LP
291 int r;
292
293 assert(j);
294 assert(j->userdata);
295
296 i = j->userdata;
9854730b
LP
297
298 if (j == i->settings_job) {
299 if (j->error != 0)
300 log_info_errno(j->error, "Settings file could not be retrieved, proceeding without.");
301 } else if (j->error != 0) {
0100b6e1
LP
302 if (j == i->checksum_job)
303 log_error_errno(j->error, "Failed to retrieve SHA256 checksum, cannot verify. (Try --verify=no?)");
304 else if (j == i->signature_job)
305 log_error_errno(j->error, "Failed to retrieve signature file, cannot verify. (Try --verify=no?)");
306 else
307 log_error_errno(j->error, "Failed to retrieve image file. (Wrong URL?)");
308
56ebfaf1
LP
309 r = j->error;
310 goto finish;
311 }
312
313 /* This is invoked if either the download completed
314 * successfully, or the download was skipped because we
315 * already have the etag. */
316
dc2c282b 317 if (!tar_pull_is_done(i))
0100b6e1
LP
318 return;
319
9854730b
LP
320 i->tar_job->disk_fd = safe_close(i->tar_job->disk_fd);
321 if (i->settings_job)
322 i->settings_job->disk_fd = safe_close(i->settings_job->disk_fd);
56ebfaf1 323
91359193
LP
324 r = tar_pull_determine_path(i, NULL, &i->final_path);
325 if (r < 0)
326 goto finish;
327
56ebfaf1
LP
328 if (i->tar_pid > 0) {
329 r = wait_for_terminate_and_warn("tar", i->tar_pid, true);
330 i->tar_pid = 0;
331 if (r < 0)
332 goto finish;
9854730b
LP
333 if (r > 0) {
334 r = -EIO;
335 goto finish;
336 }
56ebfaf1
LP
337 }
338
0100b6e1
LP
339 if (!i->tar_job->etag_exists) {
340 /* This is a new download, verify it, and move it into place */
341
dc2c282b 342 tar_pull_report_progress(i, TAR_VERIFYING);
7079cfef 343
91359193 344 r = pull_verify(i->tar_job, NULL, i->settings_job, i->checksum_job, i->signature_job);
0100b6e1
LP
345 if (r < 0)
346 goto finish;
347
dc2c282b 348 tar_pull_report_progress(i, TAR_FINALIZING);
7079cfef 349
b6e676ce 350 r = import_make_read_only(i->temp_path);
56ebfaf1
LP
351 if (r < 0)
352 goto finish;
353
f85ef957
AC
354 r = rename_noreplace(AT_FDCWD, i->temp_path, AT_FDCWD, i->final_path);
355 if (r < 0) {
356 log_error_errno(r, "Failed to rename to final image name: %m");
56ebfaf1
LP
357 goto finish;
358 }
56ebfaf1 359
9854730b
LP
360 i->temp_path = mfree(i->temp_path);
361
362 if (i->settings_job &&
91359193 363 i->settings_job->error == 0) {
9854730b
LP
364
365 assert(i->settings_temp_path);
366 assert(i->settings_path);
367
91359193
LP
368 /* Also move the settings file into place, if it exist. Note that we do so only if we also
369 * moved the tar file in place, to keep things strictly in sync. */
370
371 i->settings_path = mfree(i->settings_path);
372 r = tar_pull_determine_path(i, ".nspawn", &i->settings_path);
373 if (r < 0)
374 goto finish;
9854730b
LP
375
376 r = import_make_read_only(i->settings_temp_path);
377 if (r < 0)
378 goto finish;
379
380 r = rename_noreplace(AT_FDCWD, i->settings_temp_path, AT_FDCWD, i->settings_path);
381 if (r < 0) {
382 log_error_errno(r, "Failed to rename settings file: %m");
383 goto finish;
384 }
385
386 i->settings_temp_path = mfree(i->settings_temp_path);
387 }
56ebfaf1
LP
388 }
389
dc2c282b 390 tar_pull_report_progress(i, TAR_COPYING);
7079cfef 391
dc2c282b 392 r = tar_pull_make_local_copy(i);
0d6e763b
LP
393 if (r < 0)
394 goto finish;
395
56ebfaf1
LP
396 r = 0;
397
398finish:
56ebfaf1
LP
399 if (i->on_finished)
400 i->on_finished(i, r, i->userdata);
401 else
402 sd_event_exit(i->event, r);
403}
404
9854730b 405static int tar_pull_job_on_open_disk_tar(PullJob *j) {
dc2c282b 406 TarPull *i;
56ebfaf1
LP
407 int r;
408
409 assert(j);
410 assert(j->userdata);
411
412 i = j->userdata;
8b71fce8 413 assert(i->tar_job == j);
8b71fce8 414 assert(i->tar_pid <= 0);
56ebfaf1 415
91359193
LP
416 if (!i->temp_path) {
417 r = tempfn_random_child(i->image_root, "tar", &i->temp_path);
418 if (r < 0)
419 return log_oom();
420 }
56ebfaf1
LP
421
422 mkdir_parents_label(i->temp_path, 0700);
423
424 r = btrfs_subvol_make(i->temp_path);
425 if (r == -ENOTTY) {
426 if (mkdir(i->temp_path, 0755) < 0)
427 return log_error_errno(errno, "Failed to create directory %s: %m", i->temp_path);
428 } else if (r < 0)
709f6e46 429 return log_error_errno(r, "Failed to create subvolume %s: %m", i->temp_path);
8c9cfc28
LP
430 else
431 (void) import_assign_pool_quota_and_warn(i->temp_path);
56ebfaf1 432
587fec42 433 j->disk_fd = import_fork_tar_x(i->temp_path, &i->tar_pid);
2c140ded
LP
434 if (j->disk_fd < 0)
435 return j->disk_fd;
56ebfaf1
LP
436
437 return 0;
438}
439
9854730b
LP
440static int tar_pull_job_on_open_disk_settings(PullJob *j) {
441 TarPull *i;
442 int r;
443
444 assert(j);
445 assert(j->userdata);
446
447 i = j->userdata;
448 assert(i->settings_job == j);
9854730b 449
91359193
LP
450 if (!i->settings_temp_path) {
451 r = tempfn_random_child(i->image_root, "settings", &i->settings_temp_path);
452 if (r < 0)
453 return log_oom();
454 }
9854730b
LP
455
456 mkdir_parents_label(i->settings_temp_path, 0700);
457
458 j->disk_fd = open(i->settings_temp_path, O_RDWR|O_CREAT|O_EXCL|O_NOCTTY|O_CLOEXEC, 0664);
459 if (j->disk_fd < 0)
460 return log_error_errno(errno, "Failed to create %s: %m", i->settings_temp_path);
461
462 return 0;
463}
464
dc2c282b
LP
465static void tar_pull_job_on_progress(PullJob *j) {
466 TarPull *i;
7079cfef
LP
467
468 assert(j);
469 assert(j->userdata);
470
471 i = j->userdata;
472
dc2c282b 473 tar_pull_report_progress(i, TAR_DOWNLOADING);
7079cfef
LP
474}
475
9854730b
LP
476int tar_pull_start(
477 TarPull *i,
478 const char *url,
479 const char *local,
480 bool force_local,
481 ImportVerify verify,
482 bool settings) {
483
56ebfaf1
LP
484 int r;
485
486 assert(i);
9854730b
LP
487 assert(verify < _IMPORT_VERIFY_MAX);
488 assert(verify >= 0);
56ebfaf1 489
56ebfaf1
LP
490 if (!http_url_is_valid(url))
491 return -EINVAL;
492
493 if (local && !machine_name_is_valid(local))
494 return -EINVAL;
495
8b71fce8
LP
496 if (i->tar_job)
497 return -EBUSY;
498
56ebfaf1
LP
499 r = free_and_strdup(&i->local, local);
500 if (r < 0)
501 return r;
9854730b 502
56ebfaf1 503 i->force_local = force_local;
0100b6e1 504 i->verify = verify;
9854730b 505 i->settings = settings;
56ebfaf1 506
9854730b 507 /* Set up download job for TAR file */
dc2c282b 508 r = pull_job_new(&i->tar_job, url, i->glue, i);
56ebfaf1
LP
509 if (r < 0)
510 return r;
511
dc2c282b 512 i->tar_job->on_finished = tar_pull_job_on_finished;
9854730b 513 i->tar_job->on_open_disk = tar_pull_job_on_open_disk_tar;
dc2c282b 514 i->tar_job->on_progress = tar_pull_job_on_progress;
0100b6e1 515 i->tar_job->calc_checksum = verify != IMPORT_VERIFY_NO;
26166c88 516 i->tar_job->grow_machine_directory = i->grow_machine_directory;
56ebfaf1 517
dc2c282b 518 r = pull_find_old_etags(url, i->image_root, DT_DIR, ".tar-", NULL, &i->tar_job->old_etags);
56ebfaf1
LP
519 if (r < 0)
520 return r;
521
9854730b
LP
522 /* Set up download job for the settings file (.nspawn) */
523 if (settings) {
91359193 524 r = pull_make_auxiliary_job(&i->settings_job, url, tar_strip_suffixes, ".nspawn", i->glue, tar_pull_job_on_finished, i);
9854730b
LP
525 if (r < 0)
526 return r;
527
528 i->settings_job->on_open_disk = tar_pull_job_on_open_disk_settings;
529 i->settings_job->on_progress = tar_pull_job_on_progress;
530 i->settings_job->calc_checksum = verify != IMPORT_VERIFY_NO;
9854730b
LP
531 }
532
533 /* Set up download of checksum/signature files */
dc2c282b 534 r = pull_make_verification_jobs(&i->checksum_job, &i->signature_job, verify, url, i->glue, tar_pull_job_on_finished, i);
0100b6e1
LP
535 if (r < 0)
536 return r;
537
dc2c282b 538 r = pull_job_begin(i->tar_job);
0100b6e1
LP
539 if (r < 0)
540 return r;
541
9854730b
LP
542 if (i->settings_job) {
543 r = pull_job_begin(i->settings_job);
544 if (r < 0)
545 return r;
546 }
547
0100b6e1 548 if (i->checksum_job) {
dc2c282b 549 i->checksum_job->on_progress = tar_pull_job_on_progress;
7079cfef 550
dc2c282b 551 r = pull_job_begin(i->checksum_job);
0100b6e1
LP
552 if (r < 0)
553 return r;
554 }
555
556 if (i->signature_job) {
dc2c282b 557 i->signature_job->on_progress = tar_pull_job_on_progress;
7079cfef 558
dc2c282b 559 r = pull_job_begin(i->signature_job);
0100b6e1
LP
560 if (r < 0)
561 return r;
562 }
563
564 return 0;
56ebfaf1 565}