2 This file is part of systemd.
4 Copyright 2015 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include "sd-daemon.h"
25 #include "alloc-util.h"
26 #include "btrfs-util.h"
31 #include "hostname-util.h"
32 #include "import-common.h"
33 #include "import-compress.h"
34 #include "import-tar.h"
36 #include "machine-pool.h"
38 #include "path-util.h"
39 #include "process-util.h"
40 #include "qcow2-util.h"
41 #include "ratelimit.h"
43 #include "string-util.h"
51 TarImportFinished on_finished
;
57 bool grow_machine_directory
;
65 ImportCompress compress
;
67 uint64_t written_since_last_grow
;
69 sd_event_source
*input_event_source
;
71 uint8_t buffer
[16*1024];
74 uint64_t written_compressed
;
75 uint64_t written_uncompressed
;
81 unsigned last_percent
;
82 RateLimit progress_rate_limit
;
85 TarImport
* tar_import_unref(TarImport
*i
) {
89 sd_event_source_unref(i
->input_event_source
);
92 (void) kill_and_sigcont(i
->tar_pid
, SIGKILL
);
93 (void) wait_for_terminate(i
->tar_pid
, NULL
);
97 (void) rm_rf(i
->temp_path
, REMOVE_ROOT
|REMOVE_PHYSICAL
|REMOVE_SUBVOLUME
);
101 import_compress_free(&i
->compress
);
103 sd_event_unref(i
->event
);
105 safe_close(i
->tar_fd
);
116 const char *image_root
,
117 TarImportFinished on_finished
,
120 _cleanup_(tar_import_unrefp
) TarImport
*i
= NULL
;
125 i
= new0(TarImport
, 1);
129 i
->input_fd
= i
->tar_fd
= -1;
130 i
->on_finished
= on_finished
;
131 i
->userdata
= userdata
;
133 RATELIMIT_INIT(i
->progress_rate_limit
, 100 * USEC_PER_MSEC
, 1);
134 i
->last_percent
= (unsigned) -1;
136 i
->image_root
= strdup(image_root
?: "/var/lib/machines");
140 i
->grow_machine_directory
= path_startswith(i
->image_root
, "/var/lib/machines");
143 i
->event
= sd_event_ref(event
);
145 r
= sd_event_default(&i
->event
);
156 static void tar_import_report_progress(TarImport
*i
) {
160 /* We have no size information, unless the source is a regular file */
161 if (!S_ISREG(i
->st
.st_mode
))
164 if (i
->written_compressed
>= (uint64_t) i
->st
.st_size
)
167 percent
= (unsigned) ((i
->written_compressed
* UINT64_C(100)) / (uint64_t) i
->st
.st_size
);
169 if (percent
== i
->last_percent
)
172 if (!ratelimit_test(&i
->progress_rate_limit
))
175 sd_notifyf(false, "X_IMPORT_PROGRESS=%u", percent
);
176 log_info("Imported %u%%.", percent
);
178 i
->last_percent
= percent
;
181 static int tar_import_finish(TarImport
*i
) {
185 assert(i
->tar_fd
>= 0);
186 assert(i
->temp_path
);
187 assert(i
->final_path
);
189 i
->tar_fd
= safe_close(i
->tar_fd
);
191 if (i
->tar_pid
> 0) {
192 r
= wait_for_terminate_and_warn("tar", i
->tar_pid
, true);
199 r
= import_make_read_only(i
->temp_path
);
205 (void) rm_rf(i
->final_path
, REMOVE_ROOT
|REMOVE_PHYSICAL
|REMOVE_SUBVOLUME
);
207 r
= rename_noreplace(AT_FDCWD
, i
->temp_path
, AT_FDCWD
, i
->final_path
);
209 return log_error_errno(r
, "Failed to move image into place: %m");
211 i
->temp_path
= mfree(i
->temp_path
);
216 static int tar_import_fork_tar(TarImport
*i
) {
221 assert(!i
->final_path
);
222 assert(!i
->temp_path
);
223 assert(i
->tar_fd
< 0);
225 i
->final_path
= strjoin(i
->image_root
, "/", i
->local
, NULL
);
229 r
= tempfn_random(i
->final_path
, NULL
, &i
->temp_path
);
233 (void) mkdir_parents_label(i
->temp_path
, 0700);
235 r
= btrfs_subvol_make(i
->temp_path
);
237 if (mkdir(i
->temp_path
, 0755) < 0)
238 return log_error_errno(errno
, "Failed to create directory %s: %m", i
->temp_path
);
240 return log_error_errno(r
, "Failed to create subvolume %s: %m", i
->temp_path
);
242 (void) import_assign_pool_quota_and_warn(i
->temp_path
);
244 i
->tar_fd
= import_fork_tar_x(i
->temp_path
, &i
->tar_pid
);
251 static int tar_import_write(const void *p
, size_t sz
, void *userdata
) {
252 TarImport
*i
= userdata
;
255 if (i
->grow_machine_directory
&& i
->written_since_last_grow
>= GROW_INTERVAL_BYTES
) {
256 i
->written_since_last_grow
= 0;
257 grow_machine_directory();
260 r
= loop_write(i
->tar_fd
, p
, sz
, false);
264 i
->written_uncompressed
+= sz
;
265 i
->written_since_last_grow
+= sz
;
270 static int tar_import_process(TarImport
*i
) {
275 assert(i
->buffer_size
< sizeof(i
->buffer
));
277 l
= read(i
->input_fd
, i
->buffer
+ i
->buffer_size
, sizeof(i
->buffer
) - i
->buffer_size
);
282 r
= log_error_errno(errno
, "Failed to read input file: %m");
286 if (i
->compress
.type
== IMPORT_COMPRESS_UNKNOWN
) {
287 log_error("Premature end of file: %m");
292 r
= tar_import_finish(i
);
298 if (i
->compress
.type
== IMPORT_COMPRESS_UNKNOWN
) {
299 r
= import_uncompress_detect(&i
->compress
, i
->buffer
, i
->buffer_size
);
301 log_error("Failed to detect file compression: %m");
304 if (r
== 0) /* Need more data */
307 r
= tar_import_fork_tar(i
);
312 r
= import_uncompress(&i
->compress
, i
->buffer
, i
->buffer_size
, tar_import_write
, i
);
314 log_error_errno(r
, "Failed to decode and write: %m");
318 i
->written_compressed
+= i
->buffer_size
;
321 tar_import_report_progress(i
);
327 i
->on_finished(i
, r
, i
->userdata
);
329 sd_event_exit(i
->event
, r
);
334 static int tar_import_on_input(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
335 TarImport
*i
= userdata
;
337 return tar_import_process(i
);
340 static int tar_import_on_defer(sd_event_source
*s
, void *userdata
) {
341 TarImport
*i
= userdata
;
343 return tar_import_process(i
);
346 int tar_import_start(TarImport
*i
, int fd
, const char *local
, bool force_local
, bool read_only
) {
353 if (!machine_name_is_valid(local
))
356 if (i
->input_fd
>= 0)
359 r
= fd_nonblock(fd
, true);
363 r
= free_and_strdup(&i
->local
, local
);
366 i
->force_local
= force_local
;
367 i
->read_only
= read_only
;
369 if (fstat(fd
, &i
->st
) < 0)
372 r
= sd_event_add_io(i
->event
, &i
->input_event_source
, fd
, EPOLLIN
, tar_import_on_input
, i
);
374 /* This fd does not support epoll, for example because it is a regular file. Busy read in that case */
375 r
= sd_event_add_defer(i
->event
, &i
->input_event_source
, tar_import_on_defer
, i
);
379 r
= sd_event_source_set_enabled(i
->input_event_source
, SD_EVENT_ON
);