1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2014 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/xattr.h>
24 #include <curl/curl.h>
26 #include "sd-daemon.h"
30 #include "btrfs-util.h"
34 #include "path-util.h"
35 #include "import-util.h"
36 #include "curl-util.h"
37 #include "qcow2-util.h"
38 #include "import-job.h"
39 #include "import-common.h"
40 #include "import-raw.h"
42 typedef enum RawProgress {
57 ImportJob *checksum_job;
58 ImportJob *signature_job;
60 RawImportFinished on_finished;
65 bool grow_machine_directory;
73 RawImport* raw_import_unref(RawImport *i) {
77 import_job_unref(i->raw_job);
78 import_job_unref(i->checksum_job);
79 import_job_unref(i->signature_job);
81 curl_glue_unref(i->glue);
82 sd_event_unref(i->event);
85 (void) unlink(i->temp_path);
100 const char *image_root,
101 RawImportFinished on_finished,
104 _cleanup_(raw_import_unrefp) RawImport *i = NULL;
109 i = new0(RawImport, 1);
113 i->on_finished = on_finished;
114 i->userdata = userdata;
116 i->image_root = strdup(image_root ?: "/var/lib/machines");
120 i->grow_machine_directory = path_startswith(i->image_root, "/var/lib/machines");
123 i->event = sd_event_ref(event);
125 r = sd_event_default(&i->event);
130 r = curl_glue_new(&i->glue, i->event);
134 i->glue->on_finished = import_job_curl_on_finished;
135 i->glue->userdata = i;
143 static void raw_import_report_progress(RawImport *i, RawProgress p) {
150 case RAW_DOWNLOADING: {
151 unsigned remain = 80;
155 if (i->checksum_job) {
156 percent += i->checksum_job->progress_percent * 5 / 100;
160 if (i->signature_job) {
161 percent += i->signature_job->progress_percent * 5 / 100;
166 percent += i->raw_job->progress_percent * remain / 100;
187 assert_not_reached("Unknown progress state");
190 sd_notifyf(false, "X_IMPORT_PROGRESS=%u", percent);
191 log_debug("Combined progress %u%%", percent);
194 static int raw_import_maybe_convert_qcow2(RawImport *i) {
195 _cleanup_close_ int converted_fd = -1;
196 _cleanup_free_ char *t = NULL;
202 r = qcow2_detect(i->raw_job->disk_fd);
204 return log_error_errno(r, "Failed to detect whether this is a QCOW2 image: %m");
208 /* This is a QCOW2 image, let's convert it */
209 r = tempfn_random(i->final_path, &t);
213 converted_fd = open(t, O_RDWR|O_CREAT|O_EXCL|O_NOCTTY|O_CLOEXEC, 0644);
214 if (converted_fd < 0)
215 return log_error_errno(errno, "Failed to create %s: %m", t);
217 r = chattr_fd(converted_fd, true, FS_NOCOW_FL);
219 log_warning_errno(errno, "Failed to set file attributes on %s: %m", t);
221 log_info("Unpacking QCOW2 file.");
223 r = qcow2_convert(i->raw_job->disk_fd, converted_fd);
226 return log_error_errno(r, "Failed to convert qcow2 image: %m");
229 unlink(i->temp_path);
235 safe_close(i->raw_job->disk_fd);
236 i->raw_job->disk_fd = converted_fd;
242 static int raw_import_make_local_copy(RawImport *i) {
243 _cleanup_free_ char *tp = NULL;
244 _cleanup_close_ int dfd = -1;
254 if (i->raw_job->etag_exists) {
255 /* We have downloaded this one previously, reopen it */
257 assert(i->raw_job->disk_fd < 0);
259 if (!i->final_path) {
260 r = import_make_path(i->raw_job->url, i->raw_job->etag, i->image_root, ".raw-", ".raw", &i->final_path);
265 i->raw_job->disk_fd = open(i->final_path, O_RDONLY|O_NOCTTY|O_CLOEXEC);
266 if (i->raw_job->disk_fd < 0)
267 return log_error_errno(errno, "Failed to open vendor image: %m");
269 /* We freshly downloaded the image, use it */
271 assert(i->raw_job->disk_fd >= 0);
273 if (lseek(i->raw_job->disk_fd, SEEK_SET, 0) == (off_t) -1)
274 return log_error_errno(errno, "Failed to seek to beginning of vendor image: %m");
277 p = strjoina(i->image_root, "/", i->local, ".raw");
279 if (i->force_local) {
280 (void) btrfs_subvol_remove(p);
281 (void) rm_rf_dangerous(p, false, true, false);
284 r = tempfn_random(p, &tp);
288 dfd = open(tp, O_WRONLY|O_CREAT|O_EXCL|O_NOCTTY|O_CLOEXEC, 0664);
290 return log_error_errno(errno, "Failed to create writable copy of image: %m");
292 /* Turn off COW writing. This should greatly improve
293 * performance on COW file systems like btrfs, since it
294 * reduces fragmentation caused by not allowing in-place
296 r = chattr_fd(dfd, true, FS_NOCOW_FL);
298 log_warning_errno(errno, "Failed to set file attributes on %s: %m", tp);
300 r = copy_bytes(i->raw_job->disk_fd, dfd, (off_t) -1, true);
303 return log_error_errno(r, "Failed to make writable copy of image: %m");
306 (void) copy_times(i->raw_job->disk_fd, dfd);
307 (void) copy_xattr(i->raw_job->disk_fd, dfd);
309 dfd = safe_close(dfd);
314 return log_error_errno(errno, "Failed to move writable image into place: %m");
317 log_info("Created new local image '%s'.", i->local);
321 static bool raw_import_is_done(RawImport *i) {
325 if (i->raw_job->state != IMPORT_JOB_DONE)
327 if (i->checksum_job && i->checksum_job->state != IMPORT_JOB_DONE)
329 if (i->signature_job && i->signature_job->state != IMPORT_JOB_DONE)
335 static void raw_import_job_on_finished(ImportJob *j) {
344 if (j == i->checksum_job)
345 log_error_errno(j->error, "Failed to retrieve SHA256 checksum, cannot verify. (Try --verify=no?)");
346 else if (j == i->signature_job)
347 log_error_errno(j->error, "Failed to retrieve signature file, cannot verify. (Try --verify=no?)");
349 log_error_errno(j->error, "Failed to retrieve image file. (Wrong URL?)");
355 /* This is invoked if either the download completed
356 * successfully, or the download was skipped because we
357 * already have the etag. In this case ->etag_exists is
360 * We only do something when we got all three files */
362 if (!raw_import_is_done(i))
365 if (!i->raw_job->etag_exists) {
366 /* This is a new download, verify it, and move it into place */
367 assert(i->raw_job->disk_fd >= 0);
369 raw_import_report_progress(i, RAW_VERIFYING);
371 r = import_verify(i->raw_job, i->checksum_job, i->signature_job);
375 raw_import_report_progress(i, RAW_UNPACKING);
377 r = raw_import_maybe_convert_qcow2(i);
381 raw_import_report_progress(i, RAW_FINALIZING);
383 r = import_make_read_only_fd(i->raw_job->disk_fd);
387 r = rename(i->temp_path, i->final_path);
389 r = log_error_errno(errno, "Failed to move RAW file into place: %m");
397 raw_import_report_progress(i, RAW_COPYING);
399 r = raw_import_make_local_copy(i);
407 i->on_finished(i, r, i->userdata);
409 sd_event_exit(i->event, r);
412 static int raw_import_job_on_open_disk(ImportJob *j) {
420 assert(i->raw_job == j);
421 assert(!i->final_path);
422 assert(!i->temp_path);
424 r = import_make_path(j->url, j->etag, i->image_root, ".raw-", ".raw", &i->final_path);
428 r = tempfn_random(i->final_path, &i->temp_path);
432 mkdir_parents_label(i->temp_path, 0700);
434 j->disk_fd = open(i->temp_path, O_RDWR|O_CREAT|O_EXCL|O_NOCTTY|O_CLOEXEC, 0644);
436 return log_error_errno(errno, "Failed to create %s: %m", i->temp_path);
438 r = chattr_fd(j->disk_fd, true, FS_NOCOW_FL);
440 log_warning_errno(errno, "Failed to set file attributes on %s: %m", i->temp_path);
445 static void raw_import_job_on_progress(ImportJob *j) {
453 raw_import_report_progress(i, RAW_DOWNLOADING);
456 int raw_import_pull(RawImport *i, const char *url, const char *local, bool force_local, ImportVerify verify) {
460 assert(verify < _IMPORT_VERIFY_MAX);
463 if (!http_url_is_valid(url))
466 if (local && !machine_name_is_valid(local))
472 r = free_and_strdup(&i->local, local);
475 i->force_local = force_local;
478 /* Queue job for the image itself */
479 r = import_job_new(&i->raw_job, url, i->glue, i);
483 i->raw_job->on_finished = raw_import_job_on_finished;
484 i->raw_job->on_open_disk = raw_import_job_on_open_disk;
485 i->raw_job->on_progress = raw_import_job_on_progress;
486 i->raw_job->calc_checksum = verify != IMPORT_VERIFY_NO;
487 i->raw_job->grow_machine_directory = i->grow_machine_directory;
489 r = import_find_old_etags(url, i->image_root, DT_REG, ".raw-", ".raw", &i->raw_job->old_etags);
493 r = import_make_verification_jobs(&i->checksum_job, &i->signature_job, verify, url, i->glue, raw_import_job_on_finished, i);
497 r = import_job_begin(i->raw_job);
501 if (i->checksum_job) {
502 i->checksum_job->on_progress = raw_import_job_on_progress;
504 r = import_job_begin(i->checksum_job);
509 if (i->signature_job) {
510 i->signature_job->on_progress = raw_import_job_on_progress;
512 r = import_job_begin(i->signature_job);