Debian Codegraph @

src/import/import-fs.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <locale.h>
4 #include <unistd.h>
5
6 #include "alloc-util.h"
7 #include "ansi-color.h"
8 #include "btrfs-util.h"
9 #include "build.h"
10 #include "copy.h"
11 #include "discover-image.h"
12 #include "fd-util.h"
13 #include "format-table.h"
14 #include "format-util.h"
15 #include "import-common.h"
16 #include "import-util.h"
17 #include "install-file.h"
18 #include "log.h"
19 #include "main-func.h"
20 #include "mkdir.h"
21 #include "options.h"
22 #include "parse-argument.h"
23 #include "path-util.h"
24 #include "ratelimit.h"
25 #include "rm-rf.h"
26 #include "runtime-scope.h"
27 #include "signal-util.h"
28 #include "string-util.h"
29 #include "time-util.h"
30 #include "tmpfile-util.h"
31 #include "verbs.h"
32
33 static bool arg_force = false;
34 static bool arg_read_only = false;
35 static bool arg_btrfs_subvol = true;
36 static bool arg_btrfs_quota = true;
37 static bool arg_sync = true;
38 static bool arg_direct = false;
39 static char *arg_image_root = NULL;
40 static ImageClass arg_class = IMAGE_MACHINE;
41 static RuntimeScope arg_runtime_scope = _RUNTIME_SCOPE_INVALID;
42
43 STATIC_DESTRUCTOR_REGISTER(arg_image_root, freep);
44
45 typedef struct ProgressInfo {
46 RateLimit limit;
47 char *path;
48 uint64_t size;
49 bool started;
50 bool logged_incomplete;
51 uint64_t bps;
52 } ProgressInfo;
53
54 static void progress_info_free(ProgressInfo *p) {
55 free(p->path);
56 }
57
58 static void progress_show(ProgressInfo *p) {
59 assert(p);
60
61 /* Show progress only every now and then. */
62 if (!ratelimit_below(&p->limit))
63 return;
64
65 /* Suppress the first message, start with the second one */
66 if (!p->started) {
67 p->started = true;
68 return;
69 }
70
71 /* Mention the list is incomplete before showing first output. */
72 if (!p->logged_incomplete) {
73 log_notice("(Note: file list shown below is incomplete, and is intended as sporadic progress report only.)");
74 p->logged_incomplete = true;
75 }
76
77 if (p->size == 0)
78 log_info("Copying tree, currently at '%s'...", p->path);
79 else if (p->bps == UINT64_MAX)
80 log_info("Copying tree, currently at '%s' (@%s)...", p->path, FORMAT_BYTES(p->size));
81 else
82 log_info("Copying tree, currently at '%s' (@%s, %s/s)...", p->path, FORMAT_BYTES(p->size), FORMAT_BYTES(p->bps));
83 }
84
85 static int progress_path(const char *path, const struct stat *st, void *userdata) {
86 ProgressInfo *p = ASSERT_PTR(userdata);
87 int r;
88
89 r = free_and_strdup(&p->path, path);
90 if (r < 0)
91 return r;
92
93 p->size = 0;
94
95 progress_show(p);
96 return 0;
97 }
98
99 static int progress_bytes(uint64_t nbytes, uint64_t bps, void *userdata) {
100 ProgressInfo *p = ASSERT_PTR(userdata);
101
102 assert(p->size != UINT64_MAX);
103
104 p->size += nbytes;
105 p->bps = bps;
106
107 progress_show(p);
108 return 0;
109 }
110
111 VERB(verb_import_fs, "run", "DIRECTORY [NAME]", 2, 3, 0,
112 "Import a directory");
113 static int verb_import_fs(int argc, char *argv[], uintptr_t _data, void *userdata) {
114 _cleanup_(rm_rf_subvolume_and_freep) char *temp_path = NULL;
115 _cleanup_(progress_info_free) ProgressInfo progress = { .bps = UINT64_MAX };
116 _cleanup_free_ char *l = NULL, *final_path = NULL;
117 const char *path = NULL, *local = NULL, *dest = NULL;
118 _cleanup_close_ int open_fd = -EBADF;
119 int r, fd;
120
121 if (argc >= 2)
122 path = empty_or_dash_to_null(argv[1]);
123
124 if (argc >= 3)
125 local = empty_or_dash_to_null(argv[2]);
126 else if (path) {
127 r = path_extract_filename(path, &l);
128 if (r < 0)
129 return log_error_errno(r, "Failed to extract filename from path '%s': %m", path);
130
131 local = l;
132 }
133
134 if (arg_direct) {
135 if (!local)
136 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No local path specified.");
137
138 if (path_is_absolute(local))
139 final_path = strdup(local);
140 else
141 final_path = path_join(arg_image_root, local);
142 if (!final_path)
143 return log_oom();
144
145 if (!path_is_valid(final_path))
146 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
147 "Local path name '%s' is not valid.", final_path);
148 } else {
149 if (local) {
150 if (!image_name_is_valid(local))
151 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
152 "Local image name '%s' is not valid.", local);
153 } else
154 local = "imported";
155
156 final_path = path_join(arg_image_root, local);
157 if (!final_path)
158 return log_oom();
159
160 if (!arg_force) {
161 r = image_find(arg_runtime_scope, arg_class, local, NULL, NULL);
162 if (r < 0) {
163 if (r != -ENOENT)
164 return log_error_errno(r, "Failed to check whether image '%s' exists: %m", local);
165 } else
166 return log_error_errno(SYNTHETIC_ERRNO(EEXIST),
167 "Image '%s' already exists.", local);
168 }
169 }
170
171 if (path) {
172 open_fd = open(path, O_DIRECTORY|O_RDONLY|O_CLOEXEC);
173 if (open_fd < 0)
174 return log_error_errno(errno, "Failed to open directory to import: %m");
175
176 fd = open_fd;
177
178 log_info("Importing '%s', saving as '%s'.", path, local);
179 } else {
180 _cleanup_free_ char *pretty = NULL;
181
182 fd = STDIN_FILENO;
183
184 (void) fd_get_path(fd, &pretty);
185 log_info("Importing '%s', saving as '%s'.", strempty(pretty), local);
186 }
187
188 log_info("Operating on image directory '%s'.", arg_image_root);
189
190 if (!arg_sync)
191 log_info("File system synchronization on completion is off.");
192
193 if (arg_direct) {
194 if (arg_force)
195 (void) rm_rf(final_path, REMOVE_ROOT|REMOVE_PHYSICAL|REMOVE_SUBVOLUME);
196
197 dest = final_path;
198 } else {
199 r = tempfn_random(final_path, NULL, &temp_path);
200 if (r < 0)
201 return log_oom();
202
203 dest = temp_path;
204 }
205
206 (void) mkdir_parents_label(dest, 0700);
207
208 progress.limit = (const RateLimit) { 200*USEC_PER_MSEC, 1 };
209
210 {
211 BLOCK_SIGNALS(SIGINT, SIGTERM);
212
213 if (arg_btrfs_subvol)
214 r = btrfs_subvol_snapshot_at_full(
215 fd, NULL,
216 AT_FDCWD, dest,
217 BTRFS_SNAPSHOT_FALLBACK_COPY|
218 BTRFS_SNAPSHOT_FALLBACK_DIRECTORY|
219 BTRFS_SNAPSHOT_RECURSIVE|
220 BTRFS_SNAPSHOT_SIGINT|
221 BTRFS_SNAPSHOT_SIGTERM,
222 progress_path,
223 progress_bytes,
224 &progress);
225 else
226 r = copy_directory_at_full(
227 fd, NULL,
228 AT_FDCWD, dest,
229 /* override_uid= */ UID_INVALID,
230 /* override_gid= */ GID_INVALID,
231 COPY_REFLINK|
232 COPY_SAME_MOUNT|
233 COPY_HARDLINKS|
234 COPY_SIGINT|
235 COPY_SIGTERM|
236 (arg_direct ? COPY_MERGE_EMPTY : 0),
237 progress_path,
238 progress_bytes,
239 &progress);
240 if (r == -EINTR) /* SIGINT/SIGTERM hit */
241 return log_error_errno(r, "Copy cancelled.");
242 if (r < 0)
243 return log_error_errno(r, "Failed to copy directory: %m");
244 }
245
246 r = import_mangle_os_tree(dest, /* userns_fd= */ -EBADF, /* flags= */ 0);
247 if (r < 0)
248 return r;
249
250 if (arg_btrfs_quota) {
251 if (!arg_direct)
252 (void) import_assign_pool_quota_and_warn(arg_image_root);
253 (void) import_assign_pool_quota_and_warn(dest);
254 }
255
256 r = install_file(AT_FDCWD, dest,
257 AT_FDCWD, arg_direct ? NULL : final_path, /* pass NULL as target in case of direct
258 * mode since file is already in place */
259 (arg_force ? INSTALL_REPLACE : 0) |
260 (arg_read_only ? INSTALL_READ_ONLY : 0) |
261 (arg_sync ? INSTALL_SYNCFS : 0));
262 if (r < 0)
263 return log_error_errno(r, "Failed to install directory as '%s': %m", final_path);
264
265 temp_path = mfree(temp_path);
266
267 log_info("Directory '%s successfully installed. Exiting.", final_path);
268 return 0;
269 }
270
271 static int help(void) {
272 _cleanup_(table_unrefp) Table *options = NULL, *verbs = NULL;
273 int r;
274
275 r = verbs_get_help_table(&verbs);
276 if (r < 0)
277 return r;
278
279 r = option_parser_get_help_table(&options);
280 if (r < 0)
281 return r;
282
283 (void) table_sync_column_widths(0, verbs, options);
284
285 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
286 "%sImport container images from file system directories.%s\n"
287 "\n%sCommands:%s\n",
288 program_invocation_short_name,
289 ansi_highlight(),
290 ansi_normal(),
291 ansi_underline(),
292 ansi_normal());
293
294 r = table_print_or_warn(verbs);
295 if (r < 0)
296 return r;
297
298 printf("\n%sOptions:%s\n",
299 ansi_underline(),
300 ansi_normal());
301
302 r = table_print_or_warn(options);
303 if (r < 0)
304 return r;
305
306 return 0;
307 }
308
309 VERB_COMMON_HELP_HIDDEN(help);
310
311 static int parse_argv(int argc, char *argv[], char ***ret_args) {
312 assert(argc >= 0);
313 assert(argv);
314 assert(ret_args);
315
316 OptionParser opts = { argc, argv };
317 int r;
318
319 FOREACH_OPTION_OR_RETURN(c, &opts)
320 switch (c) {
321
322 OPTION_COMMON_HELP:
323 return help();
324
325 OPTION_COMMON_VERSION:
326 return version();
327
328 OPTION_LONG("force", NULL, "Force creation of image"):
329 arg_force = true;
330 break;
331
332 OPTION_LONG("image-root", "PATH", "Image root directory"):
333 r = parse_path_argument(opts.arg, /* suppress_root= */ false, &arg_image_root);
334 if (r < 0)
335 return r;
336 break;
337
338 OPTION_LONG("read-only", NULL, "Create a read-only image"):
339 arg_read_only = true;
340 break;
341
342 OPTION_LONG("direct", NULL, "Import directly to specified directory"):
343 arg_direct = true;
344 break;
345
346 OPTION_LONG("btrfs-subvol", "BOOL",
347 "Controls whether to create a btrfs subvolume instead of a directory"):
348 r = parse_boolean_argument("--btrfs-subvol=", opts.arg, &arg_btrfs_subvol);
349 if (r < 0)
350 return r;
351 break;
352
353 OPTION_LONG("btrfs-quota", "BOOL",
354 "Controls whether to set up quota for btrfs subvolume"):
355 r = parse_boolean_argument("--btrfs-quota=", opts.arg, &arg_btrfs_quota);
356 if (r < 0)
357 return r;
358 break;
359
360 OPTION_LONG("sync", "BOOL", "Controls whether to sync() before completing"):
361 r = parse_boolean_argument("--sync=", opts.arg, &arg_sync);
362 if (r < 0)
363 return r;
364 break;
365
366 OPTION_LONG("class", "CLASS",
367 "Select image class (machine, sysext, confext, portable)"):
368 arg_class = image_class_from_string(opts.arg);
369 if (arg_class < 0)
370 return log_error_errno(arg_class, "Failed to parse --class= argument: %s", opts.arg);
371 break;
372
373 OPTION_COMMON_SYSTEM:
374 arg_runtime_scope = RUNTIME_SCOPE_SYSTEM;
375 break;
376
377 OPTION_COMMON_USER:
378 arg_runtime_scope = RUNTIME_SCOPE_USER;
379 break;
380 }
381
382 if (!arg_image_root) {
383 r = image_root_pick(arg_runtime_scope < 0 ? RUNTIME_SCOPE_SYSTEM : arg_runtime_scope, arg_class, /* runtime= */ false, &arg_image_root);
384 if (r < 0)
385 return log_error_errno(r, "Failed to pick image root: %m");
386 }
387
388 *ret_args = option_parser_get_args(&opts);
389 return 1;
390 }
391
392 static int run(int argc, char *argv[]) {
393 int r;
394
395 setlocale(LC_ALL, "");
396 log_setup();
397
398 char **args = NULL;
399 r = parse_argv(argc, argv, &args);
400 if (r <= 0)
401 return r;
402
403 return dispatch_verb(args, NULL);
404 }
405
406 DEFINE_MAIN_FUNCTION(run);