]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/partition/makefs.c
2 SPDX-License-Identifier: LGPL-2.1+
12 #include "alloc-util.h"
13 #include "dissect-image.h"
14 #include "main-func.h"
15 #include "process-util.h"
16 #include "signal-util.h"
17 #include "string-util.h"
19 static int makefs(const char *type
, const char *device
) {
24 if (streq(type
, "swap"))
25 mkfs
= "/sbin/mkswap";
27 mkfs
= strjoina("/sbin/mkfs.", type
);
28 if (access(mkfs
, X_OK
) != 0)
29 return log_error_errno(errno
, "%s is not executable: %m", mkfs
);
31 r
= safe_fork("(fsck)", FORK_RESET_SIGNALS
|FORK_DEATHSIG
|FORK_LOG
, &pid
);
35 const char *cmdline
[3] = { mkfs
, device
, NULL
};
39 execv(cmdline
[0], (char**) cmdline
);
43 return wait_for_terminate_and_check(mkfs
, pid
, WAIT_LOG
);
46 static int run(int argc
, char *argv
[]) {
47 const char *device
, *type
;
48 _cleanup_free_
char *detected
= NULL
;
55 log_error("This program expects two arguments.");
62 if (stat(device
, &st
) < 0)
63 return log_error_errno(errno
, "Failed to stat \"%s\": %m", device
);
65 if (!S_ISBLK(st
.st_mode
))
66 log_info("%s is not a block device.", device
);
68 r
= probe_filesystem(device
, &detected
);
70 return log_warning_errno(r
,
72 "Cannot reliably determine probe \"%s\", refusing to proceed." :
73 "Failed to probe \"%s\": %m",
77 log_info("%s is not empty (type %s), exiting", device
, detected
);
81 return makefs(type
, device
);
84 DEFINE_MAIN_FUNCTION(run
);