]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
72648326 LP |
2 | |
3 | #include <getopt.h> | |
ca78ad1d | 4 | #include <locale.h> |
72648326 LP |
5 | |
6 | #include "sd-event.h" | |
dccca82b | 7 | #include "sd-id128.h" |
3f6fd1ba | 8 | |
b5efdb8a | 9 | #include "alloc-util.h" |
25300b5a | 10 | #include "hostname-util.h" |
85dbc41d | 11 | #include "import-util.h" |
3f6fd1ba | 12 | #include "machine-image.h" |
5e332028 | 13 | #include "main-func.h" |
6bedfcbb | 14 | #include "parse-util.h" |
3f6fd1ba LP |
15 | #include "pull-raw.h" |
16 | #include "pull-tar.h" | |
17 | #include "signal-util.h" | |
07630cea | 18 | #include "string-util.h" |
3f6fd1ba | 19 | #include "verbs.h" |
49cf4170 | 20 | #include "web-util.h" |
72648326 LP |
21 | |
22 | static bool arg_force = false; | |
5f129649 | 23 | static const char *arg_image_root = "/var/lib/machines"; |
8f695058 | 24 | static ImportVerify arg_verify = IMPORT_VERIFY_SIGNATURE; |
9854730b | 25 | static bool arg_settings = true; |
91359193 | 26 | static bool arg_roothash = true; |
91f4347e | 27 | |
3d7415f4 LP |
28 | static int interrupt_signal_handler(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) { |
29 | log_notice("Transfer aborted."); | |
30 | sd_event_exit(sd_event_source_get_event(s), EINTR); | |
31 | return 0; | |
32 | } | |
33 | ||
dc2c282b | 34 | static void on_tar_finished(TarPull *pull, int error, void *userdata) { |
56ebfaf1 | 35 | sd_event *event = userdata; |
dc2c282b | 36 | assert(pull); |
56ebfaf1 LP |
37 | |
38 | if (error == 0) | |
39 | log_info("Operation completed successfully."); | |
56ebfaf1 | 40 | |
3d7415f4 | 41 | sd_event_exit(event, abs(error)); |
56ebfaf1 LP |
42 | } |
43 | ||
44 | static int pull_tar(int argc, char *argv[], void *userdata) { | |
dc2c282b | 45 | _cleanup_(tar_pull_unrefp) TarPull *pull = NULL; |
4afd3348 | 46 | _cleanup_(sd_event_unrefp) sd_event *event = NULL; |
56ebfaf1 LP |
47 | const char *url, *local; |
48 | _cleanup_free_ char *l = NULL, *ll = NULL; | |
49 | int r; | |
50 | ||
51 | url = argv[1]; | |
52 | if (!http_url_is_valid(url)) { | |
53 | log_error("URL '%s' is not valid.", url); | |
54 | return -EINVAL; | |
55 | } | |
56 | ||
57 | if (argc >= 3) | |
58 | local = argv[2]; | |
59 | else { | |
85dbc41d | 60 | r = import_url_last_component(url, &l); |
56ebfaf1 LP |
61 | if (r < 0) |
62 | return log_error_errno(r, "Failed get final component of URL: %m"); | |
63 | ||
64 | local = l; | |
65 | } | |
66 | ||
dc90e0fa | 67 | local = empty_or_dash_to_null(local); |
56ebfaf1 LP |
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 | ||
dc90e0fa | 153 | local = empty_or_dash_to_null(local); |
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); | |
baaa35ad ZJS |
273 | if (arg_verify < 0) |
274 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
275 | "Invalid verification setting '%s'", optarg); | |
8f695058 LP |
276 | |
277 | break; | |
278 | ||
9854730b LP |
279 | case ARG_SETTINGS: |
280 | r = parse_boolean(optarg); | |
281 | if (r < 0) | |
94c9ff16 | 282 | return log_error_errno(r, "Failed to parse --settings= parameter '%s': %m", optarg); |
9854730b LP |
283 | |
284 | arg_settings = r; | |
285 | break; | |
286 | ||
91359193 LP |
287 | case ARG_ROOTHASH: |
288 | r = parse_boolean(optarg); | |
289 | if (r < 0) | |
94c9ff16 | 290 | return log_error_errno(r, "Failed to parse --roothash= parameter '%s': %m", optarg); |
91359193 LP |
291 | |
292 | arg_roothash = r; | |
293 | break; | |
294 | ||
72648326 LP |
295 | case '?': |
296 | return -EINVAL; | |
297 | ||
298 | default: | |
299 | assert_not_reached("Unhandled option"); | |
300 | } | |
301 | ||
302 | return 1; | |
303 | } | |
304 | ||
dc2c282b | 305 | static int pull_main(int argc, char *argv[]) { |
7eeeb28e | 306 | static const Verb verbs[] = { |
aa9bd499 LP |
307 | { "help", VERB_ANY, VERB_ANY, 0, help }, |
308 | { "tar", 2, 3, 0, pull_tar }, | |
309 | { "raw", 2, 3, 0, pull_raw }, | |
72648326 LP |
310 | {} |
311 | }; | |
312 | ||
313 | return dispatch_verb(argc, argv, verbs, NULL); | |
314 | } | |
315 | ||
5272ae42 | 316 | static int run(int argc, char *argv[]) { |
72648326 LP |
317 | int r; |
318 | ||
319 | setlocale(LC_ALL, ""); | |
320 | log_parse_environment(); | |
321 | log_open(); | |
322 | ||
323 | r = parse_argv(argc, argv); | |
324 | if (r <= 0) | |
5272ae42 | 325 | return r; |
72648326 | 326 | |
ce30c8dc | 327 | (void) ignore_signals(SIGPIPE, -1); |
b6e676ce | 328 | |
5272ae42 | 329 | return pull_main(argc, argv); |
72648326 | 330 | } |
5272ae42 ZJS |
331 | |
332 | DEFINE_MAIN_FUNCTION(run); |