Unverified Commit 1dd111eb by Christian Brauner Committed by Stéphane Graber

bdev: record output from mkfs.*

parent 185f6ea5
...@@ -736,24 +736,31 @@ int detect_fs(struct bdev *bdev, char *type, int len) ...@@ -736,24 +736,31 @@ int detect_fs(struct bdev *bdev, char *type, int len)
exit(1); exit(1);
} }
int do_mkfs(const char *path, const char *fstype) int do_mkfs_exec_wrapper(void *args)
{ {
pid_t pid; int ret;
char *mkfs;
char **data = args;
/* strlen("mkfs.")
* +
* strlen(data[0])
* +
* \0
*/
size_t len = 5 + strlen(data[0]) + 1;
if ((pid = fork()) < 0) { mkfs = malloc(len);
ERROR("error forking"); if (!mkfs)
return -1; return -1;
}
if (pid > 0)
return wait_for_pid(pid);
// If the file is not a block device, we don't want mkfs to ask ret = snprintf(mkfs, len, "mkfs.%s", data[0]);
// us about whether to proceed. if (ret < 0 || (size_t)ret >= len)
if (null_stdfds() < 0) return -1;
exit(EXIT_FAILURE);
execlp("mkfs", "mkfs", "-t", fstype, path, (char *)NULL); TRACE("executing \"%s %s\"", mkfs, data[1]);
exit(EXIT_FAILURE); execlp(mkfs, mkfs, data[1], (char *)NULL);
SYSERROR("failed to run \"%s %s \"", mkfs, data[1]);
return -1;
} }
/* /*
......
...@@ -131,7 +131,7 @@ int bdev_destroy_wrapper(void *data); ...@@ -131,7 +131,7 @@ int bdev_destroy_wrapper(void *data);
*/ */
int blk_getsize(struct bdev *bdev, uint64_t *size); int blk_getsize(struct bdev *bdev, uint64_t *size);
int detect_fs(struct bdev *bdev, char *type, int len); int detect_fs(struct bdev *bdev, char *type, int len);
int do_mkfs(const char *path, const char *fstype); int do_mkfs_exec_wrapper(void *args);
int is_blktype(struct bdev *b); int is_blktype(struct bdev *b);
int mount_unknown_fs(const char *rootfs, const char *target, int mount_unknown_fs(const char *rootfs, const char *target,
const char *options); const char *options);
......
...@@ -191,8 +191,10 @@ int loop_mount(struct bdev *bdev) ...@@ -191,8 +191,10 @@ int loop_mount(struct bdev *bdev)
src += 5; src += 5;
loopfd = lxc_prepare_loop_dev(src, loname, LO_FLAGS_AUTOCLEAR); loopfd = lxc_prepare_loop_dev(src, loname, LO_FLAGS_AUTOCLEAR);
if (loopfd < 0) if (loopfd < 0) {
ERROR("failed to prepare loop device for loop file \"%s\"", src);
return -1; return -1;
}
DEBUG("prepared loop device \"%s\"", loname); DEBUG("prepared loop device \"%s\"", loname);
ret = mount_unknown_fs(loname, bdev->dest, bdev->mntopts); ret = mount_unknown_fs(loname, bdev->dest, bdev->mntopts);
...@@ -224,6 +226,9 @@ int loop_umount(struct bdev *bdev) ...@@ -224,6 +226,9 @@ int loop_umount(struct bdev *bdev)
static int do_loop_create(const char *path, uint64_t size, const char *fstype) static int do_loop_create(const char *path, uint64_t size, const char *fstype)
{ {
int fd, ret; int fd, ret;
const char *cmd_args[2] = {fstype, path};
char cmd_output[MAXPATHLEN];
// create the new loopback file. // create the new loopback file.
fd = creat(path, S_IRUSR|S_IWUSR); fd = creat(path, S_IRUSR|S_IWUSR);
if (fd < 0) if (fd < 0)
...@@ -245,11 +250,10 @@ static int do_loop_create(const char *path, uint64_t size, const char *fstype) ...@@ -245,11 +250,10 @@ static int do_loop_create(const char *path, uint64_t size, const char *fstype)
} }
// create an fs in the loopback file // create an fs in the loopback file
if (do_mkfs(path, fstype) < 0) { ret = run_command(cmd_output, sizeof(cmd_output), do_mkfs_exec_wrapper,
ERROR("Error creating filesystem type %s on %s", fstype, (void *)cmd_args);
path); if (ret < 0)
return -1; return -1;
}
return 0; return 0;
} }
...@@ -282,6 +282,8 @@ int lvm_clonepaths(struct bdev *orig, struct bdev *new, const char *oldname, ...@@ -282,6 +282,8 @@ int lvm_clonepaths(struct bdev *orig, struct bdev *new, const char *oldname,
char fstype[100]; char fstype[100];
uint64_t size = newsize; uint64_t size = newsize;
int len, ret; int len, ret;
const char *cmd_args[2];
char cmd_output[MAXPATHLEN];
if (!orig->src || !orig->dest) if (!orig->src || !orig->dest)
return -1; return -1;
...@@ -348,11 +350,14 @@ int lvm_clonepaths(struct bdev *orig, struct bdev *new, const char *oldname, ...@@ -348,11 +350,14 @@ int lvm_clonepaths(struct bdev *orig, struct bdev *new, const char *oldname,
ERROR("Error creating new lvm blockdev"); ERROR("Error creating new lvm blockdev");
return -1; return -1;
} }
if (do_mkfs(new->src, fstype) < 0) {
ERROR("Error creating filesystem type %s on %s", fstype, cmd_args[0] = fstype;
new->src); cmd_args[1] = new->src;
// create an fs in the loopback file
ret = run_command(cmd_output, sizeof(cmd_output),
do_mkfs_exec_wrapper, (void *)cmd_args);
if (ret < 0)
return -1; return -1;
}
} }
return 0; return 0;
...@@ -378,6 +383,8 @@ int lvm_create(struct bdev *bdev, const char *dest, const char *n, ...@@ -378,6 +383,8 @@ int lvm_create(struct bdev *bdev, const char *dest, const char *n,
const char *vg, *thinpool, *fstype, *lv = n; const char *vg, *thinpool, *fstype, *lv = n;
uint64_t sz; uint64_t sz;
int ret, len; int ret, len;
const char *cmd_args[2];
char cmd_output[MAXPATHLEN];
if (!specs) if (!specs)
return -1; return -1;
...@@ -416,11 +423,14 @@ int lvm_create(struct bdev *bdev, const char *dest, const char *n, ...@@ -416,11 +423,14 @@ int lvm_create(struct bdev *bdev, const char *dest, const char *n,
fstype = specs->fstype; fstype = specs->fstype;
if (!fstype) if (!fstype)
fstype = DEFAULT_FSTYPE; fstype = DEFAULT_FSTYPE;
if (do_mkfs(bdev->src, fstype) < 0) {
ERROR("Error creating filesystem type %s on %s", fstype, cmd_args[0] = fstype;
bdev->src); cmd_args[1] = bdev->src;
ret = run_command(cmd_output, sizeof(cmd_output), do_mkfs_exec_wrapper,
(void *)cmd_args);
if (ret < 0)
return -1; return -1;
}
if (!(bdev->dest = strdup(dest))) if (!(bdev->dest = strdup(dest)))
return -1; return -1;
......
...@@ -51,6 +51,8 @@ int rbd_create(struct bdev *bdev, const char *dest, const char *n, ...@@ -51,6 +51,8 @@ int rbd_create(struct bdev *bdev, const char *dest, const char *n,
int ret, len; int ret, len;
char sz[24]; char sz[24];
pid_t pid; pid_t pid;
const char *cmd_args[2];
char cmd_output[MAXPATHLEN];
if (!specs) if (!specs)
return -1; return -1;
...@@ -104,11 +106,13 @@ int rbd_create(struct bdev *bdev, const char *dest, const char *n, ...@@ -104,11 +106,13 @@ int rbd_create(struct bdev *bdev, const char *dest, const char *n,
if (!fstype) if (!fstype)
fstype = DEFAULT_FSTYPE; fstype = DEFAULT_FSTYPE;
if (do_mkfs(bdev->src, fstype) < 0) { cmd_args[0] = fstype;
ERROR("Error creating filesystem type %s on %s", fstype, cmd_args[1] = bdev->src;
bdev->src); ret = run_command(cmd_output, sizeof(cmd_output), do_mkfs_exec_wrapper,
(void *)cmd_args);
if (ret < 0)
return -1; return -1;
}
if (!(bdev->dest = strdup(dest))) if (!(bdev->dest = strdup(dest)))
return -1; return -1;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment