1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2015 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
24 #include "sd-daemon.h"
27 #include "path-util.h"
28 #include "btrfs-util.h"
31 #include "ratelimit.h"
32 #include "machine-pool.h"
33 #include "qcow2-util.h"
34 #include "import-compress.h"
35 #include "import-common.h"
36 #include "import-raw.h"
43 RawImportFinished on_finished;
49 bool grow_machine_directory;
57 ImportCompress compress;
59 uint64_t written_since_last_grow;
61 sd_event_source *input_event_source;
63 uint8_t buffer[16*1024];
66 uint64_t written_compressed;
67 uint64_t written_uncompressed;
71 unsigned last_percent;
72 RateLimit progress_rate_limit;
75 RawImport* raw_import_unref(RawImport *i) {
79 sd_event_unref(i->event);
82 (void) unlink(i->temp_path);
86 import_compress_free(&i->compress);
88 sd_event_source_unref(i->input_event_source);
90 safe_close(i->output_fd);
103 const char *image_root,
104 RawImportFinished on_finished,
107 _cleanup_(raw_import_unrefp) RawImport *i = NULL;
112 i = new0(RawImport, 1);
116 i->input_fd = i->output_fd = -1;
117 i->on_finished = on_finished;
118 i->userdata = userdata;
120 RATELIMIT_INIT(i->progress_rate_limit, 500 * USEC_PER_MSEC, 1);
121 i->last_percent = (unsigned) -1;
123 i->image_root = strdup(image_root ?: "/var/lib/machines");
127 i->grow_machine_directory = path_startswith(i->image_root, "/var/lib/machines");
130 i->event = sd_event_ref(event);
132 r = sd_event_default(&i->event);
143 static void raw_import_report_progress(RawImport *i) {
147 /* We have no size information, unless the source is a regular file */
148 if (!S_ISREG(i->st.st_mode))
151 if (i->written_compressed >= (uint64_t) i->st.st_size)
154 percent = (unsigned) ((i->written_compressed * UINT64_C(100)) / (uint64_t) i->st.st_size);
156 if (percent == i->last_percent)
159 if (!ratelimit_test(&i->progress_rate_limit))
162 sd_notifyf(false, "X_IMPORT_PROGRESS=%u", percent);
163 log_info("Imported %u%%.", percent);
165 i->last_percent = percent;
168 static int raw_import_maybe_convert_qcow2(RawImport *i) {
169 _cleanup_close_ int converted_fd = -1;
170 _cleanup_free_ char *t = NULL;
175 r = qcow2_detect(i->output_fd);
177 return log_error_errno(r, "Failed to detect whether this is a QCOW2 image: %m");
181 /* This is a QCOW2 image, let's convert it */
182 r = tempfn_random(i->final_path, &t);
186 converted_fd = open(t, O_RDWR|O_CREAT|O_EXCL|O_NOCTTY|O_CLOEXEC, 0664);
187 if (converted_fd < 0)
188 return log_error_errno(errno, "Failed to create %s: %m", t);
190 r = chattr_fd(converted_fd, true, FS_NOCOW_FL);
192 log_warning_errno(errno, "Failed to set file attributes on %s: %m", t);
194 log_info("Unpacking QCOW2 file.");
196 r = qcow2_convert(i->output_fd, converted_fd);
199 return log_error_errno(r, "Failed to convert qcow2 image: %m");
202 (void) unlink(i->temp_path);
207 safe_close(i->output_fd);
208 i->output_fd = converted_fd;
214 static int raw_import_finish(RawImport *i) {
218 assert(i->output_fd >= 0);
219 assert(i->temp_path);
220 assert(i->final_path);
222 /* In case this was a sparse file, make sure the file system is right */
223 if (i->written_uncompressed > 0) {
224 if (ftruncate(i->output_fd, i->written_uncompressed) < 0)
225 return log_error_errno(errno, "Failed to truncate file: %m");
228 r = raw_import_maybe_convert_qcow2(i);
232 if (S_ISREG(i->st.st_mode)) {
233 (void) copy_times(i->input_fd, i->output_fd);
234 (void) copy_xattr(i->input_fd, i->output_fd);
238 r = import_make_read_only_fd(i->output_fd);
243 if (i->force_local) {
244 (void) btrfs_subvol_remove(i->final_path);
245 (void) rm_rf_dangerous(i->final_path, false, true, false);
248 if (renameat2(AT_FDCWD, i->temp_path, AT_FDCWD, i->final_path, RENAME_NOREPLACE) < 0)
249 return log_error_errno(errno, "Failed to move image into place: %m");
257 static int raw_import_open_disk(RawImport *i) {
262 assert(!i->final_path);
263 assert(!i->temp_path);
264 assert(i->output_fd < 0);
266 i->final_path = strjoin(i->image_root, "/", i->local, ".raw", NULL);
270 r = tempfn_random(i->final_path, &i->temp_path);
274 (void) mkdir_parents_label(i->temp_path, 0700);
276 i->output_fd = open(i->temp_path, O_RDWR|O_CREAT|O_EXCL|O_NOCTTY|O_CLOEXEC, 0664);
277 if (i->output_fd < 0)
278 return log_error_errno(errno, "Failed to open destination %s: %m", i->temp_path);
280 r = chattr_fd(i->output_fd, true, FS_NOCOW_FL);
282 log_warning_errno(errno, "Failed to set file attributes on %s: %m", i->temp_path);
287 static int raw_import_try_reflink(RawImport *i) {
292 assert(i->input_fd >= 0);
293 assert(i->output_fd >= 0);
295 if (i->compress.type != IMPORT_COMPRESS_UNCOMPRESSED)
298 if (!S_ISREG(i->st.st_mode))
301 p = lseek(i->input_fd, 0, SEEK_CUR);
303 return log_error_errno(errno, "Failed to read file offset of input file: %m");
305 /* Let's only try a btrfs reflink, if we are reading from the beginning of the file */
306 if ((uint64_t) p != (uint64_t) i->buffer_size)
309 r = btrfs_reflink(i->input_fd, i->output_fd);
316 static int raw_import_write(const void *p, size_t sz, void *userdata) {
317 RawImport *i = userdata;
320 if (i->grow_machine_directory && i->written_since_last_grow >= GROW_INTERVAL_BYTES) {
321 i->written_since_last_grow = 0;
322 grow_machine_directory();
325 n = sparse_write(i->output_fd, p, sz, 64);
331 i->written_uncompressed += sz;
332 i->written_since_last_grow += sz;
337 static int raw_import_process(RawImport *i) {
342 assert(i->buffer_size < sizeof(i->buffer));
344 l = read(i->input_fd, i->buffer + i->buffer_size, sizeof(i->buffer) - i->buffer_size);
346 r = log_error_errno(errno, "Failed to read input file: %m");
350 if (i->compress.type == IMPORT_COMPRESS_UNKNOWN) {
351 log_error("Premature end of file: %m");
356 r = raw_import_finish(i);
362 if (i->compress.type == IMPORT_COMPRESS_UNKNOWN) {
363 r = import_uncompress_detect(&i->compress, i->buffer, i->buffer_size);
365 log_error("Failed to detect file compression: %m");
368 if (r == 0) /* Need more data */
371 r = raw_import_open_disk(i);
375 r = raw_import_try_reflink(i);
379 r = raw_import_finish(i);
384 r = import_uncompress(&i->compress, i->buffer, i->buffer_size, raw_import_write, i);
386 log_error_errno(r, "Failed to decode and write: %m");
390 i->written_compressed += i->buffer_size;
393 raw_import_report_progress(i);
399 i->on_finished(i, r, i->userdata);
401 sd_event_exit(i->event, r);
406 static int raw_import_on_input(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
407 RawImport *i = userdata;
409 return raw_import_process(i);
412 static int raw_import_on_defer(sd_event_source *s, void *userdata) {
413 RawImport *i = userdata;
415 return raw_import_process(i);
418 int raw_import_start(RawImport *i, int fd, const char *local, bool force_local, bool read_only) {
425 if (!machine_name_is_valid(local))
428 if (i->input_fd >= 0)
431 r = free_and_strdup(&i->local, local);
434 i->force_local = force_local;
435 i->read_only = read_only;
437 if (fstat(fd, &i->st) < 0)
440 r = sd_event_add_io(i->event, &i->input_event_source, fd, EPOLLIN, raw_import_on_input, i);
442 /* This fd does not support epoll, for example because it is a regular file. Busy read in that case */
443 r = sd_event_add_defer(i->event, &i->input_event_source, raw_import_on_defer, i);
447 r = sd_event_source_set_enabled(i->input_event_source, SD_EVENT_ON);