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 if (isempty(local
) || streq(local
, "-"))
71 r
= tar_strip_suffixes(local
, &ll
);
77 if (!machine_name_is_valid(local
)) {
78 log_error("Local image name '%s' is not valid.", local
);
83 r
= image_find(IMAGE_MACHINE
, local
, NULL
);
86 return log_error_errno(r
, "Failed to check whether image '%s' exists: %m", local
);
88 log_error("Image '%s' already exists.", local
);
93 log_info("Pulling '%s', saving as '%s'.", url
, local
);
95 log_info("Pulling '%s'.", url
);
97 r
= sd_event_default(&event
);
99 return log_error_errno(r
, "Failed to allocate event loop: %m");
101 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGTERM
, SIGINT
, -1) >= 0);
102 (void) sd_event_add_signal(event
, NULL
, SIGTERM
, interrupt_signal_handler
, NULL
);
103 (void) sd_event_add_signal(event
, NULL
, SIGINT
, interrupt_signal_handler
, NULL
);
105 r
= tar_pull_new(&pull
, event
, arg_image_root
, on_tar_finished
, event
);
107 return log_error_errno(r
, "Failed to allocate puller: %m");
109 r
= tar_pull_start(pull
, url
, local
, arg_force
, arg_verify
, arg_settings
);
111 return log_error_errno(r
, "Failed to pull image: %m");
113 r
= sd_event_loop(event
);
115 return log_error_errno(r
, "Failed to run event loop: %m");
117 log_info("Exiting.");
121 static void on_raw_finished(RawPull
*pull
, int error
, void *userdata
) {
122 sd_event
*event
= userdata
;
126 log_info("Operation completed successfully.");
128 sd_event_exit(event
, abs(error
));
131 static int pull_raw(int argc
, char *argv
[], void *userdata
) {
132 _cleanup_(raw_pull_unrefp
) RawPull
*pull
= NULL
;
133 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
134 const char *url
, *local
;
135 _cleanup_free_
char *l
= NULL
, *ll
= NULL
;
139 if (!http_url_is_valid(url
)) {
140 log_error("URL '%s' is not valid.", url
);
147 r
= import_url_last_component(url
, &l
);
149 return log_error_errno(r
, "Failed get final component of URL: %m");
154 if (isempty(local
) || streq(local
, "-"))
158 r
= raw_strip_suffixes(local
, &ll
);
164 if (!machine_name_is_valid(local
)) {
165 log_error("Local image name '%s' is not valid.", local
);
170 r
= image_find(IMAGE_MACHINE
, local
, NULL
);
173 return log_error_errno(r
, "Failed to check whether image '%s' exists: %m", local
);
175 log_error("Image '%s' already exists.", local
);
180 log_info("Pulling '%s', saving as '%s'.", url
, local
);
182 log_info("Pulling '%s'.", url
);
184 r
= sd_event_default(&event
);
186 return log_error_errno(r
, "Failed to allocate event loop: %m");
188 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGTERM
, SIGINT
, -1) >= 0);
189 (void) sd_event_add_signal(event
, NULL
, SIGTERM
, interrupt_signal_handler
, NULL
);
190 (void) sd_event_add_signal(event
, NULL
, SIGINT
, interrupt_signal_handler
, NULL
);
192 r
= raw_pull_new(&pull
, event
, arg_image_root
, on_raw_finished
, event
);
194 return log_error_errno(r
, "Failed to allocate puller: %m");
196 r
= raw_pull_start(pull
, url
, local
, arg_force
, arg_verify
, arg_settings
, arg_roothash
);
198 return log_error_errno(r
, "Failed to pull image: %m");
200 r
= sd_event_loop(event
);
202 return log_error_errno(r
, "Failed to run event loop: %m");
204 log_info("Exiting.");
208 static int help(int argc
, char *argv
[], void *userdata
) {
210 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
211 "Download container or virtual machine images.\n\n"
212 " -h --help Show this help\n"
213 " --version Show package version\n"
214 " --force Force creation of image\n"
215 " --verify=MODE Verify downloaded image, one of: 'no',\n"
216 " 'checksum', 'signature'\n"
217 " --settings=BOOL Download settings file with image\n"
218 " --roothash=BOOL Download root hash file with image\n"
219 " --image-root=PATH Image root directory\n\n"
221 " tar URL [NAME] Download a TAR image\n"
222 " raw URL [NAME] Download a RAW image\n",
223 program_invocation_short_name
);
228 static int parse_argv(int argc
, char *argv
[]) {
239 static const struct option options
[] = {
240 { "help", no_argument
, NULL
, 'h' },
241 { "version", no_argument
, NULL
, ARG_VERSION
},
242 { "force", no_argument
, NULL
, ARG_FORCE
},
243 { "image-root", required_argument
, NULL
, ARG_IMAGE_ROOT
},
244 { "verify", required_argument
, NULL
, ARG_VERIFY
},
245 { "settings", required_argument
, NULL
, ARG_SETTINGS
},
246 { "roothash", required_argument
, NULL
, ARG_ROOTHASH
},
255 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
260 return help(0, NULL
, NULL
);
270 arg_image_root
= optarg
;
274 arg_verify
= import_verify_from_string(optarg
);
276 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
277 "Invalid verification setting '%s'", optarg
);
282 r
= parse_boolean(optarg
);
284 return log_error_errno(r
, "Failed to parse --settings= parameter '%s': %m", optarg
);
290 r
= parse_boolean(optarg
);
292 return log_error_errno(r
, "Failed to parse --roothash= parameter '%s': %m", optarg
);
301 assert_not_reached("Unhandled option");
307 static int pull_main(int argc
, char *argv
[]) {
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
},
315 return dispatch_verb(argc
, argv
, verbs
, NULL
);
318 static int run(int argc
, char *argv
[]) {
321 setlocale(LC_ALL
, "");
322 log_parse_environment();
325 r
= parse_argv(argc
, argv
);
329 (void) ignore_signals(SIGPIPE
, -1);
331 return pull_main(argc
, argv
);
334 DEFINE_MAIN_FUNCTION(run
);