1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
8 #include "alloc-util.h"
9 #include "btrfs-util.h"
14 #include "hostname-util.h"
15 #include "import-common.h"
16 #include "import-compress.h"
17 #include "import-tar.h"
18 #include "install-file.h"
20 #include "machine-pool.h"
21 #include "mkdir-label.h"
22 #include "path-util.h"
23 #include "process-util.h"
24 #include "qcow2-util.h"
25 #include "ratelimit.h"
27 #include "string-util.h"
28 #include "tmpfile-util.h"
35 TarImportFinished 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
;
57 struct stat input_stat
;
61 unsigned last_percent
;
62 RateLimit progress_ratelimit
;
65 TarImport
* tar_import_unref(TarImport
*i
) {
69 sd_event_source_unref(i
->input_event_source
);
72 sigkill_wait(i
->tar_pid
);
74 rm_rf_subvolume_and_free(i
->temp_path
);
76 import_compress_free(&i
->compress
);
78 sd_event_unref(i
->event
);
80 safe_close(i
->tar_fd
);
91 const char *image_root
,
92 TarImportFinished on_finished
,
95 _cleanup_(tar_import_unrefp
) TarImport
*i
= NULL
;
96 _cleanup_free_
char *root
= NULL
;
101 root
= strdup(image_root
?: "/var/lib/machines");
105 i
= new(TarImport
, 1);
112 .on_finished
= on_finished
,
113 .userdata
= userdata
,
114 .last_percent
= UINT_MAX
,
115 .image_root
= TAKE_PTR(root
),
116 .progress_ratelimit
= { 100 * USEC_PER_MSEC
, 1 },
120 i
->event
= sd_event_ref(event
);
122 r
= sd_event_default(&i
->event
);
132 static void tar_import_report_progress(TarImport
*i
) {
136 /* We have no size information, unless the source is a regular file */
137 if (!S_ISREG(i
->input_stat
.st_mode
))
140 if (i
->written_compressed
>= (uint64_t) i
->input_stat
.st_size
)
143 percent
= (unsigned) ((i
->written_compressed
* UINT64_C(100)) / (uint64_t) i
->input_stat
.st_size
);
145 if (percent
== i
->last_percent
)
148 if (!ratelimit_below(&i
->progress_ratelimit
))
151 sd_notifyf(false, "X_IMPORT_PROGRESS=%u", percent
);
152 log_info("Imported %u%%.", percent
);
154 i
->last_percent
= percent
;
157 static int tar_import_finish(TarImport
*i
) {
162 assert(i
->tar_fd
>= 0);
164 i
->tar_fd
= safe_close(i
->tar_fd
);
166 if (i
->tar_pid
> 0) {
167 r
= wait_for_terminate_and_check("tar", TAKE_PID(i
->tar_pid
), WAIT_LOG
);
170 if (r
!= EXIT_SUCCESS
)
174 assert_se(d
= i
->temp_path
?: i
->local
);
176 r
= import_mangle_os_tree(d
);
182 AT_FDCWD
, i
->final_path
,
183 (i
->flags
& IMPORT_FORCE
? INSTALL_REPLACE
: 0) |
184 (i
->flags
& IMPORT_READ_ONLY
? INSTALL_READ_ONLY
: 0) |
185 (i
->flags
& IMPORT_SYNC
? INSTALL_SYNCFS
: 0));
187 return log_error_errno(r
, "Failed to move '%s' into place: %m", i
->final_path
?: i
->local
);
189 i
->temp_path
= mfree(i
->temp_path
);
194 static int tar_import_fork_tar(TarImport
*i
) {
195 const char *d
, *root
;
200 assert(!i
->final_path
);
201 assert(!i
->temp_path
);
202 assert(i
->tar_fd
< 0);
204 if (i
->flags
& IMPORT_DIRECT
) {
208 i
->final_path
= path_join(i
->image_root
, i
->local
);
212 r
= tempfn_random(i
->final_path
, NULL
, &i
->temp_path
);
217 root
= i
->image_root
;
222 (void) mkdir_parents_label(d
, 0700);
224 if (FLAGS_SET(i
->flags
, IMPORT_DIRECT
|IMPORT_FORCE
))
225 (void) rm_rf(d
, REMOVE_ROOT
|REMOVE_PHYSICAL
|REMOVE_SUBVOLUME
);
227 if (i
->flags
& IMPORT_BTRFS_SUBVOL
)
228 r
= btrfs_subvol_make_fallback(d
, 0755);
230 r
= RET_NERRNO(mkdir(d
, 0755));
231 if (r
== -EEXIST
&& (i
->flags
& IMPORT_DIRECT
)) /* EEXIST is OK if in direct mode, but not otherwise,
232 * because in that case our temporary path collided */
235 return log_error_errno(r
, "Failed to create directory/subvolume %s: %m", d
);
236 if (r
> 0 && (i
->flags
& IMPORT_BTRFS_QUOTA
)) { /* actually btrfs subvol */
237 if (!(i
->flags
& IMPORT_DIRECT
))
238 (void) import_assign_pool_quota_and_warn(root
);
239 (void) import_assign_pool_quota_and_warn(d
);
242 i
->tar_fd
= import_fork_tar_x(d
, &i
->tar_pid
);
249 static int tar_import_write(const void *p
, size_t sz
, void *userdata
) {
250 TarImport
*i
= userdata
;
253 r
= loop_write(i
->tar_fd
, p
, sz
, false);
257 i
->written_uncompressed
+= sz
;
262 static int tar_import_process(TarImport
*i
) {
267 assert(i
->buffer_size
< sizeof(i
->buffer
));
269 l
= read(i
->input_fd
, i
->buffer
+ i
->buffer_size
, sizeof(i
->buffer
) - i
->buffer_size
);
274 r
= log_error_errno(errno
, "Failed to read input file: %m");
280 if (i
->compress
.type
== IMPORT_COMPRESS_UNKNOWN
) {
282 if (l
== 0) { /* EOF */
283 log_debug("File too short to be compressed, as no compression signature fits in, thus assuming uncompressed.");
284 import_uncompress_force_off(&i
->compress
);
286 r
= import_uncompress_detect(&i
->compress
, i
->buffer
, i
->buffer_size
);
288 log_error_errno(r
, "Failed to detect file compression: %m");
291 if (r
== 0) /* Need more data */
295 r
= tar_import_fork_tar(i
);
300 r
= import_uncompress(&i
->compress
, i
->buffer
, i
->buffer_size
, tar_import_write
, i
);
302 log_error_errno(r
, "Failed to decode and write: %m");
306 i
->written_compressed
+= i
->buffer_size
;
309 if (l
== 0) { /* EOF */
310 r
= tar_import_finish(i
);
314 tar_import_report_progress(i
);
320 i
->on_finished(i
, r
, i
->userdata
);
322 sd_event_exit(i
->event
, r
);
327 static int tar_import_on_input(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
328 TarImport
*i
= userdata
;
330 return tar_import_process(i
);
333 static int tar_import_on_defer(sd_event_source
*s
, void *userdata
) {
334 TarImport
*i
= userdata
;
336 return tar_import_process(i
);
339 int tar_import_start(TarImport
*i
, int fd
, const char *local
, ImportFlags flags
) {
345 assert(!(flags
& ~IMPORT_FLAGS_MASK_TAR
));
347 if (!import_validate_local(local
, flags
))
350 if (i
->input_fd
>= 0)
353 r
= fd_nonblock(fd
, true);
357 r
= free_and_strdup(&i
->local
, local
);
363 if (fstat(fd
, &i
->input_stat
) < 0)
366 r
= sd_event_add_io(i
->event
, &i
->input_event_source
, fd
, EPOLLIN
, tar_import_on_input
, i
);
368 /* This fd does not support epoll, for example because it is a regular file. Busy read in that case */
369 r
= sd_event_add_defer(i
->event
, &i
->input_event_source
, tar_import_on_defer
, i
);
373 r
= sd_event_source_set_enabled(i
->input_event_source
, SD_EVENT_ON
);