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