2013-06-26 16:15:19 +05:30
|
|
|
#!/usr/bin/env ruby
|
|
|
|
#
|
|
|
|
# Copyright (C) 2013 Red Hat, GmbH
|
|
|
|
#
|
2013-06-28 17:13:59 +05:30
|
|
|
# Simple thinp provisioning XML metadata creation tool
|
2013-06-26 16:15:19 +05:30
|
|
|
#
|
|
|
|
|
|
|
|
require 'optparse'
|
|
|
|
require 'pathname'
|
|
|
|
|
|
|
|
#----------------------------------------------------------------
|
|
|
|
|
|
|
|
$prg = Pathname.new($0).basename
|
|
|
|
|
|
|
|
def check_opts(opts)
|
|
|
|
abort "#{$prg} - 3 arguments required!" if opts.length < 3
|
2013-07-02 21:06:15 +05:30
|
|
|
abort "#{$prg} - block size must be 2^^N with N > 1" if opts[:blocksize] < 2 || (opts[:blocksize] & opts[:blocksize] - 1) != 0
|
2013-06-28 17:13:59 +05:30
|
|
|
abort "#{$prg} - mappings must be > 0" if opts[:mappings] < 1
|
|
|
|
abort "#{$prg} - number of thin provisioned devices or snapshots must be > 0" if opts[:thins].nil? || opts[:thins] < 1
|
2013-07-02 21:06:15 +05:30
|
|
|
abort "#{$prg} - size variation too large!" if !opts[:variation].nil? && (opts[:mappings] < 2 || (opts[:variation] >= 2 * (opts[:mappings] - 1)))
|
2013-06-26 16:15:19 +05:30
|
|
|
end
|
|
|
|
|
2013-06-28 17:13:59 +05:30
|
|
|
def parse_command_line(argv)
|
2013-06-26 16:15:19 +05:30
|
|
|
opts = {}
|
|
|
|
|
|
|
|
os = OptionParser.new do |o|
|
|
|
|
o.banner = "Thin Provisioning XML Test Metadata Generator.\nUsage: #{$prg} [opts]"
|
2013-06-28 17:13:59 +05:30
|
|
|
o.on("-b", "--block-size #SECTORS", Integer, "Block size of thin provisioned devices in sectors.") { |bs| opts[:blocksize] = bs }
|
|
|
|
o.on("-m", "--mappings #MAPPINGS", Integer, "Number of mappings per thin device or snapshot.") { |m| opts[:mappings] = m }
|
|
|
|
o.on("-r", "--range-mappings", "Create range mappings") { opts[:range] = true }
|
|
|
|
o.on("-t", "--thin-devices #THINS", Integer, "Sum of thin devices and snapshots.") { |mt| opts[:thins] = mt }
|
2013-07-02 21:06:15 +05:30
|
|
|
o.on("-v", "--variations #MAPPINGS]", Integer, "Mapping variation of thin devices and snapshots.") { |mv| opts[:variation] = mv }
|
2013-06-28 17:13:59 +05:30
|
|
|
o.on("-h", "--help", "Output this help.") { puts o; exit }
|
2013-06-26 16:15:19 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
os.parse!(argv)
|
|
|
|
rescue OptionParser::ParseError => e
|
|
|
|
abort "#{$prg} #{e}\n#{$prg} #{os}"
|
|
|
|
end
|
|
|
|
|
|
|
|
check_opts(opts)
|
|
|
|
opts
|
|
|
|
end
|
|
|
|
|
|
|
|
def begin_superblock(blocksize, nr_data_blocks)
|
2013-06-28 15:40:17 +05:30
|
|
|
"<superblock uuid=\"\" time=\"0\" transaction=\"1\" data_block_size=\"#{blocksize}\" nr_data_blocks=\"#{nr_data_blocks}\">"
|
2013-06-26 16:15:19 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def end_superblock
|
2013-06-28 15:40:17 +05:30
|
|
|
"</superblock>"
|
2013-06-26 16:15:19 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def begin_device(devid, size)
|
2013-06-28 15:40:17 +05:30
|
|
|
" <device dev_id=\"#{devid}\" mapped_blocks=\"#{size}\" transaction=\"0\" creation_time=\"0\" snap_time=\"0\">"
|
2013-06-26 16:15:19 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def end_device
|
2013-06-28 15:40:17 +05:30
|
|
|
" </device>"
|
2013-06-26 16:15:19 +05:30
|
|
|
end
|
|
|
|
|
2013-06-26 19:32:05 +05:30
|
|
|
def single_mapping(from, to)
|
2013-06-28 15:40:17 +05:30
|
|
|
" <single_mapping origin_block=\"#{from}\" data_block=\"#{to}\" time=\"1\"/>"
|
2013-06-26 16:15:19 +05:30
|
|
|
end
|
|
|
|
|
2013-06-26 16:31:51 +05:30
|
|
|
def range_mapping(from, to, length)
|
2013-06-28 15:40:17 +05:30
|
|
|
" <range_mapping origin_begin=\"#{from}\" data_begin=\"#{to}\" length=\"#{length}\" time=\"1\"/>"
|
2013-06-26 16:15:19 +05:30
|
|
|
end
|
|
|
|
|
2013-06-28 17:13:59 +05:30
|
|
|
def this_mappings(opts)
|
|
|
|
opts[:variation].nil? ? opts[:mappings] : ((2 * opts[:mappings] - rand(opts[:variation])) / 2)
|
2013-06-26 21:36:16 +05:30
|
|
|
end
|
|
|
|
|
2013-06-28 17:13:59 +05:30
|
|
|
def xml_metadata(opts)
|
|
|
|
mappings, dev_id, nr_data_blocks, to = [], 0, 0, 0
|
2013-06-26 16:15:19 +05:30
|
|
|
|
2013-06-27 19:06:50 +05:30
|
|
|
0.step(opts[:thins] - 1) do
|
2013-06-28 17:13:59 +05:30
|
|
|
mappings << (m = this_mappings(opts))
|
|
|
|
nr_data_blocks += m
|
2013-06-27 19:06:50 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
puts begin_superblock(opts[:blocksize], nr_data_blocks)
|
2013-06-28 17:13:59 +05:30
|
|
|
mappings.each do |b|
|
2013-06-26 16:31:51 +05:30
|
|
|
if opts[:range]
|
2013-06-28 15:40:17 +05:30
|
|
|
puts begin_device(dev_id, b), range_mapping(0, to, b), end_device
|
2013-06-26 16:31:51 +05:30
|
|
|
else
|
2013-06-28 15:40:17 +05:30
|
|
|
puts begin_device(dev_id, b)
|
|
|
|
0.step(b - 1) { |from| puts single_mapping(from, to + from) }
|
|
|
|
puts end_device
|
2013-06-26 16:15:19 +05:30
|
|
|
end
|
2013-06-28 15:40:17 +05:30
|
|
|
dev_id += 1
|
|
|
|
to += b
|
2013-06-26 16:15:19 +05:30
|
|
|
end
|
|
|
|
puts end_superblock
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------
|
|
|
|
# Main
|
|
|
|
#
|
2013-06-28 17:13:59 +05:30
|
|
|
xml_metadata(parse_command_line(ARGV))
|