storage: make storage_init() smart and simplify

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