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