| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <unistd.h> |
| 4 |
|
| 5 |
#include "sd-daemon.h" |
| 6 |
#include "sd-event.h" |
| 7 |
#include "sd-varlink.h" |
| 8 |
|
| 9 |
#include "alloc-util.h" |
| 10 |
#include "btrfs-util.h" |
| 11 |
#include "copy.h" |
| 12 |
#include "curl-util.h" |
| 13 |
#include "dissect-image.h" |
| 14 |
#include "errno-util.h" |
| 15 |
#include "fd-util.h" |
| 16 |
#include "fs-util.h" |
| 17 |
#include "install-file.h" |
| 18 |
#include "log.h" |
| 19 |
#include "mkdir.h" |
| 20 |
#include "path-util.h" |
| 21 |
#include "pidref.h" |
| 22 |
#include "pretty-print.h" |
| 23 |
#include "process-util.h" |
| 24 |
#include "pull-common.h" |
| 25 |
#include "pull-job.h" |
| 26 |
#include "pull-tar.h" |
| 27 |
#include "ratelimit.h" |
| 28 |
#include "rm-rf.h" |
| 29 |
#include "string-util.h" |
| 30 |
#include "terminal-util.h" |
| 31 |
#include "time-util.h" |
| 32 |
#include "tmpfile-util.h" |
| 33 |
#include "uid-classification.h" |
| 34 |
#include "web-util.h" |
| 35 |
|
| 36 |
typedef enum TarProgress { |
| 37 |
TAR_DOWNLOADING, |
| 38 |
TAR_VERIFYING, |
| 39 |
TAR_FINALIZING, |
| 40 |
TAR_COPYING, |
| 41 |
} TarProgress; |
| 42 |
|
| 43 |
typedef struct TarPull { |
| 44 |
sd_event *event; |
| 45 |
CurlGlue *glue; |
| 46 |
|
| 47 |
ImportFlags flags; |
| 48 |
ImportVerify verify; |
| 49 |
char *image_root; |
| 50 |
|
| 51 |
PullJob *tar_job; |
| 52 |
PullJob *checksum_job; |
| 53 |
PullJob *signature_job; |
| 54 |
PullJob *settings_job; |
| 55 |
|
| 56 |
TarPullFinished on_finished; |
| 57 |
void *userdata; |
| 58 |
|
| 59 |
char *local; |
| 60 |
|
| 61 |
PidRef tar_pid; |
| 62 |
|
| 63 |
char *final_path; |
| 64 |
char *temp_path; |
| 65 |
|
| 66 |
char *settings_path; |
| 67 |
char *settings_temp_path; |
| 68 |
|
| 69 |
int tree_fd; |
| 70 |
int userns_fd; |
| 71 |
|
| 72 |
unsigned last_percent; |
| 73 |
RateLimit progress_ratelimit; |
| 74 |
} TarPull; |
| 75 |
|
| 76 |
TarPull* tar_pull_unref(TarPull *p) { |
| 77 |
if (!p) |
| 78 |
return NULL; |
| 79 |
|
| 80 |
pidref_done_sigkill_wait(&p->tar_pid); |
| 81 |
|
| 82 |
pull_job_unref(p->tar_job); |
| 83 |
pull_job_unref(p->checksum_job); |
| 84 |
pull_job_unref(p->signature_job); |
| 85 |
pull_job_unref(p->settings_job); |
| 86 |
|
| 87 |
curl_glue_unref(p->glue); |
| 88 |
sd_event_unref(p->event); |
| 89 |
|
| 90 |
if (p->temp_path) { |
| 91 |
import_remove_tree(p->temp_path, &p->userns_fd, p->flags); |
| 92 |
free(p->temp_path); |
| 93 |
} |
| 94 |
unlink_and_free(p->settings_temp_path); |
| 95 |
|
| 96 |
free(p->final_path); |
| 97 |
free(p->settings_path); |
| 98 |
free(p->image_root); |
| 99 |
free(p->local); |
| 100 |
|
| 101 |
safe_close(p->tree_fd); |
| 102 |
safe_close(p->userns_fd); |
| 103 |
|
| 104 |
return mfree(p); |
| 105 |
} |
| 106 |
|
| 107 |
int tar_pull_new( |
| 108 |
TarPull **ret, |
| 109 |
sd_event *event, |
| 110 |
const char *image_root, |
| 111 |
TarPullFinished on_finished, |
| 112 |
void *userdata) { |
| 113 |
|
| 114 |
_cleanup_(curl_glue_unrefp) CurlGlue *g = NULL; |
| 115 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 116 |
_cleanup_(tar_pull_unrefp) TarPull *p = NULL; |
| 117 |
_cleanup_free_ char *root = NULL; |
| 118 |
int r; |
| 119 |
|
| 120 |
assert(image_root); |
| 121 |
assert(ret); |
| 122 |
|
| 123 |
root = strdup(image_root); |
| 124 |
if (!root) |
| 125 |
return -ENOMEM; |
| 126 |
|
| 127 |
if (event) |
| 128 |
e = sd_event_ref(event); |
| 129 |
else { |
| 130 |
r = sd_event_default(&e); |
| 131 |
if (r < 0) |
| 132 |
return r; |
| 133 |
} |
| 134 |
|
| 135 |
r = curl_glue_new(&g, e); |
| 136 |
if (r < 0) |
| 137 |
return r; |
| 138 |
|
| 139 |
p = new(TarPull, 1); |
| 140 |
if (!p) |
| 141 |
return -ENOMEM; |
| 142 |
|
| 143 |
*p = (TarPull) { |
| 144 |
.on_finished = on_finished, |
| 145 |
.userdata = userdata, |
| 146 |
.image_root = TAKE_PTR(root), |
| 147 |
.event = TAKE_PTR(e), |
| 148 |
.glue = TAKE_PTR(g), |
| 149 |
.tar_pid = PIDREF_NULL, |
| 150 |
.tree_fd = -EBADF, |
| 151 |
.userns_fd = -EBADF, |
| 152 |
.last_percent = UINT_MAX, |
| 153 |
.progress_ratelimit = { 100 * USEC_PER_MSEC, 1 }, |
| 154 |
}; |
| 155 |
|
| 156 |
*ret = TAKE_PTR(p); |
| 157 |
|
| 158 |
return 0; |
| 159 |
} |
| 160 |
|
| 161 |
static void tar_pull_report_progress(TarPull *p, TarProgress progress) { |
| 162 |
unsigned percent; |
| 163 |
|
| 164 |
assert(p); |
| 165 |
|
| 166 |
switch (progress) { |
| 167 |
|
| 168 |
case TAR_DOWNLOADING: { |
| 169 |
unsigned remain = 85; |
| 170 |
|
| 171 |
percent = 0; |
| 172 |
|
| 173 |
if (p->checksum_job) { |
| 174 |
percent += p->checksum_job->progress_percent * 5 / 100; |
| 175 |
remain -= 5; |
| 176 |
} |
| 177 |
|
| 178 |
if (p->signature_job) { |
| 179 |
percent += p->signature_job->progress_percent * 5 / 100; |
| 180 |
remain -= 5; |
| 181 |
} |
| 182 |
|
| 183 |
if (p->settings_job) { |
| 184 |
percent += p->settings_job->progress_percent * 5 / 100; |
| 185 |
remain -= 5; |
| 186 |
} |
| 187 |
|
| 188 |
if (p->tar_job) |
| 189 |
percent += p->tar_job->progress_percent * remain / 100; |
| 190 |
break; |
| 191 |
} |
| 192 |
|
| 193 |
case TAR_VERIFYING: |
| 194 |
percent = 85; |
| 195 |
break; |
| 196 |
|
| 197 |
case TAR_FINALIZING: |
| 198 |
percent = 90; |
| 199 |
break; |
| 200 |
|
| 201 |
case TAR_COPYING: |
| 202 |
percent = 95; |
| 203 |
break; |
| 204 |
|
| 205 |
default: |
| 206 |
assert_not_reached(); |
| 207 |
} |
| 208 |
|
| 209 |
if (percent == p->last_percent) |
| 210 |
return; |
| 211 |
|
| 212 |
if (!ratelimit_below(&p->progress_ratelimit)) |
| 213 |
return; |
| 214 |
|
| 215 |
sd_notifyf(false, "X_IMPORT_PROGRESS=%u%%", percent); |
| 216 |
|
| 217 |
if (isatty_safe(STDERR_FILENO)) |
| 218 |
draw_progress_bar("Total:", percent); |
| 219 |
|
| 220 |
log_debug("Combined progress %u%%", percent); |
| 221 |
|
| 222 |
p->last_percent = percent; |
| 223 |
} |
| 224 |
|
| 225 |
static int tar_pull_determine_path( |
| 226 |
TarPull *p, |
| 227 |
const char *suffix, |
| 228 |
char **field /* input + output (!) */) { |
| 229 |
int r; |
| 230 |
|
| 231 |
assert(p); |
| 232 |
assert(field); |
| 233 |
|
| 234 |
if (*field) |
| 235 |
return 0; |
| 236 |
|
| 237 |
assert(p->tar_job); |
| 238 |
|
| 239 |
r = pull_make_path(p->tar_job->url, p->tar_job->etag, p->image_root, ".tar-", suffix, field); |
| 240 |
if (r < 0) |
| 241 |
return log_oom(); |
| 242 |
|
| 243 |
return 1; |
| 244 |
} |
| 245 |
|
| 246 |
static int tar_pull_make_local_copy(TarPull *p) { |
| 247 |
_cleanup_(rm_rf_subvolume_and_freep) char *t = NULL; |
| 248 |
_cleanup_free_ char *path = NULL; |
| 249 |
const char *source; |
| 250 |
int r; |
| 251 |
|
| 252 |
assert(p); |
| 253 |
assert(p->tar_job); |
| 254 |
|
| 255 |
if (!p->local) |
| 256 |
return 0; |
| 257 |
|
| 258 |
/* Creates a copy/clone of the original downloaded version (which is supposed to remain untouched) |
| 259 |
* under a local image name (which may then be modified) */ |
| 260 |
|
| 261 |
assert(p->final_path); |
| 262 |
|
| 263 |
path = path_join(p->image_root, p->local); |
| 264 |
if (!path) |
| 265 |
return log_oom(); |
| 266 |
|
| 267 |
if (FLAGS_SET(p->flags, IMPORT_PULL_KEEP_DOWNLOAD)) { |
| 268 |
r = tempfn_random(path, NULL, &t); |
| 269 |
if (r < 0) |
| 270 |
return log_error_errno(r, "Failed to generate temporary filename for %s: %m", path); |
| 271 |
|
| 272 |
if (FLAGS_SET(p->flags, IMPORT_FOREIGN_UID)) { |
| 273 |
/* Copy in userns */ |
| 274 |
|
| 275 |
r = import_make_foreign_userns(&p->userns_fd); |
| 276 |
if (r < 0) |
| 277 |
return r; |
| 278 |
|
| 279 |
_cleanup_(sd_varlink_unrefp) sd_varlink *mountfsd_link = NULL; |
| 280 |
r = mountfsd_connect(&mountfsd_link); |
| 281 |
if (r < 0) |
| 282 |
return log_error_errno(r, "Failed to connect to mountfsd: %m"); |
| 283 |
|
| 284 |
/* Usually, tar_pull_job_on_open_disk_tar() would allocate ->tree_fd for us, but if |
| 285 |
* already downloaded the image before, and are just making a copy of the original |
| 286 |
* download, we need to open ->tree_fd now */ |
| 287 |
if (p->tree_fd < 0) { |
| 288 |
_cleanup_close_ int directory_fd = open(p->final_path, O_DIRECTORY|O_CLOEXEC); |
| 289 |
if (directory_fd < 0) |
| 290 |
return log_error_errno(errno, "Failed to open '%s': %m", p->final_path); |
| 291 |
|
| 292 |
struct stat st; |
| 293 |
if (fstat(directory_fd, &st) < 0) |
| 294 |
return log_error_errno(errno, "Failed to stat '%s': %m", p->final_path); |
| 295 |
|
| 296 |
if (!uid_is_foreign(st.st_uid)) |
| 297 |
return log_error_errno( |
| 298 |
SYNTHETIC_ERRNO(EINVAL), |
| 299 |
"Image tree '%s' is not owned by the foreign UID range, refusing.", |
| 300 |
p->final_path); |
| 301 |
|
| 302 |
r = mountfsd_mount_directory_fd( |
| 303 |
mountfsd_link, |
| 304 |
directory_fd, |
| 305 |
p->userns_fd, |
| 306 |
DISSECT_IMAGE_FOREIGN_UID, |
| 307 |
&p->tree_fd); |
| 308 |
if (r < 0) |
| 309 |
return log_error_errno(r, "Failed to mount directory via mountfsd: %m"); |
| 310 |
} |
| 311 |
|
| 312 |
_cleanup_close_ int directory_fd = -EBADF; |
| 313 |
r = mountfsd_make_directory( |
| 314 |
mountfsd_link, |
| 315 |
t, |
| 316 |
MODE_INVALID, |
| 317 |
/* flags= */ 0, |
| 318 |
&directory_fd); |
| 319 |
if (r < 0) |
| 320 |
return log_error_errno(r, "Failed to make directory via mountfsd: %m"); |
| 321 |
|
| 322 |
_cleanup_close_ int copy_fd = -EBADF; |
| 323 |
r = mountfsd_mount_directory_fd( |
| 324 |
mountfsd_link, |
| 325 |
directory_fd, |
| 326 |
p->userns_fd, |
| 327 |
DISSECT_IMAGE_FOREIGN_UID, |
| 328 |
©_fd); |
| 329 |
if (r < 0) |
| 330 |
return log_error_errno(r, "Failed to mount directory via mountfsd: %m"); |
| 331 |
|
| 332 |
r = copy_tree_at_foreign(p->tree_fd, copy_fd, p->userns_fd); |
| 333 |
if (r < 0) |
| 334 |
return r; |
| 335 |
} else { |
| 336 |
/* Copy locally */ |
| 337 |
if (p->flags & IMPORT_BTRFS_SUBVOL) |
| 338 |
r = btrfs_subvol_snapshot_at( |
| 339 |
AT_FDCWD, p->final_path, |
| 340 |
AT_FDCWD, t, |
| 341 |
(p->flags & IMPORT_BTRFS_QUOTA ? BTRFS_SNAPSHOT_QUOTA : 0)| |
| 342 |
BTRFS_SNAPSHOT_FALLBACK_COPY| |
| 343 |
BTRFS_SNAPSHOT_FALLBACK_DIRECTORY| |
| 344 |
BTRFS_SNAPSHOT_RECURSIVE); |
| 345 |
else |
| 346 |
r = copy_tree(p->final_path, t, UID_INVALID, GID_INVALID, COPY_REFLINK|COPY_HARDLINKS, NULL, NULL); |
| 347 |
if (r < 0) |
| 348 |
return log_error_errno(r, "Failed to create original download image: %m"); |
| 349 |
} |
| 350 |
|
| 351 |
source = t; |
| 352 |
} else |
| 353 |
source = p->final_path; |
| 354 |
|
| 355 |
r = install_file(AT_FDCWD, source, |
| 356 |
AT_FDCWD, path, |
| 357 |
(p->flags & IMPORT_FORCE ? INSTALL_REPLACE : 0) | |
| 358 |
(p->flags & IMPORT_READ_ONLY ? INSTALL_READ_ONLY|INSTALL_GRACEFUL : 0) | |
| 359 |
(p->flags & IMPORT_SYNC ? INSTALL_SYNCFS|INSTALL_GRACEFUL : 0)); |
| 360 |
if (r < 0) |
| 361 |
return log_error_errno(r, "Failed to install local image '%s': %m", path); |
| 362 |
|
| 363 |
t = mfree(t); |
| 364 |
|
| 365 |
clear_progress_bar(/* prefix= */ NULL); |
| 366 |
log_info("Created new local image '%s'.", p->local); |
| 367 |
|
| 368 |
if (FLAGS_SET(p->flags, IMPORT_PULL_SETTINGS)) { |
| 369 |
_cleanup_free_ char *local_settings = NULL; |
| 370 |
assert(p->settings_job); |
| 371 |
|
| 372 |
r = tar_pull_determine_path(p, ".nspawn", &p->settings_path); |
| 373 |
if (r < 0) |
| 374 |
return r; |
| 375 |
|
| 376 |
local_settings = strjoin(p->image_root, "/", p->local, ".nspawn"); |
| 377 |
if (!local_settings) |
| 378 |
return log_oom(); |
| 379 |
|
| 380 |
if (FLAGS_SET(p->flags, IMPORT_PULL_KEEP_DOWNLOAD)) |
| 381 |
r = copy_file_atomic( |
| 382 |
p->settings_path, |
| 383 |
local_settings, |
| 384 |
0664, |
| 385 |
COPY_REFLINK | |
| 386 |
(FLAGS_SET(p->flags, IMPORT_FORCE) ? COPY_REPLACE : 0) | |
| 387 |
(FLAGS_SET(p->flags, IMPORT_SYNC) ? COPY_FSYNC_FULL : 0)); |
| 388 |
else |
| 389 |
r = install_file(AT_FDCWD, p->settings_path, |
| 390 |
AT_FDCWD, local_settings, |
| 391 |
(p->flags & IMPORT_FORCE ? INSTALL_REPLACE : 0) | |
| 392 |
(p->flags & IMPORT_SYNC ? INSTALL_SYNCFS : 0)); |
| 393 |
if (r == -EEXIST) |
| 394 |
log_warning_errno(r, "Settings file %s already exists, not replacing.", local_settings); |
| 395 |
else if (r == -ENOENT) |
| 396 |
log_debug_errno(r, "Skipping creation of settings file, since none was found."); |
| 397 |
else if (r < 0) |
| 398 |
log_warning_errno(r, "Failed to install settings files %s, ignoring: %m", local_settings); |
| 399 |
else |
| 400 |
log_info("Created new settings file %s.", local_settings); |
| 401 |
} |
| 402 |
|
| 403 |
return 0; |
| 404 |
} |
| 405 |
|
| 406 |
static bool tar_pull_is_done(TarPull *p) { |
| 407 |
assert(p); |
| 408 |
assert(p->tar_job); |
| 409 |
|
| 410 |
if (!PULL_JOB_IS_COMPLETE(p->tar_job)) |
| 411 |
return false; |
| 412 |
if (p->checksum_job && !PULL_JOB_IS_COMPLETE(p->checksum_job)) |
| 413 |
return false; |
| 414 |
if (p->signature_job && !PULL_JOB_IS_COMPLETE(p->signature_job)) |
| 415 |
return false; |
| 416 |
if (p->settings_job && !PULL_JOB_IS_COMPLETE(p->settings_job)) |
| 417 |
return false; |
| 418 |
|
| 419 |
return true; |
| 420 |
} |
| 421 |
|
| 422 |
static void tar_pull_job_on_finished(PullJob *j) { |
| 423 |
int r; |
| 424 |
|
| 425 |
assert(j); |
| 426 |
TarPull *p = ASSERT_PTR(j->userdata); |
| 427 |
|
| 428 |
if (j->error != 0) { |
| 429 |
clear_progress_bar(/* prefix= */ NULL); |
| 430 |
|
| 431 |
if (j == p->tar_job) { |
| 432 |
if (j->error == -ENOMEDIUM) /* HTTP 404 */ |
| 433 |
r = log_error_errno(j->error, "Failed to retrieve image file. (Wrong URL?)"); |
| 434 |
else |
| 435 |
r = log_error_errno(j->error, "Failed to retrieve image file."); |
| 436 |
goto finish; |
| 437 |
} else if (j == p->checksum_job) { |
| 438 |
r = log_error_errno(j->error, "Failed to retrieve SHA256 checksum, cannot verify. (Try --verify=no?)"); |
| 439 |
goto finish; |
| 440 |
} else if (j == p->signature_job) |
| 441 |
log_debug_errno(j->error, "Signature job for %s failed, proceeding for now.", j->url); |
| 442 |
else if (j == p->settings_job) |
| 443 |
log_info_errno(j->error, "Settings file could not be retrieved, proceeding without."); |
| 444 |
else |
| 445 |
assert_not_reached(); |
| 446 |
} |
| 447 |
|
| 448 |
/* This is invoked if either the download completed successfully, or the download was skipped because |
| 449 |
* we already have the etag. */ |
| 450 |
|
| 451 |
if (!tar_pull_is_done(p)) |
| 452 |
return; |
| 453 |
|
| 454 |
if (p->signature_job && p->signature_job->error != 0) { |
| 455 |
assert(p->checksum_job); |
| 456 |
|
| 457 |
r = log_error_errno(p->signature_job->error, |
| 458 |
"Failed to retrieve signature file, cannot verify. (Try --verify=no?)"); |
| 459 |
goto finish; |
| 460 |
} |
| 461 |
|
| 462 |
pull_job_close_disk_fd(p->tar_job); |
| 463 |
pull_job_close_disk_fd(p->settings_job); |
| 464 |
|
| 465 |
if (pidref_is_set(&p->tar_pid)) { |
| 466 |
r = pidref_wait_for_terminate_and_check("tar", &p->tar_pid, WAIT_LOG); |
| 467 |
if (r < 0) |
| 468 |
goto finish; |
| 469 |
pidref_done(&p->tar_pid); |
| 470 |
if (r != EXIT_SUCCESS) { |
| 471 |
r = -EIO; |
| 472 |
goto finish; |
| 473 |
} |
| 474 |
} |
| 475 |
|
| 476 |
if (!p->tar_job->etag_exists) { |
| 477 |
/* This is a new download, verify it, and move it into place */ |
| 478 |
|
| 479 |
tar_pull_report_progress(p, TAR_VERIFYING); |
| 480 |
|
| 481 |
clear_progress_bar(/* prefix= */ NULL); |
| 482 |
r = pull_verify(p->verify, |
| 483 |
p->tar_job, |
| 484 |
p->checksum_job, |
| 485 |
p->signature_job, |
| 486 |
p->settings_job, |
| 487 |
/* roothash_job= */ NULL, |
| 488 |
/* roothash_signature_job= */ NULL, |
| 489 |
/* verity_job= */ NULL); |
| 490 |
if (r < 0) |
| 491 |
goto finish; |
| 492 |
} |
| 493 |
|
| 494 |
if (p->flags & IMPORT_DIRECT) { |
| 495 |
assert(!p->settings_job); |
| 496 |
assert(p->local); |
| 497 |
assert(!p->temp_path); |
| 498 |
|
| 499 |
tar_pull_report_progress(p, TAR_FINALIZING); |
| 500 |
|
| 501 |
r = import_mangle_os_tree_fd(p->tree_fd, p->userns_fd, p->flags); |
| 502 |
if (r < 0) |
| 503 |
goto finish; |
| 504 |
|
| 505 |
r = install_file( |
| 506 |
AT_FDCWD, p->local, |
| 507 |
AT_FDCWD, NULL, |
| 508 |
(p->flags & IMPORT_READ_ONLY ? INSTALL_READ_ONLY|INSTALL_GRACEFUL : 0) | |
| 509 |
(p->flags & IMPORT_SYNC ? INSTALL_SYNCFS|INSTALL_GRACEFUL : 0)); |
| 510 |
if (r < 0) { |
| 511 |
log_error_errno(r, "Failed to finalize '%s': %m", p->local); |
| 512 |
goto finish; |
| 513 |
} |
| 514 |
} else { |
| 515 |
r = tar_pull_determine_path(p, NULL, &p->final_path); |
| 516 |
if (r < 0) |
| 517 |
goto finish; |
| 518 |
|
| 519 |
if (!p->tar_job->etag_exists) { |
| 520 |
/* This is a new download, verify it, and move it into place */ |
| 521 |
|
| 522 |
assert(p->temp_path); |
| 523 |
assert(p->final_path); |
| 524 |
|
| 525 |
tar_pull_report_progress(p, TAR_FINALIZING); |
| 526 |
|
| 527 |
r = import_mangle_os_tree_fd(p->tree_fd, p->userns_fd, p->flags); |
| 528 |
if (r < 0) |
| 529 |
goto finish; |
| 530 |
|
| 531 |
r = install_file( |
| 532 |
AT_FDCWD, p->temp_path, |
| 533 |
AT_FDCWD, p->final_path, |
| 534 |
(p->flags & IMPORT_PULL_KEEP_DOWNLOAD ? INSTALL_READ_ONLY|INSTALL_GRACEFUL : 0) | |
| 535 |
(p->flags & IMPORT_SYNC ? INSTALL_SYNCFS|INSTALL_GRACEFUL : 0)); |
| 536 |
if (r < 0) { |
| 537 |
log_error_errno(r, "Failed to rename to final image name to %s: %m", p->final_path); |
| 538 |
goto finish; |
| 539 |
} |
| 540 |
|
| 541 |
p->temp_path = mfree(p->temp_path); |
| 542 |
|
| 543 |
if (p->settings_job && |
| 544 |
p->settings_job->error == 0) { |
| 545 |
|
| 546 |
/* Also move the settings file into place, if it exists. Note that we do so only if we also |
| 547 |
* moved the tar file in place, to keep things strictly in sync. */ |
| 548 |
assert(p->settings_temp_path); |
| 549 |
|
| 550 |
/* Regenerate final name for this auxiliary file, we might know the etag of the file now, and |
| 551 |
* we should incorporate it in the file name if we can */ |
| 552 |
p->settings_path = mfree(p->settings_path); |
| 553 |
|
| 554 |
r = tar_pull_determine_path(p, ".nspawn", &p->settings_path); |
| 555 |
if (r < 0) |
| 556 |
goto finish; |
| 557 |
|
| 558 |
r = install_file( |
| 559 |
AT_FDCWD, p->settings_temp_path, |
| 560 |
AT_FDCWD, p->settings_path, |
| 561 |
INSTALL_READ_ONLY|INSTALL_GRACEFUL| |
| 562 |
(p->flags & IMPORT_SYNC ? INSTALL_FSYNC_FULL : 0)); |
| 563 |
if (r < 0) { |
| 564 |
log_error_errno(r, "Failed to rename settings file to %s: %m", p->settings_path); |
| 565 |
goto finish; |
| 566 |
} |
| 567 |
|
| 568 |
p->settings_temp_path = mfree(p->settings_temp_path); |
| 569 |
} |
| 570 |
} |
| 571 |
|
| 572 |
tar_pull_report_progress(p, TAR_COPYING); |
| 573 |
|
| 574 |
r = tar_pull_make_local_copy(p); |
| 575 |
if (r < 0) |
| 576 |
goto finish; |
| 577 |
} |
| 578 |
|
| 579 |
r = 0; |
| 580 |
|
| 581 |
finish: |
| 582 |
if (p->on_finished) |
| 583 |
p->on_finished(p, r, p->userdata); |
| 584 |
else |
| 585 |
sd_event_exit(p->event, r); |
| 586 |
} |
| 587 |
|
| 588 |
static int tar_pull_job_on_open_disk_tar(PullJob *j) { |
| 589 |
const char *where; |
| 590 |
int r; |
| 591 |
|
| 592 |
assert(j); |
| 593 |
|
| 594 |
TarPull *p = ASSERT_PTR(j->userdata); |
| 595 |
assert(p->tar_job == j); |
| 596 |
assert(!pidref_is_set(&p->tar_pid)); |
| 597 |
assert(p->tree_fd < 0); |
| 598 |
|
| 599 |
if (p->flags & IMPORT_DIRECT) |
| 600 |
where = p->local; |
| 601 |
else { |
| 602 |
if (!p->temp_path) { |
| 603 |
r = tempfn_random_child(p->image_root, "tar", &p->temp_path); |
| 604 |
if (r < 0) |
| 605 |
return log_oom(); |
| 606 |
} |
| 607 |
|
| 608 |
where = p->temp_path; |
| 609 |
} |
| 610 |
|
| 611 |
(void) mkdir_parents_label(where, 0700); |
| 612 |
|
| 613 |
if (FLAGS_SET(p->flags, IMPORT_DIRECT|IMPORT_FORCE)) |
| 614 |
(void) rm_rf(where, REMOVE_ROOT|REMOVE_PHYSICAL|REMOVE_SUBVOLUME); |
| 615 |
|
| 616 |
if (FLAGS_SET(p->flags, IMPORT_FOREIGN_UID)) { |
| 617 |
r = import_make_foreign_userns(&p->userns_fd); |
| 618 |
if (r < 0) |
| 619 |
return r; |
| 620 |
|
| 621 |
_cleanup_(sd_varlink_unrefp) sd_varlink *mountfsd_link = NULL; |
| 622 |
r = mountfsd_connect(&mountfsd_link); |
| 623 |
if (r < 0) |
| 624 |
return log_error_errno(r, "Failed to connect to mountfsd: %m"); |
| 625 |
|
| 626 |
_cleanup_close_ int directory_fd = -EBADF; |
| 627 |
r = mountfsd_make_directory( |
| 628 |
mountfsd_link, |
| 629 |
where, |
| 630 |
MODE_INVALID, |
| 631 |
/* flags= */ 0, |
| 632 |
&directory_fd); |
| 633 |
if (r < 0) |
| 634 |
return log_error_errno(r, "Failed to make directory via mountfsd: %m"); |
| 635 |
|
| 636 |
r = mountfsd_mount_directory_fd( |
| 637 |
mountfsd_link, |
| 638 |
directory_fd, |
| 639 |
p->userns_fd, |
| 640 |
DISSECT_IMAGE_FOREIGN_UID, |
| 641 |
&p->tree_fd); |
| 642 |
if (r < 0) |
| 643 |
return log_error_errno(r, "Failed to mount directory via mountfsd: %m"); |
| 644 |
} else { |
| 645 |
if (p->flags & IMPORT_BTRFS_SUBVOL) |
| 646 |
r = btrfs_subvol_make_fallback(AT_FDCWD, where, 0755); |
| 647 |
else |
| 648 |
r = RET_NERRNO(mkdir(where, 0755)); |
| 649 |
if (r == -EEXIST && (p->flags & IMPORT_DIRECT)) /* EEXIST is OK if in direct mode, but not otherwise, |
| 650 |
* because in that case our temporary path collided */ |
| 651 |
r = 0; |
| 652 |
if (r < 0) |
| 653 |
return log_error_errno(r, "Failed to create directory/subvolume %s: %m", where); |
| 654 |
|
| 655 |
if (r > 0 && (p->flags & IMPORT_BTRFS_QUOTA)) { /* actually btrfs subvol */ |
| 656 |
if (!(p->flags & IMPORT_DIRECT)) |
| 657 |
(void) import_assign_pool_quota_and_warn(p->image_root); |
| 658 |
(void) import_assign_pool_quota_and_warn(where); |
| 659 |
} |
| 660 |
|
| 661 |
p->tree_fd = open(where, O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW); |
| 662 |
if (p->tree_fd < 0) |
| 663 |
return log_error_errno(errno, "Failed to open '%s': %m", where); |
| 664 |
} |
| 665 |
|
| 666 |
j->disk_fd = import_fork_tar_x(p->tree_fd, p->userns_fd, &p->tar_pid); |
| 667 |
if (j->disk_fd < 0) |
| 668 |
return j->disk_fd; |
| 669 |
|
| 670 |
return 0; |
| 671 |
} |
| 672 |
|
| 673 |
static int tar_pull_job_on_open_disk_settings(PullJob *j) { |
| 674 |
TarPull *p; |
| 675 |
int r; |
| 676 |
|
| 677 |
assert(j); |
| 678 |
assert(j->userdata); |
| 679 |
|
| 680 |
p = j->userdata; |
| 681 |
assert(p->settings_job == j); |
| 682 |
|
| 683 |
if (!p->settings_temp_path) { |
| 684 |
r = tempfn_random_child(p->image_root, "settings", &p->settings_temp_path); |
| 685 |
if (r < 0) |
| 686 |
return log_oom(); |
| 687 |
} |
| 688 |
|
| 689 |
(void) mkdir_parents_label(p->settings_temp_path, 0700); |
| 690 |
|
| 691 |
j->disk_fd = open(p->settings_temp_path, O_RDWR|O_CREAT|O_EXCL|O_NOCTTY|O_CLOEXEC, 0664); |
| 692 |
if (j->disk_fd < 0) |
| 693 |
return log_error_errno(errno, "Failed to create %s: %m", p->settings_temp_path); |
| 694 |
|
| 695 |
return 0; |
| 696 |
} |
| 697 |
|
| 698 |
static void tar_pull_job_on_progress(PullJob *j) { |
| 699 |
TarPull *p; |
| 700 |
|
| 701 |
assert(j); |
| 702 |
assert(j->userdata); |
| 703 |
|
| 704 |
p = j->userdata; |
| 705 |
|
| 706 |
tar_pull_report_progress(p, TAR_DOWNLOADING); |
| 707 |
} |
| 708 |
|
| 709 |
int tar_pull_start( |
| 710 |
TarPull *p, |
| 711 |
const char *url, |
| 712 |
const char *local, |
| 713 |
ImportFlags flags, |
| 714 |
ImportVerify verify, |
| 715 |
const struct iovec *checksum) { |
| 716 |
|
| 717 |
int r; |
| 718 |
|
| 719 |
assert(p); |
| 720 |
assert(verify == _IMPORT_VERIFY_INVALID || verify < _IMPORT_VERIFY_MAX); |
| 721 |
assert(verify == _IMPORT_VERIFY_INVALID || verify >= 0); |
| 722 |
assert((verify < 0) || !iovec_is_set(checksum)); |
| 723 |
assert(!(flags & ~IMPORT_PULL_FLAGS_MASK_TAR)); |
| 724 |
assert(!(flags & IMPORT_PULL_SETTINGS) || !(flags & IMPORT_DIRECT)); |
| 725 |
assert(!(flags & IMPORT_PULL_SETTINGS) || !iovec_is_set(checksum)); |
| 726 |
|
| 727 |
if (!http_url_is_valid(url) && !file_url_is_valid(url)) |
| 728 |
return -EINVAL; |
| 729 |
|
| 730 |
if (local && !pull_validate_local(local, flags)) |
| 731 |
return -EINVAL; |
| 732 |
|
| 733 |
if (p->tar_job) |
| 734 |
return -EBUSY; |
| 735 |
|
| 736 |
r = free_and_strdup(&p->local, local); |
| 737 |
if (r < 0) |
| 738 |
return r; |
| 739 |
|
| 740 |
p->flags = flags; |
| 741 |
p->verify = verify; |
| 742 |
|
| 743 |
/* Set up download job for TAR file */ |
| 744 |
r = pull_job_new(&p->tar_job, url, p->glue, p); |
| 745 |
if (r < 0) |
| 746 |
return r; |
| 747 |
|
| 748 |
p->tar_job->on_finished = tar_pull_job_on_finished; |
| 749 |
p->tar_job->on_open_disk = tar_pull_job_on_open_disk_tar; |
| 750 |
|
| 751 |
if (iovec_is_set(checksum)) { |
| 752 |
if (!iovec_memdup(checksum, &p->tar_job->expected_checksum)) |
| 753 |
return -ENOMEM; |
| 754 |
|
| 755 |
p->tar_job->calc_checksum = true; |
| 756 |
} else |
| 757 |
p->tar_job->calc_checksum = verify != IMPORT_VERIFY_NO; |
| 758 |
|
| 759 |
if (!FLAGS_SET(flags, IMPORT_DIRECT)) { |
| 760 |
r = pull_find_old_etags(url, p->image_root, DT_DIR, ".tar-", NULL, &p->tar_job->old_etags); |
| 761 |
if (r < 0) |
| 762 |
return r; |
| 763 |
} |
| 764 |
|
| 765 |
/* Set up download of checksum/signature files */ |
| 766 |
r = pull_make_verification_jobs( |
| 767 |
&p->checksum_job, |
| 768 |
&p->signature_job, |
| 769 |
verify, |
| 770 |
url, |
| 771 |
p->glue, |
| 772 |
tar_pull_job_on_finished, |
| 773 |
p); |
| 774 |
if (r < 0) |
| 775 |
return r; |
| 776 |
|
| 777 |
/* Set up download job for the settings file (.nspawn) */ |
| 778 |
if (FLAGS_SET(flags, IMPORT_PULL_SETTINGS)) { |
| 779 |
r = pull_make_auxiliary_job( |
| 780 |
&p->settings_job, |
| 781 |
url, |
| 782 |
tar_strip_suffixes, |
| 783 |
".nspawn", |
| 784 |
verify, |
| 785 |
p->glue, |
| 786 |
tar_pull_job_on_open_disk_settings, |
| 787 |
tar_pull_job_on_finished, |
| 788 |
p); |
| 789 |
if (r < 0) |
| 790 |
return r; |
| 791 |
} |
| 792 |
|
| 793 |
PullJob *j; |
| 794 |
FOREACH_ARGUMENT(j, |
| 795 |
p->tar_job, |
| 796 |
p->checksum_job, |
| 797 |
p->signature_job, |
| 798 |
p->settings_job) { |
| 799 |
|
| 800 |
if (!j) |
| 801 |
continue; |
| 802 |
|
| 803 |
j->on_progress = tar_pull_job_on_progress; |
| 804 |
j->sync = FLAGS_SET(flags, IMPORT_SYNC); |
| 805 |
|
| 806 |
r = pull_job_begin(j); |
| 807 |
if (r < 0) |
| 808 |
return r; |
| 809 |
} |
| 810 |
|
| 811 |
return 0; |
| 812 |
} |