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 "signal-util.h"
29 #include "machine-image.h"
30 #include "import-util.h"
31 #include "import-tar.h"
32 #include "import-raw.h"
34 static bool arg_force
= false;
35 static bool arg_read_only
= false;
36 static const char *arg_image_root
= "/var/lib/machines";
38 static int interrupt_signal_handler(sd_event_source
*s
, const struct signalfd_siginfo
*si
, void *userdata
) {
39 log_notice("Transfer aborted.");
40 sd_event_exit(sd_event_source_get_event(s
), EINTR
);
44 static void on_tar_finished(TarImport
*import
, int error
, void *userdata
) {
45 sd_event
*event
= userdata
;
49 log_info("Operation completed successfully.");
51 sd_event_exit(event
, abs(error
));
54 static int import_tar(int argc
, char *argv
[], void *userdata
) {
55 _cleanup_(tar_import_unrefp
) TarImport
*import
= NULL
;
56 _cleanup_event_unref_ sd_event
*event
= NULL
;
57 const char *path
= NULL
, *local
= NULL
;
58 _cleanup_free_
char *ll
= NULL
;
59 _cleanup_close_
int open_fd
= -1;
64 if (isempty(path
) || streq(path
, "-"))
70 local
= basename(path
);
71 if (isempty(local
) || streq(local
, "-"))
75 r
= tar_strip_suffixes(local
, &ll
);
81 if (!machine_name_is_valid(local
)) {
82 log_error("Local image name '%s' is not valid.", local
);
87 r
= image_find(local
, NULL
);
89 return log_error_errno(r
, "Failed to check whether image '%s' exists: %m", local
);
91 log_error_errno(EEXIST
, "Image '%s' already exists.", local
);
99 open_fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
101 return log_error_errno(errno
, "Failed to open tar image to import: %m");
105 log_info("Importing '%s', saving as '%s'.", path
, local
);
107 _cleanup_free_
char *pretty
= NULL
;
111 (void) readlink_malloc("/proc/self/fd/0", &pretty
);
112 log_info("Importing '%s', saving as '%s'.", strna(pretty
), local
);
115 r
= sd_event_default(&event
);
117 return log_error_errno(r
, "Failed to allocate event loop: %m");
119 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGTERM
, SIGINT
, -1) >= 0);
120 (void) sd_event_add_signal(event
, NULL
, SIGTERM
, interrupt_signal_handler
, NULL
);
121 (void) sd_event_add_signal(event
, NULL
, SIGINT
, interrupt_signal_handler
, NULL
);
123 r
= tar_import_new(&import
, event
, arg_image_root
, on_tar_finished
, event
);
125 return log_error_errno(r
, "Failed to allocate importer: %m");
127 r
= tar_import_start(import
, fd
, local
, arg_force
, arg_read_only
);
129 return log_error_errno(r
, "Failed to import image: %m");
131 r
= sd_event_loop(event
);
133 return log_error_errno(r
, "Failed to run event loop: %m");
135 log_info("Exiting.");
139 static void on_raw_finished(RawImport
*import
, int error
, void *userdata
) {
140 sd_event
*event
= userdata
;
144 log_info("Operation completed successfully.");
146 sd_event_exit(event
, abs(error
));
149 static int import_raw(int argc
, char *argv
[], void *userdata
) {
150 _cleanup_(raw_import_unrefp
) RawImport
*import
= NULL
;
151 _cleanup_event_unref_ sd_event
*event
= NULL
;
152 const char *path
= NULL
, *local
= NULL
;
153 _cleanup_free_
char *ll
= NULL
;
154 _cleanup_close_
int open_fd
= -1;
159 if (isempty(path
) || streq(path
, "-"))
165 local
= basename(path
);
166 if (isempty(local
) || streq(local
, "-"))
170 r
= raw_strip_suffixes(local
, &ll
);
176 if (!machine_name_is_valid(local
)) {
177 log_error("Local image name '%s' is not valid.", local
);
182 r
= image_find(local
, NULL
);
184 return log_error_errno(r
, "Failed to check whether image '%s' exists: %m", local
);
186 log_error_errno(EEXIST
, "Image '%s' already exists.", local
);
194 open_fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
196 return log_error_errno(errno
, "Failed to open raw image to import: %m");
200 log_info("Importing '%s', saving as '%s'.", path
, local
);
202 _cleanup_free_
char *pretty
= NULL
;
206 (void) readlink_malloc("/proc/self/fd/0", &pretty
);
207 log_info("Importing '%s', saving as '%s'.", pretty
, local
);
210 r
= sd_event_default(&event
);
212 return log_error_errno(r
, "Failed to allocate event loop: %m");
214 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGTERM
, SIGINT
, -1) >= 0);
215 (void) sd_event_add_signal(event
, NULL
, SIGTERM
, interrupt_signal_handler
, NULL
);
216 (void) sd_event_add_signal(event
, NULL
, SIGINT
, interrupt_signal_handler
, NULL
);
218 r
= raw_import_new(&import
, event
, arg_image_root
, on_raw_finished
, event
);
220 return log_error_errno(r
, "Failed to allocate importer: %m");
222 r
= raw_import_start(import
, fd
, local
, arg_force
, arg_read_only
);
224 return log_error_errno(r
, "Failed to import image: %m");
226 r
= sd_event_loop(event
);
228 return log_error_errno(r
, "Failed to run event loop: %m");
230 log_info("Exiting.");
234 static int help(int argc
, char *argv
[], void *userdata
) {
236 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
237 "Import container or virtual machine images.\n\n"
238 " -h --help Show this help\n"
239 " --version Show package version\n"
240 " --force Force creation of image\n"
241 " --image-root=PATH Image root directory\n"
242 " --read-only Create a read-only image\n\n"
244 " tar FILE [NAME] Import a TAR image\n"
245 " raw FILE [NAME] Import a RAW image\n",
246 program_invocation_short_name
);
251 static int parse_argv(int argc
, char *argv
[]) {
260 static const struct option options
[] = {
261 { "help", no_argument
, NULL
, 'h' },
262 { "version", no_argument
, NULL
, ARG_VERSION
},
263 { "force", no_argument
, NULL
, ARG_FORCE
},
264 { "image-root", required_argument
, NULL
, ARG_IMAGE_ROOT
},
265 { "read-only", no_argument
, NULL
, ARG_READ_ONLY
},
274 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
279 return help(0, NULL
, NULL
);
282 puts(PACKAGE_STRING
);
283 puts(SYSTEMD_FEATURES
);
291 arg_image_root
= optarg
;
295 arg_read_only
= true;
302 assert_not_reached("Unhandled option");
308 static int import_main(int argc
, char *argv
[]) {
310 static const Verb verbs
[] = {
311 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
312 { "tar", 2, 3, 0, import_tar
},
313 { "raw", 2, 3, 0, import_raw
},
317 return dispatch_verb(argc
, argv
, verbs
, NULL
);
320 int main(int argc
, char *argv
[]) {
323 setlocale(LC_ALL
, "");
324 log_parse_environment();
327 r
= parse_argv(argc
, argv
);
331 (void) ignore_signals(SIGPIPE
, -1);
333 r
= import_main(argc
, argv
);
336 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;