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 PoetteringRawImport* raw_import_unref(RawImport *i) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering sd_event_source_unref(i->input_event_source);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering _cleanup_(raw_import_unrefp) RawImport *i = NULL;
587fec427c80b6c34dcf1d7570f891fcb652a7c5Lennart Poettering RATELIMIT_INIT(i->progress_rate_limit, 100 * USEC_PER_MSEC, 1);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering i->image_root = strdup(image_root ?: "/var/lib/machines");
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering i->grow_machine_directory = path_startswith(i->image_root, "/var/lib/machines");
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poetteringstatic void raw_import_report_progress(RawImport *i) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering /* We have no size information, unless the source is a regular file */
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering if (i->written_compressed >= (uint64_t) i->st.st_size)
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering percent = (unsigned) ((i->written_compressed * UINT64_C(100)) / (uint64_t) i->st.st_size);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering if (!ratelimit_test(&i->progress_rate_limit))
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering sd_notifyf(false, "X_IMPORT_PROGRESS=%u", percent);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poetteringstatic int raw_import_maybe_convert_qcow2(RawImport *i) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering return log_error_errno(r, "Failed to detect whether this is a QCOW2 image: %m");
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering /* This is a QCOW2 image, let's convert it */
14bcf25c8b94b5c3556ba3983028a2b35ed0572fLennart Poettering r = tempfn_random(i->final_path, NULL, &t);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering converted_fd = open(t, O_RDWR|O_CREAT|O_EXCL|O_NOCTTY|O_CLOEXEC, 0664);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering return log_error_errno(errno, "Failed to create %s: %m", t);
1ed8f8c16da99551c6731764759878a0bc243fdeLennart Poettering r = chattr_fd(converted_fd, FS_NOCOW_FL, FS_NOCOW_FL);
709f6e46a35ec492b70eb92943d82a8d838ce918Michal Schmidt log_warning_errno(r, "Failed to set file attributes on %s: %m", t);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering r = qcow2_convert(i->output_fd, converted_fd);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering return log_error_errno(r, "Failed to convert qcow2 image: %m");
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poetteringstatic int raw_import_finish(RawImport *i) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering /* In case this was a sparse file, make sure the file system is right */
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering if (ftruncate(i->output_fd, i->written_uncompressed) < 0)
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering return log_error_errno(errno, "Failed to truncate file: %m");
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering (void) copy_times(i->input_fd, i->output_fd);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering (void) copy_xattr(i->input_fd, i->output_fd);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering r = import_make_read_only_fd(i->output_fd);
d9e2daaf3d8649650cf9784b4fe9d9de4507da0cLennart Poettering (void) rm_rf(i->final_path, REMOVE_ROOT|REMOVE_PHYSICAL|REMOVE_SUBVOLUME);
f85ef957e647c5182acf5e64298f68e4b7fbfe8fAlban Crequy r = rename_noreplace(AT_FDCWD, i->temp_path, AT_FDCWD, i->final_path);
f85ef957e647c5182acf5e64298f68e4b7fbfe8fAlban Crequy return log_error_errno(r, "Failed to move image into place: %m");
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poetteringstatic int raw_import_open_disk(RawImport *i) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering i->final_path = strjoin(i->image_root, "/", i->local, ".raw", NULL);
14bcf25c8b94b5c3556ba3983028a2b35ed0572fLennart Poettering r = tempfn_random(i->final_path, NULL, &i->temp_path);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering (void) mkdir_parents_label(i->temp_path, 0700);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering i->output_fd = open(i->temp_path, O_RDWR|O_CREAT|O_EXCL|O_NOCTTY|O_CLOEXEC, 0664);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering return log_error_errno(errno, "Failed to open destination %s: %m", i->temp_path);
1ed8f8c16da99551c6731764759878a0bc243fdeLennart Poettering r = chattr_fd(i->output_fd, FS_NOCOW_FL, FS_NOCOW_FL);
709f6e46a35ec492b70eb92943d82a8d838ce918Michal Schmidt log_warning_errno(r, "Failed to set file attributes on %s: %m", i->temp_path);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poetteringstatic int raw_import_try_reflink(RawImport *i) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering if (i->compress.type != IMPORT_COMPRESS_UNCOMPRESSED)
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering return log_error_errno(errno, "Failed to read file offset of input file: %m");
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering /* Let's only try a btrfs reflink, if we are reading from the beginning of the file */
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering if ((uint64_t) p != (uint64_t) i->buffer_size)
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering r = btrfs_reflink(i->input_fd, i->output_fd);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poetteringstatic int raw_import_write(const void *p, size_t sz, void *userdata) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering if (i->grow_machine_directory && i->written_since_last_grow >= GROW_INTERVAL_BYTES) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering n = sparse_write(i->output_fd, p, sz, 64);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poetteringstatic int raw_import_process(RawImport *i) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering assert(i->buffer_size < sizeof(i->buffer));
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering l = read(i->input_fd, i->buffer + i->buffer_size, sizeof(i->buffer) - i->buffer_size);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering r = log_error_errno(errno, "Failed to read input file: %m");
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering if (i->compress.type == IMPORT_COMPRESS_UNKNOWN) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering if (i->compress.type == IMPORT_COMPRESS_UNKNOWN) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering r = import_uncompress_detect(&i->compress, i->buffer, i->buffer_size);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering log_error("Failed to detect file compression: %m");
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering if (r == 0) /* Need more data */
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering r = import_uncompress(&i->compress, i->buffer, i->buffer_size, raw_import_write, i);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering log_error_errno(r, "Failed to decode and write: %m");
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poetteringstatic int raw_import_on_input(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poetteringstatic int raw_import_on_defer(sd_event_source *s, void *userdata) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poetteringint raw_import_start(RawImport *i, int fd, const char *local, bool force_local, bool read_only) {
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering r = sd_event_add_io(i->event, &i->input_event_source, fd, EPOLLIN, raw_import_on_input, i);
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering /* This fd does not support epoll, for example because it is a regular file. Busy read in that case */
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering r = sd_event_add_defer(i->event, &i->input_event_source, raw_import_on_defer, i);