1 /* SPDX-License-Identifier: LGPL-2.1+ */
9 #include "alloc-util.h"
12 #include "hostname-util.h"
13 #include "import-raw.h"
14 #include "import-tar.h"
15 #include "import-util.h"
16 #include "machine-image.h"
17 #include "main-func.h"
18 #include "signal-util.h"
19 #include "string-util.h"
22 static bool arg_force
= false;
23 static bool arg_read_only
= false;
24 static const char *arg_image_root
= "/var/lib/machines";
26 static int interrupt_signal_handler(sd_event_source
*s
, const struct signalfd_siginfo
*si
, void *userdata
) {
27 log_notice("Transfer aborted.");
28 sd_event_exit(sd_event_source_get_event(s
), EINTR
);
32 static void on_tar_finished(TarImport
*import
, int error
, void *userdata
) {
33 sd_event
*event
= userdata
;
37 log_info("Operation completed successfully.");
39 sd_event_exit(event
, abs(error
));
42 static int import_tar(int argc
, char *argv
[], void *userdata
) {
43 _cleanup_(tar_import_unrefp
) TarImport
*import
= NULL
;
44 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
45 const char *path
= NULL
, *local
= NULL
;
46 _cleanup_free_
char *ll
= NULL
;
47 _cleanup_close_
int open_fd
= -1;
52 path
= empty_or_dash_to_null(path
);
57 local
= basename(path
);
58 local
= empty_or_dash_to_null(local
);
61 r
= tar_strip_suffixes(local
, &ll
);
67 if (!machine_name_is_valid(local
)) {
68 log_error("Local image name '%s' is not valid.", local
);
73 r
= image_find(IMAGE_MACHINE
, local
, NULL
);
76 return log_error_errno(r
, "Failed to check whether image '%s' exists: %m", local
);
78 log_error("Image '%s' already exists.", local
);
86 open_fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
88 return log_error_errno(errno
, "Failed to open tar image to import: %m");
92 log_info("Importing '%s', saving as '%s'.", path
, local
);
94 _cleanup_free_
char *pretty
= NULL
;
98 (void) fd_get_path(fd
, &pretty
);
99 log_info("Importing '%s', saving as '%s'.", strna(pretty
), local
);
102 r
= sd_event_default(&event
);
104 return log_error_errno(r
, "Failed to allocate event loop: %m");
106 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGTERM
, SIGINT
, -1) >= 0);
107 (void) sd_event_add_signal(event
, NULL
, SIGTERM
, interrupt_signal_handler
, NULL
);
108 (void) sd_event_add_signal(event
, NULL
, SIGINT
, interrupt_signal_handler
, NULL
);
110 r
= tar_import_new(&import
, event
, arg_image_root
, on_tar_finished
, event
);
112 return log_error_errno(r
, "Failed to allocate importer: %m");
114 r
= tar_import_start(import
, fd
, local
, arg_force
, arg_read_only
);
116 return log_error_errno(r
, "Failed to import image: %m");
118 r
= sd_event_loop(event
);
120 return log_error_errno(r
, "Failed to run event loop: %m");
122 log_info("Exiting.");
126 static void on_raw_finished(RawImport
*import
, int error
, void *userdata
) {
127 sd_event
*event
= userdata
;
131 log_info("Operation completed successfully.");
133 sd_event_exit(event
, abs(error
));
136 static int import_raw(int argc
, char *argv
[], void *userdata
) {
137 _cleanup_(raw_import_unrefp
) RawImport
*import
= NULL
;
138 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
139 const char *path
= NULL
, *local
= NULL
;
140 _cleanup_free_
char *ll
= NULL
;
141 _cleanup_close_
int open_fd
= -1;
146 path
= empty_or_dash_to_null(path
);
151 local
= basename(path
);
152 local
= empty_or_dash_to_null(local
);
155 r
= raw_strip_suffixes(local
, &ll
);
161 if (!machine_name_is_valid(local
)) {
162 log_error("Local image name '%s' is not valid.", local
);
167 r
= image_find(IMAGE_MACHINE
, local
, NULL
);
170 return log_error_errno(r
, "Failed to check whether image '%s' exists: %m", local
);
172 log_error("Image '%s' already exists.", local
);
180 open_fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
182 return log_error_errno(errno
, "Failed to open raw image to import: %m");
186 log_info("Importing '%s', saving as '%s'.", path
, local
);
188 _cleanup_free_
char *pretty
= NULL
;
192 (void) fd_get_path(fd
, &pretty
);
193 log_info("Importing '%s', saving as '%s'.", strempty(pretty
), local
);
196 r
= sd_event_default(&event
);
198 return log_error_errno(r
, "Failed to allocate event loop: %m");
200 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGTERM
, SIGINT
, -1) >= 0);
201 (void) sd_event_add_signal(event
, NULL
, SIGTERM
, interrupt_signal_handler
, NULL
);
202 (void) sd_event_add_signal(event
, NULL
, SIGINT
, interrupt_signal_handler
, NULL
);
204 r
= raw_import_new(&import
, event
, arg_image_root
, on_raw_finished
, event
);
206 return log_error_errno(r
, "Failed to allocate importer: %m");
208 r
= raw_import_start(import
, fd
, local
, arg_force
, arg_read_only
);
210 return log_error_errno(r
, "Failed to import image: %m");
212 r
= sd_event_loop(event
);
214 return log_error_errno(r
, "Failed to run event loop: %m");
216 log_info("Exiting.");
220 static int help(int argc
, char *argv
[], void *userdata
) {
222 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
223 "Import container or virtual machine images.\n\n"
224 " -h --help Show this help\n"
225 " --version Show package version\n"
226 " --force Force creation of image\n"
227 " --image-root=PATH Image root directory\n"
228 " --read-only Create a read-only image\n\n"
230 " tar FILE [NAME] Import a TAR image\n"
231 " raw FILE [NAME] Import a RAW image\n",
232 program_invocation_short_name
);
237 static int parse_argv(int argc
, char *argv
[]) {
246 static const struct option options
[] = {
247 { "help", no_argument
, NULL
, 'h' },
248 { "version", no_argument
, NULL
, ARG_VERSION
},
249 { "force", no_argument
, NULL
, ARG_FORCE
},
250 { "image-root", required_argument
, NULL
, ARG_IMAGE_ROOT
},
251 { "read-only", no_argument
, NULL
, ARG_READ_ONLY
},
260 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
265 return help(0, NULL
, NULL
);
275 arg_image_root
= optarg
;
279 arg_read_only
= true;
286 assert_not_reached("Unhandled option");
292 static int import_main(int argc
, char *argv
[]) {
293 static const Verb verbs
[] = {
294 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
295 { "tar", 2, 3, 0, import_tar
},
296 { "raw", 2, 3, 0, import_raw
},
300 return dispatch_verb(argc
, argv
, verbs
, NULL
);
303 static int run(int argc
, char *argv
[]) {
306 setlocale(LC_ALL
, "");
307 log_parse_environment();
310 r
= parse_argv(argc
, argv
);
314 (void) ignore_signals(SIGPIPE
, -1);
316 return import_main(argc
, argv
);
319 DEFINE_MAIN_FUNCTION(run
);