bdev: support -B best and -B lvm,dir

-B dev will check whether btrfs, zfs, or lvm can be used,
in that order, and fall back to dir.

-B lvm,btrfs will try lvm first, then btrfs, then fail.

Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
This commit is contained in:
Serge Hallyn 2013-08-15 12:55:50 -05:00
parent d3060bd055
commit d44e88c266
2 changed files with 41 additions and 10 deletions

View File

@ -1977,6 +1977,22 @@ struct bdev *bdev_copy(const char *src, const char *oldname, const char *cname,
exit(0);
}
static struct bdev * do_bdev_create(const char *dest, const char *type,
const char *cname, struct bdev_specs *specs)
{
struct bdev *bdev = bdev_get(type);
if (!bdev) {
return NULL;
}
if (bdev->ops->create(bdev, dest, cname, specs) < 0) {
bdev_put(bdev);
return NULL;
}
return bdev;
}
/*
* bdev_create:
* Create a backing store for a container.
@ -1992,23 +2008,35 @@ struct bdev *bdev_create(const char *dest, const char *type,
const char *cname, struct bdev_specs *specs)
{
struct bdev *bdev;
char *best_options[] = {"btrfs", "zfs", "lvm", "dir", NULL};
if (!type)
type = "dir";
bdev = bdev_get(type);
if (!bdev) {
ERROR("Unknown fs type: %s\n", type);
return NULL;
}
if (bdev->ops->create(bdev, dest, cname, specs) < 0) {
bdev_put(bdev);
return NULL;
}
return do_bdev_create(dest, "dir", cname, specs);
if (strcmp(type, "best") == 0) {
int i;
// try for the best backing store type, according to our
// opinionated preferences
for (i=0; best_options[i]; i++) {
if ((bdev = do_bdev_create(dest, best_options[i], cname, specs)))
return bdev;
}
return NULL; // 'dir' should never fail, so this shouldn't happen
}
// -B lvm,dir
if (index(type, ',') != NULL) {
char *dup = alloca(strlen(type)+1), *saveptr, *token;
strcpy(dup, type);
for (token = strtok_r(dup, ",", &saveptr); token;
token = strtok_r(NULL, ",", &saveptr)) {
if ((bdev = do_bdev_create(dest, token, cname, specs)))
return bdev;
}
}
return do_bdev_create(dest, type, cname, specs);
}
char *overlayfs_getlower(char *p)
{

View File

@ -679,8 +679,11 @@ static struct bdev *do_bdev_create(struct lxc_container *c, const char *type,
return NULL;
bdev = bdev_create(dest, type, c->name, specs);
if (!bdev)
if (!bdev) {
ERROR("Failed to create backing store type %s\n", type);
return NULL;
}
lxcapi_set_config_item(c, "lxc.rootfs", bdev->src);
return bdev;
}