import-raw.c revision 3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2014 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include "utf8.h"
#include "strv.h"
#include "copy.h"
#include "btrfs-util.h"
#include "util.h"
#include "macro.h"
#include "mkdir.h"
#include "import-util.h"
#include "curl-util.h"
#include "qcow2-util.h"
#include "import-job.h"
#include "import-common.h"
#include "import-raw.h"
typedef struct RawImportFile RawImportFile;
struct RawImport {
char *image_root;
void *userdata;
char *local;
bool force_local;
char *temp_path;
char *final_path;
};
if (!i)
return NULL;
import_job_unref(i->raw_job);
curl_glue_unref(i->glue);
sd_event_unref(i->event);
if (i->temp_path) {
}
free(i->final_path);
free(i->image_root);
free(i);
return NULL;
}
int raw_import_new(
const char *image_root,
void *userdata) {
int r;
if (!i)
return -ENOMEM;
i->on_finished = on_finished;
if (!i->image_root)
return -ENOMEM;
if (event)
else {
r = sd_event_default(&i->event);
if (r < 0)
return r;
}
if (r < 0)
return r;
*ret = i;
i = NULL;
return 0;
}
static int raw_import_maybe_convert_qcow2(RawImport *i) {
_cleanup_free_ char *t = NULL;
int r;
assert(i);
if (r < 0)
return log_error_errno(r, "Failed to detect whether this is a QCOW2 image: %m");
if (r == 0)
return 0;
/* This is a QCOW2 image, let's convert it */
r = tempfn_random(i->final_path, &t);
if (r < 0)
return log_oom();
if (converted_fd < 0)
if (r < 0)
log_info("Unpacking QCOW2 file.");
if (r < 0) {
unlink(t);
return log_error_errno(r, "Failed to convert qcow2 image: %m");
}
i->temp_path = t;
t = NULL;
converted_fd = -1;
return 1;
}
static int raw_import_make_local_copy(RawImport *i) {
const char *p;
int r;
assert(i);
if (!i->local)
return 0;
if (i->raw_job->etag_exists) {
/* We have downloaded this one previously, reopen it */
if (!i->final_path) {
r = import_make_path(i->raw_job->url, i->raw_job->etag, i->image_root, ".raw-", ".raw", &i->final_path);
if (r < 0)
return log_oom();
}
} else {
/* We freshly downloaded the image, use it */
}
if (i->force_local) {
(void) btrfs_subvol_remove(p);
(void) rm_rf_dangerous(p, false, true, false);
}
r = tempfn_random(p, &tp);
if (r < 0)
return log_oom();
if (dfd < 0)
/* Turn off COW writing. This should greatly improve
* performance on COW file systems like btrfs, since it
* reduces fragmentation caused by not allowing in-place
* writes. */
if (r < 0)
if (r < 0) {
return log_error_errno(r, "Failed to make writable copy of image: %m");
}
if (r < 0) {
}
return 0;
}
static bool raw_import_is_done(RawImport *i) {
assert(i);
return false;
return false;
return false;
return true;
}
static void raw_import_job_on_finished(ImportJob *j) {
RawImport *i;
int r;
assert(j);
i = j->userdata;
if (j->error != 0) {
if (j == i->checksum_job)
else if (j == i->signature_job)
else
r = j->error;
goto finish;
}
/* This is invoked if either the download completed
* successfully, or the download was skipped because we
* already have the etag. In this case ->etag_exists is
* true.
*
* We only do something when we got all three files */
if (!raw_import_is_done(i))
return;
if (!i->raw_job->etag_exists) {
/* This is a new download, verify it, and move it into place */
if (r < 0)
goto finish;
r = raw_import_maybe_convert_qcow2(i);
if (r < 0)
goto finish;
if (r < 0)
goto finish;
if (r < 0) {
goto finish;
}
}
r = raw_import_make_local_copy(i);
if (r < 0)
goto finish;
r = 0;
if (i->on_finished)
i->on_finished(i, r, i->userdata);
else
sd_event_exit(i->event, r);
}
static int raw_import_job_on_open_disk(ImportJob *j) {
RawImport *i;
int r;
assert(j);
i = j->userdata;
assert(!i->final_path);
if (r < 0)
return log_oom();
if (r <0)
return log_oom();
if (j->disk_fd < 0)
if (r < 0)
return 0;
}
int raw_import_pull(RawImport *i, const char *url, const char *local, bool force_local, ImportVerify verify) {
int r;
assert(i);
if (!http_url_is_valid(url))
return -EINVAL;
return -EINVAL;
if (i->raw_job)
return -EBUSY;
if (r < 0)
return r;
i->force_local = force_local;
/* Queue job for the image itself */
if (r < 0)
return r;
if (r < 0)
return r;
r = import_make_verification_jobs(&i->checksum_job, &i->signature_job, verify, url, i->glue, raw_import_job_on_finished, i);
if (r < 0)
return r;
r = import_job_begin(i->raw_job);
if (r < 0)
return r;
if (i->checksum_job) {
r = import_job_begin(i->checksum_job);
if (r < 0)
return r;
}
if (i->signature_job) {
r = import_job_begin(i->signature_job);
if (r < 0)
return r;
}
return 0;
}