1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2015 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
23 #include "sd-daemon.h"
26 #include "alloc-util.h"
27 #include "btrfs-util.h"
32 #include "hostname-util.h"
33 #include "import-common.h"
34 #include "import-compress.h"
35 #include "import-tar.h"
37 #include "machine-pool.h"
39 #include "path-util.h"
40 #include "process-util.h"
41 #include "qcow2-util.h"
42 #include "ratelimit.h"
44 #include "string-util.h"
52 TarImportFinished on_finished
;
58 bool grow_machine_directory
;
66 ImportCompress compress
;
68 uint64_t written_since_last_grow
;
70 sd_event_source
*input_event_source
;
72 uint8_t buffer
[16*1024];
75 uint64_t written_compressed
;
76 uint64_t written_uncompressed
;
82 unsigned last_percent
;
83 RateLimit progress_rate_limit
;
86 TarImport
* tar_import_unref(TarImport
*i
) {
90 sd_event_source_unref(i
->input_event_source
);
93 (void) kill_and_sigcont(i
->tar_pid
, SIGKILL
);
94 (void) wait_for_terminate(i
->tar_pid
, NULL
);
98 (void) rm_rf(i
->temp_path
, REMOVE_ROOT
|REMOVE_PHYSICAL
|REMOVE_SUBVOLUME
);
102 import_compress_free(&i
->compress
);
104 sd_event_unref(i
->event
);
106 safe_close(i
->tar_fd
);
117 const char *image_root
,
118 TarImportFinished on_finished
,
121 _cleanup_(tar_import_unrefp
) TarImport
*i
= NULL
;
126 i
= new0(TarImport
, 1);
130 i
->input_fd
= i
->tar_fd
= -1;
131 i
->on_finished
= on_finished
;
132 i
->userdata
= userdata
;
134 RATELIMIT_INIT(i
->progress_rate_limit
, 100 * USEC_PER_MSEC
, 1);
135 i
->last_percent
= (unsigned) -1;
137 i
->image_root
= strdup(image_root
?: "/var/lib/machines");
141 i
->grow_machine_directory
= path_startswith(i
->image_root
, "/var/lib/machines");
144 i
->event
= sd_event_ref(event
);
146 r
= sd_event_default(&i
->event
);
157 static void tar_import_report_progress(TarImport
*i
) {
161 /* We have no size information, unless the source is a regular file */
162 if (!S_ISREG(i
->st
.st_mode
))
165 if (i
->written_compressed
>= (uint64_t) i
->st
.st_size
)
168 percent
= (unsigned) ((i
->written_compressed
* UINT64_C(100)) / (uint64_t) i
->st
.st_size
);
170 if (percent
== i
->last_percent
)
173 if (!ratelimit_test(&i
->progress_rate_limit
))
176 sd_notifyf(false, "X_IMPORT_PROGRESS=%u", percent
);
177 log_info("Imported %u%%.", percent
);
179 i
->last_percent
= percent
;
182 static int tar_import_finish(TarImport
*i
) {
186 assert(i
->tar_fd
>= 0);
187 assert(i
->temp_path
);
188 assert(i
->final_path
);
190 i
->tar_fd
= safe_close(i
->tar_fd
);
192 if (i
->tar_pid
> 0) {
193 r
= wait_for_terminate_and_check("tar", i
->tar_pid
, WAIT_LOG
);
200 r
= import_make_read_only(i
->temp_path
);
206 (void) rm_rf(i
->final_path
, REMOVE_ROOT
|REMOVE_PHYSICAL
|REMOVE_SUBVOLUME
);
208 r
= rename_noreplace(AT_FDCWD
, i
->temp_path
, AT_FDCWD
, i
->final_path
);
210 return log_error_errno(r
, "Failed to move image into place: %m");
212 i
->temp_path
= mfree(i
->temp_path
);
217 static int tar_import_fork_tar(TarImport
*i
) {
222 assert(!i
->final_path
);
223 assert(!i
->temp_path
);
224 assert(i
->tar_fd
< 0);
226 i
->final_path
= strjoin(i
->image_root
, "/", i
->local
);
230 r
= tempfn_random(i
->final_path
, NULL
, &i
->temp_path
);
234 (void) mkdir_parents_label(i
->temp_path
, 0700);
236 r
= btrfs_subvol_make(i
->temp_path
);
238 if (mkdir(i
->temp_path
, 0755) < 0)
239 return log_error_errno(errno
, "Failed to create directory %s: %m", i
->temp_path
);
241 return log_error_errno(r
, "Failed to create subvolume %s: %m", i
->temp_path
);
243 (void) import_assign_pool_quota_and_warn(i
->temp_path
);
245 i
->tar_fd
= import_fork_tar_x(i
->temp_path
, &i
->tar_pid
);
252 static int tar_import_write(const void *p
, size_t sz
, void *userdata
) {
253 TarImport
*i
= userdata
;
256 if (i
->grow_machine_directory
&& i
->written_since_last_grow
>= GROW_INTERVAL_BYTES
) {
257 i
->written_since_last_grow
= 0;
258 grow_machine_directory();
261 r
= loop_write(i
->tar_fd
, p
, sz
, false);
265 i
->written_uncompressed
+= sz
;
266 i
->written_since_last_grow
+= sz
;
271 static int tar_import_process(TarImport
*i
) {
276 assert(i
->buffer_size
< sizeof(i
->buffer
));
278 l
= read(i
->input_fd
, i
->buffer
+ i
->buffer_size
, sizeof(i
->buffer
) - i
->buffer_size
);
283 r
= log_error_errno(errno
, "Failed to read input file: %m");
287 if (i
->compress
.type
== IMPORT_COMPRESS_UNKNOWN
) {
288 log_error("Premature end of file.");
293 r
= tar_import_finish(i
);
299 if (i
->compress
.type
== IMPORT_COMPRESS_UNKNOWN
) {
300 r
= import_uncompress_detect(&i
->compress
, i
->buffer
, i
->buffer_size
);
302 log_error_errno(r
, "Failed to detect file compression: %m");
305 if (r
== 0) /* Need more data */
308 r
= tar_import_fork_tar(i
);
313 r
= import_uncompress(&i
->compress
, i
->buffer
, i
->buffer_size
, tar_import_write
, i
);
315 log_error_errno(r
, "Failed to decode and write: %m");
319 i
->written_compressed
+= i
->buffer_size
;
322 tar_import_report_progress(i
);
328 i
->on_finished(i
, r
, i
->userdata
);
330 sd_event_exit(i
->event
, r
);
335 static int tar_import_on_input(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
336 TarImport
*i
= userdata
;
338 return tar_import_process(i
);
341 static int tar_import_on_defer(sd_event_source
*s
, void *userdata
) {
342 TarImport
*i
= userdata
;
344 return tar_import_process(i
);
347 int tar_import_start(TarImport
*i
, int fd
, const char *local
, bool force_local
, bool read_only
) {
354 if (!machine_name_is_valid(local
))
357 if (i
->input_fd
>= 0)
360 r
= fd_nonblock(fd
, true);
364 r
= free_and_strdup(&i
->local
, local
);
367 i
->force_local
= force_local
;
368 i
->read_only
= read_only
;
370 if (fstat(fd
, &i
->st
) < 0)
373 r
= sd_event_add_io(i
->event
, &i
->input_event_source
, fd
, EPOLLIN
, tar_import_on_input
, i
);
375 /* This fd does not support epoll, for example because it is a regular file. Busy read in that case */
376 r
= sd_event_add_defer(i
->event
, &i
->input_event_source
, tar_import_on_defer
, i
);
380 r
= sd_event_source_set_enabled(i
->input_event_source
, SD_EVENT_ON
);