import.c revision 0d6e763b48cabe8899a20823b015c9a988e38659
/*-*- 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 <getopt.h>
#include "sd-event.h"
#include "event-util.h"
#include "verbs.h"
#include "build.h"
#include "machine-image.h"
#include "import-tar.h"
#include "import-raw.h"
#include "import-dkr.h"
static bool arg_force = false;
static const char *arg_image_root = "/var/lib/machines";
static const char* arg_dkr_index_url = DEFAULT_DKR_INDEX_URL;
if (error == 0)
log_info("Operation completed successfully.");
else
}
const char *e, *p;
char *s;
e--;
p = e;
p--;
if (e <= p)
return -EINVAL;
s = strndup(p, e - p);
if (!s)
return -ENOMEM;
*ret = s;
return 0;
}
const char *e;
char *s;
if (!e)
if (!e)
if (!e)
if (!e)
if (e <= name)
return -EINVAL;
if (!s)
return -ENOMEM;
*ret = s;
return 0;
}
int r;
if (!http_url_is_valid(url)) {
return -EINVAL;
}
if (argc >= 3)
else {
r = url_final_component(url, &l);
if (r < 0)
return log_error_errno(r, "Failed get final component of URL: %m");
local = l;
}
if (local) {
if (r < 0)
return log_oom();
if (!machine_name_is_valid(local)) {
return -EINVAL;
}
if (!arg_force) {
if (r < 0)
else if (r > 0) {
return -EEXIST;
}
}
} else
r = sd_event_default(&event);
if (r < 0)
return log_error_errno(r, "Failed to allocate event loop: %m");
if (r < 0)
return log_error_errno(r, "Failed to allocate importer: %m");
if (r < 0)
return log_error_errno(r, "Failed to pull image: %m");
r = sd_event_loop(event);
if (r < 0)
return log_error_errno(r, "Failed to run event loop: %m");
log_info("Exiting.");
return 0;
}
if (error == 0)
log_info("Operation completed successfully.");
else
}
static int strip_raw_suffixes(const char *p, char **ret) {
static const char suffixes[] =
".xz\0"
".raw\0"
".qcow2\0"
".img\0";
_cleanup_free_ char *q = NULL;
q = strdup(p);
if (!q)
return -ENOMEM;
for (;;) {
const char *sfx;
bool changed = false;
char *e;
if (e) {
*e = 0;
changed = true;
}
}
if (!changed)
break;
}
*ret = q;
q = NULL;
return 0;
}
int r;
if (!http_url_is_valid(url)) {
return -EINVAL;
}
if (argc >= 3)
else {
r = url_final_component(url, &l);
if (r < 0)
return log_error_errno(r, "Failed get final component of URL: %m");
local = l;
}
if (local) {
if (r < 0)
return log_oom();
if (!machine_name_is_valid(local)) {
return -EINVAL;
}
if (!arg_force) {
if (r < 0)
else if (r > 0) {
return -EEXIST;
}
}
} else
r = sd_event_default(&event);
if (r < 0)
return log_error_errno(r, "Failed to allocate event loop: %m");
if (r < 0)
return log_error_errno(r, "Failed to allocate importer: %m");
if (r < 0)
return log_error_errno(r, "Failed to pull image: %m");
r = sd_event_loop(event);
if (r < 0)
return log_error_errno(r, "Failed to run event loop: %m");
log_info("Exiting.");
return 0;
}
if (error == 0)
log_info("Operation completed successfully.");
else
}
int r;
if (!arg_dkr_index_url) {
log_error("Please specify an index URL with --dkr-index-url=");
return -EINVAL;
}
if (tag) {
tag++;
} else {
tag = "latest";
}
if (!dkr_name_is_valid(name)) {
return -EINVAL;
}
if (!dkr_tag_is_valid(tag)) {
return -EINVAL;
}
if (argc >= 3)
else {
if (local)
local++;
else
}
if (local) {
const char *p;
if (!machine_name_is_valid(local)) {
return -EINVAL;
}
if (!arg_force) {
return 0;
}
} else
r = sd_event_default(&event);
if (r < 0)
return log_error_errno(r, "Failed to allocate event loop: %m");
if (r < 0)
return log_error_errno(r, "Failed to allocate importer: %m");
if (r < 0)
return log_error_errno(r, "Failed to pull image: %m");
r = sd_event_loop(event);
if (r < 0)
return log_error_errno(r, "Failed to run event loop: %m");
log_info("Exiting.");
return 0;
}
printf("%s [OPTIONS...] {COMMAND} ...\n\n"
"Import container or virtual machine image.\n\n"
" -h --help Show this help\n"
" --version Show package version\n"
" --force Force creation of image\n"
" --image-root= Image root directory\n"
" --dkr-index-url=URL Specify index URL to use for downloads\n\n"
"Commands:\n"
" pull-tar URL [NAME] Download a TAR image\n"
" pull-raw URL [NAME] Download a RAW image\n"
" pull-dkr REMOTE [NAME] Download a DKR image\n",
return 0;
}
enum {
ARG_VERSION = 0x100,
};
{}
};
int c;
switch (c) {
case 'h':
case ARG_VERSION:
return 0;
case ARG_FORCE:
arg_force = true;
break;
case ARG_DKR_INDEX_URL:
if (!dkr_url_is_valid(optarg)) {
return -EINVAL;
}
break;
case ARG_IMAGE_ROOT:
break;
case '?':
return -EINVAL;
default:
assert_not_reached("Unhandled option");
}
return 1;
}
{}
};
}
int r;
log_open();
if (r <= 0)
goto finish;
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}