Skip to content

Instantly share code, notes, and snippets.

@msimpson

msimpson/raid5

Created Jul 23, 2011
Embed
What would you like to do?
A simple script to demonstrate how Raid 5 uses parity to recover data.
#!/usr/bin/env ruby
$drives = {
:a => 'Example 1.',
:b => 'abcdefghijklmnopqrstuvwxyz',
:c => '0123456789',
:d => '3.14159265',
:parity => ''
}
def generate_product(letter = :parity)
drives = $drives.select{ |l| l != letter }
capacity = drives.values.map{ |data| data.length }.max
product = ''
capacity.times do |position|
byte = drives.inject(0) do |sum, drive|
sum ^ drive[1].ljust(capacity, ' ')[position].ord
end
product += byte.chr
end
product.strip
end
def print_drives
$drives.each do |letter, data|
if letter != :parity
printf "%-7s: '%s'\n", letter, data
else
printf "parity : "
p data
puts
end
end
end
puts "\nGenerate parity"
$drives[:parity] = generate_product
print_drives
print "Enter a drive to erase (letter): "
drive = gets.strip.to_sym
puts "\nErase drive '#{drive}'"
$drives[drive] = ''
print_drives
puts "Recover drive '#{drive}'"
$drives[drive] = generate_product drive
print_drives
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.