]>
Commit | Line | Data |
---|---|---|
b6e676ce 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" | |
3f6fd1ba | 25 | |
b5efdb8a | 26 | #include "alloc-util.h" |
b6e676ce | 27 | #include "event-util.h" |
3ffd4af2 | 28 | #include "fd-util.h" |
f4f15635 | 29 | #include "fs-util.h" |
25300b5a | 30 | #include "hostname-util.h" |
b6e676ce | 31 | #include "import-raw.h" |
3f6fd1ba LP |
32 | #include "import-tar.h" |
33 | #include "import-util.h" | |
34 | #include "machine-image.h" | |
35 | #include "signal-util.h" | |
07630cea | 36 | #include "string-util.h" |
3f6fd1ba | 37 | #include "verbs.h" |
b6e676ce LP |
38 | |
39 | static bool arg_force = false; | |
40 | static bool arg_read_only = false; | |
41 | static const char *arg_image_root = "/var/lib/machines"; | |
42 | ||
43 | static int interrupt_signal_handler(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) { | |
44 | log_notice("Transfer aborted."); | |
45 | sd_event_exit(sd_event_source_get_event(s), EINTR); | |
46 | return 0; | |
47 | } | |
48 | ||
49 | static void on_tar_finished(TarImport *import, int error, void *userdata) { | |
50 | sd_event *event = userdata; | |
51 | assert(import); | |
52 | ||
53 | if (error == 0) | |
54 | log_info("Operation completed successfully."); | |
55 | ||
56 | sd_event_exit(event, abs(error)); | |
57 | } | |
58 | ||
59 | static int import_tar(int argc, char *argv[], void *userdata) { | |
60 | _cleanup_(tar_import_unrefp) TarImport *import = NULL; | |
61 | _cleanup_event_unref_ sd_event *event = NULL; | |
62 | const char *path = NULL, *local = NULL; | |
63 | _cleanup_free_ char *ll = NULL; | |
64 | _cleanup_close_ int open_fd = -1; | |
65 | int r, fd; | |
66 | ||
67 | if (argc >= 2) | |
68 | path = argv[1]; | |
69 | if (isempty(path) || streq(path, "-")) | |
70 | path = NULL; | |
71 | ||
72 | if (argc >= 3) | |
73 | local = argv[2]; | |
74 | else if (path) | |
75 | local = basename(path); | |
76 | if (isempty(local) || streq(local, "-")) | |
77 | local = NULL; | |
78 | ||
79 | if (local) { | |
80 | r = tar_strip_suffixes(local, &ll); | |
81 | if (r < 0) | |
82 | return log_oom(); | |
83 | ||
84 | local = ll; | |
85 | ||
86 | if (!machine_name_is_valid(local)) { | |
87 | log_error("Local image name '%s' is not valid.", local); | |
88 | return -EINVAL; | |
89 | } | |
90 | ||
91 | if (!arg_force) { | |
92 | r = image_find(local, NULL); | |
93 | if (r < 0) | |
94 | return log_error_errno(r, "Failed to check whether image '%s' exists: %m", local); | |
95 | else if (r > 0) { | |
96 | log_error_errno(EEXIST, "Image '%s' already exists.", local); | |
97 | return -EEXIST; | |
98 | } | |
99 | } | |
100 | } else | |
101 | local = "imported"; | |
102 | ||
103 | if (path) { | |
104 | open_fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY); | |
105 | if (open_fd < 0) | |
106 | return log_error_errno(errno, "Failed to open tar image to import: %m"); | |
107 | ||
108 | fd = open_fd; | |
109 | ||
110 | log_info("Importing '%s', saving as '%s'.", path, local); | |
111 | } else { | |
112 | _cleanup_free_ char *pretty = NULL; | |
113 | ||
114 | fd = STDIN_FILENO; | |
115 | ||
116 | (void) readlink_malloc("/proc/self/fd/0", &pretty); | |
117 | log_info("Importing '%s', saving as '%s'.", strna(pretty), local); | |
118 | } | |
119 | ||
120 | r = sd_event_default(&event); | |
121 | if (r < 0) | |
122 | return log_error_errno(r, "Failed to allocate event loop: %m"); | |
123 | ||
72c0a2c2 LP |
124 | assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, -1) >= 0); |
125 | (void) sd_event_add_signal(event, NULL, SIGTERM, interrupt_signal_handler, NULL); | |
126 | (void) sd_event_add_signal(event, NULL, SIGINT, interrupt_signal_handler, NULL); | |
b6e676ce LP |
127 | |
128 | r = tar_import_new(&import, event, arg_image_root, on_tar_finished, event); | |
129 | if (r < 0) | |
130 | return log_error_errno(r, "Failed to allocate importer: %m"); | |
131 | ||
132 | r = tar_import_start(import, fd, local, arg_force, arg_read_only); | |
133 | if (r < 0) | |
134 | return log_error_errno(r, "Failed to import image: %m"); | |
135 | ||
136 | r = sd_event_loop(event); | |
137 | if (r < 0) | |
138 | return log_error_errno(r, "Failed to run event loop: %m"); | |
139 | ||
140 | log_info("Exiting."); | |
141 | return -r; | |
142 | } | |
143 | ||
144 | static void on_raw_finished(RawImport *import, int error, void *userdata) { | |
145 | sd_event *event = userdata; | |
146 | assert(import); | |
147 | ||
148 | if (error == 0) | |
149 | log_info("Operation completed successfully."); | |
150 | ||
151 | sd_event_exit(event, abs(error)); | |
152 | } | |
153 | ||
154 | static int import_raw(int argc, char *argv[], void *userdata) { | |
155 | _cleanup_(raw_import_unrefp) RawImport *import = NULL; | |
156 | _cleanup_event_unref_ sd_event *event = NULL; | |
157 | const char *path = NULL, *local = NULL; | |
158 | _cleanup_free_ char *ll = NULL; | |
159 | _cleanup_close_ int open_fd = -1; | |
160 | int r, fd; | |
161 | ||
162 | if (argc >= 2) | |
163 | path = argv[1]; | |
164 | if (isempty(path) || streq(path, "-")) | |
165 | path = NULL; | |
166 | ||
167 | if (argc >= 3) | |
168 | local = argv[2]; | |
169 | else if (path) | |
170 | local = basename(path); | |
171 | if (isempty(local) || streq(local, "-")) | |
172 | local = NULL; | |
173 | ||
174 | if (local) { | |
175 | r = raw_strip_suffixes(local, &ll); | |
176 | if (r < 0) | |
177 | return log_oom(); | |
178 | ||
179 | local = ll; | |
180 | ||
181 | if (!machine_name_is_valid(local)) { | |
182 | log_error("Local image name '%s' is not valid.", local); | |
183 | return -EINVAL; | |
184 | } | |
185 | ||
186 | if (!arg_force) { | |
187 | r = image_find(local, NULL); | |
188 | if (r < 0) | |
189 | return log_error_errno(r, "Failed to check whether image '%s' exists: %m", local); | |
190 | else if (r > 0) { | |
191 | log_error_errno(EEXIST, "Image '%s' already exists.", local); | |
192 | return -EEXIST; | |
193 | } | |
194 | } | |
195 | } else | |
196 | local = "imported"; | |
197 | ||
198 | if (path) { | |
199 | open_fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY); | |
200 | if (open_fd < 0) | |
201 | return log_error_errno(errno, "Failed to open raw image to import: %m"); | |
202 | ||
203 | fd = open_fd; | |
204 | ||
205 | log_info("Importing '%s', saving as '%s'.", path, local); | |
206 | } else { | |
207 | _cleanup_free_ char *pretty = NULL; | |
208 | ||
209 | fd = STDIN_FILENO; | |
210 | ||
211 | (void) readlink_malloc("/proc/self/fd/0", &pretty); | |
212 | log_info("Importing '%s', saving as '%s'.", pretty, local); | |
213 | } | |
214 | ||
215 | r = sd_event_default(&event); | |
216 | if (r < 0) | |
217 | return log_error_errno(r, "Failed to allocate event loop: %m"); | |
218 | ||
72c0a2c2 LP |
219 | assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, -1) >= 0); |
220 | (void) sd_event_add_signal(event, NULL, SIGTERM, interrupt_signal_handler, NULL); | |
221 | (void) sd_event_add_signal(event, NULL, SIGINT, interrupt_signal_handler, NULL); | |
b6e676ce LP |
222 | |
223 | r = raw_import_new(&import, event, arg_image_root, on_raw_finished, event); | |
224 | if (r < 0) | |
225 | return log_error_errno(r, "Failed to allocate importer: %m"); | |
226 | ||
227 | r = raw_import_start(import, fd, local, arg_force, arg_read_only); | |
228 | if (r < 0) | |
229 | return log_error_errno(r, "Failed to import image: %m"); | |
230 | ||
231 | r = sd_event_loop(event); | |
232 | if (r < 0) | |
233 | return log_error_errno(r, "Failed to run event loop: %m"); | |
234 | ||
235 | log_info("Exiting."); | |
236 | return -r; | |
237 | } | |
238 | ||
239 | static int help(int argc, char *argv[], void *userdata) { | |
240 | ||
241 | printf("%s [OPTIONS...] {COMMAND} ...\n\n" | |
587fec42 | 242 | "Import container or virtual machine images.\n\n" |
b6e676ce LP |
243 | " -h --help Show this help\n" |
244 | " --version Show package version\n" | |
245 | " --force Force creation of image\n" | |
246 | " --image-root=PATH Image root directory\n" | |
247 | " --read-only Create a read-only image\n\n" | |
248 | "Commands:\n" | |
587fec42 LP |
249 | " tar FILE [NAME] Import a TAR image\n" |
250 | " raw FILE [NAME] Import a RAW image\n", | |
b6e676ce LP |
251 | program_invocation_short_name); |
252 | ||
253 | return 0; | |
254 | } | |
255 | ||
256 | static int parse_argv(int argc, char *argv[]) { | |
257 | ||
258 | enum { | |
259 | ARG_VERSION = 0x100, | |
260 | ARG_FORCE, | |
261 | ARG_IMAGE_ROOT, | |
262 | ARG_READ_ONLY, | |
263 | }; | |
264 | ||
265 | static const struct option options[] = { | |
266 | { "help", no_argument, NULL, 'h' }, | |
267 | { "version", no_argument, NULL, ARG_VERSION }, | |
268 | { "force", no_argument, NULL, ARG_FORCE }, | |
269 | { "image-root", required_argument, NULL, ARG_IMAGE_ROOT }, | |
270 | { "read-only", no_argument, NULL, ARG_READ_ONLY }, | |
271 | {} | |
272 | }; | |
273 | ||
274 | int c; | |
275 | ||
276 | assert(argc >= 0); | |
277 | assert(argv); | |
278 | ||
279 | while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) | |
280 | ||
281 | switch (c) { | |
282 | ||
283 | case 'h': | |
284 | return help(0, NULL, NULL); | |
285 | ||
286 | case ARG_VERSION: | |
3f6fd1ba | 287 | return version(); |
b6e676ce LP |
288 | |
289 | case ARG_FORCE: | |
290 | arg_force = true; | |
291 | break; | |
292 | ||
293 | case ARG_IMAGE_ROOT: | |
294 | arg_image_root = optarg; | |
295 | break; | |
296 | ||
297 | case ARG_READ_ONLY: | |
298 | arg_read_only = true; | |
299 | break; | |
300 | ||
301 | case '?': | |
302 | return -EINVAL; | |
303 | ||
304 | default: | |
305 | assert_not_reached("Unhandled option"); | |
306 | } | |
307 | ||
308 | return 1; | |
309 | } | |
310 | ||
311 | static int import_main(int argc, char *argv[]) { | |
312 | ||
313 | static const Verb verbs[] = { | |
314 | { "help", VERB_ANY, VERB_ANY, 0, help }, | |
315 | { "tar", 2, 3, 0, import_tar }, | |
316 | { "raw", 2, 3, 0, import_raw }, | |
317 | {} | |
318 | }; | |
319 | ||
320 | return dispatch_verb(argc, argv, verbs, NULL); | |
321 | } | |
322 | ||
323 | int main(int argc, char *argv[]) { | |
324 | int r; | |
325 | ||
326 | setlocale(LC_ALL, ""); | |
327 | log_parse_environment(); | |
328 | log_open(); | |
329 | ||
330 | r = parse_argv(argc, argv); | |
331 | if (r <= 0) | |
332 | goto finish; | |
333 | ||
ce30c8dc | 334 | (void) ignore_signals(SIGPIPE, -1); |
b6e676ce LP |
335 | |
336 | r = import_main(argc, argv); | |
337 | ||
338 | finish: | |
339 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; | |
340 | } |