]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/import/import-raw.c
7f6a7202747a4f851883546bdcbc783d89e00482
[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 = TAKE_PTR(i);
145
146 return 0;
147 }
148
149 static void raw_import_report_progress(RawImport *i) {
150 unsigned percent;
151 assert(i);
152
153 /* We have no size information, unless the source is a regular file */
154 if (!S_ISREG(i->st.st_mode))
155 return;
156
157 if (i->written_compressed >= (uint64_t) i->st.st_size)
158 percent = 100;
159 else
160 percent = (unsigned) ((i->written_compressed * UINT64_C(100)) / (uint64_t) i->st.st_size);
161
162 if (percent == i->last_percent)
163 return;
164
165 if (!ratelimit_test(&i->progress_rate_limit))
166 return;
167
168 sd_notifyf(false, "X_IMPORT_PROGRESS=%u", percent);
169 log_info("Imported %u%%.", percent);
170
171 i->last_percent = percent;
172 }
173
174 static int raw_import_maybe_convert_qcow2(RawImport *i) {
175 _cleanup_close_ int converted_fd = -1;
176 _cleanup_free_ char *t = NULL;
177 int r;
178
179 assert(i);
180
181 r = qcow2_detect(i->output_fd);
182 if (r < 0)
183 return log_error_errno(r, "Failed to detect whether this is a QCOW2 image: %m");
184 if (r == 0)
185 return 0;
186
187 /* This is a QCOW2 image, let's convert it */
188 r = tempfn_random(i->final_path, NULL, &t);
189 if (r < 0)
190 return log_oom();
191
192 converted_fd = open(t, O_RDWR|O_CREAT|O_EXCL|O_NOCTTY|O_CLOEXEC, 0664);
193 if (converted_fd < 0)
194 return log_error_errno(errno, "Failed to create %s: %m", t);
195
196 r = chattr_fd(converted_fd, FS_NOCOW_FL, FS_NOCOW_FL);
197 if (r < 0)
198 log_warning_errno(r, "Failed to set file attributes on %s: %m", t);
199
200 log_info("Unpacking QCOW2 file.");
201
202 r = qcow2_convert(i->output_fd, converted_fd);
203 if (r < 0) {
204 unlink(t);
205 return log_error_errno(r, "Failed to convert qcow2 image: %m");
206 }
207
208 (void) unlink(i->temp_path);
209 free_and_replace(i->temp_path, t);
210
211 safe_close(i->output_fd);
212 i->output_fd = TAKE_FD(converted_fd);
213
214 return 1;
215 }
216
217 static int raw_import_finish(RawImport *i) {
218 int r;
219
220 assert(i);
221 assert(i->output_fd >= 0);
222 assert(i->temp_path);
223 assert(i->final_path);
224
225 /* In case this was a sparse file, make sure the file system is right */
226 if (i->written_uncompressed > 0) {
227 if (ftruncate(i->output_fd, i->written_uncompressed) < 0)
228 return log_error_errno(errno, "Failed to truncate file: %m");
229 }
230
231 r = raw_import_maybe_convert_qcow2(i);
232 if (r < 0)
233 return r;
234
235 if (S_ISREG(i->st.st_mode)) {
236 (void) copy_times(i->input_fd, i->output_fd);
237 (void) copy_xattr(i->input_fd, i->output_fd);
238 }
239
240 if (i->read_only) {
241 r = import_make_read_only_fd(i->output_fd);
242 if (r < 0)
243 return r;
244 }
245
246 if (i->force_local)
247 (void) rm_rf(i->final_path, REMOVE_ROOT|REMOVE_PHYSICAL|REMOVE_SUBVOLUME);
248
249 r = rename_noreplace(AT_FDCWD, i->temp_path, AT_FDCWD, i->final_path);
250 if (r < 0)
251 return log_error_errno(r, "Failed to move image into place: %m");
252
253 i->temp_path = mfree(i->temp_path);
254
255 return 0;
256 }
257
258 static int raw_import_open_disk(RawImport *i) {
259 int r;
260
261 assert(i);
262
263 assert(!i->final_path);
264 assert(!i->temp_path);
265 assert(i->output_fd < 0);
266
267 i->final_path = strjoin(i->image_root, "/", i->local, ".raw");
268 if (!i->final_path)
269 return log_oom();
270
271 r = tempfn_random(i->final_path, NULL, &i->temp_path);
272 if (r < 0)
273 return log_oom();
274
275 (void) mkdir_parents_label(i->temp_path, 0700);
276
277 i->output_fd = open(i->temp_path, O_RDWR|O_CREAT|O_EXCL|O_NOCTTY|O_CLOEXEC, 0664);
278 if (i->output_fd < 0)
279 return log_error_errno(errno, "Failed to open destination %s: %m", i->temp_path);
280
281 r = chattr_fd(i->output_fd, FS_NOCOW_FL, FS_NOCOW_FL);
282 if (r < 0)
283 log_warning_errno(r, "Failed to set file attributes on %s: %m", i->temp_path);
284
285 return 0;
286 }
287
288 static int raw_import_try_reflink(RawImport *i) {
289 off_t p;
290 int r;
291
292 assert(i);
293 assert(i->input_fd >= 0);
294 assert(i->output_fd >= 0);
295
296 if (i->compress.type != IMPORT_COMPRESS_UNCOMPRESSED)
297 return 0;
298
299 if (!S_ISREG(i->st.st_mode))
300 return 0;
301
302 p = lseek(i->input_fd, 0, SEEK_CUR);
303 if (p == (off_t) -1)
304 return log_error_errno(errno, "Failed to read file offset of input file: %m");
305
306 /* Let's only try a btrfs reflink, if we are reading from the beginning of the file */
307 if ((uint64_t) p != (uint64_t) i->buffer_size)
308 return 0;
309
310 r = btrfs_reflink(i->input_fd, i->output_fd);
311 if (r >= 0)
312 return 1;
313
314 return 0;
315 }
316
317 static int raw_import_write(const void *p, size_t sz, void *userdata) {
318 RawImport *i = userdata;
319 ssize_t n;
320
321 if (i->grow_machine_directory && i->written_since_last_grow >= GROW_INTERVAL_BYTES) {
322 i->written_since_last_grow = 0;
323 grow_machine_directory();
324 }
325
326 n = sparse_write(i->output_fd, p, sz, 64);
327 if (n < 0)
328 return -errno;
329 if ((size_t) n < sz)
330 return -EIO;
331
332 i->written_uncompressed += sz;
333 i->written_since_last_grow += sz;
334
335 return 0;
336 }
337
338 static int raw_import_process(RawImport *i) {
339 ssize_t l;
340 int r;
341
342 assert(i);
343 assert(i->buffer_size < sizeof(i->buffer));
344
345 l = read(i->input_fd, i->buffer + i->buffer_size, sizeof(i->buffer) - i->buffer_size);
346 if (l < 0) {
347 if (errno == EAGAIN)
348 return 0;
349
350 r = log_error_errno(errno, "Failed to read input file: %m");
351 goto finish;
352 }
353 if (l == 0) {
354 if (i->compress.type == IMPORT_COMPRESS_UNKNOWN) {
355 log_error("Premature end of file.");
356 r = -EIO;
357 goto finish;
358 }
359
360 r = raw_import_finish(i);
361 goto finish;
362 }
363
364 i->buffer_size += l;
365
366 if (i->compress.type == IMPORT_COMPRESS_UNKNOWN) {
367 r = import_uncompress_detect(&i->compress, i->buffer, i->buffer_size);
368 if (r < 0) {
369 log_error_errno(r, "Failed to detect file compression: %m");
370 goto finish;
371 }
372 if (r == 0) /* Need more data */
373 return 0;
374
375 r = raw_import_open_disk(i);
376 if (r < 0)
377 goto finish;
378
379 r = raw_import_try_reflink(i);
380 if (r < 0)
381 goto finish;
382 if (r > 0) {
383 r = raw_import_finish(i);
384 goto finish;
385 }
386 }
387
388 r = import_uncompress(&i->compress, i->buffer, i->buffer_size, raw_import_write, i);
389 if (r < 0) {
390 log_error_errno(r, "Failed to decode and write: %m");
391 goto finish;
392 }
393
394 i->written_compressed += i->buffer_size;
395 i->buffer_size = 0;
396
397 raw_import_report_progress(i);
398
399 return 0;
400
401 finish:
402 if (i->on_finished)
403 i->on_finished(i, r, i->userdata);
404 else
405 sd_event_exit(i->event, r);
406
407 return 0;
408 }
409
410 static int raw_import_on_input(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
411 RawImport *i = userdata;
412
413 return raw_import_process(i);
414 }
415
416 static int raw_import_on_defer(sd_event_source *s, void *userdata) {
417 RawImport *i = userdata;
418
419 return raw_import_process(i);
420 }
421
422 int raw_import_start(RawImport *i, int fd, const char *local, bool force_local, bool read_only) {
423 int r;
424
425 assert(i);
426 assert(fd >= 0);
427 assert(local);
428
429 if (!machine_name_is_valid(local))
430 return -EINVAL;
431
432 if (i->input_fd >= 0)
433 return -EBUSY;
434
435 r = fd_nonblock(fd, true);
436 if (r < 0)
437 return r;
438
439 r = free_and_strdup(&i->local, local);
440 if (r < 0)
441 return r;
442 i->force_local = force_local;
443 i->read_only = read_only;
444
445 if (fstat(fd, &i->st) < 0)
446 return -errno;
447
448 r = sd_event_add_io(i->event, &i->input_event_source, fd, EPOLLIN, raw_import_on_input, i);
449 if (r == -EPERM) {
450 /* This fd does not support epoll, for example because it is a regular file. Busy read in that case */
451 r = sd_event_add_defer(i->event, &i->input_event_source, raw_import_on_defer, i);
452 if (r < 0)
453 return r;
454
455 r = sd_event_source_set_enabled(i->input_event_source, SD_EVENT_ON);
456 }
457 if (r < 0)
458 return r;
459
460 i->input_fd = fd;
461 return r;
462 }