2013-06-26 16:15:19 +05:30
|
|
|
#!/usr/bin/env ruby
|
|
|
|
#
|
|
|
|
# Copyright (C) 2013 Red Hat, GmbH
|
|
|
|
#
|
|
|
|
# Simple XML metadata creation tool
|
|
|
|
#
|
|
|
|
|
|
|
|
require 'optparse'
|
|
|
|
require 'pathname'
|
|
|
|
|
|
|
|
#----------------------------------------------------------------
|
|
|
|
|
|
|
|
$prg = Pathname.new($0).basename
|
|
|
|
|
|
|
|
def init_units
|
|
|
|
units = {}
|
|
|
|
units[:bytes_per_sector] = 512
|
2013-06-26 18:29:25 +05:30
|
|
|
units[:chars] = "bskKmMgGtTpPeEzZyY"
|
2013-06-26 16:15:19 +05:30
|
|
|
units[:strings] = [ 'bytes', 'sectors',
|
|
|
|
'kilobytes', 'kibibytes', 'megabytes', 'mebibytes',
|
|
|
|
'gigabytes', 'gibibytes', 'terabytes', 'tebibytes',
|
|
|
|
'petabytes', 'pebibytes', 'exabytes', 'ebibytes',
|
|
|
|
'zetabytes', 'zebibytes', 'yottabytes', 'yobibytes' ]
|
|
|
|
units[:factors] = [ 1, units[:bytes_per_sector] ]
|
2013-06-26 18:29:25 +05:30
|
|
|
1.step(8) { |e| units[:factors] += [ 1024**e, 1000**e ] }
|
2013-06-26 16:15:19 +05:30
|
|
|
units
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_index(unit_char, units)
|
|
|
|
unit_char ? units[:chars].index(unit_char) : 1
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_sectors(size, units)
|
|
|
|
a = size.split(/[#{units[:chars]}]/)
|
|
|
|
s = size.to_i.to_s
|
|
|
|
abort "#{$prg} - only one unit character allowed!" if a.length > 1 || size.length - s.length > 1
|
|
|
|
abort "#{$prg} - invalid unit specifier!" if s != a[0]
|
|
|
|
size.to_i * units[:factors][get_index(size[a[0].length], units)] / units[:bytes_per_sector]
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_opts(opts)
|
|
|
|
abort "#{$prg} - 3 arguments required!" if opts.length < 3
|
2013-06-26 21:36:16 +05:30
|
|
|
abort "#{$prg} - block size must be > 0" if opts[:blocksize] <= 0
|
2013-06-26 16:15:19 +05:30
|
|
|
abort "#{$prg} - size must be much greater/equal blocksize" if opts[:thinsize] < opts[:blocksize]
|
|
|
|
abort "#{$prg} - number of thin provisioned devices or snapshots must be > 0" if opts[:thins].nil? || opts[:thins] == 0
|
2013-06-26 21:36:16 +05:30
|
|
|
abort "#{$prg} - size variation too large!" if !opts[:variation].nil? && (opts[:variation] > 2 * (opts[:thinsize] - opts[:blocksize]))
|
2013-06-26 16:15:19 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def parse_command_line(argv, units)
|
|
|
|
opts = {}
|
|
|
|
|
|
|
|
os = OptionParser.new do |o|
|
|
|
|
o.banner = "Thin Provisioning XML Test Metadata Generator.\nUsage: #{$prg} [opts]"
|
|
|
|
o.on("-b", "--block-size BLOCKSIZE[#{units[:chars]}]", String,
|
|
|
|
"Block size of thin provisioned devices.") do |bs|
|
|
|
|
opts[:blocksize] = to_sectors(bs, units)
|
|
|
|
end
|
|
|
|
o.on("-s", "--thin-size SIZE[#{units[:chars]}]", String, "Average size of thin devices and snapshots.") do |sz|
|
|
|
|
opts[:thinsize] = to_sectors(sz, units)
|
|
|
|
end
|
|
|
|
o.on("-r", "--range-mappings", "Create range mappings") do
|
|
|
|
opts[:range] = true
|
|
|
|
end
|
|
|
|
o.on("-t", "--thins #THINS", Integer, "Sum of thin devices and snapshots.") do |mt|
|
|
|
|
opts[:thins] = mt
|
|
|
|
end
|
2013-06-26 21:36:16 +05:30
|
|
|
o.on("-v", "--size-variation SIZE[#{units[:chars]}]", String, "Size variation of thin devices and snapshots.") do |sv|
|
|
|
|
opts[:variation] = to_sectors(sv, units)
|
|
|
|
end
|
2013-06-26 16:15:19 +05:30
|
|
|
o.on("-h", "--help", "Output this help.") do
|
|
|
|
puts o
|
|
|
|
exit
|
|
|
|
end
|
|
|
|
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)
|
|
|
|
"<superblock uuid=\"\" time=\"0\" transaction=\"0\" data_block_size=\"#{blocksize}\" nr_data_blocks=\"#{nr_data_blocks}\">\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
def end_superblock
|
|
|
|
"</superblock>\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
def begin_device(devid, size)
|
2013-06-27 18:34:57 +05:30
|
|
|
" <device dev_id=\"#{devid}\" mapped_blocks=\"#{size}\" transaction=\"0\" creation_time=\"0\" snap_time=\"0\">\n"
|
2013-06-26 16:15:19 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def end_device
|
|
|
|
" </device>\n"
|
|
|
|
end
|
|
|
|
|
2013-06-26 19:32:05 +05:30
|
|
|
def single_mapping(from, to)
|
2013-06-26 16:15:19 +05:30
|
|
|
" <single_mapping time=\"0\" origin_block=\"#{from}\" data_block=\"#{to}\"/>\n"
|
|
|
|
end
|
|
|
|
|
2013-06-26 16:31:51 +05:30
|
|
|
def range_mapping(from, to, length)
|
2013-06-26 17:52:03 +05:30
|
|
|
" <range_mapping time=\"0\" origin_begin=\"#{from}\" data_begin=\"#{to}\" length=\"#{length}\"/>\n"
|
2013-06-26 16:15:19 +05:30
|
|
|
end
|
|
|
|
|
2013-06-26 21:36:16 +05:30
|
|
|
def blocks(opts)
|
|
|
|
opts[:thinsize] / opts[:blocksize]
|
|
|
|
end
|
|
|
|
|
|
|
|
def this_blocks(opts)
|
|
|
|
opts[:variation].nil? ? blocks(opts) : (2 * opts[:thinsize] - rand(opts[:variation])) / 2 / opts[:blocksize]
|
|
|
|
end
|
|
|
|
|
2013-06-26 16:15:19 +05:30
|
|
|
def xml_metadata(opts, units)
|
2013-06-26 21:36:16 +05:30
|
|
|
to = 0
|
2013-06-26 16:15:19 +05:30
|
|
|
|
2013-06-26 21:36:16 +05:30
|
|
|
puts begin_superblock(opts[:blocksize], 2 * opts[:thins] * blocks(opts))
|
|
|
|
0.step(opts[:thins] - 1) do |dev_id|
|
|
|
|
b = this_blocks(opts)
|
|
|
|
puts begin_device(dev_id, b)
|
2013-06-26 16:31:51 +05:30
|
|
|
if opts[:range]
|
2013-06-26 21:36:16 +05:30
|
|
|
puts range_mapping(0, to, b)
|
|
|
|
to += b
|
2013-06-26 16:31:51 +05:30
|
|
|
else
|
2013-06-26 21:36:16 +05:30
|
|
|
0.step(b - 1) do |from|
|
2013-06-26 16:31:51 +05:30
|
|
|
puts single_mapping(from, to)
|
|
|
|
to += 1
|
|
|
|
end
|
2013-06-26 16:15:19 +05:30
|
|
|
end
|
|
|
|
puts end_device
|
|
|
|
end
|
|
|
|
puts end_superblock
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------
|
|
|
|
# Main
|
|
|
|
#
|
|
|
|
xml_metadata(parse_command_line(ARGV, (units = init_units)), units)
|