1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
8 #include "alloc-util.h"
9 #include "btrfs-util.h"
12 #include "format-util.h"
14 #include "hostname-util.h"
15 #include "import-common.h"
16 #include "import-compress.h"
17 #include "import-raw.h"
18 #include "install-file.h"
20 #include "machine-pool.h"
21 #include "mkdir-label.h"
22 #include "path-util.h"
23 #include "qcow2-util.h"
24 #include "ratelimit.h"
26 #include "string-util.h"
27 #include "tmpfile-util.h"
34 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
;
56 struct stat input_stat
;
57 struct stat output_stat
;
59 unsigned last_percent
;
60 RateLimit progress_ratelimit
;
66 RawImport
* raw_import_unref(RawImport
*i
) {
70 sd_event_source_unref(i
->input_event_source
);
72 unlink_and_free(i
->temp_path
);
74 import_compress_free(&i
->compress
);
76 sd_event_unref(i
->event
);
78 safe_close(i
->output_fd
);
89 const char *image_root
,
90 RawImportFinished on_finished
,
93 _cleanup_(raw_import_unrefp
) RawImport
*i
= NULL
;
94 _cleanup_free_
char *root
= NULL
;
99 root
= strdup(image_root
?: "/var/lib/machines");
103 i
= new(RawImport
, 1);
110 .on_finished
= on_finished
,
111 .userdata
= userdata
,
112 .last_percent
= UINT_MAX
,
113 .image_root
= TAKE_PTR(root
),
114 .progress_ratelimit
= { 100 * USEC_PER_MSEC
, 1 },
115 .offset
= UINT64_MAX
,
116 .size_max
= UINT64_MAX
,
120 i
->event
= sd_event_ref(event
);
122 r
= sd_event_default(&i
->event
);
131 static void raw_import_report_progress(RawImport
*i
) {
135 /* We have no size information, unless the source is a regular file */
136 if (!S_ISREG(i
->input_stat
.st_mode
))
139 if (i
->written_compressed
>= (uint64_t) i
->input_stat
.st_size
)
142 percent
= (unsigned) ((i
->written_compressed
* UINT64_C(100)) / (uint64_t) i
->input_stat
.st_size
);
144 if (percent
== i
->last_percent
)
147 if (!ratelimit_below(&i
->progress_ratelimit
))
150 sd_notifyf(false, "X_IMPORT_PROGRESS=%u", percent
);
151 log_info("Imported %u%%.", percent
);
153 i
->last_percent
= percent
;
156 static int raw_import_maybe_convert_qcow2(RawImport
*i
) {
157 _cleanup_close_
int converted_fd
= -EBADF
;
158 _cleanup_(unlink_and_freep
) char *t
= NULL
;
159 _cleanup_free_
char *f
= NULL
;
164 /* Do QCOW2 conversion if enabled and not in direct mode */
165 if ((i
->flags
& (IMPORT_CONVERT_QCOW2
|IMPORT_DIRECT
)) != IMPORT_CONVERT_QCOW2
)
168 assert(i
->final_path
);
170 r
= qcow2_detect(i
->output_fd
);
172 return log_error_errno(r
, "Failed to detect whether this is a QCOW2 image: %m");
176 /* This is a QCOW2 image, let's convert it */
177 r
= tempfn_random(i
->final_path
, NULL
, &f
);
181 converted_fd
= open(f
, O_RDWR
|O_CREAT
|O_EXCL
|O_NOCTTY
|O_CLOEXEC
, 0664);
182 if (converted_fd
< 0)
183 return log_error_errno(errno
, "Failed to create %s: %m", f
);
187 (void) import_set_nocow_and_log(converted_fd
, t
);
189 log_info("Unpacking QCOW2 file.");
191 r
= qcow2_convert(i
->output_fd
, converted_fd
);
193 return log_error_errno(r
, "Failed to convert qcow2 image: %m");
195 unlink_and_free(i
->temp_path
);
196 i
->temp_path
= TAKE_PTR(t
);
197 close_and_replace(i
->output_fd
, converted_fd
);
202 static int raw_import_finish(RawImport
*i
) {
206 assert(i
->output_fd
>= 0);
208 /* Nothing of what is below applies to block devices */
209 if (S_ISBLK(i
->output_stat
.st_mode
)) {
211 if (i
->flags
& IMPORT_SYNC
) {
212 if (fsync(i
->output_fd
) < 0)
213 return log_error_errno(errno
, "Failed to synchronize block device: %m");
219 assert(S_ISREG(i
->output_stat
.st_mode
));
221 /* If an offset is specified we only are supposed to affect part of an existing output file or block
222 * device, thus don't manipulate file properties in that case */
224 if (i
->offset
== UINT64_MAX
) {
225 /* In case this was a sparse file, make sure the file size is right */
226 if (i
->written_uncompressed
> 0) {
227 if (ftruncate(i
->output_fd
, i
->written_uncompressed
) < 0)
228 return log_error_errno(errno
, "Failed to truncate file: %m");
231 r
= raw_import_maybe_convert_qcow2(i
);
235 if (S_ISREG(i
->input_stat
.st_mode
)) {
236 (void) copy_times(i
->input_fd
, i
->output_fd
, COPY_CRTIME
);
237 (void) copy_xattr(i
->input_fd
, NULL
, i
->output_fd
, NULL
, 0);
241 r
= install_file(AT_FDCWD
, i
->temp_path
?: i
->local
,
242 AT_FDCWD
, i
->final_path
,
243 (i
->flags
& IMPORT_FORCE
? INSTALL_REPLACE
: 0) |
244 (i
->flags
& IMPORT_READ_ONLY
? INSTALL_READ_ONLY
: 0) |
245 (i
->flags
& IMPORT_SYNC
? INSTALL_FSYNC_FULL
: 0));
247 return log_error_errno(r
, "Failed to move image into place: %m");
249 i
->temp_path
= mfree(i
->temp_path
);
251 log_info("Wrote %s.", FORMAT_BYTES(i
->written_uncompressed
));
256 static int raw_import_open_disk(RawImport
*i
) {
261 assert(!i
->final_path
);
262 assert(!i
->temp_path
);
263 assert(i
->output_fd
< 0);
265 if (i
->flags
& IMPORT_DIRECT
) {
266 (void) mkdir_parents_label(i
->local
, 0700);
268 /* In direct mode we just open/create the local path and truncate it (like shell >
269 * redirection would do it) — except if an offset was passed, in which case we are supposed
270 * to operate on a section of the file only, in which case we apparently work on an some
271 * existing thing (i.e. are not the sole thing stored in the file), in which case we will
272 * neither truncate nor create. */
274 i
->output_fd
= open(i
->local
, O_RDWR
|O_NOCTTY
|O_CLOEXEC
|(i
->offset
== UINT64_MAX
? O_TRUNC
|O_CREAT
: 0), 0664);
275 if (i
->output_fd
< 0)
276 return log_error_errno(errno
, "Failed to open destination '%s': %m", i
->local
);
278 if (i
->offset
== UINT64_MAX
)
279 (void) import_set_nocow_and_log(i
->output_fd
, i
->local
);
281 i
->final_path
= strjoin(i
->image_root
, "/", i
->local
, ".raw");
285 r
= tempfn_random(i
->final_path
, NULL
, &i
->temp_path
);
289 (void) mkdir_parents_label(i
->temp_path
, 0700);
291 i
->output_fd
= open(i
->temp_path
, O_RDWR
|O_CREAT
|O_EXCL
|O_NOCTTY
|O_CLOEXEC
, 0664);
292 if (i
->output_fd
< 0)
293 return log_error_errno(errno
, "Failed to open destination '%s': %m", i
->temp_path
);
295 (void) import_set_nocow_and_log(i
->output_fd
, i
->temp_path
);
298 if (fstat(i
->output_fd
, &i
->output_stat
) < 0)
299 return log_error_errno(errno
, "Failed to stat() output file: %m");
301 if (!S_ISREG(i
->output_stat
.st_mode
) && !S_ISBLK(i
->output_stat
.st_mode
))
302 return log_error_errno(SYNTHETIC_ERRNO(EBADFD
),
303 "Target file is not a regular file or block device");
305 if (i
->offset
!= UINT64_MAX
) {
306 if (lseek(i
->output_fd
, i
->offset
, SEEK_SET
) < 0)
307 return log_error_errno(errno
, "Failed to seek to offset: %m");
313 static int raw_import_try_reflink(RawImport
*i
) {
318 assert(i
->input_fd
>= 0);
319 assert(i
->output_fd
>= 0);
321 if (i
->compress
.type
!= IMPORT_COMPRESS_UNCOMPRESSED
)
324 if (i
->offset
!= UINT64_MAX
|| i
->size_max
!= UINT64_MAX
)
327 if (!S_ISREG(i
->input_stat
.st_mode
) || !S_ISREG(i
->output_stat
.st_mode
))
330 p
= lseek(i
->input_fd
, 0, SEEK_CUR
);
332 return log_error_errno(errno
, "Failed to read file offset of input file: %m");
334 /* Let's only try a btrfs reflink, if we are reading from the beginning of the file */
335 if ((uint64_t) p
!= (uint64_t) i
->buffer_size
)
338 r
= reflink(i
->input_fd
, i
->output_fd
);
342 log_debug_errno(r
, "Couldn't establish reflink, using copy: %m");
346 static int raw_import_write(const void *p
, size_t sz
, void *userdata
) {
347 RawImport
*i
= ASSERT_PTR(userdata
);
348 bool too_much
= false;
354 if (i
->written_uncompressed
>= UINT64_MAX
- sz
)
355 return log_error_errno(SYNTHETIC_ERRNO(EOVERFLOW
), "File too large, overflow");
357 if (i
->size_max
!= UINT64_MAX
) {
358 if (i
->written_uncompressed
>= i
->size_max
) {
363 if (i
->written_uncompressed
+ sz
> i
->size_max
) {
365 sz
= i
->size_max
- i
->written_uncompressed
; /* since we have the data in memory
366 * already, we might as well write it to
371 /* Generate sparse file if we created/truncated the file */
372 if (S_ISREG(i
->output_stat
.st_mode
) && i
->offset
== UINT64_MAX
) {
375 n
= sparse_write(i
->output_fd
, p
, sz
, 64);
377 return log_error_errno((int) n
, "Failed to write file: %m");
379 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Short write");
381 r
= loop_write(i
->output_fd
, p
, sz
);
383 return log_error_errno(r
, "Failed to write file: %m");
386 i
->written_uncompressed
+= sz
;
390 return log_error_errno(SYNTHETIC_ERRNO(E2BIG
), "File too large");
395 static int raw_import_process(RawImport
*i
) {
400 assert(i
->buffer_size
< sizeof(i
->buffer
));
402 l
= read(i
->input_fd
, i
->buffer
+ i
->buffer_size
, sizeof(i
->buffer
) - i
->buffer_size
);
407 r
= log_error_errno(errno
, "Failed to read input file: %m");
413 if (i
->compress
.type
== IMPORT_COMPRESS_UNKNOWN
) {
415 if (l
== 0) { /* EOF */
416 log_debug("File too short to be compressed, as no compression signature fits in, thus assuming uncompressed.");
417 import_uncompress_force_off(&i
->compress
);
419 r
= import_uncompress_detect(&i
->compress
, i
->buffer
, i
->buffer_size
);
421 log_error_errno(r
, "Failed to detect file compression: %m");
424 if (r
== 0) /* Need more data */
428 r
= raw_import_open_disk(i
);
432 r
= raw_import_try_reflink(i
);
439 r
= import_uncompress(&i
->compress
, i
->buffer
, i
->buffer_size
, raw_import_write
, i
);
441 log_error_errno(r
, "Failed to decode and write: %m");
445 i
->written_compressed
+= i
->buffer_size
;
448 if (l
== 0) /* EOF */
451 raw_import_report_progress(i
);
456 r
= raw_import_finish(i
);
460 i
->on_finished(i
, r
, i
->userdata
);
462 sd_event_exit(i
->event
, r
);
467 static int raw_import_on_input(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
468 RawImport
*i
= userdata
;
470 return raw_import_process(i
);
473 static int raw_import_on_defer(sd_event_source
*s
, void *userdata
) {
474 RawImport
*i
= userdata
;
476 return raw_import_process(i
);
479 int raw_import_start(
491 assert(!(flags
& ~IMPORT_FLAGS_MASK_RAW
));
492 assert(offset
== UINT64_MAX
|| FLAGS_SET(flags
, IMPORT_DIRECT
));
494 if (!import_validate_local(local
, flags
))
497 if (i
->input_fd
>= 0)
500 r
= fd_nonblock(fd
, true);
504 r
= free_and_strdup(&i
->local
, local
);
510 i
->size_max
= size_max
;
512 if (fstat(fd
, &i
->input_stat
) < 0)
515 r
= sd_event_add_io(i
->event
, &i
->input_event_source
, fd
, EPOLLIN
, raw_import_on_input
, i
);
517 /* This fd does not support epoll, for example because it is a regular file. Busy read in that case */
518 r
= sd_event_add_defer(i
->event
, &i
->input_event_source
, raw_import_on_defer
, i
);
522 r
= sd_event_source_set_enabled(i
->input_event_source
, SD_EVENT_ON
);