storage: make storage_init() smart and simplify

parent 068aa488
......@@ -1204,7 +1204,7 @@ static int lxc_setup_rootfs(struct lxc_conf *conf)
return -1;
}
bdev = storage_init(conf, rootfs->path, rootfs->mount, rootfs->options);
bdev = storage_init(conf);
if (!bdev) {
ERROR("Failed to mount rootfs \"%s\" onto \"%s\" with options \"%s\".",
rootfs->path, rootfs->mount,
......
......@@ -1250,7 +1250,7 @@ static bool create_run_template(struct lxc_container *c, char *tpath, bool need_
exit(1);
}
bdev = storage_init(c->lxc_conf, c->lxc_conf->rootfs.path, c->lxc_conf->rootfs.mount, NULL);
bdev = storage_init(c->lxc_conf);
if (!bdev) {
ERROR("Error opening rootfs");
exit(1);
......@@ -3326,7 +3326,7 @@ static int clone_update_rootfs(struct clone_update_data *data)
if (unshare(CLONE_NEWNS) < 0)
return -1;
bdev = storage_init(c->lxc_conf, c->lxc_conf->rootfs.path, c->lxc_conf->rootfs.mount, NULL);
bdev = storage_init(c->lxc_conf);
if (!bdev)
return -1;
if (strcmp(bdev->type, "dir") != 0) {
......@@ -3657,7 +3657,7 @@ static bool do_lxcapi_rename(struct lxc_container *c, const char *newname)
ERROR("Renaming a container with snapshots is not supported");
return false;
}
bdev = storage_init(c->lxc_conf, c->lxc_conf->rootfs.path, c->lxc_conf->rootfs.mount, NULL);
bdev = storage_init(c->lxc_conf);
if (!bdev) {
ERROR("Failed to find original backing store type");
return false;
......@@ -4007,8 +4007,7 @@ static bool do_lxcapi_snapshot_restore(struct lxc_container *c, const char *snap
return false;
}
bdev = storage_init(c->lxc_conf, c->lxc_conf->rootfs.path,
c->lxc_conf->rootfs.mount, NULL);
bdev = storage_init(c->lxc_conf);
if (!bdev) {
ERROR("Failed to find original backing store type");
return false;
......
......@@ -213,7 +213,7 @@ static const struct lxc_storage_type bdevs[] = {
static const size_t numbdevs = sizeof(bdevs) / sizeof(struct lxc_storage_type);
static const struct lxc_storage_type *get_storage_by_name(const char *name,
static const struct lxc_storage_type *get_storage_by_name(const char *path,
const char *type)
{
int ret;
......@@ -222,7 +222,7 @@ static const struct lxc_storage_type *get_storage_by_name(const char *name,
if (type)
cmplen = strlen(type);
else
cmplen = strcspn(name, ":");
cmplen = strcspn(path, ":");
if (cmplen == 0)
return NULL;
......@@ -230,7 +230,7 @@ static const struct lxc_storage_type *get_storage_by_name(const char *name,
if (type)
ret = strncmp(bdevs[i].name, type, cmplen);
else
ret = strncmp(bdevs[i].name, name, cmplen);
ret = strncmp(bdevs[i].name, path, cmplen);
if (ret == 0)
break;
}
......@@ -242,18 +242,19 @@ static const struct lxc_storage_type *get_storage_by_name(const char *name,
return &bdevs[i];
}
const struct lxc_storage_type *storage_query(struct lxc_conf *conf,
const char *src)
static const struct lxc_storage_type *storage_query(struct lxc_conf *conf)
{
size_t i;
const struct lxc_storage_type *bdev;
const char *path = conf->rootfs.path;
const char *type = conf->rootfs.bdev_type;
bdev = get_storage_by_name(src, conf->rootfs.bdev_type);
bdev = get_storage_by_name(path, type);
if (bdev)
return bdev;
for (i = 0; i < numbdevs; i++)
if (bdevs[i].ops->detect(src))
if (bdevs[i].ops->detect(path))
break;
if (i == numbdevs)
......@@ -316,9 +317,10 @@ static struct lxc_storage *do_storage_create(const char *dest, const char *type,
bool storage_can_backup(struct lxc_conf *conf)
{
struct lxc_storage *bdev = storage_init(conf, NULL, NULL, NULL);
bool ret;
struct lxc_storage *bdev;
bdev = storage_init(conf);
if (!bdev)
return false;
......@@ -361,7 +363,7 @@ struct lxc_storage *storage_copy(struct lxc_container *c, const char *cname,
return NULL;
}
orig = storage_init(c->lxc_conf, src, NULL, NULL);
orig = storage_init(c->lxc_conf);
if (!orig) {
ERROR("Failed to detect storage driver for \"%s\"", src);
return NULL;
......@@ -597,32 +599,32 @@ bool storage_destroy(struct lxc_conf *conf)
struct lxc_storage *r;
bool ret = false;
r = storage_init(conf, conf->rootfs.path, conf->rootfs.mount, NULL);
r = storage_init(conf);
if (!r)
return ret;
if (r->ops->destroy(r) == 0)
ret = r->ops->destroy(r);
if (ret == 0)
ret = true;
storage_put(r);
return ret;
}
struct lxc_storage *storage_init(struct lxc_conf *conf, const char *src,
const char *dst, const char *mntopts)
struct lxc_storage *storage_init(struct lxc_conf *conf)
{
struct lxc_storage *bdev;
const struct lxc_storage_type *q;
const char *src = conf->rootfs.path;
const char *dst = conf->rootfs.mount;
const char *mntopts = conf->rootfs.options;
BUILD_BUG_ON(LXC_STORAGE_INTERNAL_OVERLAY_RESTORE <= LXC_CLONE_MAXFLAGS);
if (!src)
src = conf->rootfs.path;
if (!src)
return NULL;
q = storage_query(conf, src);
q = storage_query(conf);
if (!q)
return NULL;
......@@ -631,18 +633,23 @@ struct lxc_storage *storage_init(struct lxc_conf *conf, const char *src,
return NULL;
memset(bdev, 0, sizeof(struct lxc_storage));
bdev->ops = q->ops;
bdev->type = q->name;
if (mntopts)
bdev->mntopts = strdup(mntopts);
if (src)
bdev->src = strdup(src);
if (dst)
bdev->dest = strdup(dst);
if (strcmp(bdev->type, "nbd") == 0)
bdev->nbd_idx = conf->nbd_idx;
if (!strcmp(bdev->type, "aufs"))
if (strcmp(bdev->type, "aufs") == 0)
WARN("The \"aufs\" driver will is deprecated and will soon be "
"removed. For similar functionality see the \"overlay\" "
"storage driver");
......@@ -659,7 +666,7 @@ bool storage_is_dir(struct lxc_conf *conf)
if (type)
return (strcmp(type, "dir") == 0);
orig = storage_init(conf, conf->lxc_rootfs.path, NULL, NULL);
orig = storage_init(conf);
if (!orig)
return bret;
......@@ -692,7 +699,7 @@ bool rootfs_is_blockdev(struct lxc_conf *conf)
if (ret == 0 && S_ISBLK(st.st_mode))
return true;
q = storage_query(conf, conf->rootfs.path);
q = storage_query(conf);
if (!q)
return false;
......
......@@ -124,18 +124,7 @@ struct lxc_storage {
extern bool storage_is_dir(struct lxc_conf *conf);
extern bool storage_can_backup(struct lxc_conf *conf);
/* Instantiate a lxc_storage object. The src is used to determine which blockdev
* type this should be. The dst and data are optional, and will be used in case
* of mount/umount.
*
* The source will be "dir:/var/lib/lxc/c1" or "lvm:/dev/lxc/c1". For other
* backing stores, this will allow additional options. In particular,
* "overlayfs:/var/lib/lxc/canonical/rootfs:/var/lib/lxc/c1/delta" will mean use
* /var/lib/lxc/canonical/rootfs as lower dir, and /var/lib/lxc/c1/delta as the
* upper, writeable layer.
*/
extern struct lxc_storage *storage_init(struct lxc_conf *conf, const char *src,
const char *dst, const char *data);
extern struct lxc_storage *storage_init(struct lxc_conf *conf);
extern struct lxc_storage *storage_copy(struct lxc_container *c0,
const char *cname, const char *lxcpath,
......
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