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/>.
26 #include "event-util.h"
28 #include "hostname-util.h"
29 #include "import-raw.h"
30 #include "import-tar.h"
31 #include "import-util.h"
32 #include "machine-image.h"
33 #include "signal-util.h"
34 #include "string-util.h"
37 static bool arg_force
= false;
38 static bool arg_read_only
= false;
39 static const char *arg_image_root
= "/var/lib/machines";
41 static int interrupt_signal_handler(sd_event_source
*s
, const struct signalfd_siginfo
*si
, void *userdata
) {
42 log_notice("Transfer aborted.");
43 sd_event_exit(sd_event_source_get_event(s
), EINTR
);
47 static void on_tar_finished(TarImport
*import
, int error
, void *userdata
) {
48 sd_event
*event
= userdata
;
52 log_info("Operation completed successfully.");
54 sd_event_exit(event
, abs(error
));
57 static int import_tar(int argc
, char *argv
[], void *userdata
) {
58 _cleanup_(tar_import_unrefp
) TarImport
*import
= NULL
;
59 _cleanup_event_unref_ sd_event
*event
= NULL
;
60 const char *path
= NULL
, *local
= NULL
;
61 _cleanup_free_
char *ll
= NULL
;
62 _cleanup_close_
int open_fd
= -1;
67 if (isempty(path
) || streq(path
, "-"))
73 local
= basename(path
);
74 if (isempty(local
) || streq(local
, "-"))
78 r
= tar_strip_suffixes(local
, &ll
);
84 if (!machine_name_is_valid(local
)) {
85 log_error("Local image name '%s' is not valid.", local
);
90 r
= image_find(local
, NULL
);
92 return log_error_errno(r
, "Failed to check whether image '%s' exists: %m", local
);
94 log_error_errno(EEXIST
, "Image '%s' already exists.", local
);
102 open_fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
104 return log_error_errno(errno
, "Failed to open tar image to import: %m");
108 log_info("Importing '%s', saving as '%s'.", path
, local
);
110 _cleanup_free_
char *pretty
= NULL
;
114 (void) readlink_malloc("/proc/self/fd/0", &pretty
);
115 log_info("Importing '%s', saving as '%s'.", strna(pretty
), local
);
118 r
= sd_event_default(&event
);
120 return log_error_errno(r
, "Failed to allocate event loop: %m");
122 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGTERM
, SIGINT
, -1) >= 0);
123 (void) sd_event_add_signal(event
, NULL
, SIGTERM
, interrupt_signal_handler
, NULL
);
124 (void) sd_event_add_signal(event
, NULL
, SIGINT
, interrupt_signal_handler
, NULL
);
126 r
= tar_import_new(&import
, event
, arg_image_root
, on_tar_finished
, event
);
128 return log_error_errno(r
, "Failed to allocate importer: %m");
130 r
= tar_import_start(import
, fd
, local
, arg_force
, arg_read_only
);
132 return log_error_errno(r
, "Failed to import image: %m");
134 r
= sd_event_loop(event
);
136 return log_error_errno(r
, "Failed to run event loop: %m");
138 log_info("Exiting.");
142 static void on_raw_finished(RawImport
*import
, int error
, void *userdata
) {
143 sd_event
*event
= userdata
;
147 log_info("Operation completed successfully.");
149 sd_event_exit(event
, abs(error
));
152 static int import_raw(int argc
, char *argv
[], void *userdata
) {
153 _cleanup_(raw_import_unrefp
) RawImport
*import
= NULL
;
154 _cleanup_event_unref_ sd_event
*event
= NULL
;
155 const char *path
= NULL
, *local
= NULL
;
156 _cleanup_free_
char *ll
= NULL
;
157 _cleanup_close_
int open_fd
= -1;
162 if (isempty(path
) || streq(path
, "-"))
168 local
= basename(path
);
169 if (isempty(local
) || streq(local
, "-"))
173 r
= raw_strip_suffixes(local
, &ll
);
179 if (!machine_name_is_valid(local
)) {
180 log_error("Local image name '%s' is not valid.", local
);
185 r
= image_find(local
, NULL
);
187 return log_error_errno(r
, "Failed to check whether image '%s' exists: %m", local
);
189 log_error_errno(EEXIST
, "Image '%s' already exists.", local
);
197 open_fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
199 return log_error_errno(errno
, "Failed to open raw image to import: %m");
203 log_info("Importing '%s', saving as '%s'.", path
, local
);
205 _cleanup_free_
char *pretty
= NULL
;
209 (void) readlink_malloc("/proc/self/fd/0", &pretty
);
210 log_info("Importing '%s', saving as '%s'.", pretty
, local
);
213 r
= sd_event_default(&event
);
215 return log_error_errno(r
, "Failed to allocate event loop: %m");
217 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGTERM
, SIGINT
, -1) >= 0);
218 (void) sd_event_add_signal(event
, NULL
, SIGTERM
, interrupt_signal_handler
, NULL
);
219 (void) sd_event_add_signal(event
, NULL
, SIGINT
, interrupt_signal_handler
, NULL
);
221 r
= raw_import_new(&import
, event
, arg_image_root
, on_raw_finished
, event
);
223 return log_error_errno(r
, "Failed to allocate importer: %m");
225 r
= raw_import_start(import
, fd
, local
, arg_force
, arg_read_only
);
227 return log_error_errno(r
, "Failed to import image: %m");
229 r
= sd_event_loop(event
);
231 return log_error_errno(r
, "Failed to run event loop: %m");
233 log_info("Exiting.");
237 static int help(int argc
, char *argv
[], void *userdata
) {
239 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
240 "Import container or virtual machine images.\n\n"
241 " -h --help Show this help\n"
242 " --version Show package version\n"
243 " --force Force creation of image\n"
244 " --image-root=PATH Image root directory\n"
245 " --read-only Create a read-only image\n\n"
247 " tar FILE [NAME] Import a TAR image\n"
248 " raw FILE [NAME] Import a RAW image\n",
249 program_invocation_short_name
);
254 static int parse_argv(int argc
, char *argv
[]) {
263 static const struct option options
[] = {
264 { "help", no_argument
, NULL
, 'h' },
265 { "version", no_argument
, NULL
, ARG_VERSION
},
266 { "force", no_argument
, NULL
, ARG_FORCE
},
267 { "image-root", required_argument
, NULL
, ARG_IMAGE_ROOT
},
268 { "read-only", no_argument
, NULL
, ARG_READ_ONLY
},
277 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
282 return help(0, NULL
, NULL
);
292 arg_image_root
= optarg
;
296 arg_read_only
= true;
303 assert_not_reached("Unhandled option");
309 static int import_main(int argc
, char *argv
[]) {
311 static const Verb verbs
[] = {
312 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
313 { "tar", 2, 3, 0, import_tar
},
314 { "raw", 2, 3, 0, import_raw
},
318 return dispatch_verb(argc
, argv
, verbs
, NULL
);
321 int main(int argc
, char *argv
[]) {
324 setlocale(LC_ALL
, "");
325 log_parse_environment();
328 r
= parse_argv(argc
, argv
);
332 (void) ignore_signals(SIGPIPE
, -1);
334 r
= import_main(argc
, argv
);
337 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;