]>
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" |
c004493c | 18 | #include "io-util.h" |
b6e676ce | 19 | #include "machine-pool.h" |
07630cea LP |
20 | #include "mkdir.h" |
21 | #include "path-util.h" | |
22 | #include "process-util.h" | |
b6e676ce | 23 | #include "qcow2-util.h" |
07630cea LP |
24 | #include "ratelimit.h" |
25 | #include "rm-rf.h" | |
26 | #include "string-util.h" | |
e4de7287 | 27 | #include "tmpfile-util.h" |
07630cea | 28 | #include "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 | ||
57 | struct stat st; | |
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 LP |
70 | |
71 | if (i->tar_pid > 1) { | |
72 | (void) kill_and_sigcont(i->tar_pid, SIGKILL); | |
73 | (void) wait_for_terminate(i->tar_pid, NULL); | |
74 | } | |
75 | ||
0dfb6503 | 76 | rm_rf_subvolume_and_free(i->temp_path); |
b6e676ce LP |
77 | |
78 | import_compress_free(&i->compress); | |
79 | ||
587fec42 | 80 | sd_event_unref(i->event); |
b6e676ce LP |
81 | |
82 | safe_close(i->tar_fd); | |
83 | ||
84 | free(i->final_path); | |
85 | free(i->image_root); | |
86 | free(i->local); | |
6b430fdb | 87 | return mfree(i); |
b6e676ce LP |
88 | } |
89 | ||
90 | int tar_import_new( | |
91 | TarImport **ret, | |
92 | sd_event *event, | |
93 | const char *image_root, | |
94 | TarImportFinished on_finished, | |
95 | void *userdata) { | |
96 | ||
97 | _cleanup_(tar_import_unrefp) TarImport *i = NULL; | |
0d94088e | 98 | _cleanup_free_ char *root = NULL; |
b6e676ce LP |
99 | int r; |
100 | ||
101 | assert(ret); | |
102 | ||
0d94088e YW |
103 | root = strdup(image_root ?: "/var/lib/machines"); |
104 | if (!root) | |
b6e676ce LP |
105 | return -ENOMEM; |
106 | ||
0d94088e YW |
107 | i = new(TarImport, 1); |
108 | if (!i) | |
b6e676ce LP |
109 | return -ENOMEM; |
110 | ||
0d94088e YW |
111 | *i = (TarImport) { |
112 | .input_fd = -1, | |
113 | .tar_fd = -1, | |
114 | .on_finished = on_finished, | |
115 | .userdata = userdata, | |
116 | .last_percent = (unsigned) -1, | |
117 | .image_root = TAKE_PTR(root), | |
5ac1530e | 118 | .progress_ratelimit = { 100 * USEC_PER_MSEC, 1 }, |
0d94088e YW |
119 | }; |
120 | ||
b6e676ce LP |
121 | if (event) |
122 | i->event = sd_event_ref(event); | |
123 | else { | |
124 | r = sd_event_default(&i->event); | |
125 | if (r < 0) | |
126 | return r; | |
127 | } | |
128 | ||
1cc6c93a | 129 | *ret = TAKE_PTR(i); |
b6e676ce LP |
130 | |
131 | return 0; | |
132 | } | |
133 | ||
134 | static void tar_import_report_progress(TarImport *i) { | |
135 | unsigned percent; | |
136 | assert(i); | |
137 | ||
138 | /* We have no size information, unless the source is a regular file */ | |
139 | if (!S_ISREG(i->st.st_mode)) | |
140 | return; | |
141 | ||
142 | if (i->written_compressed >= (uint64_t) i->st.st_size) | |
143 | percent = 100; | |
144 | else | |
145 | percent = (unsigned) ((i->written_compressed * UINT64_C(100)) / (uint64_t) i->st.st_size); | |
146 | ||
147 | if (percent == i->last_percent) | |
148 | return; | |
149 | ||
5ac1530e | 150 | if (!ratelimit_below(&i->progress_ratelimit)) |
b6e676ce LP |
151 | return; |
152 | ||
153 | sd_notifyf(false, "X_IMPORT_PROGRESS=%u", percent); | |
154 | log_info("Imported %u%%.", percent); | |
155 | ||
156 | i->last_percent = percent; | |
157 | } | |
158 | ||
159 | static int tar_import_finish(TarImport *i) { | |
160 | int r; | |
161 | ||
162 | assert(i); | |
163 | assert(i->tar_fd >= 0); | |
164 | assert(i->temp_path); | |
165 | assert(i->final_path); | |
166 | ||
167 | i->tar_fd = safe_close(i->tar_fd); | |
168 | ||
169 | if (i->tar_pid > 0) { | |
7d4904fe | 170 | r = wait_for_terminate_and_check("tar", i->tar_pid, WAIT_LOG); |
b6e676ce LP |
171 | i->tar_pid = 0; |
172 | if (r < 0) | |
173 | return r; | |
d02bfa50 LP |
174 | if (r != EXIT_SUCCESS) |
175 | return -EPROTO; | |
b6e676ce LP |
176 | } |
177 | ||
e21b7229 LP |
178 | r = import_mangle_os_tree(i->temp_path); |
179 | if (r < 0) | |
180 | return r; | |
181 | ||
5183c50a | 182 | if (i->flags & IMPORT_READ_ONLY) { |
b6e676ce LP |
183 | r = import_make_read_only(i->temp_path); |
184 | if (r < 0) | |
185 | return r; | |
186 | } | |
187 | ||
5183c50a | 188 | if (i->flags & IMPORT_FORCE) |
d9e2daaf | 189 | (void) rm_rf(i->final_path, REMOVE_ROOT|REMOVE_PHYSICAL|REMOVE_SUBVOLUME); |
b6e676ce | 190 | |
f85ef957 AC |
191 | r = rename_noreplace(AT_FDCWD, i->temp_path, AT_FDCWD, i->final_path); |
192 | if (r < 0) | |
193 | return log_error_errno(r, "Failed to move image into place: %m"); | |
b6e676ce | 194 | |
a1e58e8e | 195 | i->temp_path = mfree(i->temp_path); |
b6e676ce LP |
196 | |
197 | return 0; | |
198 | } | |
199 | ||
200 | static int tar_import_fork_tar(TarImport *i) { | |
201 | int r; | |
202 | ||
203 | assert(i); | |
204 | ||
205 | assert(!i->final_path); | |
206 | assert(!i->temp_path); | |
207 | assert(i->tar_fd < 0); | |
208 | ||
657ee2d8 | 209 | i->final_path = path_join(i->image_root, i->local); |
b6e676ce LP |
210 | if (!i->final_path) |
211 | return log_oom(); | |
212 | ||
14bcf25c | 213 | r = tempfn_random(i->final_path, NULL, &i->temp_path); |
b6e676ce LP |
214 | if (r < 0) |
215 | return log_oom(); | |
216 | ||
217 | (void) mkdir_parents_label(i->temp_path, 0700); | |
218 | ||
82c4440d LP |
219 | r = btrfs_subvol_make_fallback(i->temp_path, 0755); |
220 | if (r < 0) | |
221 | return log_error_errno(r, "Failed to create directory/subvolume %s: %m", i->temp_path); | |
052ba0eb LP |
222 | if (r > 0) { /* actually btrfs subvol */ |
223 | (void) import_assign_pool_quota_and_warn(i->image_root); | |
8c9cfc28 | 224 | (void) import_assign_pool_quota_and_warn(i->temp_path); |
052ba0eb | 225 | } |
b6e676ce | 226 | |
587fec42 | 227 | i->tar_fd = import_fork_tar_x(i->temp_path, &i->tar_pid); |
b6e676ce LP |
228 | if (i->tar_fd < 0) |
229 | return i->tar_fd; | |
230 | ||
231 | return 0; | |
232 | } | |
233 | ||
234 | static int tar_import_write(const void *p, size_t sz, void *userdata) { | |
235 | TarImport *i = userdata; | |
236 | int r; | |
237 | ||
b6e676ce LP |
238 | r = loop_write(i->tar_fd, p, sz, false); |
239 | if (r < 0) | |
240 | return r; | |
241 | ||
242 | i->written_uncompressed += sz; | |
b6e676ce LP |
243 | |
244 | return 0; | |
245 | } | |
246 | ||
247 | static int tar_import_process(TarImport *i) { | |
248 | ssize_t l; | |
249 | int r; | |
250 | ||
251 | assert(i); | |
252 | assert(i->buffer_size < sizeof(i->buffer)); | |
253 | ||
254 | l = read(i->input_fd, i->buffer + i->buffer_size, sizeof(i->buffer) - i->buffer_size); | |
255 | if (l < 0) { | |
587fec42 LP |
256 | if (errno == EAGAIN) |
257 | return 0; | |
258 | ||
b6e676ce LP |
259 | r = log_error_errno(errno, "Failed to read input file: %m"); |
260 | goto finish; | |
261 | } | |
b6e676ce LP |
262 | |
263 | i->buffer_size += l; | |
264 | ||
265 | if (i->compress.type == IMPORT_COMPRESS_UNKNOWN) { | |
c9b6ebef LP |
266 | |
267 | if (l == 0) { /* EOF */ | |
268 | log_debug("File too short to be compressed, as no compression signature fits in, thus assuming uncompressed."); | |
269 | import_uncompress_force_off(&i->compress); | |
270 | } else { | |
271 | r = import_uncompress_detect(&i->compress, i->buffer, i->buffer_size); | |
272 | if (r < 0) { | |
273 | log_error_errno(r, "Failed to detect file compression: %m"); | |
274 | goto finish; | |
275 | } | |
276 | if (r == 0) /* Need more data */ | |
277 | return 0; | |
b6e676ce | 278 | } |
b6e676ce LP |
279 | |
280 | r = tar_import_fork_tar(i); | |
281 | if (r < 0) | |
282 | goto finish; | |
283 | } | |
284 | ||
285 | r = import_uncompress(&i->compress, i->buffer, i->buffer_size, tar_import_write, i); | |
286 | if (r < 0) { | |
287 | log_error_errno(r, "Failed to decode and write: %m"); | |
288 | goto finish; | |
289 | } | |
290 | ||
291 | i->written_compressed += i->buffer_size; | |
292 | i->buffer_size = 0; | |
293 | ||
c9b6ebef LP |
294 | if (l == 0) { /* EOF */ |
295 | r = tar_import_finish(i); | |
296 | goto finish; | |
297 | } | |
298 | ||
b6e676ce LP |
299 | tar_import_report_progress(i); |
300 | ||
301 | return 0; | |
302 | ||
303 | finish: | |
304 | if (i->on_finished) | |
305 | i->on_finished(i, r, i->userdata); | |
306 | else | |
307 | sd_event_exit(i->event, r); | |
308 | ||
309 | return 0; | |
310 | } | |
311 | ||
312 | static int tar_import_on_input(sd_event_source *s, int fd, uint32_t revents, void *userdata) { | |
313 | TarImport *i = userdata; | |
314 | ||
315 | return tar_import_process(i); | |
316 | } | |
317 | ||
318 | static int tar_import_on_defer(sd_event_source *s, void *userdata) { | |
319 | TarImport *i = userdata; | |
320 | ||
321 | return tar_import_process(i); | |
322 | } | |
323 | ||
5183c50a | 324 | int tar_import_start(TarImport *i, int fd, const char *local, ImportFlags flags) { |
b6e676ce LP |
325 | int r; |
326 | ||
327 | assert(i); | |
328 | assert(fd >= 0); | |
329 | assert(local); | |
5183c50a | 330 | assert(!(flags & ~IMPORT_FLAGS_MASK)); |
b6e676ce | 331 | |
52ef5dd7 | 332 | if (!hostname_is_valid(local, 0)) |
b6e676ce LP |
333 | return -EINVAL; |
334 | ||
335 | if (i->input_fd >= 0) | |
336 | return -EBUSY; | |
337 | ||
587fec42 LP |
338 | r = fd_nonblock(fd, true); |
339 | if (r < 0) | |
340 | return r; | |
341 | ||
b6e676ce LP |
342 | r = free_and_strdup(&i->local, local); |
343 | if (r < 0) | |
344 | return r; | |
5183c50a LP |
345 | |
346 | i->flags = flags; | |
b6e676ce LP |
347 | |
348 | if (fstat(fd, &i->st) < 0) | |
349 | return -errno; | |
350 | ||
351 | r = sd_event_add_io(i->event, &i->input_event_source, fd, EPOLLIN, tar_import_on_input, i); | |
352 | if (r == -EPERM) { | |
353 | /* This fd does not support epoll, for example because it is a regular file. Busy read in that case */ | |
354 | r = sd_event_add_defer(i->event, &i->input_event_source, tar_import_on_defer, i); | |
355 | if (r < 0) | |
356 | return r; | |
357 | ||
358 | r = sd_event_source_set_enabled(i->input_event_source, SD_EVENT_ON); | |
359 | } | |
360 | if (r < 0) | |
361 | return r; | |
362 | ||
363 | i->input_fd = fd; | |
364 | return r; | |
365 | } |