Skip to content

Commit

Permalink
rbd: add --format option
Browse files Browse the repository at this point in the history
This chooses whether to use the original (supported by krbd)
or the new (supports layering) format.

Signed-off-by: Josh Durgin <[email protected]>
  • Loading branch information
jdurgin committed Sep 18, 2012
1 parent a112419 commit 582001e
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 44 deletions.
13 changes: 13 additions & 0 deletions doc/man/8/rbd.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ Options
Parameters
==========

.. option:: --format format

Specifies which object layout to use. The default is 1.

* format 1 - Use the original format for a new rbd image. This format is
understood by all versions of librbd and the kernel rbd module, but
does not support newer features like cloning.

* format 2 - Use the second rbd format, which is supported by
librbd (but not the kernel rbd module) at this time. This adds
support for cloning and is more easily extensible to allow more
features in the future.

.. option:: --size size-in-mb

Specifies the size (in megabytes) of the new rbd image.
Expand Down
55 changes: 41 additions & 14 deletions src/rbd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ void usage()
" create [--order <bits>] --size <MB> <name> create an empty image\n"
" clone [--order <bits>] <parentsnap> <clonename>\n"
" clone a snapshot into a COW\n"
" child image\n"
" child image\n"
" children <snap-name> display children of snapshot\n"
" flatten <image-name> fill clone with parent data\n"
" (make it independent)\n"
Expand Down Expand Up @@ -112,8 +112,9 @@ void usage()
" --size <size in MB> size of image for create and resize\n"
" --order <bits> the object size in bits; object size will be\n"
" (1 << order) bytes. Default is 22 (4 MB).\n"
"\n"
"For the map command:\n"
" --format <format-number> format to use when creating an image\n"
" format 1 is the original format (default)\n"
" format 2 supports cloning\n"
" --id <username> rados user (without 'client.' prefix) to authenticate as\n"
" --keyfile <path> file containing secret key for use with cephx\n";
}
Expand Down Expand Up @@ -167,13 +168,13 @@ static int do_list(librbd::RBD &rbd, librados::IoCtx& io_ctx)

static int do_create(librbd::RBD &rbd, librados::IoCtx& io_ctx,
const char *imgname, uint64_t size, int *order,
bool old_format, uint64_t features)
int format, uint64_t features)
{
int r;
if (features == 0)
features = RBD_FEATURES_ALL;

if (old_format)
if (format == 1)
r = rbd.create(io_ctx, imgname, size, order);
else
r = rbd.create2(io_ctx, imgname, size, features, order);
Expand Down Expand Up @@ -256,11 +257,13 @@ static int do_show_info(const char *imgname, librbd::Image& image,
<< std::endl
<< "\tblock_name_prefix: " << info.block_name_prefix
<< std::endl
<< "\told format: " << (old_format ? "True" : "False")
<< std::endl
<< "\tfeatures: " << feature_str(features)
<< "\tformat: " << (old_format ? "1" : "2")
<< std::endl;

if (!old_format) {
cout << "\tfeatures: " << feature_str(features) << std::endl;
}

// snapshot info, if present
if (snapname) {
cout << "\tprotected: " << (snap_protected ? "True" : "False")
Expand Down Expand Up @@ -520,7 +523,7 @@ static void set_pool_image_name(const char *orig_pool, const char *orig_img,

static int do_import(librbd::RBD &rbd, librados::IoCtx& io_ctx,
const char *imgname, int *order, const char *path,
bool old_format, uint64_t features, int64_t size)
int format, uint64_t features, int64_t size)
{
int fd, r;
struct stat stat_buf;
Expand Down Expand Up @@ -558,7 +561,7 @@ static int do_import(librbd::RBD &rbd, librados::IoCtx& io_ctx,

assert(imgname);

r = do_create(rbd, io_ctx, imgname, size, order, old_format, features);
r = do_create(rbd, io_ctx, imgname, size, order, format, features);
if (r < 0) {
cerr << "image creation failed" << std::endl;
return r;
Expand Down Expand Up @@ -1093,7 +1096,8 @@ int main(int argc, const char **argv)
const char *poolname = NULL;
uint64_t size = 0; // in bytes
int order = 0;
bool old_format = true;
bool format_specified = false;
int format = 1;
uint64_t features = RBD_FEATURE_LAYERING;
const char *imgname = NULL, *snapname = NULL, *destname = NULL, *dest_poolname = NULL, *dest_snapname = NULL, *path = NULL, *devpath = NULL;

Expand All @@ -1111,7 +1115,15 @@ int main(int argc, const char **argv)
usage();
return 0;
} else if (ceph_argparse_flag(args, i, "--new-format", (char*)NULL)) {
old_format = false;
format = 2;
format_specified = true;
} else if (ceph_argparse_withint(args, i, &format, &err, "--format",
(char*)NULL)) {
if (!err.str().empty()) {
cerr << err.str() << std::endl;
return EXIT_FAILURE;
}
format_specified = true;
} else if (ceph_argparse_witharg(args, i, &val, "-p", "--pool", (char*)NULL)) {
poolname = strdup(val.c_str());
} else if (ceph_argparse_witharg(args, i, &val, "--dest-pool", (char*)NULL)) {
Expand Down Expand Up @@ -1222,6 +1234,21 @@ int main(int argc, const char **argv)
}
}

if (format_specified && opt_cmd != OPT_IMPORT && opt_cmd != OPT_CREATE) {
cerr << "error: format can only be set when "
<< "creating or importing an image" << std::endl;
usage();
return EXIT_FAILURE;
}

if (format_specified) {
if (format < 1 || format > 2) {
cerr << "error: format must be 1 or 2" << std::endl;
usage();
return EXIT_FAILURE;
}
}

if (opt_cmd == OPT_EXPORT && !imgname) {
cerr << "error: image name was not specified" << std::endl;
usage();
Expand Down Expand Up @@ -1383,7 +1410,7 @@ int main(int argc, const char **argv)
usage();
return EXIT_FAILURE;
}
r = do_create(rbd, io_ctx, imgname, size, &order, old_format, features);
r = do_create(rbd, io_ctx, imgname, size, &order, format, features);
if (r < 0) {
cerr << "create error: " << cpp_strerror(-r) << std::endl;
return EXIT_FAILURE;
Expand Down Expand Up @@ -1572,7 +1599,7 @@ int main(int argc, const char **argv)
return EXIT_FAILURE;
}
r = do_import(rbd, dest_io_ctx, destname, &order, path,
old_format, features, size);
format, features, size);
if (r < 0) {
cerr << "import failed: " << cpp_strerror(-r) << std::endl;
return EXIT_FAILURE;
Expand Down
7 changes: 4 additions & 3 deletions src/test/cli/rbd/help.t
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
create [--order <bits>] --size <MB> <name> create an empty image
clone [--order <bits>] <parentsnap> <clonename>
clone a snapshot into a COW
child image
child image
children <snap-name> display children of snapshot
flatten <image-name> fill clone with parent data
(make it independent)
Expand Down Expand Up @@ -47,7 +47,8 @@
--size <size in MB> size of image for create and resize
--order <bits> the object size in bits; object size will be
(1 << order) bytes. Default is 22 (4 MB).

For the map command:
--format <format-number> format to use when creating an image
format 1 is the original format (default)
format 2 supports cloning
--id <username> rados user (without 'client.' prefix) to authenticate as
--keyfile <path> file containing secret key for use with cephx
63 changes: 36 additions & 27 deletions src/test/cli/rbd/invalid-snap-usage.t
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
create [--order <bits>] --size <MB> <name> create an empty image
clone [--order <bits>] <parentsnap> <clonename>
clone a snapshot into a COW
child image
child image
children <snap-name> display children of snapshot
flatten <image-name> fill clone with parent data
(make it independent)
Expand Down Expand Up @@ -48,8 +48,9 @@
--size <size in MB> size of image for create and resize
--order <bits> the object size in bits; object size will be
(1 << order) bytes. Default is 22 (4 MB).

For the map command:
--format <format-number> format to use when creating an image
format 1 is the original format (default)
format 2 supports cloning
--id <username> rados user (without 'client.' prefix) to authenticate as
--keyfile <path> file containing secret key for use with cephx
[1]
Expand All @@ -63,7 +64,7 @@
create [--order <bits>] --size <MB> <name> create an empty image
clone [--order <bits>] <parentsnap> <clonename>
clone a snapshot into a COW
child image
child image
children <snap-name> display children of snapshot
flatten <image-name> fill clone with parent data
(make it independent)
Expand Down Expand Up @@ -103,8 +104,9 @@
--size <size in MB> size of image for create and resize
--order <bits> the object size in bits; object size will be
(1 << order) bytes. Default is 22 (4 MB).

For the map command:
--format <format-number> format to use when creating an image
format 1 is the original format (default)
format 2 supports cloning
--id <username> rados user (without 'client.' prefix) to authenticate as
--keyfile <path> file containing secret key for use with cephx
[1]
Expand All @@ -118,7 +120,7 @@
create [--order <bits>] --size <MB> <name> create an empty image
clone [--order <bits>] <parentsnap> <clonename>
clone a snapshot into a COW
child image
child image
children <snap-name> display children of snapshot
flatten <image-name> fill clone with parent data
(make it independent)
Expand Down Expand Up @@ -158,8 +160,9 @@
--size <size in MB> size of image for create and resize
--order <bits> the object size in bits; object size will be
(1 << order) bytes. Default is 22 (4 MB).

For the map command:
--format <format-number> format to use when creating an image
format 1 is the original format (default)
format 2 supports cloning
--id <username> rados user (without 'client.' prefix) to authenticate as
--keyfile <path> file containing secret key for use with cephx
[1]
Expand All @@ -173,7 +176,7 @@
create [--order <bits>] --size <MB> <name> create an empty image
clone [--order <bits>] <parentsnap> <clonename>
clone a snapshot into a COW
child image
child image
children <snap-name> display children of snapshot
flatten <image-name> fill clone with parent data
(make it independent)
Expand Down Expand Up @@ -213,8 +216,9 @@
--size <size in MB> size of image for create and resize
--order <bits> the object size in bits; object size will be
(1 << order) bytes. Default is 22 (4 MB).

For the map command:
--format <format-number> format to use when creating an image
format 1 is the original format (default)
format 2 supports cloning
--id <username> rados user (without 'client.' prefix) to authenticate as
--keyfile <path> file containing secret key for use with cephx
[1]
Expand All @@ -228,7 +232,7 @@
create [--order <bits>] --size <MB> <name> create an empty image
clone [--order <bits>] <parentsnap> <clonename>
clone a snapshot into a COW
child image
child image
children <snap-name> display children of snapshot
flatten <image-name> fill clone with parent data
(make it independent)
Expand Down Expand Up @@ -268,8 +272,9 @@
--size <size in MB> size of image for create and resize
--order <bits> the object size in bits; object size will be
(1 << order) bytes. Default is 22 (4 MB).

For the map command:
--format <format-number> format to use when creating an image
format 1 is the original format (default)
format 2 supports cloning
--id <username> rados user (without 'client.' prefix) to authenticate as
--keyfile <path> file containing secret key for use with cephx
[1]
Expand All @@ -283,7 +288,7 @@
create [--order <bits>] --size <MB> <name> create an empty image
clone [--order <bits>] <parentsnap> <clonename>
clone a snapshot into a COW
child image
child image
children <snap-name> display children of snapshot
flatten <image-name> fill clone with parent data
(make it independent)
Expand Down Expand Up @@ -323,8 +328,9 @@
--size <size in MB> size of image for create and resize
--order <bits> the object size in bits; object size will be
(1 << order) bytes. Default is 22 (4 MB).

For the map command:
--format <format-number> format to use when creating an image
format 1 is the original format (default)
format 2 supports cloning
--id <username> rados user (without 'client.' prefix) to authenticate as
--keyfile <path> file containing secret key for use with cephx
[1]
Expand All @@ -338,7 +344,7 @@
create [--order <bits>] --size <MB> <name> create an empty image
clone [--order <bits>] <parentsnap> <clonename>
clone a snapshot into a COW
child image
child image
children <snap-name> display children of snapshot
flatten <image-name> fill clone with parent data
(make it independent)
Expand Down Expand Up @@ -378,8 +384,9 @@
--size <size in MB> size of image for create and resize
--order <bits> the object size in bits; object size will be
(1 << order) bytes. Default is 22 (4 MB).

For the map command:
--format <format-number> format to use when creating an image
format 1 is the original format (default)
format 2 supports cloning
--id <username> rados user (without 'client.' prefix) to authenticate as
--keyfile <path> file containing secret key for use with cephx
[1]
Expand All @@ -393,7 +400,7 @@
create [--order <bits>] --size <MB> <name> create an empty image
clone [--order <bits>] <parentsnap> <clonename>
clone a snapshot into a COW
child image
child image
children <snap-name> display children of snapshot
flatten <image-name> fill clone with parent data
(make it independent)
Expand Down Expand Up @@ -433,8 +440,9 @@
--size <size in MB> size of image for create and resize
--order <bits> the object size in bits; object size will be
(1 << order) bytes. Default is 22 (4 MB).

For the map command:
--format <format-number> format to use when creating an image
format 1 is the original format (default)
format 2 supports cloning
--id <username> rados user (without 'client.' prefix) to authenticate as
--keyfile <path> file containing secret key for use with cephx
[1]
Expand All @@ -448,7 +456,7 @@
create [--order <bits>] --size <MB> <name> create an empty image
clone [--order <bits>] <parentsnap> <clonename>
clone a snapshot into a COW
child image
child image
children <snap-name> display children of snapshot
flatten <image-name> fill clone with parent data
(make it independent)
Expand Down Expand Up @@ -488,8 +496,9 @@
--size <size in MB> size of image for create and resize
--order <bits> the object size in bits; object size will be
(1 << order) bytes. Default is 22 (4 MB).

For the map command:
--format <format-number> format to use when creating an image
format 1 is the original format (default)
format 2 supports cloning
--id <username> rados user (without 'client.' prefix) to authenticate as
--keyfile <path> file containing secret key for use with cephx
[1]

0 comments on commit 582001e

Please sign in to comment.