1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2015 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
24 #include "sd-daemon.h"
27 #include "path-util.h"
28 #include "btrfs-util.h"
32 #include "ratelimit.h"
33 #include "machine-pool.h"
34 #include "qcow2-util.h"
35 #include "import-compress.h"
36 #include "import-common.h"
37 #include "import-raw.h"
44 RawImportFinished on_finished
;
50 bool grow_machine_directory
;
58 ImportCompress compress
;
60 uint64_t written_since_last_grow
;
62 sd_event_source
*input_event_source
;
64 uint8_t buffer
[16*1024];
67 uint64_t written_compressed
;
68 uint64_t written_uncompressed
;
72 unsigned last_percent
;
73 RateLimit progress_rate_limit
;
76 RawImport
* raw_import_unref(RawImport
*i
) {
80 sd_event_unref(i
->event
);
83 (void) unlink(i
->temp_path
);
87 import_compress_free(&i
->compress
);
89 sd_event_source_unref(i
->input_event_source
);
91 safe_close(i
->output_fd
);
104 const char *image_root
,
105 RawImportFinished on_finished
,
108 _cleanup_(raw_import_unrefp
) RawImport
*i
= NULL
;
113 i
= new0(RawImport
, 1);
117 i
->input_fd
= i
->output_fd
= -1;
118 i
->on_finished
= on_finished
;
119 i
->userdata
= userdata
;
121 RATELIMIT_INIT(i
->progress_rate_limit
, 100 * USEC_PER_MSEC
, 1);
122 i
->last_percent
= (unsigned) -1;
124 i
->image_root
= strdup(image_root
?: "/var/lib/machines");
128 i
->grow_machine_directory
= path_startswith(i
->image_root
, "/var/lib/machines");
131 i
->event
= sd_event_ref(event
);
133 r
= sd_event_default(&i
->event
);
144 static void raw_import_report_progress(RawImport
*i
) {
148 /* We have no size information, unless the source is a regular file */
149 if (!S_ISREG(i
->st
.st_mode
))
152 if (i
->written_compressed
>= (uint64_t) i
->st
.st_size
)
155 percent
= (unsigned) ((i
->written_compressed
* UINT64_C(100)) / (uint64_t) i
->st
.st_size
);
157 if (percent
== i
->last_percent
)
160 if (!ratelimit_test(&i
->progress_rate_limit
))
163 sd_notifyf(false, "X_IMPORT_PROGRESS=%u", percent
);
164 log_info("Imported %u%%.", percent
);
166 i
->last_percent
= percent
;
169 static int raw_import_maybe_convert_qcow2(RawImport
*i
) {
170 _cleanup_close_
int converted_fd
= -1;
171 _cleanup_free_
char *t
= NULL
;
176 r
= qcow2_detect(i
->output_fd
);
178 return log_error_errno(r
, "Failed to detect whether this is a QCOW2 image: %m");
182 /* This is a QCOW2 image, let's convert it */
183 r
= tempfn_random(i
->final_path
, &t
);
187 converted_fd
= open(t
, O_RDWR
|O_CREAT
|O_EXCL
|O_NOCTTY
|O_CLOEXEC
, 0664);
188 if (converted_fd
< 0)
189 return log_error_errno(errno
, "Failed to create %s: %m", t
);
191 r
= chattr_fd(converted_fd
, true, FS_NOCOW_FL
);
193 log_warning_errno(errno
, "Failed to set file attributes on %s: %m", t
);
195 log_info("Unpacking QCOW2 file.");
197 r
= qcow2_convert(i
->output_fd
, converted_fd
);
200 return log_error_errno(r
, "Failed to convert qcow2 image: %m");
203 (void) unlink(i
->temp_path
);
208 safe_close(i
->output_fd
);
209 i
->output_fd
= converted_fd
;
215 static int raw_import_finish(RawImport
*i
) {
219 assert(i
->output_fd
>= 0);
220 assert(i
->temp_path
);
221 assert(i
->final_path
);
223 /* In case this was a sparse file, make sure the file system is right */
224 if (i
->written_uncompressed
> 0) {
225 if (ftruncate(i
->output_fd
, i
->written_uncompressed
) < 0)
226 return log_error_errno(errno
, "Failed to truncate file: %m");
229 r
= raw_import_maybe_convert_qcow2(i
);
233 if (S_ISREG(i
->st
.st_mode
)) {
234 (void) copy_times(i
->input_fd
, i
->output_fd
);
235 (void) copy_xattr(i
->input_fd
, i
->output_fd
);
239 r
= import_make_read_only_fd(i
->output_fd
);
244 if (i
->force_local
) {
245 (void) btrfs_subvol_remove(i
->final_path
);
246 (void) rm_rf(i
->final_path
, REMOVE_ROOT
|REMOVE_PHYSICAL
);
249 r
= rename_noreplace(AT_FDCWD
, i
->temp_path
, AT_FDCWD
, i
->final_path
);
251 return log_error_errno(r
, "Failed to move image into place: %m");
259 static int raw_import_open_disk(RawImport
*i
) {
264 assert(!i
->final_path
);
265 assert(!i
->temp_path
);
266 assert(i
->output_fd
< 0);
268 i
->final_path
= strjoin(i
->image_root
, "/", i
->local
, ".raw", NULL
);
272 r
= tempfn_random(i
->final_path
, &i
->temp_path
);
276 (void) mkdir_parents_label(i
->temp_path
, 0700);
278 i
->output_fd
= open(i
->temp_path
, O_RDWR
|O_CREAT
|O_EXCL
|O_NOCTTY
|O_CLOEXEC
, 0664);
279 if (i
->output_fd
< 0)
280 return log_error_errno(errno
, "Failed to open destination %s: %m", i
->temp_path
);
282 r
= chattr_fd(i
->output_fd
, true, FS_NOCOW_FL
);
284 log_warning_errno(errno
, "Failed to set file attributes on %s: %m", i
->temp_path
);
289 static int raw_import_try_reflink(RawImport
*i
) {
294 assert(i
->input_fd
>= 0);
295 assert(i
->output_fd
>= 0);
297 if (i
->compress
.type
!= IMPORT_COMPRESS_UNCOMPRESSED
)
300 if (!S_ISREG(i
->st
.st_mode
))
303 p
= lseek(i
->input_fd
, 0, SEEK_CUR
);
305 return log_error_errno(errno
, "Failed to read file offset of input file: %m");
307 /* Let's only try a btrfs reflink, if we are reading from the beginning of the file */
308 if ((uint64_t) p
!= (uint64_t) i
->buffer_size
)
311 r
= btrfs_reflink(i
->input_fd
, i
->output_fd
);
318 static int raw_import_write(const void *p
, size_t sz
, void *userdata
) {
319 RawImport
*i
= userdata
;
322 if (i
->grow_machine_directory
&& i
->written_since_last_grow
>= GROW_INTERVAL_BYTES
) {
323 i
->written_since_last_grow
= 0;
324 grow_machine_directory();
327 n
= sparse_write(i
->output_fd
, p
, sz
, 64);
333 i
->written_uncompressed
+= sz
;
334 i
->written_since_last_grow
+= sz
;
339 static int raw_import_process(RawImport
*i
) {
344 assert(i
->buffer_size
< sizeof(i
->buffer
));
346 l
= read(i
->input_fd
, i
->buffer
+ i
->buffer_size
, sizeof(i
->buffer
) - i
->buffer_size
);
351 r
= log_error_errno(errno
, "Failed to read input file: %m");
355 if (i
->compress
.type
== IMPORT_COMPRESS_UNKNOWN
) {
356 log_error("Premature end of file: %m");
361 r
= raw_import_finish(i
);
367 if (i
->compress
.type
== IMPORT_COMPRESS_UNKNOWN
) {
368 r
= import_uncompress_detect(&i
->compress
, i
->buffer
, i
->buffer_size
);
370 log_error("Failed to detect file compression: %m");
373 if (r
== 0) /* Need more data */
376 r
= raw_import_open_disk(i
);
380 r
= raw_import_try_reflink(i
);
384 r
= raw_import_finish(i
);
389 r
= import_uncompress(&i
->compress
, i
->buffer
, i
->buffer_size
, raw_import_write
, i
);
391 log_error_errno(r
, "Failed to decode and write: %m");
395 i
->written_compressed
+= i
->buffer_size
;
398 raw_import_report_progress(i
);
404 i
->on_finished(i
, r
, i
->userdata
);
406 sd_event_exit(i
->event
, r
);
411 static int raw_import_on_input(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
412 RawImport
*i
= userdata
;
414 return raw_import_process(i
);
417 static int raw_import_on_defer(sd_event_source
*s
, void *userdata
) {
418 RawImport
*i
= userdata
;
420 return raw_import_process(i
);
423 int raw_import_start(RawImport
*i
, int fd
, const char *local
, bool force_local
, bool read_only
) {
430 if (!machine_name_is_valid(local
))
433 if (i
->input_fd
>= 0)
436 r
= fd_nonblock(fd
, true);
440 r
= free_and_strdup(&i
->local
, local
);
443 i
->force_local
= force_local
;
444 i
->read_only
= read_only
;
446 if (fstat(fd
, &i
->st
) < 0)
449 r
= sd_event_add_io(i
->event
, &i
->input_event_source
, fd
, EPOLLIN
, raw_import_on_input
, i
);
451 /* This fd does not support epoll, for example because it is a regular file. Busy read in that case */
452 r
= sd_event_add_defer(i
->event
, &i
->input_event_source
, raw_import_on_defer
, i
);
456 r
= sd_event_source_set_enabled(i
->input_event_source
, SD_EVENT_ON
);