From: Christian Brauner Date: Wed, 30 Dec 2015 14:27:48 +0000 (+0100) Subject: Split bdev into modules: lxcrbd X-Git-Tag: lxc-2.0.0.beta2~65^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3ceb28207202e17191a0cf195a971ccaa6c7e5d1;p=thirdparty%2Flxc.git Split bdev into modules: lxcrbd The functions - rbd_clonepaths(); - rbd_create(); - rbd_destroy(); - rbd_detect(); - rbd_mount(); - rbd_umount(); move from bdev.c to lxcrbd.{c,h}. All functions previously declared static become extern. Adapt Makefile.am to include lxcrbd.{c,h}. The structs - struct bdev; /* defined in bdev.h */ - struct bdev_specs; /* defined in lxccontainer.h */ - struct lxc_conf; /* defined conf.h */ are forward declared/put as incomplete types into lxcrbd.h as the functions associated with rbd need access to it. Put: - #define __STDC_FORMAT_MACROS and include: - #include in lxcrbd.c so that the format specifier PRIu64 is available. Signed-off-by: Christian Brauner --- diff --git a/src/lxc/Makefile.am b/src/lxc/Makefile.am index 697d3c39d..1816ac72d 100644 --- a/src/lxc/Makefile.am +++ b/src/lxc/Makefile.am @@ -13,6 +13,7 @@ noinst_HEADERS = \ bdev/lxcloop.h \ bdev/lxclvm.h \ bdev/lxcoverlay.h \ + bdev/lxcrbd.h \ bdev/lxcrsync.h \ bdev/lxczfs.h \ caps.h \ @@ -74,6 +75,7 @@ liblxc_so_SOURCES = \ bdev/lxcloop.c bdev/lxcloop.h \ bdev/lxclvm.c bdev/lxclvm.h \ bdev/lxcoverlay.c bdev/lxcoverlay.h \ + bdev/lxcrbd.c bdev/lxcrbd.h \ bdev/lxcrsync.c bdev/lxcrsync.h \ bdev/lxczfs.c bdev/lxczfs.h \ commands.c commands.h \ diff --git a/src/lxc/bdev/bdev.c b/src/lxc/bdev/bdev.c index 700a3ece1..54a2284f2 100644 --- a/src/lxc/bdev/bdev.c +++ b/src/lxc/bdev/bdev.c @@ -59,6 +59,7 @@ #include "lxclvm.h" #include "lxcloop.h" #include "lxcoverlay.h" +#include "lxcrbd.h" #include "lxcrsync.h" #include "lxczfs.h" #include "namespace.h" @@ -151,6 +152,18 @@ static const struct bdev_ops ovl_ops = { .can_backup = true, }; +/* rbd */ +static const struct bdev_ops rbd_ops = { + .detect = &rbd_detect, + .mount = &rbd_mount, + .umount = &rbd_umount, + .clone_paths = &rbd_clonepaths, + .destroy = &rbd_destroy, + .create = &rbd_create, + .can_snapshot = false, + .can_backup = false, +}; + /* zfs */ static const struct bdev_ops zfs_ops = { .detect = &zfs_detect, @@ -183,18 +196,6 @@ static int nbd_umount(struct bdev *bdev); static bool requires_nbd(const char *path); static bool wait_for_partition(const char *path); -/* functions associated with a rdb bdev struct */ -static int rbd_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); -static int rbd_create(struct bdev *bdev, const char *dest, const char *n, - struct bdev_specs *specs); -static int rbd_destroy(struct bdev *orig); -static int rbd_detect(const char *path); -static int rbd_mount(struct bdev *bdev); -static int rbd_umount(struct bdev *bdev); - /* helpers */ /* * These are copied from conf.c. However as conf.c will be moved to using @@ -511,167 +512,6 @@ int is_blktype(struct bdev *b) return 0; } -/* - * CEPH RBD ops - */ - -static int rbd_detect(const char *path) -{ - if ( memcmp(path, "/dev/rbd/", 9) == 0) - return 1; - return 0; -} - -static int rbd_mount(struct bdev *bdev) -{ - if (strcmp(bdev->type, "rbd")) - return -22; - if (!bdev->src || !bdev->dest) - return -22; - - if ( !file_exists(bdev->src) ) { - // if blkdev does not exist it should be mapped, because it is not persistent on reboot - ERROR("Block device %s is not mapped.", bdev->src); - return -1; - } - - return mount_unknown_fs(bdev->src, bdev->dest, bdev->mntopts); -} - -static int rbd_umount(struct bdev *bdev) -{ - if (strcmp(bdev->type, "rbd")) - return -22; - if (!bdev->src || !bdev->dest) - return -22; - return umount(bdev->dest); -} - -static int rbd_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) -{ - ERROR("rbd clonepaths not implemented"); - return -1; -} - -static int rbd_destroy(struct bdev *orig) -{ - pid_t pid; - char *rbdfullname; - - if ( file_exists(orig->src) ) { - if ((pid = fork()) < 0) - return -1; - if (!pid) { - execlp("rbd", "rbd", "unmap" , orig->src, NULL); - exit(1); - } - if (wait_for_pid(pid) < 0) - return -1; - } - - if ((pid = fork()) < 0) - return -1; - if (!pid) { - rbdfullname = alloca(strlen(orig->src) - 8); - strcpy( rbdfullname, &orig->src[9] ); - execlp("rbd", "rbd", "rm" , rbdfullname, NULL); - exit(1); - } - return wait_for_pid(pid); - -} - -static int rbd_create(struct bdev *bdev, const char *dest, const char *n, - struct bdev_specs *specs) -{ - const char *rbdpool, *rbdname = n, *fstype; - uint64_t size; - int ret, len; - char sz[24]; - pid_t pid; - - if (!specs) - return -1; - - rbdpool = specs->rbd.rbdpool; - if (!rbdpool) - rbdpool = lxc_global_config_value("lxc.bdev.rbd.rbdpool"); - - if (specs->rbd.rbdname) - rbdname = specs->rbd.rbdname; - - /* source device /dev/rbd/lxc/ctn */ - len = strlen(rbdpool) + strlen(rbdname) + 11; - bdev->src = malloc(len); - if (!bdev->src) - return -1; - - ret = snprintf(bdev->src, len, "/dev/rbd/%s/%s", rbdpool, rbdname); - if (ret < 0 || ret >= len) - return -1; - - // fssize is in bytes. - size = specs->fssize; - if (!size) - size = DEFAULT_FS_SIZE; - - // in megabytes for rbd tool - ret = snprintf(sz, 24, "%"PRIu64, size / 1024 / 1024 ); - if (ret < 0 || ret >= 24) - exit(1); - - if ((pid = fork()) < 0) - return -1; - if (!pid) { - execlp("rbd", "rbd", "create" , "--pool", rbdpool, rbdname, "--size", sz, NULL); - exit(1); - } - if (wait_for_pid(pid) < 0) - return -1; - - if ((pid = fork()) < 0) - return -1; - if (!pid) { - execlp("rbd", "rbd", "map", "--pool", rbdpool, rbdname, NULL); - exit(1); - } - if (wait_for_pid(pid) < 0) - return -1; - - fstype = specs->fstype; - if (!fstype) - fstype = DEFAULT_FSTYPE; - - if (do_mkfs(bdev->src, fstype) < 0) { - ERROR("Error creating filesystem type %s on %s", fstype, - bdev->src); - return -1; - } - if (!(bdev->dest = strdup(dest))) - return -1; - - if (mkdir_p(bdev->dest, 0755) < 0 && errno != EEXIST) { - ERROR("Error creating %s", bdev->dest); - return -1; - } - - return 0; -} - -static const struct bdev_ops rbd_ops = { - .detect = &rbd_detect, - .mount = &rbd_mount, - .umount = &rbd_umount, - .clone_paths = &rbd_clonepaths, - .destroy = &rbd_destroy, - .create = &rbd_create, - .can_snapshot = false, - .can_backup = false, -}; - // // nbd dev ops // diff --git a/src/lxc/bdev/lxcrbd.c b/src/lxc/bdev/lxcrbd.c new file mode 100644 index 000000000..113d2fcb9 --- /dev/null +++ b/src/lxc/bdev/lxcrbd.c @@ -0,0 +1,181 @@ +/* + * lxc: linux Container library + * + * (C) Copyright IBM Corp. 2007, 2008 + * + * Authors: + * Daniel Lezcano + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#define _GNU_SOURCE +#define __STDC_FORMAT_MACROS /* Required for PRIu64 to work. */ +#include /* Required for PRIu64 to work. */ +#include +#include +#include +#include + +#include "bdev.h" +#include "log.h" +#include "utils.h" + +lxc_log_define(lxcrbd, lxc); + +int rbd_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) +{ + ERROR("rbd clonepaths not implemented"); + return -1; +} + +int rbd_create(struct bdev *bdev, const char *dest, const char *n, + struct bdev_specs *specs) +{ + const char *rbdpool, *rbdname = n, *fstype; + uint64_t size; + int ret, len; + char sz[24]; + pid_t pid; + + if (!specs) + return -1; + + rbdpool = specs->rbd.rbdpool; + if (!rbdpool) + rbdpool = lxc_global_config_value("lxc.bdev.rbd.rbdpool"); + + if (specs->rbd.rbdname) + rbdname = specs->rbd.rbdname; + + /* source device /dev/rbd/lxc/ctn */ + len = strlen(rbdpool) + strlen(rbdname) + 11; + bdev->src = malloc(len); + if (!bdev->src) + return -1; + + ret = snprintf(bdev->src, len, "/dev/rbd/%s/%s", rbdpool, rbdname); + if (ret < 0 || ret >= len) + return -1; + + // fssize is in bytes. + size = specs->fssize; + if (!size) + size = DEFAULT_FS_SIZE; + + // in megabytes for rbd tool + ret = snprintf(sz, 24, "%"PRIu64, size / 1024 / 1024 ); + if (ret < 0 || ret >= 24) + exit(1); + + if ((pid = fork()) < 0) + return -1; + if (!pid) { + execlp("rbd", "rbd", "create" , "--pool", rbdpool, rbdname, "--size", sz, NULL); + exit(1); + } + if (wait_for_pid(pid) < 0) + return -1; + + if ((pid = fork()) < 0) + return -1; + if (!pid) { + execlp("rbd", "rbd", "map", "--pool", rbdpool, rbdname, NULL); + exit(1); + } + if (wait_for_pid(pid) < 0) + return -1; + + fstype = specs->fstype; + if (!fstype) + fstype = DEFAULT_FSTYPE; + + if (do_mkfs(bdev->src, fstype) < 0) { + ERROR("Error creating filesystem type %s on %s", fstype, + bdev->src); + return -1; + } + if (!(bdev->dest = strdup(dest))) + return -1; + + if (mkdir_p(bdev->dest, 0755) < 0 && errno != EEXIST) { + ERROR("Error creating %s", bdev->dest); + return -1; + } + + return 0; +} + +int rbd_destroy(struct bdev *orig) +{ + pid_t pid; + char *rbdfullname; + + if ( file_exists(orig->src) ) { + if ((pid = fork()) < 0) + return -1; + if (!pid) { + execlp("rbd", "rbd", "unmap" , orig->src, NULL); + exit(1); + } + if (wait_for_pid(pid) < 0) + return -1; + } + + if ((pid = fork()) < 0) + return -1; + if (!pid) { + rbdfullname = alloca(strlen(orig->src) - 8); + strcpy( rbdfullname, &orig->src[9] ); + execlp("rbd", "rbd", "rm" , rbdfullname, NULL); + exit(1); + } + return wait_for_pid(pid); + +} + +int rbd_detect(const char *path) +{ + if ( memcmp(path, "/dev/rbd/", 9) == 0) + return 1; + return 0; +} + +int rbd_mount(struct bdev *bdev) +{ + if (strcmp(bdev->type, "rbd")) + return -22; + if (!bdev->src || !bdev->dest) + return -22; + + if ( !file_exists(bdev->src) ) { + // if blkdev does not exist it should be mapped, because it is not persistent on reboot + ERROR("Block device %s is not mapped.", bdev->src); + return -1; + } + + return mount_unknown_fs(bdev->src, bdev->dest, bdev->mntopts); +} + +int rbd_umount(struct bdev *bdev) +{ + if (strcmp(bdev->type, "rbd")) + return -22; + if (!bdev->src || !bdev->dest) + return -22; + return umount(bdev->dest); +} diff --git a/src/lxc/bdev/lxcrbd.h b/src/lxc/bdev/lxcrbd.h new file mode 100644 index 000000000..19fa026b7 --- /dev/null +++ b/src/lxc/bdev/lxcrbd.h @@ -0,0 +1,52 @@ +/* + * lxc: linux Container library + * + * (C) Copyright IBM Corp. 2007, 2008 + * + * Authors: + * Daniel Lezcano + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef __LXC_RDB_H +#define __LXC_RDB_H + +#define _GNU_SOURCE +#include + +/* defined in bdev.h */ +struct bdev; + +/* defined in lxccontainer.h */ +struct bdev_specs; + +/* defined conf.h */ +struct lxc_conf; + +/* + * Functions associated with an rdb bdev struct. + */ +int rbd_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); +int rbd_create(struct bdev *bdev, const char *dest, const char *n, + struct bdev_specs *specs); +int rbd_destroy(struct bdev *orig); +int rbd_detect(const char *path); +int rbd_mount(struct bdev *bdev); +int rbd_umount(struct bdev *bdev); + +#endif /* __LXC_RDB_H */