1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2015 Lennart Poettering
13 #include "alloc-util.h"
16 #include "hostname-util.h"
17 #include "import-raw.h"
18 #include "import-tar.h"
19 #include "import-util.h"
20 #include "machine-image.h"
21 #include "signal-util.h"
22 #include "string-util.h"
25 static bool arg_force
= false;
26 static bool arg_read_only
= false;
27 static const char *arg_image_root
= "/var/lib/machines";
29 static int interrupt_signal_handler(sd_event_source
*s
, const struct signalfd_siginfo
*si
, void *userdata
) {
30 log_notice("Transfer aborted.");
31 sd_event_exit(sd_event_source_get_event(s
), EINTR
);
35 static void on_tar_finished(TarImport
*import
, int error
, void *userdata
) {
36 sd_event
*event
= userdata
;
40 log_info("Operation completed successfully.");
42 sd_event_exit(event
, abs(error
));
45 static int import_tar(int argc
, char *argv
[], void *userdata
) {
46 _cleanup_(tar_import_unrefp
) TarImport
*import
= NULL
;
47 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
48 const char *path
= NULL
, *local
= NULL
;
49 _cleanup_free_
char *ll
= NULL
;
50 _cleanup_close_
int open_fd
= -1;
55 if (isempty(path
) || streq(path
, "-"))
61 local
= basename(path
);
62 if (isempty(local
) || streq(local
, "-"))
66 r
= tar_strip_suffixes(local
, &ll
);
72 if (!machine_name_is_valid(local
)) {
73 log_error("Local image name '%s' is not valid.", local
);
78 r
= image_find(local
, NULL
);
80 return log_error_errno(r
, "Failed to check whether image '%s' exists: %m", local
);
82 log_error("Image '%s' already exists.", local
);
90 open_fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
92 return log_error_errno(errno
, "Failed to open tar image to import: %m");
96 log_info("Importing '%s', saving as '%s'.", path
, local
);
98 _cleanup_free_
char *pretty
= NULL
;
102 (void) readlink_malloc("/proc/self/fd/0", &pretty
);
103 log_info("Importing '%s', saving as '%s'.", strna(pretty
), local
);
106 r
= sd_event_default(&event
);
108 return log_error_errno(r
, "Failed to allocate event loop: %m");
110 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGTERM
, SIGINT
, -1) >= 0);
111 (void) sd_event_add_signal(event
, NULL
, SIGTERM
, interrupt_signal_handler
, NULL
);
112 (void) sd_event_add_signal(event
, NULL
, SIGINT
, interrupt_signal_handler
, NULL
);
114 r
= tar_import_new(&import
, event
, arg_image_root
, on_tar_finished
, event
);
116 return log_error_errno(r
, "Failed to allocate importer: %m");
118 r
= tar_import_start(import
, fd
, local
, arg_force
, arg_read_only
);
120 return log_error_errno(r
, "Failed to import image: %m");
122 r
= sd_event_loop(event
);
124 return log_error_errno(r
, "Failed to run event loop: %m");
126 log_info("Exiting.");
130 static void on_raw_finished(RawImport
*import
, int error
, void *userdata
) {
131 sd_event
*event
= userdata
;
135 log_info("Operation completed successfully.");
137 sd_event_exit(event
, abs(error
));
140 static int import_raw(int argc
, char *argv
[], void *userdata
) {
141 _cleanup_(raw_import_unrefp
) RawImport
*import
= NULL
;
142 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
143 const char *path
= NULL
, *local
= NULL
;
144 _cleanup_free_
char *ll
= NULL
;
145 _cleanup_close_
int open_fd
= -1;
150 if (isempty(path
) || streq(path
, "-"))
156 local
= basename(path
);
157 if (isempty(local
) || streq(local
, "-"))
161 r
= raw_strip_suffixes(local
, &ll
);
167 if (!machine_name_is_valid(local
)) {
168 log_error("Local image name '%s' is not valid.", local
);
173 r
= image_find(local
, NULL
);
175 return log_error_errno(r
, "Failed to check whether image '%s' exists: %m", local
);
177 log_error("Image '%s' already exists.", local
);
185 open_fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
187 return log_error_errno(errno
, "Failed to open raw image to import: %m");
191 log_info("Importing '%s', saving as '%s'.", path
, local
);
193 _cleanup_free_
char *pretty
= NULL
;
197 (void) readlink_malloc("/proc/self/fd/0", &pretty
);
198 log_info("Importing '%s', saving as '%s'.", pretty
, local
);
201 r
= sd_event_default(&event
);
203 return log_error_errno(r
, "Failed to allocate event loop: %m");
205 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGTERM
, SIGINT
, -1) >= 0);
206 (void) sd_event_add_signal(event
, NULL
, SIGTERM
, interrupt_signal_handler
, NULL
);
207 (void) sd_event_add_signal(event
, NULL
, SIGINT
, interrupt_signal_handler
, NULL
);
209 r
= raw_import_new(&import
, event
, arg_image_root
, on_raw_finished
, event
);
211 return log_error_errno(r
, "Failed to allocate importer: %m");
213 r
= raw_import_start(import
, fd
, local
, arg_force
, arg_read_only
);
215 return log_error_errno(r
, "Failed to import image: %m");
217 r
= sd_event_loop(event
);
219 return log_error_errno(r
, "Failed to run event loop: %m");
221 log_info("Exiting.");
225 static int help(int argc
, char *argv
[], void *userdata
) {
227 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
228 "Import container or virtual machine images.\n\n"
229 " -h --help Show this help\n"
230 " --version Show package version\n"
231 " --force Force creation of image\n"
232 " --image-root=PATH Image root directory\n"
233 " --read-only Create a read-only image\n\n"
235 " tar FILE [NAME] Import a TAR image\n"
236 " raw FILE [NAME] Import a RAW image\n",
237 program_invocation_short_name
);
242 static int parse_argv(int argc
, char *argv
[]) {
251 static const struct option options
[] = {
252 { "help", no_argument
, NULL
, 'h' },
253 { "version", no_argument
, NULL
, ARG_VERSION
},
254 { "force", no_argument
, NULL
, ARG_FORCE
},
255 { "image-root", required_argument
, NULL
, ARG_IMAGE_ROOT
},
256 { "read-only", no_argument
, NULL
, ARG_READ_ONLY
},
265 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
270 return help(0, NULL
, NULL
);
280 arg_image_root
= optarg
;
284 arg_read_only
= true;
291 assert_not_reached("Unhandled option");
297 static int import_main(int argc
, char *argv
[]) {
299 static const Verb verbs
[] = {
300 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
301 { "tar", 2, 3, 0, import_tar
},
302 { "raw", 2, 3, 0, import_raw
},
306 return dispatch_verb(argc
, argv
, verbs
, NULL
);
309 int main(int argc
, char *argv
[]) {
312 setlocale(LC_ALL
, "");
313 log_parse_environment();
316 r
= parse_argv(argc
, argv
);
320 (void) ignore_signals(SIGPIPE
, -1);
322 r
= import_main(argc
, argv
);
325 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;