]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/partition/makefs.c
dissect-image: return error if results are ambiguous
[thirdparty/systemd.git] / src / partition / makefs.c
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;
37
38 if (streq(type, "swap"))
39 mkfs = "/sbin/mkswap";
40 else
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);
44
45 pid = fork();
46 if (pid < 0)
47 return log_error_errno(errno, "fork(): %m");
48
49 if (pid == 0) {
50 const char *cmdline[3] = { mkfs, device, NULL };
51
52 /* Child */
53
54 (void) reset_all_signal_handlers();
55 (void) reset_signal_mask();
56 assert_se(prctl(PR_SET_PDEATHSIG, SIGTERM) == 0);
57
58 execv(cmdline[0], (char**) cmdline);
59 _exit(EXIT_FAILURE);
60 }
61
62 return wait_for_terminate_and_warn(mkfs, pid, true);
63 }
64
65 int main(int argc, char *argv[]) {
66 const char *device, *type;
67 _cleanup_free_ char *detected = NULL;
68 struct stat st;
69 int r;
70
71 log_set_target(LOG_TARGET_AUTO);
72 log_parse_environment();
73 log_open();
74
75 if (argc != 3) {
76 log_error("This program expects two arguments.");
77 return EXIT_FAILURE;
78 }
79
80 type = argv[1];
81 device = argv[2];
82
83 if (stat(device, &st) < 0) {
84 r = log_error_errno(errno, "Failed to stat \"%s\": %m", device);
85 goto finish;
86 }
87
88 if (!S_ISBLK(st.st_mode))
89 log_info("%s is not a block device.", device);
90
91 r = probe_filesystem(device, &detected);
92 if (r < 0) {
93 log_warning_errno(r,
94 r == -EUCLEAN ?
95 "Cannot reliably determine probe \"%s\", refusing to proceed." :
96 "Failed to probe \"%s\": %m",
97 device);
98 goto finish;
99 }
100
101 if (detected) {
102 log_info("%s is not empty (type %s), exiting", device, detected);
103 goto finish;
104 }
105
106 r = makefs(type, device);
107
108 finish:
109 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
110 }