summaryrefslogtreecommitdiffstats
path: root/src/tools/rbd/action/Migration.cc
blob: 1ce6201d96489c44ce1c9356c75512412fcbbfbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#include "include/compat.h"
#include "common/errno.h"
#include "common/safe_io.h"

#include "tools/rbd/ArgumentTypes.h"
#include "tools/rbd/Shell.h"
#include "tools/rbd/Utils.h"

#include <sys/types.h>
#include <fcntl.h>
#include <iostream>
#include <boost/program_options.hpp>

namespace rbd {
namespace action {
namespace migration {

namespace at = argument_types;
namespace po = boost::program_options;

static int do_execute(librados::IoCtx& io_ctx, const std::string &image_name,
                      bool no_progress) {
  utils::ProgressContext pc("Image migration", no_progress);
  int r = librbd::RBD().migration_execute_with_progress(io_ctx,
                                                        image_name.c_str(), pc);
  if (r < 0) {
    pc.fail();
    std::cerr << "rbd: migration failed: " << cpp_strerror(r) << std::endl;
    return r;
  }
  pc.finish();
  return 0;
}

static int do_abort(librados::IoCtx& io_ctx, const std::string &image_name,
                    bool no_progress) {
  utils::ProgressContext pc("Abort image migration", no_progress);
  int r = librbd::RBD().migration_abort_with_progress(io_ctx,
                                                      image_name.c_str(), pc);
  if (r < 0) {
    pc.fail();
    std::cerr << "rbd: aborting migration failed: " << cpp_strerror(r)
              << std::endl;
    return r;
  }
  pc.finish();
  return 0;
}

static int do_commit(librados::IoCtx& io_ctx, const std::string &image_name,
                     bool force, bool no_progress) {
  librbd::image_migration_status_t migration_status;
  int r = librbd::RBD().migration_status(io_ctx, image_name.c_str(),
                                         &migration_status,
                                         sizeof(migration_status));
  if (r < 0) {
    std::cerr << "rbd: getting migration status failed: " << cpp_strerror(r)
              << std::endl;
    return r;
  }

  librados::IoCtx dst_io_ctx;
  r = librados::Rados(io_ctx).ioctx_create2(migration_status.dest_pool_id, dst_io_ctx);
  if (r < 0) {
    std::cerr << "rbd: accessing source pool id="
              << migration_status.dest_pool_id << " failed: "
              << cpp_strerror(r) << std::endl;
    return r;
  }

  r = utils::set_namespace(migration_status.dest_pool_namespace, &dst_io_ctx);
  if (r < 0) {
    return r;
  }

  librbd::Image image;
  r = utils::open_image_by_id(dst_io_ctx, migration_status.dest_image_id,
                              true, &image);
  if (r < 0) {
    return r;
  }

  std::vector<librbd::linked_image_spec_t> children;
  r = image.list_descendants(&children);
  if (r < 0) {
    std::cerr << "rbd: listing descendants failed: " << cpp_strerror(r)
              << std::endl;
    return r;
  }

  if (children.size() > 0) {
    std::cerr << "rbd: the image has "
              << (children.size() == 1 ? "a descendant" : "descendants") << ": "
              << std::endl;
    for (auto& child : children) {
      std::cerr << "  " << child.pool_name << "/";
      if (!child.pool_namespace.empty()) {
        std::cerr << child.pool_namespace << "/";
      }
      std::cerr << child.image_name;
      if (child.trash) {
        std::cerr << " (trash " << child.image_id << ")";
      }
      std::cerr << std::endl;
    }
    std::cerr << "Warning: in-use, read-only descendant images"
              << " will not detect the parent update." << std::endl;
    if (force) {
      std::cerr << "Proceeding anyway due to force flag set." << std::endl;
    } else {
      std::cerr << "Ensure no descendant images are opened read-only"
                << " and run again with force flag." << std::endl;
      return -EBUSY;
    }
  }

  utils::ProgressContext pc("Commit image migration", no_progress);
  r = librbd::RBD().migration_commit_with_progress(io_ctx, image_name.c_str(),
                                                   pc);
  if (r < 0) {
    pc.fail();
    std::cerr << "rbd: committing migration failed: " << cpp_strerror(r)
              << std::endl;
    return r;
  }
  pc.finish();
  return 0;
}

void get_prepare_arguments(po::options_description *positional,
                           po::options_description *options) {
  options->add_options()
    ("import-only", po::bool_switch(), "only import data from source")
    ("source-spec-path", po::value<std::string>(),
     "source-spec file (or '-' for stdin)")
    ("source-spec", po::value<std::string>(),
     "source-spec");
  at::add_image_or_snap_spec_options(positional, options,
                                     at::ARGUMENT_MODIFIER_SOURCE);
  at::add_image_spec_options(positional, options, at::ARGUMENT_MODIFIER_DEST);
  at::add_create_image_options(options, true);
  at::add_flatten_option(options);
}

int execute_prepare(const po::variables_map &vm,
                    const std::vector<std::string> &ceph_global_init_args) {
  bool import_only = vm["import-only"].as<bool>();

  size_t arg_index = 0;
  std::string pool_name;
  std::string namespace_name;
  std::string image_name;
  std::string snap_name;
  int r = utils::get_pool_image_snapshot_names(
    vm, at::ARGUMENT_MODIFIER_SOURCE, &arg_index, &pool_name, &namespace_name,
    &image_name, import_only ? &snap_name : nullptr, true,
    import_only ? utils::SNAPSHOT_PRESENCE_PERMITTED :
                  utils::SNAPSHOT_PRESENCE_NONE,
    utils::SPEC_VALIDATION_NONE);
  if (r < 0) {
    return r;
  }

  std::string dst_pool_name;
  std::string dst_namespace_name;
  std::string dst_image_name;
  r = utils::get_pool_image_snapshot_names(
    vm, at::ARGUMENT_MODIFIER_DEST, &arg_index, &dst_pool_name,
    &dst_namespace_name, &dst_image_name, nullptr, false,
    utils::SNAPSHOT_PRESENCE_NONE, utils::SPEC_VALIDATION_FULL);
  if (r < 0) {
    return r;
  }

  std::string source_spec;
  if (vm.count("source-spec") && vm.count("source-spec-path")) {
    std::cerr << "rbd: cannot specify both source-spec and source-spec-path"
              << std::endl;
    return -EINVAL;
  } else if (vm.count("source-spec-path")) {
    std::string source_spec_path = vm["source-spec-path"].as<std::string>();

    int fd = STDIN_FILENO;
    if (source_spec_path != "-") {
      fd = open(source_spec_path.c_str(), O_RDONLY);
      if (fd < 0) {
        r = -errno;
        std::cerr << "rbd: error opening " << source_spec_path << std::endl;
        return r;
      }
    }

    source_spec.resize(4096);
    r = safe_read(fd, source_spec.data(), source_spec.size() - 1);
    if (fd != STDIN_FILENO) {
      VOID_TEMP_FAILURE_RETRY(close(fd));
    }

    if (r >= 0) {
      source_spec.resize(r);
    } else {
      std::cerr << "rbd: error reading source-spec file: " << cpp_strerror(r)
                << std::endl;
      return r;
    }
  } else if (vm.count("source-spec")) {
    source_spec = vm["source-spec"].as<std::string>();
  }

  librados::Rados rados;
  librados::IoCtx io_ctx;
  r = utils::init(pool_name, namespace_name, &rados, &io_ctx);
  if (r < 0) {
    return r;
  }

  librados::IoCtx dst_io_ctx;
  if (source_spec.empty()) {
    utils::normalize_pool_name(&dst_pool_name);
    r = utils::init_io_ctx(rados, dst_pool_name, dst_namespace_name,
                           &dst_io_ctx);
    if (r < 0) {
      return r;
    }
  }

  if (import_only && source_spec.empty()) {
    if (snap_name.empty()) {
      std::cerr << "rbd: snapshot name was not specified" << std::endl;
      return -EINVAL;
    }

    std::stringstream ss;
    ss << R"({)"
       << R"("type":"native",)"
       << R"("pool_id":)" << io_ctx.get_id() << R"(,)"
       << R"("pool_namespace":")" << io_ctx.get_namespace() << R"(",)"
       << R"("image_name":")" << image_name << R"(",)"
       << R"("snap_name":")" << snap_name << R"(")"
       << R"(})";
    source_spec = ss.str();

    if (dst_image_name.empty()) {
      std::cerr << "rbd: destination image name must be provided" << std::endl;
      return -EINVAL;
    }
    io_ctx = dst_io_ctx;
    image_name = dst_image_name;
    snap_name = "";
  } else if (!import_only && !source_spec.empty()) {
    std::cerr << "rbd: --import-only must be used in combination with "
              << "source-spec/source-spec-path" << std::endl;
    return -EINVAL;
  }

  if (!snap_name.empty()) {
    std::cerr << "rbd: snapshot name specified for a command that doesn't "
              << "use it" << std::endl;
    return -EINVAL;
  }

  librbd::ImageOptions opts;
  r = utils::get_image_options(vm, true, &opts);
  if (r < 0) {
    return r;
  }

  if (source_spec.empty()) {
    if (dst_image_name.empty()) {
      dst_image_name = image_name;
    }

    int r = librbd::RBD().migration_prepare(io_ctx, image_name.c_str(),
                                            dst_io_ctx, dst_image_name.c_str(),
                                            opts);
    if (r < 0) {
      std::cerr << "rbd: preparing migration failed: " << cpp_strerror(r)
                << std::endl;
      return r;
    }
  } else {
    ceph_assert(import_only);
    r = librbd::RBD().migration_prepare_import(source_spec.c_str(), io_ctx,
                                               image_name.c_str(), opts);
    if (r < 0) {
      std::cerr << "rbd: preparing import migration failed: " << cpp_strerror(r)
                << std::endl;
      return r;
    }
  }

  return 0;
}

void get_execute_arguments(po::options_description *positional,
                           po::options_description *options) {
  at::add_image_spec_options(positional, options, at::ARGUMENT_MODIFIER_NONE);
  at::add_no_progress_option(options);
}

int execute_execute(const po::variables_map &vm,
                    const std::vector<std::string> &ceph_global_init_args) {
  size_t arg_index = 0;
  std::string pool_name;
  std::string namespace_name;
  std::string image_name;
  int r = utils::get_pool_image_snapshot_names(
    vm, at::ARGUMENT_MODIFIER_NONE, &arg_index, &pool_name, &namespace_name,
    &image_name, nullptr, true, utils::SNAPSHOT_PRESENCE_NONE,
    utils::SPEC_VALIDATION_NONE);
  if (r < 0) {
    return r;
  }

  librados::Rados rados;
  librados::IoCtx io_ctx;
  r = utils::init(pool_name, namespace_name, &rados, &io_ctx);
  if (r < 0) {
    return r;
  }
  io_ctx.set_pool_full_try();

  r = do_execute(io_ctx, image_name, vm[at::NO_PROGRESS].as<bool>());
  if (r < 0) {
    return r;
  }

  return 0;
}

void get_abort_arguments(po::options_description *positional,
                          po::options_description *options) {
  at::add_image_spec_options(positional, options, at::ARGUMENT_MODIFIER_NONE);
  at::add_no_progress_option(options);
}

int execute_abort(const po::variables_map &vm,
                  const std::vector<std::string> &ceph_global_init_args) {
  size_t arg_index = 0;
  std::string pool_name;
  std::string namespace_name;
  std::string image_name;
  int r = utils::get_pool_image_snapshot_names(
    vm, at::ARGUMENT_MODIFIER_NONE, &arg_index, &pool_name, &namespace_name,
    &image_name, nullptr, true, utils::SNAPSHOT_PRESENCE_NONE,
    utils::SPEC_VALIDATION_NONE);
  if (r < 0) {
    return r;
  }

  librados::Rados rados;
  librados::IoCtx io_ctx;
  r = utils::init(pool_name, namespace_name, &rados, &io_ctx);
  if (r < 0) {
    return r;
  }
  io_ctx.set_pool_full_try();

  r = do_abort(io_ctx, image_name, vm[at::NO_PROGRESS].as<bool>());
  if (r < 0) {
    return r;
  }

  return 0;
}

void get_commit_arguments(po::options_description *positional,
                          po::options_description *options) {
  at::add_image_spec_options(positional, options, at::ARGUMENT_MODIFIER_NONE);
  at::add_no_progress_option(options);
  options->add_options()
      ("force", po::bool_switch(), "proceed even if the image has children");
}

int execute_commit(const po::variables_map &vm,
                   const std::vector<std::string> &ceph_global_init_args) {
  size_t arg_index = 0;
  std::string pool_name;
  std::string namespace_name;
  std::string image_name;
  int r = utils::get_pool_image_snapshot_names(
    vm, at::ARGUMENT_MODIFIER_NONE, &arg_index, &pool_name, &namespace_name,
    &image_name, nullptr, true, utils::SNAPSHOT_PRESENCE_NONE,
    utils::SPEC_VALIDATION_NONE);
  if (r < 0) {
    return r;
  }

  librados::Rados rados;
  librados::IoCtx io_ctx;
  r = utils::init(pool_name, namespace_name, &rados, &io_ctx);
  if (r < 0) {
    return r;
  }
  io_ctx.set_pool_full_try();

  r = do_commit(io_ctx, image_name, vm["force"].as<bool>(),
                vm[at::NO_PROGRESS].as<bool>());
  if (r < 0) {
    return r;
  }

  return 0;
}

Shell::SwitchArguments switched_arguments({"import-only"});

Shell::Action action_prepare(
  {"migration", "prepare"}, {}, "Prepare image migration.",
  at::get_long_features_help(), &get_prepare_arguments, &execute_prepare);

Shell::Action action_execute(
  {"migration", "execute"}, {}, "Execute image migration.", "",
  &get_execute_arguments, &execute_execute);

Shell::Action action_abort(
  {"migration", "abort"}, {}, "Cancel interrupted image migration.", "",
  &get_abort_arguments, &execute_abort);

Shell::Action action_commit(
  {"migration", "commit"}, {}, "Commit image migration.", "",
  &get_commit_arguments, &execute_commit);

} // namespace migration
} // namespace action
} // namespace rbd