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