1 /* SPDX-License-Identifier: LGPL-2.1+ */
8 #include "alloc-util.h"
9 #include "btrfs-util.h"
10 #include "chattr-util.h"
15 #include "hostname-util.h"
16 #include "import-common.h"
17 #include "import-compress.h"
18 #include "import-raw.h"
20 #include "machine-pool.h"
22 #include "path-util.h"
23 #include "qcow2-util.h"
24 #include "ratelimit.h"
26 #include "string-util.h"
34 RawImportFinished on_finished
;
40 bool grow_machine_directory
;
48 ImportCompress compress
;
50 uint64_t written_since_last_grow
;
52 sd_event_source
*input_event_source
;
54 uint8_t buffer
[16*1024];
57 uint64_t written_compressed
;
58 uint64_t written_uncompressed
;
62 unsigned last_percent
;
63 RateLimit progress_rate_limit
;
66 RawImport
* raw_import_unref(RawImport
*i
) {
70 sd_event_unref(i
->event
);
73 (void) unlink(i
->temp_path
);
77 import_compress_free(&i
->compress
);
79 sd_event_source_unref(i
->input_event_source
);
81 safe_close(i
->output_fd
);
92 const char *image_root
,
93 RawImportFinished on_finished
,
96 _cleanup_(raw_import_unrefp
) RawImport
*i
= NULL
;
101 i
= new0(RawImport
, 1);
105 i
->input_fd
= i
->output_fd
= -1;
106 i
->on_finished
= on_finished
;
107 i
->userdata
= userdata
;
109 RATELIMIT_INIT(i
->progress_rate_limit
, 100 * USEC_PER_MSEC
, 1);
110 i
->last_percent
= (unsigned) -1;
112 i
->image_root
= strdup(image_root
?: "/var/lib/machines");
116 i
->grow_machine_directory
= path_startswith(i
->image_root
, "/var/lib/machines");
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 if (i
->grow_machine_directory
&& i
->written_since_last_grow
>= GROW_INTERVAL_BYTES
) {
304 i
->written_since_last_grow
= 0;
305 grow_machine_directory();
308 n
= sparse_write(i
->output_fd
, p
, sz
, 64);
314 i
->written_uncompressed
+= sz
;
315 i
->written_since_last_grow
+= sz
;
320 static int raw_import_process(RawImport
*i
) {
325 assert(i
->buffer_size
< sizeof(i
->buffer
));
327 l
= read(i
->input_fd
, i
->buffer
+ i
->buffer_size
, sizeof(i
->buffer
) - i
->buffer_size
);
332 r
= log_error_errno(errno
, "Failed to read input file: %m");
336 if (i
->compress
.type
== IMPORT_COMPRESS_UNKNOWN
) {
337 log_error("Premature end of file.");
342 r
= raw_import_finish(i
);
348 if (i
->compress
.type
== IMPORT_COMPRESS_UNKNOWN
) {
349 r
= import_uncompress_detect(&i
->compress
, i
->buffer
, i
->buffer_size
);
351 log_error_errno(r
, "Failed to detect file compression: %m");
354 if (r
== 0) /* Need more data */
357 r
= raw_import_open_disk(i
);
361 r
= raw_import_try_reflink(i
);
365 r
= raw_import_finish(i
);
370 r
= import_uncompress(&i
->compress
, i
->buffer
, i
->buffer_size
, raw_import_write
, i
);
372 log_error_errno(r
, "Failed to decode and write: %m");
376 i
->written_compressed
+= i
->buffer_size
;
379 raw_import_report_progress(i
);
385 i
->on_finished(i
, r
, i
->userdata
);
387 sd_event_exit(i
->event
, r
);
392 static int raw_import_on_input(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
393 RawImport
*i
= userdata
;
395 return raw_import_process(i
);
398 static int raw_import_on_defer(sd_event_source
*s
, void *userdata
) {
399 RawImport
*i
= userdata
;
401 return raw_import_process(i
);
404 int raw_import_start(RawImport
*i
, int fd
, const char *local
, bool force_local
, bool read_only
) {
411 if (!machine_name_is_valid(local
))
414 if (i
->input_fd
>= 0)
417 r
= fd_nonblock(fd
, true);
421 r
= free_and_strdup(&i
->local
, local
);
424 i
->force_local
= force_local
;
425 i
->read_only
= read_only
;
427 if (fstat(fd
, &i
->st
) < 0)
430 r
= sd_event_add_io(i
->event
, &i
->input_event_source
, fd
, EPOLLIN
, raw_import_on_input
, i
);
432 /* This fd does not support epoll, for example because it is a regular file. Busy read in that case */
433 r
= sd_event_add_defer(i
->event
, &i
->input_event_source
, raw_import_on_defer
, i
);
437 r
= sd_event_source_set_enabled(i
->input_event_source
, SD_EVENT_ON
);