Commit 3ceb2820 by Christian Brauner

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 <inttypes.h> in lxcrbd.c so that the format specifier PRIu64 is available. Signed-off-by: 's avatarChristian Brauner <christian.brauner@mailbox.org>
parent 304b4cf3
...@@ -13,6 +13,7 @@ noinst_HEADERS = \ ...@@ -13,6 +13,7 @@ noinst_HEADERS = \
bdev/lxcloop.h \ bdev/lxcloop.h \
bdev/lxclvm.h \ bdev/lxclvm.h \
bdev/lxcoverlay.h \ bdev/lxcoverlay.h \
bdev/lxcrbd.h \
bdev/lxcrsync.h \ bdev/lxcrsync.h \
bdev/lxczfs.h \ bdev/lxczfs.h \
caps.h \ caps.h \
...@@ -74,6 +75,7 @@ liblxc_so_SOURCES = \ ...@@ -74,6 +75,7 @@ liblxc_so_SOURCES = \
bdev/lxcloop.c bdev/lxcloop.h \ bdev/lxcloop.c bdev/lxcloop.h \
bdev/lxclvm.c bdev/lxclvm.h \ bdev/lxclvm.c bdev/lxclvm.h \
bdev/lxcoverlay.c bdev/lxcoverlay.h \ bdev/lxcoverlay.c bdev/lxcoverlay.h \
bdev/lxcrbd.c bdev/lxcrbd.h \
bdev/lxcrsync.c bdev/lxcrsync.h \ bdev/lxcrsync.c bdev/lxcrsync.h \
bdev/lxczfs.c bdev/lxczfs.h \ bdev/lxczfs.c bdev/lxczfs.h \
commands.c commands.h \ commands.c commands.h \
......
...@@ -59,6 +59,7 @@ ...@@ -59,6 +59,7 @@
#include "lxclvm.h" #include "lxclvm.h"
#include "lxcloop.h" #include "lxcloop.h"
#include "lxcoverlay.h" #include "lxcoverlay.h"
#include "lxcrbd.h"
#include "lxcrsync.h" #include "lxcrsync.h"
#include "lxczfs.h" #include "lxczfs.h"
#include "namespace.h" #include "namespace.h"
...@@ -151,6 +152,18 @@ static const struct bdev_ops ovl_ops = { ...@@ -151,6 +152,18 @@ static const struct bdev_ops ovl_ops = {
.can_backup = true, .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 */ /* zfs */
static const struct bdev_ops zfs_ops = { static const struct bdev_ops zfs_ops = {
.detect = &zfs_detect, .detect = &zfs_detect,
...@@ -183,18 +196,6 @@ static int nbd_umount(struct bdev *bdev); ...@@ -183,18 +196,6 @@ static int nbd_umount(struct bdev *bdev);
static bool requires_nbd(const char *path); static bool requires_nbd(const char *path);
static bool wait_for_partition(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 */ /* helpers */
/* /*
* These are copied from conf.c. However as conf.c will be moved to using * 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) ...@@ -511,167 +512,6 @@ int is_blktype(struct bdev *b)
return 0; 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 // nbd dev ops
// //
......
/*
* lxc: linux Container library
*
* (C) Copyright IBM Corp. 2007, 2008
*
* Authors:
* Daniel Lezcano <daniel.lezcano at free.fr>
*
* 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 <inttypes.h> /* Required for PRIu64 to work. */
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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);
}
/*
* lxc: linux Container library
*
* (C) Copyright IBM Corp. 2007, 2008
*
* Authors:
* Daniel Lezcano <daniel.lezcano at free.fr>
*
* 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 <stdint.h>
/* 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 */
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