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