1 /* SPDX-License-Identifier: LGPL-2.1+ */
9 #include "alloc-util.h"
10 #include "hostname-util.h"
11 #include "import-util.h"
12 #include "machine-image.h"
13 #include "main-func.h"
14 #include "parse-util.h"
17 #include "signal-util.h"
18 #include "string-util.h"
22 static bool arg_force
= false;
23 static const char *arg_image_root
= "/var/lib/machines";
24 static ImportVerify arg_verify
= IMPORT_VERIFY_SIGNATURE
;
25 static bool arg_settings
= true;
26 static bool arg_roothash
= true;
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
);
34 static void on_tar_finished(TarPull
*pull
, int error
, void *userdata
) {
35 sd_event
*event
= userdata
;
39 log_info("Operation completed successfully.");
41 sd_event_exit(event
, abs(error
));
44 static int pull_tar(int argc
, char *argv
[], void *userdata
) {
45 _cleanup_(tar_pull_unrefp
) TarPull
*pull
= NULL
;
46 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
47 const char *url
, *local
;
48 _cleanup_free_
char *l
= NULL
, *ll
= NULL
;
52 if (!http_url_is_valid(url
)) {
53 log_error("URL '%s' is not valid.", url
);
60 r
= import_url_last_component(url
, &l
);
62 return log_error_errno(r
, "Failed get final component of URL: %m");
67 local
= empty_or_dash_to_null(local
);
70 r
= tar_strip_suffixes(local
, &ll
);
76 if (!machine_name_is_valid(local
)) {
77 log_error("Local image name '%s' is not valid.", local
);
82 r
= image_find(IMAGE_MACHINE
, local
, NULL
);
85 return log_error_errno(r
, "Failed to check whether image '%s' exists: %m", local
);
87 log_error("Image '%s' already exists.", local
);
92 log_info("Pulling '%s', saving as '%s'.", url
, local
);
94 log_info("Pulling '%s'.", url
);
96 r
= sd_event_default(&event
);
98 return log_error_errno(r
, "Failed to allocate event loop: %m");
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
);
104 r
= tar_pull_new(&pull
, event
, arg_image_root
, on_tar_finished
, event
);
106 return log_error_errno(r
, "Failed to allocate puller: %m");
108 r
= tar_pull_start(pull
, url
, local
, arg_force
, arg_verify
, arg_settings
);
110 return log_error_errno(r
, "Failed to pull image: %m");
112 r
= sd_event_loop(event
);
114 return log_error_errno(r
, "Failed to run event loop: %m");
116 log_info("Exiting.");
120 static void on_raw_finished(RawPull
*pull
, int error
, void *userdata
) {
121 sd_event
*event
= userdata
;
125 log_info("Operation completed successfully.");
127 sd_event_exit(event
, abs(error
));
130 static int pull_raw(int argc
, char *argv
[], void *userdata
) {
131 _cleanup_(raw_pull_unrefp
) RawPull
*pull
= NULL
;
132 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
133 const char *url
, *local
;
134 _cleanup_free_
char *l
= NULL
, *ll
= NULL
;
138 if (!http_url_is_valid(url
)) {
139 log_error("URL '%s' is not valid.", url
);
146 r
= import_url_last_component(url
, &l
);
148 return log_error_errno(r
, "Failed get final component of URL: %m");
153 local
= empty_or_dash_to_null(local
);
156 r
= raw_strip_suffixes(local
, &ll
);
162 if (!machine_name_is_valid(local
)) {
163 log_error("Local image name '%s' is not valid.", local
);
168 r
= image_find(IMAGE_MACHINE
, local
, NULL
);
171 return log_error_errno(r
, "Failed to check whether image '%s' exists: %m", local
);
173 log_error("Image '%s' already exists.", local
);
178 log_info("Pulling '%s', saving as '%s'.", url
, local
);
180 log_info("Pulling '%s'.", url
);
182 r
= sd_event_default(&event
);
184 return log_error_errno(r
, "Failed to allocate event loop: %m");
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
);
190 r
= raw_pull_new(&pull
, event
, arg_image_root
, on_raw_finished
, event
);
192 return log_error_errno(r
, "Failed to allocate puller: %m");
194 r
= raw_pull_start(pull
, url
, local
, arg_force
, arg_verify
, arg_settings
, arg_roothash
);
196 return log_error_errno(r
, "Failed to pull image: %m");
198 r
= sd_event_loop(event
);
200 return log_error_errno(r
, "Failed to run event loop: %m");
202 log_info("Exiting.");
206 static int help(int argc
, char *argv
[], void *userdata
) {
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"
219 " tar URL [NAME] Download a TAR image\n"
220 " raw URL [NAME] Download a RAW image\n",
221 program_invocation_short_name
);
226 static int parse_argv(int argc
, char *argv
[]) {
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
},
253 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
258 return help(0, NULL
, NULL
);
268 arg_image_root
= optarg
;
272 arg_verify
= import_verify_from_string(optarg
);
274 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
275 "Invalid verification setting '%s'", optarg
);
280 r
= parse_boolean(optarg
);
282 return log_error_errno(r
, "Failed to parse --settings= parameter '%s': %m", optarg
);
288 r
= parse_boolean(optarg
);
290 return log_error_errno(r
, "Failed to parse --roothash= parameter '%s': %m", optarg
);
299 assert_not_reached("Unhandled option");
305 static int pull_main(int argc
, char *argv
[]) {
306 static const Verb verbs
[] = {
307 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
308 { "tar", 2, 3, 0, pull_tar
},
309 { "raw", 2, 3, 0, pull_raw
},
313 return dispatch_verb(argc
, argv
, verbs
, NULL
);
316 static int run(int argc
, char *argv
[]) {
319 setlocale(LC_ALL
, "");
320 log_parse_environment();
323 r
= parse_argv(argc
, argv
);
327 (void) ignore_signals(SIGPIPE
, -1);
329 return pull_main(argc
, argv
);
332 DEFINE_MAIN_FUNCTION(run
);