]>
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" |
f4f15635 | 12 | #include "fs-util.h" |
07630cea LP |
13 | #include "hostname-util.h" |
14 | #include "import-common.h" | |
15 | #include "import-compress.h" | |
3ffd4af2 | 16 | #include "import-raw.h" |
c004493c | 17 | #include "io-util.h" |
b6e676ce | 18 | #include "machine-pool.h" |
07630cea LP |
19 | #include "mkdir.h" |
20 | #include "path-util.h" | |
b6e676ce | 21 | #include "qcow2-util.h" |
07630cea LP |
22 | #include "ratelimit.h" |
23 | #include "rm-rf.h" | |
24 | #include "string-util.h" | |
e4de7287 | 25 | #include "tmpfile-util.h" |
07630cea | 26 | #include "util.h" |
b6e676ce LP |
27 | |
28 | struct RawImport { | |
29 | sd_event *event; | |
30 | ||
31 | char *image_root; | |
32 | ||
33 | RawImportFinished on_finished; | |
34 | void *userdata; | |
35 | ||
36 | char *local; | |
5183c50a | 37 | ImportFlags flags; |
b6e676ce LP |
38 | |
39 | char *temp_path; | |
40 | char *final_path; | |
41 | ||
42 | int input_fd; | |
43 | int output_fd; | |
44 | ||
45 | ImportCompress compress; | |
46 | ||
b6e676ce LP |
47 | sd_event_source *input_event_source; |
48 | ||
49 | uint8_t buffer[16*1024]; | |
50 | size_t buffer_size; | |
51 | ||
52 | uint64_t written_compressed; | |
53 | uint64_t written_uncompressed; | |
54 | ||
55 | struct stat st; | |
56 | ||
57 | unsigned last_percent; | |
5ac1530e | 58 | RateLimit progress_ratelimit; |
b6e676ce LP |
59 | }; |
60 | ||
61 | RawImport* raw_import_unref(RawImport *i) { | |
62 | if (!i) | |
63 | return NULL; | |
64 | ||
65 | sd_event_unref(i->event); | |
66 | ||
0dfb6503 | 67 | unlink_and_free(i->temp_path); |
b6e676ce LP |
68 | |
69 | import_compress_free(&i->compress); | |
70 | ||
71 | sd_event_source_unref(i->input_event_source); | |
72 | ||
73 | safe_close(i->output_fd); | |
74 | ||
75 | free(i->final_path); | |
76 | free(i->image_root); | |
77 | free(i->local); | |
6b430fdb | 78 | return mfree(i); |
b6e676ce LP |
79 | } |
80 | ||
81 | int raw_import_new( | |
82 | RawImport **ret, | |
83 | sd_event *event, | |
84 | const char *image_root, | |
85 | RawImportFinished on_finished, | |
86 | void *userdata) { | |
87 | ||
88 | _cleanup_(raw_import_unrefp) RawImport *i = NULL; | |
0d94088e | 89 | _cleanup_free_ char *root = NULL; |
b6e676ce LP |
90 | int r; |
91 | ||
92 | assert(ret); | |
93 | ||
0d94088e YW |
94 | root = strdup(image_root ?: "/var/lib/machines"); |
95 | if (!root) | |
b6e676ce LP |
96 | return -ENOMEM; |
97 | ||
0d94088e YW |
98 | i = new(RawImport, 1); |
99 | if (!i) | |
b6e676ce LP |
100 | return -ENOMEM; |
101 | ||
0d94088e YW |
102 | *i = (RawImport) { |
103 | .input_fd = -1, | |
104 | .output_fd = -1, | |
105 | .on_finished = on_finished, | |
106 | .userdata = userdata, | |
f5fbe71d | 107 | .last_percent = UINT_MAX, |
0d94088e | 108 | .image_root = TAKE_PTR(root), |
5ac1530e | 109 | .progress_ratelimit = { 100 * USEC_PER_MSEC, 1 }, |
0d94088e YW |
110 | }; |
111 | ||
b6e676ce LP |
112 | if (event) |
113 | i->event = sd_event_ref(event); | |
114 | else { | |
115 | r = sd_event_default(&i->event); | |
116 | if (r < 0) | |
117 | return r; | |
118 | } | |
119 | ||
1cc6c93a | 120 | *ret = TAKE_PTR(i); |
b6e676ce LP |
121 | |
122 | return 0; | |
123 | } | |
124 | ||
125 | static void raw_import_report_progress(RawImport *i) { | |
126 | unsigned percent; | |
127 | assert(i); | |
128 | ||
129 | /* We have no size information, unless the source is a regular file */ | |
130 | if (!S_ISREG(i->st.st_mode)) | |
131 | return; | |
132 | ||
133 | if (i->written_compressed >= (uint64_t) i->st.st_size) | |
134 | percent = 100; | |
135 | else | |
136 | percent = (unsigned) ((i->written_compressed * UINT64_C(100)) / (uint64_t) i->st.st_size); | |
137 | ||
138 | if (percent == i->last_percent) | |
139 | return; | |
140 | ||
5ac1530e | 141 | if (!ratelimit_below(&i->progress_ratelimit)) |
b6e676ce LP |
142 | return; |
143 | ||
144 | sd_notifyf(false, "X_IMPORT_PROGRESS=%u", percent); | |
145 | log_info("Imported %u%%.", percent); | |
146 | ||
147 | i->last_percent = percent; | |
148 | } | |
149 | ||
150 | static int raw_import_maybe_convert_qcow2(RawImport *i) { | |
151 | _cleanup_close_ int converted_fd = -1; | |
152 | _cleanup_free_ char *t = NULL; | |
153 | int r; | |
154 | ||
155 | assert(i); | |
156 | ||
157 | r = qcow2_detect(i->output_fd); | |
158 | if (r < 0) | |
159 | return log_error_errno(r, "Failed to detect whether this is a QCOW2 image: %m"); | |
160 | if (r == 0) | |
161 | return 0; | |
162 | ||
163 | /* This is a QCOW2 image, let's convert it */ | |
14bcf25c | 164 | r = tempfn_random(i->final_path, NULL, &t); |
b6e676ce LP |
165 | if (r < 0) |
166 | return log_oom(); | |
167 | ||
168 | converted_fd = open(t, O_RDWR|O_CREAT|O_EXCL|O_NOCTTY|O_CLOEXEC, 0664); | |
169 | if (converted_fd < 0) | |
170 | return log_error_errno(errno, "Failed to create %s: %m", t); | |
171 | ||
137c6c6b | 172 | (void) import_set_nocow_and_log(converted_fd, t); |
b6e676ce LP |
173 | |
174 | log_info("Unpacking QCOW2 file."); | |
175 | ||
176 | r = qcow2_convert(i->output_fd, converted_fd); | |
177 | if (r < 0) { | |
6990fb6b | 178 | (void) unlink(t); |
b6e676ce LP |
179 | return log_error_errno(r, "Failed to convert qcow2 image: %m"); |
180 | } | |
181 | ||
182 | (void) unlink(i->temp_path); | |
f9ecfd3b | 183 | free_and_replace(i->temp_path, t); |
0706c012 | 184 | CLOSE_AND_REPLACE(i->output_fd, converted_fd); |
b6e676ce LP |
185 | |
186 | return 1; | |
187 | } | |
188 | ||
189 | static int raw_import_finish(RawImport *i) { | |
190 | int r; | |
191 | ||
192 | assert(i); | |
193 | assert(i->output_fd >= 0); | |
194 | assert(i->temp_path); | |
195 | assert(i->final_path); | |
196 | ||
197 | /* In case this was a sparse file, make sure the file system is right */ | |
198 | if (i->written_uncompressed > 0) { | |
199 | if (ftruncate(i->output_fd, i->written_uncompressed) < 0) | |
200 | return log_error_errno(errno, "Failed to truncate file: %m"); | |
201 | } | |
202 | ||
203 | r = raw_import_maybe_convert_qcow2(i); | |
204 | if (r < 0) | |
205 | return r; | |
206 | ||
207 | if (S_ISREG(i->st.st_mode)) { | |
adc6f43b | 208 | (void) copy_times(i->input_fd, i->output_fd, COPY_CRTIME); |
b6e676ce LP |
209 | (void) copy_xattr(i->input_fd, i->output_fd); |
210 | } | |
211 | ||
5183c50a | 212 | if (i->flags & IMPORT_READ_ONLY) { |
b6e676ce LP |
213 | r = import_make_read_only_fd(i->output_fd); |
214 | if (r < 0) | |
215 | return r; | |
216 | } | |
217 | ||
5183c50a | 218 | if (i->flags & IMPORT_FORCE) |
d9e2daaf | 219 | (void) rm_rf(i->final_path, REMOVE_ROOT|REMOVE_PHYSICAL|REMOVE_SUBVOLUME); |
b6e676ce | 220 | |
f85ef957 AC |
221 | r = rename_noreplace(AT_FDCWD, i->temp_path, AT_FDCWD, i->final_path); |
222 | if (r < 0) | |
223 | return log_error_errno(r, "Failed to move image into place: %m"); | |
b6e676ce | 224 | |
a1e58e8e | 225 | i->temp_path = mfree(i->temp_path); |
b6e676ce LP |
226 | |
227 | return 0; | |
228 | } | |
229 | ||
230 | static int raw_import_open_disk(RawImport *i) { | |
231 | int r; | |
232 | ||
233 | assert(i); | |
234 | ||
235 | assert(!i->final_path); | |
236 | assert(!i->temp_path); | |
237 | assert(i->output_fd < 0); | |
238 | ||
605405c6 | 239 | i->final_path = strjoin(i->image_root, "/", i->local, ".raw"); |
b6e676ce LP |
240 | if (!i->final_path) |
241 | return log_oom(); | |
242 | ||
14bcf25c | 243 | r = tempfn_random(i->final_path, NULL, &i->temp_path); |
b6e676ce LP |
244 | if (r < 0) |
245 | return log_oom(); | |
246 | ||
247 | (void) mkdir_parents_label(i->temp_path, 0700); | |
248 | ||
249 | i->output_fd = open(i->temp_path, O_RDWR|O_CREAT|O_EXCL|O_NOCTTY|O_CLOEXEC, 0664); | |
250 | if (i->output_fd < 0) | |
251 | return log_error_errno(errno, "Failed to open destination %s: %m", i->temp_path); | |
252 | ||
137c6c6b | 253 | (void) import_set_nocow_and_log(i->output_fd, i->temp_path); |
b6e676ce LP |
254 | return 0; |
255 | } | |
256 | ||
257 | static int raw_import_try_reflink(RawImport *i) { | |
258 | off_t p; | |
259 | int r; | |
260 | ||
261 | assert(i); | |
262 | assert(i->input_fd >= 0); | |
263 | assert(i->output_fd >= 0); | |
264 | ||
265 | if (i->compress.type != IMPORT_COMPRESS_UNCOMPRESSED) | |
266 | return 0; | |
267 | ||
268 | if (!S_ISREG(i->st.st_mode)) | |
269 | return 0; | |
270 | ||
271 | p = lseek(i->input_fd, 0, SEEK_CUR); | |
272 | if (p == (off_t) -1) | |
273 | return log_error_errno(errno, "Failed to read file offset of input file: %m"); | |
274 | ||
275 | /* Let's only try a btrfs reflink, if we are reading from the beginning of the file */ | |
276 | if ((uint64_t) p != (uint64_t) i->buffer_size) | |
277 | return 0; | |
278 | ||
279 | r = btrfs_reflink(i->input_fd, i->output_fd); | |
280 | if (r >= 0) | |
281 | return 1; | |
282 | ||
283 | return 0; | |
284 | } | |
285 | ||
286 | static int raw_import_write(const void *p, size_t sz, void *userdata) { | |
287 | RawImport *i = userdata; | |
288 | ssize_t n; | |
289 | ||
b6e676ce LP |
290 | n = sparse_write(i->output_fd, p, sz, 64); |
291 | if (n < 0) | |
e986910e | 292 | return (int) n; |
b6e676ce LP |
293 | if ((size_t) n < sz) |
294 | return -EIO; | |
295 | ||
296 | i->written_uncompressed += sz; | |
b6e676ce LP |
297 | |
298 | return 0; | |
299 | } | |
300 | ||
301 | static int raw_import_process(RawImport *i) { | |
302 | ssize_t l; | |
303 | int r; | |
304 | ||
305 | assert(i); | |
306 | assert(i->buffer_size < sizeof(i->buffer)); | |
307 | ||
308 | l = read(i->input_fd, i->buffer + i->buffer_size, sizeof(i->buffer) - i->buffer_size); | |
309 | if (l < 0) { | |
587fec42 LP |
310 | if (errno == EAGAIN) |
311 | return 0; | |
312 | ||
b6e676ce LP |
313 | r = log_error_errno(errno, "Failed to read input file: %m"); |
314 | goto finish; | |
315 | } | |
b6e676ce LP |
316 | |
317 | i->buffer_size += l; | |
318 | ||
319 | if (i->compress.type == IMPORT_COMPRESS_UNKNOWN) { | |
c9b6ebef LP |
320 | |
321 | if (l == 0) { /* EOF */ | |
322 | log_debug("File too short to be compressed, as no compression signature fits in, thus assuming uncompressed."); | |
323 | import_uncompress_force_off(&i->compress); | |
324 | } else { | |
325 | r = import_uncompress_detect(&i->compress, i->buffer, i->buffer_size); | |
326 | if (r < 0) { | |
327 | log_error_errno(r, "Failed to detect file compression: %m"); | |
328 | goto finish; | |
329 | } | |
330 | if (r == 0) /* Need more data */ | |
331 | return 0; | |
b6e676ce | 332 | } |
b6e676ce LP |
333 | |
334 | r = raw_import_open_disk(i); | |
335 | if (r < 0) | |
336 | goto finish; | |
337 | ||
338 | r = raw_import_try_reflink(i); | |
339 | if (r < 0) | |
340 | goto finish; | |
c9b6ebef LP |
341 | if (r > 0) |
342 | goto complete; | |
b6e676ce LP |
343 | } |
344 | ||
345 | r = import_uncompress(&i->compress, i->buffer, i->buffer_size, raw_import_write, i); | |
346 | if (r < 0) { | |
347 | log_error_errno(r, "Failed to decode and write: %m"); | |
348 | goto finish; | |
349 | } | |
350 | ||
351 | i->written_compressed += i->buffer_size; | |
352 | i->buffer_size = 0; | |
353 | ||
c9b6ebef LP |
354 | if (l == 0) /* EOF */ |
355 | goto complete; | |
356 | ||
b6e676ce LP |
357 | raw_import_report_progress(i); |
358 | ||
359 | return 0; | |
360 | ||
c9b6ebef LP |
361 | complete: |
362 | r = raw_import_finish(i); | |
363 | ||
b6e676ce LP |
364 | finish: |
365 | if (i->on_finished) | |
366 | i->on_finished(i, r, i->userdata); | |
367 | else | |
368 | sd_event_exit(i->event, r); | |
369 | ||
370 | return 0; | |
371 | } | |
372 | ||
373 | static int raw_import_on_input(sd_event_source *s, int fd, uint32_t revents, void *userdata) { | |
374 | RawImport *i = userdata; | |
375 | ||
376 | return raw_import_process(i); | |
377 | } | |
378 | ||
379 | static int raw_import_on_defer(sd_event_source *s, void *userdata) { | |
380 | RawImport *i = userdata; | |
381 | ||
382 | return raw_import_process(i); | |
383 | } | |
384 | ||
5183c50a | 385 | int raw_import_start(RawImport *i, int fd, const char *local, ImportFlags flags) { |
b6e676ce LP |
386 | int r; |
387 | ||
388 | assert(i); | |
389 | assert(fd >= 0); | |
390 | assert(local); | |
5183c50a | 391 | assert(!(flags & ~IMPORT_FLAGS_MASK)); |
b6e676ce | 392 | |
52ef5dd7 | 393 | if (!hostname_is_valid(local, 0)) |
b6e676ce LP |
394 | return -EINVAL; |
395 | ||
396 | if (i->input_fd >= 0) | |
397 | return -EBUSY; | |
398 | ||
587fec42 LP |
399 | r = fd_nonblock(fd, true); |
400 | if (r < 0) | |
401 | return r; | |
402 | ||
b6e676ce LP |
403 | r = free_and_strdup(&i->local, local); |
404 | if (r < 0) | |
405 | return r; | |
5183c50a LP |
406 | |
407 | i->flags = flags; | |
b6e676ce LP |
408 | |
409 | if (fstat(fd, &i->st) < 0) | |
410 | return -errno; | |
411 | ||
412 | r = sd_event_add_io(i->event, &i->input_event_source, fd, EPOLLIN, raw_import_on_input, i); | |
413 | if (r == -EPERM) { | |
414 | /* This fd does not support epoll, for example because it is a regular file. Busy read in that case */ | |
415 | r = sd_event_add_defer(i->event, &i->input_event_source, raw_import_on_defer, i); | |
416 | if (r < 0) | |
417 | return r; | |
418 | ||
419 | r = sd_event_source_set_enabled(i->input_event_source, SD_EVENT_ON); | |
420 | } | |
421 | if (r < 0) | |
422 | return r; | |
423 | ||
424 | i->input_fd = fd; | |
425 | return r; | |
426 | } |