[thin_generate_metadata] Preparation for revising the features

This commit is contained in:
Ming-Hung Tsai 2020-05-27 12:01:59 +08:00
parent 128ecc27f2
commit 0c6d132774

View File

@ -17,24 +17,12 @@
// <http://www.gnu.org/licenses/>.
#include "base/output_file_requirements.h"
#include "persistent-data/file_utils.h"
#include "thin-provisioning/commands.h"
#include "thin-provisioning/emitter.h"
#include "thin-provisioning/human_readable_format.h"
#include "thin-provisioning/metadata.h"
#include "thin-provisioning/restore_emitter.h"
#include "thin-provisioning/xml_format.h"
#include "version.h"
#include <boost/optional.hpp>
#include <fstream>
#include <getopt.h>
#include <iostream>
#include <libgen.h>
#include <linux/fs.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
using namespace boost;
@ -48,78 +36,14 @@ namespace {
struct flags {
flags()
: data_block_size(128),
nr_data_blocks(10240),
nr_thins(1),
blocks_per_thin(1024),
run_lengths(1024) {
nr_data_blocks(10240)
{
}
block_address data_block_size;
block_address nr_data_blocks;
unsigned nr_thins;
block_address blocks_per_thin;
block_address run_lengths;
optional<string> output;
};
// This is how we stir in some entropy. It mixes up the data
// device.
class shuffler {
public:
shuffler(block_address nr_blocks, unsigned run_lengths)
: nr_blocks_(nr_blocks / run_lengths),
run_lengths_(run_lengths) {
}
block_address map(block_address b) const {
return reverse(b / run_lengths_) + (b % run_lengths_);
}
private:
block_address reverse(block_address b) const {
return nr_blocks_ - b - 1ull;
}
block_address nr_blocks_;
block_address run_lengths_;
};
void generate_device(emitter::ptr e, shuffler const &s, uint32_t dev_id,
block_address nr_blocks, block_address base) {
e->begin_device(dev_id, nr_blocks, 0, 0, 0);
for (unsigned b = 0; b < nr_blocks; b++)
e->single_map(b, s.map(base + b), 0);
e->end_device();
}
void generate_metadata(flags const &fs, emitter::ptr e) {
e->begin_superblock("fake metadata", 0, 0, optional<uint32_t>(), optional<uint32_t>(),
fs.data_block_size, fs.nr_data_blocks, optional<uint64_t>());
shuffler s(fs.nr_data_blocks, fs.run_lengths);
for (unsigned i = 0; i < fs.nr_thins; i++)
generate_device(e, s, i, fs.blocks_per_thin, i * fs.blocks_per_thin);
e->end_superblock();
}
int create_metadata(flags const &fs) {
try {
// The block size gets updated by the restorer.
block_manager::ptr bm(open_bm(*fs.output, block_manager::READ_WRITE));
metadata::ptr md(new metadata(bm, metadata::CREATE, 128, 0));
emitter::ptr restorer = create_restore_emitter(md);
generate_metadata(fs, restorer);
} catch (std::exception &e) {
cerr << e.what() << endl;
return 1;
}
return 0;
}
}
//----------------------------------------------------------------
@ -137,9 +61,6 @@ thin_generate_metadata_cmd::usage(std::ostream &out) const
<< " {-h|--help}\n"
<< " --data-block-size <block size>\n"
<< " --nr-data-blocks <nr>\n"
<< " --nr-thins <count>\n"
<< " --blocks-per-thin <count>\n"
<< " --run-lengths <count>\n"
<< " {-o|--output} <output device or file>\n"
<< " {-V|--version}" << endl;
}
@ -151,14 +72,11 @@ thin_generate_metadata_cmd::run(int argc, char **argv)
struct flags fs;
const char *shortopts = "hi:o:qV";
const struct option longopts[] = {
{ "help", no_argument, NULL, 'h'},
{ "output", required_argument, NULL, 'o'},
{ "data-block-size", required_argument, NULL, 1},
{ "nr-data-blocks", required_argument, NULL, 2},
{ "nr-thins", required_argument, NULL, 3},
{ "blocks-per-thin", required_argument, NULL, 4},
{ "run-lengths", required_argument, NULL, 5},
{ "version", no_argument, NULL, 'V'},
{ "help", no_argument, NULL, 'h' },
{ "output", required_argument, NULL, 'o' },
{ "data-block-size", required_argument, NULL, 101 },
{ "nr-data-blocks", required_argument, NULL, 102 },
{ "version", no_argument, NULL, 'V' },
{ NULL, no_argument, NULL, 0 }
};
@ -172,26 +90,14 @@ thin_generate_metadata_cmd::run(int argc, char **argv)
fs.output = optarg;
break;
case 1:
case 101:
fs.data_block_size = parse_uint64(optarg, "data block size");
break;
case 2:
case 102:
fs.nr_data_blocks = parse_uint64(optarg, "nr data blocks");
break;
case 3:
fs.nr_thins = parse_uint64(optarg, "nr thins");
break;
case 4:
fs.blocks_per_thin = parse_uint64(optarg, "blocks per thin");
break;
case 5:
fs.run_lengths = parse_uint64(optarg, "run lengths");
break;
case 'V':
cout << THIN_PROVISIONING_TOOLS_VERSION << endl;
return 0;
@ -209,7 +115,7 @@ thin_generate_metadata_cmd::run(int argc, char **argv)
} else
check_output_file_requirements(*fs.output);
return create_metadata(fs);
return 0;
}
//----------------------------------------------------------------