]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/partition/makefs.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
9 #include "alloc-util.h"
10 #include "blockdev-util.h"
11 #include "dissect-image.h"
13 #include "main-func.h"
14 #include "process-util.h"
15 #include "signal-util.h"
16 #include "string-util.h"
18 static int makefs(const char *type
, const char *device
) {
23 if (streq(type
, "swap"))
24 mkfs
= "/sbin/mkswap";
26 mkfs
= strjoina("/sbin/mkfs.", type
);
27 if (access(mkfs
, X_OK
) != 0)
28 return log_error_errno(errno
, "%s is not executable: %m", mkfs
);
30 r
= safe_fork("(mkfs)", FORK_RESET_SIGNALS
|FORK_DEATHSIG
|FORK_RLIMIT_NOFILE_SAFE
|FORK_LOG
, &pid
);
34 const char *cmdline
[3] = { mkfs
, device
, NULL
};
38 execv(cmdline
[0], (char**) cmdline
);
42 return wait_for_terminate_and_check(mkfs
, pid
, WAIT_LOG
);
45 static int run(int argc
, char *argv
[]) {
46 _cleanup_free_
char *device
= NULL
, *type
= NULL
, *detected
= NULL
;
47 _cleanup_close_
int lock_fd
= -1;
54 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
55 "This program expects two arguments.");
57 /* type and device must be copied because makefs calls safe_fork, which clears argv[] */
58 type
= strdup(argv
[1]);
62 device
= strdup(argv
[2]);
66 if (stat(device
, &st
) < 0)
67 return log_error_errno(errno
, "Failed to stat \"%s\": %m", device
);
69 if (S_ISBLK(st
.st_mode
)) {
70 /* Lock the device so that udev doesn't interfere with our work */
72 lock_fd
= lock_whole_block_device(st
.st_rdev
, LOCK_EX
);
74 return log_error_errno(lock_fd
, "Failed to lock whole block device of \"%s\": %m", device
);
76 log_info("%s is not a block device.", device
);
78 r
= probe_filesystem(device
, &detected
);
80 return log_error_errno(r
, "Ambiguous results of probing for file system on \"%s\", refusing to proceed.", device
);
82 return log_error_errno(r
, "Failed to probe \"%s\": %m", device
);
84 log_info("'%s' is not empty (contains file system of type %s), exiting.", device
, detected
);
88 return makefs(type
, device
);
91 DEFINE_MAIN_FUNCTION(run
);