]>
Commit | Line | Data |
---|---|---|
587fec42 LP |
1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
2 | ||
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2015 Lennart Poettering | |
7 | ||
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. | |
12 | ||
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. | |
17 | ||
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/>. | |
20 | ***/ | |
21 | ||
22 | #include <getopt.h> | |
23 | ||
24 | #include "sd-event.h" | |
25 | #include "event-util.h" | |
24882e06 | 26 | #include "signal-util.h" |
587fec42 LP |
27 | #include "verbs.h" |
28 | #include "build.h" | |
29 | #include "machine-image.h" | |
30 | #include "import-util.h" | |
31 | #include "export-tar.h" | |
32 | #include "export-raw.h" | |
33 | ||
34 | static ImportCompressType arg_compress = IMPORT_COMPRESS_UNKNOWN; | |
35 | ||
36 | static void determine_compression_from_filename(const char *p) { | |
37 | ||
38 | if (arg_compress != IMPORT_COMPRESS_UNKNOWN) | |
39 | return; | |
40 | ||
41 | if (!p) { | |
42 | arg_compress = IMPORT_COMPRESS_UNCOMPRESSED; | |
43 | return; | |
44 | } | |
45 | ||
46 | if (endswith(p, ".xz")) | |
47 | arg_compress = IMPORT_COMPRESS_XZ; | |
48 | else if (endswith(p, ".gz")) | |
49 | arg_compress = IMPORT_COMPRESS_GZIP; | |
50 | else if (endswith(p, ".bz2")) | |
51 | arg_compress = IMPORT_COMPRESS_BZIP2; | |
52 | else | |
53 | arg_compress = IMPORT_COMPRESS_UNCOMPRESSED; | |
54 | } | |
55 | ||
56 | static int interrupt_signal_handler(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) { | |
57 | log_notice("Transfer aborted."); | |
58 | sd_event_exit(sd_event_source_get_event(s), EINTR); | |
59 | return 0; | |
60 | } | |
61 | ||
62 | static void on_tar_finished(TarExport *export, int error, void *userdata) { | |
63 | sd_event *event = userdata; | |
64 | assert(export); | |
65 | ||
66 | if (error == 0) | |
67 | log_info("Operation completed successfully."); | |
68 | ||
69 | sd_event_exit(event, abs(error)); | |
70 | } | |
71 | ||
72 | static int export_tar(int argc, char *argv[], void *userdata) { | |
73 | _cleanup_(tar_export_unrefp) TarExport *export = NULL; | |
74 | _cleanup_event_unref_ sd_event *event = NULL; | |
75 | _cleanup_(image_unrefp) Image *image = NULL; | |
76 | const char *path = NULL, *local = NULL; | |
77 | _cleanup_close_ int open_fd = -1; | |
78 | int r, fd; | |
79 | ||
80 | if (machine_name_is_valid(argv[1])) { | |
81 | r = image_find(argv[1], &image); | |
82 | if (r < 0) | |
83 | return log_error_errno(r, "Failed to look for machine %s: %m", argv[1]); | |
84 | if (r == 0) { | |
85 | log_error("Machine image %s not found.", argv[1]); | |
86 | return -ENOENT; | |
87 | } | |
88 | ||
89 | local = image->path; | |
90 | } else | |
91 | local = argv[1]; | |
92 | ||
93 | if (argc >= 3) | |
94 | path = argv[2]; | |
95 | if (isempty(path) || streq(path, "-")) | |
96 | path = NULL; | |
97 | ||
98 | determine_compression_from_filename(path); | |
99 | ||
100 | if (path) { | |
101 | open_fd = open(path, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC|O_NOCTTY, 0666); | |
102 | if (open_fd < 0) | |
103 | return log_error_errno(errno, "Failed to open tar image for export: %m"); | |
104 | ||
105 | fd = open_fd; | |
106 | ||
107 | log_info("Exporting '%s', saving to '%s' with compression '%s'.", local, path, import_compress_type_to_string(arg_compress)); | |
108 | } else { | |
109 | _cleanup_free_ char *pretty = NULL; | |
110 | ||
111 | fd = STDOUT_FILENO; | |
112 | ||
113 | (void) readlink_malloc("/proc/self/fd/1", &pretty); | |
114 | log_info("Exporting '%s', saving to '%s' with compression '%s'.", local, strna(pretty), import_compress_type_to_string(arg_compress)); | |
115 | } | |
116 | ||
117 | r = sd_event_default(&event); | |
118 | if (r < 0) | |
119 | return log_error_errno(r, "Failed to allocate event loop: %m"); | |
120 | ||
121 | assert_se(sigprocmask_many(SIG_BLOCK, SIGTERM, SIGINT, -1) == 0); | |
122 | sd_event_add_signal(event, NULL, SIGTERM, interrupt_signal_handler, NULL); | |
123 | sd_event_add_signal(event, NULL, SIGINT, interrupt_signal_handler, NULL); | |
124 | ||
125 | r = tar_export_new(&export, event, on_tar_finished, event); | |
126 | if (r < 0) | |
127 | return log_error_errno(r, "Failed to allocate exporter: %m"); | |
128 | ||
129 | r = tar_export_start(export, local, fd, arg_compress); | |
130 | if (r < 0) | |
131 | return log_error_errno(r, "Failed to export image: %m"); | |
132 | ||
133 | r = sd_event_loop(event); | |
134 | if (r < 0) | |
135 | return log_error_errno(r, "Failed to run event loop: %m"); | |
136 | ||
137 | log_info("Exiting."); | |
138 | return -r; | |
139 | } | |
140 | ||
141 | static void on_raw_finished(RawExport *export, int error, void *userdata) { | |
142 | sd_event *event = userdata; | |
143 | assert(export); | |
144 | ||
145 | if (error == 0) | |
146 | log_info("Operation completed successfully."); | |
147 | ||
148 | sd_event_exit(event, abs(error)); | |
149 | } | |
150 | ||
151 | static int export_raw(int argc, char *argv[], void *userdata) { | |
152 | _cleanup_(raw_export_unrefp) RawExport *export = NULL; | |
153 | _cleanup_event_unref_ sd_event *event = NULL; | |
154 | _cleanup_(image_unrefp) Image *image = NULL; | |
155 | const char *path = NULL, *local = NULL; | |
156 | _cleanup_close_ int open_fd = -1; | |
157 | int r, fd; | |
158 | ||
159 | if (machine_name_is_valid(argv[1])) { | |
160 | r = image_find(argv[1], &image); | |
161 | if (r < 0) | |
162 | return log_error_errno(r, "Failed to look for machine %s: %m", argv[1]); | |
163 | if (r == 0) { | |
164 | log_error("Machine image %s not found.", argv[1]); | |
165 | return -ENOENT; | |
166 | } | |
167 | ||
168 | local = image->path; | |
169 | } else | |
170 | local = argv[1]; | |
171 | ||
172 | if (argc >= 3) | |
173 | path = argv[2]; | |
174 | if (isempty(path) || streq(path, "-")) | |
175 | path = NULL; | |
176 | ||
177 | determine_compression_from_filename(path); | |
178 | ||
179 | if (path) { | |
180 | open_fd = open(path, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC|O_NOCTTY, 0666); | |
181 | if (open_fd < 0) | |
182 | return log_error_errno(errno, "Failed to open raw image for export: %m"); | |
183 | ||
184 | fd = open_fd; | |
185 | ||
186 | log_info("Exporting '%s', saving to '%s' with compression '%s'.", local, path, import_compress_type_to_string(arg_compress)); | |
187 | } else { | |
188 | _cleanup_free_ char *pretty = NULL; | |
189 | ||
190 | fd = STDOUT_FILENO; | |
191 | ||
192 | (void) readlink_malloc("/proc/self/fd/1", &pretty); | |
193 | log_info("Exporting '%s', saving to '%s' with compression '%s'.", local, strna(pretty), import_compress_type_to_string(arg_compress)); | |
194 | } | |
195 | ||
196 | r = sd_event_default(&event); | |
197 | if (r < 0) | |
198 | return log_error_errno(r, "Failed to allocate event loop: %m"); | |
199 | ||
200 | assert_se(sigprocmask_many(SIG_BLOCK, SIGTERM, SIGINT, -1) == 0); | |
201 | sd_event_add_signal(event, NULL, SIGTERM, interrupt_signal_handler, NULL); | |
202 | sd_event_add_signal(event, NULL, SIGINT, interrupt_signal_handler, NULL); | |
203 | ||
204 | r = raw_export_new(&export, event, on_raw_finished, event); | |
205 | if (r < 0) | |
206 | return log_error_errno(r, "Failed to allocate exporter: %m"); | |
207 | ||
208 | r = raw_export_start(export, local, fd, arg_compress); | |
209 | if (r < 0) | |
210 | return log_error_errno(r, "Failed to export image: %m"); | |
211 | ||
212 | r = sd_event_loop(event); | |
213 | if (r < 0) | |
214 | return log_error_errno(r, "Failed to run event loop: %m"); | |
215 | ||
216 | log_info("Exiting."); | |
217 | return -r; | |
218 | } | |
219 | ||
220 | static int help(int argc, char *argv[], void *userdata) { | |
221 | ||
222 | printf("%s [OPTIONS...] {COMMAND} ...\n\n" | |
223 | "Export container or virtual machine images.\n\n" | |
224 | " -h --help Show this help\n" | |
225 | " --version Show package version\n" | |
226 | " --format=FORMAT Select format\n\n" | |
227 | "Commands:\n" | |
228 | " tar NAME [FILE] Export a TAR image\n" | |
229 | " raw NAME [FILE] Export a RAW image\n", | |
230 | program_invocation_short_name); | |
231 | ||
232 | return 0; | |
233 | } | |
234 | ||
235 | static int parse_argv(int argc, char *argv[]) { | |
236 | ||
237 | enum { | |
238 | ARG_VERSION = 0x100, | |
239 | ARG_FORMAT, | |
240 | }; | |
241 | ||
242 | static const struct option options[] = { | |
243 | { "help", no_argument, NULL, 'h' }, | |
244 | { "version", no_argument, NULL, ARG_VERSION }, | |
245 | { "format", required_argument, NULL, ARG_FORMAT }, | |
246 | {} | |
247 | }; | |
248 | ||
249 | int c; | |
250 | ||
251 | assert(argc >= 0); | |
252 | assert(argv); | |
253 | ||
254 | while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) | |
255 | ||
256 | switch (c) { | |
257 | ||
258 | case 'h': | |
259 | return help(0, NULL, NULL); | |
260 | ||
261 | case ARG_VERSION: | |
262 | puts(PACKAGE_STRING); | |
263 | puts(SYSTEMD_FEATURES); | |
264 | return 0; | |
265 | ||
266 | case ARG_FORMAT: | |
267 | if (streq(optarg, "uncompressed")) | |
268 | arg_compress = IMPORT_COMPRESS_UNCOMPRESSED; | |
269 | else if (streq(optarg, "xz")) | |
270 | arg_compress = IMPORT_COMPRESS_XZ; | |
271 | else if (streq(optarg, "gzip")) | |
272 | arg_compress = IMPORT_COMPRESS_GZIP; | |
273 | else if (streq(optarg, "bzip2")) | |
274 | arg_compress = IMPORT_COMPRESS_BZIP2; | |
275 | else { | |
276 | log_error("Unknown format: %s", optarg); | |
277 | return -EINVAL; | |
278 | } | |
279 | break; | |
280 | ||
281 | case '?': | |
282 | return -EINVAL; | |
283 | ||
284 | default: | |
285 | assert_not_reached("Unhandled option"); | |
286 | } | |
287 | ||
288 | return 1; | |
289 | } | |
290 | ||
291 | static int export_main(int argc, char *argv[]) { | |
292 | ||
293 | static const Verb verbs[] = { | |
294 | { "help", VERB_ANY, VERB_ANY, 0, help }, | |
295 | { "tar", 2, 3, 0, export_tar }, | |
296 | { "raw", 2, 3, 0, export_raw }, | |
297 | {} | |
298 | }; | |
299 | ||
300 | return dispatch_verb(argc, argv, verbs, NULL); | |
301 | } | |
302 | ||
303 | int main(int argc, char *argv[]) { | |
304 | int r; | |
305 | ||
306 | setlocale(LC_ALL, ""); | |
307 | log_parse_environment(); | |
308 | log_open(); | |
309 | ||
310 | r = parse_argv(argc, argv); | |
311 | if (r <= 0) | |
312 | goto finish; | |
313 | ||
314 | ignore_signals(SIGPIPE, -1); | |
315 | ||
316 | r = export_main(argc, argv); | |
317 | ||
318 | finish: | |
319 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; | |
320 | } |