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