]>
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
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
24 #include <sys/prctl.h>
26 #include <sys/types.h>
29 #include "alloc-util.h"
30 #include "dissect-image.h"
31 #include "signal-util.h"
32 #include "string-util.h"
34 static int makefs(const char *type
, const char *device
) {
38 if (streq(type
, "swap"))
39 mkfs
= "/sbin/mkswap";
41 mkfs
= strjoina("/sbin/mkfs.", type
);
42 if (access(mkfs
, X_OK
) != 0)
43 return log_error_errno(errno
, "%s is not executable: %m", mkfs
);
47 return log_error_errno(errno
, "fork(): %m");
50 const char *cmdline
[3] = { mkfs
, device
, NULL
};
54 (void) reset_all_signal_handlers();
55 (void) reset_signal_mask();
56 assert_se(prctl(PR_SET_PDEATHSIG
, SIGTERM
) == 0);
58 execv(cmdline
[0], (char**) cmdline
);
62 return wait_for_terminate_and_warn(mkfs
, pid
, true);
65 int main(int argc
, char *argv
[]) {
66 const char *device
, *type
;
67 _cleanup_free_
char *detected
= NULL
;
71 log_set_target(LOG_TARGET_AUTO
);
72 log_parse_environment();
76 log_error("This program expects two arguments.");
83 if (stat(device
, &st
) < 0) {
84 r
= log_error_errno(errno
, "Failed to stat \"%s\": %m", device
);
88 if (!S_ISBLK(st
.st_mode
))
89 log_info("%s is not a block device.", device
);
91 r
= probe_filesystem(device
, &detected
);
95 "Cannot reliably determine probe \"%s\", refusing to proceed." :
96 "Failed to probe \"%s\": %m",
102 log_info("%s is not empty (type %s), exiting", device
, detected
);
106 r
= makefs(type
, device
);
109 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;