import.c revision 8620a9a32391fd74d70ddc07c9b79729ad4ec067
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering This file is part of systemd.
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering Copyright 2014 Lennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering systemd is free software; you can redistribute it and/or modify it
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering under the terms of the GNU Lesser General Public License as published by
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering (at your option) any later version.
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering systemd is distributed in the hope that it will be useful, but
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering Lesser General Public License for more details.
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering You should have received a copy of the GNU Lesser General Public License
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensenstatic bool arg_force = false;
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poetteringstatic const char* arg_dkr_index_url = DEFAULT_DKR_INDEX_URL;
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poetteringstatic void on_gpt_finished(GptImport *import, int error, void *userdata) {
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering log_info("Operation completed successfully.");
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering log_info_errno(error, "Operation failed: %m");
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poetteringstatic int pull_gpt(int argc, char *argv[], void *userdata) {
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering _cleanup_(gpt_import_unrefp) GptImport *import = NULL;
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering _cleanup_event_unref_ sd_event *event = NULL;
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering log_error("URL '%s' is not valid.", url);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering const char *e, *p;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (isempty(local) || streq(local, "-"))
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering const char *p;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek log_error("Local image name '%s' is not valid.", local);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering p = strappenda("/var/lib/container/", local, ".gpt");
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering log_info("Image '%s' already exists.", local);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering return log_error_errno(errno, "Can't check if image '%s' already exists: %m", local);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering log_info("Pulling '%s', saving as '%s'.", url, local);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering return log_error_errno(r, "Failed to allocate event loop: %m");
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering assert_se(sigprocmask_many(SIG_BLOCK, SIGTERM, SIGINT, -1) == 0);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering sd_event_add_signal(event, NULL, SIGTERM, NULL, NULL);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering sd_event_add_signal(event, NULL, SIGINT, NULL, NULL);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering r = gpt_import_new(&import, event, on_gpt_finished, event);
874bc134ac6504c45e94174e37af13ff21a6bfe2Zbigniew Jędrzejewski-Szmek return log_error_errno(r, "Failed to allocate importer: %m");
874bc134ac6504c45e94174e37af13ff21a6bfe2Zbigniew Jędrzejewski-Szmek r = gpt_import_pull(import, url, local, arg_force);
874bc134ac6504c45e94174e37af13ff21a6bfe2Zbigniew Jędrzejewski-Szmek return log_error_errno(r, "Failed to pull image: %m");
874bc134ac6504c45e94174e37af13ff21a6bfe2Zbigniew Jędrzejewski-Szmek return log_error_errno(r, "Failed to run event loop: %m");
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poetteringstatic void on_dkr_finished(DkrImport *import, int error, void *userdata) {
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering log_info("Operation completed successfully.");
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering log_info_errno(error, "Operation failed: %m");
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmekstatic int pull_dkr(int argc, char *argv[], void *userdata) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek _cleanup_(dkr_import_unrefp) DkrImport *import = NULL;
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering _cleanup_event_unref_ sd_event *event = NULL;
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering log_error("Please specify an index URL with --dkr-index-url=");
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering log_error("Remote name '%s' is not valid.", name);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering log_error("Tag name '%s' is not valid.", tag);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering const char *p;
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering log_error("Local image name '%s' is not valid.", local);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering p = strappenda("/var/lib/container/", local);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering log_info("Image '%s' already exists.", local);
1f6b411372076426c0faf0bb350437fb4d82931fLennart Poettering return log_error_errno(errno, "Can't check if image '%s' already exists: %m", local);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek log_info("Pulling '%s' with tag '%s', saving as '%s'.", name, tag, local);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek log_info("Pulling '%s' with tag '%s'.", name, tag);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return log_error_errno(r, "Failed to allocate event loop: %m");
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek assert_se(sigprocmask_many(SIG_BLOCK, SIGTERM, SIGINT, -1) == 0);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering sd_event_add_signal(event, NULL, SIGTERM, NULL, NULL);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering sd_event_add_signal(event, NULL, SIGINT, NULL, NULL);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering r = dkr_import_new(&import, event, arg_dkr_index_url, on_dkr_finished, event);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering return log_error_errno(r, "Failed to allocate importer: %m");
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering r = dkr_import_pull(import, name, tag, local, arg_force);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering return log_error_errno(r, "Failed to pull image: %m");
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering return log_error_errno(r, "Failed to run event loop: %m");
fa1c4b518ec7d8ec2d647213ee651cde4d6c9d7eZbigniew Jędrzejewski-Szmekstatic int help(int argc, char *argv[], void *userdata) {
505b6a61c22d5565e9308045c7b9bf79f7d0517eLennart Poettering printf("%s [OPTIONS...] {COMMAND} ...\n\n"
505b6a61c22d5565e9308045c7b9bf79f7d0517eLennart Poettering "Import container or virtual machine image.\n\n"
505b6a61c22d5565e9308045c7b9bf79f7d0517eLennart Poettering " -h --help Show this help\n"
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering " --version Show package version\n"
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering " --force Force creation of image\n"
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering " --dkr-index-url=URL Specify index URL to use for downloads\n\n"
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering " pull-dkr REMOTE [NAME] Download a DKR image\n"
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering " pull-gpt URL [NAME] Download a GPT image\n",
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poetteringstatic int parse_argv(int argc, char *argv[]) {
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering { "version", no_argument, NULL, ARG_VERSION },
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering { "force", no_argument, NULL, ARG_FORCE },
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering { "dkr-index-url", required_argument, NULL, ARG_DKR_INDEX_URL },
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen log_error("Index URL is not valid: %s", optarg);
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poetteringstatic int import_main(int argc, char *argv[]) {
3b3154df7e2773332bb814e167187367a0ccae4aLennart Poettering return dispatch_verb(argc, argv, verbs, NULL);