]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
72648326 LP |
2 | |
3 | #include <getopt.h> | |
4 | ||
5 | #include "sd-event.h" | |
dccca82b | 6 | #include "sd-id128.h" |
3f6fd1ba | 7 | |
b5efdb8a | 8 | #include "alloc-util.h" |
25300b5a | 9 | #include "hostname-util.h" |
85dbc41d | 10 | #include "import-util.h" |
3f6fd1ba | 11 | #include "machine-image.h" |
6bedfcbb | 12 | #include "parse-util.h" |
3f6fd1ba LP |
13 | #include "pull-raw.h" |
14 | #include "pull-tar.h" | |
15 | #include "signal-util.h" | |
07630cea | 16 | #include "string-util.h" |
3f6fd1ba | 17 | #include "verbs.h" |
49cf4170 | 18 | #include "web-util.h" |
72648326 LP |
19 | |
20 | static bool arg_force = false; | |
5f129649 | 21 | static const char *arg_image_root = "/var/lib/machines"; |
8f695058 | 22 | static ImportVerify arg_verify = IMPORT_VERIFY_SIGNATURE; |
9854730b | 23 | static bool arg_settings = true; |
91359193 | 24 | static bool arg_roothash = true; |
91f4347e | 25 | |
3d7415f4 LP |
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 | ||
dc2c282b | 32 | static void on_tar_finished(TarPull *pull, int error, void *userdata) { |
56ebfaf1 | 33 | sd_event *event = userdata; |
dc2c282b | 34 | assert(pull); |
56ebfaf1 LP |
35 | |
36 | if (error == 0) | |
37 | log_info("Operation completed successfully."); | |
56ebfaf1 | 38 | |
3d7415f4 | 39 | sd_event_exit(event, abs(error)); |
56ebfaf1 LP |
40 | } |
41 | ||
42 | static int pull_tar(int argc, char *argv[], void *userdata) { | |
dc2c282b | 43 | _cleanup_(tar_pull_unrefp) TarPull *pull = NULL; |
4afd3348 | 44 | _cleanup_(sd_event_unrefp) sd_event *event = NULL; |
56ebfaf1 LP |
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 { | |
85dbc41d | 58 | r = import_url_last_component(url, &l); |
56ebfaf1 LP |
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) { | |
3d7415f4 | 69 | r = tar_strip_suffixes(local, &ll); |
56ebfaf1 LP |
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) { | |
5ef46e5f | 81 | r = image_find(IMAGE_MACHINE, local, NULL); |
3a6ce860 LP |
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 { | |
92dfd88b | 86 | log_error("Image '%s' already exists.", local); |
56ebfaf1 LP |
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 | ||
72c0a2c2 LP |
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); | |
56ebfaf1 | 102 | |
dc2c282b | 103 | r = tar_pull_new(&pull, event, arg_image_root, on_tar_finished, event); |
56ebfaf1 | 104 | if (r < 0) |
dc2c282b | 105 | return log_error_errno(r, "Failed to allocate puller: %m"); |
56ebfaf1 | 106 | |
9854730b | 107 | r = tar_pull_start(pull, url, local, arg_force, arg_verify, arg_settings); |
56ebfaf1 LP |
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."); | |
3d7415f4 | 116 | return -r; |
56ebfaf1 LP |
117 | } |
118 | ||
dc2c282b | 119 | static void on_raw_finished(RawPull *pull, int error, void *userdata) { |
90199220 | 120 | sd_event *event = userdata; |
dc2c282b | 121 | assert(pull); |
90199220 LP |
122 | |
123 | if (error == 0) | |
124 | log_info("Operation completed successfully."); | |
90199220 | 125 | |
3d7415f4 | 126 | sd_event_exit(event, abs(error)); |
edce2aed LP |
127 | } |
128 | ||
aceac2f0 | 129 | static int pull_raw(int argc, char *argv[], void *userdata) { |
dc2c282b | 130 | _cleanup_(raw_pull_unrefp) RawPull *pull = NULL; |
4afd3348 | 131 | _cleanup_(sd_event_unrefp) sd_event *event = NULL; |
edce2aed | 132 | const char *url, *local; |
0d6e763b | 133 | _cleanup_free_ char *l = NULL, *ll = NULL; |
90199220 LP |
134 | int r; |
135 | ||
136 | url = argv[1]; | |
a2e03378 | 137 | if (!http_url_is_valid(url)) { |
90199220 LP |
138 | log_error("URL '%s' is not valid.", url); |
139 | return -EINVAL; | |
140 | } | |
141 | ||
8620a9a3 LP |
142 | if (argc >= 3) |
143 | local = argv[2]; | |
144 | else { | |
85dbc41d | 145 | r = import_url_last_component(url, &l); |
0d6e763b LP |
146 | if (r < 0) |
147 | return log_error_errno(r, "Failed get final component of URL: %m"); | |
90199220 | 148 | |
0d6e763b | 149 | local = l; |
90199220 LP |
150 | } |
151 | ||
8620a9a3 LP |
152 | if (isempty(local) || streq(local, "-")) |
153 | local = NULL; | |
90199220 | 154 | |
8620a9a3 | 155 | if (local) { |
3d7415f4 | 156 | r = raw_strip_suffixes(local, &ll); |
edce2aed LP |
157 | if (r < 0) |
158 | return log_oom(); | |
159 | ||
0d6e763b | 160 | local = ll; |
8620a9a3 LP |
161 | |
162 | if (!machine_name_is_valid(local)) { | |
163 | log_error("Local image name '%s' is not valid.", local); | |
164 | return -EINVAL; | |
165 | } | |
166 | ||
0d6e763b | 167 | if (!arg_force) { |
5ef46e5f | 168 | r = image_find(IMAGE_MACHINE, local, NULL); |
3a6ce860 LP |
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 { | |
92dfd88b | 173 | log_error("Image '%s' already exists.", local); |
0d6e763b | 174 | return -EEXIST; |
8620a9a3 | 175 | } |
0d6e763b | 176 | } |
90199220 | 177 | |
8620a9a3 LP |
178 | log_info("Pulling '%s', saving as '%s'.", url, local); |
179 | } else | |
180 | log_info("Pulling '%s'.", url); | |
90199220 LP |
181 | |
182 | r = sd_event_default(&event); | |
183 | if (r < 0) | |
184 | return log_error_errno(r, "Failed to allocate event loop: %m"); | |
185 | ||
72c0a2c2 LP |
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); | |
90199220 | 189 | |
dc2c282b | 190 | r = raw_pull_new(&pull, event, arg_image_root, on_raw_finished, event); |
90199220 | 191 | if (r < 0) |
dc2c282b | 192 | return log_error_errno(r, "Failed to allocate puller: %m"); |
90199220 | 193 | |
91359193 | 194 | r = raw_pull_start(pull, url, local, arg_force, arg_verify, arg_settings, arg_roothash); |
90199220 LP |
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."); | |
3d7415f4 | 203 | return -r; |
90199220 LP |
204 | } |
205 | ||
72648326 LP |
206 | static int help(int argc, char *argv[], void *userdata) { |
207 | ||
208 | printf("%s [OPTIONS...] {COMMAND} ...\n\n" | |
587fec42 | 209 | "Download container or virtual machine images.\n\n" |
72648326 LP |
210 | " -h --help Show this help\n" |
211 | " --version Show package version\n" | |
91f4347e | 212 | " --force Force creation of image\n" |
9854730b LP |
213 | " --verify=MODE Verify downloaded image, one of: 'no',\n" |
214 | " 'checksum', 'signature'\n" | |
215 | " --settings=BOOL Download settings file with image\n" | |
91359193 | 216 | " --roothash=BOOL Download root hash file with image\n" |
b43d75c3 | 217 | " --image-root=PATH Image root directory\n\n" |
72648326 | 218 | "Commands:\n" |
aa9bd499 | 219 | " tar URL [NAME] Download a TAR image\n" |
b43d75c3 | 220 | " raw URL [NAME] Download a RAW image\n", |
72648326 LP |
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, | |
087682d1 | 231 | ARG_IMAGE_ROOT, |
8f695058 | 232 | ARG_VERIFY, |
9854730b | 233 | ARG_SETTINGS, |
91359193 | 234 | ARG_ROOTHASH, |
72648326 LP |
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 }, | |
087682d1 | 241 | { "image-root", required_argument, NULL, ARG_IMAGE_ROOT }, |
8f695058 | 242 | { "verify", required_argument, NULL, ARG_VERIFY }, |
9854730b | 243 | { "settings", required_argument, NULL, ARG_SETTINGS }, |
91359193 | 244 | { "roothash", required_argument, NULL, ARG_ROOTHASH }, |
72648326 LP |
245 | {} |
246 | }; | |
247 | ||
9854730b | 248 | int c, r; |
72648326 LP |
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': | |
7eeeb28e | 258 | return help(0, NULL, NULL); |
72648326 LP |
259 | |
260 | case ARG_VERSION: | |
3f6fd1ba | 261 | return version(); |
72648326 LP |
262 | |
263 | case ARG_FORCE: | |
264 | arg_force = true; | |
265 | break; | |
266 | ||
087682d1 LP |
267 | case ARG_IMAGE_ROOT: |
268 | arg_image_root = optarg; | |
269 | break; | |
270 | ||
8f695058 LP |
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 | ||
9854730b LP |
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'", optarg); | |
284 | ||
285 | arg_settings = r; | |
286 | break; | |
287 | ||
91359193 LP |
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'", optarg); | |
292 | ||
293 | arg_roothash = r; | |
294 | break; | |
295 | ||
72648326 LP |
296 | case '?': |
297 | return -EINVAL; | |
298 | ||
299 | default: | |
300 | assert_not_reached("Unhandled option"); | |
301 | } | |
302 | ||
303 | return 1; | |
304 | } | |
305 | ||
dc2c282b | 306 | static int pull_main(int argc, char *argv[]) { |
72648326 | 307 | |
7eeeb28e | 308 | static const Verb verbs[] = { |
aa9bd499 LP |
309 | { "help", VERB_ANY, VERB_ANY, 0, help }, |
310 | { "tar", 2, 3, 0, pull_tar }, | |
311 | { "raw", 2, 3, 0, pull_raw }, | |
72648326 LP |
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 | ||
ce30c8dc | 329 | (void) ignore_signals(SIGPIPE, -1); |
b6e676ce | 330 | |
dc2c282b | 331 | r = pull_main(argc, argv); |
72648326 LP |
332 | |
333 | finish: | |
334 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; | |
335 | } |