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