storage: make detect method return bool

parent 3b0e906f
......@@ -61,7 +61,7 @@ struct bdev;
struct bdev_ops {
/* detect whether path is of this bdev type */
int (*detect)(const char *path);
bool (*detect)(const char *path);
// mount requires src and dest to be set.
int (*mount)(struct bdev *bdev);
int (*umount)(struct bdev *bdev);
......
......@@ -227,12 +227,12 @@ int aufs_destroy(struct bdev *orig)
return lxc_rmdir_onedev(upper, NULL);
}
int aufs_detect(const char *path)
bool aufs_detect(const char *path)
{
if (!strncmp(path, "aufs:", 5))
return 1;
return true;
return 0;
return false;
}
int aufs_mount(struct bdev *bdev)
......
......@@ -25,6 +25,7 @@
#define __LXC_AUFS_H
#define _GNU_SOURCE
#include <stdbool.h>
#include <stdint.h>
#if IS_BIONIC
......@@ -54,7 +55,7 @@ int aufs_clonepaths(struct bdev *orig, struct bdev *new, const char *oldname,
int aufs_create(struct bdev *bdev, const char *dest, const char *n,
struct bdev_specs *specs);
int aufs_destroy(struct bdev *orig);
int aufs_detect(const char *path);
bool aufs_detect(const char *path);
int aufs_mount(struct bdev *bdev);
int aufs_umount(struct bdev *bdev);
......
......@@ -166,26 +166,26 @@ int is_btrfs_subvol(const char *path)
return stfs.f_type == BTRFS_SUPER_MAGIC;
}
int btrfs_detect(const char *path)
bool btrfs_detect(const char *path)
{
struct stat st;
int ret;
if (!strncmp(path, "btrfs:", 6))
return 1;
return true;
if (!is_btrfs_fs(path))
return 0;
return false;
/* make sure it's a subvolume */
ret = stat(path, &st);
if (ret < 0)
return 0;
return false;
if (st.st_ino == 256 && S_ISDIR(st.st_mode))
return 1;
return true;
return 0;
return false;
}
int btrfs_mount(struct bdev *bdev)
......
......@@ -397,7 +397,7 @@ int btrfs_clonepaths(struct bdev *orig, struct bdev *new, const char *oldname,
int btrfs_create(struct bdev *bdev, const char *dest, const char *n,
struct bdev_specs *specs);
int btrfs_destroy(struct bdev *orig);
int btrfs_detect(const char *path);
bool btrfs_detect(const char *path);
int btrfs_mount(struct bdev *bdev);
int btrfs_umount(struct bdev *bdev);
......
......@@ -134,15 +134,15 @@ int dir_destroy(struct bdev *orig)
return 0;
}
int dir_detect(const char *path)
bool dir_detect(const char *path)
{
if (!strncmp(path, "dir:", 4))
return 1;
return true;
if (is_dir(path))
return 1;
return true;
return 0;
return false;
}
int dir_mount(struct bdev *bdev)
......
......@@ -25,6 +25,7 @@
#define __LXC_DIR_H
#define _GNU_SOURCE
#include <stdbool.h>
#include <stdint.h>
/* defined in bdev.h */
......@@ -45,7 +46,7 @@ int dir_clonepaths(struct bdev *orig, struct bdev *new, const char *oldname,
int dir_create(struct bdev *bdev, const char *dest, const char *n,
struct bdev_specs *specs);
int dir_destroy(struct bdev *orig);
int dir_detect(const char *path);
bool dir_detect(const char *path);
int dir_mount(struct bdev *bdev);
int dir_umount(struct bdev *bdev);
......
......@@ -206,22 +206,22 @@ int loop_destroy(struct bdev *orig) {
return unlink(orig->src + 5);
}
int loop_detect(const char *path)
bool loop_detect(const char *path)
{
int ret;
struct stat s;
if (!strncmp(path, "loop:", 5))
return 1;
return true;
ret = stat(path, &s);
if (ret < 0)
return 0;
return false;
if (__S_ISTYPE(s.st_mode, S_IFREG))
return 1;
return true;
return 0;
return false;
}
int loop_mount(struct bdev *bdev)
......
......@@ -25,6 +25,7 @@
#define __LXC_LOOP_H
#define _GNU_SOURCE
#include <stdbool.h>
#include <stdint.h>
/* defined in bdev.h */
......@@ -45,7 +46,7 @@ int loop_clonepaths(struct bdev *orig, struct bdev *new, const char *oldname,
int loop_create(struct bdev *bdev, const char *dest, const char *n,
struct bdev_specs *specs);
int loop_destroy(struct bdev *orig);
int loop_detect(const char *path);
bool loop_detect(const char *path);
int loop_mount(struct bdev *bdev);
int loop_umount(struct bdev *bdev);
......
......@@ -193,7 +193,7 @@ static int do_lvm_create(const char *path, uint64_t size, const char *thinpool)
/* Look at "/sys/dev/block/maj:min/dm/uuid". If it contains the hardcoded LVM
* prefix "LVM-" then this is an lvm2 LV.
*/
int lvm_detect(const char *path)
bool lvm_detect(const char *path)
{
int fd;
ssize_t ret;
......@@ -201,35 +201,35 @@ int lvm_detect(const char *path)
char devp[MAXPATHLEN], buf[4];
if (!strncmp(path, "lvm:", 4))
return 1;
return true;
ret = stat(path, &statbuf);
if (ret < 0)
return 0;
return false;
if (!S_ISBLK(statbuf.st_mode))
return 0;
return false;
ret = snprintf(devp, MAXPATHLEN, "/sys/dev/block/%d:%d/dm/uuid",
major(statbuf.st_rdev), minor(statbuf.st_rdev));
if (ret < 0 || ret >= MAXPATHLEN) {
ERROR("Failed to create string");
return 0;
return false;
}
fd = open(devp, O_RDONLY);
if (fd < 0)
return 0;
return false;
ret = read(fd, buf, sizeof(buf));
close(fd);
if (ret != sizeof(buf))
return 0;
return false;
if (strncmp(buf, "LVM-", 4))
return 0;
return false;
return 1;
return true;
}
int lvm_mount(struct bdev *bdev)
......
......@@ -40,7 +40,7 @@ struct lxc_conf;
/*
* Functions associated with an lvm bdev struct.
*/
int lvm_detect(const char *path);
bool lvm_detect(const char *path);
int lvm_mount(struct bdev *bdev);
int lvm_umount(struct bdev *bdev);
int lvm_compare_lv_attr(const char *path, int pos, const char expected);
......
......@@ -106,12 +106,12 @@ int nbd_destroy(struct bdev *orig)
return -ENOSYS;
}
int nbd_detect(const char *path)
bool nbd_detect(const char *path)
{
if (!strncmp(path, "nbd:", 4))
return 1;
return true;
return 0;
return false;
}
int nbd_mount(struct bdev *bdev)
......
......@@ -46,7 +46,7 @@ int nbd_clonepaths(struct bdev *orig, struct bdev *new, const char *oldname,
int nbd_create(struct bdev *bdev, const char *dest, const char *n,
struct bdev_specs *specs);
int nbd_destroy(struct bdev *orig);
int nbd_detect(const char *path);
bool nbd_detect(const char *path);
int nbd_mount(struct bdev *bdev);
int nbd_umount(struct bdev *bdev);
......
......@@ -332,15 +332,15 @@ int ovl_destroy(struct bdev *orig)
return lxc_rmdir_onedev(upper, NULL);
}
int ovl_detect(const char *path)
bool ovl_detect(const char *path)
{
if (!strncmp(path, "overlayfs:", 10))
return 1;
return true;
if (!strncmp(path, "overlay:", 8))
return 1;
return true;
return 0;
return false;
}
int ovl_mount(struct bdev *bdev)
......
......@@ -25,6 +25,7 @@
#define __LXC_OVERLAY_H
#include <grp.h>
#include <stdbool.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/types.h>
......@@ -56,7 +57,7 @@ int ovl_clonepaths(struct bdev *orig, struct bdev *new, const char *oldname,
int ovl_create(struct bdev *bdev, const char *dest, const char *n,
struct bdev_specs *specs);
int ovl_destroy(struct bdev *orig);
int ovl_detect(const char *path);
bool ovl_detect(const char *path);
int ovl_mount(struct bdev *bdev);
int ovl_umount(struct bdev *bdev);
......
......@@ -219,15 +219,15 @@ int rbd_destroy(struct bdev *orig)
return 0;
}
int rbd_detect(const char *path)
bool rbd_detect(const char *path)
{
if (!strncmp(path, "rbd:", 4))
return 1;
return true;
if (!strncmp(path, "/dev/rbd/", 9))
return 1;
return true;
return 0;
return false;
}
int rbd_mount(struct bdev *bdev)
......
......@@ -25,6 +25,7 @@
#define __LXC_RDB_H
#define _GNU_SOURCE
#include <stdbool.h>
#include <stdint.h>
/* defined in bdev.h */
......@@ -45,7 +46,7 @@ int rbd_clonepaths(struct bdev *orig, struct bdev *new, const char *oldname,
int rbd_create(struct bdev *bdev, const char *dest, const char *n,
struct bdev_specs *specs);
int rbd_destroy(struct bdev *orig);
int rbd_detect(const char *path);
bool rbd_detect(const char *path);
int rbd_mount(struct bdev *bdev);
int rbd_umount(struct bdev *bdev);
......
......@@ -22,12 +22,12 @@
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/mount.h>
#include <unistd.h>
#include "bdev.h"
#include "config.h"
......@@ -37,50 +37,48 @@
lxc_log_define(lxczfs, lxc);
/*
* zfs ops:
* There are two ways we could do this. We could always specify the 'zfs device'
/* There are two ways we could do this. We could always specify the 'zfs device'
* (i.e. tank/lxc lxc/container) as rootfs. But instead (at least right now) we
* have lxc-create specify $lxcpath/$lxcname/rootfs as the mountpoint, so that
* have lxc-create specify <lxcpath>/<lxcname>/rootfs as the mountpoint, so that
* it is always mounted. That means 'mount' is really never needed and could be
* noop, but for the sake of flexibility let's always bind-mount.
*/
int zfs_list_entry(const char *path, char *output, size_t inlen)
static bool zfs_list_entry(const char *path, char *output, size_t inlen)
{
struct lxc_popen_FILE *f;
int found=0;
bool found = false;
f = lxc_popen("zfs list 2> /dev/null");
if (f == NULL) {
SYSERROR("popen failed");
return 0;
return false;
}
while (fgets(output, inlen, f->f)) {
if (strstr(output, path)) {
found = 1;
found = true;
break;
}
}
(void) lxc_pclose(f);
(void)lxc_pclose(f);
return found;
}
int zfs_detect(const char *path)
bool zfs_detect(const char *path)
{
if (!strncmp(path, "zfs:", 4))
return 1;
return true;
char *output = malloc(LXC_LOG_BUFFER_SIZE);
if (!output) {
ERROR("out of memory");
return 0;
return false;
}
int found = zfs_list_entry(path, output, LXC_LOG_BUFFER_SIZE);
bool found = zfs_list_entry(path, output, LXC_LOG_BUFFER_SIZE);
free(output);
return found;
......@@ -104,7 +102,8 @@ int zfs_mount(struct bdev *bdev)
}
src = lxc_storage_get_path(bdev->src, bdev->type);
ret = mount(src, bdev->dest, "bind", MS_BIND | MS_REC | mntflags, mntdata);
ret = mount(src, bdev->dest, "bind", MS_BIND | MS_REC | mntflags,
mntdata);
free(mntdata);
return ret;
......@@ -122,7 +121,7 @@ int zfs_umount(struct bdev *bdev)
}
int zfs_clone(const char *opath, const char *npath, const char *oname,
const char *nname, const char *lxcpath, int snapshot)
const char *nname, const char *lxcpath, int snapshot)
{
// use the 'zfs list | grep opath' entry to get the zfsroot
char output[MAXPATHLEN], option[MAXPATHLEN];
......@@ -144,8 +143,9 @@ int zfs_clone(const char *opath, const char *npath, const char *oname,
zfsroot = lxc_global_config_value("lxc.bdev.zfs.root");
}
ret = snprintf(option, MAXPATHLEN, "-omountpoint=%s/%s/rootfs", lxcpath, nname);
if (ret < 0 || ret >= MAXPATHLEN)
ret = snprintf(option, MAXPATHLEN, "-omountpoint=%s/%s/rootfs", lxcpath,
nname);
if (ret < 0 || ret >= MAXPATHLEN)
return -1;
// zfs create -omountpoint=$lxcpath/$lxcname $zfsroot/$nname
......@@ -154,10 +154,12 @@ int zfs_clone(const char *opath, const char *npath, const char *oname,
return -1;
if (!pid) {
char dev[MAXPATHLEN];
ret = snprintf(dev, MAXPATHLEN, "%s/%s", zfsroot, nname);
if (ret < 0 || ret >= MAXPATHLEN)
ret =
snprintf(dev, MAXPATHLEN, "%s/%s", zfsroot, nname);
if (ret < 0 || ret >= MAXPATHLEN)
exit(EXIT_FAILURE);
execlp("zfs", "zfs", "create", option, dev, (char *)NULL);
execlp("zfs", "zfs", "create", option, dev,
(char *)NULL);
exit(EXIT_FAILURE);
}
return wait_for_pid(pid);
......@@ -167,11 +169,11 @@ int zfs_clone(const char *opath, const char *npath, const char *oname,
// zfs clone zfsroot/oname@nname zfsroot/nname
char path1[MAXPATHLEN], path2[MAXPATHLEN];
ret = snprintf(path1, MAXPATHLEN, "%s/%s@%s", zfsroot,
oname, nname);
ret = snprintf(path1, MAXPATHLEN, "%s/%s@%s", zfsroot, oname,
nname);
if (ret < 0 || ret >= MAXPATHLEN)
return -1;
(void) snprintf(path2, MAXPATHLEN, "%s/%s", zfsroot, nname);
(void)snprintf(path2, MAXPATHLEN, "%s/%s", zfsroot, nname);
// if the snapshot exists, delete it
if ((pid = fork()) < 0)
......@@ -184,7 +186,7 @@ int zfs_clone(const char *opath, const char *npath, const char *oname,
exit(EXIT_FAILURE);
}
// it probably doesn't exist so destroy probably will fail.
(void) wait_for_pid(pid);
(void)wait_for_pid(pid);
// run first (snapshot) command
if ((pid = fork()) < 0)
......@@ -200,7 +202,8 @@ int zfs_clone(const char *opath, const char *npath, const char *oname,
if ((pid = fork()) < 0)
return -1;
if (!pid) {
execlp("zfs", "zfs", "clone", option, path1, path2, (char *)NULL);
execlp("zfs", "zfs", "clone", option, path1, path2,
(char *)NULL);
exit(EXIT_FAILURE);
}
return wait_for_pid(pid);
......@@ -208,8 +211,8 @@ int zfs_clone(const char *opath, const char *npath, const char *oname,
}
int zfs_clonepaths(struct bdev *orig, struct bdev *new, const char *oldname,
const char *cname, const char *oldpath, const char *lxcpath, int snap,
uint64_t newsize, struct lxc_conf *conf)
const char *cname, const char *oldpath, const char *lxcpath,
int snap, uint64_t newsize, struct lxc_conf *conf)
{
char *origsrc, *newsrc;
int len, ret;
......@@ -218,7 +221,8 @@ int zfs_clonepaths(struct bdev *orig, struct bdev *new, const char *oldname,
return -1;
if (snap && strcmp(orig->type, "zfs")) {
ERROR("zfs snapshot from %s backing store is not supported", orig->type);
ERROR("zfs snapshot from %s backing store is not supported",
orig->type);
return -1;
}
......@@ -286,7 +290,7 @@ int zfs_create_exec_wrapper(void *args)
}
int zfs_create(struct bdev *bdev, const char *dest, const char *n,
struct bdev_specs *specs)
struct bdev_specs *specs)
{
const char *zfsroot;
char cmd_output[MAXPATHLEN], dev[MAXPATHLEN], option[MAXPATHLEN];
......@@ -317,11 +321,11 @@ int zfs_create(struct bdev *bdev, const char *dest, const char *n,
return -1;
ret = snprintf(option, MAXPATHLEN, "-omountpoint=%s", bdev->dest);
if (ret < 0 || ret >= MAXPATHLEN)
if (ret < 0 || ret >= MAXPATHLEN)
return -1;
ret = snprintf(dev, MAXPATHLEN, "%s/%s", zfsroot, n);
if (ret < 0 || ret >= MAXPATHLEN)
if (ret < 0 || ret >= MAXPATHLEN)
return -1;
cmd_args.options = option;
......@@ -329,6 +333,7 @@ int zfs_create(struct bdev *bdev, const char *dest, const char *n,
ret = run_command(cmd_output, sizeof(cmd_output),
zfs_create_exec_wrapper, (void *)&cmd_args);
if (ret < 0)
ERROR("Failed to create zfs dataset \"%s\": %s", dev, cmd_output);
ERROR("Failed to create zfs dataset \"%s\": %s", dev,
cmd_output);
return ret;
}
......@@ -53,8 +53,7 @@ int zfs_create(struct bdev *bdev, const char *dest, const char *n,
* container busy.
*/
int zfs_destroy(struct bdev *orig);
int zfs_detect(const char *path);
int zfs_list_entry(const char *path, char *output, size_t inlen);
bool zfs_detect(const char *path);
int zfs_mount(struct bdev *bdev);
int zfs_umount(struct bdev *bdev);
......
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