1 /* SPDX-License-Identifier: LGPL-2.1+ */
8 #include "alloc-util.h"
9 #include "btrfs-util.h"
10 #include "chattr-util.h"
14 #include "hostname-util.h"
15 #include "import-common.h"
16 #include "import-compress.h"
17 #include "import-raw.h"
19 #include "machine-pool.h"
21 #include "path-util.h"
22 #include "qcow2-util.h"
23 #include "ratelimit.h"
25 #include "string-util.h"
26 #include "tmpfile-util.h"
34 RawImportFinished on_finished
;
47 ImportCompress compress
;
49 sd_event_source
*input_event_source
;
51 uint8_t buffer
[16*1024];
54 uint64_t written_compressed
;
55 uint64_t written_uncompressed
;
59 unsigned last_percent
;
60 RateLimit progress_rate_limit
;
63 RawImport
* raw_import_unref(RawImport
*i
) {
67 sd_event_unref(i
->event
);
70 (void) unlink(i
->temp_path
);
74 import_compress_free(&i
->compress
);
76 sd_event_source_unref(i
->input_event_source
);
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
= (unsigned) -1,
113 .image_root
= TAKE_PTR(root
),
116 RATELIMIT_INIT(i
->progress_rate_limit
, 100 * USEC_PER_MSEC
, 1);
119 i
->event
= sd_event_ref(event
);
121 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
->st
.st_mode
))
139 if (i
->written_compressed
>= (uint64_t) i
->st
.st_size
)
142 percent
= (unsigned) ((i
->written_compressed
* UINT64_C(100)) / (uint64_t) i
->st
.st_size
);
144 if (percent
== i
->last_percent
)
147 if (!ratelimit_below(&i
->progress_rate_limit
))
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
= -1;
158 _cleanup_free_
char *t
= NULL
;
163 r
= qcow2_detect(i
->output_fd
);
165 return log_error_errno(r
, "Failed to detect whether this is a QCOW2 image: %m");
169 /* This is a QCOW2 image, let's convert it */
170 r
= tempfn_random(i
->final_path
, NULL
, &t
);
174 converted_fd
= open(t
, O_RDWR
|O_CREAT
|O_EXCL
|O_NOCTTY
|O_CLOEXEC
, 0664);
175 if (converted_fd
< 0)
176 return log_error_errno(errno
, "Failed to create %s: %m", t
);
178 r
= chattr_fd(converted_fd
, FS_NOCOW_FL
, FS_NOCOW_FL
, NULL
);
180 log_warning_errno(r
, "Failed to set file attributes on %s: %m", t
);
182 log_info("Unpacking QCOW2 file.");
184 r
= qcow2_convert(i
->output_fd
, converted_fd
);
187 return log_error_errno(r
, "Failed to convert qcow2 image: %m");
190 (void) unlink(i
->temp_path
);
191 free_and_replace(i
->temp_path
, t
);
193 safe_close(i
->output_fd
);
194 i
->output_fd
= TAKE_FD(converted_fd
);
199 static int raw_import_finish(RawImport
*i
) {
203 assert(i
->output_fd
>= 0);
204 assert(i
->temp_path
);
205 assert(i
->final_path
);
207 /* In case this was a sparse file, make sure the file system is right */
208 if (i
->written_uncompressed
> 0) {
209 if (ftruncate(i
->output_fd
, i
->written_uncompressed
) < 0)
210 return log_error_errno(errno
, "Failed to truncate file: %m");
213 r
= raw_import_maybe_convert_qcow2(i
);
217 if (S_ISREG(i
->st
.st_mode
)) {
218 (void) copy_times(i
->input_fd
, i
->output_fd
);
219 (void) copy_xattr(i
->input_fd
, i
->output_fd
);
223 r
= import_make_read_only_fd(i
->output_fd
);
229 (void) rm_rf(i
->final_path
, REMOVE_ROOT
|REMOVE_PHYSICAL
|REMOVE_SUBVOLUME
);
231 r
= rename_noreplace(AT_FDCWD
, i
->temp_path
, AT_FDCWD
, i
->final_path
);
233 return log_error_errno(r
, "Failed to move image into place: %m");
235 i
->temp_path
= mfree(i
->temp_path
);
240 static int raw_import_open_disk(RawImport
*i
) {
245 assert(!i
->final_path
);
246 assert(!i
->temp_path
);
247 assert(i
->output_fd
< 0);
249 i
->final_path
= strjoin(i
->image_root
, "/", i
->local
, ".raw");
253 r
= tempfn_random(i
->final_path
, NULL
, &i
->temp_path
);
257 (void) mkdir_parents_label(i
->temp_path
, 0700);
259 i
->output_fd
= open(i
->temp_path
, O_RDWR
|O_CREAT
|O_EXCL
|O_NOCTTY
|O_CLOEXEC
, 0664);
260 if (i
->output_fd
< 0)
261 return log_error_errno(errno
, "Failed to open destination %s: %m", i
->temp_path
);
263 r
= chattr_fd(i
->output_fd
, FS_NOCOW_FL
, FS_NOCOW_FL
, NULL
);
265 log_warning_errno(r
, "Failed to set file attributes on %s: %m", i
->temp_path
);
270 static int raw_import_try_reflink(RawImport
*i
) {
275 assert(i
->input_fd
>= 0);
276 assert(i
->output_fd
>= 0);
278 if (i
->compress
.type
!= IMPORT_COMPRESS_UNCOMPRESSED
)
281 if (!S_ISREG(i
->st
.st_mode
))
284 p
= lseek(i
->input_fd
, 0, SEEK_CUR
);
286 return log_error_errno(errno
, "Failed to read file offset of input file: %m");
288 /* Let's only try a btrfs reflink, if we are reading from the beginning of the file */
289 if ((uint64_t) p
!= (uint64_t) i
->buffer_size
)
292 r
= btrfs_reflink(i
->input_fd
, i
->output_fd
);
299 static int raw_import_write(const void *p
, size_t sz
, void *userdata
) {
300 RawImport
*i
= userdata
;
303 n
= sparse_write(i
->output_fd
, p
, sz
, 64);
309 i
->written_uncompressed
+= sz
;
314 static int raw_import_process(RawImport
*i
) {
319 assert(i
->buffer_size
< sizeof(i
->buffer
));
321 l
= read(i
->input_fd
, i
->buffer
+ i
->buffer_size
, sizeof(i
->buffer
) - i
->buffer_size
);
326 r
= log_error_errno(errno
, "Failed to read input file: %m");
330 if (i
->compress
.type
== IMPORT_COMPRESS_UNKNOWN
) {
331 log_error("Premature end of file.");
336 r
= raw_import_finish(i
);
342 if (i
->compress
.type
== IMPORT_COMPRESS_UNKNOWN
) {
343 r
= import_uncompress_detect(&i
->compress
, i
->buffer
, i
->buffer_size
);
345 log_error_errno(r
, "Failed to detect file compression: %m");
348 if (r
== 0) /* Need more data */
351 r
= raw_import_open_disk(i
);
355 r
= raw_import_try_reflink(i
);
359 r
= raw_import_finish(i
);
364 r
= import_uncompress(&i
->compress
, i
->buffer
, i
->buffer_size
, raw_import_write
, i
);
366 log_error_errno(r
, "Failed to decode and write: %m");
370 i
->written_compressed
+= i
->buffer_size
;
373 raw_import_report_progress(i
);
379 i
->on_finished(i
, r
, i
->userdata
);
381 sd_event_exit(i
->event
, r
);
386 static int raw_import_on_input(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
387 RawImport
*i
= userdata
;
389 return raw_import_process(i
);
392 static int raw_import_on_defer(sd_event_source
*s
, void *userdata
) {
393 RawImport
*i
= userdata
;
395 return raw_import_process(i
);
398 int raw_import_start(RawImport
*i
, int fd
, const char *local
, bool force_local
, bool read_only
) {
405 if (!machine_name_is_valid(local
))
408 if (i
->input_fd
>= 0)
411 r
= fd_nonblock(fd
, true);
415 r
= free_and_strdup(&i
->local
, local
);
418 i
->force_local
= force_local
;
419 i
->read_only
= read_only
;
421 if (fstat(fd
, &i
->st
) < 0)
424 r
= sd_event_add_io(i
->event
, &i
->input_event_source
, fd
, EPOLLIN
, raw_import_on_input
, i
);
426 /* This fd does not support epoll, for example because it is a regular file. Busy read in that case */
427 r
= sd_event_add_defer(i
->event
, &i
->input_event_source
, raw_import_on_defer
, i
);
431 r
= sd_event_source_set_enabled(i
->input_event_source
, SD_EVENT_ON
);