]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/import/import.c
grypt-util: drop two emacs modelines
[thirdparty/systemd.git] / src / import / import.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright 2015 Lennart Poettering
4 ***/
5
6 #include <getopt.h>
7
8 #include "sd-event.h"
9 #include "sd-id128.h"
10
11 #include "alloc-util.h"
12 #include "fd-util.h"
13 #include "fs-util.h"
14 #include "hostname-util.h"
15 #include "import-raw.h"
16 #include "import-tar.h"
17 #include "import-util.h"
18 #include "machine-image.h"
19 #include "signal-util.h"
20 #include "string-util.h"
21 #include "verbs.h"
22
23 static bool arg_force = false;
24 static bool arg_read_only = false;
25 static const char *arg_image_root = "/var/lib/machines";
26
27 static int interrupt_signal_handler(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
28 log_notice("Transfer aborted.");
29 sd_event_exit(sd_event_source_get_event(s), EINTR);
30 return 0;
31 }
32
33 static void on_tar_finished(TarImport *import, int error, void *userdata) {
34 sd_event *event = userdata;
35 assert(import);
36
37 if (error == 0)
38 log_info("Operation completed successfully.");
39
40 sd_event_exit(event, abs(error));
41 }
42
43 static int import_tar(int argc, char *argv[], void *userdata) {
44 _cleanup_(tar_import_unrefp) TarImport *import = NULL;
45 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
46 const char *path = NULL, *local = NULL;
47 _cleanup_free_ char *ll = NULL;
48 _cleanup_close_ int open_fd = -1;
49 int r, fd;
50
51 if (argc >= 2)
52 path = argv[1];
53 if (isempty(path) || streq(path, "-"))
54 path = NULL;
55
56 if (argc >= 3)
57 local = argv[2];
58 else if (path)
59 local = basename(path);
60 if (isempty(local) || streq(local, "-"))
61 local = NULL;
62
63 if (local) {
64 r = tar_strip_suffixes(local, &ll);
65 if (r < 0)
66 return log_oom();
67
68 local = ll;
69
70 if (!machine_name_is_valid(local)) {
71 log_error("Local image name '%s' is not valid.", local);
72 return -EINVAL;
73 }
74
75 if (!arg_force) {
76 r = image_find(IMAGE_MACHINE, local, NULL);
77 if (r < 0) {
78 if (r != -ENOENT)
79 return log_error_errno(r, "Failed to check whether image '%s' exists: %m", local);
80 } else {
81 log_error("Image '%s' already exists.", local);
82 return -EEXIST;
83 }
84 }
85 } else
86 local = "imported";
87
88 if (path) {
89 open_fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY);
90 if (open_fd < 0)
91 return log_error_errno(errno, "Failed to open tar image to import: %m");
92
93 fd = open_fd;
94
95 log_info("Importing '%s', saving as '%s'.", path, local);
96 } else {
97 _cleanup_free_ char *pretty = NULL;
98
99 fd = STDIN_FILENO;
100
101 (void) readlink_malloc("/proc/self/fd/0", &pretty);
102 log_info("Importing '%s', saving as '%s'.", strna(pretty), local);
103 }
104
105 r = sd_event_default(&event);
106 if (r < 0)
107 return log_error_errno(r, "Failed to allocate event loop: %m");
108
109 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, -1) >= 0);
110 (void) sd_event_add_signal(event, NULL, SIGTERM, interrupt_signal_handler, NULL);
111 (void) sd_event_add_signal(event, NULL, SIGINT, interrupt_signal_handler, NULL);
112
113 r = tar_import_new(&import, event, arg_image_root, on_tar_finished, event);
114 if (r < 0)
115 return log_error_errno(r, "Failed to allocate importer: %m");
116
117 r = tar_import_start(import, fd, local, arg_force, arg_read_only);
118 if (r < 0)
119 return log_error_errno(r, "Failed to import image: %m");
120
121 r = sd_event_loop(event);
122 if (r < 0)
123 return log_error_errno(r, "Failed to run event loop: %m");
124
125 log_info("Exiting.");
126 return -r;
127 }
128
129 static void on_raw_finished(RawImport *import, int error, void *userdata) {
130 sd_event *event = userdata;
131 assert(import);
132
133 if (error == 0)
134 log_info("Operation completed successfully.");
135
136 sd_event_exit(event, abs(error));
137 }
138
139 static int import_raw(int argc, char *argv[], void *userdata) {
140 _cleanup_(raw_import_unrefp) RawImport *import = NULL;
141 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
142 const char *path = NULL, *local = NULL;
143 _cleanup_free_ char *ll = NULL;
144 _cleanup_close_ int open_fd = -1;
145 int r, fd;
146
147 if (argc >= 2)
148 path = argv[1];
149 if (isempty(path) || streq(path, "-"))
150 path = NULL;
151
152 if (argc >= 3)
153 local = argv[2];
154 else if (path)
155 local = basename(path);
156 if (isempty(local) || streq(local, "-"))
157 local = NULL;
158
159 if (local) {
160 r = raw_strip_suffixes(local, &ll);
161 if (r < 0)
162 return log_oom();
163
164 local = ll;
165
166 if (!machine_name_is_valid(local)) {
167 log_error("Local image name '%s' is not valid.", local);
168 return -EINVAL;
169 }
170
171 if (!arg_force) {
172 r = image_find(IMAGE_MACHINE, local, NULL);
173 if (r < 0) {
174 if (r != -ENOENT)
175 return log_error_errno(r, "Failed to check whether image '%s' exists: %m", local);
176 } else {
177 log_error("Image '%s' already exists.", local);
178 return -EEXIST;
179 }
180 }
181 } else
182 local = "imported";
183
184 if (path) {
185 open_fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY);
186 if (open_fd < 0)
187 return log_error_errno(errno, "Failed to open raw image to import: %m");
188
189 fd = open_fd;
190
191 log_info("Importing '%s', saving as '%s'.", path, local);
192 } else {
193 _cleanup_free_ char *pretty = NULL;
194
195 fd = STDIN_FILENO;
196
197 (void) readlink_malloc("/proc/self/fd/0", &pretty);
198 log_info("Importing '%s', saving as '%s'.", strempty(pretty), local);
199 }
200
201 r = sd_event_default(&event);
202 if (r < 0)
203 return log_error_errno(r, "Failed to allocate event loop: %m");
204
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);
208
209 r = raw_import_new(&import, event, arg_image_root, on_raw_finished, event);
210 if (r < 0)
211 return log_error_errno(r, "Failed to allocate importer: %m");
212
213 r = raw_import_start(import, fd, local, arg_force, arg_read_only);
214 if (r < 0)
215 return log_error_errno(r, "Failed to import image: %m");
216
217 r = sd_event_loop(event);
218 if (r < 0)
219 return log_error_errno(r, "Failed to run event loop: %m");
220
221 log_info("Exiting.");
222 return -r;
223 }
224
225 static int help(int argc, char *argv[], void *userdata) {
226
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"
234 "Commands:\n"
235 " tar FILE [NAME] Import a TAR image\n"
236 " raw FILE [NAME] Import a RAW image\n",
237 program_invocation_short_name);
238
239 return 0;
240 }
241
242 static int parse_argv(int argc, char *argv[]) {
243
244 enum {
245 ARG_VERSION = 0x100,
246 ARG_FORCE,
247 ARG_IMAGE_ROOT,
248 ARG_READ_ONLY,
249 };
250
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 },
257 {}
258 };
259
260 int c;
261
262 assert(argc >= 0);
263 assert(argv);
264
265 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
266
267 switch (c) {
268
269 case 'h':
270 return help(0, NULL, NULL);
271
272 case ARG_VERSION:
273 return version();
274
275 case ARG_FORCE:
276 arg_force = true;
277 break;
278
279 case ARG_IMAGE_ROOT:
280 arg_image_root = optarg;
281 break;
282
283 case ARG_READ_ONLY:
284 arg_read_only = true;
285 break;
286
287 case '?':
288 return -EINVAL;
289
290 default:
291 assert_not_reached("Unhandled option");
292 }
293
294 return 1;
295 }
296
297 static int import_main(int argc, char *argv[]) {
298
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 },
303 {}
304 };
305
306 return dispatch_verb(argc, argv, verbs, NULL);
307 }
308
309 int main(int argc, char *argv[]) {
310 int r;
311
312 setlocale(LC_ALL, "");
313 log_parse_environment();
314 log_open();
315
316 r = parse_argv(argc, argv);
317 if (r <= 0)
318 goto finish;
319
320 (void) ignore_signals(SIGPIPE, -1);
321
322 r = import_main(argc, argv);
323
324 finish:
325 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
326 }