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"
27 #include "hostname-util.h"
28 #include "import-raw.h"
29 #include "import-tar.h"
30 #include "import-util.h"
31 #include "machine-image.h"
32 #include "signal-util.h"
35 static bool arg_force
= false;
36 static bool arg_read_only
= false;
37 static const char *arg_image_root
= "/var/lib/machines";
39 static int interrupt_signal_handler(sd_event_source
*s
, const struct signalfd_siginfo
*si
, void *userdata
) {
40 log_notice("Transfer aborted.");
41 sd_event_exit(sd_event_source_get_event(s
), EINTR
);
45 static void on_tar_finished(TarImport
*import
, int error
, void *userdata
) {
46 sd_event
*event
= userdata
;
50 log_info("Operation completed successfully.");
52 sd_event_exit(event
, abs(error
));
55 static int import_tar(int argc
, char *argv
[], void *userdata
) {
56 _cleanup_(tar_import_unrefp
) TarImport
*import
= NULL
;
57 _cleanup_event_unref_ sd_event
*event
= NULL
;
58 const char *path
= NULL
, *local
= NULL
;
59 _cleanup_free_
char *ll
= NULL
;
60 _cleanup_close_
int open_fd
= -1;
65 if (isempty(path
) || streq(path
, "-"))
71 local
= basename(path
);
72 if (isempty(local
) || streq(local
, "-"))
76 r
= tar_strip_suffixes(local
, &ll
);
82 if (!machine_name_is_valid(local
)) {
83 log_error("Local image name '%s' is not valid.", local
);
88 r
= image_find(local
, NULL
);
90 return log_error_errno(r
, "Failed to check whether image '%s' exists: %m", local
);
92 log_error_errno(EEXIST
, "Image '%s' already exists.", local
);
100 open_fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
102 return log_error_errno(errno
, "Failed to open tar image to import: %m");
106 log_info("Importing '%s', saving as '%s'.", path
, local
);
108 _cleanup_free_
char *pretty
= NULL
;
112 (void) readlink_malloc("/proc/self/fd/0", &pretty
);
113 log_info("Importing '%s', saving as '%s'.", strna(pretty
), local
);
116 r
= sd_event_default(&event
);
118 return log_error_errno(r
, "Failed to allocate event loop: %m");
120 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGTERM
, SIGINT
, -1) >= 0);
121 (void) sd_event_add_signal(event
, NULL
, SIGTERM
, interrupt_signal_handler
, NULL
);
122 (void) sd_event_add_signal(event
, NULL
, SIGINT
, interrupt_signal_handler
, NULL
);
124 r
= tar_import_new(&import
, event
, arg_image_root
, on_tar_finished
, event
);
126 return log_error_errno(r
, "Failed to allocate importer: %m");
128 r
= tar_import_start(import
, fd
, local
, arg_force
, arg_read_only
);
130 return log_error_errno(r
, "Failed to import image: %m");
132 r
= sd_event_loop(event
);
134 return log_error_errno(r
, "Failed to run event loop: %m");
136 log_info("Exiting.");
140 static void on_raw_finished(RawImport
*import
, int error
, void *userdata
) {
141 sd_event
*event
= userdata
;
145 log_info("Operation completed successfully.");
147 sd_event_exit(event
, abs(error
));
150 static int import_raw(int argc
, char *argv
[], void *userdata
) {
151 _cleanup_(raw_import_unrefp
) RawImport
*import
= NULL
;
152 _cleanup_event_unref_ sd_event
*event
= NULL
;
153 const char *path
= NULL
, *local
= NULL
;
154 _cleanup_free_
char *ll
= NULL
;
155 _cleanup_close_
int open_fd
= -1;
160 if (isempty(path
) || streq(path
, "-"))
166 local
= basename(path
);
167 if (isempty(local
) || streq(local
, "-"))
171 r
= raw_strip_suffixes(local
, &ll
);
177 if (!machine_name_is_valid(local
)) {
178 log_error("Local image name '%s' is not valid.", local
);
183 r
= image_find(local
, NULL
);
185 return log_error_errno(r
, "Failed to check whether image '%s' exists: %m", local
);
187 log_error_errno(EEXIST
, "Image '%s' already exists.", local
);
195 open_fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
197 return log_error_errno(errno
, "Failed to open raw image to import: %m");
201 log_info("Importing '%s', saving as '%s'.", path
, local
);
203 _cleanup_free_
char *pretty
= NULL
;
207 (void) readlink_malloc("/proc/self/fd/0", &pretty
);
208 log_info("Importing '%s', saving as '%s'.", pretty
, local
);
211 r
= sd_event_default(&event
);
213 return log_error_errno(r
, "Failed to allocate event loop: %m");
215 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGTERM
, SIGINT
, -1) >= 0);
216 (void) sd_event_add_signal(event
, NULL
, SIGTERM
, interrupt_signal_handler
, NULL
);
217 (void) sd_event_add_signal(event
, NULL
, SIGINT
, interrupt_signal_handler
, NULL
);
219 r
= raw_import_new(&import
, event
, arg_image_root
, on_raw_finished
, event
);
221 return log_error_errno(r
, "Failed to allocate importer: %m");
223 r
= raw_import_start(import
, fd
, local
, arg_force
, arg_read_only
);
225 return log_error_errno(r
, "Failed to import image: %m");
227 r
= sd_event_loop(event
);
229 return log_error_errno(r
, "Failed to run event loop: %m");
231 log_info("Exiting.");
235 static int help(int argc
, char *argv
[], void *userdata
) {
237 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
238 "Import container or virtual machine images.\n\n"
239 " -h --help Show this help\n"
240 " --version Show package version\n"
241 " --force Force creation of image\n"
242 " --image-root=PATH Image root directory\n"
243 " --read-only Create a read-only image\n\n"
245 " tar FILE [NAME] Import a TAR image\n"
246 " raw FILE [NAME] Import a RAW image\n",
247 program_invocation_short_name
);
252 static int parse_argv(int argc
, char *argv
[]) {
261 static const struct option options
[] = {
262 { "help", no_argument
, NULL
, 'h' },
263 { "version", no_argument
, NULL
, ARG_VERSION
},
264 { "force", no_argument
, NULL
, ARG_FORCE
},
265 { "image-root", required_argument
, NULL
, ARG_IMAGE_ROOT
},
266 { "read-only", no_argument
, NULL
, ARG_READ_ONLY
},
275 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
280 return help(0, NULL
, NULL
);
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 (void) ignore_signals(SIGPIPE
, -1);
332 r
= import_main(argc
, argv
);
335 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;