1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2014 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 #include "event-util.h"
28 #include "import-gpt.h"
29 #include "import-dkr.h"
31 static bool arg_force = false;
33 static const char* arg_dkr_index_url = DEFAULT_DKR_INDEX_URL;
35 static void on_gpt_finished(GptImport *import, int error, void *userdata) {
36 sd_event *event = userdata;
40 log_info("Operation completed successfully.");
42 log_info_errno(error, "Operation failed: %m");
44 sd_event_exit(event, error);
47 static int pull_gpt(int argc, char *argv[], void *userdata) {
48 _cleanup_(gpt_import_unrefp) GptImport *import = NULL;
49 _cleanup_event_unref_ sd_event *event = NULL;
50 _cleanup_free_ char *local_truncated = NULL, *local_generated = NULL;
51 const char *url, *local, *suffix;
57 if (!gpt_url_is_valid(url)) {
58 log_error("URL '%s' is not valid.", url);
62 if (isempty(local) || streq(local, "-"))
68 e = url + strlen(url);
69 while (e > url && e[-1] == '/')
73 while (p > url && p[-1] != '/')
76 local_generated = strndup(p, e - p);
80 local = local_generated;
83 suffix = endswith(local, ".gpt");
85 local_truncated = strndup(local, suffix - local);
89 local = local_truncated;
92 if (!machine_name_is_valid(local)) {
93 log_error("Local image name '%s' is not valid.", local);
97 log_info("Pulling '%s' as '%s'", url, local);
99 r = sd_event_default(&event);
101 return log_error_errno(r, "Failed to allocate event loop: %m");
103 assert_se(sigprocmask_many(SIG_BLOCK, SIGTERM, SIGINT, -1) == 0);
104 sd_event_add_signal(event, NULL, SIGTERM, NULL, NULL);
105 sd_event_add_signal(event, NULL, SIGINT, NULL, NULL);
107 r = gpt_import_new(&import, event, on_gpt_finished, event);
109 return log_error_errno(r, "Failed to allocate importer: %m");
111 r = gpt_import_pull(import, url, local, arg_force);
113 return log_error_errno(r, "Failed to pull image: %m");
115 r = sd_event_loop(event);
117 return log_error_errno(r, "Failed to run event loop: %m");
119 log_info("Exiting.");
124 static void on_dkr_finished(DkrImport *import, int error, void *userdata) {
125 sd_event *event = userdata;
129 log_info("Operation completed successfully.");
131 log_info_errno(error, "Operation failed: %m");
133 sd_event_exit(event, error);
136 static int pull_dkr(int argc, char *argv[], void *userdata) {
137 _cleanup_(dkr_import_unrefp) DkrImport *import = NULL;
138 _cleanup_event_unref_ sd_event *event = NULL;
139 const char *name, *tag, *local;
142 if (!arg_dkr_index_url) {
143 log_error("Please specify an index URL with --dkr-index-url=");
147 tag = strchr(argv[1], ':');
149 name = strndupa(argv[1], tag - argv[1]);
159 local = strchr(name, '/');
166 if (isempty(local) || streq(local, "-"))
169 if (!dkr_name_is_valid(name)) {
170 log_error("Remote name '%s' is not valid.", name);
174 if (!dkr_tag_is_valid(tag)) {
175 log_error("Tag name '%s' is not valid.", tag);
182 if (!machine_name_is_valid(local)) {
183 log_error("Local image name '%s' is not valid.", local);
187 p = strappenda("/var/lib/container/", local);
188 if (laccess(p, F_OK) >= 0) {
190 log_info("Image '%s' already exists.", local);
193 } else if (errno != ENOENT)
194 return log_error_errno(errno, "Can't check if image '%s' already exists: %m", local);
196 log_info("Pulling '%s' with tag '%s', saving as '%s'.", name, tag, local);
198 log_info("Pulling '%s' with tag '%s'.", name, tag);
200 r = sd_event_default(&event);
202 return log_error_errno(r, "Failed to allocate event loop: %m");
204 assert_se(sigprocmask_many(SIG_BLOCK, SIGTERM, SIGINT, -1) == 0);
205 sd_event_add_signal(event, NULL, SIGTERM, NULL, NULL);
206 sd_event_add_signal(event, NULL, SIGINT, NULL, NULL);
208 r = dkr_import_new(&import, event, arg_dkr_index_url, on_dkr_finished, event);
210 return log_error_errno(r, "Failed to allocate importer: %m");
212 r = dkr_import_pull(import, name, tag, local, arg_force);
214 return log_error_errno(r, "Failed to pull image: %m");
216 r = sd_event_loop(event);
218 return log_error_errno(r, "Failed to run event loop: %m");
220 log_info("Exiting.");
225 static int help(int argc, char *argv[], void *userdata) {
227 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
228 "Import container or virtual machine image.\n\n"
229 " -h --help Show this help\n"
230 " --version Show package version\n"
231 " --force Force creation of image\n"
232 " --dkr-index-url=URL Specify index URL to use for downloads\n\n"
234 " pull-dkr REMOTE [NAME] Download a DKR image\n"
235 " pull-gpt URL [NAME] Download a GPT image\n",
236 program_invocation_short_name);
241 static int parse_argv(int argc, char *argv[]) {
249 static const struct option options[] = {
250 { "help", no_argument, NULL, 'h' },
251 { "version", no_argument, NULL, ARG_VERSION },
252 { "force", no_argument, NULL, ARG_FORCE },
253 { "dkr-index-url", required_argument, NULL, ARG_DKR_INDEX_URL },
262 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
267 return help(0, NULL, NULL);
270 puts(PACKAGE_STRING);
271 puts(SYSTEMD_FEATURES);
278 case ARG_DKR_INDEX_URL:
279 if (!dkr_url_is_valid(optarg)) {
280 log_error("Index URL is not valid: %s", optarg);
284 arg_dkr_index_url = optarg;
291 assert_not_reached("Unhandled option");
297 static int import_main(int argc, char *argv[]) {
299 static const Verb verbs[] = {
300 { "help", VERB_ANY, VERB_ANY, 0, help },
301 { "pull-dkr", 2, 3, 0, pull_dkr },
302 { "pull-gpt", 2, 3, 0, pull_gpt },
306 return dispatch_verb(argc, argv, verbs, NULL);
309 int main(int argc, char *argv[]) {
312 setlocale(LC_ALL, "");
313 log_parse_environment();
316 r = parse_argv(argc, argv);
320 r = import_main(argc, argv);
323 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;