1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2015 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 "machine-image.h"
29 #include "import-util.h"
30 #include "import-tar.h"
31 #include "import-raw.h"
33 static bool arg_force = false;
34 static bool arg_read_only = false;
35 static const char *arg_image_root = "/var/lib/machines";
37 static int interrupt_signal_handler(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
38 log_notice("Transfer aborted.");
39 sd_event_exit(sd_event_source_get_event(s), EINTR);
43 static void on_tar_finished(TarImport *import, int error, void *userdata) {
44 sd_event *event = userdata;
48 log_info("Operation completed successfully.");
50 sd_event_exit(event, abs(error));
53 static int import_tar(int argc, char *argv[], void *userdata) {
54 _cleanup_(tar_import_unrefp) TarImport *import = NULL;
55 _cleanup_event_unref_ sd_event *event = NULL;
56 const char *path = NULL, *local = NULL;
57 _cleanup_free_ char *ll = NULL;
58 _cleanup_close_ int open_fd = -1;
63 if (isempty(path) || streq(path, "-"))
69 local = basename(path);
70 if (isempty(local) || streq(local, "-"))
74 r = tar_strip_suffixes(local, &ll);
80 if (!machine_name_is_valid(local)) {
81 log_error("Local image name '%s' is not valid.", local);
86 r = image_find(local, NULL);
88 return log_error_errno(r, "Failed to check whether image '%s' exists: %m", local);
90 log_error_errno(EEXIST, "Image '%s' already exists.", local);
98 open_fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY);
100 return log_error_errno(errno, "Failed to open tar image to import: %m");
104 log_info("Importing '%s', saving as '%s'.", path, local);
106 _cleanup_free_ char *pretty = NULL;
110 (void) readlink_malloc("/proc/self/fd/0", &pretty);
111 log_info("Importing '%s', saving as '%s'.", strna(pretty), local);
114 r = sd_event_default(&event);
116 return log_error_errno(r, "Failed to allocate event loop: %m");
118 assert_se(sigprocmask_many(SIG_BLOCK, SIGTERM, SIGINT, -1) == 0);
119 sd_event_add_signal(event, NULL, SIGTERM, interrupt_signal_handler, NULL);
120 sd_event_add_signal(event, NULL, SIGINT, interrupt_signal_handler, NULL);
122 r = tar_import_new(&import, event, arg_image_root, on_tar_finished, event);
124 return log_error_errno(r, "Failed to allocate importer: %m");
126 r = tar_import_start(import, fd, local, arg_force, arg_read_only);
128 return log_error_errno(r, "Failed to import image: %m");
130 r = sd_event_loop(event);
132 return log_error_errno(r, "Failed to run event loop: %m");
134 log_info("Exiting.");
138 static void on_raw_finished(RawImport *import, int error, void *userdata) {
139 sd_event *event = userdata;
143 log_info("Operation completed successfully.");
145 sd_event_exit(event, abs(error));
148 static int import_raw(int argc, char *argv[], void *userdata) {
149 _cleanup_(raw_import_unrefp) RawImport *import = NULL;
150 _cleanup_event_unref_ sd_event *event = NULL;
151 const char *path = NULL, *local = NULL;
152 _cleanup_free_ char *ll = NULL;
153 _cleanup_close_ int open_fd = -1;
158 if (isempty(path) || streq(path, "-"))
164 local = basename(path);
165 if (isempty(local) || streq(local, "-"))
169 r = raw_strip_suffixes(local, &ll);
175 if (!machine_name_is_valid(local)) {
176 log_error("Local image name '%s' is not valid.", local);
181 r = image_find(local, NULL);
183 return log_error_errno(r, "Failed to check whether image '%s' exists: %m", local);
185 log_error_errno(EEXIST, "Image '%s' already exists.", local);
193 open_fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY);
195 return log_error_errno(errno, "Failed to open raw image to import: %m");
199 log_info("Importing '%s', saving as '%s'.", path, local);
201 _cleanup_free_ char *pretty = NULL;
205 (void) readlink_malloc("/proc/self/fd/0", &pretty);
206 log_info("Importing '%s', saving as '%s'.", pretty, local);
209 r = sd_event_default(&event);
211 return log_error_errno(r, "Failed to allocate event loop: %m");
213 assert_se(sigprocmask_many(SIG_BLOCK, SIGTERM, SIGINT, -1) == 0);
214 sd_event_add_signal(event, NULL, SIGTERM, interrupt_signal_handler, NULL);
215 sd_event_add_signal(event, NULL, SIGINT, interrupt_signal_handler, NULL);
217 r = raw_import_new(&import, event, arg_image_root, on_raw_finished, event);
219 return log_error_errno(r, "Failed to allocate importer: %m");
221 r = raw_import_start(import, fd, local, arg_force, arg_read_only);
223 return log_error_errno(r, "Failed to import image: %m");
225 r = sd_event_loop(event);
227 return log_error_errno(r, "Failed to run event loop: %m");
229 log_info("Exiting.");
233 static int help(int argc, char *argv[], void *userdata) {
235 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
236 "Import container or virtual machine images.\n\n"
237 " -h --help Show this help\n"
238 " --version Show package version\n"
239 " --force Force creation of image\n"
240 " --image-root=PATH Image root directory\n"
241 " --read-only Create a read-only image\n\n"
243 " tar FILE [NAME] Import a TAR image\n"
244 " raw FILE [NAME] Import a RAW image\n",
245 program_invocation_short_name);
250 static int parse_argv(int argc, char *argv[]) {
259 static const struct option options[] = {
260 { "help", no_argument, NULL, 'h' },
261 { "version", no_argument, NULL, ARG_VERSION },
262 { "force", no_argument, NULL, ARG_FORCE },
263 { "image-root", required_argument, NULL, ARG_IMAGE_ROOT },
264 { "read-only", no_argument, NULL, ARG_READ_ONLY },
273 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
278 return help(0, NULL, NULL);
281 puts(PACKAGE_STRING);
282 puts(SYSTEMD_FEATURES);
290 arg_image_root = optarg;
294 arg_read_only = true;
301 assert_not_reached("Unhandled option");
307 static int import_main(int argc, char *argv[]) {
309 static const Verb verbs[] = {
310 { "help", VERB_ANY, VERB_ANY, 0, help },
311 { "tar", 2, 3, 0, import_tar },
312 { "raw", 2, 3, 0, import_raw },
316 return dispatch_verb(argc, argv, verbs, NULL);
319 int main(int argc, char *argv[]) {
322 setlocale(LC_ALL, "");
323 log_parse_environment();
326 r = parse_argv(argc, argv);
330 ignore_signals(SIGPIPE, -1);
332 r = import_main(argc, argv);
335 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;