Created
May 20, 2014 19:59
-
-
Save treydock/cb95089a6b09df3abd10 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'pp' | |
require 'optparse' | |
def parse(args) | |
@options = {} | |
@options['debug'] = false | |
OptionParser.new do |opts| | |
opts.banner = "Usage: #{File.basename(__FILE__)} [options]" | |
opts.separator "" | |
opts.separator "Specific options:" | |
opts.on('--[no-]debug', 'Enable debug output.', | |
" Default: #{@options['debug']}") do |debug| | |
@options['debug'] = debug | |
end | |
opts.on('-h', '--help', 'Display this screen.') do | |
puts opts | |
exit | |
end | |
begin | |
opts.parse!(args) | |
rescue OptionParser::InvalidOption, OptionParser::MissingArgument | |
puts $!.to_s | |
puts opts | |
exit | |
end | |
end | |
@options | |
end | |
def debug_output(msg = "", object = nil, opt = {}) | |
return unless opt['debug'] | |
puts "DEBUG: #{msg}" | |
pp object unless object.nil? | |
end | |
options = parse(ARGV) | |
drives = [] | |
controller_count = `sas2ircu LIST | grep -c -E '^\s+Index'`.strip.to_i | |
debug_output("Controller count: #{controller_count}", nil, options) | |
(0..(controller_count-1)).each do |n| | |
output = `sas2ircu #{n} DISPLAY` | |
drives_output = output.split(/^$/) | |
debug_output("Drives outout:\n", drives_output, options) | |
drives_output.each do |output| | |
d = {} | |
output.each_line do |line| | |
case line | |
when /Slot \#\s+: ([0-9]+)$/ | |
d['slot'] = $1 | |
when /SAS Address\s+:\s(.*)$/ | |
d['address'] = $1.gsub('-', '') | |
when /GUID\s+: (.*)$/ | |
d['guid'] = $1 | |
end | |
end | |
drives << d unless d['guid'].nil? or d['guid'] == 'N/A' | |
end | |
end | |
debug_output("Drives:\n", drives, options) | |
drives.each do |drive| | |
Dir['/dev/disk/by-id/*'].each do |id_dev| | |
if File.basename(id_dev) =~ /wwn-0x#{drive['guid']}$/ | |
drive['name'] = File.basename(File.readlink(id_dev)) | |
Dir['/dev/disk/by-path/*'].each do |path_dev| | |
if File.basename(File.readlink(path_dev)) == drive['name'] | |
drive['path'] = path_dev | |
end | |
end | |
end | |
end | |
end | |
drives.each_with_index do |drive,i| | |
vdev = "%02d" % (i.to_i + 1) | |
puts "alias d#{vdev} #{drive['path']}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment