import-raw.c revision 8f6950587ab7b4d6fe1b51241759cc3a4682b96d
/*-*- 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 <gcrypt.h>
#include "utf8.h"
#include "strv.h"
#include "copy.h"
#include "btrfs-util.h"
#include "util.h"
#include "macro.h"
#include "mkdir.h"
#include "curl-util.h"
#include "qcow2-util.h"
#include "import-job.h"
#include "import-util.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(RawImport **ret, sd_event *event, const char *image_root, RawImportFinished on_finished, 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 int raw_import_verify_sha256sum(RawImport *i) {
const char *p, *line;
int r;
assert(i);
assert(i->sha256sums_job);
if (r < 0)
return log_oom();
if (!filename_is_valid(fn)) {
log_error("Cannot verify checksum, could not determine valid server-side file name.");
return -EBADMSG;
}
line,
log_error("Checksum did not check out, payload has been tempered with.");
return -EBADMSG;
}
return 0;
}
static int raw_import_finalize(RawImport *i) {
int r;
assert(i);
if (!IMPORT_JOB_STATE_IS_COMPLETE(i->raw_job) ||
return 0;
if (i->verify != IMPORT_VERIFY_NO &&
i->raw_job->etag_exists) {
assert(i->final_path);
r = raw_import_verify_sha256sum(i);
if (r < 0)
return r;
if (r < 0)
}
r = raw_import_make_local_copy(i);
if (r < 0)
return r;
return 1;
}
static void raw_import_invoke_finished(RawImport *i, int r) {
assert(i);
if (i->on_finished)
i->on_finished(i, r, i->userdata);
else
sd_event_exit(i->event, r);
}
static void raw_import_raw_job_on_finished(ImportJob *j) {
RawImport *i;
int r;
assert(j);
i = j->userdata;
if (j->error != 0) {
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. */
if (!j->etag_exists) {
r = raw_import_maybe_convert_qcow2(i);
if (r < 0)
goto finish;
r = import_make_read_only_fd(j->disk_fd);
if (r < 0)
goto finish;
}
r = raw_import_finalize(i);
if (r < 0)
goto finish;
if (r == 0)
return;
r = 0;
raw_import_invoke_finished(i, r);
}
static void raw_import_sha256sums_job_on_finished(ImportJob *j) {
RawImport *i;
int r;
assert(j);
i = j->userdata;
if (j->error != 0) {
r = j->error;
goto finish;
}
r = raw_import_finalize(i);
if (r < 0)
goto finish;
if (r == 0)
return;
r = 0;
raw_import_invoke_finished(i, r);
}
static int raw_import_raw_job_on_open_disk(ImportJob *j) {
RawImport *i;
int r;
assert(j);
i = j->userdata;
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 (i->raw_job)
return -EBUSY;
if (!http_url_is_valid(url))
return -EINVAL;
return -EINVAL;
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;
if (verify != IMPORT_VERIFY_NO) {
/* Queue job for the SHA256SUMS file for the image */
if (r < 0)
return r;
if (r < 0)
return r;
r = import_job_begin(i->sha256sums_job);
if (r < 0)
return r;
}
r = import_job_begin(i->raw_job);
if (r < 0)
return r;
return 0;
}