]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/import/import-tar.c
Merge pull request #10909 from yuwata/import-cleanups
[thirdparty/systemd.git] / src / import / import-tar.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <linux/fs.h>
4
5 #include "sd-daemon.h"
6 #include "sd-event.h"
7
8 #include "alloc-util.h"
9 #include "btrfs-util.h"
10 #include "copy.h"
11 #include "fd-util.h"
12 #include "fileio.h"
13 #include "fs-util.h"
14 #include "hostname-util.h"
15 #include "import-common.h"
16 #include "import-compress.h"
17 #include "import-tar.h"
18 #include "io-util.h"
19 #include "machine-pool.h"
20 #include "mkdir.h"
21 #include "path-util.h"
22 #include "process-util.h"
23 #include "qcow2-util.h"
24 #include "ratelimit.h"
25 #include "rm-rf.h"
26 #include "string-util.h"
27 #include "util.h"
28
29 struct TarImport {
30 sd_event *event;
31
32 char *image_root;
33
34 TarImportFinished on_finished;
35 void *userdata;
36
37 char *local;
38 bool force_local;
39 bool read_only;
40 bool grow_machine_directory;
41
42 char *temp_path;
43 char *final_path;
44
45 int input_fd;
46 int tar_fd;
47
48 ImportCompress compress;
49
50 uint64_t written_since_last_grow;
51
52 sd_event_source *input_event_source;
53
54 uint8_t buffer[16*1024];
55 size_t buffer_size;
56
57 uint64_t written_compressed;
58 uint64_t written_uncompressed;
59
60 struct stat st;
61
62 pid_t tar_pid;
63
64 unsigned last_percent;
65 RateLimit progress_rate_limit;
66 };
67
68 TarImport* tar_import_unref(TarImport *i) {
69 if (!i)
70 return NULL;
71
72 sd_event_source_unref(i->input_event_source);
73
74 if (i->tar_pid > 1) {
75 (void) kill_and_sigcont(i->tar_pid, SIGKILL);
76 (void) wait_for_terminate(i->tar_pid, NULL);
77 }
78
79 if (i->temp_path) {
80 (void) rm_rf(i->temp_path, REMOVE_ROOT|REMOVE_PHYSICAL|REMOVE_SUBVOLUME);
81 free(i->temp_path);
82 }
83
84 import_compress_free(&i->compress);
85
86 sd_event_unref(i->event);
87
88 safe_close(i->tar_fd);
89
90 free(i->final_path);
91 free(i->image_root);
92 free(i->local);
93 return mfree(i);
94 }
95
96 int tar_import_new(
97 TarImport **ret,
98 sd_event *event,
99 const char *image_root,
100 TarImportFinished on_finished,
101 void *userdata) {
102
103 _cleanup_(tar_import_unrefp) TarImport *i = NULL;
104 _cleanup_free_ char *root = NULL;
105 bool grow;
106 int r;
107
108 assert(ret);
109
110 root = strdup(image_root ?: "/var/lib/machines");
111 if (!root)
112 return -ENOMEM;
113
114 grow = path_startswith(root, "/var/lib/machines");
115
116 i = new(TarImport, 1);
117 if (!i)
118 return -ENOMEM;
119
120 *i = (TarImport) {
121 .input_fd = -1,
122 .tar_fd = -1,
123 .on_finished = on_finished,
124 .userdata = userdata,
125 .last_percent = (unsigned) -1,
126 .image_root = TAKE_PTR(root),
127 .grow_machine_directory = grow,
128 };
129
130 RATELIMIT_INIT(i->progress_rate_limit, 100 * USEC_PER_MSEC, 1);
131
132 if (event)
133 i->event = sd_event_ref(event);
134 else {
135 r = sd_event_default(&i->event);
136 if (r < 0)
137 return r;
138 }
139
140 *ret = TAKE_PTR(i);
141
142 return 0;
143 }
144
145 static void tar_import_report_progress(TarImport *i) {
146 unsigned percent;
147 assert(i);
148
149 /* We have no size information, unless the source is a regular file */
150 if (!S_ISREG(i->st.st_mode))
151 return;
152
153 if (i->written_compressed >= (uint64_t) i->st.st_size)
154 percent = 100;
155 else
156 percent = (unsigned) ((i->written_compressed * UINT64_C(100)) / (uint64_t) i->st.st_size);
157
158 if (percent == i->last_percent)
159 return;
160
161 if (!ratelimit_below(&i->progress_rate_limit))
162 return;
163
164 sd_notifyf(false, "X_IMPORT_PROGRESS=%u", percent);
165 log_info("Imported %u%%.", percent);
166
167 i->last_percent = percent;
168 }
169
170 static int tar_import_finish(TarImport *i) {
171 int r;
172
173 assert(i);
174 assert(i->tar_fd >= 0);
175 assert(i->temp_path);
176 assert(i->final_path);
177
178 i->tar_fd = safe_close(i->tar_fd);
179
180 if (i->tar_pid > 0) {
181 r = wait_for_terminate_and_check("tar", i->tar_pid, WAIT_LOG);
182 i->tar_pid = 0;
183 if (r < 0)
184 return r;
185 }
186
187 if (i->read_only) {
188 r = import_make_read_only(i->temp_path);
189 if (r < 0)
190 return r;
191 }
192
193 if (i->force_local)
194 (void) rm_rf(i->final_path, REMOVE_ROOT|REMOVE_PHYSICAL|REMOVE_SUBVOLUME);
195
196 r = rename_noreplace(AT_FDCWD, i->temp_path, AT_FDCWD, i->final_path);
197 if (r < 0)
198 return log_error_errno(r, "Failed to move image into place: %m");
199
200 i->temp_path = mfree(i->temp_path);
201
202 return 0;
203 }
204
205 static int tar_import_fork_tar(TarImport *i) {
206 int r;
207
208 assert(i);
209
210 assert(!i->final_path);
211 assert(!i->temp_path);
212 assert(i->tar_fd < 0);
213
214 i->final_path = strjoin(i->image_root, "/", i->local);
215 if (!i->final_path)
216 return log_oom();
217
218 r = tempfn_random(i->final_path, NULL, &i->temp_path);
219 if (r < 0)
220 return log_oom();
221
222 (void) mkdir_parents_label(i->temp_path, 0700);
223
224 r = btrfs_subvol_make(i->temp_path);
225 if (r == -ENOTTY) {
226 if (mkdir(i->temp_path, 0755) < 0)
227 return log_error_errno(errno, "Failed to create directory %s: %m", i->temp_path);
228 } else if (r < 0)
229 return log_error_errno(r, "Failed to create subvolume %s: %m", i->temp_path);
230 else
231 (void) import_assign_pool_quota_and_warn(i->temp_path);
232
233 i->tar_fd = import_fork_tar_x(i->temp_path, &i->tar_pid);
234 if (i->tar_fd < 0)
235 return i->tar_fd;
236
237 return 0;
238 }
239
240 static int tar_import_write(const void *p, size_t sz, void *userdata) {
241 TarImport *i = userdata;
242 int r;
243
244 if (i->grow_machine_directory && i->written_since_last_grow >= GROW_INTERVAL_BYTES) {
245 i->written_since_last_grow = 0;
246 grow_machine_directory();
247 }
248
249 r = loop_write(i->tar_fd, p, sz, false);
250 if (r < 0)
251 return r;
252
253 i->written_uncompressed += sz;
254 i->written_since_last_grow += sz;
255
256 return 0;
257 }
258
259 static int tar_import_process(TarImport *i) {
260 ssize_t l;
261 int r;
262
263 assert(i);
264 assert(i->buffer_size < sizeof(i->buffer));
265
266 l = read(i->input_fd, i->buffer + i->buffer_size, sizeof(i->buffer) - i->buffer_size);
267 if (l < 0) {
268 if (errno == EAGAIN)
269 return 0;
270
271 r = log_error_errno(errno, "Failed to read input file: %m");
272 goto finish;
273 }
274 if (l == 0) {
275 if (i->compress.type == IMPORT_COMPRESS_UNKNOWN) {
276 log_error("Premature end of file.");
277 r = -EIO;
278 goto finish;
279 }
280
281 r = tar_import_finish(i);
282 goto finish;
283 }
284
285 i->buffer_size += l;
286
287 if (i->compress.type == IMPORT_COMPRESS_UNKNOWN) {
288 r = import_uncompress_detect(&i->compress, i->buffer, i->buffer_size);
289 if (r < 0) {
290 log_error_errno(r, "Failed to detect file compression: %m");
291 goto finish;
292 }
293 if (r == 0) /* Need more data */
294 return 0;
295
296 r = tar_import_fork_tar(i);
297 if (r < 0)
298 goto finish;
299 }
300
301 r = import_uncompress(&i->compress, i->buffer, i->buffer_size, tar_import_write, i);
302 if (r < 0) {
303 log_error_errno(r, "Failed to decode and write: %m");
304 goto finish;
305 }
306
307 i->written_compressed += i->buffer_size;
308 i->buffer_size = 0;
309
310 tar_import_report_progress(i);
311
312 return 0;
313
314 finish:
315 if (i->on_finished)
316 i->on_finished(i, r, i->userdata);
317 else
318 sd_event_exit(i->event, r);
319
320 return 0;
321 }
322
323 static int tar_import_on_input(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
324 TarImport *i = userdata;
325
326 return tar_import_process(i);
327 }
328
329 static int tar_import_on_defer(sd_event_source *s, void *userdata) {
330 TarImport *i = userdata;
331
332 return tar_import_process(i);
333 }
334
335 int tar_import_start(TarImport *i, int fd, const char *local, bool force_local, bool read_only) {
336 int r;
337
338 assert(i);
339 assert(fd >= 0);
340 assert(local);
341
342 if (!machine_name_is_valid(local))
343 return -EINVAL;
344
345 if (i->input_fd >= 0)
346 return -EBUSY;
347
348 r = fd_nonblock(fd, true);
349 if (r < 0)
350 return r;
351
352 r = free_and_strdup(&i->local, local);
353 if (r < 0)
354 return r;
355 i->force_local = force_local;
356 i->read_only = read_only;
357
358 if (fstat(fd, &i->st) < 0)
359 return -errno;
360
361 r = sd_event_add_io(i->event, &i->input_event_source, fd, EPOLLIN, tar_import_on_input, i);
362 if (r == -EPERM) {
363 /* This fd does not support epoll, for example because it is a regular file. Busy read in that case */
364 r = sd_event_add_defer(i->event, &i->input_event_source, tar_import_on_defer, i);
365 if (r < 0)
366 return r;
367
368 r = sd_event_source_set_enabled(i->input_event_source, SD_EVENT_ON);
369 }
370 if (r < 0)
371 return r;
372
373 i->input_fd = fd;
374 return r;
375 }