export.c revision 25300b5a1fcf54674a69d0f4ab08925be00b0227
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering This file is part of systemd.
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering Copyright 2015 Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering systemd is free software; you can redistribute it and/or modify it
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering under the terms of the GNU Lesser General Public License as published by
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering (at your option) any later version.
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering systemd is distributed in the hope that it will be useful, but
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering Lesser General Public License for more details.
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering You should have received a copy of the GNU Lesser General Public License
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poetteringstatic ImportCompressType arg_compress = IMPORT_COMPRESS_UNKNOWN;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poetteringstatic void determine_compression_from_filename(const char *p) {
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering if (arg_compress != IMPORT_COMPRESS_UNKNOWN)
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering arg_compress = IMPORT_COMPRESS_UNCOMPRESSED;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering arg_compress = IMPORT_COMPRESS_UNCOMPRESSED;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poetteringstatic int interrupt_signal_handler(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering sd_event_exit(sd_event_source_get_event(s), EINTR);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poetteringstatic void on_tar_finished(TarExport *export, int error, void *userdata) {
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering log_info("Operation completed successfully.");
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poetteringstatic int export_tar(int argc, char *argv[], void *userdata) {
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering _cleanup_(tar_export_unrefp) TarExport *export = NULL;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering _cleanup_event_unref_ sd_event *event = NULL;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering _cleanup_(image_unrefp) Image *image = NULL;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering return log_error_errno(r, "Failed to look for machine %s: %m", argv[1]);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering log_error("Machine image %s not found.", argv[1]);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering determine_compression_from_filename(path);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering open_fd = open(path, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC|O_NOCTTY, 0666);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering return log_error_errno(errno, "Failed to open tar image for export: %m");
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering log_info("Exporting '%s', saving to '%s' with compression '%s'.", local, path, import_compress_type_to_string(arg_compress));
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering (void) readlink_malloc("/proc/self/fd/1", &pretty);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering log_info("Exporting '%s', saving to '%s' with compression '%s'.", local, strna(pretty), import_compress_type_to_string(arg_compress));
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering return log_error_errno(r, "Failed to allocate event loop: %m");
72c0a2c255b172ebbb2a2b7dab7c9aec4c9582d9Lennart Poettering assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, -1) >= 0);
72c0a2c255b172ebbb2a2b7dab7c9aec4c9582d9Lennart Poettering (void) sd_event_add_signal(event, NULL, SIGTERM, interrupt_signal_handler, NULL);
72c0a2c255b172ebbb2a2b7dab7c9aec4c9582d9Lennart Poettering (void) sd_event_add_signal(event, NULL, SIGINT, interrupt_signal_handler, NULL);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering r = tar_export_new(&export, event, on_tar_finished, event);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering return log_error_errno(r, "Failed to allocate exporter: %m");
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering r = tar_export_start(export, local, fd, arg_compress);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering return log_error_errno(r, "Failed to export image: %m");
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering return log_error_errno(r, "Failed to run event loop: %m");
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poetteringstatic void on_raw_finished(RawExport *export, int error, void *userdata) {
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering log_info("Operation completed successfully.");
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poetteringstatic int export_raw(int argc, char *argv[], void *userdata) {
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering _cleanup_(raw_export_unrefp) RawExport *export = NULL;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering _cleanup_event_unref_ sd_event *event = NULL;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering _cleanup_(image_unrefp) Image *image = NULL;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering return log_error_errno(r, "Failed to look for machine %s: %m", argv[1]);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering log_error("Machine image %s not found.", argv[1]);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering determine_compression_from_filename(path);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering open_fd = open(path, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC|O_NOCTTY, 0666);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering return log_error_errno(errno, "Failed to open raw image for export: %m");
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering log_info("Exporting '%s', saving to '%s' with compression '%s'.", local, path, import_compress_type_to_string(arg_compress));
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering (void) readlink_malloc("/proc/self/fd/1", &pretty);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering log_info("Exporting '%s', saving to '%s' with compression '%s'.", local, strna(pretty), import_compress_type_to_string(arg_compress));
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering return log_error_errno(r, "Failed to allocate event loop: %m");
72c0a2c255b172ebbb2a2b7dab7c9aec4c9582d9Lennart Poettering assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, -1) >= 0);
72c0a2c255b172ebbb2a2b7dab7c9aec4c9582d9Lennart Poettering (void) sd_event_add_signal(event, NULL, SIGTERM, interrupt_signal_handler, NULL);
72c0a2c255b172ebbb2a2b7dab7c9aec4c9582d9Lennart Poettering (void) sd_event_add_signal(event, NULL, SIGINT, interrupt_signal_handler, NULL);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering r = raw_export_new(&export, event, on_raw_finished, event);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering return log_error_errno(r, "Failed to allocate exporter: %m");
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering r = raw_export_start(export, local, fd, arg_compress);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering return log_error_errno(r, "Failed to export image: %m");
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering return log_error_errno(r, "Failed to run event loop: %m");
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poetteringstatic int help(int argc, char *argv[], void *userdata) {
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering printf("%s [OPTIONS...] {COMMAND} ...\n\n"
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering "Export container or virtual machine images.\n\n"
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering " -h --help Show this help\n"
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering " --version Show package version\n"
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering " --format=FORMAT Select format\n\n"
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering " tar NAME [FILE] Export a TAR image\n"
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering " raw NAME [FILE] Export a RAW image\n",
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poetteringstatic int parse_argv(int argc, char *argv[]) {
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering { "version", no_argument, NULL, ARG_VERSION },
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering { "format", required_argument, NULL, ARG_FORMAT },
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering arg_compress = IMPORT_COMPRESS_UNCOMPRESSED;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poetteringstatic int export_main(int argc, char *argv[]) {
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering return dispatch_verb(argc, argv, verbs, NULL);