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