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"
28 #include "chattr-util.h"
33 #include "hostname-util.h"
34 #include "import-common.h"
35 #include "import-compress.h"
36 #include "import-raw.h"
38 #include "machine-pool.h"
40 #include "path-util.h"
41 #include "qcow2-util.h"
42 #include "ratelimit.h"
44 #include "string-util.h"
52 RawImportFinished 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
;
80 unsigned last_percent
;
81 RateLimit progress_rate_limit
;
84 RawImport
* raw_import_unref(RawImport
*i
) {
88 sd_event_unref(i
->event
);
91 (void) unlink(i
->temp_path
);
95 import_compress_free(&i
->compress
);
97 sd_event_source_unref(i
->input_event_source
);
99 safe_close(i
->output_fd
);
110 const char *image_root
,
111 RawImportFinished on_finished
,
114 _cleanup_(raw_import_unrefp
) RawImport
*i
= NULL
;
119 i
= new0(RawImport
, 1);
123 i
->input_fd
= i
->output_fd
= -1;
124 i
->on_finished
= on_finished
;
125 i
->userdata
= userdata
;
127 RATELIMIT_INIT(i
->progress_rate_limit
, 100 * USEC_PER_MSEC
, 1);
128 i
->last_percent
= (unsigned) -1;
130 i
->image_root
= strdup(image_root
?: "/var/lib/machines");
134 i
->grow_machine_directory
= path_startswith(i
->image_root
, "/var/lib/machines");
137 i
->event
= sd_event_ref(event
);
139 r
= sd_event_default(&i
->event
);
150 static void raw_import_report_progress(RawImport
*i
) {
154 /* We have no size information, unless the source is a regular file */
155 if (!S_ISREG(i
->st
.st_mode
))
158 if (i
->written_compressed
>= (uint64_t) i
->st
.st_size
)
161 percent
= (unsigned) ((i
->written_compressed
* UINT64_C(100)) / (uint64_t) i
->st
.st_size
);
163 if (percent
== i
->last_percent
)
166 if (!ratelimit_test(&i
->progress_rate_limit
))
169 sd_notifyf(false, "X_IMPORT_PROGRESS=%u", percent
);
170 log_info("Imported %u%%.", percent
);
172 i
->last_percent
= percent
;
175 static int raw_import_maybe_convert_qcow2(RawImport
*i
) {
176 _cleanup_close_
int converted_fd
= -1;
177 _cleanup_free_
char *t
= NULL
;
182 r
= qcow2_detect(i
->output_fd
);
184 return log_error_errno(r
, "Failed to detect whether this is a QCOW2 image: %m");
188 /* This is a QCOW2 image, let's convert it */
189 r
= tempfn_random(i
->final_path
, NULL
, &t
);
193 converted_fd
= open(t
, O_RDWR
|O_CREAT
|O_EXCL
|O_NOCTTY
|O_CLOEXEC
, 0664);
194 if (converted_fd
< 0)
195 return log_error_errno(errno
, "Failed to create %s: %m", t
);
197 r
= chattr_fd(converted_fd
, FS_NOCOW_FL
, FS_NOCOW_FL
);
199 log_warning_errno(r
, "Failed to set file attributes on %s: %m", t
);
201 log_info("Unpacking QCOW2 file.");
203 r
= qcow2_convert(i
->output_fd
, converted_fd
);
206 return log_error_errno(r
, "Failed to convert qcow2 image: %m");
209 (void) unlink(i
->temp_path
);
210 free_and_replace(i
->temp_path
, t
);
212 safe_close(i
->output_fd
);
213 i
->output_fd
= converted_fd
;
219 static int raw_import_finish(RawImport
*i
) {
223 assert(i
->output_fd
>= 0);
224 assert(i
->temp_path
);
225 assert(i
->final_path
);
227 /* In case this was a sparse file, make sure the file system is right */
228 if (i
->written_uncompressed
> 0) {
229 if (ftruncate(i
->output_fd
, i
->written_uncompressed
) < 0)
230 return log_error_errno(errno
, "Failed to truncate file: %m");
233 r
= raw_import_maybe_convert_qcow2(i
);
237 if (S_ISREG(i
->st
.st_mode
)) {
238 (void) copy_times(i
->input_fd
, i
->output_fd
);
239 (void) copy_xattr(i
->input_fd
, i
->output_fd
);
243 r
= import_make_read_only_fd(i
->output_fd
);
249 (void) rm_rf(i
->final_path
, REMOVE_ROOT
|REMOVE_PHYSICAL
|REMOVE_SUBVOLUME
);
251 r
= rename_noreplace(AT_FDCWD
, i
->temp_path
, AT_FDCWD
, i
->final_path
);
253 return log_error_errno(r
, "Failed to move image into place: %m");
255 i
->temp_path
= mfree(i
->temp_path
);
260 static int raw_import_open_disk(RawImport
*i
) {
265 assert(!i
->final_path
);
266 assert(!i
->temp_path
);
267 assert(i
->output_fd
< 0);
269 i
->final_path
= strjoin(i
->image_root
, "/", i
->local
, ".raw");
273 r
= tempfn_random(i
->final_path
, NULL
, &i
->temp_path
);
277 (void) mkdir_parents_label(i
->temp_path
, 0700);
279 i
->output_fd
= open(i
->temp_path
, O_RDWR
|O_CREAT
|O_EXCL
|O_NOCTTY
|O_CLOEXEC
, 0664);
280 if (i
->output_fd
< 0)
281 return log_error_errno(errno
, "Failed to open destination %s: %m", i
->temp_path
);
283 r
= chattr_fd(i
->output_fd
, FS_NOCOW_FL
, FS_NOCOW_FL
);
285 log_warning_errno(r
, "Failed to set file attributes on %s: %m", i
->temp_path
);
290 static int raw_import_try_reflink(RawImport
*i
) {
295 assert(i
->input_fd
>= 0);
296 assert(i
->output_fd
>= 0);
298 if (i
->compress
.type
!= IMPORT_COMPRESS_UNCOMPRESSED
)
301 if (!S_ISREG(i
->st
.st_mode
))
304 p
= lseek(i
->input_fd
, 0, SEEK_CUR
);
306 return log_error_errno(errno
, "Failed to read file offset of input file: %m");
308 /* Let's only try a btrfs reflink, if we are reading from the beginning of the file */
309 if ((uint64_t) p
!= (uint64_t) i
->buffer_size
)
312 r
= btrfs_reflink(i
->input_fd
, i
->output_fd
);
319 static int raw_import_write(const void *p
, size_t sz
, void *userdata
) {
320 RawImport
*i
= userdata
;
323 if (i
->grow_machine_directory
&& i
->written_since_last_grow
>= GROW_INTERVAL_BYTES
) {
324 i
->written_since_last_grow
= 0;
325 grow_machine_directory();
328 n
= sparse_write(i
->output_fd
, p
, sz
, 64);
334 i
->written_uncompressed
+= sz
;
335 i
->written_since_last_grow
+= sz
;
340 static int raw_import_process(RawImport
*i
) {
345 assert(i
->buffer_size
< sizeof(i
->buffer
));
347 l
= read(i
->input_fd
, i
->buffer
+ i
->buffer_size
, sizeof(i
->buffer
) - i
->buffer_size
);
352 r
= log_error_errno(errno
, "Failed to read input file: %m");
356 if (i
->compress
.type
== IMPORT_COMPRESS_UNKNOWN
) {
357 log_error("Premature end of file.");
362 r
= raw_import_finish(i
);
368 if (i
->compress
.type
== IMPORT_COMPRESS_UNKNOWN
) {
369 r
= import_uncompress_detect(&i
->compress
, i
->buffer
, i
->buffer_size
);
371 log_error_errno(r
, "Failed to detect file compression: %m");
374 if (r
== 0) /* Need more data */
377 r
= raw_import_open_disk(i
);
381 r
= raw_import_try_reflink(i
);
385 r
= raw_import_finish(i
);
390 r
= import_uncompress(&i
->compress
, i
->buffer
, i
->buffer_size
, raw_import_write
, i
);
392 log_error_errno(r
, "Failed to decode and write: %m");
396 i
->written_compressed
+= i
->buffer_size
;
399 raw_import_report_progress(i
);
405 i
->on_finished(i
, r
, i
->userdata
);
407 sd_event_exit(i
->event
, r
);
412 static int raw_import_on_input(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
413 RawImport
*i
= userdata
;
415 return raw_import_process(i
);
418 static int raw_import_on_defer(sd_event_source
*s
, void *userdata
) {
419 RawImport
*i
= userdata
;
421 return raw_import_process(i
);
424 int raw_import_start(RawImport
*i
, int fd
, const char *local
, bool force_local
, bool read_only
) {
431 if (!machine_name_is_valid(local
))
434 if (i
->input_fd
>= 0)
437 r
= fd_nonblock(fd
, true);
441 r
= free_and_strdup(&i
->local
, local
);
444 i
->force_local
= force_local
;
445 i
->read_only
= read_only
;
447 if (fstat(fd
, &i
->st
) < 0)
450 r
= sd_event_add_io(i
->event
, &i
->input_event_source
, fd
, EPOLLIN
, raw_import_on_input
, i
);
452 /* This fd does not support epoll, for example because it is a regular file. Busy read in that case */
453 r
= sd_event_add_defer(i
->event
, &i
->input_event_source
, raw_import_on_defer
, i
);
457 r
= sd_event_source_set_enabled(i
->input_event_source
, SD_EVENT_ON
);