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 "alloc-util.h"
27 #include "event-util.h"
30 #include "hostname-util.h"
31 #include "import-raw.h"
32 #include "import-tar.h"
33 #include "import-util.h"
34 #include "machine-image.h"
35 #include "signal-util.h"
36 #include "string-util.h"
39 static bool arg_force
= false;
40 static bool arg_read_only
= false;
41 static const char *arg_image_root
= "/var/lib/machines";
43 static int interrupt_signal_handler(sd_event_source
*s
, const struct signalfd_siginfo
*si
, void *userdata
) {
44 log_notice("Transfer aborted.");
45 sd_event_exit(sd_event_source_get_event(s
), EINTR
);
49 static void on_tar_finished(TarImport
*import
, int error
, void *userdata
) {
50 sd_event
*event
= userdata
;
54 log_info("Operation completed successfully.");
56 sd_event_exit(event
, abs(error
));
59 static int import_tar(int argc
, char *argv
[], void *userdata
) {
60 _cleanup_(tar_import_unrefp
) TarImport
*import
= NULL
;
61 _cleanup_event_unref_ sd_event
*event
= NULL
;
62 const char *path
= NULL
, *local
= NULL
;
63 _cleanup_free_
char *ll
= NULL
;
64 _cleanup_close_
int open_fd
= -1;
69 if (isempty(path
) || streq(path
, "-"))
75 local
= basename(path
);
76 if (isempty(local
) || streq(local
, "-"))
80 r
= tar_strip_suffixes(local
, &ll
);
86 if (!machine_name_is_valid(local
)) {
87 log_error("Local image name '%s' is not valid.", local
);
92 r
= image_find(local
, NULL
);
94 return log_error_errno(r
, "Failed to check whether image '%s' exists: %m", local
);
96 log_error_errno(EEXIST
, "Image '%s' already exists.", local
);
104 open_fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
106 return log_error_errno(errno
, "Failed to open tar image to import: %m");
110 log_info("Importing '%s', saving as '%s'.", path
, local
);
112 _cleanup_free_
char *pretty
= NULL
;
116 (void) readlink_malloc("/proc/self/fd/0", &pretty
);
117 log_info("Importing '%s', saving as '%s'.", strna(pretty
), local
);
120 r
= sd_event_default(&event
);
122 return log_error_errno(r
, "Failed to allocate event loop: %m");
124 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGTERM
, SIGINT
, -1) >= 0);
125 (void) sd_event_add_signal(event
, NULL
, SIGTERM
, interrupt_signal_handler
, NULL
);
126 (void) sd_event_add_signal(event
, NULL
, SIGINT
, interrupt_signal_handler
, NULL
);
128 r
= tar_import_new(&import
, event
, arg_image_root
, on_tar_finished
, event
);
130 return log_error_errno(r
, "Failed to allocate importer: %m");
132 r
= tar_import_start(import
, fd
, local
, arg_force
, arg_read_only
);
134 return log_error_errno(r
, "Failed to import image: %m");
136 r
= sd_event_loop(event
);
138 return log_error_errno(r
, "Failed to run event loop: %m");
140 log_info("Exiting.");
144 static void on_raw_finished(RawImport
*import
, int error
, void *userdata
) {
145 sd_event
*event
= userdata
;
149 log_info("Operation completed successfully.");
151 sd_event_exit(event
, abs(error
));
154 static int import_raw(int argc
, char *argv
[], void *userdata
) {
155 _cleanup_(raw_import_unrefp
) RawImport
*import
= NULL
;
156 _cleanup_event_unref_ sd_event
*event
= NULL
;
157 const char *path
= NULL
, *local
= NULL
;
158 _cleanup_free_
char *ll
= NULL
;
159 _cleanup_close_
int open_fd
= -1;
164 if (isempty(path
) || streq(path
, "-"))
170 local
= basename(path
);
171 if (isempty(local
) || streq(local
, "-"))
175 r
= raw_strip_suffixes(local
, &ll
);
181 if (!machine_name_is_valid(local
)) {
182 log_error("Local image name '%s' is not valid.", local
);
187 r
= image_find(local
, NULL
);
189 return log_error_errno(r
, "Failed to check whether image '%s' exists: %m", local
);
191 log_error_errno(EEXIST
, "Image '%s' already exists.", local
);
199 open_fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
201 return log_error_errno(errno
, "Failed to open raw image to import: %m");
205 log_info("Importing '%s', saving as '%s'.", path
, local
);
207 _cleanup_free_
char *pretty
= NULL
;
211 (void) readlink_malloc("/proc/self/fd/0", &pretty
);
212 log_info("Importing '%s', saving as '%s'.", pretty
, local
);
215 r
= sd_event_default(&event
);
217 return log_error_errno(r
, "Failed to allocate event loop: %m");
219 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGTERM
, SIGINT
, -1) >= 0);
220 (void) sd_event_add_signal(event
, NULL
, SIGTERM
, interrupt_signal_handler
, NULL
);
221 (void) sd_event_add_signal(event
, NULL
, SIGINT
, interrupt_signal_handler
, NULL
);
223 r
= raw_import_new(&import
, event
, arg_image_root
, on_raw_finished
, event
);
225 return log_error_errno(r
, "Failed to allocate importer: %m");
227 r
= raw_import_start(import
, fd
, local
, arg_force
, arg_read_only
);
229 return log_error_errno(r
, "Failed to import image: %m");
231 r
= sd_event_loop(event
);
233 return log_error_errno(r
, "Failed to run event loop: %m");
235 log_info("Exiting.");
239 static int help(int argc
, char *argv
[], void *userdata
) {
241 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
242 "Import container or virtual machine images.\n\n"
243 " -h --help Show this help\n"
244 " --version Show package version\n"
245 " --force Force creation of image\n"
246 " --image-root=PATH Image root directory\n"
247 " --read-only Create a read-only image\n\n"
249 " tar FILE [NAME] Import a TAR image\n"
250 " raw FILE [NAME] Import a RAW image\n",
251 program_invocation_short_name
);
256 static int parse_argv(int argc
, char *argv
[]) {
265 static const struct option options
[] = {
266 { "help", no_argument
, NULL
, 'h' },
267 { "version", no_argument
, NULL
, ARG_VERSION
},
268 { "force", no_argument
, NULL
, ARG_FORCE
},
269 { "image-root", required_argument
, NULL
, ARG_IMAGE_ROOT
},
270 { "read-only", no_argument
, NULL
, ARG_READ_ONLY
},
279 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
284 return help(0, NULL
, NULL
);
294 arg_image_root
= optarg
;
298 arg_read_only
= true;
305 assert_not_reached("Unhandled option");
311 static int import_main(int argc
, char *argv
[]) {
313 static const Verb verbs
[] = {
314 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
315 { "tar", 2, 3, 0, import_tar
},
316 { "raw", 2, 3, 0, import_raw
},
320 return dispatch_verb(argc
, argv
, verbs
, NULL
);
323 int main(int argc
, char *argv
[]) {
326 setlocale(LC_ALL
, "");
327 log_parse_environment();
330 r
= parse_argv(argc
, argv
);
334 (void) ignore_signals(SIGPIPE
, -1);
336 r
= import_main(argc
, argv
);
339 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;