Skip to content

Commit

Permalink
rbd: use the unified --export-format option for rbd export and rbd im…
Browse files Browse the repository at this point in the history
…port

Signed-off-by: Dongsheng Yang <[email protected]>
  • Loading branch information
yangdongsheng committed Feb 19, 2017
1 parent 4ee764f commit f16899a
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 20 deletions.
8 changes: 4 additions & 4 deletions doc/man/8/rbd.rst
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,9 @@ Commands
:command:`export` [--export-format *format (1 or 2)*] (*image-spec* | *snap-spec*) [*dest-path*]
Exports image to dest path (use - for stdout).
The --export-format accepts '1' or '2' currently. Format 2 allow us to export not only the content
of image, but also the snapshots and other priorities, such as image_order, features.
of image, but also the snapshots and other properties, such as image_order, features.

:command:`import` [--import-format *format (1 or 2)*] [--image-format *format-id*] [--object-size *size-in-B/K/M*] [--stripe-unit *size-in-B/K/M* --stripe-count *num*] [--image-feature *feature-name*]... [--image-shared] *src-path* [*image-spec*]
:command:`import` [--export-format *format (1 or 2)*] [--image-format *format-id*] [--object-size *size-in-B/K/M*] [--stripe-unit *size-in-B/K/M* --stripe-count *num*] [--image-feature *feature-name*]... [--image-shared] *src-path* [*image-spec*]
Creates a new image and imports its data from path (use - for
stdin). The import operation will try to create sparse rbd images
if possible. For import from stdin, the sparsification unit is
Expand All @@ -238,8 +238,8 @@ Commands
The --stripe-unit and --stripe-count arguments are optional, but must be
used together.

The --import-format accepts '1' or '2' currently. Format 2 allow us to import not only the content
of image, but also the snapshots and other priorities, such as image_order, features.
The --export-format accepts '1' or '2' currently. Format 2 allow us to import not only the content
of image, but also the snapshots and other properties, such as image_order, features.

:command:`export-diff` [--from-snap *snap-name*] [--whole-object] (*image-spec* | *snap-spec*) *dest-path*
Exports an incremental diff for an image to dest path (use - for stdout). If
Expand Down
19 changes: 19 additions & 0 deletions src/tools/rbd/ArgumentTypes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,11 @@ void add_no_error_option(boost::program_options::options_description *opt) {
(NO_ERROR.c_str(), po::bool_switch(), "continue after error");
}

void add_export_format_option(boost::program_options::options_description *opt) {
opt->add_options()
("export-format", po::value<ExportFormat>(), "format of image file");
}

std::string get_short_features_help(bool append_suffix) {
std::ostringstream oss;
bool first_feature = true;
Expand Down Expand Up @@ -492,5 +497,19 @@ void validate(boost::any& v, const std::vector<std::string>& values,
throw po::validation_error(po::validation_error::invalid_option_value);
}

void validate(boost::any& v, const std::vector<std::string>& values,
ExportFormat *target_type, int) {
po::validators::check_first_occurrence(v);
const std::string &s = po::validators::get_single_string(values);

std::string parse_error;
uint64_t format = strict_sistrtoll(s.c_str(), &parse_error);
if (!parse_error.empty() || (format != 1 && format != 2)) {
throw po::validation_error(po::validation_error::invalid_option_value);
}

v = boost::any(format);
}

} // namespace argument_types
} // namespace rbd
7 changes: 7 additions & 0 deletions src/tools/rbd/ArgumentTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ struct Format : public TypedValue<std::string> {

struct JournalObjectSize {};

struct ExportFormat {};

void validate(boost::any& v, const std::vector<std::string>& values,
ExportFormat *target_type, int);

void add_export_format_option(boost::program_options::options_description *opt);

std::string get_name_prefix(ArgumentModifier modifier);
std::string get_description_prefix(ArgumentModifier modifier);

Expand Down
9 changes: 1 addition & 8 deletions src/tools/rbd/action/Export.cc
Original file line number Diff line number Diff line change
Expand Up @@ -358,12 +358,6 @@ class C_Export : public Context

static int do_export(librbd::Image& image, const char *path, bool no_progress, int export_format)
{
// check current supported formats.
if (export_format != 1 && export_format != 2) {
std::cerr << "rbd: wrong file format to import" << std::endl;
return -EINVAL;
}

librbd::image_info_t info;
int64_t r = image.stat(info, sizeof(info));
if (r < 0)
Expand Down Expand Up @@ -520,8 +514,7 @@ void get_arguments(po::options_description *positional,
at::add_path_options(positional, options,
"export file (or '-' for stdout)");
at::add_no_progress_option(options);
options->add_options()
("export-format", po::value<int>(), "format to export image");
at::add_export_format_option(options);
}

int execute(const po::variables_map &vm) {
Expand Down
9 changes: 1 addition & 8 deletions src/tools/rbd/action/Import.cc
Original file line number Diff line number Diff line change
Expand Up @@ -294,12 +294,6 @@ static int do_import(librbd::RBD &rbd, librados::IoCtx& io_ctx,
librbd::ImageOptions& opts, bool no_progress,
int import_format)
{
// check current supported formats.
if (import_format != 1 && import_format != 2) {
std::cerr << "rbd: wrong file format to import" << std::endl;
return -EINVAL;
}

int fd, r;
struct stat stat_buf;
utils::ProgressContext pc("Importing image", no_progress);
Expand Down Expand Up @@ -569,8 +563,7 @@ void get_arguments(po::options_description *positional,
at::add_image_spec_options(positional, options, at::ARGUMENT_MODIFIER_DEST);
at::add_create_image_options(options, true);
at::add_no_progress_option(options);
options->add_options()
("import-format", po::value<int>(), "format of the file to be imported");
at::add_export_format_option(options);

// TODO legacy rbd allowed import to accept both 'image'/'dest' and
// 'pool'/'dest-pool'
Expand Down

0 comments on commit f16899a

Please sign in to comment.