Skip to content

Commit

Permalink
storage: prefix all btrfs paths
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
  • Loading branch information
Christian Brauner committed Jul 2, 2017
1 parent e970555 commit 3f88da0
Showing 1 changed file with 47 additions and 12 deletions.
59 changes: 47 additions & 12 deletions src/lxc/bdev/lxcbtrfs.c
Expand Up @@ -194,11 +194,12 @@ int btrfs_detect(const char *path)
int btrfs_mount(struct bdev *bdev)
{
unsigned long mntflags;
char *mntdata;
char *mntdata, *src;
int ret;

if (strcmp(bdev->type, "btrfs"))
return -22;

if (!bdev->src || !bdev->dest)
return -22;

Expand All @@ -207,7 +208,9 @@ int btrfs_mount(struct bdev *bdev)
return -22;
}

ret = mount(bdev->src, bdev->dest, "bind", MS_BIND | MS_REC | mntflags, mntdata);
src = lxc_storage_get_path(bdev->src, "btrfs");

ret = mount(src, bdev->dest, "bind", MS_BIND | MS_REC | mntflags, mntdata);
free(mntdata);
return ret;
}
Expand All @@ -216,8 +219,10 @@ int btrfs_umount(struct bdev *bdev)
{
if (strcmp(bdev->type, "btrfs"))
return -22;

if (!bdev->src || !bdev->dest)
return -22;

return umount(bdev->dest);
}

Expand Down Expand Up @@ -346,7 +351,9 @@ int btrfs_snapshot(const char *orig, const char *new)

static int btrfs_snapshot_wrapper(void *data)
{
char *src;
struct rsync_data_char *arg = data;

if (setgid(0) < 0) {
ERROR("Failed to setgid to 0");
return -1;
Expand All @@ -357,14 +364,19 @@ static int btrfs_snapshot_wrapper(void *data)
ERROR("Failed to setuid to 0");
return -1;
}
return btrfs_snapshot(arg->src, arg->dest);

src = lxc_storage_get_path(arg->src, "btrfs");

return btrfs_snapshot(src, arg->dest);
}

int btrfs_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)
{
char *src;

if (!orig->dest || !orig->src)
return -1;

Expand All @@ -375,21 +387,26 @@ int btrfs_clonepaths(struct bdev *orig, struct bdev *new, const char *oldname,
orig->type);
return -1;
}
len = strlen(lxcpath) + strlen(cname) + strlen("rootfs") + 3;

len = strlen(lxcpath) + strlen(cname) + strlen("rootfs") + 6 + 3;
new->src = malloc(len);
if (!new->src)
return -1;
ret = snprintf(new->src, len, "%s/%s/rootfs", lxcpath, cname);

ret = snprintf(new->src, len, "btrfs:%s/%s/rootfs", lxcpath, cname);
if (ret < 0 || ret >= len)
return -1;
} else {
// in case rootfs is in custom path, reuse it
if ((new->src = dir_new_path(orig->src, oldname, cname, oldpath, lxcpath)) == NULL)
/* In case rootfs is in custom path, reuse it. */
new->src = dir_new_path(orig->src, oldname, cname, oldpath, lxcpath);
if (!new->src)
return -1;

}

if ((new->dest = strdup(new->src)) == NULL)
src = lxc_storage_get_path(new->src, "btrfs");
new->dest = strdup(src);
if (!new->dest)
return -1;

if (orig->mntopts && (new->mntopts = strdup(orig->mntopts)) == NULL)
Expand Down Expand Up @@ -734,21 +751,39 @@ bool btrfs_try_remove_subvol(const char *path)
{
if (!btrfs_detect(path))
return false;

return btrfs_recursive_destroy(path) == 0;
}

int btrfs_destroy(struct bdev *orig)
{
return btrfs_recursive_destroy(orig->src);
char *src;

src = lxc_storage_get_path(orig->src, "btrfs");

return btrfs_recursive_destroy(src);
}

int btrfs_create(struct bdev *bdev, const char *dest, const char *n,
struct bdev_specs *specs)
{
bdev->src = strdup(dest);
int ret;
size_t len;

len = strlen(dest) + 1;
/* strlen("btrfs:") */
len += 6;
bdev->src = malloc(len);
if (!bdev->src)
return -1;

ret = snprintf(bdev->src, len, "btrfs:%s", dest);
if (ret < 0 || (size_t)ret >= len)
return -1;

bdev->dest = strdup(dest);
if (!bdev->src || !bdev->dest)
if (!bdev->dest)
return -1;

return btrfs_subvolume_create(bdev->dest);
}

0 comments on commit 3f88da0

Please sign in to comment.