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-tar.h"
43 TarImportFinished 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;
73 unsigned last_percent;
74 RateLimit progress_rate_limit;
77 TarImport* tar_import_unref(TarImport *i) {
81 sd_event_unref(i->event);
84 (void) kill_and_sigcont(i->tar_pid, SIGKILL);
85 (void) wait_for_terminate(i->tar_pid, NULL);
89 (void) btrfs_subvol_remove(i->temp_path);
90 (void) rm_rf_dangerous(i->temp_path, false, true, false);
94 import_compress_free(&i->compress);
96 sd_event_source_unref(i->input_event_source);
98 safe_close(i->tar_fd);
111 const char *image_root,
112 TarImportFinished on_finished,
115 _cleanup_(tar_import_unrefp) TarImport *i = NULL;
120 i = new0(TarImport, 1);
124 i->input_fd = i->tar_fd = -1;
125 i->on_finished = on_finished;
126 i->userdata = userdata;
128 RATELIMIT_INIT(i->progress_rate_limit, 500 * USEC_PER_MSEC, 1);
129 i->last_percent = (unsigned) -1;
131 i->image_root = strdup(image_root ?: "/var/lib/machines");
135 i->grow_machine_directory = path_startswith(i->image_root, "/var/lib/machines");
138 i->event = sd_event_ref(event);
140 r = sd_event_default(&i->event);
151 static void tar_import_report_progress(TarImport *i) {
155 /* We have no size information, unless the source is a regular file */
156 if (!S_ISREG(i->st.st_mode))
159 if (i->written_compressed >= (uint64_t) i->st.st_size)
162 percent = (unsigned) ((i->written_compressed * UINT64_C(100)) / (uint64_t) i->st.st_size);
164 if (percent == i->last_percent)
167 if (!ratelimit_test(&i->progress_rate_limit))
170 sd_notifyf(false, "X_IMPORT_PROGRESS=%u", percent);
171 log_info("Imported %u%%.", percent);
173 i->last_percent = percent;
176 static int tar_import_finish(TarImport *i) {
180 assert(i->tar_fd >= 0);
181 assert(i->temp_path);
182 assert(i->final_path);
184 i->tar_fd = safe_close(i->tar_fd);
186 if (i->tar_pid > 0) {
187 r = wait_for_terminate_and_warn("tar", i->tar_pid, true);
194 r = import_make_read_only(i->temp_path);
199 if (i->force_local) {
200 (void) btrfs_subvol_remove(i->final_path);
201 (void) rm_rf_dangerous(i->final_path, false, true, false);
204 if (renameat2(AT_FDCWD, i->temp_path, AT_FDCWD, i->final_path, RENAME_NOREPLACE) < 0)
205 return log_error_errno(errno, "Failed to move image into place: %m");
213 static int tar_import_fork_tar(TarImport *i) {
218 assert(!i->final_path);
219 assert(!i->temp_path);
220 assert(i->tar_fd < 0);
222 i->final_path = strjoin(i->image_root, "/", i->local, NULL);
226 r = tempfn_random(i->final_path, &i->temp_path);
230 (void) mkdir_parents_label(i->temp_path, 0700);
232 r = btrfs_subvol_make(i->temp_path);
234 if (mkdir(i->temp_path, 0755) < 0)
235 return log_error_errno(errno, "Failed to create directory %s: %m", i->temp_path);
237 return log_error_errno(errno, "Failed to create subvolume %s: %m", i->temp_path);
239 i->tar_fd = import_fork_tar(i->temp_path, &i->tar_pid);
246 static int tar_import_write(const void *p, size_t sz, void *userdata) {
247 TarImport *i = userdata;
250 if (i->grow_machine_directory && i->written_since_last_grow >= GROW_INTERVAL_BYTES) {
251 i->written_since_last_grow = 0;
252 grow_machine_directory();
255 r = loop_write(i->tar_fd, p, sz, false);
259 i->written_uncompressed += sz;
260 i->written_since_last_grow += sz;
265 static int tar_import_process(TarImport *i) {
270 assert(i->buffer_size < sizeof(i->buffer));
272 l = read(i->input_fd, i->buffer + i->buffer_size, sizeof(i->buffer) - i->buffer_size);
274 r = log_error_errno(errno, "Failed to read input file: %m");
278 if (i->compress.type == IMPORT_COMPRESS_UNKNOWN) {
279 log_error("Premature end of file: %m");
284 r = tar_import_finish(i);
290 if (i->compress.type == IMPORT_COMPRESS_UNKNOWN) {
291 r = import_uncompress_detect(&i->compress, i->buffer, i->buffer_size);
293 log_error("Failed to detect file compression: %m");
296 if (r == 0) /* Need more data */
299 r = tar_import_fork_tar(i);
304 r = import_uncompress(&i->compress, i->buffer, i->buffer_size, tar_import_write, i);
306 log_error_errno(r, "Failed to decode and write: %m");
310 i->written_compressed += i->buffer_size;
313 tar_import_report_progress(i);
319 i->on_finished(i, r, i->userdata);
321 sd_event_exit(i->event, r);
326 static int tar_import_on_input(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
327 TarImport *i = userdata;
329 return tar_import_process(i);
332 static int tar_import_on_defer(sd_event_source *s, void *userdata) {
333 TarImport *i = userdata;
335 return tar_import_process(i);
338 int tar_import_start(TarImport *i, int fd, const char *local, bool force_local, bool read_only) {
345 if (!machine_name_is_valid(local))
348 if (i->input_fd >= 0)
351 r = free_and_strdup(&i->local, local);
354 i->force_local = force_local;
355 i->read_only = read_only;
357 if (fstat(fd, &i->st) < 0)
360 r = sd_event_add_io(i->event, &i->input_event_source, fd, EPOLLIN, tar_import_on_input, i);
362 /* This fd does not support epoll, for example because it is a regular file. Busy read in that case */
363 r = sd_event_add_defer(i->event, &i->input_event_source, tar_import_on_defer, i);
367 r = sd_event_source_set_enabled(i->input_event_source, SD_EVENT_ON);