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