pull-raw.c revision 0d39fa9c69b97a2ceb156053deef69c0866c2b97
/*-*- 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 "sd-daemon.h"
#include "btrfs-util.h"
#include "copy.h"
#include "curl-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "hostname-util.h"
#include "import-common.h"
#include "import-util.h"
#include "macro.h"
#include "mkdir.h"
#include "path-util.h"
#include "pull-common.h"
#include "pull-job.h"
#include "pull-raw.h"
#include "qcow2-util.h"
#include "rm-rf.h"
#include "string-util.h"
#include "strv.h"
#include "utf8.h"
#include "util.h"
typedef enum RawProgress {
} RawProgress;
struct RawPull {
char *image_root;
void *userdata;
char *local;
bool force_local;
bool grow_machine_directory;
bool settings;
char *final_path;
char *temp_path;
char *settings_path;
char *settings_temp_path;
};
if (!i)
return NULL;
pull_job_unref(i->raw_job);
curl_glue_unref(i->glue);
sd_event_unref(i->event);
if (i->temp_path) {
}
if (i->settings_temp_path) {
(void) unlink(i->settings_temp_path);
free(i->settings_temp_path);
}
free(i->final_path);
free(i->settings_path);
free(i->image_root);
free(i);
return NULL;
}
int raw_pull_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;
}
unsigned percent;
assert(i);
switch (p) {
case RAW_DOWNLOADING: {
unsigned remain = 80;
percent = 0;
if (i->settings_job) {
remain -= 5;
}
if (i->checksum_job) {
remain -= 5;
}
if (i->signature_job) {
remain -= 5;
}
if (i->raw_job)
break;
}
case RAW_VERIFYING:
percent = 80;
break;
case RAW_UNPACKING:
percent = 85;
break;
case RAW_FINALIZING:
percent = 90;
break;
case RAW_COPYING:
percent = 95;
break;
default:
assert_not_reached("Unknown progress state");
}
}
static int raw_pull_maybe_convert_qcow2(RawPull *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 */
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_pull_make_local_copy(RawPull *i) {
const char *p;
int r;
assert(i);
if (!i->local)
return 0;
if (!i->final_path) {
r = pull_make_path(i->raw_job->url, i->raw_job->etag, i->image_root, ".raw-", ".raw", &i->final_path);
if (r < 0)
return log_oom();
}
if (i->raw_job->etag_exists) {
/* We have downloaded this one previously, reopen it */
} else {
/* We freshly downloaded the image, use it */
}
if (i->force_local)
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) {
}
if (i->settings) {
const char *local_settings;
assert(i->settings_job);
if (!i->settings_path) {
r = pull_make_path(i->settings_job->url, i->settings_job->etag, i->image_root, ".settings-", NULL, &i->settings_path);
if (r < 0)
return log_oom();
}
if (r == -EEXIST)
else if (r < 0 && r != -ENOENT)
else
}
return 0;
}
static bool raw_pull_is_done(RawPull *i) {
assert(i);
if (!PULL_JOB_IS_COMPLETE(i->raw_job))
return false;
return false;
return false;
return false;
return true;
}
static void raw_pull_job_on_finished(PullJob *j) {
RawPull *i;
int r;
assert(j);
i = j->userdata;
if (j == i->settings_job) {
if (j->error != 0)
} else 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_pull_is_done(i))
return;
if (i->settings_job)
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_pull_maybe_convert_qcow2(i);
if (r < 0)
goto finish;
if (r < 0)
goto finish;
if (r < 0) {
log_error_errno(r, "Failed to move RAW file into place: %m");
goto finish;
}
if (i->settings_job &&
i->settings_job->error == 0 &&
!i->settings_job->etag_exists) {
assert(i->settings_temp_path);
assert(i->settings_path);
r = import_make_read_only(i->settings_temp_path);
if (r < 0)
goto finish;
if (r < 0) {
log_error_errno(r, "Failed to rename settings file: %m");
goto finish;
}
}
}
r = raw_pull_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_pull_job_on_open_disk_raw(PullJob *j) {
RawPull *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;
}
static int raw_pull_job_on_open_disk_settings(PullJob *j) {
RawPull *i;
int r;
assert(j);
i = j->userdata;
assert(i->settings_job == j);
assert(!i->settings_path);
assert(!i->settings_temp_path);
if (r < 0)
return log_oom();
if (r < 0)
return log_oom();
if (j->disk_fd < 0)
return 0;
}
static void raw_pull_job_on_progress(PullJob *j) {
RawPull *i;
assert(j);
i = j->userdata;
}
int raw_pull_start(
RawPull *i,
const char *url,
const char *local,
bool force_local,
bool settings) {
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;
if (settings) {
if (r < 0)
return r;
r = pull_find_old_etags(i->settings_job->url, i->image_root, DT_REG, ".settings-", NULL, &i->settings_job->old_etags);
if (r < 0)
return r;
}
r = pull_make_verification_jobs(&i->checksum_job, &i->signature_job, verify, url, i->glue, raw_pull_job_on_finished, i);
if (r < 0)
return r;
r = pull_job_begin(i->raw_job);
if (r < 0)
return r;
if (i->settings_job) {
r = pull_job_begin(i->settings_job);
if (r < 0)
return r;
}
if (i->checksum_job) {
r = pull_job_begin(i->checksum_job);
if (r < 0)
return r;
}
if (i->signature_job) {
r = pull_job_begin(i->signature_job);
if (r < 0)
return r;
}
return 0;
}