mkfs-util: turn quiet/discard to a flags field

Add a new `MakeFilesystemFlags` enum and use it to replace the existing
`quiet` and `discard` booleans on `make_filesystem()`.

Update the callers.  While we're doing that, consolidate some duplicated
logic in systemd-repart.

Signed-off-by: Allison Karlitskaya <allison.karlitskaya@redhat.com>
This commit is contained in:
Allison Karlitskaya
2024-12-03 09:21:16 +01:00
parent c69598a3ab
commit d89283b48e
6 changed files with 41 additions and 31 deletions

View File

@@ -70,8 +70,7 @@ static int run(int argc, char *argv[]) {
label,
/* root = */ NULL,
uuid,
/* discard = */ true,
/* quiet = */ true,
MKFS_DISCARD | MKFS_QUIET,
/* sector_size = */ 0,
/* compression = */ NULL,
/* compression_level = */ NULL,

View File

@@ -2382,8 +2382,7 @@ int home_create_luks(
user_record_user_name_and_realm(h),
/* root = */ NULL,
fs_uuid,
user_record_luks_discard(h),
/* quiet = */ true,
(user_record_luks_discard(h) ? MKFS_DISCARD : 0) | MKFS_QUIET,
/* sector_size = */ 0,
/* compression = */ NULL,
/* compression_level= */ NULL,

View File

@@ -2365,6 +2365,18 @@ static bool partition_needs_populate(const Partition *p) {
(p->suppressing && partition_needs_populate(p->suppressing));
}
static MakeFileSystemFlags partition_mkfs_flags(const Partition *p) {
MakeFileSystemFlags flags = 0;
if (arg_discard)
flags |= MKFS_DISCARD;
if (streq(p->format, "erofs") && !DEBUG_LOGGING)
flags |= MKFS_QUIET;
return flags;
}
static int partition_read_definition(Partition *p, const char *path, const char *const *conf_file_dirs) {
ConfigTableItem table[] = {
@@ -6254,8 +6266,7 @@ static int context_mkfs(Context *context) {
return r;
r = make_filesystem(partition_target_path(t), p->format, strempty(p->new_label), root,
p->fs_uuid, arg_discard,
/* quiet = */ streq(p->format, "erofs") && !DEBUG_LOGGING,
p->fs_uuid, partition_mkfs_flags(p),
context->fs_sector_size, p->compression, p->compression_level,
extra_mkfs_options);
if (r < 0)
@@ -7849,8 +7860,7 @@ static int context_minimize(Context *context) {
strempty(p->new_label),
root,
fs_uuid,
arg_discard,
/* quiet = */ streq(p->format, "erofs") && !DEBUG_LOGGING,
partition_mkfs_flags(p),
context->fs_sector_size,
p->compression,
p->compression_level,
@@ -7941,8 +7951,7 @@ static int context_minimize(Context *context) {
strempty(p->new_label),
root,
p->fs_uuid,
arg_discard,
/* quiet = */ streq(p->format, "erofs") && !DEBUG_LOGGING,
partition_mkfs_flags(p),
context->fs_sector_size,
p->compression,
p->compression_level,

View File

@@ -322,8 +322,7 @@ int make_filesystem(
const char *label,
const char *root,
sd_id128_t uuid,
bool discard,
bool quiet,
MakeFileSystemFlags flags,
uint64_t sector_size,
char *compression,
char *compression_level,
@@ -424,7 +423,7 @@ int make_filesystem(
"-U", vol_id,
"-I", "256",
"-m", "0",
"-E", discard ? "discard,lazy_itable_init=1" : "nodiscard,lazy_itable_init=1",
"-E", FLAGS_SET(flags, MKFS_DISCARD) ? "discard,lazy_itable_init=1" : "nodiscard,lazy_itable_init=1",
"-b", "4096",
"-T", "default");
if (!argv)
@@ -433,7 +432,7 @@ int make_filesystem(
if (root && strv_extend_many(&argv, "-d", root) < 0)
return log_oom();
if (quiet && strv_extend(&argv, "-q") < 0)
if (FLAGS_SET(flags, MKFS_QUIET) && strv_extend(&argv, "-q") < 0)
return log_oom();
if (strv_extend(&argv, node) < 0)
@@ -454,13 +453,13 @@ int make_filesystem(
if (!argv)
return log_oom();
if (!discard && strv_extend(&argv, "--nodiscard") < 0)
if (!FLAGS_SET(flags, MKFS_DISCARD) && strv_extend(&argv, "--nodiscard") < 0)
return log_oom();
if (root && strv_extend_many(&argv, "-r", root) < 0)
return log_oom();
if (quiet && strv_extend(&argv, "-q") < 0)
if (FLAGS_SET(flags, MKFS_QUIET) && strv_extend(&argv, "-q") < 0)
return log_oom();
if (compression) {
@@ -479,7 +478,7 @@ int make_filesystem(
/* mkfs.btrfs unconditionally warns about several settings changing from v5.15 onwards which
* isn't silenced by "-q", so let's redirect stdout to /dev/null as well. */
if (quiet)
if (FLAGS_SET(flags, MKFS_QUIET))
stdio_fds[1] = -EBADF;
/* mkfs.btrfs expects a sector size of at least 4k bytes. */
@@ -495,11 +494,11 @@ int make_filesystem(
"-f", /* force override, without this it doesn't seem to want to write to an empty partition */
"-l", label,
"-U", vol_id,
"-t", one_zero(discard));
"-t", one_zero(FLAGS_SET(flags, MKFS_DISCARD)));
if (!argv)
return log_oom();
if (quiet && strv_extend(&argv, "-q") < 0)
if (FLAGS_SET(flags, MKFS_QUIET) && strv_extend(&argv, "-q") < 0)
return log_oom();
if (sector_size > 0) {
@@ -525,7 +524,7 @@ int make_filesystem(
if (!argv)
return log_oom();
if (!discard && strv_extend(&argv, "-K") < 0)
if (!FLAGS_SET(flags, MKFS_DISCARD) && strv_extend(&argv, "-K") < 0)
return log_oom();
if (root) {
@@ -557,7 +556,7 @@ int make_filesystem(
return log_oom();
}
if (quiet && strv_extend(&argv, "-q") < 0)
if (FLAGS_SET(flags, MKFS_QUIET) && strv_extend(&argv, "-q") < 0)
return log_oom();
if (strv_extend(&argv, node) < 0)
@@ -584,7 +583,7 @@ int make_filesystem(
return log_oom();
/* mkfs.vfat does not have a --quiet option so let's redirect stdout to /dev/null instead. */
if (quiet)
if (FLAGS_SET(flags, MKFS_QUIET))
stdio_fds[1] = -EBADF;
} else if (streq(fstype, "swap")) {
@@ -597,7 +596,7 @@ int make_filesystem(
if (!argv)
return log_oom();
if (quiet)
if (FLAGS_SET(flags, MKFS_QUIET))
stdio_fds[1] = -EBADF;
} else if (streq(fstype, "squashfs")) {
@@ -617,7 +616,7 @@ int make_filesystem(
}
/* mksquashfs -quiet option is pretty new so let's redirect stdout to /dev/null instead. */
if (quiet)
if (FLAGS_SET(flags, MKFS_QUIET))
stdio_fds[1] = -EBADF;
} else if (streq(fstype, "erofs")) {
@@ -626,7 +625,7 @@ int make_filesystem(
if (!argv)
return log_oom();
if (quiet && strv_extend(&argv, "--quiet") < 0)
if (FLAGS_SET(flags, MKFS_QUIET) && strv_extend(&argv, "--quiet") < 0)
return log_oom();
if (compression) {

View File

@@ -5,6 +5,11 @@
#include "forward.h"
typedef enum MakeFilesystemFlags {
MKFS_QUIET = 1 << 0, /* Suppress mkfs command output */
MKFS_DISCARD = 1 << 1, /* Enable 'discard' mode on the filesystem */
} MakeFileSystemFlags;
int mkfs_exists(const char *fstype);
int mkfs_supports_root_option(const char *fstype);
@@ -15,8 +20,7 @@ int make_filesystem(
const char *label,
const char *root,
sd_id128_t uuid,
bool discard,
bool quiet,
MakeFileSystemFlags flags,
uint64_t sector_size,
char *compression,
char *compression_level,

View File

@@ -266,16 +266,16 @@ static int run(int argc, char *argv[]) {
assert_se(r >= 0);
assert_se(sd_id128_randomize(&id) >= 0);
assert_se(make_filesystem(dissected->partitions[PARTITION_ESP].node, "vfat", "EFI", NULL, id, true, false, 0, NULL, NULL, NULL) >= 0);
assert_se(make_filesystem(dissected->partitions[PARTITION_ESP].node, "vfat", "EFI", NULL, id, MKFS_DISCARD, 0, NULL, NULL, NULL) >= 0);
assert_se(sd_id128_randomize(&id) >= 0);
assert_se(make_filesystem(dissected->partitions[PARTITION_XBOOTLDR].node, "vfat", "xbootldr", NULL, id, true, false, 0, NULL, NULL, NULL) >= 0);
assert_se(make_filesystem(dissected->partitions[PARTITION_XBOOTLDR].node, "vfat", "xbootldr", NULL, id, MKFS_DISCARD, 0, NULL, NULL, NULL) >= 0);
assert_se(sd_id128_randomize(&id) >= 0);
assert_se(make_filesystem(dissected->partitions[PARTITION_ROOT].node, "ext4", "root", NULL, id, true, false, 0, NULL, NULL, NULL) >= 0);
assert_se(make_filesystem(dissected->partitions[PARTITION_ROOT].node, "ext4", "root", NULL, id, MKFS_DISCARD, 0, NULL, NULL, NULL) >= 0);
assert_se(sd_id128_randomize(&id) >= 0);
assert_se(make_filesystem(dissected->partitions[PARTITION_HOME].node, "ext4", "home", NULL, id, true, false, 0, NULL, NULL, NULL) >= 0);
assert_se(make_filesystem(dissected->partitions[PARTITION_HOME].node, "ext4", "home", NULL, id, MKFS_DISCARD, 0, NULL, NULL, NULL) >= 0);
dissected = dissected_image_unref(dissected);