From b750101eb236130cf056c675997decbac904cc49 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 17:35:18 +0200 Subject: Adding upstream version 252.22. Signed-off-by: Daniel Baumann --- src/partition/makefs.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 src/partition/makefs.c (limited to 'src/partition/makefs.c') diff --git a/src/partition/makefs.c b/src/partition/makefs.c new file mode 100644 index 0000000..e8aa813 --- /dev/null +++ b/src/partition/makefs.c @@ -0,0 +1,71 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +#include +#include +#include +#include +#include +#include + +#include "alloc-util.h" +#include "blockdev-util.h" +#include "dissect-image.h" +#include "fd-util.h" +#include "main-func.h" +#include "mkfs-util.h" +#include "process-util.h" +#include "signal-util.h" +#include "string-util.h" + +static int run(int argc, char *argv[]) { + _cleanup_free_ char *device = NULL, *fstype = NULL, *detected = NULL; + _cleanup_close_ int lock_fd = -1; + sd_id128_t uuid; + struct stat st; + int r; + + log_setup(); + + if (argc != 3) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), + "This program expects two arguments."); + + /* type and device must be copied because makefs calls safe_fork, which clears argv[] */ + fstype = strdup(argv[1]); + if (!fstype) + return log_oom(); + + device = strdup(argv[2]); + if (!device) + return log_oom(); + + if (stat(device, &st) < 0) + return log_error_errno(errno, "Failed to stat \"%s\": %m", device); + + if (S_ISBLK(st.st_mode)) { + /* Lock the device so that udev doesn't interfere with our work */ + + lock_fd = lock_whole_block_device(st.st_rdev, LOCK_EX); + if (lock_fd < 0) + return log_error_errno(lock_fd, "Failed to lock whole block device of \"%s\": %m", device); + } else + log_debug("%s is not a block device, no need to lock.", device); + + r = probe_filesystem(device, &detected); + if (r == -EUCLEAN) + return log_error_errno(r, "Ambiguous results of probing for file system on \"%s\", refusing to proceed.", device); + if (r < 0) + return log_error_errno(r, "Failed to probe \"%s\": %m", device); + if (detected) { + log_info("'%s' is not empty (contains file system of type %s), exiting.", device, detected); + return 0; + } + + r = sd_id128_randomize(&uuid); + if (r < 0) + return log_error_errno(r, "Failed to generate UUID for file system: %m"); + + return make_filesystem(device, fstype, basename(device), NULL, uuid, true); +} + +DEFINE_MAIN_FUNCTION(run); -- cgit v1.2.3