1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2015 Lennart Poettering
11 #include "alloc-util.h"
12 #include "btrfs-util.h"
13 #include "chattr-util.h"
18 #include "hostname-util.h"
19 #include "import-common.h"
20 #include "import-compress.h"
21 #include "import-raw.h"
23 #include "machine-pool.h"
25 #include "path-util.h"
26 #include "qcow2-util.h"
27 #include "ratelimit.h"
29 #include "string-util.h"
37 RawImportFinished on_finished
;
43 bool grow_machine_directory
;
51 ImportCompress compress
;
53 uint64_t written_since_last_grow
;
55 sd_event_source
*input_event_source
;
57 uint8_t buffer
[16*1024];
60 uint64_t written_compressed
;
61 uint64_t written_uncompressed
;
65 unsigned last_percent
;
66 RateLimit progress_rate_limit
;
69 RawImport
* raw_import_unref(RawImport
*i
) {
73 sd_event_unref(i
->event
);
76 (void) unlink(i
->temp_path
);
80 import_compress_free(&i
->compress
);
82 sd_event_source_unref(i
->input_event_source
);
84 safe_close(i
->output_fd
);
95 const char *image_root
,
96 RawImportFinished on_finished
,
99 _cleanup_(raw_import_unrefp
) RawImport
*i
= NULL
;
104 i
= new0(RawImport
, 1);
108 i
->input_fd
= i
->output_fd
= -1;
109 i
->on_finished
= on_finished
;
110 i
->userdata
= userdata
;
112 RATELIMIT_INIT(i
->progress_rate_limit
, 100 * USEC_PER_MSEC
, 1);
113 i
->last_percent
= (unsigned) -1;
115 i
->image_root
= strdup(image_root
?: "/var/lib/machines");
119 i
->grow_machine_directory
= path_startswith(i
->image_root
, "/var/lib/machines");
122 i
->event
= sd_event_ref(event
);
124 r
= sd_event_default(&i
->event
);
134 static void raw_import_report_progress(RawImport
*i
) {
138 /* We have no size information, unless the source is a regular file */
139 if (!S_ISREG(i
->st
.st_mode
))
142 if (i
->written_compressed
>= (uint64_t) i
->st
.st_size
)
145 percent
= (unsigned) ((i
->written_compressed
* UINT64_C(100)) / (uint64_t) i
->st
.st_size
);
147 if (percent
== i
->last_percent
)
150 if (!ratelimit_below(&i
->progress_rate_limit
))
153 sd_notifyf(false, "X_IMPORT_PROGRESS=%u", percent
);
154 log_info("Imported %u%%.", percent
);
156 i
->last_percent
= percent
;
159 static int raw_import_maybe_convert_qcow2(RawImport
*i
) {
160 _cleanup_close_
int converted_fd
= -1;
161 _cleanup_free_
char *t
= NULL
;
166 r
= qcow2_detect(i
->output_fd
);
168 return log_error_errno(r
, "Failed to detect whether this is a QCOW2 image: %m");
172 /* This is a QCOW2 image, let's convert it */
173 r
= tempfn_random(i
->final_path
, NULL
, &t
);
177 converted_fd
= open(t
, O_RDWR
|O_CREAT
|O_EXCL
|O_NOCTTY
|O_CLOEXEC
, 0664);
178 if (converted_fd
< 0)
179 return log_error_errno(errno
, "Failed to create %s: %m", t
);
181 r
= chattr_fd(converted_fd
, FS_NOCOW_FL
, FS_NOCOW_FL
);
183 log_warning_errno(r
, "Failed to set file attributes on %s: %m", t
);
185 log_info("Unpacking QCOW2 file.");
187 r
= qcow2_convert(i
->output_fd
, converted_fd
);
190 return log_error_errno(r
, "Failed to convert qcow2 image: %m");
193 (void) unlink(i
->temp_path
);
194 free_and_replace(i
->temp_path
, t
);
196 safe_close(i
->output_fd
);
197 i
->output_fd
= TAKE_FD(converted_fd
);
202 static int raw_import_finish(RawImport
*i
) {
206 assert(i
->output_fd
>= 0);
207 assert(i
->temp_path
);
208 assert(i
->final_path
);
210 /* In case this was a sparse file, make sure the file system is right */
211 if (i
->written_uncompressed
> 0) {
212 if (ftruncate(i
->output_fd
, i
->written_uncompressed
) < 0)
213 return log_error_errno(errno
, "Failed to truncate file: %m");
216 r
= raw_import_maybe_convert_qcow2(i
);
220 if (S_ISREG(i
->st
.st_mode
)) {
221 (void) copy_times(i
->input_fd
, i
->output_fd
);
222 (void) copy_xattr(i
->input_fd
, i
->output_fd
);
226 r
= import_make_read_only_fd(i
->output_fd
);
232 (void) rm_rf(i
->final_path
, REMOVE_ROOT
|REMOVE_PHYSICAL
|REMOVE_SUBVOLUME
);
234 r
= rename_noreplace(AT_FDCWD
, i
->temp_path
, AT_FDCWD
, i
->final_path
);
236 return log_error_errno(r
, "Failed to move image into place: %m");
238 i
->temp_path
= mfree(i
->temp_path
);
243 static int raw_import_open_disk(RawImport
*i
) {
248 assert(!i
->final_path
);
249 assert(!i
->temp_path
);
250 assert(i
->output_fd
< 0);
252 i
->final_path
= strjoin(i
->image_root
, "/", i
->local
, ".raw");
256 r
= tempfn_random(i
->final_path
, NULL
, &i
->temp_path
);
260 (void) mkdir_parents_label(i
->temp_path
, 0700);
262 i
->output_fd
= open(i
->temp_path
, O_RDWR
|O_CREAT
|O_EXCL
|O_NOCTTY
|O_CLOEXEC
, 0664);
263 if (i
->output_fd
< 0)
264 return log_error_errno(errno
, "Failed to open destination %s: %m", i
->temp_path
);
266 r
= chattr_fd(i
->output_fd
, FS_NOCOW_FL
, FS_NOCOW_FL
);
268 log_warning_errno(r
, "Failed to set file attributes on %s: %m", i
->temp_path
);
273 static int raw_import_try_reflink(RawImport
*i
) {
278 assert(i
->input_fd
>= 0);
279 assert(i
->output_fd
>= 0);
281 if (i
->compress
.type
!= IMPORT_COMPRESS_UNCOMPRESSED
)
284 if (!S_ISREG(i
->st
.st_mode
))
287 p
= lseek(i
->input_fd
, 0, SEEK_CUR
);
289 return log_error_errno(errno
, "Failed to read file offset of input file: %m");
291 /* Let's only try a btrfs reflink, if we are reading from the beginning of the file */
292 if ((uint64_t) p
!= (uint64_t) i
->buffer_size
)
295 r
= btrfs_reflink(i
->input_fd
, i
->output_fd
);
302 static int raw_import_write(const void *p
, size_t sz
, void *userdata
) {
303 RawImport
*i
= userdata
;
306 if (i
->grow_machine_directory
&& i
->written_since_last_grow
>= GROW_INTERVAL_BYTES
) {
307 i
->written_since_last_grow
= 0;
308 grow_machine_directory();
311 n
= sparse_write(i
->output_fd
, p
, sz
, 64);
317 i
->written_uncompressed
+= sz
;
318 i
->written_since_last_grow
+= sz
;
323 static int raw_import_process(RawImport
*i
) {
328 assert(i
->buffer_size
< sizeof(i
->buffer
));
330 l
= read(i
->input_fd
, i
->buffer
+ i
->buffer_size
, sizeof(i
->buffer
) - i
->buffer_size
);
335 r
= log_error_errno(errno
, "Failed to read input file: %m");
339 if (i
->compress
.type
== IMPORT_COMPRESS_UNKNOWN
) {
340 log_error("Premature end of file.");
345 r
= raw_import_finish(i
);
351 if (i
->compress
.type
== IMPORT_COMPRESS_UNKNOWN
) {
352 r
= import_uncompress_detect(&i
->compress
, i
->buffer
, i
->buffer_size
);
354 log_error_errno(r
, "Failed to detect file compression: %m");
357 if (r
== 0) /* Need more data */
360 r
= raw_import_open_disk(i
);
364 r
= raw_import_try_reflink(i
);
368 r
= raw_import_finish(i
);
373 r
= import_uncompress(&i
->compress
, i
->buffer
, i
->buffer_size
, raw_import_write
, i
);
375 log_error_errno(r
, "Failed to decode and write: %m");
379 i
->written_compressed
+= i
->buffer_size
;
382 raw_import_report_progress(i
);
388 i
->on_finished(i
, r
, i
->userdata
);
390 sd_event_exit(i
->event
, r
);
395 static int raw_import_on_input(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
396 RawImport
*i
= userdata
;
398 return raw_import_process(i
);
401 static int raw_import_on_defer(sd_event_source
*s
, void *userdata
) {
402 RawImport
*i
= userdata
;
404 return raw_import_process(i
);
407 int raw_import_start(RawImport
*i
, int fd
, const char *local
, bool force_local
, bool read_only
) {
414 if (!machine_name_is_valid(local
))
417 if (i
->input_fd
>= 0)
420 r
= fd_nonblock(fd
, true);
424 r
= free_and_strdup(&i
->local
, local
);
427 i
->force_local
= force_local
;
428 i
->read_only
= read_only
;
430 if (fstat(fd
, &i
->st
) < 0)
433 r
= sd_event_add_io(i
->event
, &i
->input_event_source
, fd
, EPOLLIN
, raw_import_on_input
, i
);
435 /* This fd does not support epoll, for example because it is a regular file. Busy read in that case */
436 r
= sd_event_add_defer(i
->event
, &i
->input_event_source
, raw_import_on_defer
, i
);
440 r
= sd_event_source_set_enabled(i
->input_event_source
, SD_EVENT_ON
);