export.c revision 25300b5a1fcf54674a69d0f4ab08925be00b0227
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering/***
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering This file is part of systemd.
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering Copyright 2015 Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart 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
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
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 Poettering***/
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering#include <getopt.h>
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering#include "sd-event.h"
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering#include "event-util.h"
24882e06c135584f16f31ba8a00fecde8b7f6fadLennart Poettering#include "signal-util.h"
25300b5a1fcf54674a69d0f4ab08925be00b0227Lennart Poettering#include "hostname-util.h"
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering#include "verbs.h"
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering#include "build.h"
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering#include "machine-image.h"
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering#include "import-util.h"
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering#include "export-tar.h"
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering#include "export-raw.h"
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poetteringstatic ImportCompressType arg_compress = IMPORT_COMPRESS_UNKNOWN;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poetteringstatic void determine_compression_from_filename(const char *p) {
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering if (arg_compress != IMPORT_COMPRESS_UNKNOWN)
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering return;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering if (!p) {
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering arg_compress = IMPORT_COMPRESS_UNCOMPRESSED;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering return;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering }
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering if (endswith(p, ".xz"))
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering arg_compress = IMPORT_COMPRESS_XZ;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering else if (endswith(p, ".gz"))
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering arg_compress = IMPORT_COMPRESS_GZIP;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering else if (endswith(p, ".bz2"))
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering arg_compress = IMPORT_COMPRESS_BZIP2;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering else
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering arg_compress = IMPORT_COMPRESS_UNCOMPRESSED;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering}
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poetteringstatic int interrupt_signal_handler(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering log_notice("Transfer aborted.");
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering sd_event_exit(sd_event_source_get_event(s), EINTR);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering return 0;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering}
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poetteringstatic void on_tar_finished(TarExport *export, int error, void *userdata) {
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering sd_event *event = userdata;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering assert(export);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering if (error == 0)
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering log_info("Operation completed successfully.");
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering sd_event_exit(event, abs(error));
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering}
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
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 const char *path = NULL, *local = NULL;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering _cleanup_close_ int open_fd = -1;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering int r, fd;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering if (machine_name_is_valid(argv[1])) {
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering r = image_find(argv[1], &image);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering if (r < 0)
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering return log_error_errno(r, "Failed to look for machine %s: %m", argv[1]);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering if (r == 0) {
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering log_error("Machine image %s not found.", argv[1]);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering return -ENOENT;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering }
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering local = image->path;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering } else
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering local = argv[1];
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering if (argc >= 3)
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering path = argv[2];
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering if (isempty(path) || streq(path, "-"))
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering path = NULL;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering determine_compression_from_filename(path);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering if (path) {
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering open_fd = open(path, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC|O_NOCTTY, 0666);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering if (open_fd < 0)
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering return log_error_errno(errno, "Failed to open tar image for export: %m");
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering fd = open_fd;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering log_info("Exporting '%s', saving to '%s' with compression '%s'.", local, path, import_compress_type_to_string(arg_compress));
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering } else {
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering _cleanup_free_ char *pretty = NULL;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering fd = STDOUT_FILENO;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
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 }
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering r = sd_event_default(&event);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering if (r < 0)
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering return log_error_errno(r, "Failed to allocate event loop: %m");
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
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
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering r = tar_export_new(&export, event, on_tar_finished, event);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering if (r < 0)
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering return log_error_errno(r, "Failed to allocate exporter: %m");
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering r = tar_export_start(export, local, fd, arg_compress);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering if (r < 0)
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering return log_error_errno(r, "Failed to export image: %m");
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering r = sd_event_loop(event);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering if (r < 0)
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering return log_error_errno(r, "Failed to run event loop: %m");
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering log_info("Exiting.");
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering return -r;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering}
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poetteringstatic void on_raw_finished(RawExport *export, int error, void *userdata) {
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering sd_event *event = userdata;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering assert(export);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering if (error == 0)
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering log_info("Operation completed successfully.");
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering sd_event_exit(event, abs(error));
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering}
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
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 const char *path = NULL, *local = NULL;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering _cleanup_close_ int open_fd = -1;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering int r, fd;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering if (machine_name_is_valid(argv[1])) {
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering r = image_find(argv[1], &image);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering if (r < 0)
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering return log_error_errno(r, "Failed to look for machine %s: %m", argv[1]);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering if (r == 0) {
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering log_error("Machine image %s not found.", argv[1]);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering return -ENOENT;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering }
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering local = image->path;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering } else
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering local = argv[1];
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering if (argc >= 3)
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering path = argv[2];
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering if (isempty(path) || streq(path, "-"))
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering path = NULL;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering determine_compression_from_filename(path);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering if (path) {
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering open_fd = open(path, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC|O_NOCTTY, 0666);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering if (open_fd < 0)
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering return log_error_errno(errno, "Failed to open raw image for export: %m");
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering fd = open_fd;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering log_info("Exporting '%s', saving to '%s' with compression '%s'.", local, path, import_compress_type_to_string(arg_compress));
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering } else {
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering _cleanup_free_ char *pretty = NULL;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering fd = STDOUT_FILENO;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
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 }
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering r = sd_event_default(&event);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering if (r < 0)
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering return log_error_errno(r, "Failed to allocate event loop: %m");
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
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
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering r = raw_export_new(&export, event, on_raw_finished, event);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering if (r < 0)
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering return log_error_errno(r, "Failed to allocate exporter: %m");
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering r = raw_export_start(export, local, fd, arg_compress);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering if (r < 0)
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering return log_error_errno(r, "Failed to export image: %m");
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering r = sd_event_loop(event);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering if (r < 0)
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering return log_error_errno(r, "Failed to run event loop: %m");
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering log_info("Exiting.");
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering return -r;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering}
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poetteringstatic int help(int argc, char *argv[], void *userdata) {
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
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 "Commands:\n"
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering " tar NAME [FILE] Export a TAR image\n"
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering " raw NAME [FILE] Export a RAW image\n",
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering program_invocation_short_name);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering return 0;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering}
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poetteringstatic int parse_argv(int argc, char *argv[]) {
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering enum {
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering ARG_VERSION = 0x100,
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering ARG_FORMAT,
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering };
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering static const struct option options[] = {
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering { "help", no_argument, NULL, 'h' },
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering { "version", no_argument, NULL, ARG_VERSION },
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering { "format", required_argument, NULL, ARG_FORMAT },
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering {}
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering };
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering int c;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering assert(argc >= 0);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering assert(argv);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering switch (c) {
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering case 'h':
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering return help(0, NULL, NULL);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering case ARG_VERSION:
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering puts(PACKAGE_STRING);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering puts(SYSTEMD_FEATURES);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering return 0;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering case ARG_FORMAT:
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering if (streq(optarg, "uncompressed"))
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering arg_compress = IMPORT_COMPRESS_UNCOMPRESSED;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering else if (streq(optarg, "xz"))
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering arg_compress = IMPORT_COMPRESS_XZ;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering else if (streq(optarg, "gzip"))
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering arg_compress = IMPORT_COMPRESS_GZIP;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering else if (streq(optarg, "bzip2"))
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering arg_compress = IMPORT_COMPRESS_BZIP2;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering else {
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering log_error("Unknown format: %s", optarg);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering return -EINVAL;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering }
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering break;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering case '?':
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering return -EINVAL;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering default:
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering assert_not_reached("Unhandled option");
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering }
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering return 1;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering}
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poetteringstatic int export_main(int argc, char *argv[]) {
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering static const Verb verbs[] = {
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering { "help", VERB_ANY, VERB_ANY, 0, help },
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering { "tar", 2, 3, 0, export_tar },
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering { "raw", 2, 3, 0, export_raw },
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering {}
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering };
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering return dispatch_verb(argc, argv, verbs, NULL);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering}
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poetteringint main(int argc, char *argv[]) {
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering int r;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering setlocale(LC_ALL, "");
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering log_parse_environment();
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering log_open();
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering r = parse_argv(argc, argv);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering if (r <= 0)
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering goto finish;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
ce30c8dcb41dfe9264f79f30c7f51c0e74576638Lennart Poettering (void) ignore_signals(SIGPIPE, -1);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering r = export_main(argc, argv);
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poetteringfinish:
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering}