]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/partition/makefs.c
2 SPDX-License-Identifier: LGPL-2.1+
4 Copyright © 2017 Zbigniew Jędrzejewski-Szmek
11 #include <sys/types.h>
14 #include "alloc-util.h"
15 #include "dissect-image.h"
16 #include "process-util.h"
17 #include "signal-util.h"
18 #include "string-util.h"
20 static int makefs(const char *type
, const char *device
) {
25 if (streq(type
, "swap"))
26 mkfs
= "/sbin/mkswap";
28 mkfs
= strjoina("/sbin/mkfs.", type
);
29 if (access(mkfs
, X_OK
) != 0)
30 return log_error_errno(errno
, "%s is not executable: %m", mkfs
);
32 r
= safe_fork("(fsck)", FORK_RESET_SIGNALS
|FORK_DEATHSIG
|FORK_LOG
, &pid
);
36 const char *cmdline
[3] = { mkfs
, device
, NULL
};
40 execv(cmdline
[0], (char**) cmdline
);
44 return wait_for_terminate_and_check(mkfs
, pid
, WAIT_LOG
);
47 int main(int argc
, char *argv
[]) {
48 const char *device
, *type
;
49 _cleanup_free_
char *detected
= NULL
;
53 log_set_target(LOG_TARGET_AUTO
);
54 log_parse_environment();
58 log_error("This program expects two arguments.");
65 if (stat(device
, &st
) < 0) {
66 r
= log_error_errno(errno
, "Failed to stat \"%s\": %m", device
);
70 if (!S_ISBLK(st
.st_mode
))
71 log_info("%s is not a block device.", device
);
73 r
= probe_filesystem(device
, &detected
);
77 "Cannot reliably determine probe \"%s\", refusing to proceed." :
78 "Failed to probe \"%s\": %m",
84 log_info("%s is not empty (type %s), exiting", device
, detected
);
88 r
= makefs(type
, device
);
91 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;