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