X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fimport%2Fimport.c;h=f3072b3775a683c796f88f8210b5a90515da0497;hb=1f67bedec8b558f9317496613d9c3f3bd595c940;hp=54f311363a682409a57164727c1631e80d906950;hpb=0c7bf33a989a58922b3eb9aaa96abd773c8754c4;p=elogind.git diff --git a/src/import/import.c b/src/import/import.c index 54f311363..f3072b377 100644 --- a/src/import/import.c +++ b/src/import/import.c @@ -3,7 +3,7 @@ /*** This file is part of systemd. - Copyright 2014 Lennart Poettering + Copyright 2015 Lennart Poettering systemd is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by @@ -25,123 +25,223 @@ #include "event-util.h" #include "verbs.h" #include "build.h" -#include "import-dkr.h" +#include "machine-image.h" +#include "import-util.h" +#include "import-tar.h" +#include "import-raw.h" static bool arg_force = false; +static bool arg_read_only = false; +static const char *arg_image_root = "/var/lib/machines"; -static const char* arg_dkr_index_url = DEFAULT_DKR_INDEX_URL; +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; +} -static void on_finished(DkrImport *import, int error, void *userdata) { +static void on_tar_finished(TarImport *import, int error, void *userdata) { sd_event *event = userdata; assert(import); if (error == 0) log_info("Operation completed successfully."); - else - log_info_errno(error, "Operation failed: %m"); - sd_event_exit(event, error); + sd_event_exit(event, abs(error)); } -static int pull_dkr(int argc, char *argv[], void *userdata) { - _cleanup_(dkr_import_unrefp) DkrImport *import = NULL; +static int import_tar(int argc, char *argv[], void *userdata) { + _cleanup_(tar_import_unrefp) TarImport *import = NULL; _cleanup_event_unref_ sd_event *event = NULL; - const char *name, *tag, *local; - int r; - - if (!arg_dkr_index_url) { - log_error("Please specify an index URL with --dkr-index-url="); - return -EINVAL; - } + const char *path = NULL, *local = NULL; + _cleanup_free_ char *ll = NULL; + _cleanup_close_ int open_fd = -1; + int r, fd; - tag = strchr(argv[1], ':'); - if (tag) { - name = strndupa(argv[1], tag - argv[1]); - tag++; - } else { - name = argv[1]; - tag = "latest"; - } + if (argc >= 2) + path = argv[1]; + if (isempty(path) || streq(path, "-")) + path = NULL; if (argc >= 3) local = argv[2]; - else { - local = strchr(name, '/'); - if (local) - local++; - else - local = name; - } - + else if (path) + local = basename(path); if (isempty(local) || streq(local, "-")) local = NULL; - if (!dkr_name_is_valid(name)) { - log_error("Remote name '%s' is not valid.", name); - return -EINVAL; - } + if (local) { + r = tar_strip_suffixes(local, &ll); + if (r < 0) + return log_oom(); + + local = ll; + + if (!machine_name_is_valid(local)) { + log_error("Local image name '%s' is not valid.", local); + return -EINVAL; + } + + if (!arg_force) { + r = image_find(local, NULL); + if (r < 0) + return log_error_errno(r, "Failed to check whether image '%s' exists: %m", local); + else if (r > 0) { + log_error_errno(EEXIST, "Image '%s' already exists.", local); + return -EEXIST; + } + } + } else + local = "imported"; + + if (path) { + open_fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY); + if (open_fd < 0) + return log_error_errno(errno, "Failed to open tar image to import: %m"); + + fd = open_fd; + + log_info("Importing '%s', saving as '%s'.", path, local); + } else { + _cleanup_free_ char *pretty = NULL; + + fd = STDIN_FILENO; - if (!dkr_tag_is_valid(tag)) { - log_error("Tag name '%s' is not valid.", tag); - return -EINVAL; + (void) readlink_malloc("/proc/self/fd/0", &pretty); + log_info("Importing '%s', saving as '%s'.", strna(pretty), local); } + r = sd_event_default(&event); + if (r < 0) + return log_error_errno(r, "Failed to allocate event loop: %m"); + + assert_se(sigprocmask_many(SIG_BLOCK, SIGTERM, SIGINT, -1) == 0); + sd_event_add_signal(event, NULL, SIGTERM, interrupt_signal_handler, NULL); + sd_event_add_signal(event, NULL, SIGINT, interrupt_signal_handler, NULL); + + r = tar_import_new(&import, event, arg_image_root, on_tar_finished, event); + if (r < 0) + return log_error_errno(r, "Failed to allocate importer: %m"); + + r = tar_import_start(import, fd, local, arg_force, arg_read_only); + if (r < 0) + return log_error_errno(r, "Failed to import 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 -r; +} + +static void on_raw_finished(RawImport *import, int error, void *userdata) { + sd_event *event = userdata; + assert(import); + + if (error == 0) + log_info("Operation completed successfully."); + + sd_event_exit(event, abs(error)); +} + +static int import_raw(int argc, char *argv[], void *userdata) { + _cleanup_(raw_import_unrefp) RawImport *import = NULL; + _cleanup_event_unref_ sd_event *event = NULL; + const char *path = NULL, *local = NULL; + _cleanup_free_ char *ll = NULL; + _cleanup_close_ int open_fd = -1; + int r, fd; + + if (argc >= 2) + path = argv[1]; + if (isempty(path) || streq(path, "-")) + path = NULL; + + if (argc >= 3) + local = argv[2]; + else if (path) + local = basename(path); + if (isempty(local) || streq(local, "-")) + local = NULL; + if (local) { - const char *p; + r = raw_strip_suffixes(local, &ll); + if (r < 0) + return log_oom(); + + local = ll; if (!machine_name_is_valid(local)) { log_error("Local image name '%s' is not valid.", local); return -EINVAL; } - p = strappenda("/var/lib/container/", local); - if (laccess(p, F_OK) >= 0) { - if (!arg_force) { - log_info("Image '%s' already exists.", local); - return 0; + if (!arg_force) { + r = image_find(local, NULL); + if (r < 0) + return log_error_errno(r, "Failed to check whether image '%s' exists: %m", local); + else if (r > 0) { + log_error_errno(EEXIST, "Image '%s' already exists.", local); + return -EEXIST; } - } else if (errno != ENOENT) - return log_error_errno(errno, "Can't check if image '%s' already exists: %m", local); - - log_info("Pulling '%s' with tag '%s', saving as '%s'.", name, tag, local); + } } else - log_info("Pulling '%s' with tag '%s'.", name, tag); + local = "imported"; + + if (path) { + open_fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY); + if (open_fd < 0) + return log_error_errno(errno, "Failed to open raw image to import: %m"); + + fd = open_fd; + + log_info("Importing '%s', saving as '%s'.", path, local); + } else { + _cleanup_free_ char *pretty = NULL; + + fd = STDIN_FILENO; + + (void) readlink_malloc("/proc/self/fd/0", &pretty); + log_info("Importing '%s', saving as '%s'.", pretty, local); + } r = sd_event_default(&event); if (r < 0) return log_error_errno(r, "Failed to allocate event loop: %m"); assert_se(sigprocmask_many(SIG_BLOCK, SIGTERM, SIGINT, -1) == 0); - sd_event_add_signal(event, NULL, SIGTERM, NULL, NULL); - sd_event_add_signal(event, NULL, SIGINT, NULL, NULL); + sd_event_add_signal(event, NULL, SIGTERM, interrupt_signal_handler, NULL); + sd_event_add_signal(event, NULL, SIGINT, interrupt_signal_handler, NULL); - r = dkr_import_new(&import, event, arg_dkr_index_url, on_dkr_finished, event); + r = raw_import_new(&import, event, arg_image_root, on_raw_finished, event); if (r < 0) return log_error_errno(r, "Failed to allocate importer: %m"); - r = dkr_import_pull(import, name, tag, local, arg_force); + r = raw_import_start(import, fd, local, arg_force, arg_read_only); if (r < 0) - return log_error_errno(r, "Failed to pull image: %m"); + return log_error_errno(r, "Failed to import 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; + return -r; } static int help(int argc, char *argv[], void *userdata) { printf("%s [OPTIONS...] {COMMAND} ...\n\n" - "Import container or virtual machine image.\n\n" + "Import container or virtual machine images.\n\n" " -h --help Show this help\n" " --version Show package version\n" " --force Force creation of image\n" - " --dkr-index-url=URL Specify index URL to use for downloads\n\n" + " --image-root=PATH Image root directory\n" + " --read-only Create a read-only image\n\n" "Commands:\n" - " pull-dkr REMOTE [NAME] Download an image\n", + " tar FILE [NAME] Import a TAR image\n" + " raw FILE [NAME] Import a RAW image\n", program_invocation_short_name); return 0; @@ -152,14 +252,16 @@ static int parse_argv(int argc, char *argv[]) { enum { ARG_VERSION = 0x100, ARG_FORCE, - ARG_DKR_INDEX_URL, + ARG_IMAGE_ROOT, + ARG_READ_ONLY, }; static const struct option options[] = { { "help", no_argument, NULL, 'h' }, { "version", no_argument, NULL, ARG_VERSION }, { "force", no_argument, NULL, ARG_FORCE }, - { "dkr-index-url", required_argument, NULL, ARG_DKR_INDEX_URL }, + { "image-root", required_argument, NULL, ARG_IMAGE_ROOT }, + { "read-only", no_argument, NULL, ARG_READ_ONLY }, {} }; @@ -184,13 +286,12 @@ static int parse_argv(int argc, char *argv[]) { arg_force = true; break; - case ARG_DKR_INDEX_URL: - if (!dkr_url_is_valid(optarg)) { - log_error("Index URL is not valid: %s", optarg); - return -EINVAL; - } + case ARG_IMAGE_ROOT: + arg_image_root = optarg; + break; - arg_dkr_index_url = optarg; + case ARG_READ_ONLY: + arg_read_only = true; break; case '?': @@ -206,8 +307,9 @@ static int parse_argv(int argc, char *argv[]) { static int import_main(int argc, char *argv[]) { static const Verb verbs[] = { - { "help", VERB_ANY, VERB_ANY, 0, help }, - { "pull-dkr", 2, 3, 0, pull_dkr }, + { "help", VERB_ANY, VERB_ANY, 0, help }, + { "tar", 2, 3, 0, import_tar }, + { "raw", 2, 3, 0, import_raw }, {} }; @@ -225,6 +327,8 @@ int main(int argc, char *argv[]) { if (r <= 0) goto finish; + ignore_signals(SIGPIPE, -1); + r = import_main(argc, argv); finish: