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