1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
8 #include "alloc-util.h"
9 #include "btrfs-util.h"
13 #include "hostname-util.h"
14 #include "import-common.h"
15 #include "import-compress.h"
16 #include "import-raw.h"
18 #include "machine-pool.h"
20 #include "path-util.h"
21 #include "qcow2-util.h"
22 #include "ratelimit.h"
24 #include "string-util.h"
25 #include "tmpfile-util.h"
33 RawImportFinished on_finished
;
46 ImportCompress compress
;
48 sd_event_source
*input_event_source
;
50 uint8_t buffer
[16*1024];
53 uint64_t written_compressed
;
54 uint64_t written_uncompressed
;
58 unsigned last_percent
;
59 RateLimit progress_ratelimit
;
62 RawImport
* raw_import_unref(RawImport
*i
) {
66 sd_event_unref(i
->event
);
69 (void) unlink(i
->temp_path
);
73 import_compress_free(&i
->compress
);
75 sd_event_source_unref(i
->input_event_source
);
77 safe_close(i
->output_fd
);
88 const char *image_root
,
89 RawImportFinished on_finished
,
92 _cleanup_(raw_import_unrefp
) RawImport
*i
= NULL
;
93 _cleanup_free_
char *root
= NULL
;
98 root
= strdup(image_root
?: "/var/lib/machines");
102 i
= new(RawImport
, 1);
109 .on_finished
= on_finished
,
110 .userdata
= userdata
,
111 .last_percent
= (unsigned) -1,
112 .image_root
= TAKE_PTR(root
),
113 .progress_ratelimit
= { 100 * USEC_PER_MSEC
, 1 },
117 i
->event
= sd_event_ref(event
);
119 r
= sd_event_default(&i
->event
);
129 static void raw_import_report_progress(RawImport
*i
) {
133 /* We have no size information, unless the source is a regular file */
134 if (!S_ISREG(i
->st
.st_mode
))
137 if (i
->written_compressed
>= (uint64_t) i
->st
.st_size
)
140 percent
= (unsigned) ((i
->written_compressed
* UINT64_C(100)) / (uint64_t) i
->st
.st_size
);
142 if (percent
== i
->last_percent
)
145 if (!ratelimit_below(&i
->progress_ratelimit
))
148 sd_notifyf(false, "X_IMPORT_PROGRESS=%u", percent
);
149 log_info("Imported %u%%.", percent
);
151 i
->last_percent
= percent
;
154 static int raw_import_maybe_convert_qcow2(RawImport
*i
) {
155 _cleanup_close_
int converted_fd
= -1;
156 _cleanup_free_
char *t
= NULL
;
161 r
= qcow2_detect(i
->output_fd
);
163 return log_error_errno(r
, "Failed to detect whether this is a QCOW2 image: %m");
167 /* This is a QCOW2 image, let's convert it */
168 r
= tempfn_random(i
->final_path
, NULL
, &t
);
172 converted_fd
= open(t
, O_RDWR
|O_CREAT
|O_EXCL
|O_NOCTTY
|O_CLOEXEC
, 0664);
173 if (converted_fd
< 0)
174 return log_error_errno(errno
, "Failed to create %s: %m", t
);
176 (void) import_set_nocow_and_log(converted_fd
, t
);
178 log_info("Unpacking QCOW2 file.");
180 r
= qcow2_convert(i
->output_fd
, converted_fd
);
183 return log_error_errno(r
, "Failed to convert qcow2 image: %m");
186 (void) unlink(i
->temp_path
);
187 free_and_replace(i
->temp_path
, t
);
188 CLOSE_AND_REPLACE(i
->output_fd
, converted_fd
);
193 static int raw_import_finish(RawImport
*i
) {
197 assert(i
->output_fd
>= 0);
198 assert(i
->temp_path
);
199 assert(i
->final_path
);
201 /* In case this was a sparse file, make sure the file system is right */
202 if (i
->written_uncompressed
> 0) {
203 if (ftruncate(i
->output_fd
, i
->written_uncompressed
) < 0)
204 return log_error_errno(errno
, "Failed to truncate file: %m");
207 r
= raw_import_maybe_convert_qcow2(i
);
211 if (S_ISREG(i
->st
.st_mode
)) {
212 (void) copy_times(i
->input_fd
, i
->output_fd
, COPY_CRTIME
);
213 (void) copy_xattr(i
->input_fd
, i
->output_fd
);
217 r
= import_make_read_only_fd(i
->output_fd
);
223 (void) rm_rf(i
->final_path
, REMOVE_ROOT
|REMOVE_PHYSICAL
|REMOVE_SUBVOLUME
);
225 r
= rename_noreplace(AT_FDCWD
, i
->temp_path
, AT_FDCWD
, i
->final_path
);
227 return log_error_errno(r
, "Failed to move image into place: %m");
229 i
->temp_path
= mfree(i
->temp_path
);
234 static int raw_import_open_disk(RawImport
*i
) {
239 assert(!i
->final_path
);
240 assert(!i
->temp_path
);
241 assert(i
->output_fd
< 0);
243 i
->final_path
= strjoin(i
->image_root
, "/", i
->local
, ".raw");
247 r
= tempfn_random(i
->final_path
, NULL
, &i
->temp_path
);
251 (void) mkdir_parents_label(i
->temp_path
, 0700);
253 i
->output_fd
= open(i
->temp_path
, O_RDWR
|O_CREAT
|O_EXCL
|O_NOCTTY
|O_CLOEXEC
, 0664);
254 if (i
->output_fd
< 0)
255 return log_error_errno(errno
, "Failed to open destination %s: %m", i
->temp_path
);
257 (void) import_set_nocow_and_log(i
->output_fd
, i
->temp_path
);
261 static int raw_import_try_reflink(RawImport
*i
) {
266 assert(i
->input_fd
>= 0);
267 assert(i
->output_fd
>= 0);
269 if (i
->compress
.type
!= IMPORT_COMPRESS_UNCOMPRESSED
)
272 if (!S_ISREG(i
->st
.st_mode
))
275 p
= lseek(i
->input_fd
, 0, SEEK_CUR
);
277 return log_error_errno(errno
, "Failed to read file offset of input file: %m");
279 /* Let's only try a btrfs reflink, if we are reading from the beginning of the file */
280 if ((uint64_t) p
!= (uint64_t) i
->buffer_size
)
283 r
= btrfs_reflink(i
->input_fd
, i
->output_fd
);
290 static int raw_import_write(const void *p
, size_t sz
, void *userdata
) {
291 RawImport
*i
= userdata
;
294 n
= sparse_write(i
->output_fd
, p
, sz
, 64);
300 i
->written_uncompressed
+= sz
;
305 static int raw_import_process(RawImport
*i
) {
310 assert(i
->buffer_size
< sizeof(i
->buffer
));
312 l
= read(i
->input_fd
, i
->buffer
+ i
->buffer_size
, sizeof(i
->buffer
) - i
->buffer_size
);
317 r
= log_error_errno(errno
, "Failed to read input file: %m");
321 if (i
->compress
.type
== IMPORT_COMPRESS_UNKNOWN
) {
322 log_error("Premature end of file.");
327 r
= raw_import_finish(i
);
333 if (i
->compress
.type
== IMPORT_COMPRESS_UNKNOWN
) {
334 r
= import_uncompress_detect(&i
->compress
, i
->buffer
, i
->buffer_size
);
336 log_error_errno(r
, "Failed to detect file compression: %m");
339 if (r
== 0) /* Need more data */
342 r
= raw_import_open_disk(i
);
346 r
= raw_import_try_reflink(i
);
350 r
= raw_import_finish(i
);
355 r
= import_uncompress(&i
->compress
, i
->buffer
, i
->buffer_size
, raw_import_write
, i
);
357 log_error_errno(r
, "Failed to decode and write: %m");
361 i
->written_compressed
+= i
->buffer_size
;
364 raw_import_report_progress(i
);
370 i
->on_finished(i
, r
, i
->userdata
);
372 sd_event_exit(i
->event
, r
);
377 static int raw_import_on_input(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
378 RawImport
*i
= userdata
;
380 return raw_import_process(i
);
383 static int raw_import_on_defer(sd_event_source
*s
, void *userdata
) {
384 RawImport
*i
= userdata
;
386 return raw_import_process(i
);
389 int raw_import_start(RawImport
*i
, int fd
, const char *local
, bool force_local
, bool read_only
) {
396 if (!hostname_is_valid(local
, 0))
399 if (i
->input_fd
>= 0)
402 r
= fd_nonblock(fd
, true);
406 r
= free_and_strdup(&i
->local
, local
);
409 i
->force_local
= force_local
;
410 i
->read_only
= read_only
;
412 if (fstat(fd
, &i
->st
) < 0)
415 r
= sd_event_add_io(i
->event
, &i
->input_event_source
, fd
, EPOLLIN
, raw_import_on_input
, i
);
417 /* This fd does not support epoll, for example because it is a regular file. Busy read in that case */
418 r
= sd_event_add_defer(i
->event
, &i
->input_event_source
, raw_import_on_defer
, i
);
422 r
= sd_event_source_set_enabled(i
->input_event_source
, SD_EVENT_ON
);