]>
Commit | Line | Data |
---|---|---|
b7f28ac5 ZJS |
1 | /*** |
2 | SPDX-License-Identifier: LGPL-2.1+ | |
3 | ||
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2017 Zbigniew Jędrzejewski-Szmek | |
7 | ||
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. | |
12 | ||
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. | |
17 | ||
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/>. | |
20 | ***/ | |
21 | ||
22 | #include <fcntl.h> | |
23 | #include <signal.h> | |
24 | #include <sys/prctl.h> | |
25 | #include <sys/stat.h> | |
26 | #include <sys/types.h> | |
27 | #include <unistd.h> | |
28 | ||
29 | #include "alloc-util.h" | |
30 | #include "dissect-image.h" | |
31 | #include "signal-util.h" | |
32 | #include "string-util.h" | |
33 | ||
34 | static int makefs(const char *type, const char *device) { | |
35 | const char *mkfs; | |
36 | pid_t pid; | |
4c253ed1 | 37 | int r; |
b7f28ac5 ZJS |
38 | |
39 | if (streq(type, "swap")) | |
40 | mkfs = "/sbin/mkswap"; | |
41 | else | |
42 | mkfs = strjoina("/sbin/mkfs.", type); | |
43 | if (access(mkfs, X_OK) != 0) | |
44 | return log_error_errno(errno, "%s is not executable: %m", mkfs); | |
45 | ||
b6e1fff1 | 46 | r = safe_fork("(fsck)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_LOG, &pid); |
4c253ed1 | 47 | if (r < 0) |
b6e1fff1 | 48 | return r; |
4c253ed1 | 49 | if (r == 0) { |
b7f28ac5 ZJS |
50 | const char *cmdline[3] = { mkfs, device, NULL }; |
51 | ||
52 | /* Child */ | |
53 | ||
b7f28ac5 ZJS |
54 | execv(cmdline[0], (char**) cmdline); |
55 | _exit(EXIT_FAILURE); | |
56 | } | |
57 | ||
7d4904fe | 58 | return wait_for_terminate_and_check(mkfs, pid, WAIT_LOG); |
b7f28ac5 ZJS |
59 | } |
60 | ||
61 | int main(int argc, char *argv[]) { | |
62 | const char *device, *type; | |
63 | _cleanup_free_ char *detected = NULL; | |
64 | struct stat st; | |
65 | int r; | |
66 | ||
67 | log_set_target(LOG_TARGET_AUTO); | |
68 | log_parse_environment(); | |
69 | log_open(); | |
70 | ||
71 | if (argc != 3) { | |
72 | log_error("This program expects two arguments."); | |
73 | return EXIT_FAILURE; | |
74 | } | |
75 | ||
76 | type = argv[1]; | |
77 | device = argv[2]; | |
78 | ||
79 | if (stat(device, &st) < 0) { | |
80 | r = log_error_errno(errno, "Failed to stat \"%s\": %m", device); | |
81 | goto finish; | |
82 | } | |
83 | ||
84 | if (!S_ISBLK(st.st_mode)) | |
85 | log_info("%s is not a block device.", device); | |
86 | ||
87 | r = probe_filesystem(device, &detected); | |
88 | if (r < 0) { | |
7cc84b2c ZJS |
89 | log_warning_errno(r, |
90 | r == -EUCLEAN ? | |
91 | "Cannot reliably determine probe \"%s\", refusing to proceed." : | |
92 | "Failed to probe \"%s\": %m", | |
93 | device); | |
b7f28ac5 ZJS |
94 | goto finish; |
95 | } | |
96 | ||
97 | if (detected) { | |
98 | log_info("%s is not empty (type %s), exiting", device, detected); | |
99 | goto finish; | |
100 | } | |
101 | ||
102 | r = makefs(type, device); | |
103 | ||
104 | finish: | |
105 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; | |
106 | } |