makefs: port to generic make_filesystem() call

This commit is contained in:
Lennart Poettering 2020-07-29 19:10:33 +02:00
parent c95f9a2351
commit da49710ef2
1 changed files with 10 additions and 31 deletions

View File

@ -11,40 +11,15 @@
#include "dissect-image.h"
#include "fd-util.h"
#include "main-func.h"
#include "mkfs-util.h"
#include "process-util.h"
#include "signal-util.h"
#include "string-util.h"
static int makefs(const char *type, const char *device) {
const char *mkfs;
pid_t pid;
int r;
if (streq(type, "swap"))
mkfs = "/sbin/mkswap";
else
mkfs = strjoina("/sbin/mkfs.", type);
if (access(mkfs, X_OK) != 0)
return log_error_errno(errno, "%s is not executable: %m", mkfs);
r = safe_fork("(mkfs)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG, &pid);
if (r < 0)
return r;
if (r == 0) {
const char *cmdline[3] = { mkfs, device, NULL };
/* Child */
execv(cmdline[0], (char**) cmdline);
_exit(EXIT_FAILURE);
}
return wait_for_terminate_and_check(mkfs, pid, WAIT_LOG);
}
static int run(int argc, char *argv[]) {
_cleanup_free_ char *device = NULL, *type = NULL, *detected = NULL;
_cleanup_free_ char *device = NULL, *fstype = NULL, *detected = NULL;
_cleanup_close_ int lock_fd = -1;
sd_id128_t uuid;
struct stat st;
int r;
@ -55,8 +30,8 @@ static int run(int argc, char *argv[]) {
"This program expects two arguments.");
/* type and device must be copied because makefs calls safe_fork, which clears argv[] */
type = strdup(argv[1]);
if (!type)
fstype = strdup(argv[1]);
if (!fstype)
return log_oom();
device = strdup(argv[2]);
@ -85,7 +60,11 @@ static int run(int argc, char *argv[]) {
return 0;
}
return makefs(type, device);
r = sd_id128_randomize(&uuid);
if (r < 0)
return log_error_errno(r, "Failed to generate UUID for file system: %m");
return make_filesystem(device, fstype, basename(device), uuid, true);
}
DEFINE_MAIN_FUNCTION(run);