1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2015 Lennart Poettering
10 #include "sd-daemon.h"
13 #include "alloc-util.h"
14 #include "btrfs-util.h"
15 #include "chattr-util.h"
20 #include "hostname-util.h"
21 #include "import-common.h"
22 #include "import-compress.h"
23 #include "import-raw.h"
25 #include "machine-pool.h"
27 #include "path-util.h"
28 #include "qcow2-util.h"
29 #include "ratelimit.h"
31 #include "string-util.h"
39 RawImportFinished on_finished
;
45 bool grow_machine_directory
;
53 ImportCompress compress
;
55 uint64_t written_since_last_grow
;
57 sd_event_source
*input_event_source
;
59 uint8_t buffer
[16*1024];
62 uint64_t written_compressed
;
63 uint64_t written_uncompressed
;
67 unsigned last_percent
;
68 RateLimit progress_rate_limit
;
71 RawImport
* raw_import_unref(RawImport
*i
) {
75 sd_event_unref(i
->event
);
78 (void) unlink(i
->temp_path
);
82 import_compress_free(&i
->compress
);
84 sd_event_source_unref(i
->input_event_source
);
86 safe_close(i
->output_fd
);
97 const char *image_root
,
98 RawImportFinished on_finished
,
101 _cleanup_(raw_import_unrefp
) RawImport
*i
= NULL
;
106 i
= new0(RawImport
, 1);
110 i
->input_fd
= i
->output_fd
= -1;
111 i
->on_finished
= on_finished
;
112 i
->userdata
= userdata
;
114 RATELIMIT_INIT(i
->progress_rate_limit
, 100 * USEC_PER_MSEC
, 1);
115 i
->last_percent
= (unsigned) -1;
117 i
->image_root
= strdup(image_root
?: "/var/lib/machines");
121 i
->grow_machine_directory
= path_startswith(i
->image_root
, "/var/lib/machines");
124 i
->event
= sd_event_ref(event
);
126 r
= sd_event_default(&i
->event
);
136 static void raw_import_report_progress(RawImport
*i
) {
140 /* We have no size information, unless the source is a regular file */
141 if (!S_ISREG(i
->st
.st_mode
))
144 if (i
->written_compressed
>= (uint64_t) i
->st
.st_size
)
147 percent
= (unsigned) ((i
->written_compressed
* UINT64_C(100)) / (uint64_t) i
->st
.st_size
);
149 if (percent
== i
->last_percent
)
152 if (!ratelimit_test(&i
->progress_rate_limit
))
155 sd_notifyf(false, "X_IMPORT_PROGRESS=%u", percent
);
156 log_info("Imported %u%%.", percent
);
158 i
->last_percent
= percent
;
161 static int raw_import_maybe_convert_qcow2(RawImport
*i
) {
162 _cleanup_close_
int converted_fd
= -1;
163 _cleanup_free_
char *t
= NULL
;
168 r
= qcow2_detect(i
->output_fd
);
170 return log_error_errno(r
, "Failed to detect whether this is a QCOW2 image: %m");
174 /* This is a QCOW2 image, let's convert it */
175 r
= tempfn_random(i
->final_path
, NULL
, &t
);
179 converted_fd
= open(t
, O_RDWR
|O_CREAT
|O_EXCL
|O_NOCTTY
|O_CLOEXEC
, 0664);
180 if (converted_fd
< 0)
181 return log_error_errno(errno
, "Failed to create %s: %m", t
);
183 r
= chattr_fd(converted_fd
, FS_NOCOW_FL
, FS_NOCOW_FL
);
185 log_warning_errno(r
, "Failed to set file attributes on %s: %m", t
);
187 log_info("Unpacking QCOW2 file.");
189 r
= qcow2_convert(i
->output_fd
, converted_fd
);
192 return log_error_errno(r
, "Failed to convert qcow2 image: %m");
195 (void) unlink(i
->temp_path
);
196 free_and_replace(i
->temp_path
, t
);
198 safe_close(i
->output_fd
);
199 i
->output_fd
= TAKE_FD(converted_fd
);
204 static int raw_import_finish(RawImport
*i
) {
208 assert(i
->output_fd
>= 0);
209 assert(i
->temp_path
);
210 assert(i
->final_path
);
212 /* In case this was a sparse file, make sure the file system is right */
213 if (i
->written_uncompressed
> 0) {
214 if (ftruncate(i
->output_fd
, i
->written_uncompressed
) < 0)
215 return log_error_errno(errno
, "Failed to truncate file: %m");
218 r
= raw_import_maybe_convert_qcow2(i
);
222 if (S_ISREG(i
->st
.st_mode
)) {
223 (void) copy_times(i
->input_fd
, i
->output_fd
);
224 (void) copy_xattr(i
->input_fd
, i
->output_fd
);
228 r
= import_make_read_only_fd(i
->output_fd
);
234 (void) rm_rf(i
->final_path
, REMOVE_ROOT
|REMOVE_PHYSICAL
|REMOVE_SUBVOLUME
);
236 r
= rename_noreplace(AT_FDCWD
, i
->temp_path
, AT_FDCWD
, i
->final_path
);
238 return log_error_errno(r
, "Failed to move image into place: %m");
240 i
->temp_path
= mfree(i
->temp_path
);
245 static int raw_import_open_disk(RawImport
*i
) {
250 assert(!i
->final_path
);
251 assert(!i
->temp_path
);
252 assert(i
->output_fd
< 0);
254 i
->final_path
= strjoin(i
->image_root
, "/", i
->local
, ".raw");
258 r
= tempfn_random(i
->final_path
, NULL
, &i
->temp_path
);
262 (void) mkdir_parents_label(i
->temp_path
, 0700);
264 i
->output_fd
= open(i
->temp_path
, O_RDWR
|O_CREAT
|O_EXCL
|O_NOCTTY
|O_CLOEXEC
, 0664);
265 if (i
->output_fd
< 0)
266 return log_error_errno(errno
, "Failed to open destination %s: %m", i
->temp_path
);
268 r
= chattr_fd(i
->output_fd
, FS_NOCOW_FL
, FS_NOCOW_FL
);
270 log_warning_errno(r
, "Failed to set file attributes on %s: %m", i
->temp_path
);
275 static int raw_import_try_reflink(RawImport
*i
) {
280 assert(i
->input_fd
>= 0);
281 assert(i
->output_fd
>= 0);
283 if (i
->compress
.type
!= IMPORT_COMPRESS_UNCOMPRESSED
)
286 if (!S_ISREG(i
->st
.st_mode
))
289 p
= lseek(i
->input_fd
, 0, SEEK_CUR
);
291 return log_error_errno(errno
, "Failed to read file offset of input file: %m");
293 /* Let's only try a btrfs reflink, if we are reading from the beginning of the file */
294 if ((uint64_t) p
!= (uint64_t) i
->buffer_size
)
297 r
= btrfs_reflink(i
->input_fd
, i
->output_fd
);
304 static int raw_import_write(const void *p
, size_t sz
, void *userdata
) {
305 RawImport
*i
= userdata
;
308 if (i
->grow_machine_directory
&& i
->written_since_last_grow
>= GROW_INTERVAL_BYTES
) {
309 i
->written_since_last_grow
= 0;
310 grow_machine_directory();
313 n
= sparse_write(i
->output_fd
, p
, sz
, 64);
319 i
->written_uncompressed
+= sz
;
320 i
->written_since_last_grow
+= sz
;
325 static int raw_import_process(RawImport
*i
) {
330 assert(i
->buffer_size
< sizeof(i
->buffer
));
332 l
= read(i
->input_fd
, i
->buffer
+ i
->buffer_size
, sizeof(i
->buffer
) - i
->buffer_size
);
337 r
= log_error_errno(errno
, "Failed to read input file: %m");
341 if (i
->compress
.type
== IMPORT_COMPRESS_UNKNOWN
) {
342 log_error("Premature end of file.");
347 r
= raw_import_finish(i
);
353 if (i
->compress
.type
== IMPORT_COMPRESS_UNKNOWN
) {
354 r
= import_uncompress_detect(&i
->compress
, i
->buffer
, i
->buffer_size
);
356 log_error_errno(r
, "Failed to detect file compression: %m");
359 if (r
== 0) /* Need more data */
362 r
= raw_import_open_disk(i
);
366 r
= raw_import_try_reflink(i
);
370 r
= raw_import_finish(i
);
375 r
= import_uncompress(&i
->compress
, i
->buffer
, i
->buffer_size
, raw_import_write
, i
);
377 log_error_errno(r
, "Failed to decode and write: %m");
381 i
->written_compressed
+= i
->buffer_size
;
384 raw_import_report_progress(i
);
390 i
->on_finished(i
, r
, i
->userdata
);
392 sd_event_exit(i
->event
, r
);
397 static int raw_import_on_input(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
398 RawImport
*i
= userdata
;
400 return raw_import_process(i
);
403 static int raw_import_on_defer(sd_event_source
*s
, void *userdata
) {
404 RawImport
*i
= userdata
;
406 return raw_import_process(i
);
409 int raw_import_start(RawImport
*i
, int fd
, const char *local
, bool force_local
, bool read_only
) {
416 if (!machine_name_is_valid(local
))
419 if (i
->input_fd
>= 0)
422 r
= fd_nonblock(fd
, true);
426 r
= free_and_strdup(&i
->local
, local
);
429 i
->force_local
= force_local
;
430 i
->read_only
= read_only
;
432 if (fstat(fd
, &i
->st
) < 0)
435 r
= sd_event_add_io(i
->event
, &i
->input_event_source
, fd
, EPOLLIN
, raw_import_on_input
, i
);
437 /* This fd does not support epoll, for example because it is a regular file. Busy read in that case */
438 r
= sd_event_add_defer(i
->event
, &i
->input_event_source
, raw_import_on_defer
, i
);
442 r
= sd_event_source_set_enabled(i
->input_event_source
, SD_EVENT_ON
);