1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2015 Lennart Poettering
13 #include "alloc-util.h"
14 #include "export-raw.h"
15 #include "export-tar.h"
18 #include "hostname-util.h"
19 #include "import-util.h"
20 #include "machine-image.h"
21 #include "signal-util.h"
22 #include "string-util.h"
25 static ImportCompressType arg_compress
= IMPORT_COMPRESS_UNKNOWN
;
27 static void determine_compression_from_filename(const char *p
) {
29 if (arg_compress
!= IMPORT_COMPRESS_UNKNOWN
)
33 arg_compress
= IMPORT_COMPRESS_UNCOMPRESSED
;
37 if (endswith(p
, ".xz"))
38 arg_compress
= IMPORT_COMPRESS_XZ
;
39 else if (endswith(p
, ".gz"))
40 arg_compress
= IMPORT_COMPRESS_GZIP
;
41 else if (endswith(p
, ".bz2"))
42 arg_compress
= IMPORT_COMPRESS_BZIP2
;
44 arg_compress
= IMPORT_COMPRESS_UNCOMPRESSED
;
47 static int interrupt_signal_handler(sd_event_source
*s
, const struct signalfd_siginfo
*si
, void *userdata
) {
48 log_notice("Transfer aborted.");
49 sd_event_exit(sd_event_source_get_event(s
), EINTR
);
53 static void on_tar_finished(TarExport
*export
, int error
, void *userdata
) {
54 sd_event
*event
= userdata
;
58 log_info("Operation completed successfully.");
60 sd_event_exit(event
, abs(error
));
63 static int export_tar(int argc
, char *argv
[], void *userdata
) {
64 _cleanup_(tar_export_unrefp
) TarExport
*export
= NULL
;
65 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
66 _cleanup_(image_unrefp
) Image
*image
= NULL
;
67 const char *path
= NULL
, *local
= NULL
;
68 _cleanup_close_
int open_fd
= -1;
71 if (machine_name_is_valid(argv
[1])) {
72 r
= image_find(IMAGE_MACHINE
, argv
[1], &image
);
74 return log_error_errno(r
, "Machine image %s not found.", argv
[1]);
76 return log_error_errno(r
, "Failed to look for machine %s: %m", argv
[1]);
84 if (isempty(path
) || streq(path
, "-"))
87 determine_compression_from_filename(path
);
90 open_fd
= open(path
, O_WRONLY
|O_CREAT
|O_TRUNC
|O_CLOEXEC
|O_NOCTTY
, 0666);
92 return log_error_errno(errno
, "Failed to open tar image for export: %m");
96 log_info("Exporting '%s', saving to '%s' with compression '%s'.", local
, path
, import_compress_type_to_string(arg_compress
));
98 _cleanup_free_
char *pretty
= NULL
;
102 (void) readlink_malloc("/proc/self/fd/1", &pretty
);
103 log_info("Exporting '%s', saving to '%s' with compression '%s'.", local
, strna(pretty
), import_compress_type_to_string(arg_compress
));
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_export_new(&export
, event
, on_tar_finished
, event
);
116 return log_error_errno(r
, "Failed to allocate exporter: %m");
118 r
= tar_export_start(export
, local
, fd
, arg_compress
);
120 return log_error_errno(r
, "Failed to export 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(RawExport
*export
, 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 export_raw(int argc
, char *argv
[], void *userdata
) {
141 _cleanup_(raw_export_unrefp
) RawExport
*export
= NULL
;
142 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
143 _cleanup_(image_unrefp
) Image
*image
= NULL
;
144 const char *path
= NULL
, *local
= NULL
;
145 _cleanup_close_
int open_fd
= -1;
148 if (machine_name_is_valid(argv
[1])) {
149 r
= image_find(IMAGE_MACHINE
, argv
[1], &image
);
151 return log_error_errno(r
, "Machine image %s not found.", argv
[1]);
153 return log_error_errno(r
, "Failed to look for machine %s: %m", argv
[1]);
161 if (isempty(path
) || streq(path
, "-"))
164 determine_compression_from_filename(path
);
167 open_fd
= open(path
, O_WRONLY
|O_CREAT
|O_TRUNC
|O_CLOEXEC
|O_NOCTTY
, 0666);
169 return log_error_errno(errno
, "Failed to open raw image for export: %m");
173 log_info("Exporting '%s', saving to '%s' with compression '%s'.", local
, path
, import_compress_type_to_string(arg_compress
));
175 _cleanup_free_
char *pretty
= NULL
;
179 (void) readlink_malloc("/proc/self/fd/1", &pretty
);
180 log_info("Exporting '%s', saving to '%s' with compression '%s'.", local
, strna(pretty
), import_compress_type_to_string(arg_compress
));
183 r
= sd_event_default(&event
);
185 return log_error_errno(r
, "Failed to allocate event loop: %m");
187 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGTERM
, SIGINT
, -1) >= 0);
188 (void) sd_event_add_signal(event
, NULL
, SIGTERM
, interrupt_signal_handler
, NULL
);
189 (void) sd_event_add_signal(event
, NULL
, SIGINT
, interrupt_signal_handler
, NULL
);
191 r
= raw_export_new(&export
, event
, on_raw_finished
, event
);
193 return log_error_errno(r
, "Failed to allocate exporter: %m");
195 r
= raw_export_start(export
, local
, fd
, arg_compress
);
197 return log_error_errno(r
, "Failed to export image: %m");
199 r
= sd_event_loop(event
);
201 return log_error_errno(r
, "Failed to run event loop: %m");
203 log_info("Exiting.");
207 static int help(int argc
, char *argv
[], void *userdata
) {
209 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
210 "Export container or virtual machine images.\n\n"
211 " -h --help Show this help\n"
212 " --version Show package version\n"
213 " --format=FORMAT Select format\n\n"
215 " tar NAME [FILE] Export a TAR image\n"
216 " raw NAME [FILE] Export a RAW image\n",
217 program_invocation_short_name
);
222 static int parse_argv(int argc
, char *argv
[]) {
229 static const struct option options
[] = {
230 { "help", no_argument
, NULL
, 'h' },
231 { "version", no_argument
, NULL
, ARG_VERSION
},
232 { "format", required_argument
, NULL
, ARG_FORMAT
},
241 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
246 return help(0, NULL
, NULL
);
252 if (streq(optarg
, "uncompressed"))
253 arg_compress
= IMPORT_COMPRESS_UNCOMPRESSED
;
254 else if (streq(optarg
, "xz"))
255 arg_compress
= IMPORT_COMPRESS_XZ
;
256 else if (streq(optarg
, "gzip"))
257 arg_compress
= IMPORT_COMPRESS_GZIP
;
258 else if (streq(optarg
, "bzip2"))
259 arg_compress
= IMPORT_COMPRESS_BZIP2
;
261 log_error("Unknown format: %s", optarg
);
270 assert_not_reached("Unhandled option");
276 static int export_main(int argc
, char *argv
[]) {
278 static const Verb verbs
[] = {
279 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
280 { "tar", 2, 3, 0, export_tar
},
281 { "raw", 2, 3, 0, export_raw
},
285 return dispatch_verb(argc
, argv
, verbs
, NULL
);
288 int main(int argc
, char *argv
[]) {
291 setlocale(LC_ALL
, "");
292 log_parse_environment();
295 r
= parse_argv(argc
, argv
);
299 (void) ignore_signals(SIGPIPE
, -1);
301 r
= export_main(argc
, argv
);
304 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;