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