]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/import/pull-tar.c
Merge pull request #5333 from poettering/machined-copy-files-userns
[thirdparty/systemd.git] / src / import / pull-tar.c
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
20 #include <curl/curl.h>
21 #include <sys/prctl.h>
22
23 #include "sd-daemon.h"
24
25 #include "alloc-util.h"
26 #include "btrfs-util.h"
27 #include "copy.h"
28 #include "curl-util.h"
29 #include "fd-util.h"
30 #include "fileio.h"
31 #include "fs-util.h"
32 #include "hostname-util.h"
33 #include "import-common.h"
34 #include "import-util.h"
35 #include "macro.h"
36 #include "mkdir.h"
37 #include "path-util.h"
38 #include "process-util.h"
39 #include "pull-common.h"
40 #include "pull-job.h"
41 #include "pull-tar.h"
42 #include "rm-rf.h"
43 #include "string-util.h"
44 #include "strv.h"
45 #include "utf8.h"
46 #include "util.h"
47 #include "web-util.h"
48
49 typedef enum TarProgress {
50 TAR_DOWNLOADING,
51 TAR_VERIFYING,
52 TAR_FINALIZING,
53 TAR_COPYING,
54 } TarProgress;
55
56 struct TarPull {
57 sd_event *event;
58 CurlGlue *glue;
59
60 char *image_root;
61
62 PullJob *tar_job;
63 PullJob *settings_job;
64 PullJob *checksum_job;
65 PullJob *signature_job;
66
67 TarPullFinished on_finished;
68 void *userdata;
69
70 char *local;
71 bool force_local;
72 bool grow_machine_directory;
73 bool settings;
74
75 pid_t tar_pid;
76
77 char *final_path;
78 char *temp_path;
79
80 char *settings_path;
81 char *settings_temp_path;
82
83 ImportVerify verify;
84 };
85
86 TarPull* tar_pull_unref(TarPull *i) {
87 if (!i)
88 return NULL;
89
90 if (i->tar_pid > 1) {
91 (void) kill_and_sigcont(i->tar_pid, SIGKILL);
92 (void) wait_for_terminate(i->tar_pid, NULL);
93 }
94
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);
99
100 curl_glue_unref(i->glue);
101 sd_event_unref(i->event);
102
103 if (i->temp_path) {
104 (void) rm_rf(i->temp_path, REMOVE_ROOT|REMOVE_PHYSICAL|REMOVE_SUBVOLUME);
105 free(i->temp_path);
106 }
107
108 if (i->settings_temp_path) {
109 (void) unlink(i->settings_temp_path);
110 free(i->settings_temp_path);
111 }
112
113 free(i->final_path);
114 free(i->settings_path);
115 free(i->image_root);
116 free(i->local);
117 return mfree(i);
118 }
119
120 int tar_pull_new(
121 TarPull **ret,
122 sd_event *event,
123 const char *image_root,
124 TarPullFinished on_finished,
125 void *userdata) {
126
127 _cleanup_(tar_pull_unrefp) TarPull *i = NULL;
128 int r;
129
130 assert(ret);
131
132 i = new0(TarPull, 1);
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
143 i->grow_machine_directory = path_startswith(i->image_root, "/var/lib/machines");
144
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 }
152
153 r = curl_glue_new(&i->glue, i->event);
154 if (r < 0)
155 return r;
156
157 i->glue->on_finished = pull_job_curl_on_finished;
158 i->glue->userdata = i;
159
160 *ret = i;
161 i = NULL;
162
163 return 0;
164 }
165
166 static void tar_pull_report_progress(TarPull *i, TarProgress p) {
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
178 if (i->settings_job) {
179 percent += i->settings_job->progress_percent * 5 / 100;
180 remain -= 5;
181 }
182
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
218 static 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
236 static int tar_pull_make_local_copy(TarPull *i) {
237 int r;
238
239 assert(i);
240 assert(i->tar_job);
241
242 if (!i->local)
243 return 0;
244
245 r = pull_make_local_copy(i->final_path, i->image_root, i->local, i->force_local);
246 if (r < 0)
247 return r;
248
249 if (i->settings) {
250 const char *local_settings;
251 assert(i->settings_job);
252
253 r = tar_pull_determine_path(i, ".nspawn", &i->settings_path);
254 if (r < 0)
255 return r;
256
257 local_settings = strjoina(i->image_root, "/", i->local, ".nspawn");
258
259 r = copy_file_atomic(i->settings_path, local_settings, 0664, 0, COPY_REFLINK | (i->force_local ? COPY_REPLACE : 0));
260 if (r == -EEXIST)
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.");
264 else if (r < 0)
265 log_warning_errno(r, "Failed to copy settings files %s, ignoring: %m", local_settings);
266 else
267 log_info("Created new settings file %s.", local_settings);
268 }
269
270 return 0;
271 }
272
273 static bool tar_pull_is_done(TarPull *i) {
274 assert(i);
275 assert(i->tar_job);
276
277 if (!PULL_JOB_IS_COMPLETE(i->tar_job))
278 return false;
279 if (i->settings_job && !PULL_JOB_IS_COMPLETE(i->settings_job))
280 return false;
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))
284 return false;
285
286 return true;
287 }
288
289 static void tar_pull_job_on_finished(PullJob *j) {
290 TarPull *i;
291 int r;
292
293 assert(j);
294 assert(j->userdata);
295
296 i = j->userdata;
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) {
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
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
317 if (!tar_pull_is_done(i))
318 return;
319
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);
323
324 r = tar_pull_determine_path(i, NULL, &i->final_path);
325 if (r < 0)
326 goto finish;
327
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;
333 if (r > 0) {
334 r = -EIO;
335 goto finish;
336 }
337 }
338
339 if (!i->tar_job->etag_exists) {
340 /* This is a new download, verify it, and move it into place */
341
342 tar_pull_report_progress(i, TAR_VERIFYING);
343
344 r = pull_verify(i->tar_job, NULL, i->settings_job, i->checksum_job, i->signature_job);
345 if (r < 0)
346 goto finish;
347
348 tar_pull_report_progress(i, TAR_FINALIZING);
349
350 r = import_make_read_only(i->temp_path);
351 if (r < 0)
352 goto finish;
353
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");
357 goto finish;
358 }
359
360 i->temp_path = mfree(i->temp_path);
361
362 if (i->settings_job &&
363 i->settings_job->error == 0) {
364
365 assert(i->settings_temp_path);
366 assert(i->settings_path);
367
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;
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 }
388 }
389
390 tar_pull_report_progress(i, TAR_COPYING);
391
392 r = tar_pull_make_local_copy(i);
393 if (r < 0)
394 goto finish;
395
396 r = 0;
397
398 finish:
399 if (i->on_finished)
400 i->on_finished(i, r, i->userdata);
401 else
402 sd_event_exit(i->event, r);
403 }
404
405 static int tar_pull_job_on_open_disk_tar(PullJob *j) {
406 TarPull *i;
407 int r;
408
409 assert(j);
410 assert(j->userdata);
411
412 i = j->userdata;
413 assert(i->tar_job == j);
414 assert(i->tar_pid <= 0);
415
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 }
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)
429 return log_error_errno(r, "Failed to create subvolume %s: %m", i->temp_path);
430 else
431 (void) import_assign_pool_quota_and_warn(i->temp_path);
432
433 j->disk_fd = import_fork_tar_x(i->temp_path, &i->tar_pid);
434 if (j->disk_fd < 0)
435 return j->disk_fd;
436
437 return 0;
438 }
439
440 static 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);
449
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 }
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
465 static void tar_pull_job_on_progress(PullJob *j) {
466 TarPull *i;
467
468 assert(j);
469 assert(j->userdata);
470
471 i = j->userdata;
472
473 tar_pull_report_progress(i, TAR_DOWNLOADING);
474 }
475
476 int 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
484 int r;
485
486 assert(i);
487 assert(verify < _IMPORT_VERIFY_MAX);
488 assert(verify >= 0);
489
490 if (!http_url_is_valid(url))
491 return -EINVAL;
492
493 if (local && !machine_name_is_valid(local))
494 return -EINVAL;
495
496 if (i->tar_job)
497 return -EBUSY;
498
499 r = free_and_strdup(&i->local, local);
500 if (r < 0)
501 return r;
502
503 i->force_local = force_local;
504 i->verify = verify;
505 i->settings = settings;
506
507 /* Set up download job for TAR file */
508 r = pull_job_new(&i->tar_job, url, i->glue, i);
509 if (r < 0)
510 return r;
511
512 i->tar_job->on_finished = tar_pull_job_on_finished;
513 i->tar_job->on_open_disk = tar_pull_job_on_open_disk_tar;
514 i->tar_job->on_progress = tar_pull_job_on_progress;
515 i->tar_job->calc_checksum = verify != IMPORT_VERIFY_NO;
516 i->tar_job->grow_machine_directory = i->grow_machine_directory;
517
518 r = pull_find_old_etags(url, i->image_root, DT_DIR, ".tar-", NULL, &i->tar_job->old_etags);
519 if (r < 0)
520 return r;
521
522 /* Set up download job for the settings file (.nspawn) */
523 if (settings) {
524 r = pull_make_auxiliary_job(&i->settings_job, url, tar_strip_suffixes, ".nspawn", i->glue, tar_pull_job_on_finished, i);
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;
531 }
532
533 /* Set up download of checksum/signature files */
534 r = pull_make_verification_jobs(&i->checksum_job, &i->signature_job, verify, url, i->glue, tar_pull_job_on_finished, i);
535 if (r < 0)
536 return r;
537
538 r = pull_job_begin(i->tar_job);
539 if (r < 0)
540 return r;
541
542 if (i->settings_job) {
543 r = pull_job_begin(i->settings_job);
544 if (r < 0)
545 return r;
546 }
547
548 if (i->checksum_job) {
549 i->checksum_job->on_progress = tar_pull_job_on_progress;
550
551 r = pull_job_begin(i->checksum_job);
552 if (r < 0)
553 return r;
554 }
555
556 if (i->signature_job) {
557 i->signature_job->on_progress = tar_pull_job_on_progress;
558
559 r = pull_job_begin(i->signature_job);
560 if (r < 0)
561 return r;
562 }
563
564 return 0;
565 }