b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering This file is part of systemd.
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering Copyright 2015 Lennart Poettering
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering systemd is free software; you can redistribute it and/or modify it
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering under the terms of the GNU Lesser General Public License as published by
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering (at your option) any later version.
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering systemd is distributed in the hope that it will be useful, but
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering Lesser General Public License for more details.
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering You should have received a copy of the GNU Lesser General Public License
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poetteringstatic const char *arg_image_root = "/var/lib/machines";
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poetteringstatic int interrupt_signal_handler(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering sd_event_exit(sd_event_source_get_event(s), EINTR);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poetteringstatic void on_tar_finished(TarImport *import, int error, void *userdata) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering log_info("Operation completed successfully.");
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poetteringstatic int import_tar(int argc, char *argv[], void *userdata) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering _cleanup_(tar_import_unrefp) TarImport *import = NULL;
4afd3348c7506dd1d36305b7bcb9feb8952b9d6bLennart Poettering _cleanup_(sd_event_unrefp) sd_event *event = NULL;
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering log_error("Local image name '%s' is not valid.", local);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering return log_error_errno(r, "Failed to check whether image '%s' exists: %m", local);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering else if (r > 0) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering log_error_errno(EEXIST, "Image '%s' already exists.", local);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering open_fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering return log_error_errno(errno, "Failed to open tar image to import: %m");
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering log_info("Importing '%s', saving as '%s'.", path, local);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering (void) readlink_malloc("/proc/self/fd/0", &pretty);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering log_info("Importing '%s', saving as '%s'.", strna(pretty), local);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart 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);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering r = tar_import_new(&import, event, arg_image_root, on_tar_finished, event);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering return log_error_errno(r, "Failed to allocate importer: %m");
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering r = tar_import_start(import, fd, local, arg_force, arg_read_only);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering return log_error_errno(r, "Failed to import image: %m");
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering return log_error_errno(r, "Failed to run event loop: %m");
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poetteringstatic void on_raw_finished(RawImport *import, int error, void *userdata) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering log_info("Operation completed successfully.");
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poetteringstatic int import_raw(int argc, char *argv[], void *userdata) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering _cleanup_(raw_import_unrefp) RawImport *import = NULL;
4afd3348c7506dd1d36305b7bcb9feb8952b9d6bLennart Poettering _cleanup_(sd_event_unrefp) sd_event *event = NULL;
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering log_error("Local image name '%s' is not valid.", local);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering return log_error_errno(r, "Failed to check whether image '%s' exists: %m", local);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering else if (r > 0) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering log_error_errno(EEXIST, "Image '%s' already exists.", local);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering open_fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering return log_error_errno(errno, "Failed to open raw image to import: %m");
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering log_info("Importing '%s', saving as '%s'.", path, local);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering (void) readlink_malloc("/proc/self/fd/0", &pretty);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering log_info("Importing '%s', saving as '%s'.", pretty, local);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart 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);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering r = raw_import_new(&import, event, arg_image_root, on_raw_finished, event);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering return log_error_errno(r, "Failed to allocate importer: %m");
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering r = raw_import_start(import, fd, local, arg_force, arg_read_only);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering return log_error_errno(r, "Failed to import image: %m");
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering return log_error_errno(r, "Failed to run event loop: %m");
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poetteringstatic int help(int argc, char *argv[], void *userdata) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering printf("%s [OPTIONS...] {COMMAND} ...\n\n"
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering "Import container or virtual machine images.\n\n"
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering " -h --help Show this help\n"
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering " --version Show package version\n"
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering " --force Force creation of image\n"
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering " --image-root=PATH Image root directory\n"
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering " --read-only Create a read-only image\n\n"
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering " tar FILE [NAME] Import a TAR image\n"
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering " raw FILE [NAME] Import a RAW image\n",
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poetteringstatic int parse_argv(int argc, char *argv[]) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering { "version", no_argument, NULL, ARG_VERSION },
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering { "force", no_argument, NULL, ARG_FORCE },
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering { "image-root", required_argument, NULL, ARG_IMAGE_ROOT },
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering { "read-only", no_argument, NULL, ARG_READ_ONLY },
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poetteringstatic int import_main(int argc, char *argv[]) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering return dispatch_verb(argc, argv, verbs, NULL);