Pass through all options with -Bbest.

Remove the union in bdev_specs and store all options if -Bbest is passed. Fixes issue #31.

Signed-off-by: Sidnei da Silva <sidnei.da.silva@canonical.com>
Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
This commit is contained in:
Sidnei da Silva 2013-10-22 19:52:30 -02:00 committed by Serge Hallyn
parent 3ffe454baf
commit 72e99249b0
3 changed files with 68 additions and 69 deletions

View File

@ -707,10 +707,10 @@ static int zfs_create(struct bdev *bdev, const char *dest, const char *n,
int ret; int ret;
pid_t pid; pid_t pid;
if (!specs || !specs->u.zfs.zfsroot) if (!specs || !specs->zfs.zfsroot)
zfsroot = default_zfs_root(); zfsroot = default_zfs_root();
else else
zfsroot = specs->u.zfs.zfsroot; zfsroot = specs->zfs.zfsroot;
if (!(bdev->dest = strdup(dest))) { if (!(bdev->dest = strdup(dest))) {
ERROR("No mount target specified or out of memory"); ERROR("No mount target specified or out of memory");
@ -873,7 +873,7 @@ static int lvm_is_thin_pool(const char *path)
static int do_lvm_create(const char *path, unsigned long size, const char *thinpool) static int do_lvm_create(const char *path, unsigned long size, const char *thinpool)
{ {
int ret, pid, len; int ret, pid, len;
char sz[24], *pathdup, *vg, *lv, *tp; char sz[24], *pathdup, *vg, *lv, *tp = NULL;
if ((pid = fork()) < 0) { if ((pid = fork()) < 0) {
SYSERROR("failed fork"); SYSERROR("failed fork");
@ -901,33 +901,37 @@ static int do_lvm_create(const char *path, unsigned long size, const char *thinp
lv++; lv++;
vg = strrchr(pathdup, '/'); vg = strrchr(pathdup, '/');
if (!vg) if (!vg) {
free(pathdup);
exit(1); exit(1);
}
vg++; vg++;
if (thinpool) { if (thinpool) {
len = strlen(pathdup) + strlen(thinpool) + 2; len = strlen(pathdup) + strlen(thinpool) + 2;
tp = alloca(len); tp = alloca(len);
INFO("checking for thin pool at path: %s", tp);
ret = snprintf(tp, len, "%s/%s", pathdup, thinpool); ret = snprintf(tp, len, "%s/%s", pathdup, thinpool);
if (ret < 0 || ret >= len) if (ret < 0 || ret >= len) {
return -1; free(pathdup);
exit(1);
}
ret = lvm_is_thin_pool(tp); ret = lvm_is_thin_pool(tp);
INFO("got %d for thin pool at path: %s", ret, tp); INFO("got %d for thin pool at path: %s", ret, tp);
if (ret < 0) if (ret < 0) {
return ret; free(pathdup);
exit(1);
}
if (!ret) if (!ret)
thinpool = NULL; tp = NULL;
} }
if (!tp) {
if (!thinpool) {
execlp("lvcreate", "lvcreate", "-L", sz, vg, "-n", lv, (char *)NULL); execlp("lvcreate", "lvcreate", "-L", sz, vg, "-n", lv, (char *)NULL);
} else { } else {
execlp("lvcreate", "lvcreate", "--thinpool", thinpool, "-V", sz, vg, "-n", lv, (char *)NULL); execlp("lvcreate", "lvcreate", "--thinpool", tp, "-V", sz, vg, "-n", lv, (char *)NULL);
} }
free(pathdup); free(pathdup);
@ -1097,17 +1101,18 @@ static int lvm_create(struct bdev *bdev, const char *dest, const char *n,
if (!specs) if (!specs)
return -1; return -1;
vg = specs->u.lvm.vg; vg = specs->lvm.vg;
if (!vg) if (!vg)
vg = default_lvm_vg(); vg = default_lvm_vg();
thinpool = specs->u.lvm.thinpool; thinpool = specs->lvm.thinpool;
if (!thinpool) if (!thinpool)
thinpool = default_lvm_thin_pool(); thinpool = default_lvm_thin_pool();
/* /dev/$vg/$lv */ /* /dev/$vg/$lv */
if (specs->u.lvm.lv) if (specs->lvm.lv)
lv = specs->u.lvm.lv; lv = specs->lvm.lv;
len = strlen(vg) + strlen(lv) + 7; len = strlen(vg) + strlen(lv) + 7;
bdev->src = malloc(len); bdev->src = malloc(len);
if (!bdev->src) if (!bdev->src)
@ -1117,8 +1122,8 @@ static int lvm_create(struct bdev *bdev, const char *dest, const char *n,
if (ret < 0 || ret >= len) if (ret < 0 || ret >= len)
return -1; return -1;
// lvm.fssize is in bytes. // fssize is in bytes.
sz = specs->u.lvm.fssize; sz = specs->fssize;
if (!sz) if (!sz)
sz = DEFAULT_FS_SIZE; sz = DEFAULT_FS_SIZE;
@ -1127,7 +1132,7 @@ static int lvm_create(struct bdev *bdev, const char *dest, const char *n,
return -1; return -1;
} }
fstype = specs->u.lvm.fstype; fstype = specs->fstype;
if (!fstype) if (!fstype)
fstype = DEFAULT_FSTYPE; fstype = DEFAULT_FSTYPE;
if (do_mkfs(bdev->src, fstype) < 0) { if (do_mkfs(bdev->src, fstype) < 0) {
@ -1721,11 +1726,11 @@ static int loop_create(struct bdev *bdev, const char *dest, const char *n,
if (ret < 0 || ret >= len + 5) if (ret < 0 || ret >= len + 5)
return -1; return -1;
sz = specs->u.loop.fssize; sz = specs->fssize;
if (!sz) if (!sz)
sz = DEFAULT_FS_SIZE; sz = DEFAULT_FS_SIZE;
fstype = specs->u.loop.fstype; fstype = specs->fstype;
if (!fstype) if (!fstype)
fstype = DEFAULT_FSTYPE; fstype = DEFAULT_FSTYPE;

View File

@ -37,22 +37,16 @@ struct bdev;
* specifications for how to create a new backing store * specifications for how to create a new backing store
*/ */
struct bdev_specs { struct bdev_specs {
union { char *fstype;
struct { unsigned long fssize; // fs size in bytes
char *zfsroot; struct {
} zfs; char *zfsroot;
struct { } zfs;
char *vg; struct {
char *lv; char *vg;
char *fstype; char *lv;
unsigned long fssize; // fs size in bytes char *thinpool; // lvm thin pool to use, if any
char *thinpool; // lvm thin pool to use, if any } lvm;
} lvm;
struct {
char *fstype;
unsigned long fssize; // fs size in bytes
} loop;
} u;
}; };
struct bdev_ops { struct bdev_ops {

View File

@ -147,23 +147,25 @@ Options :\n\
bool validate_bdev_args(struct lxc_arguments *a) bool validate_bdev_args(struct lxc_arguments *a)
{ {
if (a->fstype || a->fssize) { if (strcmp(a->bdevtype, "best") != 0) {
if (strcmp(a->bdevtype, "lvm") != 0 && if (a->fstype || a->fssize) {
strcmp(a->bdevtype, "loop") != 0) { if (strcmp(a->bdevtype, "lvm") != 0 &&
fprintf(stderr, "filesystem type and size are only valid with block devices\n"); strcmp(a->bdevtype, "loop") != 0) {
return false; fprintf(stderr, "filesystem type and size are only valid with block devices\n");
return false;
}
} }
} if (strcmp(a->bdevtype, "lvm") != 0) {
if (strcmp(a->bdevtype, "lvm") != 0) { if (a->lvname || a->vgname || a->thinpool) {
if (a->lvname || a->vgname || a->thinpool) { fprintf(stderr, "--lvname, --vgname and --thinpool are only valid with -B lvm\n");
fprintf(stderr, "--lvname, --vgname and --thinpool are only valid with -B lvm\n"); return false;
return false; }
} }
} if (strcmp(a->bdevtype, "zfs") != 0) {
if (strcmp(a->bdevtype, "zfs") != 0) { if (a->zfsroot) {
if (a->zfsroot) { fprintf(stderr, "zfsroot is only valid with -B zfs\n");
fprintf(stderr, "zfsroot is only valid with -B zfs\n"); return false;
return false; }
} }
} }
return true; return true;
@ -217,26 +219,24 @@ int main(int argc, char *argv[])
else else
c->load_config(c, LXC_DEFAULT_CONFIG); c->load_config(c, LXC_DEFAULT_CONFIG);
if (strcmp(my_args.bdevtype, "zfs") == 0) { if (my_args.fstype)
spec.fstype = my_args.fstype;
if (my_args.fssize)
spec.fssize = my_args.fssize;
if (strcmp(my_args.bdevtype, "zfs") == 0 || strcmp(my_args.bdevtype, "best") == 0) {
if (my_args.zfsroot) if (my_args.zfsroot)
spec.u.zfs.zfsroot = my_args.zfsroot; spec.zfs.zfsroot = my_args.zfsroot;
} else if (strcmp(my_args.bdevtype, "lvm") == 0) { }
if (strcmp(my_args.bdevtype, "lvm") == 0 || strcmp(my_args.bdevtype, "best") == 0) {
if (my_args.lvname) if (my_args.lvname)
spec.u.lvm.lv = my_args.lvname; spec.lvm.lv = my_args.lvname;
if (my_args.vgname) if (my_args.vgname)
spec.u.lvm.vg = my_args.vgname; spec.lvm.vg = my_args.vgname;
if (my_args.thinpool) if (my_args.thinpool)
spec.u.lvm.thinpool = my_args.thinpool; spec.lvm.thinpool = my_args.thinpool;
if (my_args.fstype) }
spec.u.lvm.fstype = my_args.fstype; if (my_args.dir) {
if (my_args.fssize)
spec.u.lvm.fssize = my_args.fssize;
} else if (strcmp(my_args.bdevtype, "loop") == 0) {
if (my_args.fstype)
spec.u.loop.fstype = my_args.fstype;
if (my_args.fssize)
spec.u.loop.fssize = my_args.fssize;
} else if (my_args.dir) {
ERROR("--dir is not yet supported"); ERROR("--dir is not yet supported");
exit(1); exit(1);
} }