Created
July 23, 2011 19:32
-
-
Save msimpson/1101786 to your computer and use it in GitHub Desktop.
A simple script to demonstrate how Raid 5 uses parity to recover data.
This file contains hidden or 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 | |
$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