1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2015 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include "alloc-util.h"
29 #include "hostname-util.h"
30 #include "import-raw.h"
31 #include "import-tar.h"
32 #include "import-util.h"
33 #include "machine-image.h"
34 #include "signal-util.h"
35 #include "string-util.h"
38 static bool arg_force
= false;
39 static bool arg_read_only
= false;
40 static const char *arg_image_root
= "/var/lib/machines";
42 static int interrupt_signal_handler(sd_event_source
*s
, const struct signalfd_siginfo
*si
, void *userdata
) {
43 log_notice("Transfer aborted.");
44 sd_event_exit(sd_event_source_get_event(s
), EINTR
);
48 static void on_tar_finished(TarImport
*import
, int error
, void *userdata
) {
49 sd_event
*event
= userdata
;
53 log_info("Operation completed successfully.");
55 sd_event_exit(event
, abs(error
));
58 static int import_tar(int argc
, char *argv
[], void *userdata
) {
59 _cleanup_(tar_import_unrefp
) TarImport
*import
= NULL
;
60 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
61 const char *path
= NULL
, *local
= NULL
;
62 _cleanup_free_
char *ll
= NULL
;
63 _cleanup_close_
int open_fd
= -1;
68 if (isempty(path
) || streq(path
, "-"))
74 local
= basename(path
);
75 if (isempty(local
) || streq(local
, "-"))
79 r
= tar_strip_suffixes(local
, &ll
);
85 if (!machine_name_is_valid(local
)) {
86 log_error("Local image name '%s' is not valid.", local
);
91 r
= image_find(local
, NULL
);
93 return log_error_errno(r
, "Failed to check whether image '%s' exists: %m", local
);
95 log_error("Image '%s' already exists.", local
);
103 open_fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
105 return log_error_errno(errno
, "Failed to open tar image to import: %m");
109 log_info("Importing '%s', saving as '%s'.", path
, local
);
111 _cleanup_free_
char *pretty
= NULL
;
115 (void) readlink_malloc("/proc/self/fd/0", &pretty
);
116 log_info("Importing '%s', saving as '%s'.", strna(pretty
), local
);
119 r
= sd_event_default(&event
);
121 return log_error_errno(r
, "Failed to allocate event loop: %m");
123 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGTERM
, SIGINT
, -1) >= 0);
124 (void) sd_event_add_signal(event
, NULL
, SIGTERM
, interrupt_signal_handler
, NULL
);
125 (void) sd_event_add_signal(event
, NULL
, SIGINT
, interrupt_signal_handler
, NULL
);
127 r
= tar_import_new(&import
, event
, arg_image_root
, on_tar_finished
, event
);
129 return log_error_errno(r
, "Failed to allocate importer: %m");
131 r
= tar_import_start(import
, fd
, local
, arg_force
, arg_read_only
);
133 return log_error_errno(r
, "Failed to import image: %m");
135 r
= sd_event_loop(event
);
137 return log_error_errno(r
, "Failed to run event loop: %m");
139 log_info("Exiting.");
143 static void on_raw_finished(RawImport
*import
, int error
, void *userdata
) {
144 sd_event
*event
= userdata
;
148 log_info("Operation completed successfully.");
150 sd_event_exit(event
, abs(error
));
153 static int import_raw(int argc
, char *argv
[], void *userdata
) {
154 _cleanup_(raw_import_unrefp
) RawImport
*import
= NULL
;
155 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
156 const char *path
= NULL
, *local
= NULL
;
157 _cleanup_free_
char *ll
= NULL
;
158 _cleanup_close_
int open_fd
= -1;
163 if (isempty(path
) || streq(path
, "-"))
169 local
= basename(path
);
170 if (isempty(local
) || streq(local
, "-"))
174 r
= raw_strip_suffixes(local
, &ll
);
180 if (!machine_name_is_valid(local
)) {
181 log_error("Local image name '%s' is not valid.", local
);
186 r
= image_find(local
, NULL
);
188 return log_error_errno(r
, "Failed to check whether image '%s' exists: %m", local
);
190 log_error("Image '%s' already exists.", local
);
198 open_fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
200 return log_error_errno(errno
, "Failed to open raw image to import: %m");
204 log_info("Importing '%s', saving as '%s'.", path
, local
);
206 _cleanup_free_
char *pretty
= NULL
;
210 (void) readlink_malloc("/proc/self/fd/0", &pretty
);
211 log_info("Importing '%s', saving as '%s'.", pretty
, local
);
214 r
= sd_event_default(&event
);
216 return log_error_errno(r
, "Failed to allocate event loop: %m");
218 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGTERM
, SIGINT
, -1) >= 0);
219 (void) sd_event_add_signal(event
, NULL
, SIGTERM
, interrupt_signal_handler
, NULL
);
220 (void) sd_event_add_signal(event
, NULL
, SIGINT
, interrupt_signal_handler
, NULL
);
222 r
= raw_import_new(&import
, event
, arg_image_root
, on_raw_finished
, event
);
224 return log_error_errno(r
, "Failed to allocate importer: %m");
226 r
= raw_import_start(import
, fd
, local
, arg_force
, arg_read_only
);
228 return log_error_errno(r
, "Failed to import image: %m");
230 r
= sd_event_loop(event
);
232 return log_error_errno(r
, "Failed to run event loop: %m");
234 log_info("Exiting.");
238 static int help(int argc
, char *argv
[], void *userdata
) {
240 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
241 "Import container or virtual machine images.\n\n"
242 " -h --help Show this help\n"
243 " --version Show package version\n"
244 " --force Force creation of image\n"
245 " --image-root=PATH Image root directory\n"
246 " --read-only Create a read-only image\n\n"
248 " tar FILE [NAME] Import a TAR image\n"
249 " raw FILE [NAME] Import a RAW image\n",
250 program_invocation_short_name
);
255 static int parse_argv(int argc
, char *argv
[]) {
264 static const struct option options
[] = {
265 { "help", no_argument
, NULL
, 'h' },
266 { "version", no_argument
, NULL
, ARG_VERSION
},
267 { "force", no_argument
, NULL
, ARG_FORCE
},
268 { "image-root", required_argument
, NULL
, ARG_IMAGE_ROOT
},
269 { "read-only", no_argument
, NULL
, ARG_READ_ONLY
},
278 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
283 return help(0, NULL
, NULL
);
293 arg_image_root
= optarg
;
297 arg_read_only
= true;
304 assert_not_reached("Unhandled option");
310 static int import_main(int argc
, char *argv
[]) {
312 static const Verb verbs
[] = {
313 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
314 { "tar", 2, 3, 0, import_tar
},
315 { "raw", 2, 3, 0, import_raw
},
319 return dispatch_verb(argc
, argv
, verbs
, NULL
);
322 int main(int argc
, char *argv
[]) {
325 setlocale(LC_ALL
, "");
326 log_parse_environment();
329 r
= parse_argv(argc
, argv
);
333 (void) ignore_signals(SIGPIPE
, -1);
335 r
= import_main(argc
, argv
);
338 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;