Debian Codegraph @

src/shared/mkfs-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stdlib.h>
4 #include <sys/mount.h>
5 #include <unistd.h>
6
7 #include "log.h"
8 #include "mkfs-util.h"
9 #include "mount-util.h"
10 #include "mountpoint-util.h"
11 #include "path-util.h"
12 #include "process-util.h"
13 #include "recurse-dir.h"
14 #include "stat-util.h"
15 #include "stdio-util.h"
16 #include "string-util.h"
17 #include "strv.h"
18 #include "utf8.h"
19
20 int mkfs_exists(const char *fstype) {
21 const char *mkfs;
22 int r;
23
24 assert(fstype);
25
26 if (STR_IN_SET(fstype, "auto", "swap")) /* these aren't real file system types, refuse early */
27 return -EINVAL;
28
29 mkfs = strjoina("mkfs.", fstype);
30 if (!filename_is_valid(mkfs)) /* refuse file system types with slashes and similar */
31 return -EINVAL;
32
33 r = find_executable(mkfs, NULL);
34 if (r == -ENOENT)
35 return false;
36 if (r < 0)
37 return r;
38
39 return true;
40 }
41
42 int mkfs_find_or_warn(const char *fstype, int have_root, char **ret) {
43 int r;
44
45 assert(fstype);
46
47 if (fstype_is_ro(fstype) && have_root == 0)
48 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
49 "Cannot generate read-only filesystem %s without a source tree.", fstype);
50
51 const char *bin = NULL;
52 if (streq(fstype, "swap")) {
53 if (have_root > 0)
54 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
55 "A swap filesystem can't be populated, refusing");
56 bin = "mkswap";
57 } else if (streq(fstype, "squashfs"))
58 bin = "mksquashfs";
59 else if (streq(fstype, "erofs"))
60 bin = "mkfs.erofs";
61 else if (fstype_is_ro(fstype))
62 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
63 "Don't know how to create read-only file system '%s', refusing.", fstype);
64 if (bin) {
65 r = find_executable(bin, ret);
66 if (r == -ENOENT)
67 return log_error_errno(SYNTHETIC_ERRNO(EPROTONOSUPPORT), "%s binary not available.", bin);
68 if (r < 0)
69 return log_error_errno(r, "Failed to determine whether %s binary exists: %m", bin);
70 return 0;
71 }
72
73 if (have_root > 0 && !mkfs_supports_root_option(fstype))
74 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
75 "Populating with source tree is not supported for %s", fstype);
76 r = mkfs_exists(fstype);
77 if (r < 0)
78 return log_error_errno(r, "Failed to determine whether mkfs binary for %s exists: %m", fstype);
79 if (r == 0)
80 return log_error_errno(SYNTHETIC_ERRNO(EPROTONOSUPPORT), "mkfs binary for %s not available.", fstype);
81
82 if (ret) {
83 char *mkfs = strjoin("mkfs.", fstype);
84 if (!mkfs)
85 return log_oom();
86 *ret = mkfs;
87 }
88
89 return 0;
90 }
91
92 int mkfs_supports_root_option(const char *fstype) {
93 return fstype_is_ro(fstype) || STR_IN_SET(fstype, "ext2", "ext3", "ext4", "btrfs", "vfat", "xfs");
94 }
95
96 static int mangle_linux_fs_label(const char *s, size_t max_len, char **ret) {
97 /* Not more than max_len bytes (12 or 16) */
98
99 assert(s);
100 assert(max_len > 0);
101 assert(ret);
102
103 const char *q;
104 char *ans;
105
106 for (q = s; *q;) {
107 int l;
108
109 l = utf8_encoded_valid_unichar(q, SIZE_MAX);
110 if (l < 0)
111 return l;
112
113 if ((size_t) (q - s + l) > max_len)
114 break;
115 q += l;
116 }
117
118 ans = memdup_suffix0(s, q - s);
119 if (!ans)
120 return -ENOMEM;
121
122 *ret = ans;
123 return 0;
124 }
125
126 static int mangle_fat_label(const char *s, char **ret) {
127 _cleanup_free_ char *q = NULL;
128 int r;
129
130 assert(s);
131 assert(ret);
132
133 r = utf8_to_ascii(s, '_', &q);
134 if (r < 0)
135 return r;
136
137 /* Classic FAT only allows 11 character uppercase labels */
138 strshorten(q, 11);
139 ascii_strupper(q);
140
141 /* mkfs.vfat: Labels with characters *?.,;:/\|+=<>[]" are not allowed.
142 * Let's also replace any control chars. */
143 for (char *p = q; *p; p++)
144 if (strchr("*?.,;:/\\|+=<>[]\"", *p) || char_is_cc(*p))
145 *p = '_';
146
147 *ret = TAKE_PTR(q);
148 return 0;
149 }
150
151 static int do_mcopy(const char *node, const char *root) {
152 _cleanup_free_ char *mcopy = NULL;
153 _cleanup_strv_free_ char **argv = NULL;
154 _cleanup_free_ DirectoryEntries *de = NULL;
155 int r;
156
157 assert(node);
158 assert(root);
159
160 /* Return early if there's nothing to copy. */
161 if (dir_is_empty(root, /* ignore_hidden_or_backup= */ false))
162 return 0;
163
164 r = find_executable("mcopy", &mcopy);
165 if (r == -ENOENT)
166 return log_error_errno(SYNTHETIC_ERRNO(EPROTONOSUPPORT), "Could not find mcopy binary.");
167 if (r < 0)
168 return log_error_errno(r, "Failed to determine whether mcopy binary exists: %m");
169
170 argv = strv_new(mcopy, "-s", "-p", "-Q", "-m", "-i", node);
171 if (!argv)
172 return log_oom();
173
174 /* mcopy copies the top level directory instead of everything in it so we have to pass all
175 * the subdirectories to mcopy instead to end up with the correct directory structure. */
176
177 r = readdir_all_at(AT_FDCWD, root, RECURSE_DIR_SORT|RECURSE_DIR_ENSURE_TYPE, &de);
178 if (r < 0)
179 return log_error_errno(r, "Failed to read '%s' contents: %m", root);
180
181 for (size_t i = 0; i < de->n_entries; i++) {
182 _cleanup_free_ char *p = NULL;
183
184 p = path_join(root, de->entries[i]->d_name);
185 if (!p)
186 return log_oom();
187
188 if (!IN_SET(de->entries[i]->d_type, DT_REG, DT_DIR)) {
189 log_debug("%s is not a file/directory which are the only file types supported by vfat, ignoring", p);
190 continue;
191 }
192
193 if (strv_consume(&argv, TAKE_PTR(p)) < 0)
194 return log_oom();
195 }
196
197 if (strv_extend(&argv, "::") < 0)
198 return log_oom();
199
200 r = pidref_safe_fork(
201 "(mcopy)",
202 FORK_RESET_SIGNALS|FORK_RLIMIT_NOFILE_SAFE|FORK_DEATHSIG_SIGTERM|FORK_LOG|FORK_WAIT|FORK_STDOUT_TO_STDERR|FORK_CLOSE_ALL_FDS,
203 /* ret= */ NULL);
204 if (r < 0)
205 return r;
206 if (r == 0) {
207 /* Avoid failures caused by mismatch in expectations between mkfs.vfat and mcopy by disabling
208 * the stricter mcopy checks using MTOOLS_SKIP_CHECK. */
209 execve(mcopy, argv, STRV_MAKE("MTOOLS_SKIP_CHECK=1", "TZ=UTC", strv_find_prefix(environ, "SOURCE_DATE_EPOCH=")));
210
211 log_error_errno(errno, "Failed to execute mcopy: %m");
212
213 _exit(EXIT_FAILURE);
214 }
215
216 return 0;
217 }
218
219 int make_filesystem(
220 const char *node,
221 const char *fstype,
222 const char *label,
223 const char *root,
224 sd_id128_t uuid,
225 MakeFileSystemFlags flags,
226 uint64_t sector_size,
227 char *compression,
228 char *compression_level,
229 char * const *extra_mkfs_args) {
230
231 _cleanup_free_ char *mkfs = NULL, *mangled_label = NULL;
232 _cleanup_strv_free_ char **argv = NULL, **env = NULL;
233 char vol_id[CONST_MAX(SD_ID128_UUID_STRING_MAX, 8U + 1U)] = {};
234 int stdio_fds[3] = { -EBADF, STDERR_FILENO, STDERR_FILENO};
235 ForkFlags fork_flags = FORK_RESET_SIGNALS|FORK_RLIMIT_NOFILE_SAFE|FORK_DEATHSIG_SIGTERM|FORK_LOG|FORK_WAIT|
236 FORK_CLOSE_ALL_FDS|FORK_REARRANGE_STDIO|FORK_REOPEN_LOG;
237 int r;
238
239 assert(node);
240 assert(fstype);
241 assert(label);
242
243 r = mkfs_find_or_warn(fstype, /* have_root= */ !!root, &mkfs);
244 if (r < 0)
245 return r;
246
247 if (STR_IN_SET(fstype, "ext2", "ext3", "ext4", "xfs", "swap")) {
248 size_t max_len =
249 streq(fstype, "xfs") ? 12 :
250 streq(fstype, "swap") ? 15 :
251 16;
252
253 r = mangle_linux_fs_label(label, max_len, &mangled_label);
254 if (r < 0)
255 return log_error_errno(r, "Failed to determine volume label from string \"%s\": %m", label);
256 label = mangled_label;
257
258 } else if (streq(fstype, "vfat")) {
259 r = mangle_fat_label(label, &mangled_label);
260 if (r < 0)
261 return log_error_errno(r, "Failed to determine FAT label from string \"%s\": %m", label);
262 label = mangled_label;
263
264 xsprintf(vol_id, "%08" PRIx32,
265 ((uint32_t) uuid.bytes[0] << 24) |
266 ((uint32_t) uuid.bytes[1] << 16) |
267 ((uint32_t) uuid.bytes[2] << 8) |
268 ((uint32_t) uuid.bytes[3])); /* Take first 32 bytes of UUID */
269 }
270
271 if (isempty(vol_id))
272 assert_se(sd_id128_to_uuid_string(uuid, vol_id));
273
274 /* When changing this conditional, also adjust the log statement below. */
275 if (STR_IN_SET(fstype, "ext2", "ext3", "ext4")) {
276 const char *ext_e_opts;
277
278 /* Set hash_seed to the same value as the filesystem UUID for reproducibility */
279 ext_e_opts = strjoina(FLAGS_SET(flags, MKFS_DISCARD) ? "discard" : "nodiscard",
280 ",lazy_itable_init=1,hash_seed=",
281 vol_id);
282
283 argv = strv_new(mkfs,
284 "-L", label,
285 "-U", vol_id,
286 "-I", "256",
287 "-m", "0",
288 "-E", ext_e_opts,
289 "-b", "4096",
290 "-T", "default");
291 if (!argv)
292 return log_oom();
293
294 if (root && strv_extend_many(&argv, "-d", root) < 0)
295 return log_oom();
296
297 if (FLAGS_SET(flags, MKFS_QUIET) && strv_extend(&argv, "-q") < 0)
298 return log_oom();
299
300 if (FLAGS_SET(flags, MKFS_FS_VERITY) && strv_extend_many(&argv, "-O", "verity") < 0)
301 return log_oom();
302
303 if (strv_extend(&argv, node) < 0)
304 return log_oom();
305
306 if (sector_size > 0) {
307 if (strv_extend(&env, "MKE2FS_DEVICE_SECTSIZE") < 0)
308 return log_oom();
309
310 if (strv_extendf(&env, "%"PRIu64, sector_size) < 0)
311 return log_oom();
312 }
313
314 /* e2fsprogs supports $SOURCE_DATE_EPOCH since v1.47.1. For older versions, we need to set
315 * $E2FSPROGS_FAKE_TIME. See the following:
316 * https://github.com/tytso/e2fsprogs/commit/b6e2913061577ad981464e435026d71a48fd5caf
317 * Note, $E2FSPROGS_FAKE_TIME and $SOURCE_DATE_EPOCH are mostly equivalent, except for the
318 * 0 value handling, where $E2FSPROGS_FAKE_TIME=0 is ignored and the current time is used,
319 * but $SOURCE_DATE_EPOCH=0 sets 1970-01-01 as the timestamp. */
320 if (!secure_getenv("E2FSPROGS_FAKE_TIME")) { /* honor $E2FSPROGS_FAKE_TIME if already set */
321 const char *e = secure_getenv("SOURCE_DATE_EPOCH");
322 if (e && strv_extend_strv(&env, STRV_MAKE("E2FSPROGS_FAKE_TIME", e), /* filter_duplicates= */ false) < 0)
323 return log_oom();
324 }
325
326 } else if (streq(fstype, "btrfs")) {
327 argv = strv_new(mkfs,
328 "-L", label,
329 "-U", vol_id);
330 if (!argv)
331 return log_oom();
332
333 if (!FLAGS_SET(flags, MKFS_DISCARD) && strv_extend(&argv, "--nodiscard") < 0)
334 return log_oom();
335
336 if (root && strv_extend_many(&argv, "-r", root) < 0)
337 return log_oom();
338
339 if (FLAGS_SET(flags, MKFS_QUIET) && strv_extend(&argv, "-q") < 0)
340 return log_oom();
341
342 if (compression) {
343 if (!root)
344 log_warning("Btrfs compression setting ignored because no files are being copied. "
345 "Compression= can only be applied when CopyFiles= is also specified.");
346 else {
347 _cleanup_free_ char *c = NULL;
348
349 c = strdup(compression);
350 if (!c)
351 return log_oom();
352
353 if (compression_level && !strextend(&c, ":", compression_level))
354 return log_oom();
355
356 if (strv_extend_many(&argv, "--compress", c) < 0)
357 return log_oom();
358 }
359 }
360
361 /* mkfs.btrfs unconditionally warns about several settings changing from v5.15 onwards which
362 * isn't silenced by "-q", so let's redirect stdout to /dev/null as well. */
363 if (FLAGS_SET(flags, MKFS_QUIET))
364 stdio_fds[1] = -EBADF;
365
366 /* mkfs.btrfs expects a sector size of at least 4k bytes. */
367 if (sector_size > 0 && strv_extendf(&argv, "--sectorsize=%"PRIu64, MAX(sector_size, 4 * U64_KB)) < 0)
368 return log_oom();
369
370 if (strv_extend(&argv, node) < 0)
371 return log_oom();
372
373 } else if (streq(fstype, "f2fs")) {
374 argv = strv_new(mkfs,
375 "-g", /* "default options" */
376 "-f", /* force override, without this it doesn't seem to want to write to an empty partition */
377 "-l", label,
378 "-U", vol_id,
379 "-t", one_zero(FLAGS_SET(flags, MKFS_DISCARD)));
380 if (!argv)
381 return log_oom();
382
383 if (FLAGS_SET(flags, MKFS_QUIET) && strv_extend(&argv, "-q") < 0)
384 return log_oom();
385
386 if (FLAGS_SET(flags, MKFS_FS_VERITY) && strv_extend_many(&argv, "-O", "verity") < 0)
387 return log_oom();
388
389 if (sector_size > 0) {
390 if (strv_extend(&argv, "-w") < 0)
391 return log_oom();
392
393 if (strv_extendf(&argv, "%"PRIu64, sector_size) < 0)
394 return log_oom();
395 }
396
397 if (strv_extend(&argv, node) < 0)
398 return log_oom();
399
400 } else if (streq(fstype, "xfs")) {
401 const char *j;
402
403 j = strjoina("uuid=", vol_id);
404
405 argv = strv_new(mkfs,
406 "-L", label,
407 "-m", j,
408 "-m", "reflink=1");
409 if (!argv)
410 return log_oom();
411
412 if (!FLAGS_SET(flags, MKFS_DISCARD) && strv_extend(&argv, "-K") < 0)
413 return log_oom();
414
415 if (root && strv_extend_many(&argv, "-p", root) < 0)
416 return log_oom();
417
418 if (sector_size > 0) {
419 if (strv_extend(&argv, "-s") < 0)
420 return log_oom();
421
422 if (strv_extendf(&argv, "size=%"PRIu64, sector_size) < 0)
423 return log_oom();
424 }
425
426 if (FLAGS_SET(flags, MKFS_QUIET) && strv_extend(&argv, "-q") < 0)
427 return log_oom();
428
429 if (strv_extend(&argv, node) < 0)
430 return log_oom();
431
432 } else if (streq(fstype, "vfat")) {
433
434 argv = strv_new(mkfs,
435 "-i", vol_id,
436 "-n", label,
437 "-F", "32"); /* yes, we force FAT32 here */
438 if (!argv)
439 return log_oom();
440
441 if (sector_size > 0) {
442 if (strv_extend(&argv, "-S") < 0)
443 return log_oom();
444
445 if (strv_extendf(&argv, "%"PRIu64, sector_size) < 0)
446 return log_oom();
447 }
448
449 if (strv_extend(&argv, node) < 0)
450 return log_oom();
451
452 /* mkfs.vfat does not have a --quiet option so let's redirect stdout to /dev/null instead. */
453 if (FLAGS_SET(flags, MKFS_QUIET))
454 stdio_fds[1] = -EBADF;
455
456 } else if (streq(fstype, "swap")) {
457 /* TODO: add --quiet once util-linux v2.38 is available everywhere. */
458
459 argv = strv_new(mkfs,
460 "-L", label,
461 "-U", vol_id,
462 node);
463 if (!argv)
464 return log_oom();
465
466 if (FLAGS_SET(flags, MKFS_QUIET))
467 stdio_fds[1] = -EBADF;
468
469 } else if (streq(fstype, "squashfs")) {
470
471 argv = strv_new(mkfs,
472 root, node, /* mksquashfs expects its arguments before the options. */
473 "-noappend");
474 if (!argv)
475 return log_oom();
476
477 if (compression) {
478 if (strv_extend_many(&argv, "-comp", compression) < 0)
479 return log_oom();
480
481 if (compression_level && strv_extend_many(&argv, "-Xcompression-level", compression_level) < 0)
482 return log_oom();
483 }
484
485 /* mksquashfs -quiet option is pretty new so let's redirect stdout to /dev/null instead. */
486 if (FLAGS_SET(flags, MKFS_QUIET))
487 stdio_fds[1] = -EBADF;
488
489 } else if (streq(fstype, "erofs")) {
490 argv = strv_new(mkfs,
491 "-U", vol_id);
492 if (!argv)
493 return log_oom();
494
495 if (FLAGS_SET(flags, MKFS_QUIET) && strv_extend(&argv, "--quiet") < 0)
496 return log_oom();
497
498 if (compression) {
499 _cleanup_free_ char *c = NULL;
500
501 c = strjoin("-z", compression);
502 if (!c)
503 return log_oom();
504
505 if (compression_level && !strextend(&c, ",level=", compression_level))
506 return log_oom();
507
508 if (strv_extend(&argv, c) < 0)
509 return log_oom();
510 }
511
512 if (strv_extend_many(&argv, node, root) < 0)
513 return log_oom();
514
515 } else {
516 /* Generic fallback for all other file systems */
517 argv = strv_new(mkfs, node);
518 if (!argv)
519 return log_oom();
520 }
521
522 if (extra_mkfs_args && strv_extend_strv(&argv, extra_mkfs_args, false) < 0)
523 return log_oom();
524
525 if (streq(fstype, "btrfs")) {
526 struct stat st;
527
528 if (stat(node, &st) < 0)
529 return log_error_errno(r, "Failed to stat '%s': %m", node);
530
531 if (S_ISBLK(st.st_mode))
532 fork_flags |= FORK_NEW_MOUNTNS;
533 }
534
535 log_info("Formatting %s as %s", node, fstype);
536
537 if (DEBUG_LOGGING) {
538 _cleanup_free_ char *j = NULL;
539
540 j = strv_join(argv, " ");
541 log_debug("Executing mkfs command: %s", strna(j));
542 }
543
544 r = pidref_safe_fork_full(
545 "(mkfs)",
546 stdio_fds,
547 /* except_fds= */ NULL,
548 /* n_except_fds= */ 0,
549 fork_flags,
550 /* ret= */ NULL);
551 if (r < 0)
552 return r;
553 if (r == 0) {
554 /* Child */
555
556 STRV_FOREACH_PAIR(k, v, env)
557 if (setenv(*k, *v, /* replace= */ true) < 0) {
558 log_error_errno(r, "Failed to set %s=%s environment variable: %m", *k, *v);
559 _exit(EXIT_FAILURE);
560 }
561
562 /* mkfs.btrfs refuses to operate on block devices with mounted partitions, even if operating
563 * on unformatted free space, so let's trick it and other mkfs tools into thinking no
564 * partitions are mounted. See https://github.com/kdave/btrfs-progs/issues/640 for more
565 ° information. */
566 if (fork_flags & FORK_NEW_MOUNTNS)
567 (void) mount_nofollow_verbose(LOG_DEBUG, "/dev/null", "/proc/self/mounts", NULL, MS_BIND, NULL);
568
569 execvp(mkfs, argv);
570
571 log_error_errno(errno, "Failed to execute %s: %m", mkfs);
572
573 _exit(EXIT_FAILURE);
574 }
575
576 if (root && streq(fstype, "vfat")) {
577 r = do_mcopy(node, root);
578 if (r < 0)
579 return r;
580 }
581
582 if (STR_IN_SET(fstype, "ext2", "ext3", "ext4", "btrfs", "f2fs", "xfs", "vfat", "swap"))
583 log_info("%s successfully formatted as %s (label \"%s\", uuid %s)",
584 node, fstype, label, vol_id);
585 else if (streq(fstype, "erofs"))
586 log_info("%s successfully formatted as %s (uuid %s, no label)",
587 node, fstype, vol_id);
588 else
589 log_info("%s successfully formatted as %s (no label or uuid specified)",
590 node, fstype);
591
592 return 0;
593 }
594
595 int mkfs_options_from_env(const char *component, const char *fstype, char ***ret) {
596 _cleanup_strv_free_ char **l = NULL;
597 const char *e;
598 char *n;
599
600 assert(component);
601 assert(fstype);
602 assert(ret);
603
604 n = strjoina("SYSTEMD_", component, "_MKFS_OPTIONS_", fstype);
605 e = getenv(ascii_strupper(n));
606 if (e) {
607 l = strv_split(e, NULL);
608 if (!l)
609 return -ENOMEM;
610 }
611
612 *ret = TAKE_PTR(l);
613 return 0;
614 }