From e3365b3bbc9a53d811d5aa4e81ce43f25243cce3 Mon Sep 17 00:00:00 2001 From: Prasanna Kumar Kalever Date: Mon, 31 Oct 2022 11:53:10 +0530 Subject: [PATCH] rbd: add --snap-id option to "rbd device map|unmap|attach|detach" Signed-off-by: Prasanna Kumar Kalever (cherry picked from commit 614c91c0054f0ad81de268adf91ef32fd8905ad4) --- doc/man/8/rbd.rst | 8 +++--- src/test/cli/rbd/help.t | 16 ++++++++--- src/tools/rbd/action/Device.cc | 4 +++ src/tools/rbd/action/Nbd.cc | 52 +++++++++++++++++++++++----------- src/tools/rbd_nbd/rbd-nbd.cc | 5 ++++ 5 files changed, 61 insertions(+), 24 deletions(-) diff --git a/doc/man/8/rbd.rst b/doc/man/8/rbd.rst index 28ab9dcf9e129..227cef16646cd 100644 --- a/doc/man/8/rbd.rst +++ b/doc/man/8/rbd.rst @@ -258,7 +258,7 @@ Commands Show the rbd images that are mapped via the rbd kernel module (default) or other supported device. -:command:`device map` [-t | --device-type *device-type*] [--cookie *device-cookie*] [--show-cookie] [--read-only] [--exclusive] [-o | --options *device-options*] *image-spec* | *snap-spec* +:command:`device map` [-t | --device-type *device-type*] [--cookie *device-cookie*] [--show-cookie] [--snap-id *snap-id*] [--read-only] [--exclusive] [-o | --options *device-options*] *image-spec* | *snap-spec* Map the specified image to a block device via the rbd kernel module (default) or other supported device (*nbd* on Linux or *ggate* on FreeBSD). @@ -266,14 +266,14 @@ Commands The --options argument is a comma separated list of device type specific options (opt1,opt2=val,...). -:command:`device unmap` [-t | --device-type *device-type*] [-o | --options *device-options*] *image-spec* | *snap-spec* | *device-path* +:command:`device unmap` [-t | --device-type *device-type*] [-o | --options *device-options*] [--snap-id *snap-id*] *image-spec* | *snap-spec* | *device-path* Unmap the block device that was mapped via the rbd kernel module (default) or other supported device. The --options argument is a comma separated list of device type specific options (opt1,opt2=val,...). -:command:`device attach` [-t | --device-type *device-type*] --device *device-path* [--cookie *device-cookie*] [--show-cookie] [--read-only] [--exclusive] [--force] [-o | --options *device-options*] *image-spec* | *snap-spec* +:command:`device attach` [-t | --device-type *device-type*] --device *device-path* [--cookie *device-cookie*] [--show-cookie] [--snap-id *snap-id*] [--read-only] [--exclusive] [--force] [-o | --options *device-options*] *image-spec* | *snap-spec* Attach the specified image to the specified block device (currently only `nbd` on Linux). This operation is unsafe and should not be normally used. In particular, specifying the wrong image or the wrong block device may @@ -282,7 +282,7 @@ Commands The --options argument is a comma separated list of device type specific options (opt1,opt2=val,...). -:command:`device detach` [-t | --device-type *device-type*] [-o | --options *device-options*] *image-spec* | *snap-spec* | *device-path* +:command:`device detach` [-t | --device-type *device-type*] [-o | --options *device-options*] [--snap-id *snap-id*] *image-spec* | *snap-spec* | *device-path* Detach the block device that was mapped or attached (currently only `nbd` on Linux). This operation is unsafe and should not be normally used. diff --git a/src/test/cli/rbd/help.t b/src/test/cli/rbd/help.t index ddb72186c77bc..e8b2c0db21d6f 100644 --- a/src/test/cli/rbd/help.t +++ b/src/test/cli/rbd/help.t @@ -581,7 +581,8 @@ [--snap ] --device [--show-cookie] [--cookie ] [--read-only] [--force] [--exclusive] [--quiesce] - [--quiesce-hook ] [--options ] + [--quiesce-hook ] [--snap-id ] + [--options ] Attach image to device. @@ -606,12 +607,14 @@ --exclusive disable automatic exclusive lock transitions --quiesce use quiesce hooks --quiesce-hook arg quiesce hook path + --snap-id arg snapshot id -o [ --options ] arg device specific options rbd help device detach usage: rbd device detach [--device-type ] [--pool ] [--namespace ] [--image ] - [--snap ] [--options ] + [--snap ] [--snap-id ] + [--options ] Detach image from device. @@ -627,6 +630,7 @@ --namespace arg namespace name --image arg image name --snap arg snapshot name + --snap-id arg snapshot id -o [ --options ] arg device specific options rbd help device list @@ -645,7 +649,8 @@ [--namespace ] [--image ] [--snap ] [--show-cookie] [--cookie ] [--read-only] [--exclusive] [--quiesce] - [--quiesce-hook ] [--options ] + [--quiesce-hook ] [--snap-id ] + [--options ] Map an image to a block device. @@ -668,12 +673,14 @@ --exclusive disable automatic exclusive lock transitions --quiesce use quiesce hooks --quiesce-hook arg quiesce hook path + --snap-id arg snapshot id -o [ --options ] arg device specific options rbd help device unmap usage: rbd device unmap [--device-type ] [--pool ] [--namespace ] [--image ] - [--snap ] [--options ] + [--snap ] [--snap-id ] + [--options ] Unmap a rbd device. @@ -689,6 +696,7 @@ --namespace arg namespace name --image arg image name --snap arg snapshot name + --snap-id arg snapshot id -o [ --options ] arg device specific options rbd help diff diff --git a/src/tools/rbd/action/Device.cc b/src/tools/rbd/action/Device.cc index 022abef9011e9..878081438377c 100644 --- a/src/tools/rbd/action/Device.cc +++ b/src/tools/rbd/action/Device.cc @@ -181,6 +181,7 @@ void get_map_arguments(po::options_description *positional, ("exclusive", po::bool_switch(), "disable automatic exclusive lock transitions") ("quiesce", po::bool_switch(), "use quiesce hooks") ("quiesce-hook", po::value(), "quiesce hook path"); + at::add_snap_id_option(options); add_device_specific_options(options); } @@ -200,6 +201,7 @@ void get_unmap_arguments(po::options_description *positional, at::add_namespace_option(options, at::ARGUMENT_MODIFIER_NONE); at::add_image_option(options, at::ARGUMENT_MODIFIER_NONE); at::add_snap_option(options, at::ARGUMENT_MODIFIER_NONE); + at::add_snap_id_option(options); add_device_specific_options(options); } @@ -222,6 +224,7 @@ void get_attach_arguments(po::options_description *positional, ("exclusive", po::bool_switch(), "disable automatic exclusive lock transitions") ("quiesce", po::bool_switch(), "use quiesce hooks") ("quiesce-hook", po::value(), "quiesce hook path"); + at::add_snap_id_option(options); add_device_specific_options(options); } @@ -241,6 +244,7 @@ void get_detach_arguments(po::options_description *positional, at::add_namespace_option(options, at::ARGUMENT_MODIFIER_NONE); at::add_image_option(options, at::ARGUMENT_MODIFIER_NONE); at::add_snap_option(options, at::ARGUMENT_MODIFIER_NONE); + at::add_snap_id_option(options); add_device_specific_options(options); } diff --git a/src/tools/rbd/action/Nbd.cc b/src/tools/rbd/action/Nbd.cc index 061fdfa306a66..dd5ef3290cfd6 100644 --- a/src/tools/rbd/action/Nbd.cc +++ b/src/tools/rbd/action/Nbd.cc @@ -122,6 +122,11 @@ int execute_attach(const po::variables_map &vm, return -EINVAL; } + if (vm.count(at::SNAPSHOT_ID)) { + args.push_back("--snap-id"); + args.push_back(std::to_string(vm[at::SNAPSHOT_ID].as())); + } + if (vm["quiesce"].as()) { args.push_back("--quiesce"); } @@ -159,23 +164,28 @@ int execute_detach(const po::variables_map &vm, device_name.clear(); } + std::vector args; + + args.push_back("detach"); std::string image_name; if (device_name.empty()) { int r = utils::get_image_or_snap_spec(vm, &image_name); if (r < 0) { return r; } - } - if (device_name.empty() && image_name.empty()) { - std::cerr << "rbd: detach requires either image name or device path" - << std::endl; - return -EINVAL; - } + if (image_name.empty()) { + std::cerr << "rbd: detach requires either image name or device path" + << std::endl; + return -EINVAL; + } - std::vector args; + if (vm.count(at::SNAPSHOT_ID)) { + args.push_back("--snap-id"); + args.push_back(std::to_string(vm[at::SNAPSHOT_ID].as())); + } + } - args.push_back("detach"); args.push_back(device_name.empty() ? image_name : device_name); if (vm.count("options")) { @@ -216,6 +226,11 @@ int execute_map(const po::variables_map &vm, args.push_back(vm["cookie"].as()); } + if (vm.count(at::SNAPSHOT_ID)) { + args.push_back("--snap-id"); + args.push_back(std::to_string(vm[at::SNAPSHOT_ID].as())); + } + if (vm["read-only"].as()) { args.push_back("--read-only"); } @@ -249,23 +264,28 @@ int execute_unmap(const po::variables_map &vm, device_name.clear(); } + std::vector args; + + args.push_back("unmap"); std::string image_name; if (device_name.empty()) { int r = utils::get_image_or_snap_spec(vm, &image_name); if (r < 0) { return r; } - } - if (device_name.empty() && image_name.empty()) { - std::cerr << "rbd: unmap requires either image name or device path" - << std::endl; - return -EINVAL; - } + if (image_name.empty()) { + std::cerr << "rbd: unmap requires either image name or device path" + << std::endl; + return -EINVAL; + } - std::vector args; + if (vm.count(at::SNAPSHOT_ID)) { + args.push_back("--snap-id"); + args.push_back(std::to_string(vm[at::SNAPSHOT_ID].as())); + } + } - args.push_back("unmap"); args.push_back(device_name.empty() ? image_name : device_name); if (vm.count("options")) { diff --git a/src/tools/rbd_nbd/rbd-nbd.cc b/src/tools/rbd_nbd/rbd-nbd.cc index 80eeff564d407..55ef9d1595d29 100644 --- a/src/tools/rbd_nbd/rbd-nbd.cc +++ b/src/tools/rbd_nbd/rbd-nbd.cc @@ -2227,6 +2227,11 @@ static int parse_args(vector& args, std::ostream *err_msg, break; } + if (cfg->snapid != CEPH_NOSNAP && !cfg->snapname.empty()) { + *err_msg << "rbd-nbd: use either snapname or snapid, not both"; + return -EINVAL; + } + if (args.begin() != args.end()) { *err_msg << "rbd-nbd: unknown args: " << *args.begin(); return -EINVAL; -- 2.39.5