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