]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
makefs: port to generic make_filesystem() call
authorLennart Poettering <lennart@poettering.net>
Wed, 29 Jul 2020 17:10:33 +0000 (19:10 +0200)
committerLennart Poettering <lennart@poettering.net>
Mon, 24 Aug 2020 20:00:07 +0000 (22:00 +0200)
src/partition/makefs.c

index 97f50c90335bcd426ef4158b4fa5ad533e0bd495..1ce6acd01e8fa1cd70b3758879555fb8775df37c 100644 (file)
 #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 makefs(const char *type, const char *device) {
-        const char *mkfs;
-        pid_t pid;
-        int r;
-
-        if (streq(type, "swap"))
-                mkfs = "/sbin/mkswap";
-        else
-                mkfs = strjoina("/sbin/mkfs.", type);
-        if (access(mkfs, X_OK) != 0)
-                return log_error_errno(errno, "%s is not executable: %m", mkfs);
-
-        r = safe_fork("(mkfs)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG, &pid);
-        if (r < 0)
-                return r;
-        if (r == 0) {
-                const char *cmdline[3] = { mkfs, device, NULL };
-
-                /* Child */
-
-                execv(cmdline[0], (char**) cmdline);
-                _exit(EXIT_FAILURE);
-        }
-
-        return wait_for_terminate_and_check(mkfs, pid, WAIT_LOG);
-}
-
 static int run(int argc, char *argv[]) {
-        _cleanup_free_ char *device = NULL, *type = NULL, *detected = NULL;
+        _cleanup_free_ char *device = NULL, *fstype = NULL, *detected = NULL;
         _cleanup_close_ int lock_fd = -1;
+        sd_id128_t uuid;
         struct stat st;
         int r;
 
@@ -55,8 +30,8 @@ static int run(int argc, char *argv[]) {
                                        "This program expects two arguments.");
 
         /* type and device must be copied because makefs calls safe_fork, which clears argv[] */
-        type = strdup(argv[1]);
-        if (!type)
+        fstype = strdup(argv[1]);
+        if (!fstype)
                 return log_oom();
 
         device = strdup(argv[2]);
@@ -85,7 +60,11 @@ static int run(int argc, char *argv[]) {
                 return 0;
         }
 
-        return makefs(type, device);
+        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), uuid, true);
 }
 
 DEFINE_MAIN_FUNCTION(run);