]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
b6e676ce LP |
2 | |
3 | #include <linux/fs.h> | |
4 | ||
5 | #include "sd-daemon.h" | |
6 | #include "sd-event.h" | |
07630cea | 7 | |
b5efdb8a | 8 | #include "alloc-util.h" |
b6e676ce LP |
9 | #include "btrfs-util.h" |
10 | #include "copy.h" | |
3ffd4af2 | 11 | #include "fd-util.h" |
0d39fa9c | 12 | #include "fileio.h" |
f4f15635 | 13 | #include "fs-util.h" |
07630cea LP |
14 | #include "hostname-util.h" |
15 | #include "import-common.h" | |
16 | #include "import-compress.h" | |
3ffd4af2 | 17 | #include "import-tar.h" |
d32a5841 | 18 | #include "install-file.h" |
c004493c | 19 | #include "io-util.h" |
b6e676ce | 20 | #include "machine-pool.h" |
35cd0ba5 | 21 | #include "mkdir-label.h" |
07630cea LP |
22 | #include "path-util.h" |
23 | #include "process-util.h" | |
b6e676ce | 24 | #include "qcow2-util.h" |
07630cea LP |
25 | #include "ratelimit.h" |
26 | #include "rm-rf.h" | |
27 | #include "string-util.h" | |
e4de7287 | 28 | #include "tmpfile-util.h" |
b6e676ce LP |
29 | |
30 | struct TarImport { | |
31 | sd_event *event; | |
32 | ||
33 | char *image_root; | |
34 | ||
35 | TarImportFinished on_finished; | |
36 | void *userdata; | |
37 | ||
38 | char *local; | |
5183c50a | 39 | ImportFlags flags; |
b6e676ce LP |
40 | |
41 | char *temp_path; | |
42 | char *final_path; | |
43 | ||
44 | int input_fd; | |
45 | int tar_fd; | |
46 | ||
47 | ImportCompress compress; | |
48 | ||
b6e676ce LP |
49 | sd_event_source *input_event_source; |
50 | ||
51 | uint8_t buffer[16*1024]; | |
52 | size_t buffer_size; | |
53 | ||
54 | uint64_t written_compressed; | |
55 | uint64_t written_uncompressed; | |
56 | ||
d32a5841 | 57 | struct stat input_stat; |
b6e676ce LP |
58 | |
59 | pid_t tar_pid; | |
60 | ||
61 | unsigned last_percent; | |
5ac1530e | 62 | RateLimit progress_ratelimit; |
b6e676ce LP |
63 | }; |
64 | ||
65 | TarImport* tar_import_unref(TarImport *i) { | |
66 | if (!i) | |
67 | return NULL; | |
68 | ||
587fec42 | 69 | sd_event_source_unref(i->input_event_source); |
b6e676ce | 70 | |
7950211d LP |
71 | if (i->tar_pid > 1) |
72 | sigkill_wait(i->tar_pid); | |
b6e676ce | 73 | |
0dfb6503 | 74 | rm_rf_subvolume_and_free(i->temp_path); |
b6e676ce LP |
75 | |
76 | import_compress_free(&i->compress); | |
77 | ||
587fec42 | 78 | sd_event_unref(i->event); |
b6e676ce LP |
79 | |
80 | safe_close(i->tar_fd); | |
81 | ||
82 | free(i->final_path); | |
83 | free(i->image_root); | |
84 | free(i->local); | |
6b430fdb | 85 | return mfree(i); |
b6e676ce LP |
86 | } |
87 | ||
88 | int tar_import_new( | |
89 | TarImport **ret, | |
90 | sd_event *event, | |
91 | const char *image_root, | |
92 | TarImportFinished on_finished, | |
93 | void *userdata) { | |
94 | ||
95 | _cleanup_(tar_import_unrefp) TarImport *i = NULL; | |
0d94088e | 96 | _cleanup_free_ char *root = NULL; |
b6e676ce LP |
97 | int r; |
98 | ||
99 | assert(ret); | |
100 | ||
0d94088e YW |
101 | root = strdup(image_root ?: "/var/lib/machines"); |
102 | if (!root) | |
b6e676ce LP |
103 | return -ENOMEM; |
104 | ||
0d94088e YW |
105 | i = new(TarImport, 1); |
106 | if (!i) | |
b6e676ce LP |
107 | return -ENOMEM; |
108 | ||
0d94088e | 109 | *i = (TarImport) { |
254d1313 ZJS |
110 | .input_fd = -EBADF, |
111 | .tar_fd = -EBADF, | |
0d94088e YW |
112 | .on_finished = on_finished, |
113 | .userdata = userdata, | |
f5fbe71d | 114 | .last_percent = UINT_MAX, |
0d94088e | 115 | .image_root = TAKE_PTR(root), |
5ac1530e | 116 | .progress_ratelimit = { 100 * USEC_PER_MSEC, 1 }, |
0d94088e YW |
117 | }; |
118 | ||
b6e676ce LP |
119 | if (event) |
120 | i->event = sd_event_ref(event); | |
121 | else { | |
122 | r = sd_event_default(&i->event); | |
123 | if (r < 0) | |
124 | return r; | |
125 | } | |
126 | ||
1cc6c93a | 127 | *ret = TAKE_PTR(i); |
b6e676ce LP |
128 | |
129 | return 0; | |
130 | } | |
131 | ||
132 | static void tar_import_report_progress(TarImport *i) { | |
133 | unsigned percent; | |
134 | assert(i); | |
135 | ||
136 | /* We have no size information, unless the source is a regular file */ | |
d32a5841 | 137 | if (!S_ISREG(i->input_stat.st_mode)) |
b6e676ce LP |
138 | return; |
139 | ||
d32a5841 | 140 | if (i->written_compressed >= (uint64_t) i->input_stat.st_size) |
b6e676ce LP |
141 | percent = 100; |
142 | else | |
d32a5841 | 143 | percent = (unsigned) ((i->written_compressed * UINT64_C(100)) / (uint64_t) i->input_stat.st_size); |
b6e676ce LP |
144 | |
145 | if (percent == i->last_percent) | |
146 | return; | |
147 | ||
5ac1530e | 148 | if (!ratelimit_below(&i->progress_ratelimit)) |
b6e676ce LP |
149 | return; |
150 | ||
a986de68 | 151 | sd_notifyf(false, "X_IMPORT_PROGRESS=%u%%", percent); |
b6e676ce LP |
152 | log_info("Imported %u%%.", percent); |
153 | ||
154 | i->last_percent = percent; | |
155 | } | |
156 | ||
157 | static int tar_import_finish(TarImport *i) { | |
d32a5841 | 158 | const char *d; |
b6e676ce LP |
159 | int r; |
160 | ||
161 | assert(i); | |
162 | assert(i->tar_fd >= 0); | |
b6e676ce LP |
163 | |
164 | i->tar_fd = safe_close(i->tar_fd); | |
165 | ||
166 | if (i->tar_pid > 0) { | |
8f03de53 | 167 | r = wait_for_terminate_and_check("tar", TAKE_PID(i->tar_pid), WAIT_LOG); |
b6e676ce LP |
168 | if (r < 0) |
169 | return r; | |
d02bfa50 LP |
170 | if (r != EXIT_SUCCESS) |
171 | return -EPROTO; | |
b6e676ce LP |
172 | } |
173 | ||
d32a5841 LP |
174 | assert_se(d = i->temp_path ?: i->local); |
175 | ||
176 | r = import_mangle_os_tree(d); | |
e21b7229 LP |
177 | if (r < 0) |
178 | return r; | |
179 | ||
d32a5841 LP |
180 | r = install_file( |
181 | AT_FDCWD, 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)); | |
f85ef957 | 186 | if (r < 0) |
d32a5841 | 187 | return log_error_errno(r, "Failed to move '%s' into place: %m", i->final_path ?: i->local); |
b6e676ce | 188 | |
a1e58e8e | 189 | i->temp_path = mfree(i->temp_path); |
b6e676ce LP |
190 | |
191 | return 0; | |
192 | } | |
193 | ||
194 | static int tar_import_fork_tar(TarImport *i) { | |
d32a5841 | 195 | const char *d, *root; |
b6e676ce LP |
196 | int r; |
197 | ||
198 | assert(i); | |
d32a5841 | 199 | assert(i->local); |
b6e676ce LP |
200 | assert(!i->final_path); |
201 | assert(!i->temp_path); | |
202 | assert(i->tar_fd < 0); | |
203 | ||
d32a5841 LP |
204 | if (i->flags & IMPORT_DIRECT) { |
205 | d = i->local; | |
206 | root = NULL; | |
207 | } else { | |
208 | i->final_path = path_join(i->image_root, i->local); | |
209 | if (!i->final_path) | |
210 | return log_oom(); | |
b6e676ce | 211 | |
d32a5841 LP |
212 | r = tempfn_random(i->final_path, NULL, &i->temp_path); |
213 | if (r < 0) | |
214 | return log_oom(); | |
215 | ||
216 | d = i->temp_path; | |
217 | root = i->image_root; | |
218 | } | |
219 | ||
220 | assert(d); | |
b6e676ce | 221 | |
d32a5841 | 222 | (void) mkdir_parents_label(d, 0700); |
b6e676ce | 223 | |
d32a5841 LP |
224 | if (FLAGS_SET(i->flags, IMPORT_DIRECT|IMPORT_FORCE)) |
225 | (void) rm_rf(d, REMOVE_ROOT|REMOVE_PHYSICAL|REMOVE_SUBVOLUME); | |
226 | ||
227 | if (i->flags & IMPORT_BTRFS_SUBVOL) | |
e54c79cc | 228 | r = btrfs_subvol_make_fallback(AT_FDCWD, d, 0755); |
d32a5841 | 229 | else |
7c248223 | 230 | r = RET_NERRNO(mkdir(d, 0755)); |
d32a5841 LP |
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 */ | |
233 | r = 0; | |
82c4440d | 234 | if (r < 0) |
d32a5841 LP |
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); | |
052ba0eb | 240 | } |
b6e676ce | 241 | |
d32a5841 | 242 | i->tar_fd = import_fork_tar_x(d, &i->tar_pid); |
b6e676ce LP |
243 | if (i->tar_fd < 0) |
244 | return i->tar_fd; | |
245 | ||
246 | return 0; | |
247 | } | |
248 | ||
249 | static int tar_import_write(const void *p, size_t sz, void *userdata) { | |
250 | TarImport *i = userdata; | |
251 | int r; | |
252 | ||
e22c60a9 | 253 | r = loop_write(i->tar_fd, p, sz); |
b6e676ce LP |
254 | if (r < 0) |
255 | return r; | |
256 | ||
257 | i->written_uncompressed += sz; | |
b6e676ce LP |
258 | |
259 | return 0; | |
260 | } | |
261 | ||
262 | static int tar_import_process(TarImport *i) { | |
263 | ssize_t l; | |
264 | int r; | |
265 | ||
266 | assert(i); | |
267 | assert(i->buffer_size < sizeof(i->buffer)); | |
268 | ||
269 | l = read(i->input_fd, i->buffer + i->buffer_size, sizeof(i->buffer) - i->buffer_size); | |
270 | if (l < 0) { | |
587fec42 LP |
271 | if (errno == EAGAIN) |
272 | return 0; | |
273 | ||
b6e676ce LP |
274 | r = log_error_errno(errno, "Failed to read input file: %m"); |
275 | goto finish; | |
276 | } | |
b6e676ce LP |
277 | |
278 | i->buffer_size += l; | |
279 | ||
280 | if (i->compress.type == IMPORT_COMPRESS_UNKNOWN) { | |
c9b6ebef LP |
281 | |
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); | |
285 | } else { | |
286 | r = import_uncompress_detect(&i->compress, i->buffer, i->buffer_size); | |
287 | if (r < 0) { | |
288 | log_error_errno(r, "Failed to detect file compression: %m"); | |
289 | goto finish; | |
290 | } | |
291 | if (r == 0) /* Need more data */ | |
292 | return 0; | |
b6e676ce | 293 | } |
b6e676ce LP |
294 | |
295 | r = tar_import_fork_tar(i); | |
296 | if (r < 0) | |
297 | goto finish; | |
298 | } | |
299 | ||
300 | r = import_uncompress(&i->compress, i->buffer, i->buffer_size, tar_import_write, i); | |
301 | if (r < 0) { | |
302 | log_error_errno(r, "Failed to decode and write: %m"); | |
303 | goto finish; | |
304 | } | |
305 | ||
306 | i->written_compressed += i->buffer_size; | |
307 | i->buffer_size = 0; | |
308 | ||
c9b6ebef LP |
309 | if (l == 0) { /* EOF */ |
310 | r = tar_import_finish(i); | |
311 | goto finish; | |
312 | } | |
313 | ||
b6e676ce LP |
314 | tar_import_report_progress(i); |
315 | ||
316 | return 0; | |
317 | ||
318 | finish: | |
319 | if (i->on_finished) | |
320 | i->on_finished(i, r, i->userdata); | |
321 | else | |
322 | sd_event_exit(i->event, r); | |
323 | ||
324 | return 0; | |
325 | } | |
326 | ||
327 | static int tar_import_on_input(sd_event_source *s, int fd, uint32_t revents, void *userdata) { | |
328 | TarImport *i = userdata; | |
329 | ||
330 | return tar_import_process(i); | |
331 | } | |
332 | ||
333 | static int tar_import_on_defer(sd_event_source *s, void *userdata) { | |
334 | TarImport *i = userdata; | |
335 | ||
336 | return tar_import_process(i); | |
337 | } | |
338 | ||
5183c50a | 339 | int tar_import_start(TarImport *i, int fd, const char *local, ImportFlags flags) { |
b6e676ce LP |
340 | int r; |
341 | ||
342 | assert(i); | |
343 | assert(fd >= 0); | |
344 | assert(local); | |
d32a5841 | 345 | assert(!(flags & ~IMPORT_FLAGS_MASK_TAR)); |
b6e676ce | 346 | |
d32a5841 | 347 | if (!import_validate_local(local, flags)) |
b6e676ce LP |
348 | return -EINVAL; |
349 | ||
350 | if (i->input_fd >= 0) | |
351 | return -EBUSY; | |
352 | ||
587fec42 LP |
353 | r = fd_nonblock(fd, true); |
354 | if (r < 0) | |
355 | return r; | |
356 | ||
b6e676ce LP |
357 | r = free_and_strdup(&i->local, local); |
358 | if (r < 0) | |
359 | return r; | |
5183c50a LP |
360 | |
361 | i->flags = flags; | |
b6e676ce | 362 | |
d32a5841 | 363 | if (fstat(fd, &i->input_stat) < 0) |
b6e676ce LP |
364 | return -errno; |
365 | ||
366 | r = sd_event_add_io(i->event, &i->input_event_source, fd, EPOLLIN, tar_import_on_input, i); | |
367 | if (r == -EPERM) { | |
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); | |
370 | if (r < 0) | |
371 | return r; | |
372 | ||
373 | r = sd_event_source_set_enabled(i->input_event_source, SD_EVENT_ON); | |
374 | } | |
375 | if (r < 0) | |
376 | return r; | |
377 | ||
378 | i->input_fd = fd; | |
d32a5841 | 379 | return 0; |
b6e676ce | 380 | } |