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