2020-11-09 05:23:58 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
2014-12-19 01:59:52 +01:00
|
|
|
|
|
|
|
#include <getopt.h>
|
2019-03-27 11:32:41 +01:00
|
|
|
#include <locale.h>
|
2014-12-19 01:59:52 +01:00
|
|
|
|
|
|
|
#include "sd-event.h"
|
2018-01-11 00:39:12 +01:00
|
|
|
#include "sd-id128.h"
|
2015-09-23 03:01:06 +02:00
|
|
|
|
2015-10-27 03:01:06 +01:00
|
|
|
#include "alloc-util.h"
|
2015-08-23 14:30:52 +02:00
|
|
|
#include "hostname-util.h"
|
2015-01-20 15:06:34 +01:00
|
|
|
#include "import-util.h"
|
2015-09-23 03:01:06 +02:00
|
|
|
#include "machine-image.h"
|
2018-11-19 20:26:37 +01:00
|
|
|
#include "main-func.h"
|
2015-10-26 16:18:16 +01:00
|
|
|
#include "parse-util.h"
|
2015-09-23 03:01:06 +02:00
|
|
|
#include "pull-raw.h"
|
|
|
|
#include "pull-tar.h"
|
|
|
|
#include "signal-util.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "string-util.h"
|
2015-09-23 03:01:06 +02:00
|
|
|
#include "verbs.h"
|
2015-10-27 00:41:29 +01:00
|
|
|
#include "web-util.h"
|
2014-12-19 01:59:52 +01:00
|
|
|
|
|
|
|
static bool arg_force = false;
|
2015-01-15 01:40:02 +01:00
|
|
|
static const char *arg_image_root = "/var/lib/machines";
|
2015-01-20 16:36:40 +01:00
|
|
|
static ImportVerify arg_verify = IMPORT_VERIFY_SIGNATURE;
|
2015-09-08 18:25:56 +02:00
|
|
|
static bool arg_settings = true;
|
2016-12-20 17:49:35 +01:00
|
|
|
static bool arg_roothash = true;
|
2014-12-22 19:42:27 +01:00
|
|
|
|
2015-01-22 03:57:15 +01:00
|
|
|
static int interrupt_signal_handler(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
|
|
|
|
log_notice("Transfer aborted.");
|
|
|
|
sd_event_exit(sd_event_source_get_event(s), EINTR);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-04 17:07:12 +01:00
|
|
|
static void on_tar_finished(TarPull *pull, int error, void *userdata) {
|
2015-01-20 01:36:11 +01:00
|
|
|
sd_event *event = userdata;
|
2015-03-04 17:07:12 +01:00
|
|
|
assert(pull);
|
2015-01-20 01:36:11 +01:00
|
|
|
|
|
|
|
if (error == 0)
|
|
|
|
log_info("Operation completed successfully.");
|
|
|
|
|
2015-01-22 03:57:15 +01:00
|
|
|
sd_event_exit(event, abs(error));
|
2015-01-20 01:36:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int pull_tar(int argc, char *argv[], void *userdata) {
|
2015-03-04 17:07:12 +01:00
|
|
|
_cleanup_(tar_pull_unrefp) TarPull *pull = NULL;
|
tree-wide: expose "p"-suffix unref calls in public APIs to make gcc cleanup easy
GLIB has recently started to officially support the gcc cleanup
attribute in its public API, hence let's do the same for our APIs.
With this patch we'll define an xyz_unrefp() call for each public
xyz_unref() call, to make it easy to use inside a
__attribute__((cleanup())) expression. Then, all code is ported over to
make use of this.
The new calls are also documented in the man pages, with examples how to
use them (well, I only added docs where the _unref() call itself already
had docs, and the examples, only cover sd_bus_unrefp() and
sd_event_unrefp()).
This also renames sd_lldp_free() to sd_lldp_unref(), since that's how we
tend to call our destructors these days.
Note that this defines no public macro that wraps gcc's attribute and
makes it easier to use. While I think it's our duty in the library to
make our stuff easy to use, I figure it's not our duty to make gcc's own
features easy to use on its own. Most likely, client code which wants to
make use of this should define its own:
#define _cleanup_(function) __attribute__((cleanup(function)))
Or similar, to make the gcc feature easier to use.
Making this logic public has the benefit that we can remove three header
files whose only purpose was to define these functions internally.
See #2008.
2015-11-27 19:13:45 +01:00
|
|
|
_cleanup_(sd_event_unrefp) sd_event *event = NULL;
|
2015-01-20 01:36:11 +01:00
|
|
|
const char *url, *local;
|
|
|
|
_cleanup_free_ char *l = NULL, *ll = NULL;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
url = argv[1];
|
2020-10-09 14:59:44 +02:00
|
|
|
if (!http_url_is_valid(url))
|
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
|
|
|
"URL '%s' is not valid.", url);
|
2015-01-20 01:36:11 +01:00
|
|
|
|
|
|
|
if (argc >= 3)
|
|
|
|
local = argv[2];
|
|
|
|
else {
|
2015-01-20 15:06:34 +01:00
|
|
|
r = import_url_last_component(url, &l);
|
2015-01-20 01:36:11 +01:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed get final component of URL: %m");
|
|
|
|
|
|
|
|
local = l;
|
|
|
|
}
|
|
|
|
|
2019-04-08 12:11:11 +02:00
|
|
|
local = empty_or_dash_to_null(local);
|
2015-01-20 01:36:11 +01:00
|
|
|
|
|
|
|
if (local) {
|
2015-01-22 03:57:15 +01:00
|
|
|
r = tar_strip_suffixes(local, &ll);
|
2015-01-20 01:36:11 +01:00
|
|
|
if (r < 0)
|
|
|
|
return log_oom();
|
|
|
|
|
|
|
|
local = ll;
|
|
|
|
|
2020-12-11 16:40:45 +01:00
|
|
|
if (!hostname_is_valid(local, 0))
|
2020-10-09 14:59:44 +02:00
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
|
|
|
"Local image name '%s' is not valid.",
|
|
|
|
local);
|
2015-01-20 01:36:11 +01:00
|
|
|
|
|
|
|
if (!arg_force) {
|
2018-04-05 15:39:43 +02:00
|
|
|
r = image_find(IMAGE_MACHINE, local, NULL);
|
2018-04-06 18:53:57 +02:00
|
|
|
if (r < 0) {
|
|
|
|
if (r != -ENOENT)
|
|
|
|
return log_error_errno(r, "Failed to check whether image '%s' exists: %m", local);
|
|
|
|
} else {
|
2020-10-09 14:59:44 +02:00
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EEXIST),
|
|
|
|
"Image '%s' already exists.",
|
|
|
|
local);
|
2015-01-20 01:36:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log_info("Pulling '%s', saving as '%s'.", url, local);
|
|
|
|
} else
|
|
|
|
log_info("Pulling '%s'.", url);
|
|
|
|
|
|
|
|
r = sd_event_default(&event);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to allocate event loop: %m");
|
|
|
|
|
2015-06-15 20:13:23 +02:00
|
|
|
assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, -1) >= 0);
|
|
|
|
(void) sd_event_add_signal(event, NULL, SIGTERM, interrupt_signal_handler, NULL);
|
|
|
|
(void) sd_event_add_signal(event, NULL, SIGINT, interrupt_signal_handler, NULL);
|
2015-01-20 01:36:11 +01:00
|
|
|
|
2015-03-04 17:07:12 +01:00
|
|
|
r = tar_pull_new(&pull, event, arg_image_root, on_tar_finished, event);
|
2015-01-20 01:36:11 +01:00
|
|
|
if (r < 0)
|
2015-03-04 17:07:12 +01:00
|
|
|
return log_error_errno(r, "Failed to allocate puller: %m");
|
2015-01-20 01:36:11 +01:00
|
|
|
|
2015-09-08 18:25:56 +02:00
|
|
|
r = tar_pull_start(pull, url, local, arg_force, arg_verify, arg_settings);
|
2015-01-20 01:36:11 +01:00
|
|
|
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.");
|
2015-01-22 03:57:15 +01:00
|
|
|
return -r;
|
2015-01-20 01:36:11 +01:00
|
|
|
}
|
|
|
|
|
2015-03-04 17:07:12 +01:00
|
|
|
static void on_raw_finished(RawPull *pull, int error, void *userdata) {
|
2014-12-24 16:44:56 +01:00
|
|
|
sd_event *event = userdata;
|
2015-03-04 17:07:12 +01:00
|
|
|
assert(pull);
|
2014-12-24 16:44:56 +01:00
|
|
|
|
|
|
|
if (error == 0)
|
|
|
|
log_info("Operation completed successfully.");
|
|
|
|
|
2015-01-22 03:57:15 +01:00
|
|
|
sd_event_exit(event, abs(error));
|
2015-01-16 20:07:25 +01:00
|
|
|
}
|
|
|
|
|
2015-01-15 01:03:33 +01:00
|
|
|
static int pull_raw(int argc, char *argv[], void *userdata) {
|
2015-03-04 17:07:12 +01:00
|
|
|
_cleanup_(raw_pull_unrefp) RawPull *pull = NULL;
|
tree-wide: expose "p"-suffix unref calls in public APIs to make gcc cleanup easy
GLIB has recently started to officially support the gcc cleanup
attribute in its public API, hence let's do the same for our APIs.
With this patch we'll define an xyz_unrefp() call for each public
xyz_unref() call, to make it easy to use inside a
__attribute__((cleanup())) expression. Then, all code is ported over to
make use of this.
The new calls are also documented in the man pages, with examples how to
use them (well, I only added docs where the _unref() call itself already
had docs, and the examples, only cover sd_bus_unrefp() and
sd_event_unrefp()).
This also renames sd_lldp_free() to sd_lldp_unref(), since that's how we
tend to call our destructors these days.
Note that this defines no public macro that wraps gcc's attribute and
makes it easier to use. While I think it's our duty in the library to
make our stuff easy to use, I figure it's not our duty to make gcc's own
features easy to use on its own. Most likely, client code which wants to
make use of this should define its own:
#define _cleanup_(function) __attribute__((cleanup(function)))
Or similar, to make the gcc feature easier to use.
Making this logic public has the benefit that we can remove three header
files whose only purpose was to define these functions internally.
See #2008.
2015-11-27 19:13:45 +01:00
|
|
|
_cleanup_(sd_event_unrefp) sd_event *event = NULL;
|
2015-01-16 20:07:25 +01:00
|
|
|
const char *url, *local;
|
2015-01-20 03:00:07 +01:00
|
|
|
_cleanup_free_ char *l = NULL, *ll = NULL;
|
2014-12-24 16:44:56 +01:00
|
|
|
int r;
|
|
|
|
|
|
|
|
url = argv[1];
|
2020-10-09 14:59:44 +02:00
|
|
|
if (!http_url_is_valid(url))
|
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
|
|
|
"URL '%s' is not valid.", url);
|
2014-12-24 16:44:56 +01:00
|
|
|
|
2014-12-26 17:10:31 +01:00
|
|
|
if (argc >= 3)
|
|
|
|
local = argv[2];
|
|
|
|
else {
|
2015-01-20 15:06:34 +01:00
|
|
|
r = import_url_last_component(url, &l);
|
2015-01-20 03:00:07 +01:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed get final component of URL: %m");
|
2014-12-24 16:44:56 +01:00
|
|
|
|
2015-01-20 03:00:07 +01:00
|
|
|
local = l;
|
2014-12-24 16:44:56 +01:00
|
|
|
}
|
|
|
|
|
2019-04-08 12:11:11 +02:00
|
|
|
local = empty_or_dash_to_null(local);
|
2014-12-24 16:44:56 +01:00
|
|
|
|
2014-12-26 17:10:31 +01:00
|
|
|
if (local) {
|
2015-01-22 03:57:15 +01:00
|
|
|
r = raw_strip_suffixes(local, &ll);
|
2015-01-16 20:07:25 +01:00
|
|
|
if (r < 0)
|
|
|
|
return log_oom();
|
|
|
|
|
2015-01-20 03:00:07 +01:00
|
|
|
local = ll;
|
2014-12-26 17:10:31 +01:00
|
|
|
|
2020-12-11 16:40:45 +01:00
|
|
|
if (!hostname_is_valid(local, 0))
|
2020-10-09 14:59:44 +02:00
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
|
|
|
"Local image name '%s' is not valid.",
|
|
|
|
local);
|
2014-12-26 17:10:31 +01:00
|
|
|
|
2015-01-20 03:00:07 +01:00
|
|
|
if (!arg_force) {
|
2018-04-05 15:39:43 +02:00
|
|
|
r = image_find(IMAGE_MACHINE, local, NULL);
|
2018-04-06 18:53:57 +02:00
|
|
|
if (r < 0) {
|
|
|
|
if (r != -ENOENT)
|
|
|
|
return log_error_errno(r, "Failed to check whether image '%s' exists: %m", local);
|
|
|
|
} else {
|
2020-10-09 14:59:44 +02:00
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EEXIST),
|
|
|
|
"Image '%s' already exists.",
|
|
|
|
local);
|
2014-12-26 17:10:31 +01:00
|
|
|
}
|
2015-01-20 03:00:07 +01:00
|
|
|
}
|
2014-12-24 16:44:56 +01:00
|
|
|
|
2014-12-26 17:10:31 +01:00
|
|
|
log_info("Pulling '%s', saving as '%s'.", url, local);
|
|
|
|
} else
|
|
|
|
log_info("Pulling '%s'.", url);
|
2014-12-24 16:44:56 +01:00
|
|
|
|
|
|
|
r = sd_event_default(&event);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to allocate event loop: %m");
|
|
|
|
|
2015-06-15 20:13:23 +02:00
|
|
|
assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, -1) >= 0);
|
|
|
|
(void) sd_event_add_signal(event, NULL, SIGTERM, interrupt_signal_handler, NULL);
|
|
|
|
(void) sd_event_add_signal(event, NULL, SIGINT, interrupt_signal_handler, NULL);
|
2014-12-24 16:44:56 +01:00
|
|
|
|
2015-03-04 17:07:12 +01:00
|
|
|
r = raw_pull_new(&pull, event, arg_image_root, on_raw_finished, event);
|
2014-12-24 16:44:56 +01:00
|
|
|
if (r < 0)
|
2015-03-04 17:07:12 +01:00
|
|
|
return log_error_errno(r, "Failed to allocate puller: %m");
|
2014-12-24 16:44:56 +01:00
|
|
|
|
2016-12-20 17:49:35 +01:00
|
|
|
r = raw_pull_start(pull, url, local, arg_force, arg_verify, arg_settings, arg_roothash);
|
2014-12-24 16:44:56 +01:00
|
|
|
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.");
|
2015-01-22 03:57:15 +01:00
|
|
|
return -r;
|
2014-12-24 16:44:56 +01:00
|
|
|
}
|
|
|
|
|
2014-12-19 01:59:52 +01:00
|
|
|
static int help(int argc, char *argv[], void *userdata) {
|
|
|
|
|
|
|
|
printf("%s [OPTIONS...] {COMMAND} ...\n\n"
|
2015-03-09 17:55:07 +01:00
|
|
|
"Download container or virtual machine images.\n\n"
|
2014-12-19 01:59:52 +01:00
|
|
|
" -h --help Show this help\n"
|
|
|
|
" --version Show package version\n"
|
2014-12-22 19:42:27 +01:00
|
|
|
" --force Force creation of image\n"
|
2015-09-08 18:25:56 +02:00
|
|
|
" --verify=MODE Verify downloaded image, one of: 'no',\n"
|
|
|
|
" 'checksum', 'signature'\n"
|
|
|
|
" --settings=BOOL Download settings file with image\n"
|
2016-12-20 17:49:35 +01:00
|
|
|
" --roothash=BOOL Download root hash file with image\n"
|
2015-12-10 12:40:04 +01:00
|
|
|
" --image-root=PATH Image root directory\n\n"
|
2014-12-19 01:59:52 +01:00
|
|
|
"Commands:\n"
|
2015-01-22 17:38:10 +01:00
|
|
|
" tar URL [NAME] Download a TAR image\n"
|
2015-12-10 12:40:04 +01:00
|
|
|
" raw URL [NAME] Download a RAW image\n",
|
2014-12-19 01:59:52 +01:00
|
|
|
program_invocation_short_name);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int parse_argv(int argc, char *argv[]) {
|
|
|
|
|
|
|
|
enum {
|
|
|
|
ARG_VERSION = 0x100,
|
|
|
|
ARG_FORCE,
|
2014-12-26 19:21:09 +01:00
|
|
|
ARG_IMAGE_ROOT,
|
2015-01-20 16:36:40 +01:00
|
|
|
ARG_VERIFY,
|
2015-09-08 18:25:56 +02:00
|
|
|
ARG_SETTINGS,
|
2016-12-20 17:49:35 +01:00
|
|
|
ARG_ROOTHASH,
|
2014-12-19 01:59:52 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct option options[] = {
|
|
|
|
{ "help", no_argument, NULL, 'h' },
|
|
|
|
{ "version", no_argument, NULL, ARG_VERSION },
|
|
|
|
{ "force", no_argument, NULL, ARG_FORCE },
|
2014-12-26 19:21:09 +01:00
|
|
|
{ "image-root", required_argument, NULL, ARG_IMAGE_ROOT },
|
2015-01-20 16:36:40 +01:00
|
|
|
{ "verify", required_argument, NULL, ARG_VERIFY },
|
2015-09-08 18:25:56 +02:00
|
|
|
{ "settings", required_argument, NULL, ARG_SETTINGS },
|
2016-12-20 17:49:35 +01:00
|
|
|
{ "roothash", required_argument, NULL, ARG_ROOTHASH },
|
2014-12-19 01:59:52 +01:00
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
2015-09-08 18:25:56 +02:00
|
|
|
int c, r;
|
2014-12-19 01:59:52 +01:00
|
|
|
|
|
|
|
assert(argc >= 0);
|
|
|
|
assert(argv);
|
|
|
|
|
|
|
|
while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
|
|
|
|
case 'h':
|
2014-12-19 19:18:48 +01:00
|
|
|
return help(0, NULL, NULL);
|
2014-12-19 01:59:52 +01:00
|
|
|
|
|
|
|
case ARG_VERSION:
|
2015-09-23 03:01:06 +02:00
|
|
|
return version();
|
2014-12-19 01:59:52 +01:00
|
|
|
|
|
|
|
case ARG_FORCE:
|
|
|
|
arg_force = true;
|
|
|
|
break;
|
|
|
|
|
2014-12-26 19:21:09 +01:00
|
|
|
case ARG_IMAGE_ROOT:
|
|
|
|
arg_image_root = optarg;
|
|
|
|
break;
|
|
|
|
|
2015-01-20 16:36:40 +01:00
|
|
|
case ARG_VERIFY:
|
|
|
|
arg_verify = import_verify_from_string(optarg);
|
2018-11-20 23:40:44 +01:00
|
|
|
if (arg_verify < 0)
|
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
|
|
|
"Invalid verification setting '%s'", optarg);
|
2015-01-20 16:36:40 +01:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
2015-09-08 18:25:56 +02:00
|
|
|
case ARG_SETTINGS:
|
|
|
|
r = parse_boolean(optarg);
|
|
|
|
if (r < 0)
|
2018-10-19 18:55:32 +02:00
|
|
|
return log_error_errno(r, "Failed to parse --settings= parameter '%s': %m", optarg);
|
2015-09-08 18:25:56 +02:00
|
|
|
|
|
|
|
arg_settings = r;
|
|
|
|
break;
|
|
|
|
|
2016-12-20 17:49:35 +01:00
|
|
|
case ARG_ROOTHASH:
|
|
|
|
r = parse_boolean(optarg);
|
|
|
|
if (r < 0)
|
2018-10-19 18:55:32 +02:00
|
|
|
return log_error_errno(r, "Failed to parse --roothash= parameter '%s': %m", optarg);
|
2016-12-20 17:49:35 +01:00
|
|
|
|
|
|
|
arg_roothash = r;
|
|
|
|
break;
|
|
|
|
|
2014-12-19 01:59:52 +01:00
|
|
|
case '?':
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert_not_reached("Unhandled option");
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-03-04 17:07:12 +01:00
|
|
|
static int pull_main(int argc, char *argv[]) {
|
2014-12-19 19:18:48 +01:00
|
|
|
static const Verb verbs[] = {
|
2015-01-22 17:38:10 +01:00
|
|
|
{ "help", VERB_ANY, VERB_ANY, 0, help },
|
|
|
|
{ "tar", 2, 3, 0, pull_tar },
|
|
|
|
{ "raw", 2, 3, 0, pull_raw },
|
2014-12-19 01:59:52 +01:00
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
|
|
|
return dispatch_verb(argc, argv, verbs, NULL);
|
|
|
|
}
|
|
|
|
|
2018-11-16 12:25:43 +01:00
|
|
|
static int run(int argc, char *argv[]) {
|
2014-12-19 01:59:52 +01:00
|
|
|
int r;
|
|
|
|
|
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
log_parse_environment();
|
|
|
|
log_open();
|
|
|
|
|
|
|
|
r = parse_argv(argc, argv);
|
|
|
|
if (r <= 0)
|
2018-11-16 12:25:43 +01:00
|
|
|
return r;
|
2014-12-19 01:59:52 +01:00
|
|
|
|
2015-05-31 23:55:55 +02:00
|
|
|
(void) ignore_signals(SIGPIPE, -1);
|
2015-03-05 00:56:08 +01:00
|
|
|
|
2018-11-16 12:25:43 +01:00
|
|
|
return pull_main(argc, argv);
|
2014-12-19 01:59:52 +01:00
|
|
|
}
|
2018-11-16 12:25:43 +01:00
|
|
|
|
|
|
|
DEFINE_MAIN_FUNCTION(run);
|