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