]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/partition/makefs.c
2 SPDX-License-Identifier: LGPL-2.1+
4 This file is part of systemd.
6 Copyright 2017 Zbigniew Jędrzejewski-Szmek
11 #include <sys/prctl.h>
13 #include <sys/types.h>
16 #include "alloc-util.h"
17 #include "dissect-image.h"
18 #include "process-util.h"
19 #include "signal-util.h"
20 #include "string-util.h"
22 static int makefs(const char *type
, const char *device
) {
27 if (streq(type
, "swap"))
28 mkfs
= "/sbin/mkswap";
30 mkfs
= strjoina("/sbin/mkfs.", type
);
31 if (access(mkfs
, X_OK
) != 0)
32 return log_error_errno(errno
, "%s is not executable: %m", mkfs
);
34 r
= safe_fork("(fsck)", FORK_RESET_SIGNALS
|FORK_DEATHSIG
|FORK_LOG
, &pid
);
38 const char *cmdline
[3] = { mkfs
, device
, NULL
};
42 execv(cmdline
[0], (char**) cmdline
);
46 return wait_for_terminate_and_check(mkfs
, pid
, WAIT_LOG
);
49 int main(int argc
, char *argv
[]) {
50 const char *device
, *type
;
51 _cleanup_free_
char *detected
= NULL
;
55 log_set_target(LOG_TARGET_AUTO
);
56 log_parse_environment();
60 log_error("This program expects two arguments.");
67 if (stat(device
, &st
) < 0) {
68 r
= log_error_errno(errno
, "Failed to stat \"%s\": %m", device
);
72 if (!S_ISBLK(st
.st_mode
))
73 log_info("%s is not a block device.", device
);
75 r
= probe_filesystem(device
, &detected
);
79 "Cannot reliably determine probe \"%s\", refusing to proceed." :
80 "Failed to probe \"%s\": %m",
86 log_info("%s is not empty (type %s), exiting", device
, detected
);
90 r
= makefs(type
, device
);
93 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;