1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
9 #include "alloc-util.h"
11 #include "discover-image.h"
12 #include "export-raw.h"
13 #include "export-tar.h"
16 #include "hostname-util.h"
17 #include "import-common.h"
18 #include "import-util.h"
19 #include "main-func.h"
20 #include "signal-util.h"
21 #include "string-util.h"
22 #include "terminal-util.h"
25 static ImportCompressType arg_compress
= IMPORT_COMPRESS_UNKNOWN
;
26 static ImageClass arg_class
= IMAGE_MACHINE
;
28 static void determine_compression_from_filename(const char *p
) {
30 if (arg_compress
!= IMPORT_COMPRESS_UNKNOWN
)
34 arg_compress
= IMPORT_COMPRESS_UNCOMPRESSED
;
38 if (endswith(p
, ".xz"))
39 arg_compress
= IMPORT_COMPRESS_XZ
;
40 else if (endswith(p
, ".gz"))
41 arg_compress
= IMPORT_COMPRESS_GZIP
;
42 else if (endswith(p
, ".bz2"))
43 arg_compress
= IMPORT_COMPRESS_BZIP2
;
45 arg_compress
= IMPORT_COMPRESS_UNCOMPRESSED
;
48 static void on_tar_finished(TarExport
*export
, int error
, void *userdata
) {
49 sd_event
*event
= userdata
;
53 log_info("Operation completed successfully.");
55 sd_event_exit(event
, abs(error
));
58 static int export_tar(int argc
, char *argv
[], void *userdata
) {
59 _cleanup_(tar_export_unrefp
) TarExport
*export
= NULL
;
60 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
61 _cleanup_(image_unrefp
) Image
*image
= NULL
;
62 const char *path
= NULL
, *local
= NULL
;
63 _cleanup_close_
int open_fd
= -EBADF
;
67 if (image_name_is_valid(local
)) {
68 r
= image_find(arg_class
, local
, NULL
, &image
);
70 return log_error_errno(r
, "Image %s not found.", local
);
72 return log_error_errno(r
, "Failed to look for image %s: %m", local
);
80 path
= empty_or_dash_to_null(path
);
82 determine_compression_from_filename(path
);
85 open_fd
= open(path
, O_WRONLY
|O_CREAT
|O_TRUNC
|O_CLOEXEC
|O_NOCTTY
, 0666);
87 return log_error_errno(errno
, "Failed to open tar image for export: %m");
91 log_info("Exporting '%s', saving to '%s' with compression '%s'.", local
, path
, import_compress_type_to_string(arg_compress
));
93 _cleanup_free_
char *pretty
= NULL
;
97 (void) fd_get_path(fd
, &pretty
);
98 log_info("Exporting '%s', saving to '%s' with compression '%s'.", local
, strna(pretty
), import_compress_type_to_string(arg_compress
));
101 r
= import_allocate_event_with_signals(&event
);
105 r
= tar_export_new(&export
, event
, on_tar_finished
, event
);
107 return log_error_errno(r
, "Failed to allocate exporter: %m");
109 r
= tar_export_start(export
, local
, fd
, arg_compress
);
111 return log_error_errno(r
, "Failed to export 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(RawExport
*export
, 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 export_raw(int argc
, char *argv
[], void *userdata
) {
132 _cleanup_(raw_export_unrefp
) RawExport
*export
= NULL
;
133 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
134 _cleanup_(image_unrefp
) Image
*image
= NULL
;
135 const char *path
= NULL
, *local
= NULL
;
136 _cleanup_close_
int open_fd
= -EBADF
;
140 if (image_name_is_valid(local
)) {
141 r
= image_find(arg_class
, local
, NULL
, &image
);
143 return log_error_errno(r
, "Image %s not found.", local
);
145 return log_error_errno(r
, "Failed to look for image %s: %m", local
);
153 path
= empty_or_dash_to_null(path
);
155 determine_compression_from_filename(path
);
158 open_fd
= open(path
, O_WRONLY
|O_CREAT
|O_TRUNC
|O_CLOEXEC
|O_NOCTTY
, 0666);
160 return log_error_errno(errno
, "Failed to open raw image for export: %m");
164 log_info("Exporting '%s', saving to '%s' with compression '%s'.", local
, path
, import_compress_type_to_string(arg_compress
));
166 _cleanup_free_
char *pretty
= NULL
;
170 (void) fd_get_path(fd
, &pretty
);
171 log_info("Exporting '%s', saving to '%s' with compression '%s'.", local
, strna(pretty
), import_compress_type_to_string(arg_compress
));
174 r
= import_allocate_event_with_signals(&event
);
178 r
= raw_export_new(&export
, event
, on_raw_finished
, event
);
180 return log_error_errno(r
, "Failed to allocate exporter: %m");
182 r
= raw_export_start(export
, local
, fd
, arg_compress
);
184 return log_error_errno(r
, "Failed to export image: %m");
186 r
= sd_event_loop(event
);
188 return log_error_errno(r
, "Failed to run event loop: %m");
190 log_info("Exiting.");
194 static int help(int argc
, char *argv
[], void *userdata
) {
195 printf("%1$s [OPTIONS...] {COMMAND} ...\n"
196 "\n%4$sExport disk images.%5$s\n"
197 "\n%2$sCommands:%3$s\n"
198 " tar NAME [FILE] Export a TAR image\n"
199 " raw NAME [FILE] Export a RAW image\n"
200 "\n%2$sOptions:%3$s\n"
201 " -h --help Show this help\n"
202 " --version Show package version\n"
203 " --format=FORMAT Select format\n"
204 " --class=CLASS Select image class (machine, sysext, confext,\n"
206 program_invocation_short_name
,
215 static int parse_argv(int argc
, char *argv
[]) {
223 static const struct option options
[] = {
224 { "help", no_argument
, NULL
, 'h' },
225 { "version", no_argument
, NULL
, ARG_VERSION
},
226 { "format", required_argument
, NULL
, ARG_FORMAT
},
227 { "class", required_argument
, NULL
, ARG_CLASS
},
236 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
241 return help(0, NULL
, NULL
);
247 if (streq(optarg
, "uncompressed"))
248 arg_compress
= IMPORT_COMPRESS_UNCOMPRESSED
;
249 else if (streq(optarg
, "xz"))
250 arg_compress
= IMPORT_COMPRESS_XZ
;
251 else if (streq(optarg
, "gzip"))
252 arg_compress
= IMPORT_COMPRESS_GZIP
;
253 else if (streq(optarg
, "bzip2"))
254 arg_compress
= IMPORT_COMPRESS_BZIP2
;
256 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
257 "Unknown format: %s", optarg
);
261 arg_class
= image_class_from_string(optarg
);
263 return log_error_errno(arg_class
, "Failed to parse --class= argument: %s", optarg
);
271 assert_not_reached();
277 static int export_main(int argc
, char *argv
[]) {
278 static const Verb verbs
[] = {
279 { "help", VERB_ANY
, VERB_ANY
, 0, help
},
280 { "tar", 2, 3, 0, export_tar
},
281 { "raw", 2, 3, 0, export_raw
},
285 return dispatch_verb(argc
, argv
, verbs
, NULL
);
288 static int run(int argc
, char *argv
[]) {
291 setlocale(LC_ALL
, "");
294 r
= parse_argv(argc
, argv
);
298 (void) ignore_signals(SIGPIPE
);
300 return export_main(argc
, argv
);
303 DEFINE_MAIN_FUNCTION(run
);