]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/mkfs-util.c
ce10e60bb6b9f24d106ce42451adaf5c0bff8467
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include "id128-util.h"
6 #include "process-util.h"
7 #include "stdio-util.h"
8 #include "string-util.h"
10 int mkfs_exists(const char *fstype
) {
16 if (STR_IN_SET(fstype
, "auto", "swap")) /* these aren't real file system types, refuse early */
19 mkfs
= strjoina("mkfs.", fstype
);
20 if (!filename_is_valid(mkfs
)) /* refuse file system types with slashes and similar */
23 r
= find_executable(mkfs
, NULL
);
39 _cleanup_free_
char *mkfs
= NULL
;
46 if (streq(fstype
, "swap")) {
47 r
= find_executable("mkswap", &mkfs
);
49 return log_error_errno(SYNTHETIC_ERRNO(EPROTONOSUPPORT
), "mkswap binary not available.");
51 return log_error_errno(r
, "Failed to determine whether mkswap binary exists: %m");
53 r
= mkfs_exists(fstype
);
55 return log_error_errno(r
, "Failed to determine whether mkfs binary for %s exists: %m", fstype
);
57 return log_error_errno(SYNTHETIC_ERRNO(EPROTONOSUPPORT
), "mkfs binary for %s is not available.", fstype
);
59 mkfs
= strjoin("mkfs.", fstype
);
64 r
= safe_fork("(mkfs)", FORK_RESET_SIGNALS
|FORK_RLIMIT_NOFILE_SAFE
|FORK_DEATHSIG
|FORK_LOG
|FORK_WAIT
|FORK_STDOUT_TO_STDERR
, NULL
);
68 char suuid
[ID128_UUID_STRING_MAX
];
71 id128_to_uuid_string(uuid
, suuid
);
73 if (streq(fstype
, "ext4"))
74 (void) execlp(mkfs
, mkfs
,
80 "-E", discard
? "lazy_itable_init=1,discard" : "lazy_itable_init=1,nodiscard",
83 else if (streq(fstype
, "btrfs")) {
85 (void) execlp(mkfs
, mkfs
, "-L", label
, "-U", suuid
, node
, NULL
);
87 (void) execlp(mkfs
, mkfs
, "-L", label
, "-U", suuid
, "--nodiscard", node
, NULL
);
89 } else if (streq(fstype
, "xfs")) {
92 j
= strjoina("uuid=", suuid
);
94 (void) execlp(mkfs
, mkfs
, "-L", label
, "-m", j
, "-m", "reflink=1", node
, NULL
);
96 (void) execlp(mkfs
, mkfs
, "-L", label
, "-m", j
, "-m", "reflink=1", "-K", node
, NULL
);
98 } else if (streq(fstype
, "vfat")) {
99 char mangled_label
[8 + 3 + 1], vol_id
[8 + 1];
101 /* Classic FAT only allows 11 character uppercase labels */
102 strncpy(mangled_label
, label
, sizeof(mangled_label
)-1);
103 mangled_label
[sizeof(mangled_label
)-1] = 0;
104 ascii_strupper(mangled_label
);
106 xsprintf(vol_id
, "%08" PRIx32
,
107 ((uint32_t) uuid
.bytes
[0] << 24) |
108 ((uint32_t) uuid
.bytes
[1] << 16) |
109 ((uint32_t) uuid
.bytes
[2] << 8) |
110 ((uint32_t) uuid
.bytes
[3])); /* Take first 32 byte of UUID */
112 (void) execlp(mkfs
, mkfs
,
115 "-F", "32", /* yes, we force FAT32 here */
118 } else if (streq(fstype
, "swap")) {
120 (void) execlp(mkfs
, mkfs
,
126 /* Generic fallback for all other file systems */
127 (void) execlp(mkfs
, mkfs
, node
, NULL
);
129 log_error_errno(errno
, "Failed to execute %s: %m", mkfs
);